From cb2d10341a11a69cddd266846bfecba7bf23460e Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Tue, 2 Mar 2021 16:19:34 +0000 Subject: [PATCH] Add set_wif to test frameworks's ECKey --- test/functional/test_framework/key.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/functional/test_framework/key.py b/test/functional/test_framework/key.py index 912c0ca978..99dcbcf6c9 100644 --- a/test/functional/test_framework/key.py +++ b/test/functional/test_framework/key.py @@ -8,6 +8,8 @@ keys, and is trivially vulnerable to side channel attacks. Do not use for anything but tests.""" import random +from .address import base58_to_byte + def modinv(a, n): """Compute the modular inverse of a modulo n @@ -337,6 +339,13 @@ class ECKey(): self.secret = secret self.compressed = compressed + def set_wif(self, wif): + (b, v) = base58_to_byte(wif) + if len(b) != 32 and len(b) != 33: + raise ValueError("invalid WIF: unexpected length {}".format(len(b))) + compressed = len(b) == 33 + self.set(b[0:32], compressed) + def generate(self, compressed=True): """Generate a random private key (compressed or uncompressed).""" self.set(random.randrange(1, SECP256K1_ORDER).to_bytes(32, 'big'), compressed)