Add set_wif to test frameworks's ECKey

This commit is contained in:
Steven Roose 2021-03-02 16:19:34 +00:00
parent af1bef5c01
commit cb2d10341a
No known key found for this signature in database
GPG key ID: 2F2A88D7F8D68E87

View file

@ -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)