Bitcoin Forum
May 07, 2024, 05:30:21 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: PSBT Transaction RPC Error: bad-txns-inputs-missingorspent  (Read 91 times)
MarkTangCd (OP)
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile
February 07, 2024, 02:35:46 AM
 #1

I want to create a PSBT transaction. But get the error when to call sendrawtransaction method.

I used Golang package: github.com/btcsuite/btcd

This is my code:

PSBT inputs
Code:
inputs = append(inputs, &TxInput{
TxId:       "02b074590fcac25129ff8f6a31375d9231e86f600f82c232d5d61fe2df5a8a96",
VOut:       uint32(0),
Sequence:   uint32(0),
Amount:     2774,
Address:    "tb1pnq36mn7l8d3kvgejvnc5q63ls9umhaqscl623eqzge8mstj332xscmeu8h",
PublicKey:  "02a33b6f980567e1b3bb5a4df3b086871694d0e03b70ee462a36686acda6b94b9d",
PrivateKey: "privatekey",
})

inputs = append(inputs, &TxInput{
TxId:       "2e9f9af0bbed192bb860a172b2ab219c48da87996c7e80bf8470033b669f354e",
VOut:       uint32(len(inputs)),
Sequence:   uint32(len(inputs)),
Amount:     1990,
Address:    config.Receiver,
PublicKey:  "02e8e9d1d88eb86fb56b26e267bda715ba81ad91850448d8c2d5d8e64937ac8ef9",
PrivateKey: "privatekey",
})
PSBT outputs
Code:
func GetTxOutByUTXOs(amount int64) []*TxOutput {
var outputs []*TxOutput
outputs = append(outputs, &TxOutput{
Address: config.Owner,
Amount:  amount - 500,
})
outputs = append(outputs, &TxOutput{
Address: config.Receiver,
Amount:  500 + 1990 - 300,
})

return outputs
}

Then I passed them to the function.
Code:
func GenerateSignedOwnerTx(ins []*TxInput, outs []*TxOutput, network *chaincfg.Params) (string, error) {
var inputs []*wire.OutPoint
var nSequences []uint32
prevOuts := make(map[wire.OutPoint]*wire.TxOut)
for _, in := range ins {
    var prevOut *wire.OutPoint
    txHash, err := chainhash.NewHashFromStr(in.TxId)
    if err != nil {
        panic(err)
    }
    prevOut = wire.NewOutPoint(txHash, in.VOut)
    inputs = append(inputs, prevOut)

    prevPkScript, err := AddrToPkScript(in.Address, network)
    if err != nil {
        panic(err)
    }
    witnessUtxo := wire.NewTxOut(in.Amount, prevPkScript)
    prevOuts[*prevOut] = witnessUtxo

    nSequences = append(nSequences, wire.MaxTxInSequenceNum)
}

var outputs []*wire.TxOut
for _, out := range outs {
    pkScript, err := AddrToPkScript(out.Address, network)
    if err != nil {
        panic(err)
    }
    outputs = append(outputs, wire.NewTxOut(out.Amount, pkScript))
}

bp, err := psbt.New(inputs, outputs, int32(2), uint32(0), nSequences)
if err != nil {
    panic(err)
}

updater, err := psbt.NewUpdater(bp)
if err != nil {
    panic(err)
}

prevOutputFetcher := txscript.NewMultiPrevOutFetcher(prevOuts)

for i, in := range ins {

    if err = signInput(updater, i, in, prevOutputFetcher, txscript.SigHashAll, network); err != nil {
        panic(err)
    }

    if err = psbt.Finalize(bp, i); err != nil {
        panic(err)
    }
}

fmt.Println("signed bp base64 encode:")
fmt.Println(bp.B64Encode())
if err = psbt.MaybeFinalizeAll(bp); err != nil {
    return "", err
}

buyerSignedTx, err := psbt.Extract(bp)
if err != nil {
    return "", err
}

var buf bytes.Buffer
if err := buyerSignedTx.Serialize(&buf); err != nil {
    return "", err
}

return hex.EncodeToString(buf.Bytes()), nil}

This is sign function.
Code:
func signInput(updater *psbt.Updater, i int, in *TxInput, prevOutFetcher *txscript.MultiPrevOutFetcher, hashType txscript.SigHashType, network *chaincfg.Params) error {
wif, err := btcutil.DecodeWIF(i[Suspicious link removed]ivateKey)
if err != nil && i != 1 {
    panic(err)
}
privKey := wif.PrivKey

prevPkScript, err := AddrToPkScript(in.Address, network)
if err != nil {
    panic(err)
}
if txscript.IsPayToPubKeyHash(prevPkScript) {
    prevTx := wire.NewMsgTx(2)
    txBytes, err := hex.DecodeString(in.NonWitnessUtxo)
    if err != nil {
        panic(err)
    }
    if err = prevTx.Deserialize(bytes.NewReader(txBytes)); err != nil {
        panic(err)
    }
    if err = updater.AddInNonWitnessUtxo(prevTx, i); err != nil {
        panic(err)
    }
} else {
    witnessUtxo := wire.NewTxOut(in.Amount, prevPkScript)
    if err = updater.AddInWitnessUtxo(witnessUtxo, i); err != nil {
        panic(err)
    }
}

if err = updater.AddInSighashType(hashType, i); err != nil {
    panic(err)
}

if txscript.IsPayToTaproot(prevPkScript) {
    internalPubKey := schnorr.SerializePubKey(privKey.PubKey())
    updater.Upsbt.Inputs[i].TaprootInternalKey = internalPubKey

    sigHashes := txscript.NewTxSigHashes(updater.Upsbt.UnsignedTx, prevOutFetcher)
    if hashType == txscript.SigHashAll {
        hashType = txscript.SigHashDefault
    }
    witness, err := txscript.TaprootWitnessSignature(updater.Upsbt.UnsignedTx, sigHashes,
        i, in.Amount, prevPkScript, hashType, privKey)
    if err != nil {
        panic(err)
    }

    updater.Upsbt.Inputs[i].TaprootKeySpendSig = witness[0]
} else if txscript.IsPayToPubKeyHash(prevPkScript) {
    signature, err := txscript.RawTxInSignature(updater.Upsbt.UnsignedTx, i, prevPkScript, hashType, privKey)
    if err != nil {
        panic(err)
    }

    if _, err := updater.Sign(i, signature, privKey.PubKey().SerializeCompressed(), nil, nil); err != nil {
        panic(err)
    }
} else {
    pubKeyBytes := privKey.PubKey().SerializeCompressed()
    sigHashes := txscript.NewTxSigHashes(updater.Upsbt.UnsignedTx, prevOutFetcher)

    script, err := PayToPubKeyHashScript(btcutil.Hash160(pubKeyBytes))
    if err != nil {
        panic(err)
    }
    signature, err := txscript.RawTxInWitnessSignature(updater.Upsbt.UnsignedTx, sigHashes, i, in.Amount, script, hashType, privKey)
    if err != nil {
        panic(err)
    }

    if txscript.IsPayToScriptHash(prevPkScript) {
        redeemScript, err := PayToWitnessPubKeyHashScript(btcutil.Hash160(pubKeyBytes))
        if err != nil {
            panic(err)
        }
        err = updater.AddInRedeemScript(redeemScript, i)
        if err != nil {
            panic(err)
        }
    }

    if _, err := updater.Sign(i, signature, pubKeyBytes, nil, nil); err != nil {
        panic(err)
    }
}
return nil}
1715059821
Hero Member
*
Offline Offline

Posts: 1715059821

View Profile Personal Message (Offline)

Ignore
1715059821
Reply with quote  #2

1715059821
Report to moderator
1715059821
Hero Member
*
Offline Offline

Posts: 1715059821

View Profile Personal Message (Offline)

Ignore
1715059821
Reply with quote  #2

1715059821
Report to moderator
1715059821
Hero Member
*
Offline Offline

Posts: 1715059821

View Profile Personal Message (Offline)

Ignore
1715059821
Reply with quote  #2

1715059821
Report to moderator
"This isn't the kind of software where we can leave so many unresolved bugs that we need a tracker for them." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715059821
Hero Member
*
Offline Offline

Posts: 1715059821

View Profile Personal Message (Offline)

Ignore
1715059821
Reply with quote  #2

1715059821
Report to moderator
1715059821
Hero Member
*
Offline Offline

Posts: 1715059821

View Profile Personal Message (Offline)

Ignore
1715059821
Reply with quote  #2

1715059821
Report to moderator
1715059821
Hero Member
*
Offline Offline

Posts: 1715059821

View Profile Personal Message (Offline)

Ignore
1715059821
Reply with quote  #2

1715059821
Report to moderator
nc50lc
Legendary
*
Online Online

Activity: 2408
Merit: 5588


Self-proclaimed Genius


View Profile
February 07, 2024, 08:10:07 AM
 #2

I want to create a PSBT transaction. But get the error when to call sendrawtransaction method.
The error indicates that when sending your transaction to your node's mempool, it's rejected for referencing already spent output(s).
The TXIDs seem to be existing in the TestNet network.
Check your inputs' output_index (vout) if it's pointing to the correct UTXO.

You can also share the signed raw testnet transaction so others and I can help find the issue (and other issues) in the finalized transaction.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
MarkTangCd (OP)
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile
February 08, 2024, 02:52:45 PM
 #3

I want to create a PSBT transaction. But get the error when to call sendrawtransaction method.
The error indicates that when sending your transaction to your node's mempool, it's rejected for referencing already spent output(s).
The TXIDs seem to be existing in the TestNet network.
Check your inputs' output_index (vout) if it's pointing to the correct UTXO.

You can also share the signed raw testnet transaction so others and I can help find the issue (and other issues) in the finalized transaction.
Thanks. The vout isn't correct. I have updated them and sent the transaction again. but I get a new error: non-mandatory-script-verify-flag (Invalid Schnorr signature).
I don't know what does the error mean.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6595


Just writing some code


View Profile WWW
February 08, 2024, 05:54:49 PM
Merited by nc50lc (1)
 #4

but I get a new error: non-mandatory-script-verify-flag (Invalid Schnorr signature).
I don't know what does the error mean.
It means your signature is invalid.

Without seeing the actual transaction, it's hard to actually provide specific help.

MarkTangCd (OP)
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile
February 10, 2024, 02:56:20 AM
 #5

but I get a new error: non-mandatory-script-verify-flag (Invalid Schnorr signature).
I don't know what does the error mean.
It means your signature is invalid.

Without seeing the actual transaction, it's hard to actually provide specific help.
I called "decoderawtransaction" method and this is result. thank for your help.

Code:
{
  "txid": "c28698694306c9043da3714cf98e3118ee66afc38fca90647916e70b14d0e7d6",
  "hash": "aef5ac68dc08a97e61f8ec2c38431ff2216d3808261f6a381e91fc39cee513cb",
  "version": 2,
  "size": 342,
  "vsize": 210,
  "weight": 840,
  "locktime": 0,
  "vin": [
    {
      "txid": "02b074590fcac25129ff8f6a31375d9231e86f600f82c232d5d61fe2df5a8a96",
      "vout": 1,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "5c20a35b46a9e9fc9d64cd67dc26b87365b5625bab3e56784cde0f3a78c4e44cb728695a6585aa3b74a9e5b2cc42a4f8ae791ce349cf90e411a3ac4e8940c6e601"
      ],
      "sequence": 4294967295
    },
    {
      "txid": "2e9f9af0bbed192bb860a172b2ab219c48da87996c7e80bf8470033b669f354e",
      "vout": 3,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "3044022001c05dba13ecb12e285e31bea43d40007c8d2073f96fa6caadd37a1cc743036802206372fc5cde96af4d551a671429b2821b087e33e8c65e2ad482e266d1d753ecf701",
        "02e8e9d1d88eb86fb56b26e267bda715ba81ad91850448d8c2d5d8e64937ac8ef9"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.00002274,
      "n": 0,
      "scriptPubKey": {
        "asm": "1 9823adcfdf3b6366233264f1406a3f8179bbf410c7f4a8e402464fb82e518a8d",
        "desc": "rawtr(9823adcfdf3b6366233264f1406a3f8179bbf410c7f4a8e402464fb82e518a8d)#yd9m3sas",
        "hex": "51209823adcfdf3b6366233264f1406a3f8179bbf410c7f4a8e402464fb82e518a8d",
        "address": "tb1pnq36mn7l8d3kvgejvnc5q63ls9umhaqscl623eqzge8mstj332xscmeu8h",
        "type": "witness_v1_taproot"
      }
    },
    {
      "value": 0.00002190,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 caec246e47c75782cdb908725d38870bb4fb365d",
        "desc": "addr(tb1qetkzgmj8catc9ndeppe96wy8pw60kdjautr8fp)#tjhdn4lj",
        "hex": "0014caec246e47c75782cdb908725d38870bb4fb365d",
        "address": "tb1qetkzgmj8catc9ndeppe96wy8pw60kdjautr8fp",
        "type": "witness_v0_keyhash"
      }
    }
  ]
}
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!