taproot – Create schnorr signature with multi-inputs in golang

0
65


I wish to create a transaction with 2 inputs and a couple of outputs.

When i ship signed transaction to rpc node. I acquired error non-mandatory-script-verify-flag (Invalid Schnorr signature).But when a transaction with 1 enter and a couple of outputs, it labored nicely.

    privKey := "xxx"
    wif, err := btcutil.DecodeWIF(privKey)

    spendAddrStr := "xxx"
    destAddrStr := "xxx"
    chain := NetParams

    spendAddr, err := btcutil.DecodeAddress(spendAddrStr, chain)
    destAddr, err := btcutil.DecodeAddress(destAddrStr, chain)

    spenderAddrByte, err := txscript.PayToAddrScript(spendAddr)
    destAddrByte, err := txscript.PayToAddrScript(destAddr)
    redeemTx := wire.NewMsgTx(2)

    utxoHash0, err := chainhash.NewHashFromStr("79495bb38f0f4c6faca219907fae4f1d30a4c717f6c1d45877f3516cfbce3cfb")
    outPoint0 := wire.NewOutPoint(utxoHash0, 0)
    txIn0 := wire.NewTxIn(outPoint0, nil, [][]byte{})
    txIn0.Sequence = wire.MaxTxInSequenceNum - 10
    redeemTx.AddTxIn(txIn0)
    utxoHash1, err := chainhash.NewHashFromStr("1109179135781611cd57e0b87cb59d5ff55d5cfe9780c326c9c3344b9b64d96d")
    outPoint1 := wire.NewOutPoint(utxoHash1, 0)
    txIn1 := wire.NewTxIn(outPoint1, nil, [][]byte{})
    txIn1.Sequence = wire.MaxTxInSequenceNum - 10
    redeemTx.AddTxIn(txIn1)

    redeemTxOut0 := wire.NewTxOut(200, destAddrByte)
    redeemTxOut1 := wire.NewTxOut(5346-233, spenderAddrByte)

    redeemTx.AddTxOut(redeemTxOut0)
    redeemTx.AddTxOut(redeemTxOut1)
    redeemTx.LockTime = 0

    if err != nil {
        log.Println("DecodeString pkScript err", err)
        return
    }

    fetcher := txscript.NewMultiPrevOutFetcher(map[wire.OutPoint]*wire.TxOut{
        *outPoint0: {
            PkScript: spenderAddrByte,
            Worth:    202,
        },
        *outPoint1: {
            PkScript: spenderAddrByte,
            Worth:    5346,
        },
    })

    witnessList := make([]wire.TxWitness, len(redeemTx.TxIn))
    for i := vary redeemTx.TxIn {
        txOut := fetcher.FetchPrevOutput(redeemTx.TxIn[i].PreviousOutPoint)
        witness, err := txscript.TaprootWitnessSignature(redeemTx, txscript.NewTxSigHashes(redeemTx, fetcher),
            i, txOut.Worth, txOut.PkScript, txscript.SigHashDefault, wif.PrivKey)
        if err != nil {
            fmt.Println(err)
        }
        witnessList[i] = witness
        //redeemTx.TxIn[i].Witness = witness
    }
    for i := vary witnessList {
        redeemTx.TxIn[i].Witness = witnessList[1]
    }

    hash, err := shopper.SendRawTransaction(redeemTx, true)
    if err != nil {
        panic(err)
    }
    log.Println(hash.String())

I suppose the issue is txscript.TaprootWitnessSignature, setting inputs and outputs of a transaction seems no drawback.

I do not know which step is incorrect.I do not know to debug this.

LEAVE A REPLY

Please enter your comment!
Please enter your name here