@@ -375,7 +375,7 @@ PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction&
375375 return txdata;
376376}
377377
378- PSBTError SignPSBTInput (const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, int sighash, SignatureData* out_sigdata, bool finalize)
378+ PSBTError SignPSBTInput (const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, std::optional< int > sighash, SignatureData* out_sigdata, bool finalize)
379379{
380380 PSBTInput& input = psbt.inputs .at (index);
381381 const CMutableTransaction& tx = *psbt.tx ;
@@ -413,12 +413,24 @@ PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransact
413413 return PSBTError::MISSING_INPUTS;
414414 }
415415
416+ // Get the sighash type
417+ // If both the field and the parameter are provided, they must match
418+ // If only the parameter is provided, use it and add it to the PSBT if it is other than SIGHASH_DEFAULT
419+ // for all input types, and not SIGHASH_ALL for non-taproot input types.
420+ // If neither are provided, use SIGHASH_DEFAULT if it is taproot, and SIGHASH_ALL for everything else.
421+ if (!sighash) sighash = utxo.scriptPubKey .IsPayToTaproot () ? SIGHASH_DEFAULT : SIGHASH_ALL;
422+ Assert (sighash.has_value ());
423+ // For user safety, the desired sighash must be provided if the PSBT wants something other than the default set in the previous line.
424+ if (input.sighash_type && input.sighash_type != sighash) {
425+ return PSBTError::SIGHASH_MISMATCH;
426+ }
427+
416428 sigdata.witness = false ;
417429 bool sig_complete;
418430 if (txdata == nullptr ) {
419431 sig_complete = ProduceSignature (provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey , sigdata);
420432 } else {
421- MutableTransactionSignatureCreator creator (tx, index, utxo.nValue , txdata, sighash);
433+ MutableTransactionSignatureCreator creator (tx, index, utxo.nValue , txdata, * sighash);
422434 sig_complete = ProduceSignature (provider, creator, utxo.scriptPubKey , sigdata);
423435 }
424436 // Verify that a witness signature was produced in case one was required.
@@ -448,10 +460,11 @@ PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransact
448460 return sig_complete ? PSBTError::OK : PSBTError::INCOMPLETE;
449461}
450462
451- void RemoveUnnecessaryTransactions (PartiallySignedTransaction& psbtx, const int & sighash_type)
463+ void RemoveUnnecessaryTransactions (PartiallySignedTransaction& psbtx, std::optional< int > sighash_type)
452464{
465+ if (!sighash_type) sighash_type = SIGHASH_DEFAULT;
453466 // Only drop non_witness_utxos if sighash_type != SIGHASH_ANYONECANPAY
454- if ((sighash_type & 0x80 ) != SIGHASH_ANYONECANPAY) {
467+ if ((* sighash_type & 0x80 ) != SIGHASH_ANYONECANPAY) {
455468 // Figure out if any non_witness_utxos should be dropped
456469 std::vector<unsigned int > to_drop;
457470 for (unsigned int i = 0 ; i < psbtx.inputs .size (); ++i) {
@@ -489,7 +502,7 @@ bool FinalizePSBT(PartiallySignedTransaction& psbtx)
489502 bool complete = true ;
490503 const PrecomputedTransactionData txdata = PrecomputePSBTData (psbtx);
491504 for (unsigned int i = 0 ; i < psbtx.tx ->vin .size (); ++i) {
492- complete &= (SignPSBTInput (DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, SIGHASH_ALL , nullptr , true ) == PSBTError::OK);
505+ complete &= (SignPSBTInput (DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, std:: nullopt , nullptr , true ) == PSBTError::OK);
493506 }
494507
495508 return complete;
0 commit comments