def test_extkey(self): xprv_str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' xprv = CBitcoinExtKey(xprv_str) xprv2 = CExtKey(xprv) self.assertEqual(xprv_str, str(CBitcoinExtKey.from_bytes(xprv2))) self.assertEqual(bytes(xprv.derive(BIP32_HARDENED_KEY_LIMIT)), bytes(xprv2.derive(BIP32_HARDENED_KEY_LIMIT))) self.assertEqual(str(xprv.neuter()), str(CBitcoinExtPubKey.from_bytes(xprv2.neuter())))
def test_invalid_xprivkey(self): invalid_xpriv_str = 'xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFAzHGBP2UuGCqWLTAPLcMtD9y5gkZ6Eq3Rjuahrv17fENZ3QzxW' with self.assertRaises(ValueError): CBitcoinExtKey(invalid_xpriv_str) valid_xprivkey = CBitcoinExtKey('xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9') with self.assertRaises(ValueError): CExtKey(valid_xprivkey[:-1]) # short length with self.assertRaises(ValueError): CExtKey(valid_xprivkey + b'\x00') # long length
def test_standard_bip32_vectors(self): for vector in BIP32_TEST_VECTORS: _, seed = vector[0] key = CBitcoinExtKey.from_seed(x(seed)) for xpub, xpriv, child_num in vector[1:]: self.assertEqual(xpub, str(key.neuter())) self.assertEqual(xpriv, str(key)) key = key.derive(child_num)
def test_invalid_derivation(self): xpriv = CBitcoinExtKey( 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' ) with self.assertRaises(ValueError): xpriv.derive(1 << 32) final_xpriv_str = 'xprvJ9DiCzes6yvKjEy8duXR1Qg6Et6CBmrR4yFJvnburXG4X6VnKbNxoTYhvVdpsxkjdXwX3D2NJHFCAnnN1DdAJCVQitnFbFWv3fL3oB2BFo4' for _ in range(255): xpriv = xpriv.derive(0) self.assertEqual(str(CBitcoinExtKey.from_bytes(xpriv)), final_xpriv_str) with self.assertRaises(ValueError): xpriv.derive(0) # depth > 255
def test_standard_bip32_vectors(self): for vector in BIP32_TEST_VECTORS: _, seed = vector[0] base_key = CBitcoinExtKey.from_seed(x(seed)) key = base_key path = [] for xpub, xpriv, child_num in vector[1:]: self.assertEqual(xpub, str(key.neuter())) self.assertEqual(xpriv, str(key)) key = key.derive(child_num) path.append(child_num) key_from_path = base_key.derive_path(str(BIP32Path(path))) self.assertEqual(key, key_from_path)
def test_standard_bip32_vectors(self) -> None: for vector in BIP32_TEST_VECTORS: _, seed, _ = vector[0] base_key = CBitcoinExtKey.from_seed(x(seed)) self.assertEqual(base_key.parent_fp, b'\x00\x00\x00\x00') key = base_key path = [] for xpub, xpriv, child_num in vector[1:]: self.assertEqual(xpub, str(key.neuter())) self.assertEqual(xpriv, str(key)) parent_fp = key.fingerprint key = key.derive(child_num) self.assertEqual(key.parent_fp, parent_fp) path.append(child_num) key_from_path = base_key.derive_path(str(BIP32Path(path))) self.assertEqual(key, key_from_path)
import os import sys from bitcointx.core import b2x from bitcointx.core.key import BIP32Path from bitcointx.base58 import Base58Error, UnexpectedBase58PrefixError from bitcointx.wallet import CBitcoinExtKey, CBitcoinExtPubKey if __name__ == '__main__': if len(sys.argv) < 2 or len(sys.argv) > 3: print("usage: {} <derivation_path> [xpriv_or_xpub]".format( sys.argv[0])) sys.exit(-1) if len(sys.argv) == 2: xkey = CBitcoinExtKey.from_seed(os.urandom(32)) print("generated xpriv: ", xkey) else: for cls in (CBitcoinExtKey, CBitcoinExtPubKey): try: xkey = cls(sys.argv[2]) break except UnexpectedBase58PrefixError: pass except Base58Error: print("ERROR: specified key is incorrectly encoded") sys.exit(-1) except ValueError: pass else: print("ERROR: specified key does not appear to be valid")
def test_from_xpriv(self): xpriv_str = 'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt' xpriv = CBitcoinExtKey(xpriv_str) self.assertEqual(xpriv_str, str(CBitcoinExtKey.from_bytes(xpriv)))
def T(base58_xprivkey, expected_hex_xprivkey): key = CBitcoinExtKey(base58_xprivkey) self.assertEqual(b2x(key), expected_hex_xprivkey)
def T(base58_xprivkey: str, expected_hex_xprivkey: str) -> None: key = CBitcoinExtKey(base58_xprivkey) self.assertEqual(b2x(key), expected_hex_xprivkey) key2 = CBitcoinExtKey.from_bytes(x(expected_hex_xprivkey)) self.assertEqual(b2x(key), b2x(key2))