• 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

Russia Considers Simplified Licensing Path For Bank-Run Crypto Exchanges

Russia Considers Simplified Licensing Path For Bank-Run Crypto Exchanges

March 7, 2026
OKX Introduces Social Trading Platform After $25 Billion Valuation

OKX Introduces Social Trading Platform After $25 Billion Valuation

March 7, 2026
Share76Tweet47

Related Posts

Russia Considers Simplified Licensing Path For Bank-Run Crypto Exchanges

Russia Considers Simplified Licensing Path For Bank-Run Crypto Exchanges

by Moussa
March 7, 2026
0

Russia’s central bank is weighing a plan that would allow banks and brokerage firms to operate cryptocurrency exchanges through a...

OKX Introduces Social Trading Platform After $25 Billion Valuation

OKX Introduces Social Trading Platform After $25 Billion Valuation

by Moussa
March 7, 2026
0

Crypto exchange OKX is rolling out a built-in social network called Orbit inside its trading app, aiming to merge market...

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

Blockchain.info wallet json created device name

by Moussa
March 7, 2026
0

I have a blockchain info wallet. From this I got a json file. Looking at the unencrypted file there are...

Shiba Inu Price Analysis: Burn Rate Skyrockets 53,000% – What Does This Mean?

Shiba Inu Price Analysis: Burn Rate Skyrockets 53,000% – What Does This Mean?

by Moussa
March 7, 2026
0

Shiba Inu just saw its burn rate explode, fueling bullish price predictions. In the past 24 hours, the SHIB burn...

Bitcoin Could Outshine Gold Through 2029, Macroeconomist Predicts

Bitcoin Could Outshine Gold Through 2029, Macroeconomist Predicts

by Moussa
March 7, 2026
0

Trusted Editorial content, reviewed by leading industry experts and seasoned editors. Ad Disclosure The gap between how investors feel about...

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

© 2024 APPROX FOUNDATION - The Crypto Currency News