btcsuite / btcd

An alternative full node bitcoin implementation written in Go (golang)
https://github.com/btcsuite/btcd/blob/master/README.md
ISC License
6.1k stars 2.31k forks source link

Cannot broadcast segwit transaction with two outputs #2102

Closed liorwavebl closed 5 months ago

liorwavebl commented 5 months ago

I'm trying to broadcast segwit transactions with two outputs. When I'm trying to broadcast it, I'm receiving this error: non-mandatory-script-verify-flag (Script failed an OP_EQUALVERIFY operation)

The signing is done via this code snippet:

redeemTx := wire.NewMsgTx(wire.TxVersion)
prevOut := wire.NewOutPoint(utxoHash, 0)
redeemTxIn := wire.NewTxIn(prevOut, nil, nil)
redeemTx.AddTxIn(redeemTxIn)
redeemTXOut0 := wire.NewTxOut(outputs[0].amount, destPkScript0)
redeemTx.AddTxOut(redeemTXOut0)
redeemTXOut1 := wire.NewTxOut(outputs[1].amount, destPkScript1)
redeemTx.AddTxOut(redeemTXOut1)

a := txscript.NewMultiPrevOutFetcher(map[wire.OutPoint]*wire.TxOut{
*utxo: {},
})
sigHashes := txscript.NewTxSigHashes(redeemTx, a)
totalAmount := outputs[0].amount + outputs[1].amount
sigScript, err := txscript.WitnessSignature(redeemTx, sigHashes, 0, totalAmount, destPkScript1, txscript.SigHashAll, wif.PrivKey, true)