def privkey_to_wif(cls, priv): # refuse to WIF-ify something that we don't recognize # as a private key; ignoring the return value of this # function as we only want to raise whatever Exception # it does: btc.read_privkey(priv) return btc.bin_to_b58check(priv, cls.WIF_PREFIX)
def test_wif_privkeys_invalid(): #first try to create wif privkey from key of wrong length bad_privs = [b'\x01\x02' * 17] #some silly private key but > 33 bytes #next try to create wif with correct length but wrong compression byte bad_privs.append(b'\x07' * 32 + b'\x02') for priv in bad_privs: with pytest.raises(Exception) as e_info: fake_wif = btc.wif_compressed_privkey( binascii.hexlify(priv).decode('ascii')) #Create a wif with wrong length bad_wif1 = btc.bin_to_b58check(b'\x01\x02' * 34, b'\x80') #Create a wif with wrong compression byte bad_wif2 = btc.bin_to_b58check(b'\x07' * 33, b'\x80') for bw in [bad_wif1, bad_wif2]: with pytest.raises(Exception) as e_info: fake_priv = btc.from_wif_privkey(bw) #Some invalid b58 from bitcoin repo; #none of these are valid as any kind of key or address with open(os.path.join(testdir, "base58_keys_invalid.json"), "r") as f: json_data = f.read() invalid_key_list = json.loads(json_data) for k in invalid_key_list: bad_key = k[0] for netval in ["mainnet", "testnet"]: #if using pytest -s ; sanity check to see what's actually being tested print('testing this key: ' + bad_key) #should throw exception with pytest.raises(Exception) as e_info: from_wif_key = btc.from_wif_privkey( bad_key, btc.get_version_byte(bad_key)) #in case the b58 check encoding is valid, we should #also check if the leading version byte is in the #expected set, and throw an error if not. if chr(btc.get_version_byte(bad_key)) not in b'\x80\xef': raise Exception("Invalid version byte")
def privkey_to_wif(cls, privkey_locktime): priv, locktime = privkey_locktime return btc.bin_to_b58check(priv, cls.WIF_PREFIX)
def privkey_to_wif(cls, priv): return btc.bin_to_b58check(priv, cls.WIF_PREFIX)