Skip to content

Commit

Permalink
Add set_wif to test frameworks's ECKey
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Mar 2, 2021
1 parent af1bef5 commit cb2d103
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/functional/test_framework/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
anything but tests."""
import random

from .address import base58_to_byte

def modinv(a, n):
"""Compute the modular inverse of a modulo n
Expand Down Expand Up @@ -337,6 +339,13 @@ def set(self, secret, compressed):
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)
Expand Down

0 comments on commit cb2d103

Please sign in to comment.