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