URI: 
       tbitcoin.py: parameterise a few address-related methods with network - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 77c8010517ffc1f1bb3bda3e37db77f6c4c524d8
   DIR parent 2d944919742c99647370812ced13a2230d9ade7d
  HTML Author: SomberNight <somber.night@protonmail.com>
       Date:   Tue, 20 Mar 2018 17:02:49 +0100
       
       bitcoin.py: parameterise a few address-related methods with network
       
       Diffstat:
         M lib/bitcoin.py                      |      34 +++++++++++++++++++------------
         M lib/transaction.py                  |       9 +++++----
       
       2 files changed, 26 insertions(+), 17 deletions(-)
       ---
   DIR diff --git a/lib/bitcoin.py b/lib/bitcoin.py
       t@@ -264,7 +264,7 @@ def hash_160(public_key):
                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 += h160
            return base_encode(s+Hash(s)[0:4], base=58)
       t@@ -276,17 +276,23 @@ def b58_address_to_hash160(addr):
            return _bytes[0], _bytes[1:21]
        
        
       -def hash160_to_p2pkh(h160):
       -    return hash160_to_b58_address(h160, constants.net.ADDRTYPE_P2PKH)
       +def hash160_to_p2pkh(h160, *, net=None):
       +    if net is None:
       +        net = constants.net
       +    return hash160_to_b58_address(h160, net.ADDRTYPE_P2PKH)
        
       -def hash160_to_p2sh(h160):
       -    return hash160_to_b58_address(h160, constants.net.ADDRTYPE_P2SH)
       +def hash160_to_p2sh(h160, *, net=None):
       +    if net is None:
       +        net = constants.net
       +    return hash160_to_b58_address(h160, net.ADDRTYPE_P2SH)
        
        def public_key_to_p2pkh(public_key):
            return hash160_to_p2pkh(hash_160(public_key))
        
       -def hash_to_segwit_addr(h, witver):
       -    return segwit_addr.encode(constants.net.SEGWIT_HRP, witver, h)
       +def hash_to_segwit_addr(h, witver, *, net=None):
       +    if net is None:
       +        net = constants.net
       +    return segwit_addr.encode(net.SEGWIT_HRP, witver, h)
        
        def public_key_to_p2wpkh(public_key):
            return hash_to_segwit_addr(hash_160(public_key), witver=0)
       t@@ -325,14 +331,16 @@ def redeem_script_to_address(txin_type, redeem_script):
                raise NotImplementedError(txin_type)
        
        
       -def script_to_address(script):
       +def script_to_address(script, *, net=None):
            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
            return addr
        
       -def address_to_script(addr):
       -    witver, witprog = segwit_addr.decode(constants.net.SEGWIT_HRP, addr)
       +def address_to_script(addr, *, net=None):
       +    if net is None:
       +        net = constants.net
       +    witver, witprog = segwit_addr.decode(net.SEGWIT_HRP, addr)
            if witprog is not None:
                assert (0 <= witver <= 16)
                OP_n = witver + 0x50 if witver > 0 else 0
       t@@ -340,11 +348,11 @@ def address_to_script(addr):
                script += push_script(bh2u(bytes(witprog)))
                return script
            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 += push_script(bh2u(hash_160))
                script += '88ac'                                     # op_equalverify, op_checksig
       -    elif addrtype == constants.net.ADDRTYPE_P2SH:
       +    elif addrtype == net.ADDRTYPE_P2SH:
                script = 'a9'                                        # op_hash_160
                script += push_script(bh2u(hash_160))
                script += '87'                                       # op_equal
   DIR diff --git a/lib/transaction.py b/lib/transaction.py
       t@@ -402,7 +402,8 @@ def parse_redeemScript(s):
            redeemScript = multisig_script(pubkeys, m)
            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)]
        
            # The Genesis Block, self-payments, and pay-by-IP-address payments look like:
       t@@ -415,19 +416,19 @@ def get_address_from_output_script(_bytes):
            # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
            match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
            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
            match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
            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
            possible_witness_versions = [opcodes.OP_0] + list(range(opcodes.OP_1, opcodes.OP_16 + 1))
            for witver, opcode in enumerate(possible_witness_versions):
                match = [ opcode, opcodes.OP_PUSHDATA4 ]
                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)