Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b97b95

Browse files
committed
psbt: add PrevOutFetcher helper func
1 parent 57c913b commit 0b97b95

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

btcutil/psbt/utils.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,36 @@ func FindLeafScript(pInput *PInput,
469469
return nil, fmt.Errorf("leaf script for target leaf hash %x not "+
470470
"found in input", targetLeafHash)
471471
}
472+
473+
// PrevOutputFetcher returns a txscript.PrevOutFetcher built from the UTXO
474+
// information in a PSBT packet.
475+
func PrevOutputFetcher(packet *Packet) *txscript.MultiPrevOutFetcher {
476+
fetcher := txscript.NewMultiPrevOutFetcher(nil)
477+
for idx, txIn := range packet.UnsignedTx.TxIn {
478+
in := packet.Inputs[idx]
479+
480+
// Skip any input that has no UTXO.
481+
if in.WitnessUtxo == nil && in.NonWitnessUtxo == nil {
482+
continue
483+
}
484+
485+
if in.NonWitnessUtxo != nil {
486+
prevIndex := txIn.PreviousOutPoint.Index
487+
fetcher.AddPrevOut(
488+
txIn.PreviousOutPoint,
489+
in.NonWitnessUtxo.TxOut[prevIndex],
490+
)
491+
492+
continue
493+
}
494+
495+
// Fall back to witness UTXO only for older wallets.
496+
if in.WitnessUtxo != nil {
497+
fetcher.AddPrevOut(
498+
txIn.PreviousOutPoint, in.WitnessUtxo,
499+
)
500+
}
501+
}
502+
503+
return fetcher
504+
}

0 commit comments

Comments
 (0)