def test_roundtrip_wif(self): k1 = PrivateKey.generate() k2 = PrivateKey.from_wif(k1.to_wif()) assert k1.key.private_numbers().private_value == k2.key.private_numbers().private_value assert k1.network is k2.network assert k1.compressed == k2.compressed
def test_roundtrip_wif(self): k1 = PrivateKey() k2 = PrivateKey.from_wif(k1.to_wif()) assert k1.secret == k2.secret assert k1.network is k2.network assert k1.compressed == k2.compressed
def test_from_private_key(self): privkey = PrivateKey.from_hex(data['privkey_hex']) pubkey = PublicKey.from_private_key(privkey) assert pubkey.network is privkey.network assert pubkey.compressed == privkey.compressed assert pubkey.pair == data['pubkey_pair']
def test_from_bytes(self): k = PrivateKey.from_bytes(data['privkey_bin']) assert k.compressed is True assert k.network is network.default assert k.to_bytes() == data['privkey_bin']
def test_from_bytes(self): k = PrivateKey.from_bytes(data['privkey_bin']) assert k.to_hex() == data['privkey_hex'] assert k.to_bytes() == data['privkey_bin'] assert k.compressed is True assert k.network is networks.default
def test_from_invalid_wif(self): too_short = encode_base58h(b'a') too_long = encode_base58h(b'a' * 30) with raises(PrivateKey.InvalidWifLength): PrivateKey.from_wif(too_short) with raises(PrivateKey.InvalidWifLength): PrivateKey.from_wif(too_long) valid = decode_base58h(PrivateKey().to_wif()) with raises(PrivateKey.InvalidCompressionByte): PrivateKey.from_wif(encode_base58h(valid[:-1] + b'a')) with raises(PrivateKey.UnknownNetwork): PrivateKey.from_wif(encode_base58h(b'a' + valid[1:]))
def test_bitcoind_valid_wifs(self, valid_wifs): for wif, secret_hex, attrs in valid_wifs: secret = encoding.b2i_bigendian(encoding.a2b_hex(secret_hex)) network_ = network.testnet if attrs['isTestnet'] else network.livenet compressed = attrs['isCompressed'] k = PrivateKey.from_wif(wif) assert k.key.private_numbers().private_value == secret assert k.network is network_ assert k.compressed == compressed
def test_bitcoind_valid_wifs(self, valid_wifs): for wif, secret_hex, attrs in valid_wifs: secret = decode_int(decode_hex(secret_hex)) network = networks.testnet if attrs['isTestnet'] else networks.livenet compressed = attrs['isCompressed'] k = PrivateKey.from_wif(wif) assert k.secret == secret assert k.network is network assert k.compressed == compressed
def test_bitcoind_valid_wifs(self, valid_wifs): for wif, secret_hex, attrs in valid_wifs: secret = decode_int(decode_hex(secret_hex)) network = networks.testnet if attrs[ 'isTestnet'] else networks.livenet compressed = attrs['isCompressed'] k = PrivateKey.from_wif(wif) assert k.secret == secret assert k.network is network assert k.compressed == compressed
def test_from_invalid_wif(self): too_short = encode_base58h('a') too_long = encode_base58h('a' * 30) with raises(PrivateKey.InvalidWifLength): PrivateKey.from_wif(too_short) with raises(PrivateKey.InvalidWifLength): PrivateKey.from_wif(too_long) valid = decode_base58h(PrivateKey().to_wif()) with raises(PrivateKey.InvalidCompressionByte): PrivateKey.from_wif(encode_base58h(valid[:-1] + 'a')) with raises(PrivateKey.UnknownNetwork): PrivateKey.from_wif(encode_base58h('a' + valid[1:]))
def test_from_invalid_wif(self): too_short = encoding.b2a_base58check('a') with raises(privkey.InvalidEncoding): PrivateKey.from_wif(too_short) too_long = encoding.b2a_base58check('a' * 30) with raises(privkey.InvalidEncoding): PrivateKey.from_wif(too_long) valid = encoding.a2b_base58check(PrivateKey.generate().to_wif()) with raises(privkey.InvalidEncoding): PrivateKey.from_wif(encoding.b2a_base58check(valid[:-1] + 'a')) with raises(privkey.InvalidEncoding): PrivateKey.from_wif(encoding.b2a_base58check('a' + valid[1:]))
def test_from_wif_test_uncompress(self): k = PrivateKey.from_wif(data['wif']['test_uncompress']) assert k.compressed is False assert k.network is networks.testnet assert k.to_wif() == data['wif']['test_uncompress']
def test_from_wif_test_compress(self): k = PrivateKey.from_wif(data['wif']['test_compress']) assert k.compressed is True assert k.network is networks.testnet assert k.to_wif() == data['wif']['test_compress']
def test_from_invalid_secret_exponent(self, invalid_exponents): for exponent in invalid_exponents: with raises(privkey.InvalidExponent): PrivateKey.from_secret_exponent(exponent)
def test_invalid_secret(self): with raises(PrivateKey.InvalidSecret): PrivateKey(-1) with raises(PrivateKey.InvalidSecret): PrivateKey(10**100)
def test_to_pubkey_compressed(self): k = PrivateKey.from_wif(data['wif']['live_uncompress']) assert k.to_public_key().to_hex() == data['pubkey']['uncompress_hex']
def test_bitcoind_invalid_wifs(self, invalid_wifs): for invalid_wif in invalid_wifs: with raises(PrivateKey.Error): PrivateKey.from_wif(invalid_wif)
def test_from_wif_live_uncompress(self): k = PrivateKey.from_wif(data['wif']['live_uncompress']) assert k.compressed is False assert k.network is networks.livenet assert k.to_wif() == data['wif']['live_uncompress']
def test_from_invalid_bytes(self): with raises(privkey.InvalidEncoding): PrivateKey.from_bytes('a') with raises(privkey.InvalidEncoding): PrivateKey.from_bytes('a' * 33)
def test_generate_random_keys(self): k1 = PrivateKey.generate() k2 = PrivateKey.generate() assert k1.key.private_numbers() != k2.key.private_numbers()
def test_to_address_test_uncompressed(self): k = PrivateKey.from_wif(data['wif']['test_uncompress']) assert k.to_address().to_string() == data['address']['test_uncompress']
def test_from_invalid_hex(self): with raises(PrivateKey.InvalidHex): PrivateKey.from_hex('a') with raises(PrivateKey.InvalidHex): PrivateKey.from_hex('a@')
def test_from_random(self): k1, k2 = PrivateKey(), PrivateKey() assert k1.secret != k2.secret
def test_from_invalid_bytes(self): with raises(PrivateKey.InvalidBinaryLength): PrivateKey.from_bytes('a') with raises(PrivateKey.InvalidBinaryLength): PrivateKey.from_bytes('a' * 33)
def test_invalid_network(self): with raises(PrivateKey.UnknownNetwork): PrivateKey(network=-1)
def test_from_secret_exponent(self, valid_exponents): for exponent in valid_exponents: k = PrivateKey.from_secret_exponent(exponent) assert k.key.private_numbers().private_value == exponent
def test_from_wif_live_compress(self): k = PrivateKey.from_wif(data['wif']['live_compress']) assert k.compressed is True assert k.network is networks.livenet assert k.to_wif() == data['wif']['live_compress']
def test_bitcoind_invalid_wifs(self, invalid_wifs): for invalid_wif in invalid_wifs: with raises(privkey.InvalidEncoding): PrivateKey.from_wif(invalid_wif)