electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 6833adf8b6fb8a48babc4c503d23e740d9b586f4
parent e85fb25146e793bcecf9a4e666e6fae315bb0159
Author: ThomasV <thomasv@electrum.org>
Date:   Wed, 26 Feb 2020 14:16:21 +0100

simplify previous commit (revert changes on transaction.py)

Diffstat:
Melectrum/lnpeer.py | 9+++++++--
Melectrum/transaction.py | 45+++++++++++++++++++--------------------------
2 files changed, 26 insertions(+), 28 deletions(-)

diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py @@ -1398,6 +1398,11 @@ class Peer(Logger): def send_closing_signed(): our_sig, closing_tx = chan.make_closing_tx(our_scriptpubkey, their_scriptpubkey, fee_sat=our_fee, drop_remote=drop_remote) self.send_message('closing_signed', channel_id=chan.channel_id, fee_satoshis=our_fee, signature=our_sig) + def verify_signature(tx, sig): + their_pubkey = chan.config[REMOTE].multisig_key.pubkey + preimage_hex = tx.serialize_preimage(0) + pre_hash = sha256d(bfh(preimage_hex)) + return ecc.verify_signature(their_pubkey, sig, pre_hash) # the funder sends the first 'closing_signed' message if chan.constraints.is_initiator: send_closing_signed() @@ -1411,11 +1416,11 @@ class Peer(Logger): their_sig = cs_payload['signature'] # verify their sig: they might have dropped their output our_sig, closing_tx = chan.make_closing_tx(our_scriptpubkey, their_scriptpubkey, fee_sat=their_fee, drop_remote=False) - if closing_tx.verify_signature(0, their_sig): + if not verify_signature(closing_tx, their_sig): drop_remote = False else: our_sig, closing_tx = chan.make_closing_tx(our_scriptpubkey, their_scriptpubkey, fee_sat=their_fee, drop_remote=True) - if closing_tx.verify_signature(0, their_sig): + if not verify_signature(closing_tx, their_sig): drop_remote = True else: raise Exception('failed to verify their signature') diff --git a/electrum/transaction.py b/electrum/transaction.py @@ -1822,37 +1822,30 @@ class PartialTransaction(Transaction): if len(self.inputs()) != len(signatures): raise Exception('expected {} signatures; got {}'.format(len(self.inputs()), len(signatures))) for i, txin in enumerate(self.inputs()): + pubkeys = [pk.hex() for pk in txin.pubkeys] sig = signatures[i] if bfh(sig) in list(txin.part_sigs.values()): continue - sig_bytes = ecc.sig_string_from_der_sig(bfh(sig[:-2])) - signing_pubkey = self.verify_signature(i, sig_bytes) - if signing_pubkey: - _logger.info(f"adding sig: txin_idx={i}, signing_pubkey={signing_pubkey.hex()}, sig={sig}") - self.add_signature_to_txin(txin_idx=i, signing_pubkey=signing_pubkey.hex(), sig=sig) - # redo raw - self.invalidate_ser_cache() - - def verify_signature(self, i: int, sig: bytes) -> bytes: - # returns the signing pubkey if verification passes - txin = self.inputs()[i] - pubkeys = [pk for pk in txin.pubkeys] - pre_hash = sha256d(bfh(self.serialize_preimage(i))) - for recid in range(4): - try: - public_key = ecc.ECPubkey.from_sig_string(sig, recid, pre_hash) - except ecc.InvalidECPointException: - # the point might not be on the curve for some recid values - continue - pubkey = public_key.get_public_key_bytes(compressed=True) - if pubkey in pubkeys: + pre_hash = sha256d(bfh(self.serialize_preimage(i))) + sig_string = ecc.sig_string_from_der_sig(bfh(sig[:-2])) + for recid in range(4): try: - public_key.verify_message_hash(sig, pre_hash) - except Exception: - _logger.exception('') + public_key = ecc.ECPubkey.from_sig_string(sig_string, recid, pre_hash) + except ecc.InvalidECPointException: + # the point might not be on the curve for some recid values continue - return pubkey - return False + pubkey_hex = public_key.get_public_key_hex(compressed=True) + if pubkey_hex in pubkeys: + try: + public_key.verify_message_hash(sig_string, pre_hash) + except Exception: + _logger.exception('') + continue + _logger.info(f"adding sig: txin_idx={i}, signing_pubkey={pubkey_hex}, sig={sig}") + self.add_signature_to_txin(txin_idx=i, signing_pubkey=pubkey_hex, sig=sig) + break + # redo raw + self.invalidate_ser_cache() def add_signature_to_txin(self, *, txin_idx: int, signing_pubkey: str, sig: str): txin = self._inputs[txin_idx]