| 1 |
import asyncio |
| 2 |
import base64 |
| 3 |
import json |
| 4 |
import os |
| 5 |
import sys |
| 6 |
import inspect |
| 7 |
|
| 8 |
import electrum_ecc as ecc |
| 9 |
|
| 10 |
from electrum import bitcoin |
| 11 |
from electrum.bitcoin import (public_key_to_p2pkh, address_from_private_key, |
| 12 |
is_address, is_private_key, |
| 13 |
var_int, _op_push, address_to_script, OnchainOutputType, address_to_payload, |
| 14 |
deserialize_privkey, serialize_privkey, is_segwit_address, |
| 15 |
is_b58_address, address_to_scripthash, is_minikey, |
| 16 |
is_compressed_privkey, EncodeBase58Check, DecodeBase58Check, |
| 17 |
script_num_to_bytes, push_script, add_number_to_script, |
| 18 |
opcodes, base_encode, base_decode, BitcoinException, |
| 19 |
taproot_tweak_pubkey, taproot_tweak_seckey, taproot_output_script, |
| 20 |
control_block_for_taproot_script_spend) |
| 21 |
from electrum import bip32 |
| 22 |
from electrum import segwit_addr |
| 23 |
from electrum.segwit_addr import DecodedBech32 |
| 24 |
from electrum.bip32 import (BIP32Node, convert_bip32_intpath_to_strpath, |
| 25 |
xpub_from_xprv, xpub_type, is_xprv, is_bip32_derivation, |
| 26 |
is_xpub, convert_bip32_strpath_to_intpath, |
| 27 |
normalize_bip32_derivation, is_all_public_derivation) |
| 28 |
from electrum.crypto import sha256d, SUPPORTED_PW_HASH_VERSIONS |
| 29 |
from electrum import crypto, constants |
| 30 |
from electrum.util import bfh, InvalidPassword, randrange |
| 31 |
from electrum.storage import WalletStorage |
| 32 |
from electrum.keystore import xtype_from_derivation |
| 33 |
|
| 34 |
from . import ElectrumTestCase |
| 35 |
from . import FAST_TESTS |
| 36 |
|
| 37 |
|
| 38 |
def needs_test_with_all_aes_implementations(func): |
| 39 |
"""Function decorator to run a unit test multiple times: |
| 40 |
once with each AES implementation. |
| 41 |
|
| 42 |
NOTE: this is inherently sequential; |
| 43 |
tests running in parallel would break things |
| 44 |
""" |
| 45 |
if FAST_TESTS: |
| 46 |
return func |
| 47 |
has_cryptodome = crypto.HAS_CRYPTODOME |
| 48 |
has_cryptography = crypto.HAS_CRYPTOGRAPHY |
| 49 |
has_pyaes = crypto.HAS_PYAES |
| 50 |
if inspect.iscoroutinefunction(func): |
| 51 |
async def run_test(*args, **kwargs): |
| 52 |
try: |
| 53 |
if has_pyaes: |
| 54 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY, crypto.HAS_PYAES) = False, False, True |
| 55 |
await func(*args, **kwargs) |
| 56 |
if has_cryptodome: |
| 57 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY, crypto.HAS_PYAES) = True, False, False |
| 58 |
await func(*args, **kwargs) |
| 59 |
if has_cryptography: |
| 60 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY, crypto.HAS_PYAES) = False, True, False |
| 61 |
await func(*args, **kwargs) |
| 62 |
finally: |
| 63 |
crypto.HAS_CRYPTODOME = has_cryptodome |
| 64 |
crypto.HAS_CRYPTOGRAPHY = has_cryptography |
| 65 |
crypto.HAS_PYAES = has_pyaes |
| 66 |
else: |
| 67 |
def run_test(*args, **kwargs): |
| 68 |
try: |
| 69 |
if has_pyaes: |
| 70 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY, crypto.HAS_PYAES) = False, False, True |
| 71 |
func(*args, **kwargs) |
| 72 |
if has_cryptodome: |
| 73 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY, crypto.HAS_PYAES) = True, False, False |
| 74 |
func(*args, **kwargs) |
| 75 |
if has_cryptography: |
| 76 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY, crypto.HAS_PYAES) = False, True, False |
| 77 |
func(*args, **kwargs) |
| 78 |
finally: |
| 79 |
crypto.HAS_CRYPTODOME = has_cryptodome |
| 80 |
crypto.HAS_CRYPTOGRAPHY = has_cryptography |
| 81 |
crypto.HAS_PYAES = has_pyaes |
| 82 |
return run_test |
| 83 |
|
| 84 |
|
| 85 |
def needs_test_with_all_chacha20_implementations(func): |
| 86 |
"""Function decorator to run a unit test multiple times: |
| 87 |
once with each ChaCha20/Poly1305 implementation. |
| 88 |
|
| 89 |
NOTE: this is inherently sequential; |
| 90 |
tests running in parallel would break things |
| 91 |
""" |
| 92 |
if FAST_TESTS: |
| 93 |
return func |
| 94 |
has_cryptodome = crypto.HAS_CRYPTODOME |
| 95 |
has_cryptography = crypto.HAS_CRYPTOGRAPHY |
| 96 |
if inspect.iscoroutinefunction(func): |
| 97 |
async def run_test(*args, **kwargs): |
| 98 |
try: |
| 99 |
if has_cryptodome: |
| 100 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY) = True, False |
| 101 |
await func(*args, **kwargs) |
| 102 |
if has_cryptography: |
| 103 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY) = False, True |
| 104 |
await func(*args, **kwargs) |
| 105 |
finally: |
| 106 |
crypto.HAS_CRYPTODOME = has_cryptodome |
| 107 |
crypto.HAS_CRYPTOGRAPHY = has_cryptography |
| 108 |
else: |
| 109 |
def run_test(*args, **kwargs): |
| 110 |
try: |
| 111 |
if has_cryptodome: |
| 112 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY) = True, False |
| 113 |
func(*args, **kwargs) |
| 114 |
if has_cryptography: |
| 115 |
(crypto.HAS_CRYPTODOME, crypto.HAS_CRYPTOGRAPHY) = False, True |
| 116 |
func(*args, **kwargs) |
| 117 |
finally: |
| 118 |
crypto.HAS_CRYPTODOME = has_cryptodome |
| 119 |
crypto.HAS_CRYPTOGRAPHY = has_cryptography |
| 120 |
return run_test |
| 121 |
|
| 122 |
|
| 123 |
def disable_ecdsa_r_value_grinding(func): |
| 124 |
"""Function decorator to run a unit test with ecdsa R-value grinding disabled. |
| 125 |
This is used when we want to pass test vectors that were created without R-value grinding. |
| 126 |
(see https://github.com/bitcoin/bitcoin/pull/13666 ) |
| 127 |
|
| 128 |
NOTE: this is inherently sequential; |
| 129 |
tests running in parallel would break things |
| 130 |
""" |
| 131 |
is_grinding = ecc.ENABLE_ECDSA_R_VALUE_GRINDING |
| 132 |
if inspect.iscoroutinefunction(func): |
| 133 |
async def run_test(*args, **kwargs): |
| 134 |
try: |
| 135 |
ecc.ENABLE_ECDSA_R_VALUE_GRINDING = False |
| 136 |
return await func(*args, **kwargs) |
| 137 |
finally: |
| 138 |
ecc.ENABLE_ECDSA_R_VALUE_GRINDING = is_grinding |
| 139 |
else: |
| 140 |
def run_test(*args, **kwargs): |
| 141 |
try: |
| 142 |
ecc.ENABLE_ECDSA_R_VALUE_GRINDING = False |
| 143 |
return func(*args, **kwargs) |
| 144 |
finally: |
| 145 |
ecc.ENABLE_ECDSA_R_VALUE_GRINDING = is_grinding |
| 146 |
return run_test |
| 147 |
|
| 148 |
|
| 149 |
class Test_bitcoin(ElectrumTestCase): |
| 150 |
|
| 151 |
def test_libsecp256k1_is_available(self): |
| 152 |
|
| 153 |
self.assertTrue(bool(ecc._libsecp256k1)) |
| 154 |
|
| 155 |
def test_pycryptodomex_is_available(self): |
| 156 |
|
| 157 |
self.assertTrue(bool(crypto.HAS_CRYPTODOME)) |
| 158 |
|
| 159 |
def test_cryptography_is_available(self): |
| 160 |
|
| 161 |
self.assertTrue(bool(crypto.HAS_CRYPTOGRAPHY)) |
| 162 |
|
| 163 |
def test_pyaes_is_available(self): |
| 164 |
|
| 165 |
self.assertTrue(bool(crypto.HAS_PYAES)) |
| 166 |
|
| 167 |
@needs_test_with_all_aes_implementations |
| 168 |
def test_crypto(self): |
| 169 |
for message in [b"Chancellor on brink of second bailout for banks", b'\xff'*512]: |
| 170 |
self._do_test_crypto(message) |
| 171 |
|
| 172 |
def _do_test_crypto(self, message: bytes): |
| 173 |
G = ecc.GENERATOR |
| 174 |
_r = G.order() |
| 175 |
pvk = randrange(_r) |
| 176 |
|
| 177 |
Pub = pvk*G |
| 178 |
pubkey_c = Pub.get_public_key_bytes(True) |
| 179 |
|
| 180 |
addr_c = public_key_to_p2pkh(pubkey_c) |
| 181 |
|
| 182 |
|
| 183 |
eck = ecc.ECPrivkey.from_secret_scalar(pvk) |
| 184 |
|
| 185 |
|
| 186 |
enc = crypto.ecies_encrypt_message(ecc.ECPubkey(pubkey_c), message) |
| 187 |
dec = crypto.ecies_decrypt_message(eck, enc) |
| 188 |
self.assertEqual(message, dec) |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
dec2 = crypto.ecies_decrypt_message(eck, enc) |
| 193 |
self.assertEqual(message, dec2) |
| 194 |
|
| 195 |
msg32 = sha256d(bitcoin.usermessage_magic(message)) |
| 196 |
sig65 = eck.ecdsa_sign_recoverable(msg32, is_compressed=True) |
| 197 |
self.assertTrue(eck.ecdsa_verify_recoverable(sig65, msg32)) |
| 198 |
|
| 199 |
@staticmethod |
| 200 |
def sign_message_with_wif_privkey(wif_privkey: str, msg: bytes) -> bytes: |
| 201 |
txin_type, privkey, compressed = deserialize_privkey(wif_privkey) |
| 202 |
key = ecc.ECPrivkey(privkey) |
| 203 |
return bitcoin.ecdsa_sign_usermessage(key, msg, is_compressed=compressed) |
| 204 |
|
| 205 |
def test_signmessage_legacy_address(self): |
| 206 |
msg1 = b'Chancellor on brink of second bailout for banks' |
| 207 |
msg2 = b'Electrum' |
| 208 |
|
| 209 |
sig1 = self.sign_message_with_wif_privkey( |
| 210 |
'L1TnU2zbNaAqMoVh65Cyvmcjzbrj41Gs9iTLcWbpJCMynXuap6UN', msg1) |
| 211 |
addr1 = '15hETetDmcXm1mM4sEf7U2KXC9hDHFMSzz' |
| 212 |
sig2 = self.sign_message_with_wif_privkey( |
| 213 |
'5Hxn5C4SQuiV6e62A1MtZmbSeQyrLFhu5uYks62pU5VBUygK2KD', msg2) |
| 214 |
addr2 = '1GPHVTY8UD9my6jyP4tb2TYJwUbDetyNC6' |
| 215 |
|
| 216 |
sig1_b64 = base64.b64encode(sig1) |
| 217 |
sig2_b64 = base64.b64encode(sig2) |
| 218 |
|
| 219 |
self.assertEqual(sig1_b64, b'Hzsu0U/THAsPz/MSuXGBKSULz2dTfmrg1NsAhFp+wH5aKfmX4Db7ExLGa7FGn0m6Mf43KsbEOWpvUUUBTM3Uusw=') |
| 220 |
self.assertEqual(sig2_b64, b'HBQdYfv7kOrxmRewLJnG7sV6KlU71O04hUnE4tai97p7Pg+D+yKaWXsdGgHTrKw90caQMo/D6b//qX50ge9P9iI=') |
| 221 |
|
| 222 |
self.assertTrue(bitcoin.verify_usermessage_with_address(addr1, sig1, msg1)) |
| 223 |
self.assertTrue(bitcoin.verify_usermessage_with_address(addr2, sig2, msg2)) |
| 224 |
|
| 225 |
self.assertFalse(bitcoin.verify_usermessage_with_address(addr1, b'wrong', msg1)) |
| 226 |
self.assertFalse(bitcoin.verify_usermessage_with_address(addr1, sig2, msg1)) |
| 227 |
|
| 228 |
def test_signmessage_low_s(self): |
| 229 |
"""`$ bitcoin-cli verifymessage` does NOT enforce the low-S rule for ecdsa sigs. This tests we do the same.""" |
| 230 |
addr = "15hETetDmcXm1mM4sEf7U2KXC9hDHFMSzz" |
| 231 |
sig_low_s = b'Hzsu0U/THAsPz/MSuXGBKSULz2dTfmrg1NsAhFp+wH5aKfmX4Db7ExLGa7FGn0m6Mf43KsbEOWpvUUUBTM3Uusw=' |
| 232 |
sig_high_s = b'IDsu0U/THAsPz/MSuXGBKSULz2dTfmrg1NsAhFp+wH5a1gZoH8kE7O05lE65YLZFzLx3sh/rDzXMbo1dQAJhhnU=' |
| 233 |
msg = b'Chancellor on brink of second bailout for banks' |
| 234 |
self.assertTrue(bitcoin.verify_usermessage_with_address(address=addr, sig65=base64.b64decode(sig_low_s), message=msg)) |
| 235 |
self.assertTrue(bitcoin.verify_usermessage_with_address(address=addr, sig65=base64.b64decode(sig_high_s), message=msg)) |
| 236 |
|
| 237 |
def test_signmessage_segwit_witness_v0_address(self): |
| 238 |
msg = b'Electrum' |
| 239 |
|
| 240 |
sig1 = self.sign_message_with_wif_privkey("p2wpkh-p2sh:L1cgMEnShp73r9iCukoPE3MogLeueNYRD9JVsfT1zVHyPBR3KqBY", msg) |
| 241 |
addr1 = "3DYoBqQ5N6dADzyQjy9FT1Ls4amiYVaqTG" |
| 242 |
self.assertEqual(base64.b64encode(sig1), b'HyFaND+87TtVbRhkTfT3mPNBCQcJ32XXtNZGW8sFldJsNpOPCegEmdcCf5Thy18hdMH88GLxZLkOby/EwVUuSeA=') |
| 243 |
self.assertTrue(bitcoin.verify_usermessage_with_address(addr1, sig1, msg)) |
| 244 |
self.assertFalse(bitcoin.verify_usermessage_with_address(addr1, sig1, b'heyheyhey')) |
| 245 |
|
| 246 |
sig2 = self.sign_message_with_wif_privkey("p2wpkh:L1cgMEnShp73r9iCukoPE3MogLeueNYRD9JVsfT1zVHyPBR3KqBY", msg) |
| 247 |
addr2 = "bc1qq2tmmcngng78nllq2pvrkchcdukemtj56uyue0" |
| 248 |
self.assertEqual(base64.b64encode(sig2), b'HyFaND+87TtVbRhkTfT3mPNBCQcJ32XXtNZGW8sFldJsNpOPCegEmdcCf5Thy18hdMH88GLxZLkOby/EwVUuSeA=') |
| 249 |
self.assertTrue(bitcoin.verify_usermessage_with_address(addr2, sig2, msg)) |
| 250 |
self.assertFalse(bitcoin.verify_usermessage_with_address(addr2, sig2, b'heyheyhey')) |
| 251 |
|
| 252 |
def test_signmessage_segwit_witness_v0_address_test_we_also_accept_sigs_from_trezor(self): |
| 253 |
"""Trezor and some other projects use a slightly different scheme for message-signing |
| 254 |
with p2wpkh and p2wpkh-p2sh addresses. Test that we also accept signatures from them. |
| 255 |
see #3861 |
| 256 |
tests from https://github.com/trezor/trezor-firmware/blob/2ce1e6ba7dbe5bbaeeb336fff0a038e59cb40ef8/tests/device_tests/bitcoin/test_signmessage.py#L39 |
| 257 |
""" |
| 258 |
msg = b"This is an example of a signed message." |
| 259 |
addr1 = "3L6TyTisPBmrDAj6RoKmDzNnj4eQi54gD2" |
| 260 |
addr2 = "bc1qannfxke2tfd4l7vhepehpvt05y83v3qsf6nfkk" |
| 261 |
sig1 = bytes.fromhex("23744de4516fac5c140808015664516a32fead94de89775cec7e24dbc24fe133075ac09301c4cc8e197bea4b6481661d5b8e9bf19d8b7b8a382ecdb53c2ee0750d") |
| 262 |
sig2 = bytes.fromhex("28b55d7600d9e9a7e2a49155ddf3cfdb8e796c207faab833010fa41fb7828889bc47cf62348a7aaa0923c0832a589fab541e8f12eb54fb711c90e2307f0f66b194") |
| 263 |
self.assertTrue(bitcoin.verify_usermessage_with_address(address=addr1, sig65=sig1, message=msg)) |
| 264 |
self.assertTrue(bitcoin.verify_usermessage_with_address(address=addr2, sig65=sig2, message=msg)) |
| 265 |
|
| 266 |
sig1_wrongtype = bytes.fromhex("27744de4516fac5c140808015664516a32fead94de89775cec7e24dbc24fe133075ac09301c4cc8e197bea4b6481661d5b8e9bf19d8b7b8a382ecdb53c2ee0750d") |
| 267 |
sig2_wrongtype = bytes.fromhex("24b55d7600d9e9a7e2a49155ddf3cfdb8e796c207faab833010fa41fb7828889bc47cf62348a7aaa0923c0832a589fab541e8f12eb54fb711c90e2307f0f66b194") |
| 268 |
self.assertFalse(bitcoin.verify_usermessage_with_address(address=addr1, sig65=sig1_wrongtype, message=msg)) |
| 269 |
self.assertFalse(bitcoin.verify_usermessage_with_address(address=addr2, sig65=sig2_wrongtype, message=msg)) |
| 270 |
|
| 271 |
@needs_test_with_all_aes_implementations |
| 272 |
def test_decrypt_message(self): |
| 273 |
key = WalletStorage.get_eckey_from_password('pw123') |
| 274 |
self.assertEqual(b'me<(s_s)>age', crypto.ecies_decrypt_message( |
| 275 |
key, b'QklFMQMDFtgT3zWSQsa+Uie8H/WvfUjlu9UN9OJtTt3KlgKeSTi6SQfuhcg1uIz9hp3WIUOFGTLr4RNQBdjPNqzXwhkcPi2Xsbiw6UCNJncVPJ6QBg==')) |
| 276 |
self.assertEqual(b'me<(s_s)>age', crypto.ecies_decrypt_message( |
| 277 |
key, b'QklFMQKXOXbylOQTSMGfo4MFRwivAxeEEkewWQrpdYTzjPhqjHcGBJwdIhB7DyRfRQihuXx1y0ZLLv7XxLzrILzkl/H4YUtZB4uWjuOAcmxQH4i/Og==')) |
| 278 |
self.assertEqual(b'hey_there' * 100, crypto.ecies_decrypt_message( |
| 279 |
key, b'QklFMQLOOsabsXtGQH8edAa6VOUa5wX8/DXmxX9NyHoAx1a5bWgllayGRVPeI2bf0ZdWK0tfal0ap0ZIVKbd2eOJybqQkILqT6E1/Syzq0Zicyb/AA1eZNkcX5y4gzloxinw00ubCA8M7gcUjJpOqbnksATcJ5y2YYXcHMGGfGurWu6uJ/UyrNobRidWppRMW5yR9/6utyNvT6OHIolCMEf7qLcmtneoXEiz51hkRdZS7weNf9mGqSbz9a2NL3sdh1A0feHIjAZgcCKcAvksNUSauf0/FnIjzTyPRpjRDMeDC8Ci3sGiuO3cvpWJwhZfbjcS26KmBv2CHWXfRRNFYOInHZNIXWNAoBB47Il5bGSMd+uXiGr+SQ9tNvcu+BiJNmFbxYqg+oQ8dGAl1DtvY2wJVY8k7vO9BIWSpyIxfGw7EDifhc5vnOmGe016p6a01C3eVGxgl23UYMrP7+fpjOcPmTSF4rk5U5ljEN3MSYqlf1QEv0OqlI9q1TwTK02VBCjMTYxDHsnt04OjNBkNO8v5uJ4NR+UUDBEp433z53I59uawZ+dbk4v4ZExcl8EGmKm3Gzbal/iJ/F7KQuX2b/ySEhLOFVYFWxK73X1nBvCSK2mC2/8fCw8oI5pmvzJwQhcCKTdEIrz3MMvAHqtPScDUOjzhXxInQOCb3+UBj1PPIdqkYLvZss1TEaBwYZjLkVnK2MBj7BaqT6Rp6+5A/fippUKHsnB6eYMEPR2YgDmCHL+4twxHJG6UWdP3ybaKiiAPy2OHNP6PTZ0HrqHOSJzBSDD+Z8YpaRg29QX3UEWlqnSKaan0VYAsV1VeaN0XFX46/TWO0L5tjhYVXJJYGqo6tIQJymxATLFRF6AZaD1Mwd27IAL04WkmoQoXfO6OFfwdp/shudY/1gBkDBvGPICBPtnqkvhGF+ZF3IRkuPwiFWeXmwBxKHsRx/3+aJu32Ml9+za41zVk2viaxcGqwTc5KMexQFLAUwqhv+aIik7U+5qk/gEVSuRoVkihoweFzKolNF+BknH2oB4rZdPixag5Zje3DvgjsSFlOl69W/67t/Gs8htfSAaHlsB8vWRQr9+v/lxTbrAw+O0E+sYGoObQ4qQMyQshNZEHbpPg63eWiHtJJnrVBvOeIbIHzoLDnMDsWVWZSMzAQ1vhX1H5QLgSEbRlKSliVY03kDkh/Nk/KOn+B2q37Ialq4JcRoIYFGJ8AoYEAD0tRuTqFddIclE75HzwaNG7NyKW1plsa72ciOPwsPJsdd5F0qdSQ3OSKtooTn7uf6dXOc4lDkfrVYRlZ0PX')) |
| 280 |
|
| 281 |
@needs_test_with_all_aes_implementations |
| 282 |
def test_encrypt_message(self): |
| 283 |
key = WalletStorage.get_eckey_from_password('secret_password77') |
| 284 |
msgs = [ |
| 285 |
bytes([0] * 555), |
| 286 |
b'cannot think of anything funny' |
| 287 |
] |
| 288 |
for plaintext in msgs: |
| 289 |
ciphertext1 = crypto.ecies_encrypt_message(key, plaintext) |
| 290 |
ciphertext2 = crypto.ecies_encrypt_message(key, plaintext) |
| 291 |
self.assertEqual(plaintext, crypto.ecies_decrypt_message(key, ciphertext1)) |
| 292 |
self.assertEqual(plaintext, crypto.ecies_decrypt_message(key, ciphertext2)) |
| 293 |
self.assertNotEqual(ciphertext1, ciphertext2) |
| 294 |
|
| 295 |
@needs_test_with_all_aes_implementations |
| 296 |
def test_aes_homomorphic(self): |
| 297 |
"""Make sure AES is homomorphic.""" |
| 298 |
payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' |
| 299 |
password = u'secret' |
| 300 |
for version in SUPPORTED_PW_HASH_VERSIONS: |
| 301 |
enc = crypto.pw_encode(payload, password, version=version) |
| 302 |
dec = crypto.pw_decode(enc, password, version=version) |
| 303 |
self.assertEqual(dec, payload) |
| 304 |
|
| 305 |
@needs_test_with_all_aes_implementations |
| 306 |
def test_aes_encode_without_password(self): |
| 307 |
"""When not passed a password, pw_encode is noop on the payload.""" |
| 308 |
payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' |
| 309 |
for version in SUPPORTED_PW_HASH_VERSIONS: |
| 310 |
enc = crypto.pw_encode(payload, None, version=version) |
| 311 |
self.assertEqual(payload, enc) |
| 312 |
|
| 313 |
@needs_test_with_all_aes_implementations |
| 314 |
def test_aes_deencode_without_password(self): |
| 315 |
"""When not passed a password, pw_decode is noop on the payload.""" |
| 316 |
payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' |
| 317 |
for version in SUPPORTED_PW_HASH_VERSIONS: |
| 318 |
enc = crypto.pw_decode(payload, None, version=version) |
| 319 |
self.assertEqual(payload, enc) |
| 320 |
|
| 321 |
@needs_test_with_all_aes_implementations |
| 322 |
def test_aes_decode_with_invalid_password(self): |
| 323 |
"""pw_decode raises an Exception when supplied an invalid password.""" |
| 324 |
payload = u"blah" |
| 325 |
password = u"uber secret" |
| 326 |
wrong_password = u"not the password" |
| 327 |
for version in SUPPORTED_PW_HASH_VERSIONS: |
| 328 |
enc = crypto.pw_encode(payload, password, version=version) |
| 329 |
with self.assertRaises(InvalidPassword): |
| 330 |
crypto.pw_decode(enc, wrong_password, version=version) |
| 331 |
|
| 332 |
|
| 333 |
enc = 'smJ7j6ccr8LnMOlx98s/ajgikv9s3R1PQuG3GyyIMmo=' |
| 334 |
with self.assertRaises(InvalidPassword): |
| 335 |
crypto.pw_decode(enc, wrong_password, version=1) |
| 336 |
|
| 337 |
@needs_test_with_all_chacha20_implementations |
| 338 |
def test_chacha20_poly1305_encrypt__with_associated_data(self): |
| 339 |
key = bytes.fromhex('37326d9d69a83b815ddfd947d21b0dd39111e5b6a5a44042c44d570ea03e3179') |
| 340 |
nonce = bytes.fromhex('010203040506070809101112') |
| 341 |
associated_data = bytes.fromhex('30c9572d4305d4f3ccb766b1db884da6f1e0086f55136a39740700c272095717') |
| 342 |
data = bytes.fromhex('4a6cd75da76cedf0a8a47e3a5734a328') |
| 343 |
self.assertEqual(bytes.fromhex('90fb51fcde1fbe4013500bd7a32280445d80ee21f0aa3acd30df72cf609de064'), |
| 344 |
crypto.chacha20_poly1305_encrypt(key=key, nonce=nonce, associated_data=associated_data, data=data)) |
| 345 |
|
| 346 |
@needs_test_with_all_chacha20_implementations |
| 347 |
def test_chacha20_poly1305_decrypt__with_associated_data(self): |
| 348 |
key = bytes.fromhex('37326d9d69a83b815ddfd947d21b0dd39111e5b6a5a44042c44d570ea03e3179') |
| 349 |
nonce = bytes.fromhex('010203040506070809101112') |
| 350 |
associated_data = bytes.fromhex('30c9572d4305d4f3ccb766b1db884da6f1e0086f55136a39740700c272095717') |
| 351 |
data = bytes.fromhex('90fb51fcde1fbe4013500bd7a32280445d80ee21f0aa3acd30df72cf609de064') |
| 352 |
self.assertEqual(bytes.fromhex('4a6cd75da76cedf0a8a47e3a5734a328'), |
| 353 |
crypto.chacha20_poly1305_decrypt(key=key, nonce=nonce, associated_data=associated_data, data=data)) |
| 354 |
with self.assertRaises(ValueError): |
| 355 |
crypto.chacha20_poly1305_decrypt(key=key, nonce=nonce, associated_data=b'', data=data) |
| 356 |
|
| 357 |
@needs_test_with_all_chacha20_implementations |
| 358 |
def test_chacha20_poly1305_encrypt__without_associated_data(self): |
| 359 |
key = bytes.fromhex('37326d9d69a83b815ddfd947d21b0dd39111e5b6a5a44042c44d570ea03e3179') |
| 360 |
nonce = bytes.fromhex('010203040506070809101112') |
| 361 |
data = bytes.fromhex('4a6cd75da76cedf0a8a47e3a5734a328') |
| 362 |
self.assertEqual(bytes.fromhex('90fb51fcde1fbe4013500bd7a322804469c2be9b1385bc5ded5cd96be510280f'), |
| 363 |
crypto.chacha20_poly1305_encrypt(key=key, nonce=nonce, data=data)) |
| 364 |
self.assertEqual(bytes.fromhex('90fb51fcde1fbe4013500bd7a322804469c2be9b1385bc5ded5cd96be510280f'), |
| 365 |
crypto.chacha20_poly1305_encrypt(key=key, nonce=nonce, data=data, associated_data=b'')) |
| 366 |
|
| 367 |
@needs_test_with_all_chacha20_implementations |
| 368 |
def test_chacha20_poly1305_decrypt__without_associated_data(self): |
| 369 |
key = bytes.fromhex('37326d9d69a83b815ddfd947d21b0dd39111e5b6a5a44042c44d570ea03e3179') |
| 370 |
nonce = bytes.fromhex('010203040506070809101112') |
| 371 |
data = bytes.fromhex('90fb51fcde1fbe4013500bd7a322804469c2be9b1385bc5ded5cd96be510280f') |
| 372 |
self.assertEqual(bytes.fromhex('4a6cd75da76cedf0a8a47e3a5734a328'), |
| 373 |
crypto.chacha20_poly1305_decrypt(key=key, nonce=nonce, data=data)) |
| 374 |
self.assertEqual(bytes.fromhex('4a6cd75da76cedf0a8a47e3a5734a328'), |
| 375 |
crypto.chacha20_poly1305_decrypt(key=key, nonce=nonce, data=data, associated_data=b'')) |
| 376 |
|
| 377 |
@needs_test_with_all_chacha20_implementations |
| 378 |
def test_chacha20_encrypt__8_byte_nonce(self): |
| 379 |
key = bytes.fromhex('37326d9d69a83b815ddfd947d21b0dd39111e5b6a5a44042c44d570ea03e3179') |
| 380 |
nonce = bytes.fromhex('0102030405060708') |
| 381 |
data = bytes.fromhex('38a0e0a7c865fe9ca31f0730cfcab610f18e6da88dc3790f1d243f711a257c78') |
| 382 |
ciphertext = crypto.chacha20_encrypt(key=key, nonce=nonce, data=data) |
| 383 |
self.assertEqual(bytes.fromhex('f62fbd74d197323c7c3d5658476a884d38ee6f4b5500add1e8dc80dcd9c15dff'), ciphertext) |
| 384 |
self.assertEqual(data, crypto.chacha20_decrypt(key=key, nonce=nonce, data=ciphertext)) |
| 385 |
|
| 386 |
@needs_test_with_all_chacha20_implementations |
| 387 |
def test_chacha20_encrypt__12_byte_nonce(self): |
| 388 |
key = bytes.fromhex('37326d9d69a83b815ddfd947d21b0dd39111e5b6a5a44042c44d570ea03e3179') |
| 389 |
nonce = bytes.fromhex('010203040506070809101112') |
| 390 |
data = bytes.fromhex('38a0e0a7c865fe9ca31f0730cfcab610f18e6da88dc3790f1d243f711a257c78') |
| 391 |
ciphertext = crypto.chacha20_encrypt(key=key, nonce=nonce, data=data) |
| 392 |
self.assertEqual(bytes.fromhex('c0b1cb75c3c23c13f47dab393add738c92c62c4e2546cb3bf2b48269a4184028'), ciphertext) |
| 393 |
self.assertEqual(data, crypto.chacha20_decrypt(key=key, nonce=nonce, data=ciphertext)) |
| 394 |
|
| 395 |
def test_sha256d(self): |
| 396 |
self.assertEqual(b'\x95MZI\xfdp\xd9\xb8\xbc\xdb5\xd2R&x)\x95\x7f~\xf7\xfalt\xf8\x84\x19\xbd\xc5\xe8"\t\xf4', |
| 397 |
sha256d(u"test")) |
| 398 |
|
| 399 |
def test_var_int(self): |
| 400 |
for i in range(0xfd): |
| 401 |
self.assertEqual(var_int(i), bfh("{:02x}".format(i))) |
| 402 |
|
| 403 |
self.assertEqual(var_int(0xfd), bfh("fdfd00")) |
| 404 |
self.assertEqual(var_int(0xfe), bfh("fdfe00")) |
| 405 |
self.assertEqual(var_int(0xff), bfh("fdff00")) |
| 406 |
self.assertEqual(var_int(0x1234), bfh("fd3412")) |
| 407 |
self.assertEqual(var_int(0xffff), bfh("fdffff")) |
| 408 |
self.assertEqual(var_int(0x10000), bfh("fe00000100")) |
| 409 |
self.assertEqual(var_int(0x12345678), bfh("fe78563412")) |
| 410 |
self.assertEqual(var_int(0xffffffff), bfh("feffffffff")) |
| 411 |
self.assertEqual(var_int(0x100000000), bfh("ff0000000001000000")) |
| 412 |
self.assertEqual(var_int(0x0123456789abcdef), bfh("ffefcdab8967452301")) |
| 413 |
|
| 414 |
def test_op_push(self): |
| 415 |
self.assertEqual(_op_push(0x00), bfh('00')) |
| 416 |
self.assertEqual(_op_push(0x12), bfh('12')) |
| 417 |
self.assertEqual(_op_push(0x4b), bfh('4b')) |
| 418 |
self.assertEqual(_op_push(0x4c), bfh('4c4c')) |
| 419 |
self.assertEqual(_op_push(0xfe), bfh('4cfe')) |
| 420 |
self.assertEqual(_op_push(0xff), bfh('4cff')) |
| 421 |
self.assertEqual(_op_push(0x100), bfh('4d0001')) |
| 422 |
self.assertEqual(_op_push(0x1234), bfh('4d3412')) |
| 423 |
self.assertEqual(_op_push(0xfffe), bfh('4dfeff')) |
| 424 |
self.assertEqual(_op_push(0xffff), bfh('4dffff')) |
| 425 |
self.assertEqual(_op_push(0x10000), bfh('4e00000100')) |
| 426 |
self.assertEqual(_op_push(0x12345678), bfh('4e78563412')) |
| 427 |
|
| 428 |
def test_script_num_to_hex(self): |
| 429 |
|
| 430 |
self.assertEqual(script_num_to_bytes(127), bfh('7f')) |
| 431 |
self.assertEqual(script_num_to_bytes(-127), bfh('ff')) |
| 432 |
self.assertEqual(script_num_to_bytes(128), bfh('8000')) |
| 433 |
self.assertEqual(script_num_to_bytes(-128), bfh('8080')) |
| 434 |
self.assertEqual(script_num_to_bytes(129), bfh('8100')) |
| 435 |
self.assertEqual(script_num_to_bytes(-129), bfh('8180')) |
| 436 |
self.assertEqual(script_num_to_bytes(256), bfh('0001')) |
| 437 |
self.assertEqual(script_num_to_bytes(-256), bfh('0081')) |
| 438 |
self.assertEqual(script_num_to_bytes(32767), bfh('ff7f')) |
| 439 |
self.assertEqual(script_num_to_bytes(-32767), bfh('ffff')) |
| 440 |
self.assertEqual(script_num_to_bytes(32768), bfh('008000')) |
| 441 |
self.assertEqual(script_num_to_bytes(-32768), bfh('008080')) |
| 442 |
|
| 443 |
def test_push_script(self): |
| 444 |
|
| 445 |
self.assertEqual(push_script(b""), bytes([opcodes.OP_0])) |
| 446 |
self.assertEqual(push_script(b'\x07'), bytes([opcodes.OP_7])) |
| 447 |
self.assertEqual(push_script(b'\x10'), bytes([opcodes.OP_16])) |
| 448 |
self.assertEqual(push_script(b'\x81'), bytes([opcodes.OP_1NEGATE])) |
| 449 |
self.assertEqual(push_script(b'\x11'), bfh('0111')) |
| 450 |
self.assertEqual(push_script(75 * b'\x42'), bfh('4b' + 75 * '42')) |
| 451 |
self.assertEqual(push_script(76 * b'\x42'), bytes([opcodes.OP_PUSHDATA1]) + bfh('4c' + 76 * '42')) |
| 452 |
self.assertEqual(push_script(100 * b'\x42'), bytes([opcodes.OP_PUSHDATA1]) + bfh('64' + 100 * '42')) |
| 453 |
self.assertEqual(push_script(255 * b'\x42'), bytes([opcodes.OP_PUSHDATA1]) + bfh('ff' + 255 * '42')) |
| 454 |
self.assertEqual(push_script(256 * b'\x42'), bytes([opcodes.OP_PUSHDATA2]) + bfh('0001' + 256 * '42')) |
| 455 |
self.assertEqual(push_script(520 * b'\x42'), bytes([opcodes.OP_PUSHDATA2]) + bfh('0802' + 520 * '42')) |
| 456 |
|
| 457 |
def test_add_number_to_script(self): |
| 458 |
|
| 459 |
self.assertEqual(add_number_to_script(0), bytes([opcodes.OP_0])) |
| 460 |
self.assertEqual(add_number_to_script(7), bytes([opcodes.OP_7])) |
| 461 |
self.assertEqual(add_number_to_script(16), bytes([opcodes.OP_16])) |
| 462 |
self.assertEqual(add_number_to_script(-1), bytes([opcodes.OP_1NEGATE])) |
| 463 |
self.assertEqual(add_number_to_script(-127), bfh('01ff')) |
| 464 |
self.assertEqual(add_number_to_script(-2), bfh('0182')) |
| 465 |
self.assertEqual(add_number_to_script(17), bfh('0111')) |
| 466 |
self.assertEqual(add_number_to_script(127), bfh('017f')) |
| 467 |
self.assertEqual(add_number_to_script(-32767), bfh('02ffff')) |
| 468 |
self.assertEqual(add_number_to_script(-128), bfh('028080')) |
| 469 |
self.assertEqual(add_number_to_script(128), bfh('028000')) |
| 470 |
self.assertEqual(add_number_to_script(32767), bfh('02ff7f')) |
| 471 |
self.assertEqual(add_number_to_script(-8388607), bfh('03ffffff')) |
| 472 |
self.assertEqual(add_number_to_script(-32768), bfh('03008080')) |
| 473 |
self.assertEqual(add_number_to_script(32768), bfh('03008000')) |
| 474 |
self.assertEqual(add_number_to_script(8388607), bfh('03ffff7f')) |
| 475 |
self.assertEqual(add_number_to_script(-2147483647), bfh('04ffffffff')) |
| 476 |
self.assertEqual(add_number_to_script(-8388608), bfh('0400008080')) |
| 477 |
self.assertEqual(add_number_to_script(8388608), bfh('0400008000')) |
| 478 |
self.assertEqual(add_number_to_script(2147483647), bfh('04ffffff7f')) |
| 479 |
|
| 480 |
def test_address_to_script(self): |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
self.assertEqual(address_to_script('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4').hex(), '0014751e76e8199196d454941c45d1b3a323f1433bd6') |
| 485 |
self.assertEqual(address_to_script('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7', net=constants.BitcoinTestnet).hex(), '00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262') |
| 486 |
self.assertEqual(address_to_script('tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy', net=constants.BitcoinTestnet).hex(), '0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433') |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
self.assertEqual(address_to_script('bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y').hex(), '5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6') |
| 494 |
self.assertEqual(address_to_script('BC1SW50QGDZ25J').hex(), '6002751e') |
| 495 |
self.assertEqual(address_to_script('bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs').hex(), '5210751e76e8199196d454941c45d1b3a323') |
| 496 |
self.assertEqual(address_to_script('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0').hex(), '512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') |
| 497 |
self.assertEqual(address_to_script('tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c', net=constants.BitcoinTestnet).hex(), '5120000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433') |
| 498 |
|
| 499 |
|
| 500 |
for net in [constants.BitcoinMainnet, constants.BitcoinTestnet]: |
| 501 |
self.assertFalse(is_address('tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty', net=net)) |
| 502 |
self.assertFalse(is_address('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5', net=net)) |
| 503 |
self.assertFalse(is_address('BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2', net=net)) |
| 504 |
self.assertFalse(is_address('bc1rw5uspcuh', net=net)) |
| 505 |
self.assertFalse(is_address('bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90', net=net)) |
| 506 |
self.assertFalse(is_address('BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P', net=net)) |
| 507 |
self.assertFalse(is_address('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7', net=net)) |
| 508 |
self.assertFalse(is_address('bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du', net=net)) |
| 509 |
self.assertFalse(is_address('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv', net=net)) |
| 510 |
self.assertFalse(is_address('bc1gmk9yu', net=net)) |
| 511 |
|
| 512 |
|
| 513 |
for net in [constants.BitcoinMainnet, constants.BitcoinTestnet]: |
| 514 |
self.assertFalse(is_address('tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut', net=net)) |
| 515 |
self.assertFalse(is_address('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd', net=net)) |
| 516 |
self.assertFalse(is_address('tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf', net=net)) |
| 517 |
self.assertFalse(is_address('BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL', net=net)) |
| 518 |
self.assertFalse(is_address('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh', net=net)) |
| 519 |
self.assertFalse(is_address('tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47', net=net)) |
| 520 |
self.assertFalse(is_address('bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4', net=net)) |
| 521 |
self.assertFalse(is_address('BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R', net=net)) |
| 522 |
self.assertFalse(is_address('bc1pw5dgrnzv', net=net)) |
| 523 |
self.assertFalse(is_address('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav', net=net)) |
| 524 |
self.assertFalse(is_address('BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P', net=net)) |
| 525 |
self.assertFalse(is_address('tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq47Zagq', net=net)) |
| 526 |
self.assertFalse(is_address('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf', net=net)) |
| 527 |
self.assertFalse(is_address('tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j', net=net)) |
| 528 |
self.assertFalse(is_address('bc1gmk9yu', net=net)) |
| 529 |
|
| 530 |
|
| 531 |
bech32_mixed_case1 = 'BC1QW508D6QEJXTDG4Y5R3zarvary0c5xw7kv8f3t4' |
| 532 |
self.assertFalse(is_address(bech32_mixed_case1)) |
| 533 |
self.assertTrue(is_address(bech32_mixed_case1.lower())) |
| 534 |
self.assertTrue(is_address(bech32_mixed_case1.upper())) |
| 535 |
|
| 536 |
|
| 537 |
self.assertEqual(address_to_script('14gcRovpkCoGkCNBivQBvw7eso7eiNAbxG').hex(), '76a91428662c67561b95c79d2257d2a93d9d151c977e9188ac') |
| 538 |
self.assertEqual(address_to_script('1BEqfzh4Y3zzLosfGhw1AsqbEKVW6e1qHv').hex(), '76a914704f4b81cadb7bf7e68c08cd3657220f680f863c88ac') |
| 539 |
self.assertEqual(address_to_script('mutXcGt1CJdkRvXuN2xoz2quAAQYQ59bRX', net=constants.BitcoinTestnet).hex(), '76a9149da64e300c5e4eb4aaffc9c2fd465348d5618ad488ac') |
| 540 |
self.assertEqual(address_to_script('miqtaRTkU3U8rzwKbEHx3g8FSz8GJtPS3K', net=constants.BitcoinTestnet).hex(), '76a914247d2d5b6334bdfa2038e85b20fc15264f8e5d2788ac') |
| 541 |
|
| 542 |
|
| 543 |
self.assertEqual(address_to_script('35ZqQJcBQMZ1rsv8aSuJ2wkC7ohUCQMJbT').hex(), 'a9142a84cf00d47f699ee7bbc1dea5ec1bdecb4ac15487') |
| 544 |
self.assertEqual(address_to_script('3PyjzJ3im7f7bcV724GR57edKDqoZvH7Ji').hex(), 'a914f47c8954e421031ad04ecd8e7752c9479206b9d387') |
| 545 |
self.assertEqual(address_to_script('2N3LSvr3hv5EVdfcrxg2Yzecf3SRvqyBE4p', net=constants.BitcoinTestnet).hex(), 'a9146eae23d8c4a941316017946fc761a7a6c85561fb87') |
| 546 |
self.assertEqual(address_to_script('2NE4ZdmxFmUgwu5wtfoN2gVniyMgRDYq1kk', net=constants.BitcoinTestnet).hex(), 'a914e4567743d378957cd2ee7072da74b1203c1a7a0b87') |
| 547 |
|
| 548 |
|
| 549 |
def test_address_to_payload(self): |
| 550 |
|
| 551 |
self.assertEqual( |
| 552 |
address_to_payload('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'), |
| 553 |
(OnchainOutputType.WITVER0_P2WPKH, bytes.fromhex('751e76e8199196d454941c45d1b3a323f1433bd6'))) |
| 554 |
|
| 555 |
|
| 556 |
self.assertEqual( |
| 557 |
address_to_payload('bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3'), |
| 558 |
(OnchainOutputType.WITVER0_P2WSH, bytes.fromhex('1863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262'))) |
| 559 |
|
| 560 |
|
| 561 |
self.assertEqual( |
| 562 |
address_to_payload('bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr'), |
| 563 |
(OnchainOutputType.WITVER1_P2TR, bytes.fromhex('a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c'))) |
| 564 |
|
| 565 |
|
| 566 |
self.assertEqual( |
| 567 |
address_to_payload('14gcRovpkCoGkCNBivQBvw7eso7eiNAbxG'), |
| 568 |
(OnchainOutputType.P2PKH, bytes.fromhex('28662c67561b95c79d2257d2a93d9d151c977e91'))) |
| 569 |
|
| 570 |
|
| 571 |
self.assertEqual( |
| 572 |
address_to_payload('35ZqQJcBQMZ1rsv8aSuJ2wkC7ohUCQMJbT'), |
| 573 |
(OnchainOutputType.P2SH, bytes.fromhex('2a84cf00d47f699ee7bbc1dea5ec1bdecb4ac154'))) |
| 574 |
|
| 575 |
def test_bech32_decode(self): |
| 576 |
|
| 577 |
|
| 578 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32, 'a', []), |
| 579 |
segwit_addr.bech32_decode('A12UEL5L')) |
| 580 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32, 'a', []), |
| 581 |
segwit_addr.bech32_decode('a12uel5l')) |
| 582 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32, 'an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio', []), |
| 583 |
segwit_addr.bech32_decode('an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs')) |
| 584 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32, 'abcdef', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]), |
| 585 |
segwit_addr.bech32_decode('abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw')) |
| 586 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32, '1', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), |
| 587 |
segwit_addr.bech32_decode('11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j')) |
| 588 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32, 'split', [24, 23, 25, 24, 22, 28, 1, 16, 11, 29, 8, 25, 23, 29, 19, 13, 16, 23, 29, 22, 25, 28, 1, 16, 11, 3, 25, 29, 27, 25, 3, 3, 29, 19, 11, 25, 3, 3, 25, 13, 24, 29, 1, 25, 3, 3, 25, 13]), |
| 589 |
segwit_addr.bech32_decode('split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w')) |
| 590 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32, '?', []), |
| 591 |
segwit_addr.bech32_decode('?1ezyfcl')) |
| 592 |
|
| 593 |
self.assertEqual(DecodedBech32(None, None, None), |
| 594 |
segwit_addr.bech32_decode('\x201nwldj5')) |
| 595 |
self.assertEqual(DecodedBech32(None, None, None), |
| 596 |
segwit_addr.bech32_decode('\x7f1axkwrx')) |
| 597 |
self.assertEqual(DecodedBech32(None, None, None), |
| 598 |
segwit_addr.bech32_decode('\x801eym55h')) |
| 599 |
self.assertEqual(DecodedBech32(None, None, None), |
| 600 |
segwit_addr.bech32_decode('an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx')) |
| 601 |
self.assertEqual(DecodedBech32(None, None, None), |
| 602 |
segwit_addr.bech32_decode('pzry9x0s0muk')) |
| 603 |
self.assertEqual(DecodedBech32(None, None, None), |
| 604 |
segwit_addr.bech32_decode('1pzry9x0s0muk')) |
| 605 |
self.assertEqual(DecodedBech32(None, None, None), |
| 606 |
segwit_addr.bech32_decode('x1b4n0q5v')) |
| 607 |
self.assertEqual(DecodedBech32(None, None, None), |
| 608 |
segwit_addr.bech32_decode('li1dgmt3')) |
| 609 |
self.assertEqual(DecodedBech32(None, None, None), |
| 610 |
segwit_addr.bech32_decode('de1lg7wt\xff')) |
| 611 |
self.assertEqual(DecodedBech32(None, None, None), |
| 612 |
segwit_addr.bech32_decode('A1G7SGD8')) |
| 613 |
self.assertEqual(DecodedBech32(None, None, None), |
| 614 |
segwit_addr.bech32_decode('10a06t8')) |
| 615 |
self.assertEqual(DecodedBech32(None, None, None), |
| 616 |
segwit_addr.bech32_decode('1qzzfhee')) |
| 617 |
|
| 618 |
|
| 619 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32M, 'a', []), |
| 620 |
segwit_addr.bech32_decode('A1LQFN3A')) |
| 621 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32M, 'a', []), |
| 622 |
segwit_addr.bech32_decode('a1lqfn3a')) |
| 623 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32M, 'an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber1', []), |
| 624 |
segwit_addr.bech32_decode('an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11sg7hg6')) |
| 625 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32M, 'abcdef', [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]), |
| 626 |
segwit_addr.bech32_decode('abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqzd3ryx')) |
| 627 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32M, '1', [31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31]), |
| 628 |
segwit_addr.bech32_decode('11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8')) |
| 629 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32M, 'split', [24, 23, 25, 24, 22, 28, 1, 16, 11, 29, 8, 25, 23, 29, 19, 13, 16, 23, 29, 22, 25, 28, 1, 16, 11, 3, 25, 29, 27, 25, 3, 3, 29, 19, 11, 25, 3, 3, 25, 13, 24, 29, 1, 25, 3, 3, 25, 13]), |
| 630 |
segwit_addr.bech32_decode('split1checkupstagehandshakeupstreamerranterredcaperredlc445v')) |
| 631 |
self.assertEqual(DecodedBech32(segwit_addr.Encoding.BECH32M, '?', []), |
| 632 |
segwit_addr.bech32_decode('?1v759aa')) |
| 633 |
|
| 634 |
self.assertEqual(DecodedBech32(None, None, None), |
| 635 |
segwit_addr.bech32_decode('\x201xj0phk')) |
| 636 |
self.assertEqual(DecodedBech32(None, None, None), |
| 637 |
segwit_addr.bech32_decode('\x7f1g6xzxy')) |
| 638 |
self.assertEqual(DecodedBech32(None, None, None), |
| 639 |
segwit_addr.bech32_decode('\x801vctc34')) |
| 640 |
self.assertEqual(DecodedBech32(None, None, None), |
| 641 |
segwit_addr.bech32_decode('an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4')) |
| 642 |
self.assertEqual(DecodedBech32(None, None, None), |
| 643 |
segwit_addr.bech32_decode('qyrz8wqd2c9m')) |
| 644 |
self.assertEqual(DecodedBech32(None, None, None), |
| 645 |
segwit_addr.bech32_decode('1qyrz8wqd2c9m')) |
| 646 |
self.assertEqual(DecodedBech32(None, None, None), |
| 647 |
segwit_addr.bech32_decode('y1b0jsk6g')) |
| 648 |
self.assertEqual(DecodedBech32(None, None, None), |
| 649 |
segwit_addr.bech32_decode('lt1igcx5c0')) |
| 650 |
self.assertEqual(DecodedBech32(None, None, None), |
| 651 |
segwit_addr.bech32_decode('in1muywd')) |
| 652 |
self.assertEqual(DecodedBech32(None, None, None), |
| 653 |
segwit_addr.bech32_decode('mm1crxm3i')) |
| 654 |
self.assertEqual(DecodedBech32(None, None, None), |
| 655 |
segwit_addr.bech32_decode('au1s5cgom')) |
| 656 |
self.assertEqual(DecodedBech32(None, None, None), |
| 657 |
segwit_addr.bech32_decode('M1VUXWEZ')) |
| 658 |
self.assertEqual(DecodedBech32(None, None, None), |
| 659 |
segwit_addr.bech32_decode('16plkw9')) |
| 660 |
self.assertEqual(DecodedBech32(None, None, None), |
| 661 |
segwit_addr.bech32_decode('1p2gdwpf')) |
| 662 |
|
| 663 |
|
| 664 |
bolt12_str = 'lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg' |
| 665 |
self.assertEqual(DecodedBech32(None, None, None), |
| 666 |
segwit_addr.bech32_decode(bolt12_str, with_checksum=True, ignore_long_length=True)) |
| 667 |
self.assertEqual(DecodedBech32(None, 'lno', [1, 0, 1, 16, 30, 16, 18, 0, 1, 8, 11, 4, 2, 27, 17, 0, 12, 21, 28, 6, 2, 27, 11, 16, 13, 17, 18, 18, 0, 25, 3, 5, 14, 13, 17, 23, 4, 26, 11, 16, 14, 17, 20, 22, 30, 27, 16, 18, 2, 9, 1, 4, 30, 19, 2, 20, 4, 0, 24, 19, 4, 8, 3, 9, 13, 25, 18, 7, 10, 28, 27, 20, 14, 9, 20, 22, 10, 28, 24, 22, 4, 4, 1, 14, 29, 17, 25, 4, 11, 21, 21, 23, 26, 11, 6, 11, 6, 0, 28, 0, 23, 30, 31, 2, 20, 13, 18, 8, 25, 21, 29, 9, 8, 9, 18, 19, 30, 22, 21, 3, 8, 3, 22, 28, 29, 8, 15, 18, 16, 13, 20, 6, 12, 6, 8]), |
| 668 |
segwit_addr.bech32_decode(bolt12_str, with_checksum=False, ignore_long_length=True)) |
| 669 |
|
| 670 |
|
| 671 |
class Test_xprv_xpub(ElectrumTestCase): |
| 672 |
|
| 673 |
xprv_xpub = ( |
| 674 |
|
| 675 |
{'xprv': 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76', |
| 676 |
'xpub': 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy', |
| 677 |
'xtype': 'standard'}, |
| 678 |
{'xprv': 'yprvAJEYHeNEPcyBoQYM7sGCxDiNCTX65u4ANgZuSGTrKN5YCC9MP84SBayrgaMyZV7zvkHrr3HVPTK853s2SPk4EttPazBZBmz6QfDkXeE8Zr7', |
| 679 |
'xpub': 'ypub6XDth9u8DzXV1tcpDtoDKMf6kVMaVMn1juVWEesTshcX4zUVvfNgjPJLXrD9N7AdTLnbHFL64KmBn3SNaTe69iZYbYCqLCCNPZKbLz9niQ4', |
| 680 |
'xtype': 'p2wpkh-p2sh'}, |
| 681 |
{'xprv': 'zprvAWgYBBk7JR8GkraNZJeEodAp2UR1VRWJTXyV1ywuUVs1awUgTiBS1ZTDtLA5F3MFDn1LZzu8dUpSKdT7ToDpvEG6PQu4bJs7zQY47Sd3sEZ', |
| 682 |
'xpub': 'zpub6jftahH18ngZyLeqfLBFAm7YaWFVttE9pku5pNMX2qPzTjoq1FVgZMmhjecyB2nqFb31gHE9vNvbaggU6vvWpNZbXEWLLUjYjFqG95LNyT8', |
| 683 |
'xtype': 'p2wpkh'}, |
| 684 |
) |
| 685 |
|
| 686 |
def _do_test_bip32(self, seed: str, sequence: str): |
| 687 |
node = BIP32Node.from_rootseed(bfh(seed), xtype='standard') |
| 688 |
xprv, xpub = node.to_xprv(), node.to_xpub() |
| 689 |
int_path = convert_bip32_strpath_to_intpath(sequence) |
| 690 |
for n in int_path: |
| 691 |
if n & bip32.BIP32_PRIME == 0: |
| 692 |
xpub2 = BIP32Node.from_xkey(xpub).subkey_at_public_derivation([n]).to_xpub() |
| 693 |
node = BIP32Node.from_xkey(xprv).subkey_at_private_derivation([n]) |
| 694 |
xprv, xpub = node.to_xprv(), node.to_xpub() |
| 695 |
if n & bip32.BIP32_PRIME == 0: |
| 696 |
self.assertEqual(xpub, xpub2) |
| 697 |
|
| 698 |
return xpub, xprv |
| 699 |
|
| 700 |
def test_bip32(self): |
| 701 |
|
| 702 |
|
| 703 |
xpub, xprv = self._do_test_bip32("000102030405060708090a0b0c0d0e0f", "m/0'/1/2'/2/1000000000") |
| 704 |
self.assertEqual("xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy", xpub) |
| 705 |
self.assertEqual("xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76", xprv) |
| 706 |
|
| 707 |
xpub, xprv = self._do_test_bip32("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542","m/0/2147483647'/1/2147483646'/2") |
| 708 |
self.assertEqual("xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt", xpub) |
| 709 |
self.assertEqual("xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j", xprv) |
| 710 |
|
| 711 |
xpub, xprv = self._do_test_bip32("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be", "m/0h") |
| 712 |
self.assertEqual("xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y", xpub) |
| 713 |
self.assertEqual("xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L", xprv) |
| 714 |
|
| 715 |
xpub, xprv = self._do_test_bip32("3ddd5602285899a946114506157c7997e5444528f3003f6134712147db19b678", "m/0h/1h") |
| 716 |
self.assertEqual("xpub6BJA1jSqiukeaesWfxe6sNK9CCGaujFFSJLomWHprUL9DePQ4JDkM5d88n49sMGJxrhpjazuXYWdMf17C9T5XnxkopaeS7jGk1GyyVziaMt", xpub) |
| 717 |
self.assertEqual("xprv9xJocDuwtYCMNAo3Zw76WENQeAS6WGXQ55RCy7tDJ8oALr4FWkuVoHJeHVAcAqiZLE7Je3vZJHxspZdFHfnBEjHqU5hG1Jaj32dVoS6XLT1", xprv) |
| 718 |
|
| 719 |
def test_xpub_from_xprv(self): |
| 720 |
"""We can derive the xpub key from a xprv.""" |
| 721 |
for xprv_details in self.xprv_xpub: |
| 722 |
result = xpub_from_xprv(xprv_details['xprv']) |
| 723 |
self.assertEqual(result, xprv_details['xpub']) |
| 724 |
|
| 725 |
def test_is_xpub(self): |
| 726 |
for xprv_details in self.xprv_xpub: |
| 727 |
xpub = xprv_details['xpub'] |
| 728 |
self.assertTrue(is_xpub(xpub)) |
| 729 |
self.assertFalse(is_xpub('xpub1nval1d')) |
| 730 |
self.assertFalse(is_xpub('xpub661MyMwAqRbcFWohJWt7PHsFEJfZAvw9ZxwQoDa4SoMgsDDM1T7WK3u9E4edkC4ugRnZ8E4xDZRpk8Rnts3Nbt97dPwT52WRONGBADWRONG')) |
| 731 |
|
| 732 |
def test_xpub_type(self): |
| 733 |
for xprv_details in self.xprv_xpub: |
| 734 |
xpub = xprv_details['xpub'] |
| 735 |
self.assertEqual(xprv_details['xtype'], xpub_type(xpub)) |
| 736 |
|
| 737 |
def test_is_xprv(self): |
| 738 |
for xprv_details in self.xprv_xpub: |
| 739 |
xprv = xprv_details['xprv'] |
| 740 |
self.assertTrue(is_xprv(xprv)) |
| 741 |
self.assertFalse(is_xprv('xprv1nval1d')) |
| 742 |
self.assertFalse(is_xprv('xprv661MyMwAqRbcFWohJWt7PHsFEJfZAvw9ZxwQoDa4SoMgsDDM1T7WK3u9E4edkC4ugRnZ8E4xDZRpk8Rnts3Nbt97dPwT52WRONGBADWRONG')) |
| 743 |
|
| 744 |
def test_bip32_from_xkey(self): |
| 745 |
bip32node1 = BIP32Node.from_xkey("xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy") |
| 746 |
self.assertEqual( |
| 747 |
BIP32Node( |
| 748 |
xtype='standard', |
| 749 |
eckey=ecc.ECPubkey(bytes.fromhex("022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011")), |
| 750 |
chaincode=bytes.fromhex("c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e"), |
| 751 |
depth=5, |
| 752 |
fingerprint=bytes.fromhex("d880d7d8"), |
| 753 |
child_number=bytes.fromhex("3b9aca00"), |
| 754 |
), |
| 755 |
bip32node1) |
| 756 |
with self.assertRaises(ValueError): |
| 757 |
BIP32Node.from_xkey( |
| 758 |
"zpub6jftahH18ngZyLeqfLBFAm7YaWFVttE9pku5pNMX2qPzTjoq1FVgZMmhjecyB2nqFb31gHE9vNvbaggU6vvWpNZbXEWLLUjYjFqG95LNyT8", |
| 759 |
allow_custom_headers=False) |
| 760 |
bip32node2 = BIP32Node.from_xkey( |
| 761 |
"zpub6jftahH18ngZyLeqfLBFAm7YaWFVttE9pku5pNMX2qPzTjoq1FVgZMmhjecyB2nqFb31gHE9vNvbaggU6vvWpNZbXEWLLUjYjFqG95LNyT8", |
| 762 |
allow_custom_headers=True) |
| 763 |
self.assertEqual(bytes.fromhex("03f18e53f3386a5f9a9d2c369ad3b84b429eb397b4bc69ce600f2d833b54ba32f4"), |
| 764 |
bip32node2.eckey.get_public_key_bytes(compressed=True)) |
| 765 |
|
| 766 |
def test_is_bip32_derivation(self): |
| 767 |
self.assertTrue(is_bip32_derivation("m/0'/1")) |
| 768 |
self.assertTrue(is_bip32_derivation("m/0'/0'")) |
| 769 |
self.assertTrue(is_bip32_derivation("m/3'/-5/8h/")) |
| 770 |
self.assertTrue(is_bip32_derivation("m/44'/0'/0'/0/0")) |
| 771 |
self.assertTrue(is_bip32_derivation("m/49'/0'/0'/0/0")) |
| 772 |
self.assertTrue(is_bip32_derivation("m")) |
| 773 |
self.assertTrue(is_bip32_derivation("m/")) |
| 774 |
self.assertFalse(is_bip32_derivation("m5")) |
| 775 |
self.assertFalse(is_bip32_derivation("mmmmmm")) |
| 776 |
self.assertFalse(is_bip32_derivation("n/")) |
| 777 |
self.assertFalse(is_bip32_derivation("")) |
| 778 |
self.assertFalse(is_bip32_derivation("m/q8462")) |
| 779 |
self.assertFalse(is_bip32_derivation("m/-8h")) |
| 780 |
|
| 781 |
def test_convert_bip32_strpath_to_intpath(self): |
| 782 |
self.assertEqual([0, 0x80000001, 0x80000001], convert_bip32_strpath_to_intpath("m/0/-1/1'")) |
| 783 |
self.assertEqual([], convert_bip32_strpath_to_intpath("m/")) |
| 784 |
self.assertEqual([2147483692, 2147488889, 221], convert_bip32_strpath_to_intpath("m/44'/5241h/221")) |
| 785 |
|
| 786 |
def test_convert_bip32_intpath_to_strpath(self): |
| 787 |
self.assertEqual("m/0/1h/1h", convert_bip32_intpath_to_strpath([0, 0x80000001, 0x80000001])) |
| 788 |
self.assertEqual("m", convert_bip32_intpath_to_strpath([])) |
| 789 |
self.assertEqual("m/44h/5241h/221", convert_bip32_intpath_to_strpath([2147483692, 2147488889, 221])) |
| 790 |
|
| 791 |
self.assertEqual("m/0/1'/1'", convert_bip32_intpath_to_strpath([0, 0x80000001, 0x80000001], hardened_char="'")) |
| 792 |
self.assertEqual("m", convert_bip32_intpath_to_strpath([], hardened_char="'")) |
| 793 |
self.assertEqual("m/44'/5241'/221", convert_bip32_intpath_to_strpath([2147483692, 2147488889, 221], hardened_char="'")) |
| 794 |
|
| 795 |
def test_normalize_bip32_derivation(self): |
| 796 |
self.assertEqual("m/0/1h/1h", normalize_bip32_derivation("m/0/1h/1'")) |
| 797 |
self.assertEqual("m", normalize_bip32_derivation("m////")) |
| 798 |
self.assertEqual("m/0/2/1h", normalize_bip32_derivation("m/0/2/-1/")) |
| 799 |
self.assertEqual("m/0/1h/1h/5h", normalize_bip32_derivation("m/0//-1/1'///5h")) |
| 800 |
|
| 801 |
def test_is_xkey_consistent_with_key_origin_info(self): |
| 802 |
|
| 803 |
self.assertTrue(bip32.is_xkey_consistent_with_key_origin_info( |
| 804 |
"Zpub75NQordWKAkaF7utBw95GEodyxqwFdR3idtTqQtrvWkYFeiuYdg5c3Q9L9bLjPLhEahLCTjmmS2YQcXPwr6twYCEJ55k6uhE5JxRqvUowmd", |
| 805 |
derivation_prefix="m/48'/1'/0'/2'", |
| 806 |
root_fingerprint="b2768d2f")) |
| 807 |
|
| 808 |
self.assertTrue(bip32.is_xkey_consistent_with_key_origin_info( |
| 809 |
"Zpub75NQordWKAkaF7utBw95GEodyxqwFdR3idtTqQtrvWkYFeiuYdg5c3Q9L9bLjPLhEahLCTjmmS2YQcXPwr6twYCEJ55k6uhE5JxRqvUowmd", |
| 810 |
derivation_prefix="m/48'/1'/0'/2'")) |
| 811 |
self.assertTrue(bip32.is_xkey_consistent_with_key_origin_info( |
| 812 |
"Zpub75NQordWKAkaF7utBw95GEodyxqwFdR3idtTqQtrvWkYFeiuYdg5c3Q9L9bLjPLhEahLCTjmmS2YQcXPwr6twYCEJ55k6uhE5JxRqvUowmd", |
| 813 |
root_fingerprint="b2768d2f")) |
| 814 |
|
| 815 |
self.assertFalse(bip32.is_xkey_consistent_with_key_origin_info( |
| 816 |
"Zpub75NQordWKAkaF7utBw95GEodyxqwFdR3idtTqQtrvWkYFeiuYdg5c3Q9L9bLjPLhEahLCTjmmS2YQcXPwr6twYCEJ55k6uhE5JxRqvUowmd", |
| 817 |
derivation_prefix="m/48'/0'/2'", |
| 818 |
root_fingerprint="b2768d2f")) |
| 819 |
|
| 820 |
self.assertFalse(bip32.is_xkey_consistent_with_key_origin_info( |
| 821 |
"Zpub75NQordWKAkaF7utBw95GEodyxqwFdR3idtTqQtrvWkYFeiuYdg5c3Q9L9bLjPLhEahLCTjmmS2YQcXPwr6twYCEJ55k6uhE5JxRqvUowmd", |
| 822 |
derivation_prefix="m/48'/1'/0'/3'", |
| 823 |
root_fingerprint="b2768d2f")) |
| 824 |
|
| 825 |
self.assertTrue(bip32.is_xkey_consistent_with_key_origin_info( |
| 826 |
"Zpub75NQordWKAkaF7utBw95GEodyxqwFdR3idtTqQtrvWkYFeiuYdg5c3Q9L9bLjPLhEahLCTjmmS2YQcXPwr6twYCEJ55k6uhE5JxRqvUowmd", |
| 827 |
derivation_prefix="m/48'/1'/1'/2'", |
| 828 |
root_fingerprint="b2768d2f")) |
| 829 |
|
| 830 |
self.assertTrue(bip32.is_xkey_consistent_with_key_origin_info( |
| 831 |
"Zpub75NQordWKAkaF7utBw95GEodyxqwFdR3idtTqQtrvWkYFeiuYdg5c3Q9L9bLjPLhEahLCTjmmS2YQcXPwr6twYCEJ55k6uhE5JxRqvUowmd", |
| 832 |
derivation_prefix="m/48'/1'/0'/2'", |
| 833 |
root_fingerprint="aaaaaaaa")) |
| 834 |
|
| 835 |
|
| 836 |
self.assertTrue(bip32.is_xkey_consistent_with_key_origin_info( |
| 837 |
"zpub6nsHdRuY92FsMKdbn9BfjBCG6X8pyhCibNP6uDvpnw2cyrVhecvHRMa3Ne8kdJZxjxgwnpbHLkcR4bfnhHy6auHPJyDTQ3kianeuVLdkCYQ", |
| 838 |
derivation_prefix="m/0'", |
| 839 |
root_fingerprint="b2e35a7d")) |
| 840 |
|
| 841 |
self.assertFalse(bip32.is_xkey_consistent_with_key_origin_info( |
| 842 |
"zpub6nsHdRuY92FsMKdbn9BfjBCG6X8pyhCibNP6uDvpnw2cyrVhecvHRMa3Ne8kdJZxjxgwnpbHLkcR4bfnhHy6auHPJyDTQ3kianeuVLdkCYQ", |
| 843 |
derivation_prefix="m/0'/0'", |
| 844 |
root_fingerprint="b2e35a7d")) |
| 845 |
|
| 846 |
self.assertFalse(bip32.is_xkey_consistent_with_key_origin_info( |
| 847 |
"zpub6nsHdRuY92FsMKdbn9BfjBCG6X8pyhCibNP6uDvpnw2cyrVhecvHRMa3Ne8kdJZxjxgwnpbHLkcR4bfnhHy6auHPJyDTQ3kianeuVLdkCYQ", |
| 848 |
derivation_prefix="m/1'", |
| 849 |
root_fingerprint="b2e35a7d")) |
| 850 |
|
| 851 |
self.assertFalse(bip32.is_xkey_consistent_with_key_origin_info( |
| 852 |
"zpub6nsHdRuY92FsMKdbn9BfjBCG6X8pyhCibNP6uDvpnw2cyrVhecvHRMa3Ne8kdJZxjxgwnpbHLkcR4bfnhHy6auHPJyDTQ3kianeuVLdkCYQ", |
| 853 |
derivation_prefix="m/0'", |
| 854 |
root_fingerprint="aaaaaaaa")) |
| 855 |
|
| 856 |
|
| 857 |
self.assertTrue(bip32.is_xkey_consistent_with_key_origin_info( |
| 858 |
"xpub661MyMwAqRbcFWohJWt7PHsFEJfZAvw9ZxwQoDa4SoMgsDDM1T7WK3u9E4edkC4ugRnZ8E4xDZRpk8Rnts3Nbt97dPwT52CwBdDWroaZf8U", |
| 859 |
derivation_prefix="m", |
| 860 |
root_fingerprint="48adc7a0")) |
| 861 |
|
| 862 |
self.assertFalse(bip32.is_xkey_consistent_with_key_origin_info( |
| 863 |
"xpub661MyMwAqRbcFWohJWt7PHsFEJfZAvw9ZxwQoDa4SoMgsDDM1T7WK3u9E4edkC4ugRnZ8E4xDZRpk8Rnts3Nbt97dPwT52CwBdDWroaZf8U", |
| 864 |
derivation_prefix="m/0", |
| 865 |
root_fingerprint="48adc7a0")) |
| 866 |
|
| 867 |
self.assertFalse(bip32.is_xkey_consistent_with_key_origin_info( |
| 868 |
"xpub661MyMwAqRbcFWohJWt7PHsFEJfZAvw9ZxwQoDa4SoMgsDDM1T7WK3u9E4edkC4ugRnZ8E4xDZRpk8Rnts3Nbt97dPwT52CwBdDWroaZf8U", |
| 869 |
derivation_prefix="m", |
| 870 |
root_fingerprint="aaaaaaaa")) |
| 871 |
|
| 872 |
def test_is_all_public_derivation(self): |
| 873 |
self.assertFalse(is_all_public_derivation("m/0/1'/1'")) |
| 874 |
self.assertFalse(is_all_public_derivation("m/0/2/1'")) |
| 875 |
self.assertFalse(is_all_public_derivation("m/0/1'/1'/5")) |
| 876 |
self.assertTrue(is_all_public_derivation("m")) |
| 877 |
self.assertTrue(is_all_public_derivation("m/0")) |
| 878 |
self.assertTrue(is_all_public_derivation("m/75/22/3")) |
| 879 |
|
| 880 |
def test_xtype_from_derivation(self): |
| 881 |
self.assertEqual('standard', xtype_from_derivation("m/44'")) |
| 882 |
self.assertEqual('standard', xtype_from_derivation("m/44'/")) |
| 883 |
self.assertEqual('standard', xtype_from_derivation("m/44'/0'/0'")) |
| 884 |
self.assertEqual('standard', xtype_from_derivation("m/44'/5241'/221")) |
| 885 |
self.assertEqual('standard', xtype_from_derivation("m/45'")) |
| 886 |
self.assertEqual('standard', xtype_from_derivation("m/45'/56165/271'")) |
| 887 |
self.assertEqual('p2wpkh-p2sh', xtype_from_derivation("m/49'")) |
| 888 |
self.assertEqual('p2wpkh-p2sh', xtype_from_derivation("m/49'/134")) |
| 889 |
self.assertEqual('p2wpkh', xtype_from_derivation("m/84'")) |
| 890 |
self.assertEqual('p2wpkh', xtype_from_derivation("m/84'/112'/992/112/33'/0/2")) |
| 891 |
self.assertEqual('p2wsh-p2sh', xtype_from_derivation("m/48'/0'/0'/1'")) |
| 892 |
self.assertEqual('p2wsh-p2sh', xtype_from_derivation("m/48'/0'/0'/1'/52112/52'")) |
| 893 |
self.assertEqual('p2wsh-p2sh', xtype_from_derivation("m/48'/9'/2'/1'")) |
| 894 |
self.assertEqual('p2wsh', xtype_from_derivation("m/48'/0'/0'/2'")) |
| 895 |
self.assertEqual('p2wsh', xtype_from_derivation("m/48'/1'/0'/2'/77'/0")) |
| 896 |
|
| 897 |
def test_version_bytes(self): |
| 898 |
xprv_headers_b58 = { |
| 899 |
'standard': 'xprv', |
| 900 |
'p2wpkh-p2sh': 'yprv', |
| 901 |
'p2wsh-p2sh': 'Yprv', |
| 902 |
'p2wpkh': 'zprv', |
| 903 |
'p2wsh': 'Zprv', |
| 904 |
} |
| 905 |
xpub_headers_b58 = { |
| 906 |
'standard': 'xpub', |
| 907 |
'p2wpkh-p2sh': 'ypub', |
| 908 |
'p2wsh-p2sh': 'Ypub', |
| 909 |
'p2wpkh': 'zpub', |
| 910 |
'p2wsh': 'Zpub', |
| 911 |
} |
| 912 |
for xtype, xkey_header_bytes in constants.net.XPRV_HEADERS.items(): |
| 913 |
xkey_header_bytes = bfh("%08x" % xkey_header_bytes) |
| 914 |
xkey_bytes = xkey_header_bytes + bytes([0] * 74) |
| 915 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 916 |
self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) |
| 917 |
|
| 918 |
xkey_bytes = xkey_header_bytes + bytes([255] * 74) |
| 919 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 920 |
self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) |
| 921 |
|
| 922 |
for xtype, xkey_header_bytes in constants.net.XPUB_HEADERS.items(): |
| 923 |
xkey_header_bytes = bfh("%08x" % xkey_header_bytes) |
| 924 |
xkey_bytes = xkey_header_bytes + bytes([0] * 74) |
| 925 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 926 |
self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) |
| 927 |
|
| 928 |
xkey_bytes = xkey_header_bytes + bytes([255] * 74) |
| 929 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 930 |
self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) |
| 931 |
|
| 932 |
|
| 933 |
class Test_xprv_xpub_testnet(ElectrumTestCase): |
| 934 |
TESTNET = True |
| 935 |
|
| 936 |
def test_version_bytes(self): |
| 937 |
xprv_headers_b58 = { |
| 938 |
'standard': 'tprv', |
| 939 |
'p2wpkh-p2sh': 'uprv', |
| 940 |
'p2wsh-p2sh': 'Uprv', |
| 941 |
'p2wpkh': 'vprv', |
| 942 |
'p2wsh': 'Vprv', |
| 943 |
} |
| 944 |
xpub_headers_b58 = { |
| 945 |
'standard': 'tpub', |
| 946 |
'p2wpkh-p2sh': 'upub', |
| 947 |
'p2wsh-p2sh': 'Upub', |
| 948 |
'p2wpkh': 'vpub', |
| 949 |
'p2wsh': 'Vpub', |
| 950 |
} |
| 951 |
for xtype, xkey_header_bytes in constants.net.XPRV_HEADERS.items(): |
| 952 |
xkey_header_bytes = bfh("%08x" % xkey_header_bytes) |
| 953 |
xkey_bytes = xkey_header_bytes + bytes([0] * 74) |
| 954 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 955 |
self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) |
| 956 |
|
| 957 |
xkey_bytes = xkey_header_bytes + bytes([255] * 74) |
| 958 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 959 |
self.assertTrue(xkey_b58.startswith(xprv_headers_b58[xtype])) |
| 960 |
|
| 961 |
for xtype, xkey_header_bytes in constants.net.XPUB_HEADERS.items(): |
| 962 |
xkey_header_bytes = bfh("%08x" % xkey_header_bytes) |
| 963 |
xkey_bytes = xkey_header_bytes + bytes([0] * 74) |
| 964 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 965 |
self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) |
| 966 |
|
| 967 |
xkey_bytes = xkey_header_bytes + bytes([255] * 74) |
| 968 |
xkey_b58 = EncodeBase58Check(xkey_bytes) |
| 969 |
self.assertTrue(xkey_b58.startswith(xpub_headers_b58[xtype])) |
| 970 |
|
| 971 |
|
| 972 |
class Test_keyImport(ElectrumTestCase): |
| 973 |
|
| 974 |
priv_pub_addr = ( |
| 975 |
{'priv': 'KzMFjMC2MPadjvX5Cd7b8AKKjjpBSoRKUTpoAtN6B3J9ezWYyXS6', |
| 976 |
'exported_privkey': 'p2pkh:KzMFjMC2MPadjvX5Cd7b8AKKjjpBSoRKUTpoAtN6B3J9ezWYyXS6', |
| 977 |
'pub': '02c6467b7e621144105ed3e4835b0b4ab7e35266a2ae1c4f8baa19e9ca93452997', |
| 978 |
'address': '17azqT8T16coRmWKYFj3UjzJuxiYrYFRBR', |
| 979 |
'minikey' : False, |
| 980 |
'txin_type': 'p2pkh', |
| 981 |
'compressed': True, |
| 982 |
'addr_encoding': 'base58', |
| 983 |
'scripthash': 'c9aecd1fef8d661a42c560bf75c8163e337099800b8face5ca3d1393a30508a7'}, |
| 984 |
{'priv': 'p2pkh:Kzj8VjwpZ99bQqVeUiRXrKuX9mLr1o6sWxFMCBJn1umC38BMiQTD', |
| 985 |
'exported_privkey': 'p2pkh:Kzj8VjwpZ99bQqVeUiRXrKuX9mLr1o6sWxFMCBJn1umC38BMiQTD', |
| 986 |
'pub': '0352d78b4b37e0f6d4e164423436f2925fa57817467178eca550a88f2821973c41', |
| 987 |
'address': '1GXgZ5Qi6gmXTHVSpUPZLy4Ci2nbfb3ZNb', |
| 988 |
'minikey': False, |
| 989 |
'txin_type': 'p2pkh', |
| 990 |
'compressed': True, |
| 991 |
'addr_encoding': 'base58', |
| 992 |
'scripthash': 'a9b2a76fc196c553b352186dfcca81fcf323a721cd8431328f8e9d54216818c1'}, |
| 993 |
{'priv': '5Hxn5C4SQuiV6e62A1MtZmbSeQyrLFhu5uYks62pU5VBUygK2KD', |
| 994 |
'exported_privkey': 'p2pkh:5Hxn5C4SQuiV6e62A1MtZmbSeQyrLFhu5uYks62pU5VBUygK2KD', |
| 995 |
'pub': '04e5fe91a20fac945845a5518450d23405ff3e3e1ce39827b47ee6d5db020a9075422d56a59195ada0035e4a52a238849f68e7a325ba5b2247013e0481c5c7cb3f', |
| 996 |
'address': '1GPHVTY8UD9my6jyP4tb2TYJwUbDetyNC6', |
| 997 |
'minikey': False, |
| 998 |
'txin_type': 'p2pkh', |
| 999 |
'compressed': False, |
| 1000 |
'addr_encoding': 'base58', |
| 1001 |
'scripthash': 'f5914651408417e1166f725a5829ff9576d0dbf05237055bf13abd2af7f79473'}, |
| 1002 |
{'priv': 'p2pkh:5KhYQCe1xd5g2tqpmmGpUWDpDuTbA8vnpbiCNDwMPAx29WNQYfN', |
| 1003 |
'exported_privkey': 'p2pkh:5KhYQCe1xd5g2tqpmmGpUWDpDuTbA8vnpbiCNDwMPAx29WNQYfN', |
| 1004 |
'pub': '048f0431b0776e8210376c81280011c2b68be43194cb00bd47b7e9aa66284b713ce09556cde3fee606051a07613f3c159ef3953b8927c96ae3dae94a6ba4182e0e', |
| 1005 |
'address': '147kiRHHm9fqeMQSgqf4k35XzuWLP9fmmS', |
| 1006 |
'minikey': False, |
| 1007 |
'txin_type': 'p2pkh', |
| 1008 |
'compressed': False, |
| 1009 |
'addr_encoding': 'base58', |
| 1010 |
'scripthash': '6dd2e07ad2de9ba8eec4bbe8467eb53f8845acff0d9e6f5627391acc22ff62df'}, |
| 1011 |
{'priv': 'LHJnnvRzsdrTX2j5QeWVsaBkabK7gfMNqNNqxnbBVRaJYfk24iJz', |
| 1012 |
'exported_privkey': 'p2wpkh-p2sh:Kz9XebiCXL2BZzhYJViiHDzn5iup1povWV8aqstzWU4sz1K5nVva', |
| 1013 |
'pub': '0279ad237ca0d812fb503ab86f25e15ebd5fa5dd95c193639a8a738dcd1acbad81', |
| 1014 |
'address': '3GeVJB3oKr7psgKR6BTXSxKtWUkfsHHhk7', |
| 1015 |
'minikey': False, |
| 1016 |
'txin_type': 'p2wpkh-p2sh', |
| 1017 |
'compressed': True, |
| 1018 |
'addr_encoding': 'base58', |
| 1019 |
'scripthash': 'd7b04e882fa6b13246829ac552a2b21461d9152eb00f0a6adb58457a3e63d7c5'}, |
| 1020 |
{'priv': 'p2wpkh-p2sh:L3CZH1pm87X4bbE6mSGvZnAZ1KcFDRomBudUkrkBG7EZhDtBVXMW', |
| 1021 |
'exported_privkey': 'p2wpkh-p2sh:L3CZH1pm87X4bbE6mSGvZnAZ1KcFDRomBudUkrkBG7EZhDtBVXMW', |
| 1022 |
'pub': '0229da20a15b3363b2c28e3c5093c180b56c439df0b968a970366bb1f38435361e', |
| 1023 |
'address': '3C79goMwT7zSTjXnPoCg6VFGAnUpZAkyus', |
| 1024 |
'minikey': False, |
| 1025 |
'txin_type': 'p2wpkh-p2sh', |
| 1026 |
'compressed': True, |
| 1027 |
'addr_encoding': 'base58', |
| 1028 |
'scripthash': '714bf6bfe1083e69539f40d4c7a7dca85d187471b35642e55f20d7e866494cf7'}, |
| 1029 |
{'priv': 'L8g5V8kFFeg2WbecahRSdobARbHz2w2STH9S8ePHVSY4fmia7Rsj', |
| 1030 |
'exported_privkey': 'p2wpkh:Kz6SuyPM5VktY5dr2d2YqdVgBA6LCWkiHqXJaC3BzxnMPSUuYzmF', |
| 1031 |
'pub': '03e9f948421aaa89415dc5f281a61b60dde12aae3181b3a76cd2d849b164fc6d0b', |
| 1032 |
'address': 'bc1qqmpt7u5e9hfznljta5gnvhyvfd2kdd0r90hwue', |
| 1033 |
'minikey': False, |
| 1034 |
'txin_type': 'p2wpkh', |
| 1035 |
'compressed': True, |
| 1036 |
'addr_encoding': 'bech32', |
| 1037 |
'scripthash': '1929acaaef3a208c715228e9f1ca0318e3a6b9394ab53c8d026137f847ecf97b'}, |
| 1038 |
{'priv': 'p2wpkh:KyDWy5WbjLA58Zesh1o8m3pADGdJ3v33DKk4m7h8BD5zDKDmDFwo', |
| 1039 |
'exported_privkey': 'p2wpkh:KyDWy5WbjLA58Zesh1o8m3pADGdJ3v33DKk4m7h8BD5zDKDmDFwo', |
| 1040 |
'pub': '038c57657171c1f73e34d5b3971d05867d50221ad94980f7e87cbc2344425e6a1e', |
| 1041 |
'address': 'bc1qpakeeg4d9ydyjxd8paqrw4xy9htsg532xzxn50', |
| 1042 |
'minikey': False, |
| 1043 |
'txin_type': 'p2wpkh', |
| 1044 |
'compressed': True, |
| 1045 |
'addr_encoding': 'bech32', |
| 1046 |
'scripthash': '242f02adde84ebb2a7dd778b2f3a81b3826f111da4d8960d826d7a4b816cb261'}, |
| 1047 |
|
| 1048 |
{'priv': 'SzavMBLoXU6kDrqtUVmffv', |
| 1049 |
'exported_privkey': 'p2pkh:5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF', |
| 1050 |
'pub': '04588d202afcc1ee4ab5254c7847ec25b9a135bbda0f2bc69ee1a714749fd77dc9f88ff2a00d7e752d44cbe16e1ebcf0890b76ec7c78886109dee76ccfc8445424', |
| 1051 |
'address': '1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj', |
| 1052 |
'minikey': True, |
| 1053 |
'txin_type': 'p2pkh', |
| 1054 |
'compressed': False, |
| 1055 |
'addr_encoding': 'base58', |
| 1056 |
'scripthash': '5b07ddfde826f5125ee823900749103cea37808038ecead5505a766a07c34445'}, |
| 1057 |
) |
| 1058 |
|
| 1059 |
def test_public_key_from_private_key(self): |
| 1060 |
for priv_details in self.priv_pub_addr: |
| 1061 |
txin_type, privkey, compressed = deserialize_privkey(priv_details['priv']) |
| 1062 |
result = ecc.ECPrivkey(privkey).get_public_key_hex(compressed=compressed) |
| 1063 |
self.assertEqual(priv_details['pub'], result) |
| 1064 |
self.assertEqual(priv_details['txin_type'], txin_type) |
| 1065 |
self.assertEqual(priv_details['compressed'], compressed) |
| 1066 |
|
| 1067 |
def test_address_from_private_key(self): |
| 1068 |
for priv_details in self.priv_pub_addr: |
| 1069 |
addr2 = address_from_private_key(priv_details['priv']) |
| 1070 |
self.assertEqual(priv_details['address'], addr2) |
| 1071 |
|
| 1072 |
def test_is_valid_address(self): |
| 1073 |
for priv_details in self.priv_pub_addr: |
| 1074 |
addr = priv_details['address'] |
| 1075 |
self.assertFalse(is_address(priv_details['priv'])) |
| 1076 |
self.assertFalse(is_address(priv_details['pub'])) |
| 1077 |
self.assertTrue(is_address(addr)) |
| 1078 |
|
| 1079 |
is_enc_b58 = priv_details['addr_encoding'] == 'base58' |
| 1080 |
self.assertEqual(is_enc_b58, is_b58_address(addr)) |
| 1081 |
|
| 1082 |
is_enc_bech32 = priv_details['addr_encoding'] == 'bech32' |
| 1083 |
self.assertEqual(is_enc_bech32, is_segwit_address(addr)) |
| 1084 |
|
| 1085 |
self.assertFalse(is_address("not an address")) |
| 1086 |
|
| 1087 |
def test_is_address_bad_checksums(self): |
| 1088 |
self.assertTrue(is_address('1819s5TxxbBtuRPr3qYskMVC8sb1pqapWx')) |
| 1089 |
self.assertFalse(is_address('1819s5TxxbBtuRPr3qYskMVC8sb1pqapWw')) |
| 1090 |
|
| 1091 |
self.assertTrue(is_address('3LrjLVnngqnaJeo3BQwMBg34iqYsjZjQUe')) |
| 1092 |
self.assertFalse(is_address('3LrjLVnngqnaJeo3BQwMBg34iqYsjZjQUd')) |
| 1093 |
|
| 1094 |
self.assertTrue(is_address('bc1qxq64lrwt02hm7tu25lr3hm9tgzh58snfe67yt6')) |
| 1095 |
self.assertFalse(is_address('bc1qxq64lrwt02hm7tu25lr3hm9tgzh58snfe67yt5')) |
| 1096 |
|
| 1097 |
def test_is_private_key(self): |
| 1098 |
for priv_details in self.priv_pub_addr: |
| 1099 |
self.assertTrue(is_private_key(priv_details['priv'])) |
| 1100 |
self.assertTrue(is_private_key(priv_details['exported_privkey'])) |
| 1101 |
self.assertFalse(is_private_key(priv_details['pub'])) |
| 1102 |
self.assertFalse(is_private_key(priv_details['address'])) |
| 1103 |
self.assertFalse(is_private_key("not a privkey")) |
| 1104 |
|
| 1105 |
def test_serialize_privkey(self): |
| 1106 |
for priv_details in self.priv_pub_addr: |
| 1107 |
txin_type, privkey, compressed = deserialize_privkey(priv_details['priv']) |
| 1108 |
priv2 = serialize_privkey(privkey, compressed, txin_type) |
| 1109 |
self.assertEqual(priv_details['exported_privkey'], priv2) |
| 1110 |
|
| 1111 |
def test_address_to_scripthash(self): |
| 1112 |
for priv_details in self.priv_pub_addr: |
| 1113 |
sh = address_to_scripthash(priv_details['address']) |
| 1114 |
self.assertEqual(priv_details['scripthash'], sh) |
| 1115 |
|
| 1116 |
def test_is_minikey(self): |
| 1117 |
for priv_details in self.priv_pub_addr: |
| 1118 |
minikey = priv_details['minikey'] |
| 1119 |
priv = priv_details['priv'] |
| 1120 |
self.assertEqual(minikey, is_minikey(priv)) |
| 1121 |
|
| 1122 |
def test_is_compressed_privkey(self): |
| 1123 |
for priv_details in self.priv_pub_addr: |
| 1124 |
self.assertEqual(priv_details['compressed'], |
| 1125 |
is_compressed_privkey(priv_details['priv'])) |
| 1126 |
|
| 1127 |
def test_segwit_uncompressed_pubkey(self): |
| 1128 |
with self.assertRaises(BitcoinException): |
| 1129 |
is_private_key("p2wpkh-p2sh:5JKXxT3wAZHcybJ9YNkuHur9vou6uuAnorBV9A8vVxGNFH5wvTW", |
| 1130 |
raise_on_error=True) |
| 1131 |
|
| 1132 |
def test_wif_with_invalid_magic_byte_for_compressed_pubkey(self): |
| 1133 |
with self.assertRaises(BitcoinException): |
| 1134 |
is_private_key("KwFAa6AumokBD2dVqQLPou42jHiVsvThY1n25HJ8Ji8REf1wxAQb", |
| 1135 |
raise_on_error=True) |
| 1136 |
|
| 1137 |
|
| 1138 |
class TestBaseEncode(ElectrumTestCase): |
| 1139 |
|
| 1140 |
def test_base43(self): |
| 1141 |
tx_hex = "020000000001021cd0e96f9ca202e017ca3465e3c13373c0df3a4cdd91c1fd02ea42a1a65d2a410000000000fdffffff757da7cf8322e5063785e2d8ada74702d2648fa2add2d533ba83c52eb110df690200000000fdffffff02d07e010000000000160014b544c86eaf95e3bb3b6d2cabb12ab40fc59cad9ca086010000000000232102ce0d066fbfcf150a5a1bbc4f312cd2eb080e8d8a47e5f2ce1a63b23215e54fb5ac02483045022100a9856bf10a950810abceeabc9a86e6ba533e130686e3d7863971b9377e7c658a0220288a69ef2b958a7c2ecfa376841d4a13817ed24fa9a0e0a6b9cb48e6439794c701210324e291735f83ff8de47301b12034950b80fa4724926a34d67e413d8ff8817c53024830450221008f885978f7af746679200ed55fe2e86c1303620824721f95cc41eb7965a3dfcf02207872082ac4a3c433d41a203e6d685a459e70e551904904711626ac899238c20a0121023d4c9deae1aacf3f822dd97a28deaec7d4e4ff97be746d124a63d20e582f5b290a971600" |
| 1142 |
tx_bytes = bfh(tx_hex) |
| 1143 |
tx_base43 = base_encode(tx_bytes, base=43) |
| 1144 |
self.assertEqual("3E2DH7.J3PKVZJ3RCOXQVS3Y./6-WE.75DDU0K58-0N1FRL565N8ZH-DG1Z.1IGWTE5HK8F7PWH5P8+V3XGZZ6GQBPHNDE+RD8CAQVV1/6PQEMJIZTGPMIJ93B8P$QX+Y2R:TGT9QW8S89U4N2.+FUT8VG+34USI/N/JJ3CE*KLSW:REE8T5Y*9:U6515JIUR$6TODLYHSDE3B5DAF:5TF7V*VAL3G40WBOM0DO2+CFKTTM$G-SO:8U0EW:M8V:4*R9ZDX$B1IRBP9PLMDK8H801PNTFB4$HL1+/U3F61P$4N:UAO88:N5D+J:HI4YR8IM:3A7K1YZ9VMRC/47$6GGW5JEL1N690TDQ4XW+TWHD:V.1.630QK*JN/.EITVU80YS3.8LWKO:2STLWZAVHUXFHQ..NZ0:.J/FTZM.KYDXIE1VBY7/:PHZMQ$.JZQ2.XT32440X/HM+UY/7QP4I+HTD9.DUSY-8R6HDR-B8/PF2NP7I2-MRW9VPW3U9.S0LQ.*221F8KVMD5ANJXZJ8WV4UFZ4R.$-NXVE+-FAL:WFERGU+WHJTHAP", |
| 1145 |
tx_base43) |
| 1146 |
self.assertEqual(tx_bytes, |
| 1147 |
base_decode(tx_base43, base=43)) |
| 1148 |
|
| 1149 |
def test_base58(self): |
| 1150 |
data_hex = '0cd394bef396200774544c58a5be0189f3ceb6a41c8da023b099ce547dd4d8071ed6ed647259fba8c26382edbf5165dfd2404e7a8885d88437db16947a116e451a5d1325e3fd075f9d370120d2ab537af69f32e74fc0ba53aaaa637752964b3ac95cfea7' |
| 1151 |
data_bytes = bfh(data_hex) |
| 1152 |
data_base58 = base_encode(data_bytes, base=58) |
| 1153 |
self.assertEqual("VuvZ2K5UEcXCVcogny7NH4Evd9UfeYipsTdWuU4jLDhyaESijKtrGWZTFzVZJPjaoC9jFBs3SFtarhDhQhAxkXosUD8PmUb5UXW1tafcoPiCp8jHy7Fe2CUPXAbYuMvAyrkocbe6", |
| 1154 |
data_base58) |
| 1155 |
self.assertEqual(data_bytes, |
| 1156 |
base_decode(data_base58, base=58)) |
| 1157 |
|
| 1158 |
def test_base58check(self): |
| 1159 |
data_hex = '0cd394bef396200774544c58a5be0189f3ceb6a41c8da023b099ce547dd4d8071ed6ed647259fba8c26382edbf5165dfd2404e7a8885d88437db16947a116e451a5d1325e3fd075f9d370120d2ab537af69f32e74fc0ba53aaaa637752964b3ac95cfea7' |
| 1160 |
data_bytes = bfh(data_hex) |
| 1161 |
data_base58check = EncodeBase58Check(data_bytes) |
| 1162 |
self.assertEqual("4GCCJsjHqFbHxWbFBvRg35cSeNLHKeNqkXqFHW87zRmz6iP1dJU9Tk2KHZkoKj45jzVsSV4ZbQ8GpPwko6V3Z7cRfux3zJhUw7TZB6Kpa8Vdya8cMuUtL5Ry3CLtMetaY42u52X7Ey6MAH", |
| 1163 |
data_base58check) |
| 1164 |
self.assertEqual(data_bytes, |
| 1165 |
DecodeBase58Check(data_base58check)) |
| 1166 |
|
| 1167 |
|
| 1168 |
class TestTaprootHelpers(ElectrumTestCase): |
| 1169 |
|
| 1170 |
def test_taproot_tweak_homomorphism(self): |
| 1171 |
|
| 1172 |
|
| 1173 |
for secret_scalar in (8, 11, 99999): |
| 1174 |
privkey = ecc.ECPrivkey.from_secret_scalar(secret_scalar) |
| 1175 |
pubkey32 = privkey.get_public_key_bytes(compressed=True)[1:] |
| 1176 |
for tree_hash in (b"", b"satoshi", b"1234"*8, bytes(range(100)), ): |
| 1177 |
tweaked_pubkey = taproot_tweak_pubkey(pubkey32, tree_hash)[1] |
| 1178 |
tweaked_seckey = taproot_tweak_seckey(privkey.get_secret_bytes(), tree_hash) |
| 1179 |
self.assertEqual(tweaked_pubkey, ecc.ECPrivkey(tweaked_seckey).get_public_key_bytes(compressed=True)[1:]) |
| 1180 |
|
| 1181 |
def test_taproot_output_script(self): |
| 1182 |
|
| 1183 |
test_vector_file = os.path.join(os.path.dirname(__file__), "bip-0341", "wallet-test-vectors.json") |
| 1184 |
with open(test_vector_file, "r") as f: |
| 1185 |
vectors = json.load(f) |
| 1186 |
def transform_tree(tree_node): |
| 1187 |
if isinstance(tree_node, dict): |
| 1188 |
return (tree_node["leafVersion"], bfh(tree_node["script"])) |
| 1189 |
assert len(tree_node) == 2, len(tree_node) |
| 1190 |
return [transform_tree(tree_node[0]), transform_tree(tree_node[1])] |
| 1191 |
def flatten_tree(tree_node): |
| 1192 |
if isinstance(tree_node, tuple): |
| 1193 |
return [tree_node] |
| 1194 |
assert len(tree_node) == 2, len(tree_node) |
| 1195 |
return flatten_tree(tree_node[0]) + flatten_tree(tree_node[1]) |
| 1196 |
assert len(vectors["scriptPubKey"]) > 0, "test vectors missing" |
| 1197 |
for tcase in vectors["scriptPubKey"]: |
| 1198 |
script_tree = transform_tree(tcase["given"]["scriptTree"]) if tcase["given"]["scriptTree"] else None |
| 1199 |
internal_pubkey = bfh(tcase["given"]["internalPubkey"]) |
| 1200 |
spk = taproot_output_script(internal_pubkey, script_tree=script_tree) |
| 1201 |
self.assertEqual(bfh(tcase["expected"]["scriptPubKey"]), spk) |
| 1202 |
self.assertEqual(tcase["expected"]["bip350Address"], bitcoin.script_to_address(spk)) |
| 1203 |
if script_tree: |
| 1204 |
flat_tree = flatten_tree(script_tree) |
| 1205 |
for script_num, jcontrol_block in enumerate(tcase["expected"]["scriptPathControlBlocks"]): |
| 1206 |
leaf_script, control_block = control_block_for_taproot_script_spend( |
| 1207 |
internal_pubkey=internal_pubkey, script_tree=script_tree, script_num=script_num) |
| 1208 |
self.assertEqual(jcontrol_block, control_block.hex()) |
| 1209 |
self.assertEqual(flat_tree[script_num][1].hex(), leaf_script.hex()) |