| 645 | } |
| 646 | |
| 647 | PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, const common::PSBTFillOptions& options, SignatureData* out_sigdata) |
| 648 | { |
| 649 | PSBTInput& input = psbt.inputs.at(index); |
| 650 | std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx(); |
| 651 | if (!unsigned_tx) { |
| 652 | return PSBTError::INVALID_TX; |
| 653 | } |
| 654 | const CMutableTransaction& tx = *unsigned_tx; |
| 655 | |
| 656 | if (PSBTInputSignedAndVerified(psbt, index, txdata)) { |
| 657 | return PSBTError::OK; |
| 658 | } |
| 659 | |
| 660 | // Fill SignatureData with input info |
| 661 | SignatureData sigdata; |
| 662 | input.FillSignatureData(sigdata); |
| 663 | |
| 664 | // Get UTXO |
| 665 | bool require_witness_sig = false; |
| 666 | CTxOut utxo; |
| 667 | |
| 668 | if (input.non_witness_utxo) { |
| 669 | // If we're taking our information from a non-witness UTXO, verify that it matches the prevout. |
| 670 | COutPoint prevout = input.GetOutPoint(); |
| 671 | if (prevout.n >= input.non_witness_utxo->vout.size()) { |
| 672 | return PSBTError::MISSING_INPUTS; |
| 673 | } |
| 674 | if (input.non_witness_utxo->GetHash() != prevout.hash) { |
| 675 | return PSBTError::MISSING_INPUTS; |
| 676 | } |
| 677 | utxo = input.non_witness_utxo->vout[prevout.n]; |
| 678 | } else if (!input.witness_utxo.IsNull()) { |
| 679 | utxo = input.witness_utxo; |
| 680 | // When we're taking our information from a witness UTXO, we can't verify it is actually data from |
| 681 | // the output being spent. This is safe in case a witness signature is produced (which includes this |
| 682 | // information directly in the hash), but not for non-witness signatures. Remember that we require |
| 683 | // a witness signature in this situation. |
| 684 | require_witness_sig = true; |
| 685 | } else { |
| 686 | return PSBTError::MISSING_INPUTS; |
| 687 | } |
| 688 | |
| 689 | // Get the sighash type |
| 690 | // If both the field and the parameter are provided, they must match |
| 691 | // If only the parameter is provided, use it and add it to the PSBT if it is other than SIGHASH_DEFAULT |
| 692 | // for all input types, and not SIGHASH_ALL for non-taproot input types. |
| 693 | // If neither are provided, use SIGHASH_DEFAULT if it is taproot, and SIGHASH_ALL for everything else. |
| 694 | int sighash{options.sighash_type.value_or(utxo.scriptPubKey.IsPayToTaproot() ? SIGHASH_DEFAULT : SIGHASH_ALL)}; |
| 695 | |
| 696 | // For user safety, the desired sighash must be provided if the PSBT wants something other than the default set in the previous line. |
| 697 | if (input.sighash_type && input.sighash_type != sighash) { |
| 698 | return PSBTError::SIGHASH_MISMATCH; |
| 699 | } |
| 700 | // Set the PSBT sighash field when sighash is not DEFAULT or ALL |
| 701 | // DEFAULT is allowed for non-taproot inputs since DEFAULT may be passed for them (e.g. the psbt being signed also has taproot inputs) |
| 702 | // Note that signing already aliases DEFAULT to ALL for non-taproot inputs. |
| 703 | if (utxo.scriptPubKey.IsPayToTaproot() ? sighash != SIGHASH_DEFAULT : |
| 704 | (sighash != SIGHASH_DEFAULT && sighash != SIGHASH_ALL)) { |
no test coverage detected