def test_child(self): seed = b"[email protected] Jimmy Song" tests = ( ( "testnet", "tb1qu6mnnk54hxfhy4aj58v0w6e7q8hghtv8wcdl7g", "tb1qscu8evdlqsucj7p84xwnrf63h4jsdr5yqga8zq", ), ( "signet", "tb1qu6mnnk54hxfhy4aj58v0w6e7q8hghtv8wcdl7g", "tb1qscu8evdlqsucj7p84xwnrf63h4jsdr5yqga8zq", ), ( "mainnet", "bc1qu6mnnk54hxfhy4aj58v0w6e7q8hghtv8y7kv9m", "bc1qscu8evdlqsucj7p84xwnrf63h4jsdr5y2wx5en", ), ) for network, want1, want2 in tests: priv = HDPrivateKey.from_seed(seed, network=network) pub = priv.pub addr = priv.child(0).p2wpkh_address() self.assertEqual(addr, want1) addr = pub.child(0).p2wpkh_address() self.assertEqual(addr, want1) addr = priv.child(0x80000002).p2wpkh_address() self.assertEqual(addr, want2) with self.assertRaises(ValueError): pub.child(0x80000002)
def test_from_seed(self): seed = b"[email protected] Jimmy Song" tests = { "testnet": "tb1q7kn55vf3mmd40gyj46r245lw87dc6us5n50lrg", "signet": "tb1q7kn55vf3mmd40gyj46r245lw87dc6us5n50lrg", "mainnet": "bc1q7kn55vf3mmd40gyj46r245lw87dc6us5ej5vcm", } for network, want in tests.items(): priv = HDPrivateKey.from_seed(seed, network=network) addr = priv.p2wpkh_address() self.assertEqual(addr, want)
def test_traverse(self): seed = b"[email protected] Jimmy Song" tests = ( ("testnet", "tb1q423gz8cenqt6vfw987vlyxql0rh2jgh4sy0tue"), ("signet", "tb1q423gz8cenqt6vfw987vlyxql0rh2jgh4sy0tue"), ("mainnet", "bc1q423gz8cenqt6vfw987vlyxql0rh2jgh46z5c82"), ) for network, want in tests: priv = HDPrivateKey.from_seed(seed, network=network) pub = priv.pub path = "m/1/2/3/4" self.assertEqual( priv.traverse(path).p2wpkh_address(), pub.traverse(path).p2wpkh_address(), ) path = "m/0/1'/2/3'" self.assertEqual( priv.traverse(path).p2wpkh_address(), want, )
def test_prv_pub(self): tests = [ { "seed": bytes.fromhex("000102030405060708090a0b0c0d0e0f"), "paths": [ [ "m", "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8", "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi", ], [ "m/0'", "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw", "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7", ], [ "m/0'/1", "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ", "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs", ], [ "m/0'/1/2'", "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5", "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM", ], [ "m/0'/1/2'/2", "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV", "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334", ], [ "m/0'/1/2'/2/1000000000", "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy", "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76", ], ], }, { "seed": bytes.fromhex( "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542" ), "paths": [ [ "m", "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB", "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U", ], [ "m/0", "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH", "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt", ], [ "m/0/2147483647'", "xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a", "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9", ], [ "m/0/2147483647'/1", "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon", "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef", ], [ "m/0/2147483647'/1/2147483646'", "xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL", "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc", ], [ "m/0/2147483647'/1/2147483646'/2", "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt", "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j", ], ], }, { "seed": bytes.fromhex( "4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be" ), "paths": [ [ "m", "xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13", "xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6", ], [ "m/0'", "xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y", "xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L", ], ], }, ] for test in tests: seed = test["seed"] for path, xpub, xprv in test["paths"]: # test from seed private_key = HDPrivateKey.from_seed(seed).traverse(path) public_key = HDPublicKey.parse(xpub) self.assertEqual(private_key.xprv(), xprv) self.assertEqual(private_key.xpub(), public_key.xpub()) self.assertEqual(private_key.address(), public_key.address())
def test_get_address(self): seedphrase = b"[email protected] Jimmy Song" mainnet_priv = HDPrivateKey.from_seed(seedphrase) testnet_priv = HDPrivateKey.from_seed(seedphrase, network="testnet") signet_priv = HDPrivateKey.from_seed(seedphrase, network="signet") tests = [ [ mainnet_priv.get_p2pkh_receiving_address, 0, 1, "13pS51XfGTVhxbtrGKVSvwf36r96tLUu1K", ], [ testnet_priv.get_p2pkh_change_address, 1, 0, "n4EiCRsEEPaJ73HWA6zYEaHwo45BrP5MHb", ], [ signet_priv.get_p2pkh_change_address, 1, 0, "n4EiCRsEEPaJ73HWA6zYEaHwo45BrP5MHb", ], [ testnet_priv.get_p2sh_p2wpkh_receiving_address, 0, 2, "2NGKoo11UopXBWLC7qqj9BjgH9F3gvLdapz", ], [ signet_priv.get_p2sh_p2wpkh_receiving_address, 0, 2, "2NGKoo11UopXBWLC7qqj9BjgH9F3gvLdapz", ], [ mainnet_priv.get_p2sh_p2wpkh_change_address, 0, 0, "38hYFPLMTykhURpCQTxkdDcpQKyieiYiU7", ], [ mainnet_priv.get_p2wpkh_receiving_address, 2, 0, "bc1qzeln78k9sghatd3uwnks8jek46qe23dw99zu9j", ], [ testnet_priv.get_p2wpkh_change_address, 1, 1, "tb1qecjwdw5uwwdfezzntec7m4kc8zkyjcamlz7dv9", ], [ signet_priv.get_p2wpkh_change_address, 1, 1, "tb1qecjwdw5uwwdfezzntec7m4kc8zkyjcamlz7dv9", ], ] for function, account, address, want in tests: got = function(account, address) self.assertEqual(got, want)