move address_to_script to bitcoin.py

This commit is contained in:
ThomasV 2017-08-29 09:53:16 +02:00
parent 5f1d9cbcf5
commit 794baa16c8
3 changed files with 23 additions and 18 deletions

View File

@ -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')
@ -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'

View File

@ -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):
@ -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
@ -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'])

View File

@ -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