1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
crate::ix!();

//-------------------------------------------[.cpp/bitcoin/src/node/psbt.h]
//-------------------------------------------[.cpp/bitcoin/src/node/psbt.cpp]

/**
  | Holds an analysis of one input from a
  | PSBT
  |
  */
pub struct PSBTInputAnalysis {

    /**
      | Whether we have UTXO information for
      | this input
      |
      */
    has_utxo:               bool,

    /**
      | Whether the input has all required information
      | including signatures
      |
      */
    is_final:               bool,

    /**
      | Which of the BIP 174 roles needs to handle
      | this input next
      |
      */
    next:                   PSBTRole,

    /**
      | Pubkeys whose BIP32 derivation path
      | is missing
      |
      */
    missing_pubkeys:        Vec<KeyID>,

    /**
      | Pubkeys whose signatures are missing
      |
      */
    missing_sigs:           Vec<KeyID>,

    /**
      | Hash160 of redeem script, if missing
      |
      */
    missing_redeem_script:  u160,

    /**
      | SHA256 of witness script, if missing
      |
      */
    missing_witness_script: u256,
}

/**
  | Holds the results of AnalyzePSBT (miscellaneous
  | information about a PSBT)
  |
  */
pub struct PSBTAnalysis {

    /**
      | Estimated weight of the transaction
      |
      */
    estimated_vsize:   Option<usize>,

    /**
      | Estimated feerate (fee / weight) of
      | the transaction
      |
      */
    estimated_feerate: Option<FeeRate>,

    /**
      | Amount of fee being paid by the transaction
      |
      */
    fee:               Option<Amount>,

    /**
      | More information about the individual
      | inputs of the transaction
      |
      */
    inputs:            Vec<PSBTInputAnalysis>,

    /**
      | Which of the BIP 174 roles needs to handle
      | the transaction next
      |
      */
    next:              PSBTRole,

    /**
      | Error message
      |
      */
    error:             String,
}

impl PSBTAnalysis {
    
    pub fn set_invalid(&mut self, err_msg: String)  {
        
        todo!();
        /*
            estimated_vsize = std::nullopt;
            estimated_feerate = std::nullopt;
            fee = std::nullopt;
            inputs.clear();
            next = PSBTRole::CREATOR;
            error = err_msg;
        */
    }
}

/**
  | Provides helpful miscellaneous information
  | about where a PSBT is in the signing workflow.
  | 
  | -----------
  | @param[in] psbtx
  | 
  | the PSBT to analyze
  | 
  | -----------
  | @return
  | 
  | A PSBTAnalysis with information about
  | the provided PSBT.
  |
  */
pub fn analyzepsbt(psbtx: PartiallySignedTransaction) -> PSBTAnalysis {
    
    todo!();
        /*
            // Go through each input and build status
        PSBTAnalysis result;

        bool calc_fee = true;

        CAmount in_amt = 0;

        result.inputs.resize(psbtx.tx->vin.size());

        const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);

        for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
            PSBTInput& input = psbtx.inputs[i];
            PSBTInputAnalysis& input_analysis = result.inputs[i];

            // We set next role here and ratchet backwards as required
            input_analysis.next = PSBTRole::EXTRACTOR;

            // Check for a UTXO
            CTxOut utxo;
            if (psbtx.GetInputUTXO(utxo, i)) {
                if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) {
                    result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i));
                    return result;
                }
                in_amt += utxo.nValue;
                input_analysis.has_utxo = true;
            } else {
                if (input.non_witness_utxo && psbtx.tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) {
                    result.SetInvalid(strprintf("PSBT is not valid. Input %u specifies invalid prevout", i));
                    return result;
                }
                input_analysis.has_utxo = false;
                input_analysis.is_final = false;
                input_analysis.next = PSBTRole::UPDATER;
                calc_fee = false;
            }

            if (!utxo.IsNull() && utxo.scriptPubKey.IsUnspendable()) {
                result.SetInvalid(strprintf("PSBT is not valid. Input %u spends unspendable output", i));
                return result;
            }

            // Check if it is final
            if (!utxo.IsNull() && !PSBTInputSigned(input)) {
                input_analysis.is_final = false;

                // Figure out what is missing
                SignatureData outdata;
                bool complete = SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, 1, &outdata);

                // Things are missing
                if (!complete) {
                    input_analysis.missing_pubkeys = outdata.missing_pubkeys;
                    input_analysis.missing_redeem_script = outdata.missing_redeem_script;
                    input_analysis.missing_witness_script = outdata.missing_witness_script;
                    input_analysis.missing_sigs = outdata.missing_sigs;

                    // If we are only missing signatures and nothing else, then next is signer
                    if (outdata.missing_pubkeys.empty() && outdata.missing_redeem_script.IsNull() && outdata.missing_witness_script.IsNull() && !outdata.missing_sigs.empty()) {
                        input_analysis.next = PSBTRole::SIGNER;
                    } else {
                        input_analysis.next = PSBTRole::UPDATER;
                    }
                } else {
                    input_analysis.next = PSBTRole::FINALIZER;
                }
            } else if (!utxo.IsNull()){
                input_analysis.is_final = true;
            }
        }

        // Calculate next role for PSBT by grabbing "minimum" PSBTInput next role
        result.next = PSBTRole::EXTRACTOR;
        for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
            PSBTInputAnalysis& input_analysis = result.inputs[i];
            result.next = std::min(result.next, input_analysis.next);
        }
        assert(result.next > PSBTRole::CREATOR);

        if (calc_fee) {
            // Get the output amount
            CAmount out_amt = std::accumulate(psbtx.tx->vout.begin(), psbtx.tx->vout.end(), CAmount(0),
                [](CAmount a, const CTxOut& b) {
                    if (!MoneyRange(a) || !MoneyRange(b.nValue) || !MoneyRange(a + b.nValue)) {
                        return CAmount(-1);
                    }
                    return a += b.nValue;
                }
            );
            if (!MoneyRange(out_amt)) {
                result.SetInvalid(strprintf("PSBT is not valid. Output amount invalid"));
                return result;
            }

            // Get the fee
            CAmount fee = in_amt - out_amt;
            result.fee = fee;

            // Estimate the size
            CMutableTransaction mtx(*psbtx.tx);
            CCoinsView view_dummy;
            CCoinsViewCache view(&view_dummy);
            bool success = true;

            for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
                PSBTInput& input = psbtx.inputs[i];
                Coin newcoin;

                if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, 1) || !psbtx.GetInputUTXO(newcoin.out, i)) {
                    success = false;
                    break;
                } else {
                    mtx.vin[i].scriptSig = input.final_script_sig;
                    mtx.vin[i].scriptWitness = input.final_script_witness;
                    newcoin.nHeight = 1;
                    view.AddCoin(psbtx.tx->vin[i].prevout, std::move(newcoin), true);
                }
            }

            if (success) {
                CTransaction ctx = CTransaction(mtx);
                size_t size = GetVirtualTransactionSize(ctx, GetTransactionSigOpCost(ctx, view, STANDARD_SCRIPT_VERIFY_FLAGS));
                result.estimated_vsize = size;
                // Estimate fee rate
                CFeeRate feerate(fee, size);
                result.estimated_feerate = feerate;
            }

        }

        return result;
        */
}