Skip to content

Commit b554add

Browse files
committed
psbt: add verification method for psbt input data
the new InputsReadyToSign method makes sure that inputs have either the nonWitnessUtxo or the witnessUtxo data set.
1 parent a18c2cf commit b554add

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

btcutil/psbt/utils.go

+24
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,30 @@ func VerifyInputOutputLen(packet *Packet, needInputs, needOutputs bool) error {
398398
return nil
399399
}
400400

401+
// InputsReadyToSign makes sure that all input data have the previous output
402+
// specified meaning that either nonwitness UTXO or the witness UTXO data is
403+
// specified in the psbt package. This check is necessary because of 2 reasons.
404+
// The sighash calculation is now different for witnessV0 and witnessV1 inputs
405+
// this means we need to check the previous output pkScript for the specific
406+
// type and the second reason is that the sighash calculation for taproot inputs
407+
// include the previous output pkscripts.
408+
func InputsReadyToSign(packet *Packet) error {
409+
err := VerifyInputOutputLen(packet, true, true)
410+
if err != nil {
411+
return err
412+
}
413+
414+
for i := range packet.UnsignedTx.TxIn {
415+
input := packet.Inputs[i]
416+
if input.NonWitnessUtxo == nil && input.WitnessUtxo == nil {
417+
return fmt.Errorf("invalid PSBT, input with index %d "+
418+
"missing utxo information", i)
419+
}
420+
}
421+
422+
return nil
423+
}
424+
401425
// NewFromSignedTx is a utility function to create a packet from an
402426
// already-signed transaction. Returned are: an unsigned transaction
403427
// serialization, a list of scriptSigs, one per input, and a list of witnesses,

0 commit comments

Comments
 (0)