diff --git a/btcutil/psbt/utils.go b/btcutil/psbt/utils.go index 65b597d59d6..c8b63e64129 100644 --- a/btcutil/psbt/utils.go +++ b/btcutil/psbt/utils.go @@ -398,6 +398,35 @@ func VerifyInputOutputLen(packet *Packet, needInputs, needOutputs bool) error { return nil } +// VerifyInputs makes sure that all input data have the previous output +// specified meaning that either nonwitness data or the witness data is +// specified in the psbt package. This check is necessary because of 2 reasons. +// The sighash calculation is now different for witnessV0 and witnessV1 inputs +// this means we need to check the previousOutput pkScript for the specific +// type and the second reason is that the sighash calculation for taproot inputs +// include the previousOutput pkscript. +func VerifyInputs(packet *Packet) error { + + if packet == nil || packet.UnsignedTx == nil { + return fmt.Errorf("PSBT packet cannot be nil") + } + + if len(packet.UnsignedTx.TxIn) != len(packet.Inputs) { + return fmt.Errorf("invalid PSBT, wire inputs don't match " + + "partial inputs") + } + + for i := range packet.UnsignedTx.TxIn { + input := packet.Inputs[i] + if input.NonWitnessUtxo == nil || input.WitnessUtxo == nil { + return fmt.Errorf("invalid PSBT, input with index %d "+ + "missing utxo information", i) + } + } + + return nil +} + // NewFromSignedTx is a utility function to create a packet from an // already-signed transaction. Returned are: an unsigned transaction // serialization, a list of scriptSigs, one per input, and a list of witnesses,