|
4 | 4 |
|
5 | 5 | #include <psbt.h>
|
6 | 6 |
|
| 7 | +#include <policy/policy.h> |
7 | 8 | #include <util/check.h>
|
8 | 9 | #include <util/strencodings.h>
|
9 | 10 |
|
@@ -273,11 +274,41 @@ void PSBTOutput::Merge(const PSBTOutput& output)
|
273 | 274 | if (m_tap_internal_key.IsNull() && !output.m_tap_internal_key.IsNull()) m_tap_internal_key = output.m_tap_internal_key;
|
274 | 275 | if (m_tap_tree.empty() && !output.m_tap_tree.empty()) m_tap_tree = output.m_tap_tree;
|
275 | 276 | }
|
| 277 | + |
276 | 278 | bool PSBTInputSigned(const PSBTInput& input)
|
277 | 279 | {
|
278 | 280 | return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
|
279 | 281 | }
|
280 | 282 |
|
| 283 | +bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata) |
| 284 | +{ |
| 285 | + CTxOut utxo; |
| 286 | + assert(psbt.inputs.size() >= input_index); |
| 287 | + const PSBTInput& input = psbt.inputs[input_index]; |
| 288 | + |
| 289 | + if (input.non_witness_utxo) { |
| 290 | + // If we're taking our information from a non-witness UTXO, verify that it matches the prevout. |
| 291 | + COutPoint prevout = psbt.tx->vin[input_index].prevout; |
| 292 | + if (prevout.n >= input.non_witness_utxo->vout.size()) { |
| 293 | + return false; |
| 294 | + } |
| 295 | + if (input.non_witness_utxo->GetHash() != prevout.hash) { |
| 296 | + return false; |
| 297 | + } |
| 298 | + utxo = input.non_witness_utxo->vout[prevout.n]; |
| 299 | + } else if (!input.witness_utxo.IsNull()) { |
| 300 | + utxo = input.witness_utxo; |
| 301 | + } else { |
| 302 | + return false; |
| 303 | + } |
| 304 | + |
| 305 | + if (txdata) { |
| 306 | + return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL}); |
| 307 | + } else { |
| 308 | + return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, MissingDataBehavior::FAIL}); |
| 309 | + } |
| 310 | +} |
| 311 | + |
281 | 312 | size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
|
282 | 313 | size_t count = 0;
|
283 | 314 | for (const auto& input : psbt.inputs) {
|
@@ -331,7 +362,7 @@ bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction&
|
331 | 362 | PSBTInput& input = psbt.inputs.at(index);
|
332 | 363 | const CMutableTransaction& tx = *psbt.tx;
|
333 | 364 |
|
334 |
| - if (PSBTInputSigned(input)) { |
| 365 | + if (PSBTInputSignedAndVerified(psbt, index, txdata)) { |
335 | 366 | return true;
|
336 | 367 | }
|
337 | 368 |
|
|
0 commit comments