tmove address_to_script to bitcoin.py - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 794baa16c8a13319b1e1a232998505054de3f626 DIR parent 5f1d9cbcf5b79de720529f0a1771650dd8c6add9 HTML Author: ThomasV <thomasv@electrum.org> Date: Tue, 29 Aug 2017 09:53:16 +0200 move address_to_script to bitcoin.py Diffstat: M lib/bitcoin.py | 20 ++++++++++++++++++++ M lib/transaction.py | 23 ++++------------------- M lib/wallet.py | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) --- DIR diff --git a/lib/bitcoin.py b/lib/bitcoin.py t@@ -199,6 +199,9 @@ def op_push(i): else: return '4e' + int_to_hex(i,4) +def push_script(x): + return op_push(len(x)//2) + x + def sha256(x): x = to_bytes(x, 'utf8') t@@ -314,7 +317,24 @@ def public_key_to_p2pkh(public_key): def public_key_to_p2wpkh(public_key): return hash_160_to_bc_address(hash_160(public_key), ADDRTYPE_P2WPKH) +def address_to_script(addr): + addrtype, hash_160 = bc_address_to_hash_160(addr) + if addrtype == ADDRTYPE_P2PKH: + script = '76a9' # op_dup, op_hash_160 + script += push_script(bh2u(hash_160)) + script += '88ac' # op_equalverify, op_checksig + elif addrtype == ADDRTYPE_P2SH: + script = 'a9' # op_hash_160 + script += push_script(bh2u(hash_160)) + script += '87' # op_equal + else: + raise BaseException('unknown address type') + return script +def address_to_scripthash(addr): + script = address_to_script(addr) + h = sha256(bytes.fromhex(script))[0:32] + return bytes(reversed(h)).hex() __b58chars = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' DIR diff --git a/lib/transaction.py b/lib/transaction.py t@@ -456,23 +456,8 @@ def deserialize(raw): # pay & redeem scripts -def push_script(x): - return op_push(len(x)//2) + x - - -def get_scriptPubKey(addr): - addrtype, hash_160 = bc_address_to_hash_160(addr) - if addrtype == bitcoin.ADDRTYPE_P2PKH: - script = '76a9' # op_dup, op_hash_160 - script += push_script(bh2u(hash_160)) - script += '88ac' # op_equalverify, op_checksig - elif addrtype == bitcoin.ADDRTYPE_P2SH: - script = 'a9' # op_hash_160 - script += push_script(bh2u(hash_160)) - script += '87' # op_equal - else: - raise BaseException('unknown address type') - return script + + def segwit_script(pubkey): t@@ -601,7 +586,7 @@ class Transaction: if output_type == TYPE_SCRIPT: return bh2u(addr) elif output_type == TYPE_ADDRESS: - return get_scriptPubKey(addr) + return bitcoin.address_to_script(addr) else: raise TypeError('Unknown output type') return script t@@ -667,7 +652,7 @@ class Transaction: def get_preimage_script(self, txin): # only for non-segwit if txin['type'] == 'p2pkh': - return get_scriptPubKey(txin['address']) + return bitcoin.address_to_script(txin['address']) elif txin['type'] == 'p2sh': pubkeys, x_pubkeys = self.get_sorted_pubkeys(txin) return multisig_script(pubkeys, txin['num_sig']) DIR diff --git a/lib/wallet.py b/lib/wallet.py t@@ -883,7 +883,7 @@ class Abstract_Wallet(PrintError): pubkey = public_key_from_private_key(privkey) address = address_from_private_key(privkey) u = network.synchronous_get(('blockchain.address.listunspent', [address])) - pay_script = transaction.get_scriptPubKey(address) + pay_script = bitcoin.address_to_script(address) for item in u: if len(inputs) >= imax: break