def test_slip132() -> None: # xkey is not a public one xprv = b"xprv9s21ZrQH143K2ZP8tyNiUtgoezZosUkw9hhir2JFzDhcUWKz8qFYk3cxdgSFoCMzt8E2Ubi1nXw71TLhwgCfzqFHfM5Snv4zboSebePRmLS" err_msg = "not a public key: " with pytest.raises(BTClibValueError, match=err_msg): slip132.address_from_xpub(xprv) address = slip132.address_from_xkey(xprv) xpub = bip32.xpub_from_xprv(xprv) address2 = slip132.address_from_xpub(xpub) assert address == address2
def test_slip132() -> None: mnemonic = "enough regret erode news field main wild jar erupt bronze velvet ugly" mxprv = bip39.mxprv_from_mnemonic(mnemonic) xprv = slip132.p2pkh_xkey(mxprv) assert ( xprv == "xprv9y7Yxxyy7wn5ktGVzAmgatW1vu7daX4V8ddWMcSbKJyK6TzkBbZDimAMCLoogpf4GEp2ThmBZ476vwe7xVC9sPsNfJcyu5isQixgF95HS31" ) with pytest.raises(BTClibValueError, match="not a public key: "): slip132.address_from_xpub(xprv) xpub = bip32.xpub_from_xprv(xprv) assert ( xpub == "xpub6C6uNUWrxKLNyNLy6CJgx2SkUvx7yynLVrZ79zrCseWHyGKtj8sUGZUq3dw9fqJGETSEeX1iztXAfRvxh6Gk2m7yVjDCx5cbRP2So559Hb5" ) address = slip132.address_from_xpub(xpub) assert slip132.address_from_xkey(xprv) == address assert address == "1DjfiAgNyvRXhYXiDgE9K7bfB82hVUPTm1" with pytest.raises(BTClibValueError, match="not a root key: "): slip132.p2pkh_xkey(xprv) yprv = slip132.p2wpkh_p2sh_xkey(mxprv) assert ( yprv == "yprvAJstKLzg5g8RzxfPiX4UT5vH6y5FJkK2yVNkxrzG92WagSCu2PnAxT34JPGCT9Wh5LJCXZi1wB7fd6FUA9veMf2kig7A6cTxD4GMjSbcrqv" ) with pytest.raises(BTClibValueError, match="not a public key: "): slip132.address_from_xpub(yprv) ypub = bip32.xpub_from_xprv(yprv) assert ( ypub == "ypub6XsEirXZv3gjDSjrpYbUpDs1ezujiD2tLiJMmFPshN3ZZEY3Zw6RWFMY9grBtrN88Qan7FAVmtPQnLfuepRy7ZVDNQkkDTMGzYoCJpQdwt4" ) address = slip132.address_from_xpub(ypub) assert slip132.address_from_xkey(yprv) == address assert address == "3QnhAKhuuwSf2bEFKpgvDKUHNe3rpZr6PG" with pytest.raises(BTClibValueError, match="not a root key: "): slip132.p2wpkh_p2sh_xkey(xprv) zprv = slip132.p2wpkh_xkey(mxprv) assert ( zprv == "zprvAceWp8rDBfL7EJ4Mz5p76v6ZmFQ3UyftmmHqfdUidHoTPoFwjApHYCXPhdWNsxLv6ozzauk8LQXNjNn2CcxLaLzezH7QTQ3rk9tn8GmzaXT" ) with pytest.raises(BTClibValueError, match="not a public key: "): slip132.address_from_xpub(zprv) zpub = bip32.xpub_from_xprv(zprv) assert ( zpub == "zpub6qdsDeP722tQSn8q67M7U43JKHEXtSPk8zDSU1tLBdLSGbb6Gi8Y5zqsYuYPhxprjsxc4ZdqvjC86iuv1SXmmvCkJgZJmoCPbDZdHwUaKbX" ) address = slip132.address_from_xpub(zpub) assert slip132.address_from_xkey(zprv) == address assert address == "bc1qcne7y6yae0lz3kceg80aunmafu0rwm3uzmf7v6" with pytest.raises(BTClibValueError, match="not a root key: "): slip132.p2wpkh_xkey(xprv)
def test_slip132_test_vectors() -> None: """SLIP132 test vector https://github.com/satoshilabs/slips/blob/master/slip-0132.md """ mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" kpath = "m/0/0" test_vectors: List[Tuple[bytes, str, str, str, str]] = [ ( NETWORKS["mainnet"].bip32_prv, "m / 44h / 0h / 0h", "xprv9xpXFhFpqdQK3TmytPBqXtGSwS3DLjojFhTGht8gwAAii8py5X6pxeBnQ6ehJiyJ6nDjWGJfZ95WxByFXVkDxHXrqu53WCRGypk2ttuqncb", "xpub6BosfCnifzxcFwrSzQiqu2DBVTshkCXacvNsWGYJVVhhawA7d4R5WSWGFNbi8Aw6ZRc1brxMyWMzG3DSSSSoekkudhUd9yLb6qx39T9nMdj", "1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA", ), ( NETWORKS["mainnet"].slip132_p2wpkh_p2sh_prv, "m / 49h / 0h / 0h", "yprvAHwhK6RbpuS3dgCYHM5jc2ZvEKd7Bi61u9FVhYMpgMSuZS613T1xxQeKTffhrHY79hZ5PsskBjcc6C2V7DrnsMsNaGDaWev3GLRQRgV7hxF", "ypub6Ww3ibxVfGzLrAH1PNcjyAWenMTbbAosGNB6VvmSEgytSER9azLDWCxoJwW7Ke7icmizBMXrzBx9979FfaHxHcrArf3zbeJJJUZPf663zsP", "37VucYSaXLCAsxYyAPfbSi9eh4iEcbShgf", ), ( NETWORKS["mainnet"].slip132_p2wpkh_prv, "m / 84h / 0h / 0h", "zprvAdG4iTXWBoARxkkzNpNh8r6Qag3irQB8PzEMkAFeTRXxHpbF9z4QgEvBRmfvqWvGp42t42nvgGpNgYSJA9iefm1yYNZKEm7z6qUWCroSQnE", "zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs", "bc1qcr8te4kr609gcawutmrza0j4xv80jy8z306fyu", ), ] for version, der_path, prv, pub, addr in test_vectors: rxprv = bip39.mxprv_from_mnemonic(mnemonic, "") mxprv = bip32.derive(rxprv, der_path, version) assert prv == mxprv mxpub = bip32.xpub_from_xprv(mxprv) assert pub == mxpub xpub = bip32.derive(mxpub, kpath) address = slip132.address_from_xpub(xpub) assert addr == address address = slip132.address_from_xkey(xpub) assert addr == address xprv = bip32.derive(mxprv, kpath) address = slip132.address_from_xkey(xprv) assert addr == address if version == NETWORKS["mainnet"].bip32_prv: address = b58.p2pkh(xpub) assert addr == address address = b58.p2pkh(xprv) assert addr == address elif version == NETWORKS["mainnet"].slip132_p2wpkh_p2sh_prv: address = b58.p2wpkh_p2sh(xpub) assert addr == address address = b58.p2wpkh_p2sh(xprv) assert addr == address elif version == NETWORKS["mainnet"].slip132_p2wpkh_prv: address = b32.p2wpkh(xpub) assert addr == address address = b32.p2wpkh(xprv) assert addr == address
def test_p2pkh_from_wif() -> None: seed = b"\x00" * 32 # better be a documented test case rxprv = bip32.rootxprv_from_seed(seed) path = "m/0h/0h/12" xprv = bip32.derive(rxprv, path) wif = b58.wif_from_prv_key(xprv) assert wif == "L2L1dqRmkmVtwStNf5wg8nnGaRn3buoQr721XShM4VwDbTcn9bpm" pub_key, _ = pub_keyinfo_from_prv_key(wif) address = b58.p2pkh(pub_key) xpub = bip32.xpub_from_xprv(xprv) assert address == slip132.address_from_xpub(xpub) err_msg = "not a private key: " with pytest.raises(BTClibValueError, match=err_msg): b58.wif_from_prv_key(xpub)