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.
| 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. |
| 128 | PartiallySignedTransaction 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); |
no test coverage detected