def address_to_scriptpubkey(address): # Raise ValueError if we cannot identify the address. get_version(address) try: version = b58decode_check(address)[:1] except ValueError: witver, data = segwit_decode(address) return segwit_scriptpubkey(witver, data) if version == MAIN_PUBKEY_HASH or version == TEST_PUBKEY_HASH: return OP_DUP + OP_HASH160 + OP_PUSH_20 + address_to_public_key_hash( address) + OP_EQUALVERIFY + OP_CHECKSIG elif version == MAIN_SCRIPT_HASH or version == TEST_SCRIPT_HASH: return OP_HASH160 + OP_PUSH_20 + address_to_public_key_hash( address) + OP_EQUAL
def construct_outputs(outputs): outputs_obj = [] for data in outputs: dest, amount = data # Segwit address (Bech32) if amount and (dest[0:2] == 'bc' or dest[0:2] == 'tb'): hrp = dest[0:2] witver, witprog = segwit_decode(hrp, dest) script = segwit_scriptpubkey(witver, witprog) amount = amount.to_bytes(8, byteorder='little') # P2SH elif amount and (b58decode_check(dest)[0:1] == MAIN_SCRIPT_HASH or b58decode_check(dest)[0:1] == TEST_SCRIPT_HASH): script = (OP_HASH160 + OP_PUSH_20 + address_to_public_key_hash(dest) + OP_EQUAL) amount = amount.to_bytes(8, byteorder='little') # P2PKH elif amount: script = (OP_DUP + OP_HASH160 + OP_PUSH_20 + address_to_public_key_hash(dest) + OP_EQUALVERIFY + OP_CHECKSIG) amount = amount.to_bytes(8, byteorder='little') # Blockchain storage else: script = (OP_RETURN + len(dest).to_bytes(1, byteorder='little') + dest) amount = b'\x00\x00\x00\x00\x00\x00\x00\x00' outputs_obj.append(TxOut(amount, script)) return outputs_obj