btcsuite / btcwallet

A secure bitcoin wallet daemon written in Go (golang)
ISC License
1.14k stars 588 forks source link

-25: TX rejected: failed to validate input 11290e0....:0 which references output cf....a015ea68, prev output script bytes 76a91....8ac) #863

Closed lenhatquang97 closed 1 year ago

lenhatquang97 commented 1 year ago

Hello everyone, I am trying to create a transaction using btcwallet.

Situation

I am using simnet based on https://gist.github.com/davecgh/2992ed85d41307e794f6. My transactions have two inputs and one outputs. I have even signed the transaction but I failed to send the transaction. I don't know what is the main root cause for this transaction?

Problem

-25: TX rejected: failed to validate input 11290e0b3fb37c00c72ffc0930035db395b468a6246045bdb8283135c72c82df:0 which references output cf908186b974a9ad818ac47dc77633ec57f1bfdbf20537db3cb09335c16c9c25:0 - OP_EQUALVERIFY failed (input witness [], input script bytes 483045022100aba979ee4183b9d45b97f4f9604b22e8f8b75e7bcbbebd33d337ebe18b1b8f7402201b994861db4e09628898cb830a06556b96b8ece607ee8754b70305ca283fe020014104bbcea8ef034bd4df764f6339248091e454268446256d851aed3c50f0cf8dd74cd7e1ea97dd5f3d750e09d4dce3798744d8466c8f976dae0e77a0350ba015ea68, prev output script bytes 76a91488fc282bfeebc8361ef36e2372f94922e3fd427088ac)

Source code

In the source code below: client means btcwallet rpcclient and I have connected to this.

SignInputTx, SignOutputTx

func SignInputTx(redeemTx *wire.MsgTx, inputAddress btcutil.Address, privKey *secp256k1.PrivateKey) []byte {
    subscript, _ := txscript.PayToAddrScript(inputAddress)
    sig, err := txscript.SignatureScript(redeemTx, 0, subscript, txscript.SigHashAll, privKey, false)
    if err != nil {
        log.Fatalf("could not generate signature: %v", err)
    }
    return sig
}
func SignOutputTx(redeemTx *wire.MsgTx, destination btcutil.Address) []byte {
    script, err := txscript.PayToAddrScript(destination)
    if err != nil {
        log.Fatal(err)
    }
    return script
}

Logic for sending raw transactions

err := client.WalletPassphrase("12345", 1)
if err != nil {
        return err
}

sourceAddress, err := client.GetAccountAddress("default")
if err != nil {
    return err
}

privKey, err := client.DumpPrivKey(sourceAddress)
if err != nil {
    return err
}

unsignedCommitTx.TxIn[0].SignatureScript = SignInputTx(unsignedCommitTx, sourceAddress, privKey.PrivKey)
unsignedCommitTx.TxOut[0].PkScript = SignOutputTx(unsignedCommitTx, inscribe.Destination)

signRawCommitTx, _, err := client.SignRawTransaction(unsignedCommitTx)
if err != nil {
    return err
}

commit, err := client.SendRawTransaction(signRawCommitTx, false)
if err != nil {
    return err
}
fmt.Println(commit)
guggero commented 1 year ago

What's the output pk script for cf908186b974a9ad818ac47dc77633ec57f1bfdbf20537db3cb09335c16c9c25:0? And what does the unsignedCommitTx look like? Your example code is missing the relevant parts

chappjc commented 1 year ago

Are you also sure you want uncompressed for your pubkey used to make the sigscript?

lenhatquang97 commented 1 year ago

@guggero output pk script is 76a91488fc282bfeebc8361ef36e2372f94922e3fd427088ac because it's only one output.

And in unsignedCommitTx, you can see the details in https://github.com/lenhatquang97/bitcoin_nft in branch Quang, with file named inscribe.go with this link: https://github.com/lenhatquang97/bitcoin_nft/blob/Quang/src/nft/inscribe.go

lenhatquang97 commented 1 year ago

@chappjc So to make the sigscript, compressed pubkey should be used?

chappjc commented 1 year ago

@chappjc So to make the sigscript, compressed pubkey should be used?

The decision was made when the address was generated. The question is, why do you say false here https://github.com/lenhatquang97/bitcoin_nft/blob/433d39b9fcb5e41d8838fb455454e8cc6f99ef9e/src/nft/inscribe.go#LL57C36-L57C36

guggero commented 1 year ago

That pk script is a legacy script. But you're trying to spend it with a Tapscript witness? It looks to me like you're mixing different concepts. At least I couldn't make heads or tails of the code you linked...

lenhatquang97 commented 1 year ago

I close this issue, because I have reproduced this bug to a simpler example https://github.com/btcsuite/btcwallet/issues/865