@@ -244,17 +244,33 @@ bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& t
244
244
input.FillSignatureData (sigdata);
245
245
246
246
// Get UTXO
247
+ bool require_witness_sig = false ;
247
248
CTxOut utxo;
248
249
if (input.non_witness_utxo ) {
250
+ // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
251
+ if (input.non_witness_utxo ->GetHash () != tx.vin [index].prevout .hash ) return false ;
252
+ // If both witness and non-witness UTXO are provided, verify that they match. This check shouldn't
253
+ // matter, as the PSBT deserializer enforces only one of both is provided, and the only way both
254
+ // can be present is when they're added simultaneously by FillPSBT (in which case they always match).
255
+ // Still, check in order to not rely on callers to enforce this.
256
+ if (!input.witness_utxo .IsNull () && input.non_witness_utxo ->vout [tx.vin [index].prevout .n ] != input.witness_utxo ) return false ;
249
257
utxo = input.non_witness_utxo ->vout [tx.vin [index].prevout .n ];
250
258
} else if (!input.witness_utxo .IsNull ()) {
251
259
utxo = input.witness_utxo ;
260
+ // When we're taking our information from a witness UTXO, we can't verify it is actually data from
261
+ // the output being spent. This is safe in case a witness signature is produced (which includes this
262
+ // information directly in the hash), but not for non-witness signatures. Remember that we require
263
+ // a witness signature in this situation.
264
+ require_witness_sig = true ;
252
265
} else {
253
266
return false ;
254
267
}
255
268
256
269
MutableTransactionSignatureCreator creator (&tx, index, utxo.nValue , sighash);
270
+ sigdata.witness = false ;
257
271
bool sig_complete = ProduceSignature (provider, creator, utxo.scriptPubKey , sigdata);
272
+ // Verify that a witness signature was produced in case one was required.
273
+ if (require_witness_sig && !sigdata.witness ) return false ;
258
274
input.FromSignatureData (sigdata);
259
275
return sig_complete;
260
276
}
0 commit comments