def test_initial_key(self): RECEIVING_ADDRESSES = [ "1LDkC1H438qSnJLHCYkQ3WTZQkSEwoYGHc", "12mENAcc8ZhZbR6hv7LGm3jV7PwbYeF8Xk", "1A3NpABFd6YHvwr1ti1r8brU3BzQuV2Nr4", "1Gn6nWAoZrpmtV9zuNbyivWvRBpcygWaQX", "1M5i5P3DhtDbnvSTfmnUbcrTVgF8GDWQW9" ] CHANGE_ADDRESSES = [ "1iiAbyBTh1J69UzD1JcrfW8JSVJ9ve9gT", "146wnqmsQNYCZ6AXRCqLkzZyGM1ZU6nr3F", "1Mwexajvia3s8AcaGUkyEg9ZZJPJeTbKTZ" ] wallet = ElectrumWallet(initial_key="00000000000000000000000000000001") for idx, address in enumerate(RECEIVING_ADDRESSES): subkey = wallet.subkey("%s/0" % idx) calculated_address = subkey.address() self.assertEqual(address, calculated_address) wif = subkey.wif() key = Key.from_text(wif) self.assertEqual(key.address(use_uncompressed=True), address) for idx, address in enumerate(CHANGE_ADDRESSES): subkey = wallet.subkey("%s/1" % idx) calculated_address = subkey.address() self.assertEqual(address, calculated_address) wif = subkey.wif() key = Key.from_text(wif) self.assertEqual(key.address(use_uncompressed=True), address)
def do_test(exp_hex, wif, c_wif, public_pair_sec, c_public_pair_sec, address_b58, c_address_b58): secret_exponent = int(exp_hex, 16) sec = h2b(public_pair_sec) c_sec = h2b(c_public_pair_sec) keys_wif = [ Key(secret_exponent=secret_exponent), Key.from_text(wif), Key.from_text(c_wif), ] key_sec = Key.from_sec(sec) key_sec_c = Key.from_sec(c_sec) keys_sec = [key_sec, key_sec_c] for key in keys_wif: self.assertEqual(key.secret_exponent(), secret_exponent) self.assertEqual(key.public_copy().secret_exponent(), None) repr(key) if key._prefer_uncompressed: self.assertEqual(key.wif(), wif) else: self.assertEqual(key.wif(), c_wif) self.assertEqual(key.wif(use_uncompressed=True), wif) self.assertEqual(key.wif(use_uncompressed=False), c_wif) for key in keys_wif + keys_sec: if key._prefer_uncompressed: self.assertEqual(key.sec(), sec) else: self.assertEqual(key.sec(), c_sec) self.assertEqual(key.sec(use_uncompressed=True), sec) self.assertEqual(key.sec(use_uncompressed=False), c_sec) if key._prefer_uncompressed: self.assertEqual(key.address(), address_b58) else: self.assertEqual(key.address(), c_address_b58) self.assertEqual(key.address(use_uncompressed=False), c_address_b58) self.assertEqual(key.address(use_uncompressed=True), address_b58) key_pub = Key.from_text(address_b58, is_compressed=False) key_pub_c = Key.from_text(c_address_b58, is_compressed=True) self.assertEqual(key_pub.address(), address_b58) self.assertEqual(key_pub.address(use_uncompressed=True), address_b58) self.assertEqual(key_pub.address(use_uncompressed=False), None) self.assertEqual(key_pub_c.address(), c_address_b58) self.assertEqual(key_pub_c.address(use_uncompressed=True), None) self.assertEqual(key_pub_c.address(use_uncompressed=False), c_address_b58)
def test_repr(self): key = Key(secret_exponent=273, netcode='XTN') address = key.address() pub_k = Key.from_text(address) self.assertEqual(repr(pub_k), '<mhDVBkZBWLtJkpbszdjZRkH1o5RZxMwxca>') wif = key.wif() priv_k = Key.from_text(wif) self.assertEqual( repr(priv_k), 'private_for <0264e1b1969f9102977691a40431b0b672055dcf31163897d996434420e6c95dc9>')
def test_sign_bitcoind_partially_signed_2_of_2(self): # Finish signing a 2 of 2 transaction, that already has one signature signed by bitcoind # This tx can be found on testnet3 blockchain # txid: 9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2 raw_script = ( "522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c168851165521" "02b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452ae" ) p2sh_lookup = build_p2sh_lookup([h2b(raw_script)]) partially_signed_raw_tx = ( "010000000196238f11a5fd3ceef4efd5a186a7e6b9217d900418e72aca917cd6a6e634" "e74100000000910047304402201b41b471d9dd93cf97eed7cfc39a5767a546f6bfbf3e" "0c91ff9ad23ab9770f1f02205ce565666271d055be1f25a7e52e34cbf659f6c70770ff" "59bd783a6fcd1be3dd0147522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e4" "2cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff5" "0511830328c1b479986452aeffffffff01a0bb0d00000000001976a9143b3beefd6f78" "02fa8706983a76a51467bfa36f8b88ac00000000") tx = Tx.from_hex(partially_signed_raw_tx) tx_out = TxOut(1000000, h2b("a914a10dfa21ee8c33b028b92562f6fe04e60563d3c087")) tx.set_unspents([tx_out]) key = Key.from_text( "cThRBRu2jAeshWL3sH3qbqdq9f4jDiDbd1SVz4qjTZD2xL1pdbsx") hash160_lookup = build_hash160_lookup([key.secret_exponent()]) self.assertEqual(tx.bad_signature_count(), 1) tx.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup) self.assertEqual(tx.bad_signature_count(), 0) self.assertEqual( tx.id(), "9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2")
def test_master_public_and_private(self): # these addresses were generated by hand using electrum with a master public key # corresponding to secret exponent 1 RECEIVING_ADDRESSES = [ "1AYPdHLna6bKFUbeXoAEVbaXUxifUwCMay", "13UeuWJba5epizAKyfCfiFKY5Kbxfdxe7B", "19f6KJUTL5AGBRvLBGiL6Zpcx53QA7zaKT", "1Cm33VuSkoUETwx5nsF1wgmGqYwJZxpZdY", "14Z6ErkETixQMUeivsYbrdoUFns2J1iSct", ] CHANGE_ADDRESSES = [ "1JVYsmjrqSy1BKvo1gYpNjX7AYea74nQYe", "1Cc7itfQaDqZK3vHYphFsySujQjBNba8mw", "15wrXvrAnyv3usGeQRohnnZ8tz9XAekbag", "1MnWCEjE5YiZpZrkP8HcXEeDqwg43RxLwu", "1Fgyp3PUx9AAg8yJe1zGXHP5dVC6i1tXbs", "12XTLd4u9jeqw4egLAUhoKLxHARCdKWkty" ] k = Key(secret_exponent=1) master_public_key = k.sec(use_uncompressed=True)[1:] wallet = ElectrumWallet(master_public_key=master_public_key) for idx, address in enumerate(RECEIVING_ADDRESSES): subkey = wallet.subkey("%s/0" % idx) calculated_address = subkey.address() self.assertEqual(address, calculated_address) for idx, address in enumerate(CHANGE_ADDRESSES): subkey = wallet.subkey("%s/1" % idx) calculated_address = subkey.address() self.assertEqual(address, calculated_address) wallet = ElectrumWallet(master_private_key=1) for idx, address in enumerate(RECEIVING_ADDRESSES): subkey = wallet.subkey("%s/0" % idx) calculated_address = subkey.address() self.assertEqual(address, calculated_address) wif = subkey.wif() key = Key.from_text(wif) self.assertEqual(key.address(use_uncompressed=True), address) for idx, address in enumerate(CHANGE_ADDRESSES): subkey = wallet.subkey("%s/1" % idx) calculated_address = subkey.address() self.assertEqual(address, calculated_address) wif = subkey.wif() key = Key.from_text(wif) self.assertEqual(key.address(use_uncompressed=True), address)
def parse_prefixes(item, PREFIX_TRANSFORMS): for k, f in PREFIX_TRANSFORMS: if item.startswith(k): try: return f(item[len(k):]) except Exception: pass try: return Key.from_text(item) except encoding.EncodingError: pass return None
def key_found(arg, payables, key_iters): try: key = Key.from_text(arg) # TODO: check network if key.wif() is None: payables.append((key.address(), 0)) return True key_iters.append(iter([key.wif()])) return True except Exception: pass return False
def change_prefix(address, new_prefix): return hash160_sec_to_bitcoin_address(Key.from_text(address).hash160(), address_prefix=new_prefix)
def make_key(x): try: return Key.from_text(x) except Exception: return None