def test_segwit_create_tx(self): from pycoin_grs.tx.tx_utils import create_tx, sign_tx from pycoin_grs.tx.Spendable import Spendable from pycoin_grs.tx.pay_to.ScriptPayToAddress import ScriptPayToAddress from pycoin_grs.tx.pay_to.ScriptPayToAddressWit import ScriptPayToAddressWit from pycoin_grs.ui import address_for_pay_to_script_wit, script_obj_from_address key1 = Key(1) coin_value = 5000000 script = ScriptPayToAddressWit(b'\0', key1.hash160()).script() tx_hash = b'\ee' * 32 tx_out_index = 0 spendable = Spendable(coin_value, script, tx_hash, tx_out_index) key2 = Key(2) tx = create_tx([spendable], [(key2.address(), coin_value)]) self.check_unsigned(tx) sign_tx(tx, [key1.wif()]) self.check_signed(tx) self.assertEqual(len(tx.txs_in[0].witness), 2) s1 = ScriptPayToAddress(key1.hash160()).script() address = address_for_pay_to_script_wit(s1) spendable.script = script_obj_from_address(address).script() tx = create_tx([spendable], [(key2.address(), coin_value)]) self.check_unsigned(tx) sign_tx(tx, [key1.wif()], p2sh_lookup=build_p2sh_lookup([s1])) self.check_signed(tx)
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 test_script_type_pay_to_public_pair(self): for se in range(1, 100): key = Key(secret_exponent=se) for b in [True, False]: st = ScriptPayToPublicKey.from_key(key, use_uncompressed=b) addr = key.address(use_uncompressed=b) self.assertEqual(st.address(), addr) sc = st.script() st = script_obj_from_script(sc) self.assertEqual(st.address(), addr)
def test_script_type_pay_to_address(self): for se in range(1, 100): key = Key(secret_exponent=se) for b in [True, False]: addr = key.address(use_uncompressed=b) st = script_obj_from_address(addr) self.assertEqual(st.address(), addr) sc = st.script() st = script_obj_from_script(sc) self.assertEqual(st.address(), addr)
def test_sign_verify(self): private_key = Key(secret_exponent=1) h = b"\x00" * 32 sig = private_key.sign(h) self.assertTrue(private_key.verify(h, sig)) public_key = private_key.public_copy() self.assertTrue(public_key.verify(h, sig)) h160_key = Key(hash160=private_key.hash160()) self.assertTrue(h160_key.verify(h, sig))
def test_solve_pay_to_public_pair(self): for se in range(1, 10): key = Key(secret_exponent=se) for b in [True, False]: addr = key.address(use_uncompressed=b) st = ScriptPayToPublicKey.from_key(key, use_uncompressed=b) self.assertEqual(st.address(), addr) hl = build_hash160_lookup([se]) sv = 100 st.solve(hash160_lookup=hl, signature_for_hash_type_f=const_f(sv), signature_type=SIGHASH_ALL) sc = st.script() st = script_obj_from_script(sc) self.assertEqual(st.address(), addr)
def test_pay_to_script_file(self): the_dir = self.set_cache_dir() p2sh_file = tempfile.NamedTemporaryFile() p2sh_file.write( "52210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102c60" "47f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258" "c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae\n".encode("utf8")) p2sh_file.write( "53210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102c60" "47f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258" "c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae\n".encode("utf8")) p2sh_file.flush() tx_source_hex = ( "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0" "0ffffffff0200f902950000000017a91415fc0754e73eb85d1cbce08786fadb7320ecb8dc8700f90295" "0000000017a914594f349df0bac3084ffea8a477bba5f03dcd45078700000000") self.launch_tool("tx -C %s" % tx_source_hex) tx_to_sign = ( "01000000020a316ea8980ef9ba02f4e6637c88229bf059f39b06238d48d06a8e" "f672aea2bb0000000000ffffffff0a316ea8980ef9ba02f4e6637c88229bf059" "f39b06238d48d06a8ef672aea2bb0100000000ffffffff01f0ca052a01000000" "1976a914751e76e8199196d454941c45d1b3a323f1433bd688ac0000000000f9" "02950000000017a91415fc0754e73eb85d1cbce08786fadb7320ecb8dc8700f9" "02950000000017a914594f349df0bac3084ffea8a477bba5f03dcd450787") wifs = ' '.join(Key(_).wif() for _ in (1, 2, 3)) signed = tempfile.mktemp(suffix=".hex") self.launch_tool("tx -a -P %s --db %s %s %s -o %s" % ( p2sh_file.name, tx_source_hex, tx_to_sign, wifs, signed), env=dict(PYCOIN_GRS_CACHE_DIR=the_dir)) tx = Tx.from_hex(open(signed).read()) self.assertEqual(tx.id(), "9d991ddccf77e33cb4584e4fc061a36da0da43589232b2e78a1aa0748ac3254b")
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_multisig_one_at_a_time(self): M = 3 N = 3 keys = [Key(secret_exponent=i) for i in range(1, N + 2)] tx_in = TxIn.coinbase_tx_in(script=b'') script = ScriptMultisig(m=M, sec_keys=[key.sec() for key in keys[:N]]).script() tx_out = TxOut(1000000, script) tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out]) tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [keys[-1].address()]) ids = [ "403e5bfc59e097bb197bf77a692d158dd3a4f7affb4a1fa41072dafe7bec7058", "5931d9995e83721243dca24772d7012afcd4378996a8b953c458175f15a544db", "9bb4421088190bbbb5b42a9eaa9baed7ec7574a407c25f71992ba56ca43d9c44", "03a1dc2a63f93a5cf5a7cb668658eb3fc2eda88c06dc287b85ba3e6aff751771" ] for i in range(1, N + 1): self.assertEqual(tx2.bad_signature_count(), 1) self.assertEqual(tx2.id(), ids[i - 1]) hash160_lookup = build_hash160_lookup(key.secret_exponent() for key in keys[i - 1:i]) tx2.sign(hash160_lookup=hash160_lookup) self.assertEqual(tx2.id(), ids[i]) self.assertEqual(tx2.bad_signature_count(), 0)
def test_against_myself(): """ Test code that verifies against ourselves only. Useful but not so great. """ from pycoin_grs.contrib.msg_signing import ( parse_signed_message, sign_message, verify_message) from pycoin_grs.encoding import bitcoin_address_to_hash160_sec_with_prefix from pycoin_grs.encoding import wif_to_tuple_of_secret_exponent_compressed for wif, right_addr in [ ('L4gXBvYrXHo59HLeyem94D9yLpRkURCHmCwQtPuWW9m6o1X8p8sp', '1LsPb3D1o1Z7CzEt1kv5QVxErfqzXxaZXv'), ('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss', '1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN'), ]: se, comp = wif_to_tuple_of_secret_exponent_compressed(wif) k = Key(secret_exponent=se, is_compressed=comp) assert k.address() == right_addr vk = Key(public_pair=k.public_pair(), is_compressed=comp) assert vk.address() == right_addr h160, pubpre = bitcoin_address_to_hash160_sec_with_prefix(right_addr) vk2 = Key(hash160=h160) assert vk2.address() == right_addr for i in range(1, 30, 10): msg = 'test message %s' % ('A'*i) sig = sign_message(k, msg, verbose=1) assert right_addr in sig # check parsing works m, a, s = parse_signed_message(sig) assert m == msg, m assert a == right_addr, a sig2 = sign_message(k, msg, verbose=0) assert sig2 in sig, (sig, sig2) assert s == sig2, s ok = verify_message(k, sig2, msg) assert ok ok = verify_message(k, sig2.encode('ascii'), msg) assert ok
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 test_repr(self): from pycoin_grs.key import Key netcode = 'XTN' key = Key(secret_exponent=273, netcode=netcode) wallet = BIP32Node.from_master_secret(bytes(key.wif().encode('ascii')), netcode) address = wallet.address() pub_k = wallet.from_text(address) self.assertEqual(repr(pub_k), '<myb5gZNXePNf2E2ksrjnHRFCwyuvt7oEay>') wif = wallet.wif() priv_k = wallet.from_text(wif) self.assertEqual( repr(priv_k), 'private_for <03ad094b1dc9fdce5d3648ca359b4e210a89d049532fdd39d9ccdd8ca393ac82f4>' )
def test_is_wif_valid(self): WIFS = ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf", "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU74NMTptX4", "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAvUcVfH"] for wif in WIFS: self.assertEqual(is_wif_valid(wif), "BTC") a = wif[:-1] + chr(ord(wif[-1])+1) self.assertEqual(is_wif_valid(a), None) NETWORK_NAMES = network_codes() for netcode in NETWORK_NAMES: for se in range(1, 10): key = Key(secret_exponent=se, netcode=netcode) for tv in [True, False]: wif = key.wif(use_uncompressed=tv) self.assertEqual(is_wif_valid(wif, allowable_netcodes=[netcode]), netcode) a = wif[:-1] + chr(ord(wif[-1])+1) self.assertEqual(is_wif_valid(a, allowable_netcodes=[netcode]), None)
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 parse_key(item, PREFIX_TRANSFORMS, network): key = parse_prefixes(item, PREFIX_TRANSFORMS) if key: return key if HASH160_RE.match(item): return Key(hash160=h2b(item), netcode=network) secret_exponent = parse_as_secret_exponent(item) if secret_exponent: return Key(secret_exponent=secret_exponent, netcode=network) if SEC_RE.match(item): return Key.from_sec(h2b(item)) public_pair = parse_as_public_pair(item) if public_pair: return Key(public_pair=public_pair, netcode=network) return None
def test_key_limits(self): nc = 'BTC' cc = b'000102030405060708090a0b0c0d0e0f' order = secp256k1_generator.order() for k in -1, 0, order, order + 1: self.assertRaises(InvalidSecretExponentError, Key, secret_exponent=k) self.assertRaises(InvalidSecretExponentError, BIP32Node, nc, cc, secret_exponent=k) for i in range(1, 512): Key(secret_exponent=i) BIP32Node(nc, cc, secret_exponent=i)
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 multisig_M_of_N_individually(self, M, N): keys = [Key(secret_exponent=i) for i in range(1, N+2)] tx_in = TxIn.coinbase_tx_in(script=b'') script = ScriptMultisig(m=M, sec_keys=[key.sec() for key in keys[:N]]).script() tx_out = TxOut(1000000, script) tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out]) for partial_key_list in itertools.permutations(keys[:N], M): tx2 = create_tx(tx1.tx_outs_as_spendable(), [keys[-1].address()]) for key in partial_key_list: self.assertEqual(tx2.bad_signature_count(), 1) hash160_lookup = build_hash160_lookup([key.secret_exponent()]) tx2.sign(hash160_lookup=hash160_lookup) self.assertEqual(tx2.bad_signature_count(), 0)
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 multisig_M_of_N(self, M, N, unsigned_id, signed_id): keys = [Key(secret_exponent=i) for i in range(1, N+2)] tx_in = TxIn.coinbase_tx_in(script=b'') script = ScriptMultisig(m=M, sec_keys=[key.sec() for key in keys[:N]]).script() tx_out = TxOut(1000000, script) tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out]) tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [keys[-1].address()]) self.assertEqual(tx2.id(), unsigned_id) self.assertEqual(tx2.bad_signature_count(), 1) hash160_lookup = build_hash160_lookup(key.secret_exponent() for key in keys) tx2.sign(hash160_lookup=hash160_lookup) self.assertEqual(tx2.id(), signed_id) self.assertEqual(tx2.bad_signature_count(), 0) self.assertEqual(sorted(who_signed.who_signed_tx(tx2, 0)), sorted(((key.address(), SIGHASH_ALL) for key in keys[:M])))
def check_tx_can_be_signed(self, tx_u, tx_s, private_keys=[], p2sh_values=[]): tx_u_prime = self.unsigned_copy(tx_s) tx_s_hex = tx_s.as_hex() tx_u_prime.set_unspents(tx_s.unspents) tx_u_prime.sign(hash160_lookup=LazySecretExponentDB( [Key(pk).wif() for pk in private_keys], {}), p2sh_lookup=build_p2sh_lookup( [h2b(x) for x in p2sh_values])) self.check_signed(tx_u_prime) tx_hex = tx_u_prime.as_hex() self.assertEqual(tx_hex, tx_s_hex)
def subkey(self, path): """ path: of the form "K" where K is an integer index, or "K/N" where N is usually a 0 (deposit address) or 1 (change address) """ t = path.split("/") if len(t) == 2: n, for_change = t else: n, = t for_change = 0 b = (str(n) + ':' + str(for_change) + ':').encode("utf8") + self.master_public_key() offset = from_bytes_32(double_sha256(b)) if self.master_private_key(): return Key(secret_exponent=((self.master_private_key() + offset) % ORDER), prefer_uncompressed=True) p1 = offset * ecdsa.generator_secp256k1 x, y = self.public_pair() p2 = ecdsa.Point(ecdsa.generator_secp256k1.curve(), x, y, ORDER) p = p1 + p2 return Key(public_pair=p.pair(), prefer_uncompressed=True)
def test_sign_pay_to_script_multisig(self): M, N = 3, 3 keys = [Key(secret_exponent=i) for i in range(1, N+2)] tx_in = TxIn.coinbase_tx_in(script=b'') underlying_script = ScriptMultisig(m=M, sec_keys=[key.sec() for key in keys[:N]]).script() address = address_for_pay_to_script(underlying_script) self.assertEqual(address, "39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q") script = standard_tx_out_script(address) tx_out = TxOut(1000000, script) tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out]) tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [address]) hash160_lookup = build_hash160_lookup(key.secret_exponent() for key in keys[:N]) p2sh_lookup = build_p2sh_lookup([underlying_script]) tx2.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup) self.assertEqual(tx2.bad_signature_count(), 0) self.assertRaises(who_signed.NoAddressesForScriptTypeError, who_signed.who_signed_tx, tx2, 0)
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_points(self): secp256k1_curve = secp256k1_generator.curve() # From <https://crypto.stackexchange.com/questions/784/are-there-any-secp256k1-ecdsa-test-examples-available> test_points = [] k = 1 x = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 y = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8 test_points.append((k, x, y)) k = 2 x = 0xC6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5 y = 0x1AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A test_points.append((k, x, y)) k = 3 x = 0xF9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9 y = 0x388F7B0F632DE8140FE337E62A37F3566500A99934C2231B6CB9FD7584B8E672 test_points.append((k, x, y)) k = 4 x = 0xE493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD13 y = 0x51ED993EA0D455B75642E2098EA51448D967AE33BFBDFE40CFE97BDC47739922 test_points.append((k, x, y)) k = 5 x = 0x2F8BDE4D1A07209355B4A7250A5C5128E88B84BDDC619AB7CBA8D569B240EFE4 y = 0xD8AC222636E5E3D6D4DBA9DDA6C9C426F788271BAB0D6840DCA87D3AA6AC62D6 test_points.append((k, x, y)) k = 6 x = 0xFFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A1460297556 y = 0xAE12777AACFBB620F3BE96017F45C560DE80F0F6518FE4A03C870C36B075F297 test_points.append((k, x, y)) k = 7 x = 0x5CBDF0646E5DB4EAA398F365F2EA7A0E3D419B7E0330E39CE92BDDEDCAC4F9BC y = 0x6AEBCA40BA255960A3178D6D861A54DBA813D0B813FDE7B5A5082628087264DA test_points.append((k, x, y)) k = 8 x = 0x2F01E5E15CCA351DAFF3843FB70F3C2F0A1BDD05E5AF888A67784EF3E10A2A01 y = 0x5C4DA8A741539949293D082A132D13B4C2E213D6BA5B7617B5DA2CB76CBDE904 test_points.append((k, x, y)) k = 9 x = 0xACD484E2F0C7F65309AD178A9F559ABDE09796974C57E714C35F110DFC27CCBE y = 0xCC338921B0A7D9FD64380971763B61E9ADD888A4375F8E0F05CC262AC64F9C37 test_points.append((k, x, y)) k = 10 x = 0xA0434D9E47F3C86235477C7B1AE6AE5D3442D49B1943C2B752A68E2A47E247C7 y = 0x893ABA425419BC27A3B6C7E693A24C696F794C2ED877A1593CBEE53B037368D7 test_points.append((k, x, y)) k = 11 x = 0x774AE7F858A9411E5EF4246B70C65AAC5649980BE5C17891BBEC17895DA008CB y = 0xD984A032EB6B5E190243DD56D7B7B365372DB1E2DFF9D6A8301D74C9C953C61B test_points.append((k, x, y)) k = 12 x = 0xD01115D548E7561B15C38F004D734633687CF4419620095BC5B0F47070AFE85A y = 0xA9F34FFDC815E0D7A8B64537E17BD81579238C5DD9A86D526B051B13F4062327 test_points.append((k, x, y)) k = 13 x = 0xF28773C2D975288BC7D1D205C3748651B075FBC6610E58CDDEEDDF8F19405AA8 y = 0x0AB0902E8D880A89758212EB65CDAF473A1A06DA521FA91F29B5CB52DB03ED81 test_points.append((k, x, y)) k = 14 x = 0x499FDF9E895E719CFD64E67F07D38E3226AA7B63678949E6E49B241A60E823E4 y = 0xCAC2F6C4B54E855190F044E4A7B3D464464279C27A3F95BCC65F40D403A13F5B test_points.append((k, x, y)) k = 15 x = 0xD7924D4F7D43EA965A465AE3095FF41131E5946F3C85F79E44ADBCF8E27E080E y = 0x581E2872A86C72A683842EC228CC6DEFEA40AF2BD896D3A5C504DC9FF6A26B58 test_points.append((k, x, y)) k = 16 x = 0xE60FCE93B59E9EC53011AABC21C23E97B2A31369B87A5AE9C44EE89E2A6DEC0A y = 0xF7E3507399E595929DB99F34F57937101296891E44D23F0BE1F32CCE69616821 test_points.append((k, x, y)) k = 17 x = 0xDEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34 y = 0x4211AB0694635168E997B0EAD2A93DAECED1F4A04A95C0F6CFB199F69E56EB77 test_points.append((k, x, y)) k = 18 x = 0x5601570CB47F238D2B0286DB4A990FA0F3BA28D1A319F5E7CF55C2A2444DA7CC y = 0xC136C1DC0CBEB930E9E298043589351D81D8E0BC736AE2A1F5192E5E8B061D58 test_points.append((k, x, y)) k = 19 x = 0x2B4EA0A797A443D293EF5CFF444F4979F06ACFEBD7E86D277475656138385B6C y = 0x85E89BC037945D93B343083B5A1C86131A01F60C50269763B570C854E5C09B7A test_points.append((k, x, y)) k = 20 x = 0x4CE119C96E2FA357200B559B2F7DD5A5F02D5290AFF74B03F3E471B273211C97 y = 0x12BA26DCB10EC1625DA61FA10A844C676162948271D96967450288EE9233DC3A test_points.append((k, x, y)) k = 112233445566778899 x = 0xA90CC3D3F3E146DAADFC74CA1372207CB4B725AE708CEF713A98EDD73D99EF29 y = 0x5A79D6B289610C68BC3B47F3D72F9788A26A06868B4D8E433E1E2AD76FB7DC76 test_points.append((k, x, y)) k = 112233445566778899112233445566778899 x = 0xE5A2636BCFD412EBF36EC45B19BFB68A1BC5F8632E678132B885F7DF99C5E9B3 y = 0x736C1CE161AE27B405CAFD2A7520370153C2C861AC51D6C1D5985D9606B45F39 test_points.append((k, x, y)) k = 28948022309329048855892746252171976963209391069768726095651290785379540373584 x = 0xA6B594B38FB3E77C6EDF78161FADE2041F4E09FD8497DB776E546C41567FEB3C y = 0x71444009192228730CD8237A490FEBA2AFE3D27D7CC1136BC97E439D13330D55 test_points.append((k, x, y)) k = 57896044618658097711785492504343953926418782139537452191302581570759080747168 x = 0x00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C63 y = 0x3F3979BF72AE8202983DC989AEC7F2FF2ED91BDD69CE02FC0700CA100E59DDF3 test_points.append((k, x, y)) k = 86844066927987146567678238756515930889628173209306178286953872356138621120752 x = 0xE24CE4BEEE294AA6350FAA67512B99D388693AE4E7F53D19882A6EA169FC1CE1 y = 0x8B71E83545FC2B5872589F99D948C03108D36797C4DE363EBD3FF6A9E1A95B10 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494317 x = 0x4CE119C96E2FA357200B559B2F7DD5A5F02D5290AFF74B03F3E471B273211C97 y = 0xED45D9234EF13E9DA259E05EF57BB3989E9D6B7D8E269698BAFD77106DCC1FF5 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494318 x = 0x2B4EA0A797A443D293EF5CFF444F4979F06ACFEBD7E86D277475656138385B6C y = 0x7A17643FC86BA26C4CBCF7C4A5E379ECE5FE09F3AFD9689C4A8F37AA1A3F60B5 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494319 x = 0x5601570CB47F238D2B0286DB4A990FA0F3BA28D1A319F5E7CF55C2A2444DA7CC y = 0x3EC93E23F34146CF161D67FBCA76CAE27E271F438C951D5E0AE6D1A074F9DED7 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494320 x = 0xDEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34 y = 0xBDEE54F96B9CAE9716684F152D56C251312E0B5FB56A3F09304E660861A910B8 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494321 x = 0xE60FCE93B59E9EC53011AABC21C23E97B2A31369B87A5AE9C44EE89E2A6DEC0A y = 0x081CAF8C661A6A6D624660CB0A86C8EFED6976E1BB2DC0F41E0CD330969E940E test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494322 x = 0xD7924D4F7D43EA965A465AE3095FF41131E5946F3C85F79E44ADBCF8E27E080E y = 0xA7E1D78D57938D597C7BD13DD733921015BF50D427692C5A3AFB235F095D90D7 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494323 x = 0x499FDF9E895E719CFD64E67F07D38E3226AA7B63678949E6E49B241A60E823E4 y = 0x353D093B4AB17AAE6F0FBB1B584C2B9BB9BD863D85C06A4339A0BF2AFC5EBCD4 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494324 x = 0xF28773C2D975288BC7D1D205C3748651B075FBC6610E58CDDEEDDF8F19405AA8 y = 0xF54F6FD17277F5768A7DED149A3250B8C5E5F925ADE056E0D64A34AC24FC0EAE test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494325 x = 0xD01115D548E7561B15C38F004D734633687CF4419620095BC5B0F47070AFE85A y = 0x560CB00237EA1F285749BAC81E8427EA86DC73A2265792AD94FAE4EB0BF9D908 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494326 x = 0x774AE7F858A9411E5EF4246B70C65AAC5649980BE5C17891BBEC17895DA008CB y = 0x267B5FCD1494A1E6FDBC22A928484C9AC8D24E1D20062957CFE28B3536AC3614 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494327 x = 0xA0434D9E47F3C86235477C7B1AE6AE5D3442D49B1943C2B752A68E2A47E247C7 y = 0x76C545BDABE643D85C4938196C5DB3969086B3D127885EA6C3411AC3FC8C9358 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494328 x = 0xACD484E2F0C7F65309AD178A9F559ABDE09796974C57E714C35F110DFC27CCBE y = 0x33CC76DE4F5826029BC7F68E89C49E165227775BC8A071F0FA33D9D439B05FF8 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494329 x = 0x2F01E5E15CCA351DAFF3843FB70F3C2F0A1BDD05E5AF888A67784EF3E10A2A01 y = 0xA3B25758BEAC66B6D6C2F7D5ECD2EC4B3D1DEC2945A489E84A25D3479342132B test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494330 x = 0x5CBDF0646E5DB4EAA398F365F2EA7A0E3D419B7E0330E39CE92BDDEDCAC4F9BC y = 0x951435BF45DAA69F5CE8729279E5AB2457EC2F47EC02184A5AF7D9D6F78D9755 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494331 x = 0xFFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A1460297556 y = 0x51ED8885530449DF0C4169FE80BA3A9F217F0F09AE701B5FC378F3C84F8A0998 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494332 x = 0x2F8BDE4D1A07209355B4A7250A5C5128E88B84BDDC619AB7CBA8D569B240EFE4 y = 0x2753DDD9C91A1C292B24562259363BD90877D8E454F297BF235782C459539959 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494333 x = 0xE493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD13 y = 0xAE1266C15F2BAA48A9BD1DF6715AEBB7269851CC404201BF30168422B88C630D test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494334 x = 0xF9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9 y = 0xC77084F09CD217EBF01CC819D5C80CA99AFF5666CB3DDCE4934602897B4715BD test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494335 x = 0xC6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5 y = 0xE51E970159C23CC65C3A7BE6B99315110809CD9ACD992F1EDC9BCE55AF301705 test_points.append((k, x, y)) k = 115792089237316195423570985008687907852837564279074904382605163141518161494336 x = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 y = 0xB7C52588D95C3B9AA25B0403F1EEF75702E84BB7597AABE663B82F6F04EF2777 test_points.append((k, x, y)) k = 0xaa5e28d6a97a2479a65527f7290311a3624d4cc0fa1578598ee3c2613bf99522 x = 0x34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6 y = 0x0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232 test_points.append((k, x, y)) k = 0x7e2b897b8cebc6361663ad410835639826d590f393d90a9538881735256dfae3 x = 0xd74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575 y = 0x131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d test_points.append((k, x, y)) k = 0x6461e6df0fe7dfd05329f41bf771b86578143d4dd1f7866fb4ca7e97c5fa945d x = 0xe8aecc370aedd953483719a116711963ce201ac3eb21d3f3257bb48668c6a72f y = 0xc25caf2f0eba1ddb2f0f3f47866299ef907867b7d27e95b3873bf98397b24ee1 test_points.append((k, x, y)) k = 0x376a3a2cdcd12581efff13ee4ad44c4044b8a0524c42422a7e1e181e4deeccec x = 0x14890e61fcd4b0bd92e5b36c81372ca6fed471ef3aa60a3e415ee4fe987daba1 y = 0x297b858d9f752ab42d3bca67ee0eb6dcd1c2b7b0dbe23397e66adc272263f982 test_points.append((k, x, y)) k = 0x1b22644a7be026548810c378d0b2994eefa6d2b9881803cb02ceff865287d1b9 x = 0xf73c65ead01c5126f28f442d087689bfa08e12763e0cec1d35b01751fd735ed3 y = 0xf449a8376906482a84ed01479bd18882b919c140d638307f0c0934ba12590bde test_points.append((k, x, y)) for k, x, y in test_points: self.assertTrue(secp256k1_curve.contains_point(x, y)) K = Key(public_pair=(x, y)) k = Key(secret_exponent=k) self.assertEqual(K.public_pair(), k.public_pair()) self.assertRaises(ValueError, lambda: secp256k1_curve.Point(0, 0)) self.assertRaises(InvalidPublicPairError, Key, public_pair=(0, 0))
def _test_sighash_single(self, netcode): k0 = Key(secret_exponent=PRIV_KEYS[0], is_compressed=True, netcode=netcode) k1 = Key(secret_exponent=PRIV_KEYS[1], is_compressed=True, netcode=netcode) k2 = Key(secret_exponent=PRIV_KEYS[2], is_compressed=True, netcode=netcode) k3 = Key(secret_exponent=PRIV_KEYS[3], is_compressed=True, netcode=netcode) k4 = Key(secret_exponent=PRIV_KEYS[4], is_compressed=True, netcode=netcode) k5 = Key(secret_exponent=PRIV_KEYS[5], is_compressed=True, netcode=netcode) # Fake a coinbase transaction coinbase_tx = Tx.coinbase_tx(k0.sec(), 500000000) coinbase_tx.txs_out.append( TxOut(1000000000, pycoin_grs_compile('%s OP_CHECKSIG' % b2h(k1.sec())))) coinbase_tx.txs_out.append( TxOut(1000000000, pycoin_grs_compile('%s OP_CHECKSIG' % b2h(k2.sec())))) self.assertEqual( '2acbe1006f7168bad538b477f7844e53de3a31ffddfcfc4c6625276dd714155a', b2h_rev(coinbase_tx.hash())) # Make the test transaction txs_in = [ TxIn(coinbase_tx.hash(), 0), TxIn(coinbase_tx.hash(), 1), TxIn(coinbase_tx.hash(), 2), ] txs_out = [ TxOut(900000000, standard_tx_out_script(k3.address())), TxOut(800000000, standard_tx_out_script(k4.address())), TxOut(800000000, standard_tx_out_script(k5.address())), ] tx = Tx(1, txs_in, txs_out) tx.set_unspents(coinbase_tx.txs_out) self.assertEqual( '791b98ef0a3ac87584fe273bc65abd89821569fd7c83538ac0625a8ca85ba587', b2h_rev(tx.hash())) sig_type = SIGHASH_SINGLE sig_hash = tx.signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type) self.assertEqual( 'cc52d785a3b4133504d1af9e60cd71ca422609cb41df3a08bbb466b2a98a885e', b2h(to_bytes_32(sig_hash))) sig = sigmake(k0, sig_hash, sig_type) self.assertTrue(sigcheck(k0, sig_hash, sig[:-1])) tx.txs_in[0].script = pycoin_grs_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(0)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type) self.assertEqual( '93bb883d70fccfba9b8aa2028567aca8357937c65af7f6f5ccc6993fd7735fb7', b2h(to_bytes_32(sig_hash))) sig = sigmake(k1, sig_hash, sig_type) self.assertTrue(sigcheck(k1, sig_hash, sig[:-1])) tx.txs_in[1].script = pycoin_grs_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(1)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type) self.assertEqual( '53ef7f67c3541bffcf4e0d06c003c6014e2aa1fb38ff33240b3e1c1f3f8e2a35', b2h(to_bytes_32(sig_hash))) sig = sigmake(k2, sig_hash, sig_type) self.assertTrue(sigcheck(k2, sig_hash, sig[:-1])) tx.txs_in[2].script = pycoin_grs_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(2)) sig_type = SIGHASH_SINGLE | SIGHASH_ANYONECANPAY sig_hash = tx.signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type) self.assertEqual( '2003393d246a7f136692ce7ab819c6eadc54ffea38eb4377ac75d7d461144e75', b2h(to_bytes_32(sig_hash))) sig = sigmake(k0, sig_hash, sig_type) self.assertTrue(sigcheck(k0, sig_hash, sig[:-1])) tx.txs_in[0].script = pycoin_grs_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(0)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type) self.assertEqual( 'e3f469ac88e9f35e8eff0bd8ad4ad3bf899c80eb7645947d60860de4a08a35df', b2h(to_bytes_32(sig_hash))) sig = sigmake(k1, sig_hash, sig_type) self.assertTrue(sigcheck(k1, sig_hash, sig[:-1])) tx.txs_in[1].script = pycoin_grs_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(1)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type) self.assertEqual( 'bacd7c3ab79cad71807312677c1788ad9565bf3c00ab9a153d206494fb8b7e6a', b2h(to_bytes_32(sig_hash))) sig = sigmake(k2, sig_hash, sig_type) self.assertTrue(sigcheck(k2, sig_hash, sig[:-1])) tx.txs_in[2].script = pycoin_grs_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(2))
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