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_public_key_from_private_key(self): for priv_details in self.priv_pub_addr: txin_type, privkey, compressed = deserialize_privkey( priv_details['priv']) result = ecc.ECPrivkey(privkey).get_public_key_hex( compressed=compressed) self.assertEqual(priv_details['pub'], result) self.assertEqual(priv_details['txin_type'], txin_type) self.assertEqual(priv_details['compressed'], compressed)
def test_sign_transaction(self): eckey1 = ecc.ECPrivkey( bfh('7e1255fddb52db1729fc3ceb21a46f95b8d9fe94cc83425e936a6c5223bb679d' )) sig1 = eckey1.sign_transaction( bfh('5a548b12369a53faaa7e51b5081829474ebdd9c924b3a8230b69aa0be254cd94' )) self.assertEqual( bfh('3045022100902a288b98392254cd23c0e9a49ac6d7920f171b8249a48e484b998f1874a2010220723d844826828f092cf400cb210c4fa0b8cd1b9d1a7f21590e78e022ff6476b9' ), sig1) eckey2 = ecc.ECPrivkey( bfh('c7ce8c1462c311eec24dff9e2532ac6241e50ae57e7d1833af21942136972f23' )) sig2 = eckey2.sign_transaction( bfh('642a2e66332f507c92bda910158dfe46fc10afbf72218764899d3af99a043fac' )) self.assertEqual( bfh('30440220618513f4cfc87dde798ce5febae7634c23e7b9254a1eabf486be820f6a7c2c4702204fef459393a2b931f949e63ced06888f35e286e446dc46feb24b5b5f81c6ed52' ), sig2)
def sign_message_with_wif_privkey(wif_privkey, msg): txin_type, privkey, compressed = deserialize_privkey(wif_privkey) key = ecc.ECPrivkey(privkey) return key.sign_message(msg, compressed)
def f(xprv): _, _, _, _, c, k = deserialize_xprv(xprv) pk = bip32_private_key([0, 0], k, c) key = ecc.ECPrivkey(pk) sig = key.sign_message(message, True) return base64.b64encode(sig).decode()
def on_receive(self, keyhash, message): self.print_error("signal arrived for", keyhash) WAIT_TIME = 10 * 60 if self.suppress_notifications: for window, xpub, K, _hash in self.cosigner_list: if server.get(_hash+'_lock'): return self.suppress_notifications = False for key, _hash, window in self.keys: if _hash == keyhash: break else: self.print_error("keyhash not found") return wallet = window.wallet if isinstance(wallet.keystore, keystore.Hardware_KeyStore): window.show_warning(_('An encrypted transaction was retrieved from cosigning pool.') + '\n' + _('However, hardware wallets do not support message decryption, ' 'which makes them not compatible with the current design of cosigner pool.')) return elif wallet.has_keystore_encryption(): # set pick to false when opening password dialog server.put(keyhash+'_pick', 'False') password = window.password_dialog(_('An encrypted transaction was retrieved from cosigning pool.') + '\n' + _('Please enter your password to decrypt it.')) if not password: # set pick back to true if password incorrect or omitted server.put(keyhash+'_pick', 'True') return else: password = None if not window.question(_("An encrypted transaction was retrieved from cosigning pool.") + '\n' + _("Do you want to open it now?")): return xprv = wallet.keystore.get_master_private_key(password) if not xprv: return try: k = bip32.deserialize_xprv(xprv)[-1] EC = ecc.ECPrivkey(k) message = bh2u(EC.decrypt_message(message)) except Exception as e: traceback.print_exc(file=sys.stdout) window.show_error(_('Error decrypting message') + ':\n' + str(e)) return tx = transaction.Transaction(message) def calculate_wait_time(expire): # calculate wait time wait_time = int((WAIT_TIME - (int(server.get_current_time()) - int(expire)))) mins, secs = divmod(wait_time, 60) return '{:02d}:{:02d}'.format(mins, secs) # check if lock has been placed for any wallets for window, xpub, K, _hash in self.cosigner_list: expire = server.get(_hash+'_lock') if expire: # set pick back to true if user lock is present server.put(keyhash+'_pick', 'True') # suppress any further notifications self.suppress_notifications = True # calculate wait time based on lock expiry and server time timeformat = calculate_wait_time(expire) # display pop up window.show_warning(_("A cosigner is currently signing the transaction.") + '\n' + _("Please wait {} until the signing has concluded.".format(timeformat))) show_timeout_wait_dialog(tx, window, prompt_if_unsaved=True) return # test if wallet has previously placed a lock current_wallet_lock = server.get(keyhash+'_lock') if not current_wallet_lock: # no lock has been placed for current wallet => lock transaction dialog server.put(keyhash+'_lock', str(server.get_current_time())) time_until_expired = '10 minutes' else: time_until_expired = calculate_wait_time(current_wallet_lock) # place flag to test for graceful shutdown server.put(keyhash+'_shutdown', 'up') window.show_warning(_("You have {} to conclude signing after which the dialog will".format(time_until_expired)) + '\n' + _("automatically close.")) show_transaction_timeout(tx, window, prompt_if_unsaved=True)