def test_xpub_version(self): mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" hd_obj = HDPrivateKey.from_mnemonic(mnemonic, network="testnet") tprv = "tprv8ZgxMBicQKsPe5YMU9gHen4Ez3ApihUfykaqUorj9t6FDqy3nP6eoXiAo2ssvpAjoLroQxHqr3R5nE3a5dU3DHTjTgJDd7zrbniJr6nrCzd" tpub = "tpubD6NzVbkrYhZ4XYa9MoLt4BiMZ4gkt2faZ4BcmKu2a9te4LDpQmvEz2L2yDERivHxFPnxXXhqDRkUNnQCpZggCyEZLBktV7VaSmwayqMJy1s" self.assertEqual(hd_obj.xprv(), tprv) self.assertEqual(hd_obj.xpub(), tpub) hd_obj = HDPrivateKey.from_mnemonic(mnemonic, network="signet") self.assertEqual(hd_obj.xprv(), tprv) self.assertEqual(hd_obj.xpub(), tpub) # parse this same HDPrivateKey from the tprv (assume we no longer have the mnemonic) recreated_obj = HDPrivateKey.parse(tprv) self.assertEqual(recreated_obj.xpub(), tpub) # Confirm the version bytes have passed through correctly to the HDPublicKey object: self.assertEqual(recreated_obj.pub.xpub(), tpub) hd_obj = HDPrivateKey.from_mnemonic(mnemonic, network="mainnet") xprv = "xprv9s21ZrQH143K3GJpoapnV8SFfukcVBSfeCficPSGfubmSFDxo1kuHnLisriDvSnRRuL2Qrg5ggqHKNVpxR86QEC8w35uxmGoggxtQTPvfUu" xpub = "xpub661MyMwAqRbcFkPHucMnrGNzDwb6teAX1RbKQmqtEF8kK3Z7LZ59qafCjB9eCRLiTVG3uxBxgKvRgbubRhqSKXnGGb1aoaqLrpMBDrVxga8" self.assertEqual(hd_obj.xprv(), xprv) self.assertEqual(hd_obj.xpub(), xpub) # parse this same HDPrivateKey from the xprv (assume we no longer have the mnemonic) recreated_obj = HDPrivateKey.parse(xprv) self.assertEqual(recreated_obj.xpub(), xpub) # Confirm the version bytes have passed through correctly to the HDPublicKey object: self.assertEqual(recreated_obj.pub.xpub(), xpub)
def test_parse(self): xpub = "xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13" hd_pub = HDPublicKey.parse(xpub) self.assertEqual(hd_pub.xpub(), xpub) xprv = "xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6" hd_priv = HDPrivateKey.parse(xprv) self.assertEqual(hd_priv.xprv(), xprv)
def test_zprv(self): mnemonic, priv = HDPrivateKey.generate(extra_entropy=1 << 128) for word in mnemonic.split(): self.assertTrue(word in BIP39) zprv = priv.xprv(version=bytes.fromhex("04b2430c")) self.assertTrue(zprv.startswith("zprv")) zpub = priv.pub.xpub(version=bytes.fromhex("04b24746")) self.assertTrue(zpub.startswith("zpub")) derived = HDPrivateKey.parse(zprv) self.assertEqual(zprv, derived.xprv(bytes.fromhex("04b2430c"))) mnemonic, priv = HDPrivateKey.generate(network="testnet") zprv = priv.xprv(bytes.fromhex("045f18bc")) self.assertTrue(zprv.startswith("vprv")) zpub = priv.pub.xpub(bytes.fromhex("045f1cf6")) self.assertTrue(zpub.startswith("vpub")) xpub = priv.pub.xpub(bytes.fromhex("043587cf")) self.assertTrue(xpub.startswith("tpub")) derived = HDPrivateKey.parse(zprv) self.assertEqual(zprv, derived.xprv(bytes.fromhex("045f18bc"))) derived_pub = HDPublicKey.parse(zpub) self.assertEqual(zpub, derived_pub.xpub(bytes.fromhex("045f1cf6"))) mnemonic, priv = HDPrivateKey.generate(network="signet") zprv = priv.xprv(bytes.fromhex("045f18bc")) self.assertTrue(zprv.startswith("vprv")) zpub = priv.pub.xpub(bytes.fromhex("045f1cf6")) self.assertTrue(zpub.startswith("vpub")) xpub = priv.pub.xpub(bytes.fromhex("043587cf")) self.assertTrue(xpub.startswith("tpub")) derived = HDPrivateKey.parse(zprv) self.assertEqual(zprv, derived.xprv(bytes.fromhex("045f18bc"))) derived_pub = HDPublicKey.parse(zpub) self.assertEqual(zpub, derived_pub.xpub(bytes.fromhex("045f1cf6"))) with self.assertRaises(ValueError): bad_zprv = encode_base58_checksum(b"\x00" * 78) HDPrivateKey.parse(bad_zprv) with self.assertRaises(ValueError): bad_zpub = encode_base58_checksum(b"\x00" * 78) HDPublicKey.parse(bad_zpub) with self.assertRaises(ValueError): derived_pub.child(1 << 31)