bitcoin.py: parameterise a few address-related methods with network
This commit is contained in:
@@ -264,7 +264,7 @@ def hash_160(public_key):
|
|||||||
return md.digest()
|
return md.digest()
|
||||||
|
|
||||||
|
|
||||||
def hash160_to_b58_address(h160, addrtype, witness_program_version=1):
|
def hash160_to_b58_address(h160, addrtype):
|
||||||
s = bytes([addrtype])
|
s = bytes([addrtype])
|
||||||
s += h160
|
s += h160
|
||||||
return base_encode(s+Hash(s)[0:4], base=58)
|
return base_encode(s+Hash(s)[0:4], base=58)
|
||||||
@@ -276,17 +276,23 @@ def b58_address_to_hash160(addr):
|
|||||||
return _bytes[0], _bytes[1:21]
|
return _bytes[0], _bytes[1:21]
|
||||||
|
|
||||||
|
|
||||||
def hash160_to_p2pkh(h160):
|
def hash160_to_p2pkh(h160, *, net=None):
|
||||||
return hash160_to_b58_address(h160, constants.net.ADDRTYPE_P2PKH)
|
if net is None:
|
||||||
|
net = constants.net
|
||||||
|
return hash160_to_b58_address(h160, net.ADDRTYPE_P2PKH)
|
||||||
|
|
||||||
def hash160_to_p2sh(h160):
|
def hash160_to_p2sh(h160, *, net=None):
|
||||||
return hash160_to_b58_address(h160, constants.net.ADDRTYPE_P2SH)
|
if net is None:
|
||||||
|
net = constants.net
|
||||||
|
return hash160_to_b58_address(h160, net.ADDRTYPE_P2SH)
|
||||||
|
|
||||||
def public_key_to_p2pkh(public_key):
|
def public_key_to_p2pkh(public_key):
|
||||||
return hash160_to_p2pkh(hash_160(public_key))
|
return hash160_to_p2pkh(hash_160(public_key))
|
||||||
|
|
||||||
def hash_to_segwit_addr(h, witver):
|
def hash_to_segwit_addr(h, witver, *, net=None):
|
||||||
return segwit_addr.encode(constants.net.SEGWIT_HRP, witver, h)
|
if net is None:
|
||||||
|
net = constants.net
|
||||||
|
return segwit_addr.encode(net.SEGWIT_HRP, witver, h)
|
||||||
|
|
||||||
def public_key_to_p2wpkh(public_key):
|
def public_key_to_p2wpkh(public_key):
|
||||||
return hash_to_segwit_addr(hash_160(public_key), witver=0)
|
return hash_to_segwit_addr(hash_160(public_key), witver=0)
|
||||||
@@ -325,14 +331,16 @@ def redeem_script_to_address(txin_type, redeem_script):
|
|||||||
raise NotImplementedError(txin_type)
|
raise NotImplementedError(txin_type)
|
||||||
|
|
||||||
|
|
||||||
def script_to_address(script):
|
def script_to_address(script, *, net=None):
|
||||||
from .transaction import get_address_from_output_script
|
from .transaction import get_address_from_output_script
|
||||||
t, addr = get_address_from_output_script(bfh(script))
|
t, addr = get_address_from_output_script(bfh(script), net=net)
|
||||||
assert t == TYPE_ADDRESS
|
assert t == TYPE_ADDRESS
|
||||||
return addr
|
return addr
|
||||||
|
|
||||||
def address_to_script(addr):
|
def address_to_script(addr, *, net=None):
|
||||||
witver, witprog = segwit_addr.decode(constants.net.SEGWIT_HRP, addr)
|
if net is None:
|
||||||
|
net = constants.net
|
||||||
|
witver, witprog = segwit_addr.decode(net.SEGWIT_HRP, addr)
|
||||||
if witprog is not None:
|
if witprog is not None:
|
||||||
assert (0 <= witver <= 16)
|
assert (0 <= witver <= 16)
|
||||||
OP_n = witver + 0x50 if witver > 0 else 0
|
OP_n = witver + 0x50 if witver > 0 else 0
|
||||||
@@ -340,11 +348,11 @@ def address_to_script(addr):
|
|||||||
script += push_script(bh2u(bytes(witprog)))
|
script += push_script(bh2u(bytes(witprog)))
|
||||||
return script
|
return script
|
||||||
addrtype, hash_160 = b58_address_to_hash160(addr)
|
addrtype, hash_160 = b58_address_to_hash160(addr)
|
||||||
if addrtype == constants.net.ADDRTYPE_P2PKH:
|
if addrtype == net.ADDRTYPE_P2PKH:
|
||||||
script = '76a9' # op_dup, op_hash_160
|
script = '76a9' # op_dup, op_hash_160
|
||||||
script += push_script(bh2u(hash_160))
|
script += push_script(bh2u(hash_160))
|
||||||
script += '88ac' # op_equalverify, op_checksig
|
script += '88ac' # op_equalverify, op_checksig
|
||||||
elif addrtype == constants.net.ADDRTYPE_P2SH:
|
elif addrtype == net.ADDRTYPE_P2SH:
|
||||||
script = 'a9' # op_hash_160
|
script = 'a9' # op_hash_160
|
||||||
script += push_script(bh2u(hash_160))
|
script += push_script(bh2u(hash_160))
|
||||||
script += '87' # op_equal
|
script += '87' # op_equal
|
||||||
|
|||||||
@@ -402,7 +402,8 @@ def parse_redeemScript(s):
|
|||||||
redeemScript = multisig_script(pubkeys, m)
|
redeemScript = multisig_script(pubkeys, m)
|
||||||
return m, n, x_pubkeys, pubkeys, redeemScript
|
return m, n, x_pubkeys, pubkeys, redeemScript
|
||||||
|
|
||||||
def get_address_from_output_script(_bytes):
|
|
||||||
|
def get_address_from_output_script(_bytes, *, net=None):
|
||||||
decoded = [x for x in script_GetOp(_bytes)]
|
decoded = [x for x in script_GetOp(_bytes)]
|
||||||
|
|
||||||
# The Genesis Block, self-payments, and pay-by-IP-address payments look like:
|
# The Genesis Block, self-payments, and pay-by-IP-address payments look like:
|
||||||
@@ -415,19 +416,19 @@ def get_address_from_output_script(_bytes):
|
|||||||
# DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
|
# DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
|
||||||
match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
|
match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
|
||||||
if match_decoded(decoded, match):
|
if match_decoded(decoded, match):
|
||||||
return TYPE_ADDRESS, hash160_to_p2pkh(decoded[2][1])
|
return TYPE_ADDRESS, hash160_to_p2pkh(decoded[2][1], net=net)
|
||||||
|
|
||||||
# p2sh
|
# p2sh
|
||||||
match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
|
match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
|
||||||
if match_decoded(decoded, match):
|
if match_decoded(decoded, match):
|
||||||
return TYPE_ADDRESS, hash160_to_p2sh(decoded[1][1])
|
return TYPE_ADDRESS, hash160_to_p2sh(decoded[1][1], net=net)
|
||||||
|
|
||||||
# segwit address
|
# segwit address
|
||||||
possible_witness_versions = [opcodes.OP_0] + list(range(opcodes.OP_1, opcodes.OP_16 + 1))
|
possible_witness_versions = [opcodes.OP_0] + list(range(opcodes.OP_1, opcodes.OP_16 + 1))
|
||||||
for witver, opcode in enumerate(possible_witness_versions):
|
for witver, opcode in enumerate(possible_witness_versions):
|
||||||
match = [ opcode, opcodes.OP_PUSHDATA4 ]
|
match = [ opcode, opcodes.OP_PUSHDATA4 ]
|
||||||
if match_decoded(decoded, match):
|
if match_decoded(decoded, match):
|
||||||
return TYPE_ADDRESS, hash_to_segwit_addr(decoded[1][1], witver=witver)
|
return TYPE_ADDRESS, hash_to_segwit_addr(decoded[1][1], witver=witver, net=net)
|
||||||
|
|
||||||
return TYPE_SCRIPT, bh2u(_bytes)
|
return TYPE_SCRIPT, bh2u(_bytes)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user