ttransaction.py: sign_txin. allow override for get_preimage_script. - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 97296432a2bcbdc13979d94f67e283666344c7e9 DIR parent 1d6f000868caa02607129a99cad7bed8ec3f28df HTML Author: SomberNight <somber.night@protonmail.com> Date: Mon, 23 Apr 2018 13:52:20 +0200 ttransaction.py: sign_txin. allow override for get_preimage_script. Diffstat: M lib/transaction.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) --- DIR diff --git a/lib/transaction.py b/lib/transaction.py t@@ -820,6 +820,10 @@ class Transaction: @classmethod def get_preimage_script(self, txin): + preimage_script = txin.get('preimage_script', None) + if preimage_script is not None: + return preimage_script + pubkeys, x_pubkeys = self.get_sorted_pubkeys(txin) if txin['type'] == 'p2pkh': return bitcoin.address_to_script(txin['address']) t@@ -1041,15 +1045,7 @@ class Transaction: sec, compressed = keypairs.get(x_pubkey) pubkey = public_key_from_private_key(sec, compressed) # add signature - pre_hash = Hash(bfh(self.serialize_preimage(i))) - pkey = regenerate_key(sec) - secexp = pkey.secret - private_key = bitcoin.MySigningKey.from_secret_exponent(secexp, curve = SECP256k1) - public_key = private_key.get_verifying_key() - sig = private_key.sign_digest_deterministic(pre_hash, hashfunc=hashlib.sha256, sigencode = ecdsa.util.sigencode_der) - if not public_key.verify_digest(sig, pre_hash, sigdecode = ecdsa.util.sigdecode_der): - raise Exception('Sanity check verifying our own signature failed.') - sig = bh2u(sig) + '01' + 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 t@@ -1057,6 +1053,18 @@ class Transaction: print_error("is_complete", self.is_complete()) self.raw = self.serialize() + def sign_txin(self, txin_index, privkey_bytes): + pre_hash = Hash(bfh(self.serialize_preimage(txin_index))) + pkey = regenerate_key(privkey_bytes) + secexp = pkey.secret + private_key = bitcoin.MySigningKey.from_secret_exponent(secexp, curve=SECP256k1) + public_key = private_key.get_verifying_key() + sig = private_key.sign_digest_deterministic(pre_hash, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der) + if not public_key.verify_digest(sig, pre_hash, sigdecode=ecdsa.util.sigdecode_der): + raise Exception('Sanity check verifying our own signature failed.') + sig = bh2u(sig) + '01' + return sig + def get_outputs(self): """convert pubkeys to addresses""" o = []