Friday, November 22, 2024
HomeBitcoinscript - Assist! About learn how to spend OP_checksigadd Multi-sig

script – Assist! About learn how to spend OP_checksigadd Multi-sig


I write a easy taproot-based script in bitcoinjs, like:

OP_CHECKSIG OP_CHECKSIGADD OP_CHECKSIGADD OP_2 OP_EQUAL

It needs to be a 3-2 threshold account, that require 2 signature. Nonetheless, it really required 3 signature, if we solely signal it with 2 keypair, it can happen:

Operation not legitimate with the present stack dimension

So how do I capable of spend this taproot tackle, with solely 2 legitimate keypair?

Right here is the code used to formulate the tackle and spend:

const leafKeys = [];
const leafPubkeys = [];
const leafKeys_WIF = [];

for (let i = 0; i < num; i++) {
    const leafKey = ECPair.makeRandom({ community });
    leafKeys.push(leafKey);
    leafPubkeys.push(toXOnly(leafKey.publicKey).toString('hex'));
    leafKeys_WIF.push(leafKey.toWIF())
}

// It returns the script that I wrote earlier than
const leafScript = asm_builder(leafPubkeys, threshold);

const scriptTree: Taptree =
{
    output: leafScript,
};

const redeem = {
    output: leafScript,
    redeemVersion: LEAF_VERSION_TAPSCRIPT,
};

const p2tr = bitcoin.funds.p2tr({
    internalPubkey: toXOnly(keypair.publicKey),
    scriptTree,
    redeem,
    community: choose_network(`regtest`),
});

console.log(`Ready until UTXO is detected at this Handle: ${p2tr.tackle!}`)

let temp_trans = await pushTrans(p2tr.tackle!)
console.log("the brand new txid is:", temp_trans)

const utxo = await getUTXOfromTx(temp_trans, p2tr.tackle!)

let psbt_origin: string = build_psbt(redeem, [utxo], p2tr.tackle, "bcrt1q5hk8re6mar775fxnwwfwse4ql9vtpn6x558g0w", "regtest", p2tr, amt, charge)
let psbt_ = ''

for (var i = 0; i < threshold; i++) {
    psbt_ = sign_psbt(psbt_origin, leafKeys_WIF[i], "regtest")
    psbt_origin = psbt_
}

const txhex = await pay_psbt_hex(psbt_origin, num - threshold, "regtest")

Resolution:

Based mostly on the reply under, I discover a solution to spending the taproot tackle, which is by pushing a brand new tapscripsig into the psbt.information.inputs.

let psbt = Psbt.fromBase64(psbt_)

for (let i = 0; i < psbt.information.inputs.size; i++) {
    for (let j = threshold; j < sign_num; j++) {
        psbt.information.inputs[i].tapScriptSig?.push({
            leafHash: psbt.information.inputs[i].tapScriptSig![0].leafHash,
            // pks right here should be the right pk of the tapscript, precisely equal to the reedem.output
            pubkey: pks[j],
            signature: Buffer.from("")
        });
    }
}

psbt.finalizeAllInputs();

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments