:: [Libbitcoin] How to fill in scriptS…
Top Page
Delete this message
Reply to this message
Author: Chang Lan
Date:  
To: libbitcoin
Subject: [Libbitcoin] How to fill in scriptSig
Hi there,

I tried making a valid transaction using libbitcoin, but it seems that the transaction turns out to be invalid. Below is the code snippet that I used to create a transaction. Can anyone point out if I did it wrong? Thanks!

void add_input(transaction_type& tx, const output_point& output)
{
    transaction_input_type input;
    output_point& prevout = input.previous_output;
    prevout = output;
    input.sequence = 4294967295;
    tx.inputs.push_back(input);
}




bool sign(transaction_type& tx, size_t input_index, const elliptic_curve_key& key, const script_type& script_code)
{
    transaction_input_type& input = tx.inputs[input_index];
    const data_chunk public_key = key.public_key();
    if (public_key.empty())
    {
        std::cerr << "internal error getting public key" << std::endl;
        return false;
    }
    hash_digest tx_hash = script_type::generate_signature_hash(tx, input_index, script_code, 1);
    if (tx_hash == null_hash)
    {
        std::cerr << "error generating signature hash" << std::endl;
        return false;
    }
    data_chunk signature = key.sign(tx_hash);
    signature.push_back(0x01);


    script_type input_script;
    input_script.push_operation({opcode::special, signature});
    std::cout << key.public_key().size() << std::endl;
    input_script.push_operation({opcode::special, key.public_key()});
    tx.inputs[input_index].script = input_script;


    return true;
}




bool create_channel_tx(transaction_type& tx, const data_chunk& payee_public_key, const data_chunk& payer_public_key, uint64_t value)
{
/* … */
    /* called add_input() for each input */


    // output 1
script_type out_script = build_contract_script(acc.key.public_key(), remote_public_key);
    transaction_output_type output;
    output.value = value;
    output.script = out_script;
    tx.outputs.push_back(output);


    // output 2
payment_address payaddr;
    set_public_key(payaddr, acc.key.public_key());
    script_type back_script = build_pubkey_hash_script(payaddr.hash());
    transaction_output_type back_output;
    back_output.value = collected_value - value;
    back_output.script = back_script;
    tx.outputs.push_back(back_output);


    // sign each input
    for (size_t i = 0; i < tx.inputs.size(); ++i)
    {
        sign(tx, i, acc.key, back_script);
    }
    return true;
}



Chang