electrum

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

commit 05e5ef16eb0b11f580ded5ad5501316888f97d26
parent 5d7157451fb47ffda8704d253e941a29a82cefcd
Author: SomberNight <somber.night@protonmail.com>
Date:   Thu, 14 Jun 2018 15:11:20 +0200

transaction.py: more flexible sign() method

handles both x_pubkeys and pubkeys in keypairs

Diffstat:
Mlib/transaction.py | 37++++++++++++++++++++-----------------
Mlib/wallet.py | 5++---
2 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/lib/transaction.py b/lib/transaction.py @@ -1090,29 +1090,32 @@ class Transaction: s, r = self.signature_count() return r == s - def sign(self, keypairs): + def sign(self, keypairs) -> None: + # keypairs: (x_)pubkey -> secret_bytes for i, txin in enumerate(self.inputs()): - num = txin['num_sig'] pubkeys, x_pubkeys = self.get_sorted_pubkeys(txin) - for j, x_pubkey in enumerate(x_pubkeys): - signatures = list(filter(None, txin['signatures'])) - if len(signatures) == num: - # txin is complete + for j, (pubkey, x_pubkey) in enumerate(zip(pubkeys, x_pubkeys)): + if self.is_txin_complete(txin): break - if x_pubkey in keypairs.keys(): - print_error("adding signature for", x_pubkey) - sec, compressed = keypairs.get(x_pubkey) - pubkey = ecc.ECPrivkey(sec).get_public_key_hex(compressed=compressed) - # add signature - sig = self.sign_txin(i, sec) - self.add_signature_to_txin(txin, j, sig) - #txin['x_pubkeys'][j] = pubkey - txin['pubkeys'][j] = pubkey # needed for fd keys - self._inputs[i] = txin + if pubkey in keypairs: + _pubkey = pubkey + elif x_pubkey in keypairs: + _pubkey = x_pubkey + else: + continue + print_error("adding signature for", _pubkey) + sec, compressed = keypairs.get(_pubkey) + # pubkey might not actually be a 02-04 pubkey for fd keys; so: + pubkey = ecc.ECPrivkey(sec).get_public_key_hex(compressed=compressed) + # add signature + sig = self.sign_txin(i, sec) + self.add_signature_to_txin(txin, j, sig) + txin['pubkeys'][j] = pubkey # needed for fd keys + self._inputs[i] = txin print_error("is_complete", self.is_complete()) self.raw = self.serialize() - def sign_txin(self, txin_index, privkey_bytes): + def sign_txin(self, txin_index, privkey_bytes) -> str: pre_hash = Hash(bfh(self.serialize_preimage(txin_index))) privkey = ecc.ECPrivkey(privkey_bytes) sig = privkey.sign_transaction(pre_hash) diff --git a/lib/wallet.py b/lib/wallet.py @@ -1220,9 +1220,8 @@ class Abstract_Wallet(PrintError): if fixed_fee is None and config.fee_per_kb() is None: raise NoDynamicFeeEstimates() - if not is_sweep: - for item in inputs: - self.add_input_info(item) + for item in inputs: + self.add_input_info(item) # change address if change_addr: