Bitcoin

javascript – bitcoinjs – required-script-verify-flag-failed (signature must be 0 for failed CHECK(MULTI)SIG operation)

Enter image description here

I’m following the Magic Eden transaction example below and looking to expand it to support multiple buyers and sellers (in red). However, you run into difficulties when adding more than one signature from different parties (broadcasting is fine for one seller). The transaction is built and signed properly, but an error occurs when trying to broadcast it to the network. mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation). For this we will use the default Segwit address (no-tr).

What am I missing or doing wrong?

Using the bitcoinjs library here is what I have so far (red only).

Seller’s signature:

let psbt = new bitcoin.Psbt( network );

const (ordinalUtxoTxId, ordinalUtxoVout) = ordinalOutput.split(":");
const tx = bitcoin.Transaction.fromHex(await getTxHexById(ordinalUtxoTxId));

try 
    tx.setWitness(parseInt(ordinalUtxoVout), ());
 catch 

const input = 
  hash: ordinalUtxoTxId,
  index: parseInt(ordinalUtxoVout),
  nonWitnessUtxo: tx.toBuffer(),
  witnessUtxo: tx.outs(ordinalUtxoVout),
  sighashType:
    bitcoin.Transaction.SIGHASH_SINGLE ;

psbt.addInput(input);

psbt.addOutput(
  address: paymentAddress,
  value: price,
);

Buyer signature (ignoring padding input and output):

// Add inscriptions inputs and outputs
for (const signedPsbt of sellerSignedPsbts) 
    // Add inscription output
    psbt.addOutput(
        address: receiverAddress,
        value: signedPsbt.data.inputs(0).witnessUtxo.value,
    );

    // Add payer signed input
    psbt.addInput(
        ...signedPsbt.data.globalMap.unsignedTx.tx.ins(0),
        // ...signedPsbt.data.inputs(0),
        witnessUtxo: signedPsbt.data.inputs(0).witnessUtxo,
        nonWitnessUtxo: signedPsbt.data.inputs(0).nonWitnessUtxo,
        // finalScriptWitness: signedPsbt.data.inputs(0).finalScriptWitness,
    );

    input++;


// Add seller payment outputs for payee
for (const signedPsbt of sellerSignedPsbts) 
    // Add payer output
    psbt.addOutput(
        ...signedPsbt.data.globalMap.unsignedTx.tx.outs(0),
    );

Merge PSBT signed by seller and buyer.

const buyerSignedPsbt = bitcoin.Psbt.fromBase64(signedBuyingPSBTBase64);

for (let index = 0; index < signedListingsPSBTBase64.length; index++) 
    const sellerSignedPsbt = bitcoin.Psbt.fromBase64(signedListingsPSBTBase64(index));

    buyerSignedPsbt.data.globalMap.unsignedTx.tx.ins(BUYING_PSBT_SELLER_SIGNATURE_INDEX + index) = sellerSignedPsbt.data.globalMap.unsignedTx.tx.ins(0);
    buyerSignedPsbt.data.inputs(BUYING_PSBT_SELLER_SIGNATURE_INDEX + index) = sellerSignedPsbt.data.inputs(0);


// broadcast buyerSignedPsbt

Related Articles

Back to top button