banner

I create a taproot multisig script,and send some satoshi to the script address.
then,withdaw satoshi from the script with 2 signatures,but got error:{“code”:-26,”message”:”mandatory-script-verify-flag-failed (Operation not valid with the current stack size)”}
but if with 3 signatures,it’s ok.tx:https://mempool.space/testnet/tx/c1b7a044776beb21856c91f7d3d688a91011f4321c6348c8c981c26082074341

script:key1 OP_CHECKSIG key2 OP_CHECKSIGADD key3 OP_CHECKSIGADD OP_2 OP_GREATERTHANOREQUAL
my js code:

const leafKeysWif = [
'---',
'---',
'---',
]
const leafKeys = []
const leafPubkeys = []
for (const leafKeyWif of leafKeysWif) {
const leafKey = ECPair.fromWIF(leafKeyWif, network)
leafKeys.push(leafKey)
leafPubkeys.push(toXOnly(leafKey.publicKey))
}

leafPubkeys.sort((a, b) => a.compare(b))
leafPubkeys.forEach((pubkey) => {
console.log(pubkey.toString(‘hex’))
})

const ops = []
leafPubkeys.forEach((pubkey) => {
if (ops.length === 0) {
ops.push(pubkey)
ops.push(bitcoin.opcodes.OP_CHECKSIG)
} else {
ops.push(pubkey)
ops.push(bitcoin.opcodes.OP_CHECKSIGADD)
}
})
ops.push(bitcoin.opcodes.OP_2)
ops.push(bitcoin.opcodes.OP_GREATERTHANOREQUAL)
const leafScript = bitcoin.script.compile(ops)

const internalPubkey = makeUnspendableInternalKey(undefined)
const payment = bitcoin.payments.p2tr({
internalPubkey: internalPubkey,
scriptTree: {
output: leafScript,
},
redeem: {
output: leafScript,
redeemVersion: LEAF_VERSION_TAPSCRIPT, // 0xc0
},
network: network,
})

console.log(payment.address) //tb1pp33rqchke8lh3425uunvtuq0kcrslupwm7kxf9rcap6s05k3ukhqmcplhw
console.log(payment.output.toString(‘hex’))// 51200c623062f6c9ff78d554e726c5f00fb6070ff02edfac649478e87507d2d1e5ae

// controlBlock
const witness = payment.witness
const controlBlock = witness[witness.length – 1]
console.log(‘controlBlock’, controlBlock.toString(‘hex’))// c050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0

const psbt = new bitcoin.Psbt({ network: network })
const utxoHash=”83c307875653061a6574f8956c86efe69a86f7a671dfea3078b2bb434fcfb84b”
const utxoVout = 0
const utxoAmount = 100000
const sendAmount = utxoAmount – 80000
psbt.addOutput({ value: sendAmount, address: ‘tb1ph8jzyv68rwx436wtnr760tsg3ut07ml8zueuqcwww04fdmrp8ntqaqjmsp’ })
psbt.addInput({
hash: utxoHash,
index: utxoVout,
witnessUtxo: { value: utxoAmount, script: payment.output },
})

psbt.updateInput(0, {
tapLeafScript: [
{
leafVersion: LEAF_VERSION_TAPSCRIPT,
script: leafScript,
controlBlock: controlBlock,
},
],
})

console.log(psbt.toHex())

psbt.signInput(0, leafKeys[0])
psbt.signInput(0, leafKeys[1])
// psbt.signInput(0, leafKeys[2]) // if add the third signature,tx is ok
psbt.finalizeAllInputs()
console.log(psbt.toHex())
const tx = psbt.extractTransaction()
const rawTx = tx.toBuffer()
const hex = rawTx.toString(‘hex’)
console.log(hex)

const broadcast = async () => {
const baseURLs=”https://mempool.space:443/testnet/api/”
const cli = new BitcoinClient(baseURLs, 30000)
const rsp = await cli.postTransaction(hex)
console.log(rsp)
}

broadcast()

banner

Converter

Source: CurrencyRate
Top Selling Multipurpose WP Theme

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

banner

Leave a Comment

Layer 1
Your Crypto & Blockchain Beacon

CryptoInsightful

Welcome to CryptoInsightful.com, your trusted source for in-depth analysis, news, and insights into the world of cryptocurrencies, blockchain technology, NFTs (Non-Fungible Tokens), and cybersecurity. Our mission is to empower you with the knowledge and understanding you need to navigate the rapidly evolving landscape of digital assets and emerging technologies.