def test_verify(self): announce = MasternodeAnnounce.deserialize(raw_announce) message = announce.serialize_for_sig() pk = bitcoin.public_key_to_p2pkh(bfh(announce.collateral_key)) self.assertTrue(announce.verify()) raw = '7ca6564432d0e0920b811887e1f9077a92924c83564e6ea8ea874fc8843ccd2b0000000000ffffffff00000000000000000000ffffc0a801014e1f410411e2638aeb4584ff2e027b6ee20e05655ff05583185b1d87188185d6955534fe02ad35caabb5e6e9ce8747ba73fdccccd2369feb9a6f2b0bdee93378e7c8f1c0410411e2638aeb4584ff2e027b6ee20e05655ff05583185b1d87188185d6955534fe02ad35caabb5e6e9ce8747ba73fdccccd2369feb9a6f2b0bdee93378e7c8f1c0411bab132617d8e6a0e3b5434c91a5a64ff13a9cfadc6c178a47b87691f13a26e7440c08660e488ddf927bba1bf04c1ec196370452a30fd3381ea8ba27d627f9d4468be80e5700000000d71101007ca6564432d0e0920b811887e1f9077a92924c83564e6ea8ea874fc8843ccd2b0000000000ffffffffd75eb4fa0cb71dd2e99d7b242784a5601c5c86d7c1cf0362a3391575070000008be80e5700000000411b6d5985008e0821c936fafc192f31963141ae2fab837e84bb9f12422711c1952d5750f9a781c89117a6f4576edc1149a1bf211e7151c5c88cf3252e2d83cb154a0000000000000000' announce = MasternodeAnnounce.deserialize(raw) msg = announce.serialize_for_sig() pk = bitcoin.public_key_to_p2pkh(bfh(announce.collateral_key)) self.assertTrue(announce.verify(pk))
def _do_test_crypto(self, message): G = ecc.generator() _r = G.order() pvk = ecdsa.util.randrange(_r) Pub = pvk * G pubkey_c = Pub.get_public_key_bytes(True) #pubkey_u = point_to_ser(Pub,False) addr_c = public_key_to_p2pkh(pubkey_c) #print "Private key ", '%064x'%pvk eck = ecc.ECPrivkey(number_to_string(pvk, _r)) #print "Compressed public key ", pubkey_c.encode('hex') enc = ecc.ECPubkey(pubkey_c).encrypt_message(message) dec = eck.decrypt_message(enc) self.assertEqual(message, dec) #print "Uncompressed public key", pubkey_u.encode('hex') #enc2 = EC_KEY.encrypt_message(message, pubkey_u) dec2 = eck.decrypt_message(enc) self.assertEqual(message, dec2) signature = eck.sign_message(message, True) #print signature eck.verify_message_for_address(signature, message)
def _do_test_crypto(self, message): G = generator_secp256k1 _r = G.order() pvk = ecdsa.util.randrange(pow(2, 256)) % _r Pub = pvk * G pubkey_c = point_to_ser(Pub, True) #pubkey_u = point_to_ser(Pub,False) addr_c = public_key_to_p2pkh(pubkey_c) #print "Private key ", '%064x'%pvk eck = EC_KEY(number_to_string(pvk, _r)) #print "Compressed public key ", pubkey_c.encode('hex') enc = EC_KEY.encrypt_message(message, pubkey_c) dec = eck.decrypt_message(enc) self.assertEqual(message, dec) #print "Uncompressed public key", pubkey_u.encode('hex') #enc2 = EC_KEY.encrypt_message(message, pubkey_u) dec2 = eck.decrypt_message(enc) self.assertEqual(message, dec2) signature = eck.sign_message(message, True) #print signature EC_KEY.verify_message(eck, signature, message)
def _do_test_crypto(self, message): G = generator_secp256k1 _r = G.order() pvk = ecdsa.util.randrange( pow(2,256) ) %_r Pub = pvk*G pubkey_c = point_to_ser(Pub,True) #pubkey_u = point_to_ser(Pub,False) addr_c = public_key_to_p2pkh(pubkey_c) #addr_u = public_key_to_bc_address(pubkey_u) #print "Private key ", '%064x'%pvk eck = EC_KEY(number_to_string(pvk,_r)) #print "Compressed public key ", pubkey_c.encode('hex') enc = EC_KEY.encrypt_message(message, pubkey_c) dec = eck.decrypt_message(enc) assert dec == message #print "Uncompressed public key", pubkey_u.encode('hex') #enc2 = EC_KEY.encrypt_message(message, pubkey_u) dec2 = eck.decrypt_message(enc) assert dec2 == message signature = eck.sign_message(message, True) #print signature EC_KEY.verify_message(eck, signature, message)
def _do_test_crypto(self, message): G = ecc.generator() _r = G.order() pvk = ecdsa.util.randrange(_r) Pub = pvk*G pubkey_c = Pub.get_public_key_bytes(True) #pubkey_u = point_to_ser(Pub,False) addr_c = public_key_to_p2pkh(pubkey_c) #print "Private key ", '%064x'%pvk eck = ecc.ECPrivkey(number_to_string(pvk,_r)) #print "Compressed public key ", pubkey_c.encode('hex') enc = ecc.ECPubkey(pubkey_c).encrypt_message(message) dec = eck.decrypt_message(enc) self.assertEqual(message, dec) #print "Uncompressed public key", pubkey_u.encode('hex') #enc2 = EC_KEY.encrypt_message(message, pubkey_u) dec2 = eck.decrypt_message(enc) self.assertEqual(message, dec2) signature = eck.sign_message(message, True) #print signature eck.verify_message_for_address(signature, message)
def test_000_positive_test(self): begin_phase = Phase('Announcement') amount = 1000 fee = 1 # generate fake signing keys G = generator_secp256k1 _r = G.order() number_of_players = 4 players_pvks = [ ecdsa.util.randrange(pow(2, 256)) % _r for i in range(number_of_players) ] players_ecks = [ EC_KEY(number_to_string(pvk, _r)) for pvk in players_pvks ] players_new_pvks = [ ecdsa.util.randrange(pow(2, 256)) % _r for i in range(number_of_players) ] players_change_pvks = [ ecdsa.util.randrange(pow(2, 256)) % _r for i in range(number_of_players) ] players_changes = [ public_key_to_p2pkh(point_to_ser(pvk * G, True)) for pvk in players_change_pvks ] players_new_addresses = [ public_key_to_p2pkh(point_to_ser(pvk * G, True)) for pvk in players_new_pvks ] players_pks = [eck.get_public_key(True) for eck in players_ecks] players = dict(zip(range(number_of_players), players_pks)) print("\n") # serverThread = fakeServerThread(HOST, PORT, number_of_players = number_of_players) #( host, port, vk, amount, fee, sk, addr_new, change) playerThreads = [ protocolThread(HOST, PORT, players[player], amount, fee, players_ecks[player], players_new_addresses[player], players_changes[player]) for player in players ] # serverThread.start() for thread in playerThreads: thread.start() # serverThread.join() self.assertTrue(True)
def test_verify(self): announce = MasternodeAnnounce.deserialize(raw_announce_70210) message = announce.serialize_for_sig() pk = bitcoin.public_key_to_p2pkh(bfh(announce.collateral_key)) self.assertTrue(announce.verify())
def verify_signature(self, sig, message, vk): pk, compressed = pubkey_from_signature(sig, Hash(msg_magic(message))) address_from_signature = public_key_to_p2pkh(point_to_ser(pk.pubkey.point, compressed)) address_from_vk = self.address(vk) return address_from_signature == address_from_signature
def address(self, vk): return public_key_to_p2pkh(vk)