Bitcoin ABC  0.22.12
P2P Digital Currency
psbt.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2018 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 <amount.h>
6 #include <coins.h>
7 #include <consensus/tx_verify.h>
8 #include <node/psbt.h>
9 #include <policy/policy.h>
10 #include <policy/settings.h>
11 #include <tinyformat.h>
12 
13 #include <numeric>
14 
16  // Go through each input and build status
17  PSBTAnalysis result;
18 
19  bool calc_fee = true;
20  bool all_final = true;
21  bool only_missing_sigs = true;
22  bool only_missing_final = false;
23  Amount in_amt{Amount::zero()};
24 
25  result.inputs.resize(psbtx.tx->vin.size());
26 
27  for (size_t i = 0; i < psbtx.tx->vin.size(); ++i) {
28  PSBTInput &input = psbtx.inputs[i];
29  PSBTInputAnalysis &input_analysis = result.inputs[i];
30 
31  // Check for a UTXO
32  CTxOut utxo;
33  if (psbtx.GetInputUTXO(utxo, i)) {
34  if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) {
35  result.SetInvalid(strprintf(
36  "PSBT is not valid. Input %u has invalid value", i));
37  return result;
38  }
39  in_amt += utxo.nValue;
40  input_analysis.has_utxo = true;
41  } else {
42  input_analysis.has_utxo = false;
43  input_analysis.is_final = false;
44  input_analysis.next = PSBTRole::UPDATER;
45  calc_fee = false;
46  }
47 
48  if (!utxo.IsNull() && utxo.scriptPubKey.IsUnspendable()) {
49  result.SetInvalid(strprintf(
50  "PSBT is not valid. Input %u spends unspendable output", i));
51  return result;
52  }
53 
54  // Check if it is final
55  if (!utxo.IsNull() && !PSBTInputSigned(input)) {
56  input_analysis.is_final = false;
57  all_final = false;
58 
59  // Figure out what is missing
60  SignatureData outdata;
61  bool complete = SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i,
62  SigHashType().withForkId(), &outdata);
63 
64  // Things are missing
65  if (!complete) {
66  input_analysis.missing_pubkeys = outdata.missing_pubkeys;
67  input_analysis.missing_redeem_script =
68  outdata.missing_redeem_script;
69  input_analysis.missing_sigs = outdata.missing_sigs;
70 
71  // If we are only missing signatures and nothing else, then next
72  // is signer
73  if (outdata.missing_pubkeys.empty() &&
74  outdata.missing_redeem_script.IsNull() &&
75  !outdata.missing_sigs.empty()) {
76  input_analysis.next = PSBTRole::SIGNER;
77  } else {
78  only_missing_sigs = false;
79  input_analysis.next = PSBTRole::UPDATER;
80  }
81  } else {
82  only_missing_final = true;
83  input_analysis.next = PSBTRole::FINALIZER;
84  }
85  } else if (!utxo.IsNull()) {
86  input_analysis.is_final = true;
87  }
88  }
89 
90  if (all_final) {
91  only_missing_sigs = false;
92  result.next = PSBTRole::EXTRACTOR;
93  }
94  if (calc_fee) {
95  // Get the output amount
96  Amount out_amt =
97  std::accumulate(psbtx.tx->vout.begin(), psbtx.tx->vout.end(),
98  Amount::zero(), [](Amount a, const CTxOut &b) {
99  if (!MoneyRange(a) || !MoneyRange(b.nValue) ||
100  !MoneyRange(a + b.nValue)) {
101  return -1 * SATOSHI;
102  }
103  return a += b.nValue;
104  });
105  if (!MoneyRange(out_amt)) {
106  result.SetInvalid(
107  strprintf("PSBT is not valid. Output amount invalid"));
108  return result;
109  }
110 
111  // Get the fee
112  Amount fee = in_amt - out_amt;
113  result.fee = fee;
114 
115  // Estimate the size
116  CMutableTransaction mtx(*psbtx.tx);
117  CCoinsView view_dummy;
118  CCoinsViewCache view(&view_dummy);
119  bool success = true;
120 
121  for (size_t i = 0; i < psbtx.tx->vin.size(); ++i) {
122  PSBTInput &input = psbtx.inputs[i];
123  CTxOut newUtxo;
124 
125  if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i,
126  SigHashType().withForkId(), nullptr, true) ||
127  !psbtx.GetInputUTXO(newUtxo, i)) {
128  success = false;
129  break;
130  } else {
131  mtx.vin[i].scriptSig = input.final_script_sig;
132  view.AddCoin(psbtx.tx->vin[i].prevout, Coin(newUtxo, 1, false),
133  true);
134  }
135  }
136 
137  if (success) {
139  size_t size = ctx.GetTotalSize();
140  result.estimated_vsize = size;
141  // Estimate fee rate
142  CFeeRate feerate(fee, size);
143  result.estimated_feerate = feerate;
144  }
145 
146  if (only_missing_sigs) {
147  result.next = PSBTRole::SIGNER;
148  } else if (only_missing_final) {
149  result.next = PSBTRole::FINALIZER;
150  } else if (all_final) {
151  result.next = PSBTRole::EXTRACTOR;
152  } else {
153  result.next = PSBTRole::UPDATER;
154  }
155  } else {
156  result.next = PSBTRole::UPDATER;
157  }
158 
159  return result;
160 }
static constexpr Amount zero()
Definition: amount.h:35
bool has_utxo
Whether we have UTXO information for this input.
Definition: psbt.h:15
CScript scriptPubKey
Definition: transaction.h:144
std::vector< CKeyID > missing_sigs
Pubkeys whose signatures are missing.
Definition: psbt.h:24
A UTXO entry.
Definition: coins.h:27
PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
Provides helpful miscellaneous information about where a PSBT is in the signing workflow.
Definition: psbt.cpp:15
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
Holds an analysis of one input from a PSBT.
Definition: psbt.h:13
secp256k1_context * ctx
std::vector< PSBTInputAnalysis > inputs
More information about the individual inputs of the transaction.
Definition: psbt.h:40
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Definition: sign.h:81
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
Definition: sign.h:79
Definition: amount.h:17
PSBTRole next
Which of the BIP 174 roles needs to handle the transaction next.
Definition: psbt.h:42
std::optional< CFeeRate > estimated_feerate
Estimated feerate (fee / weight) of the transaction.
Definition: psbt.h:36
Holds the results of AnalyzePSBT (miscellaneous information about a PSBT)
Definition: psbt.h:32
static constexpr Amount SATOSHI
Definition: amount.h:151
A version of CTransaction with the PSBT format.
Definition: psbt.h:335
std::vector< CKeyID > missing_pubkeys
Pubkeys whose BIP32 derivation path is missing.
Definition: psbt.h:22
std::optional< size_t > estimated_vsize
Estimated weight of the transaction.
Definition: psbt.h:34
bool IsNull() const
Definition: uint256.h:26
bool IsNull() const
Definition: transaction.h:164
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:552
std::vector< CTxIn > vin
Definition: transaction.h:299
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
uint160 missing_redeem_script
Hash160 of redeem script, if missing.
Definition: psbt.h:26
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
Definition: sign.h:83
Abstract view on the open txout dataset.
Definition: coins.h:175
void SetInvalid(std::string err_msg)
Definition: psbt.h:46
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:103
const SigningProvider & DUMMY_SIGNING_PROVIDER
A structure for PSBTs which contain per-input information.
Definition: psbt.h:43
bool is_final
Whether the input has all required information including signatures.
Definition: psbt.h:17
An output of a transaction.
Definition: transaction.h:141
bool SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, SigHashType sighash, SignatureData *out_sigdata, bool use_dummy)
Signs a PSBTInput, verifying that all provided data matches what is being signed. ...
Definition: psbt.cpp:198
PSBTRole next
Which of the BIP 174 roles needs to handle this input next.
Definition: psbt.h:19
std::vector< PSBTInput > inputs
Definition: psbt.h:337
A mutable version of CTransaction.
Definition: transaction.h:297
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:172
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:86
CScript final_script_sig
Definition: psbt.h:46
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
std::optional< CMutableTransaction > tx
Definition: psbt.h:336
bool GetInputUTXO(CTxOut &utxo, int input_index) const
Finds the UTXO for a given input index.
Definition: psbt.cpp:65
Amount nValue
Definition: transaction.h:143
std::optional< Amount > fee
Amount of fee being paid by the transaction.
Definition: psbt.h:38
Signature hash type wrapper class.
Definition: sighashtype.h:37