Tuesday, October 1, 2024

script – Assist! About the best way to spend OP_checksigadd Multi-sig

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

<pk1> OP_CHECKSIG <pk2> OP_CHECKSIGADD <pk3> OP_CHECKSIGADD OP_2 OP_EQUAL

It must 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 Deal with: ${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, payment)
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:

Primarily based on the reply under, I discover a method to spending the taproot tackle, which is by pushing a brand new tapscripsig into the psbt.knowledge.inputs.

let psbt = Psbt.fromBase64(psbt_)

for (let i = 0; i < psbt.knowledge.inputs.size; i++) {
    for (let j = threshold; j < sign_num; j++) {
        psbt.knowledge.inputs[i].tapScriptSig?.push({
            leafHash: psbt.knowledge.inputs[i].tapScriptSig![0].leafHash,
            // pks right here should be the proper 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

Latest Articles