Tuesday, November 5, 2024

script – Assist! About find out how 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 ought to be a 3-2 threshold account, that require 2 signature. Nevertheless, it really required 3 signature, if we solely signal it with 2 keypair, it is going to happen:

Operation not legitimate with the present stack measurement

So how do I in a position to spend this taproot handle, with solely 2 legitimate keypair?

Right here is the code used to formulate the handle 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.handle!}`)

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

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

let psbt_origin: string = build_psbt(redeem, [utxo], p2tr.handle, "bcrt1q5hk8re6mar775fxnwwfwse4ql9vtpn6x558g0w", "regtest", p2tr, amt, price)
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")

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles