Ejemplo n.º 1
0
def test_address_to_public_key_hash():
    assert address_to_public_key_hash(BITCOIN_ADDRESS) == PUBKEY_HASH
    assert address_to_public_key_hash(
        BITCOIN_ADDRESS_COMPRESSED) == PUBKEY_HASH_COMPRESSED
    assert address_to_public_key_hash(BITCOIN_ADDRESS_PAY2SH) == PAY2SH_HASH
    assert address_to_public_key_hash(
        BITCOIN_ADDRESS_TEST_PAY2SH) == PAY2SH_TEST_HASH
Ejemplo n.º 2
0
def construct_outputs(outputs):
    outputs_obj = []

    for data in outputs:
        dest, amount = data

        # P2SH
        if 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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
def construct_output_block(outputs):

    output_block = b''

    for data in outputs:
        dest, amount = data

        # Real recipient
        if amount:
            script = (OP_DUP + OP_HASH160 + OP_PUSH_20 +
                      address_to_public_key_hash(dest) + OP_EQUALVERIFY +
                      OP_CHECKSIG)

            output_block += amount.to_bytes(8, byteorder='little')

        # Blockchain storage
        else:
            script = (OP_RETURN + len(dest).to_bytes(1, byteorder='little') +
                      dest)

            output_block += b'\x00\x00\x00\x00\x00\x00\x00\x00'

        output_block += int_to_unknown_bytes(len(script), byteorder='little')
        output_block += script

    return output_block
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def test_address_to_public_key_hash():
    assert address_to_public_key_hash(BITCOIN_ADDRESS) == PUBKEY_HASH
    assert address_to_public_key_hash(
        BITCOIN_ADDRESS_COMPRESSED) == PUBKEY_HASH_COMPRESSED
    with pytest.raises(ValueError):
        address_to_public_key_hash(BITCOIN_ADDRESS_PAY2SH)
    with pytest.raises(ValueError):
        address_to_public_key_hash(BITCOIN_ADDRESS_TEST_PAY2SH)