MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ProcessPSBT

Function ProcessPSBT

src/rpc/rawtransaction.cpp:128–212  ·  view source on GitHub ↗

Update PSBT with information from the mempool, the UTXO set, the txindex, and the provided descriptors. Optionally, sign the inputs that we can using information from the descriptors.

Source from the content-addressed store, hash-verified

126// Update PSBT with information from the mempool, the UTXO set, the txindex, and the provided descriptors.
127// Optionally, sign the inputs that we can using information from the descriptors.
128PartiallySignedTransaction ProcessPSBT(const std::string& psbt_string, const std::any& context, const HidingSigningProvider& provider, std::optional<int> sighash_type, bool finalize)
129{
130 // Unserialize the transactions
131 util::Result<PartiallySignedTransaction> psbt_res = DecodeBase64PSBT(psbt_string);
132 if (!psbt_res) {
133 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", util::ErrorString(psbt_res).original));
134 }
135 PartiallySignedTransaction psbtx = *psbt_res;
136
137 if (g_txindex) g_txindex->BlockUntilSyncedToCurrentChain();
138 const NodeContext& node = EnsureAnyNodeContext(context);
139
140 // If we can't find the corresponding full transaction for all of our inputs,
141 // this will be used to find just the utxos for the segwit inputs for which
142 // the full transaction isn't found
143 std::map<COutPoint, Coin> coins;
144
145 // Fetch previous transactions:
146 // First, look in the txindex and the mempool
147 for (PSBTInput& psbt_input : psbtx.inputs) {
148 // The `non_witness_utxo` is the whole previous transaction
149 if (psbt_input.non_witness_utxo) continue;
150
151 CTransactionRef tx;
152
153 // Look in the txindex
154 if (g_txindex) {
155 uint256 block_hash;
156 g_txindex->FindTx(psbt_input.prev_txid, block_hash, tx);
157 }
158 // If we still don't have it look in the mempool
159 if (!tx) {
160 tx = node.mempool->get(psbt_input.prev_txid);
161 }
162 if (tx) {
163 psbt_input.non_witness_utxo = tx;
164 } else {
165 coins[psbt_input.GetOutPoint()]; // Create empty map entry keyed by prevout
166 }
167 }
168
169 // If we still haven't found all of the inputs, look for the missing ones in the utxo set
170 if (!coins.empty()) {
171 FindCoins(node, coins);
172 for (PSBTInput& input : psbtx.inputs) {
173 // If there are still missing utxos, add them if they were found in the utxo set
174 if (!input.non_witness_utxo) {
175 const Coin& coin = coins.at(input.GetOutPoint());
176 if (!coin.out.IsNull() && IsSegWitOutput(provider, coin.out.scriptPubKey)) {
177 input.witness_utxo = coin.out;
178 }
179 }
180 }
181 }
182
183 std::optional<PrecomputedTransactionData> txdata_res = PrecomputePSBTData(psbtx);
184 if (!txdata_res) {
185 throw JSONRPCPSBTError(common::PSBTError::INVALID_TX);

Callers 2

utxoupdatepsbtFunction · 0.85
descriptorprocesspsbtFunction · 0.85

Calls 15

DecodeBase64PSBTFunction · 0.85
JSONRPCErrorFunction · 0.85
ErrorStringFunction · 0.85
FindCoinsFunction · 0.85
IsSegWitOutputFunction · 0.85
PrecomputePSBTDataFunction · 0.85
JSONRPCPSBTErrorFunction · 0.85
PSBTInputSignedFunction · 0.85
SignPSBTInputFunction · 0.85
UpdatePSBTOutputFunction · 0.85
FindTxMethod · 0.80

Tested by

no test coverage detected