• 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

Chainalysis Finds $443B MENA Crypto Economy as UAE Growth Outpaces Region

Polymarket Sets $3.9M Refund Plan After Third-Party Script Compromise

November 2, 2026
Bitcoin Mining Revenue Hits 11-Month High Despite Brutal Hashprice Decline

Bitcoin Mining Revenue Hits 11-Month High Despite Brutal Hashprice Decline

November 1, 2026
Share76Tweet47

Related Posts

Chainalysis Finds $443B MENA Crypto Economy as UAE Growth Outpaces Region

Polymarket Sets $3.9M Refund Plan After Third-Party Script Compromise

by Moussa
November 2, 2026
0

TL;DR: Polymarket says its October 29 incident involved a compromised monitoring script. Its October 30 update put total refunds at...

Bitcoin Mining Revenue Hits 11-Month High Despite Brutal Hashprice Decline

Bitcoin Mining Revenue Hits 11-Month High Despite Brutal Hashprice Decline

by Moussa
November 1, 2026
0

Key TakeawaysBitcoin miners face mounting pressure as market conditions deteriorate.October’s mining figures reveal an unexpected financial contradiction.November could expose deeper...

Why is my Bitcoin Core v32.1 MSVC build smaller than the official Windows release build?

by Moussa
November 1, 2026
0

I built Bitcoin Core v32.1 from source on Windows 11 26H2 + VS2026 18.11.3 using doc/build-windows-msvc.md build process and noticed...

Bitcoin FOMC Hopes Hang in the Balance as Oil Surges and Ukraine Seeks Peace

Bitcoin FOMC Hopes Hang in the Balance as Oil Surges and Ukraine Seeks Peace

by Moussa
November 1, 2026
0

Bitcoin recovered toward $81,000 as we prepared for FOMC latest interest rate decision. BTC bounced nearly 2% during the rebound,...

Binance Processes $4.65B in Withdrawals Across 15 Networks After Hack

Binance Processes $4.65B in Withdrawals Across 15 Networks After Hack

by Moussa
November 1, 2026
0

Key TakeawaysNet outflows equaled approximately 2.1% of Binance’s reported assets.All withdrawal routes in the initial phase reopened by 12:36 UTC.An...

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

© 2024 APPROX FOUNDATION - The Crypto Currency News