diff --git a/lib/tests/test_bitcoin.py b/lib/tests/test_bitcoin.py index 712b31da5..d19ea6697 100644 --- a/lib/tests/test_bitcoin.py +++ b/lib/tests/test_bitcoin.py @@ -11,8 +11,9 @@ from lib.bitcoin import ( var_int, op_push, address_to_script, regenerate_key, verify_message, deserialize_privkey, serialize_privkey, is_segwit_address, is_b58_address, address_to_scripthash, is_minikey, is_compressed, is_xpub, - xpub_type, is_xprv, is_bip32_derivation, seed_type) + xpub_type, is_xprv, is_bip32_derivation, seed_type, EncodeBase58Check) from lib.util import bfh +from lib import constants from . import TestCaseForTestnet @@ -259,6 +260,79 @@ class Test_xprv_xpub(unittest.TestCase): self.assertFalse(is_bip32_derivation("")) self.assertFalse(is_bip32_derivation("m/q8462")) + def test_version_bytes(self): + xprv_headers_b58 = { + 'standard': 'xprv', + 'p2wpkh-p2sh': 'yprv', + 'p2wsh-p2sh': 'Yprv', + 'p2wpkh': 'zprv', + 'p2wsh': 'Zprv', + } + xpub_headers_b58 = { + 'standard': 'xpub', + 'p2wpkh-p2sh': 'ypub', + 'p2wsh-p2sh': 'Ypub', + 'p2wpkh': 'zpub', + 'p2wsh': 'Zpub', + } + for xtype, xkey_header_bytes in constants.net.XPRV_HEADERS.items(): + xkey_header_bytes = bfh("%08x" % xkey_header_bytes) + xkey_bytes = xkey_header_bytes + bytes([0] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) + + xkey_bytes = xkey_header_bytes + bytes([255] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) + + for xtype, xkey_header_bytes in constants.net.XPUB_HEADERS.items(): + xkey_header_bytes = bfh("%08x" % xkey_header_bytes) + xkey_bytes = xkey_header_bytes + bytes([0] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) + + xkey_bytes = xkey_header_bytes + bytes([255] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) + + +class Test_xprv_xpub_testnet(TestCaseForTestnet): + + def test_version_bytes(self): + xprv_headers_b58 = { + 'standard': 'tprv', + 'p2wpkh-p2sh': 'uprv', + 'p2wsh-p2sh': 'Uprv', + 'p2wpkh': 'vprv', + 'p2wsh': 'Vprv', + } + xpub_headers_b58 = { + 'standard': 'tpub', + 'p2wpkh-p2sh': 'upub', + 'p2wsh-p2sh': 'Upub', + 'p2wpkh': 'vpub', + 'p2wsh': 'Vpub', + } + for xtype, xkey_header_bytes in constants.net.XPRV_HEADERS.items(): + xkey_header_bytes = bfh("%08x" % xkey_header_bytes) + xkey_bytes = xkey_header_bytes + bytes([0] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) + + xkey_bytes = xkey_header_bytes + bytes([255] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) + + for xtype, xkey_header_bytes in constants.net.XPUB_HEADERS.items(): + xkey_header_bytes = bfh("%08x" % xkey_header_bytes) + xkey_bytes = xkey_header_bytes + bytes([0] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) + + xkey_bytes = xkey_header_bytes + bytes([255] * 74) + xkey_b58 = EncodeBase58Check(xkey_bytes) + self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) + class Test_keyImport(unittest.TestCase):