diff --git a/electrum/bitcoin.py b/electrum/bitcoin.py index 3917e2d87..27bf1d619 100644 --- a/electrum/bitcoin.py +++ b/electrum/bitcoin.py @@ -400,9 +400,7 @@ def address_to_script(addr: str, *, net=None) -> str: return script addrtype, hash_160_ = b58_address_to_hash160(addr) if addrtype == net.ADDRTYPE_P2PKH: - script = bytes([opcodes.OP_DUP, opcodes.OP_HASH160]).hex() - script += push_script(bh2u(hash_160_)) - script += bytes([opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG]).hex() + script = pubkeyhash_to_p2pkh_script(bh2u(hash_160_)) elif addrtype == net.ADDRTYPE_P2SH: script = opcodes.OP_HASH160.hex() script += push_script(bh2u(hash_160_)) @@ -422,6 +420,13 @@ def script_to_scripthash(script: str) -> str: def public_key_to_p2pk_script(pubkey: str) -> str: return push_script(pubkey) + opcodes.OP_CHECKSIG.hex() +def pubkeyhash_to_p2pkh_script(pubkey_hash160: str) -> str: + script = bytes([opcodes.OP_DUP, opcodes.OP_HASH160]).hex() + script += push_script(pubkey_hash160) + script += bytes([opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG]).hex() + return script + + __b58chars = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' assert len(__b58chars) == 58 diff --git a/electrum/transaction.py b/electrum/transaction.py index 1501e0ef5..bcacc7f2a 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -909,14 +909,12 @@ class Transaction: return preimage_script pubkeys, x_pubkeys = self.get_sorted_pubkeys(txin) - if txin['type'] == 'p2pkh': - return bitcoin.address_to_script(txin['address']) - elif txin['type'] in ['p2sh', 'p2wsh', 'p2wsh-p2sh']: + if txin['type'] in ['p2sh', 'p2wsh', 'p2wsh-p2sh']: return multisig_script(pubkeys, txin['num_sig']) - elif txin['type'] in ['p2wpkh', 'p2wpkh-p2sh']: + elif txin['type'] in ['p2pkh', 'p2wpkh', 'p2wpkh-p2sh']: pubkey = pubkeys[0] pkh = bh2u(hash_160(bfh(pubkey))) - return '76a9' + push_script(pkh) + '88ac' + return bitcoin.pubkeyhash_to_p2pkh_script(pkh) elif txin['type'] == 'p2pk': pubkey = pubkeys[0] return bitcoin.public_key_to_p2pk_script(pubkey)