| 14 | |
| 15 | namespace node { |
| 16 | PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) |
| 17 | { |
| 18 | // Go through each input and build status |
| 19 | PSBTAnalysis result; |
| 20 | |
| 21 | std::optional<CMutableTransaction> unsigned_tx = psbtx.GetUnsignedTx(); |
| 22 | if (!unsigned_tx) { |
| 23 | result.SetInvalid("PSBT cannot be made into a valid transaction"); |
| 24 | return result; |
| 25 | } |
| 26 | CMutableTransaction& mtx = *unsigned_tx; |
| 27 | |
| 28 | bool calc_fee = true; |
| 29 | |
| 30 | CAmount in_amt = 0; |
| 31 | |
| 32 | result.inputs.resize(psbtx.inputs.size()); |
| 33 | |
| 34 | // PrecomputePSBTData calls GetUnsignedTx() which we checked already works |
| 35 | const PrecomputedTransactionData txdata = *PrecomputePSBTData(psbtx); |
| 36 | |
| 37 | for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) { |
| 38 | PSBTInput& input = psbtx.inputs[i]; |
| 39 | PSBTInputAnalysis& input_analysis = result.inputs[i]; |
| 40 | |
| 41 | // We set next role here and ratchet backwards as required |
| 42 | input_analysis.next = PSBTRole::EXTRACTOR; |
| 43 | |
| 44 | // Check for a UTXO |
| 45 | CTxOut utxo; |
| 46 | if (input.GetUTXO(utxo)) { |
| 47 | if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) { |
| 48 | result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i)); |
| 49 | return result; |
| 50 | } |
| 51 | in_amt += utxo.nValue; |
| 52 | input_analysis.has_utxo = true; |
| 53 | } else { |
| 54 | if (input.non_witness_utxo && input.prev_out >= input.non_witness_utxo->vout.size()) { |
| 55 | result.SetInvalid(strprintf("PSBT is not valid. Input %u specifies invalid prevout", i)); |
| 56 | return result; |
| 57 | } |
| 58 | input_analysis.has_utxo = false; |
| 59 | input_analysis.is_final = false; |
| 60 | input_analysis.next = PSBTRole::UPDATER; |
| 61 | calc_fee = false; |
| 62 | } |
| 63 | |
| 64 | if (!utxo.IsNull() && utxo.scriptPubKey.IsUnspendable()) { |
| 65 | result.SetInvalid(strprintf("PSBT is not valid. Input %u spends unspendable output", i)); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | // Check if it is final |
| 70 | if (!PSBTInputSignedAndVerified(psbtx, i, &txdata)) { |
| 71 | input_analysis.is_final = false; |
| 72 | |
| 73 | // Figure out what is missing |