Esempio n. 1
0
def make_send_to_address_tx(recipient_address,
                            amount,
                            private_key,
                            blockchain_client=BlockchainInfoClient(),
                            fee=STANDARD_FEE,
                            change_address=None):
    """ Builds and signs a "send to address" transaction.
    """
    # get out the private key object, sending address, and inputs
    private_key_obj, from_address, inputs = analyze_private_key(
        private_key, blockchain_client)
    # get the change address
    if not change_address:
        change_address = from_address
    # create the outputs
    outputs = make_pay_to_address_outputs(recipient_address,
                                          amount,
                                          inputs,
                                          change_address,
                                          fee=fee)
    # serialize the transaction
    unsigned_tx = serialize_transaction(inputs, outputs)

    # generate a scriptSig for each input
    for i in xrange(0, len(inputs)):
        signed_tx = sign_transaction(unsigned_tx, i, private_key_obj.to_hex())
        unsigned_tx = signed_tx

    # return the signed tx
    return signed_tx
Esempio n. 2
0
def make_op_return_tx(data,
                      private_key,
                      blockchain_client=BlockchainInfoClient(),
                      fee=OP_RETURN_FEE,
                      change_address=None,
                      format='bin'):
    """ Builds and signs an OP_RETURN transaction.
    """
    # get out the private key object, sending address, and inputs
    private_key_obj, from_address, inputs = analyze_private_key(
        private_key, blockchain_client)
    # get the change address
    if not change_address:
        change_address = from_address
    # create the outputs
    outputs = make_op_return_outputs(data,
                                     inputs,
                                     change_address,
                                     fee=fee,
                                     format=format)
    # serialize the transaction
    unsigned_tx = serialize_transaction(inputs, outputs)

    # generate a scriptSig for each input
    for i in xrange(0, len(inputs)):
        signed_tx = sign_transaction(unsigned_tx, i, private_key_obj.to_hex())
        unsigned_tx = signed_tx

    # return the signed tx
    return signed_tx
Esempio n. 3
0
def serialize_sign_and_broadcast(inputs, outputs, private_key,
                                 blockchain_client=BlockchainInfoClient()):
    # extract the private key object
    private_key_obj = get_private_key_obj(private_key)

    # serialize the transaction
    unsigned_tx = serialize_transaction(inputs, outputs)
    
    # generate a scriptSig for each input
    for i in xrange(0, len(inputs)):
        signed_tx = sign_transaction( unsigned_tx, i, private_key_obj.to_hex() )
        unsigned_tx = signed_tx

    # dispatch the signed transction to the network
    response = broadcast_transaction(signed_tx, blockchain_client)
    # return the response
    return response
Esempio n. 4
0
def serialize_sign_and_broadcast(inputs, outputs, private_key,
                                 blockchain_client=BlockchainInfoClient()):
    # extract the private key object
    private_key_obj = get_private_key_obj(private_key)

    # serialize the transaction
    unsigned_tx = serialize_transaction(inputs, outputs)

    # generate a scriptSig for each input
    for i in xrange(0, len(inputs)):
        signed_tx = sign_transaction(unsigned_tx, i, private_key_obj.to_hex())
        unsigned_tx = signed_tx

    # dispatch the signed transaction to the network
    response = broadcast_transaction(signed_tx, blockchain_client)
    # return the response
    return response
Esempio n. 5
0
def sign_all_unsigned_inputs(hex_privkey, unsigned_tx_hex):
    """
        Sign a serialized transaction's unsigned inputs

        @hex_privkey: private key that should sign inputs
        @unsigned_tx_hex: hex transaction with unsigned inputs

        Returns: signed hex transaction
    """
    inputs, outputs, locktime, version = deserialize_transaction(unsigned_tx_hex)
    tx_hex = unsigned_tx_hex
    for index in xrange(0, len(inputs)):
        if len(inputs[index]['script_sig']) == 0:

            # tx with index i signed with privkey
            tx_hex = sign_transaction(str(unsigned_tx_hex), index, hex_privkey)
            unsigned_tx_hex = tx_hex

    return tx_hex
Esempio n. 6
0
def sign_all_unsigned_inputs(hex_privkey, unsigned_tx_hex):
    """
        Sign a serialized transaction's unsigned inputs

        @hex_privkey: private key that should sign inputs
        @unsigned_tx_hex: hex transaction with unsigned inputs

        Returns: signed hex transaction
    """
    inputs, outputs, locktime, version = deserialize_transaction(unsigned_tx_hex)

    for index in xrange(0, len(inputs)):
        if len(inputs[index]['script_sig']) == 0:

            # tx with index i signed with privkey
            tx_hex = sign_transaction(str(unsigned_tx_hex), index, hex_privkey)
            unsigned_tx_hex = tx_hex

    return tx_hex
Esempio n. 7
0
def make_send_to_address_tx(recipient_address, amount, private_key,
        blockchain_client=BlockchainInfoClient(), fee=STANDARD_FEE,
        change_address=None):
    """ Builds and signs a "send to address" transaction.
    """
    # get out the private key object, sending address, and inputs
    private_key_obj, from_address, inputs = analyze_private_key(private_key,
        blockchain_client)
    # get the change address
    if not change_address:
        change_address = from_address
    # create the outputs
    outputs = make_pay_to_address_outputs(recipient_address, amount, inputs,
                                          change_address, fee=fee)
    # serialize the transaction
    unsigned_tx = serialize_transaction(inputs, outputs)
    
    # generate a scriptSig for each input
    for i in xrange(0, len(inputs)):
        signed_tx = sign_transaction( unsigned_tx, i, private_key_obj.to_hex() )
        unsigned_tx = signed_tx

    # return the signed tx
    return signed_tx
Esempio n. 8
0
def make_op_return_tx(data, private_key,
        blockchain_client=BlockchainInfoClient(), fee=OP_RETURN_FEE,
        change_address=None, format='bin'):
    """ Builds and signs an OP_RETURN transaction.
    """
    # get out the private key object, sending address, and inputs
    private_key_obj, from_address, inputs = analyze_private_key(private_key,
        blockchain_client)
    # get the change address
    if not change_address:
        change_address = from_address
    # create the outputs
    outputs = make_op_return_outputs(data, inputs, change_address,
        fee=fee, format=format)
    # serialize the transaction
    unsigned_tx = serialize_transaction(inputs, outputs)
    
    # generate a scriptSig for each input
    for i in xrange(0, len(inputs)):
        signed_tx = sign_transaction( unsigned_tx, i, private_key_obj.to_hex() )
        unsigned_tx = signed_tx
    
    # return the signed tx
    return signed_tx