• About
  • FAQ
  • Earn Bitcoin while Surfing the net
  • Buy & Sell Crypto on Paxful
Newsletter
Approx Foundation
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
Approx Foundation
No Result
View All Result
Home Bitcoin

bitcoin core – PSBT Transaction RPC Error: bad-txns-inputs-missingorspent

Moussa by Moussa
February 7, 2024
in Bitcoin
0
peer discovery – how to obtain the IP addresses of nodes for mining pools?
189
SHARES
1.5k
VIEWS
Share on FacebookShare on Twitter


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:
enter image description here
PSBT inputs

enter image description here
PSBT outputs

Then I passed them to the function.

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.

func signInput(updater *psbt.Updater, i int, in *TxInput, prevOutFetcher *txscript.MultiPrevOutFetcher, hashType txscript.SigHashType, network *chaincfg.Params) error {
wif, err := btcutil.DecodeWIF(in.PrivateKey)
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

}



Source link

Related articles

SuperTrend Flips Bullish On XRP Daily Chart — But Key $1.55 Resistance Awaits

SuperTrend Flips Bullish On XRP Daily Chart — But Key $1.55 Resistance Awaits

April 19, 2026
Bitcoin Rebounds, But Crypto’s Security Crisis Intensifies

Bitcoin Rebounds, But Crypto’s Security Crisis Intensifies

April 19, 2026
Share76Tweet47

Related Posts

SuperTrend Flips Bullish On XRP Daily Chart — But Key $1.55 Resistance Awaits

SuperTrend Flips Bullish On XRP Daily Chart — But Key $1.55 Resistance Awaits

by Moussa
April 19, 2026
0

The XRP market has recorded a major positive development, as the SuperTrend flashed its first buy signal on the daily...

Bitcoin Rebounds, But Crypto’s Security Crisis Intensifies

Bitcoin Rebounds, But Crypto’s Security Crisis Intensifies

by Moussa
April 19, 2026
0

This editorial is from last week’s edition of the newsletter Week in Review. Subscribe to the newsletter to get this...

How do Bitcoin mining pools typically handle payout frequency versus thresholds?

taproot – Was the OP_SUCCESSx reservation in BIP-342 designed with specific opcode families in mind, or as a generic forward-compatibility mechanism?

by Moussa
April 19, 2026
0

In Pieter Wuille's recent answer , BIP-342's deliberate minimization of semantic changes was attributed to the expectation that "those could...

Ethereum Foundation Veteran Josh Stark Is Stepping Down

by Moussa
April 19, 2026
0

Josh Stark, one of the Ethereum Foundation most senior executives, is stepping down after a five-year tenure, a departure that...

Iran Ceasefire Drives Bitcoin Above $75,000, But Can It Push It To $100,000?

Iran Ceasefire Drives Bitcoin Above $75,000, But Can It Push It To $100,000?

by Moussa
April 19, 2026
0

Trusted Editorial content, reviewed by leading industry experts and seasoned editors. Ad Disclosure Bitcoin has climbed back above $75,000 as...

Load More

youssufi.com

sephina.com

[vc_row full_width="stretch_row" parallax="content-moving" vc_row_background="" background_repeat="no-repeat" background_position="center center" footer_scheme="dark" css=".vc_custom_1517813231908{padding-top: 60px !important;padding-bottom: 30px !important;background-color: #191818 !important;background-position: center;background-repeat: no-repeat !important;background-size: cover !important;}" footer_widget_title_color="#fcbf46" footer_button_bg="#fcb11e"][vc_column width="1/4"]

We bring you the latest in Crypto News

[/vc_column][vc_column width="1/4"][vc_wp_categories]
[/vc_column][vc_column width="1/4"][vc_wp_tagcloud taxonomy="post_tag"][/vc_column][vc_column width="1/4"]

Newsletter

[vc_raw_html]JTNDcCUzRSUzQ2RpdiUyMGNsYXNzJTNEJTIydG5wJTIwdG5wLXN1YnNjcmlwdGlvbiUyMiUzRSUwQSUzQ2Zvcm0lMjBtZXRob2QlM0QlMjJwb3N0JTIyJTIwYWN0aW9uJTNEJTIyaHR0cHMlM0ElMkYlMkZhcHByb3gub3JnJTJGJTNGbmElM0RzJTIyJTNFJTBBJTBBJTNDaW5wdXQlMjB0eXBlJTNEJTIyaGlkZGVuJTIyJTIw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M0UlM0NkaXYlMjBjbGFzcyUzRCUyMnRucC1maWVsZCUyMHRucC1maWVsZC1idXR0b24lMjIlM0UlM0NpbnB1dCUyMGNsYXNzJTNEJTIydG5wLXN1Ym1pdCUyMiUyMHR5cGUlM0QlMjJzdWJtaXQlMjIlMjB2YWx1ZSUzRCUyMlN1YnNjcmliZSUyMiUyMCUzRSUwQSUzQyUyRmRpdiUzRSUwQSUzQyUyRmZvcm0lM0UlMEElM0MlMkZkaXYlM0UlM0NiciUyRiUzRSUzQyUyRnAlM0U=[/vc_raw_html][/vc_column][/vc_row]
No Result
View All Result
  • Contact Us
  • Homepages
  • Business
  • Guide

© 2024 APPROX FOUNDATION - The Crypto Currency News