Tuesday, November 5, 2024

checklocktimeverify – Non-witness P2SH spending utilizing C++ libbitcoin

I’m making an attempt to spend an UTXO (non-witness P2SH) utilizing libbitcoin. I’ve take a look at libbitcoin-system wiki documentation (https://github.com/libbitcoin/libbitcoin-system/wiki) however have not discovered an answer for my drawback. Sadly, the web page solely gives information to nested-segwit P2SH spending.

The redeem script is the next =>

<expiry time> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

Under is the C++ code I wrote to spend =>

    int major() {
        pockets::ec_private privateKey1("secret for apparent motive");
        pockets::ec_public publicKey1 = privateKey1.to_public();
        data_chunk pubKey1 = to_chunk(publicKey1.level());

        short_hash KeyHash1 = bitcoin_short_hash(publicKey1.level());
        data_chunk locktime;
        extend_data(locktime, to_little_endian<uint32_t>(1713352486));

        script redeem_script = script({operation(locktime), operation(opcode(177)), operation(opcode(117)), operation(opcode(118)), operation(opcode(169)), operation(to_chunk(KeyHash1)), operation(opcode(136)), operation(opcode(172))});

        pockets::payment_address from_address1("p2sh-cltv-address");
        pockets::payment_address to_address1("p2pkh-legacy-address");

        hash_digest prev_txid1;
        decode_hash(prev_txid1, "prev-txid-in-hexadecimal-format");

        uint32_t input_index1 = 0;
        output_point vin1(prev_txid1, input_index1);

        //make Enter
        enter input1 = enter();
        input1.set_previous_output(vin1);
        input1.set_sequence(0xfffffffe);

        uint64_t output_value1;
        decode_base10(output_value1, "0.00999770", 8);
        script output_script1 = script().to_pay_key_hash_pattern(to_address1.hash());
        output output1(output_value1, output_script1);
    
        // Get present time as a time_point
        auto now = std::chrono::system_clock::now();
        auto epoch = std::chrono::system_clock::to_time_t(now);
        auto unix_time = static_cast<uint32_t>(epoch);

        chain::transaction tx = chain::transaction();
        tx.set_version(2);
        tx.set_locktime(unix_time - 100000);
        tx.inputs().push_back(input1);
        tx.outputs().push_back(output1); 

        endorsement sig1; 
        if (script::create_endorsement(sig1, privateKey1.secret(), redeem_script, tx, 0u, sighash_algorithm::all, script_version::zero, 1000000))
        {
            std::cout << "Signature: " << std::endl;
            std::cout << encode_base16(sig1) << "n" << std::endl; 
        }

        operation::record scriptSig1; 
        scriptSig1.push_back(operation(sig1));
        scriptSig1.push_back(operation(pubKey1));
        scriptSig1.push_back(operation(redeem_script.to_data(0)));
        script unlockingScript1(scriptSig1);
        std::cout << unlockingScript1.to_string(0) << "n" << std:: endl;

        //Make Signed TX
        tx.inputs()[0].set_script(unlockingScript1);
        std::cout << "Uncooked Transaction: " << std::endl;
        std::cout << encode_base16(tx.to_data(true, true)) << std::endl;
        return 0;
    }

The printed of the created raw-transaction returned an error (Signature have to be zero for failed CHECK(MULTI)SIG operation), which signifies that one thing went improper with the signature half. I checked each (not less than I feel so) particulars, from scripts to values however I can not inform precisely what’s improper. Can any individual assist?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles