Line data Source code
1 : // Copyright (c) 2009-2021 The Bitcoin Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <coins.h> 6 : #include <consensus/tx_verify.h> 7 : #include <node/psbt.h> 8 : #include <policy/policy.h> 9 : #include <policy/settings.h> 10 : #include <tinyformat.h> 11 : 12 : #include <numeric> 13 : 14 : namespace node { 15 0 : PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) 16 : { 17 : // Go through each input and build status 18 0 : PSBTAnalysis result; 19 : 20 0 : bool calc_fee = true; 21 : 22 0 : CAmount in_amt = 0; 23 : 24 0 : result.inputs.resize(psbtx.tx->vin.size()); 25 : 26 0 : const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx); 27 : 28 0 : for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) { 29 0 : PSBTInput& input = psbtx.inputs[i]; 30 0 : PSBTInputAnalysis& input_analysis = result.inputs[i]; 31 : 32 : // We set next role here and ratchet backwards as required 33 0 : input_analysis.next = PSBTRole::EXTRACTOR; 34 : 35 : // Check for a UTXO 36 0 : CTxOut utxo; 37 0 : if (psbtx.GetInputUTXO(utxo, i)) { 38 0 : if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) { 39 0 : result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i)); 40 0 : return result; 41 : } 42 0 : in_amt += utxo.nValue; 43 0 : input_analysis.has_utxo = true; 44 0 : } else { 45 0 : if (input.non_witness_utxo && psbtx.tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) { 46 0 : result.SetInvalid(strprintf("PSBT is not valid. Input %u specifies invalid prevout", i)); 47 0 : return result; 48 : } 49 0 : input_analysis.has_utxo = false; 50 0 : input_analysis.is_final = false; 51 0 : input_analysis.next = PSBTRole::UPDATER; 52 0 : calc_fee = false; 53 : } 54 : 55 0 : if (!utxo.IsNull() && utxo.scriptPubKey.IsUnspendable()) { 56 0 : result.SetInvalid(strprintf("PSBT is not valid. Input %u spends unspendable output", i)); 57 0 : return result; 58 : } 59 : 60 : // Check if it is final 61 0 : if (!utxo.IsNull() && !PSBTInputSigned(input)) { 62 0 : input_analysis.is_final = false; 63 : 64 : // Figure out what is missing 65 0 : SignatureData outdata; 66 0 : bool complete = SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, 1, &outdata); 67 : 68 : // Things are missing 69 0 : if (!complete) { 70 0 : input_analysis.missing_pubkeys = outdata.missing_pubkeys; 71 0 : input_analysis.missing_redeem_script = outdata.missing_redeem_script; 72 0 : input_analysis.missing_sigs = outdata.missing_sigs; 73 : 74 : // If we are only missing signatures and nothing else, then next is signer 75 0 : if (outdata.missing_pubkeys.empty() && outdata.missing_redeem_script.IsNull() && !outdata.missing_sigs.empty()) { 76 0 : input_analysis.next = PSBTRole::SIGNER; 77 0 : } else { 78 0 : input_analysis.next = PSBTRole::UPDATER; 79 : } 80 0 : } else { 81 0 : input_analysis.next = PSBTRole::FINALIZER; 82 : } 83 0 : } else if (!utxo.IsNull()){ 84 0 : input_analysis.is_final = true; 85 0 : } 86 0 : } 87 : 88 : // Calculate next role for PSBT by grabbing "minumum" PSBTInput next role 89 0 : result.next = PSBTRole::EXTRACTOR; 90 0 : for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) { 91 0 : PSBTInputAnalysis& input_analysis = result.inputs[i]; 92 0 : result.next = std::min(result.next, input_analysis.next); 93 0 : } 94 0 : assert(result.next > PSBTRole::CREATOR); 95 : 96 0 : if (calc_fee) { 97 : // Get the output amount 98 0 : CAmount out_amt = std::accumulate(psbtx.tx->vout.begin(), psbtx.tx->vout.end(), CAmount(0), 99 0 : [](CAmount a, const CTxOut& b) { 100 0 : if (!MoneyRange(a) || !MoneyRange(b.nValue) || !MoneyRange(a + b.nValue)) { 101 0 : return CAmount(-1); 102 : } 103 0 : return a += b.nValue; 104 0 : } 105 : ); 106 0 : if (!MoneyRange(out_amt)) { 107 0 : result.SetInvalid("PSBT is not valid. Output amount invalid"); 108 0 : return result; 109 : } 110 : 111 : // Get the fee 112 0 : CAmount fee = in_amt - out_amt; 113 0 : result.fee = fee; 114 : 115 : // Estimate the size 116 0 : CMutableTransaction mtx(*psbtx.tx); 117 0 : CCoinsView view_dummy; 118 0 : CCoinsViewCache view(&view_dummy); 119 0 : bool success = true; 120 : 121 0 : for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) { 122 0 : PSBTInput& input = psbtx.inputs[i]; 123 0 : Coin newcoin; 124 : 125 0 : if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, 1) || !psbtx.GetInputUTXO(newcoin.out, i)) { 126 0 : success = false; 127 0 : break; 128 : } else { 129 0 : mtx.vin[i].scriptSig = input.final_script_sig; 130 0 : newcoin.nHeight = 1; 131 0 : view.AddCoin(psbtx.tx->vin[i].prevout, std::move(newcoin), true); 132 : } 133 0 : } 134 : 135 0 : if (success) { 136 0 : CTransaction ctx = CTransaction(mtx); 137 0 : size_t size = GetVirtualTransactionSize(ctx, GetTransactionSigOpCount(ctx, view, STANDARD_SCRIPT_VERIFY_FLAGS)); 138 0 : result.estimated_vsize = size; 139 : // Estimate fee rate 140 0 : CFeeRate feerate(fee, size); 141 0 : result.estimated_feerate = feerate; 142 0 : } 143 : 144 0 : } 145 : 146 0 : return result; 147 0 : } 148 : } // namespace node