Line data Source code
1 : // Copyright (c) 2009-2022 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 <psbt.h>
6 :
7 : #include <policy/policy.h>
8 : #include <script/signingprovider.h>
9 : #include <util/check.h>
10 : #include <util/strencodings.h>
11 :
12 :
13 0 : PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx) : tx(tx)
14 : {
15 0 : inputs.resize(tx.vin.size());
16 0 : outputs.resize(tx.vout.size());
17 2 : }
18 2 :
19 0 : bool PartiallySignedTransaction::IsNull() const
20 : {
21 0 : return !tx && inputs.empty() && outputs.empty() && unknown.empty();
22 : }
23 :
24 0 : bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
25 : {
26 : // Prohibited to merge two PSBTs over different transactions
27 2 : if (tx->GetHash() != psbt.tx->GetHash()) {
28 0 : return false;
29 : }
30 :
31 0 : for (unsigned int i = 0; i < inputs.size(); ++i) {
32 0 : inputs[i].Merge(psbt.inputs[i]);
33 0 : }
34 0 : for (unsigned int i = 0; i < outputs.size(); ++i) {
35 0 : outputs[i].Merge(psbt.outputs[i]);
36 0 : }
37 0 : for (auto& xpub_pair : psbt.m_xpubs) {
38 0 : if (m_xpubs.count(xpub_pair.first) == 0) {
39 0 : m_xpubs[xpub_pair.first] = xpub_pair.second;
40 0 : } else {
41 0 : m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
42 : }
43 : }
44 0 : unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
45 :
46 0 : return true;
47 0 : }
48 :
49 0 : bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin)
50 : {
51 0 : if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) {
52 0 : return false;
53 : }
54 0 : tx->vin.push_back(txin);
55 0 : psbtin.partial_sigs.clear();
56 0 : psbtin.final_script_sig.clear();
57 0 : psbtin.final_script_witness.SetNull();
58 0 : inputs.push_back(psbtin);
59 0 : return true;
60 0 : }
61 :
62 0 : bool PartiallySignedTransaction::AddOutput(const CTxOut& txout, const PSBTOutput& psbtout)
63 : {
64 0 : tx->vout.push_back(txout);
65 0 : outputs.push_back(psbtout);
66 0 : return true;
67 : }
68 :
69 0 : bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const
70 : {
71 0 : const PSBTInput& input = inputs[input_index];
72 0 : uint32_t prevout_index = tx->vin[input_index].prevout.n;
73 0 : if (input.non_witness_utxo) {
74 0 : if (prevout_index >= input.non_witness_utxo->vout.size()) {
75 0 : return false;
76 : }
77 0 : if (input.non_witness_utxo->GetHash() != tx->vin[input_index].prevout.hash) {
78 0 : return false;
79 : }
80 0 : utxo = input.non_witness_utxo->vout[prevout_index];
81 0 : } else if (!input.witness_utxo.IsNull()) {
82 0 : utxo = input.witness_utxo;
83 0 : } else {
84 0 : return false;
85 : }
86 0 : return true;
87 0 : }
88 :
89 0 : bool PSBTInput::IsNull() const
90 : {
91 0 : return !non_witness_utxo && witness_utxo.IsNull() && partial_sigs.empty() && unknown.empty() && hd_keypaths.empty() && redeem_script.empty() && witness_script.empty();
92 : }
93 :
94 0 : void PSBTInput::FillSignatureData(SignatureData& sigdata) const
95 : {
96 0 : if (!final_script_sig.empty()) {
97 0 : sigdata.scriptSig = final_script_sig;
98 0 : sigdata.complete = true;
99 0 : }
100 0 : if (!final_script_witness.IsNull()) {
101 0 : sigdata.scriptWitness = final_script_witness;
102 0 : sigdata.complete = true;
103 0 : }
104 0 : if (sigdata.complete) {
105 0 : return;
106 : }
107 :
108 0 : sigdata.signatures.insert(partial_sigs.begin(), partial_sigs.end());
109 0 : if (!redeem_script.empty()) {
110 0 : sigdata.redeem_script = redeem_script;
111 0 : }
112 0 : if (!witness_script.empty()) {
113 0 : sigdata.witness_script = witness_script;
114 0 : }
115 0 : for (const auto& key_pair : hd_keypaths) {
116 0 : sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
117 : }
118 0 : if (!m_tap_key_sig.empty()) {
119 0 : sigdata.taproot_key_path_sig = m_tap_key_sig;
120 0 : }
121 0 : for (const auto& [pubkey_leaf, sig] : m_tap_script_sigs) {
122 0 : sigdata.taproot_script_sigs.emplace(pubkey_leaf, sig);
123 : }
124 0 : if (!m_tap_internal_key.IsNull()) {
125 0 : sigdata.tr_spenddata.internal_key = m_tap_internal_key;
126 0 : }
127 0 : if (!m_tap_merkle_root.IsNull()) {
128 0 : sigdata.tr_spenddata.merkle_root = m_tap_merkle_root;
129 0 : }
130 0 : for (const auto& [leaf_script, control_block] : m_tap_scripts) {
131 0 : sigdata.tr_spenddata.scripts.emplace(leaf_script, control_block);
132 : }
133 0 : for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
134 0 : sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
135 : }
136 0 : for (const auto& [hash, preimage] : ripemd160_preimages) {
137 0 : sigdata.ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
138 : }
139 0 : for (const auto& [hash, preimage] : sha256_preimages) {
140 0 : sigdata.sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
141 : }
142 0 : for (const auto& [hash, preimage] : hash160_preimages) {
143 0 : sigdata.hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
144 : }
145 0 : for (const auto& [hash, preimage] : hash256_preimages) {
146 0 : sigdata.hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
147 : }
148 0 : }
149 :
150 0 : void PSBTInput::FromSignatureData(const SignatureData& sigdata)
151 : {
152 0 : if (sigdata.complete) {
153 0 : partial_sigs.clear();
154 0 : hd_keypaths.clear();
155 0 : redeem_script.clear();
156 0 : witness_script.clear();
157 :
158 0 : if (!sigdata.scriptSig.empty()) {
159 0 : final_script_sig = sigdata.scriptSig;
160 0 : }
161 0 : if (!sigdata.scriptWitness.IsNull()) {
162 0 : final_script_witness = sigdata.scriptWitness;
163 0 : }
164 0 : return;
165 : }
166 :
167 0 : partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end());
168 0 : if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
169 0 : redeem_script = sigdata.redeem_script;
170 0 : }
171 0 : if (witness_script.empty() && !sigdata.witness_script.empty()) {
172 0 : witness_script = sigdata.witness_script;
173 0 : }
174 0 : for (const auto& entry : sigdata.misc_pubkeys) {
175 0 : hd_keypaths.emplace(entry.second);
176 : }
177 0 : if (!sigdata.taproot_key_path_sig.empty()) {
178 0 : m_tap_key_sig = sigdata.taproot_key_path_sig;
179 0 : }
180 0 : for (const auto& [pubkey_leaf, sig] : sigdata.taproot_script_sigs) {
181 0 : m_tap_script_sigs.emplace(pubkey_leaf, sig);
182 : }
183 0 : if (!sigdata.tr_spenddata.internal_key.IsNull()) {
184 0 : m_tap_internal_key = sigdata.tr_spenddata.internal_key;
185 0 : }
186 0 : if (!sigdata.tr_spenddata.merkle_root.IsNull()) {
187 0 : m_tap_merkle_root = sigdata.tr_spenddata.merkle_root;
188 0 : }
189 0 : for (const auto& [leaf_script, control_block] : sigdata.tr_spenddata.scripts) {
190 0 : m_tap_scripts.emplace(leaf_script, control_block);
191 : }
192 0 : for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
193 0 : m_tap_bip32_paths.emplace(pubkey, leaf_origin);
194 : }
195 0 : }
196 :
197 0 : void PSBTInput::Merge(const PSBTInput& input)
198 : {
199 0 : if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
200 0 : if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
201 0 : witness_utxo = input.witness_utxo;
202 0 : }
203 :
204 0 : partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end());
205 0 : ripemd160_preimages.insert(input.ripemd160_preimages.begin(), input.ripemd160_preimages.end());
206 0 : sha256_preimages.insert(input.sha256_preimages.begin(), input.sha256_preimages.end());
207 0 : hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end());
208 0 : hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end());
209 0 : hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
210 0 : unknown.insert(input.unknown.begin(), input.unknown.end());
211 0 : m_tap_script_sigs.insert(input.m_tap_script_sigs.begin(), input.m_tap_script_sigs.end());
212 0 : m_tap_scripts.insert(input.m_tap_scripts.begin(), input.m_tap_scripts.end());
213 0 : m_tap_bip32_paths.insert(input.m_tap_bip32_paths.begin(), input.m_tap_bip32_paths.end());
214 :
215 0 : if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script;
216 0 : if (witness_script.empty() && !input.witness_script.empty()) witness_script = input.witness_script;
217 0 : if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig;
218 0 : if (final_script_witness.IsNull() && !input.final_script_witness.IsNull()) final_script_witness = input.final_script_witness;
219 0 : if (m_tap_key_sig.empty() && !input.m_tap_key_sig.empty()) m_tap_key_sig = input.m_tap_key_sig;
220 0 : if (m_tap_internal_key.IsNull() && !input.m_tap_internal_key.IsNull()) m_tap_internal_key = input.m_tap_internal_key;
221 0 : if (m_tap_merkle_root.IsNull() && !input.m_tap_merkle_root.IsNull()) m_tap_merkle_root = input.m_tap_merkle_root;
222 0 : }
223 :
224 0 : void PSBTOutput::FillSignatureData(SignatureData& sigdata) const
225 : {
226 0 : if (!redeem_script.empty()) {
227 0 : sigdata.redeem_script = redeem_script;
228 0 : }
229 0 : if (!witness_script.empty()) {
230 0 : sigdata.witness_script = witness_script;
231 0 : }
232 0 : for (const auto& key_pair : hd_keypaths) {
233 0 : sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
234 : }
235 0 : if (!m_tap_tree.empty() && m_tap_internal_key.IsFullyValid()) {
236 0 : TaprootBuilder builder;
237 0 : for (const auto& [depth, leaf_ver, script] : m_tap_tree) {
238 0 : builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true);
239 : }
240 0 : assert(builder.IsComplete());
241 0 : builder.Finalize(m_tap_internal_key);
242 0 : TaprootSpendData spenddata = builder.GetSpendData();
243 :
244 0 : sigdata.tr_spenddata.internal_key = m_tap_internal_key;
245 0 : sigdata.tr_spenddata.Merge(spenddata);
246 0 : }
247 0 : for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
248 0 : sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
249 : }
250 0 : }
251 :
252 0 : void PSBTOutput::FromSignatureData(const SignatureData& sigdata)
253 : {
254 0 : if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
255 0 : redeem_script = sigdata.redeem_script;
256 0 : }
257 0 : if (witness_script.empty() && !sigdata.witness_script.empty()) {
258 0 : witness_script = sigdata.witness_script;
259 0 : }
260 0 : for (const auto& entry : sigdata.misc_pubkeys) {
261 0 : hd_keypaths.emplace(entry.second);
262 : }
263 0 : if (!sigdata.tr_spenddata.internal_key.IsNull()) {
264 0 : m_tap_internal_key = sigdata.tr_spenddata.internal_key;
265 0 : }
266 0 : if (sigdata.tr_builder.has_value() && sigdata.tr_builder->HasScripts()) {
267 0 : m_tap_tree = sigdata.tr_builder->GetTreeTuples();
268 0 : }
269 0 : for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
270 0 : m_tap_bip32_paths.emplace(pubkey, leaf_origin);
271 : }
272 0 : }
273 :
274 0 : bool PSBTOutput::IsNull() const
275 : {
276 0 : return redeem_script.empty() && witness_script.empty() && hd_keypaths.empty() && unknown.empty();
277 : }
278 :
279 0 : void PSBTOutput::Merge(const PSBTOutput& output)
280 : {
281 0 : hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end());
282 0 : unknown.insert(output.unknown.begin(), output.unknown.end());
283 0 : m_tap_bip32_paths.insert(output.m_tap_bip32_paths.begin(), output.m_tap_bip32_paths.end());
284 :
285 0 : if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
286 0 : if (witness_script.empty() && !output.witness_script.empty()) witness_script = output.witness_script;
287 0 : if (m_tap_internal_key.IsNull() && !output.m_tap_internal_key.IsNull()) m_tap_internal_key = output.m_tap_internal_key;
288 0 : if (m_tap_tree.empty() && !output.m_tap_tree.empty()) m_tap_tree = output.m_tap_tree;
289 0 : }
290 :
291 0 : bool PSBTInputSigned(const PSBTInput& input)
292 : {
293 0 : return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
294 : }
295 :
296 0 : bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
297 : {
298 0 : CTxOut utxo;
299 0 : assert(psbt.inputs.size() >= input_index);
300 0 : const PSBTInput& input = psbt.inputs[input_index];
301 :
302 0 : if (input.non_witness_utxo) {
303 : // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
304 0 : COutPoint prevout = psbt.tx->vin[input_index].prevout;
305 0 : if (prevout.n >= input.non_witness_utxo->vout.size()) {
306 0 : return false;
307 : }
308 0 : if (input.non_witness_utxo->GetHash() != prevout.hash) {
309 0 : return false;
310 : }
311 0 : utxo = input.non_witness_utxo->vout[prevout.n];
312 0 : } else if (!input.witness_utxo.IsNull()) {
313 0 : utxo = input.witness_utxo;
314 0 : } else {
315 0 : return false;
316 : }
317 :
318 0 : if (txdata) {
319 0 : return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL});
320 : } else {
321 0 : return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, MissingDataBehavior::FAIL});
322 : }
323 0 : }
324 :
325 0 : size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
326 0 : size_t count = 0;
327 0 : for (const auto& input : psbt.inputs) {
328 0 : if (!PSBTInputSigned(input)) {
329 0 : count++;
330 0 : }
331 : }
332 :
333 0 : return count;
334 : }
335 :
336 0 : void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
337 : {
338 0 : CMutableTransaction& tx = *Assert(psbt.tx);
339 0 : const CTxOut& out = tx.vout.at(index);
340 0 : PSBTOutput& psbt_out = psbt.outputs.at(index);
341 :
342 : // Fill a SignatureData with output info
343 0 : SignatureData sigdata;
344 0 : psbt_out.FillSignatureData(sigdata);
345 :
346 : // Construct a would-be spend of this output, to update sigdata with.
347 : // Note that ProduceSignature is used to fill in metadata (not actual signatures),
348 : // so provider does not need to provide any private keys (it can be a HidingSigningProvider).
349 0 : MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, SIGHASH_ALL);
350 0 : ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
351 :
352 : // Put redeem_script, witness_script, key paths, into PSBTOutput.
353 0 : psbt_out.FromSignatureData(sigdata);
354 0 : }
355 :
356 0 : PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& psbt)
357 : {
358 0 : const CMutableTransaction& tx = *psbt.tx;
359 0 : bool have_all_spent_outputs = true;
360 0 : std::vector<CTxOut> utxos(tx.vin.size());
361 0 : for (size_t idx = 0; idx < tx.vin.size(); ++idx) {
362 0 : if (!psbt.GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs = false;
363 0 : }
364 0 : PrecomputedTransactionData txdata;
365 0 : if (have_all_spent_outputs) {
366 0 : txdata.Init(tx, std::move(utxos), true);
367 0 : } else {
368 0 : txdata.Init(tx, {}, true);
369 : }
370 0 : return txdata;
371 0 : }
372 :
373 0 : bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, int sighash, SignatureData* out_sigdata, bool finalize)
374 : {
375 0 : PSBTInput& input = psbt.inputs.at(index);
376 0 : const CMutableTransaction& tx = *psbt.tx;
377 :
378 0 : if (PSBTInputSignedAndVerified(psbt, index, txdata)) {
379 0 : return true;
380 : }
381 :
382 : // Fill SignatureData with input info
383 0 : SignatureData sigdata;
384 0 : input.FillSignatureData(sigdata);
385 :
386 : // Get UTXO
387 0 : bool require_witness_sig = false;
388 0 : CTxOut utxo;
389 :
390 0 : if (input.non_witness_utxo) {
391 : // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
392 0 : COutPoint prevout = tx.vin[index].prevout;
393 0 : if (prevout.n >= input.non_witness_utxo->vout.size()) {
394 0 : return false;
395 : }
396 0 : if (input.non_witness_utxo->GetHash() != prevout.hash) {
397 0 : return false;
398 : }
399 0 : utxo = input.non_witness_utxo->vout[prevout.n];
400 0 : } else if (!input.witness_utxo.IsNull()) {
401 0 : utxo = input.witness_utxo;
402 : // When we're taking our information from a witness UTXO, we can't verify it is actually data from
403 : // the output being spent. This is safe in case a witness signature is produced (which includes this
404 : // information directly in the hash), but not for non-witness signatures. Remember that we require
405 : // a witness signature in this situation.
406 0 : require_witness_sig = true;
407 0 : } else {
408 0 : return false;
409 : }
410 :
411 0 : sigdata.witness = false;
412 : bool sig_complete;
413 0 : if (txdata == nullptr) {
414 0 : sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
415 0 : } else {
416 0 : MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, sighash);
417 0 : sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
418 0 : }
419 : // Verify that a witness signature was produced in case one was required.
420 0 : if (require_witness_sig && !sigdata.witness) return false;
421 :
422 : // If we are not finalizing, set sigdata.complete to false to not set the scriptWitness
423 0 : if (!finalize && sigdata.complete) sigdata.complete = false;
424 :
425 0 : input.FromSignatureData(sigdata);
426 :
427 : // If we have a witness signature, put a witness UTXO.
428 0 : if (sigdata.witness) {
429 0 : input.witness_utxo = utxo;
430 : // We can remove the non_witness_utxo if and only if there are no non-segwit or segwit v0
431 : // inputs in this transaction. Since this requires inspecting the entire transaction, this
432 : // is something for the caller to deal with (i.e. FillPSBT).
433 0 : }
434 :
435 : // Fill in the missing info
436 0 : if (out_sigdata) {
437 0 : out_sigdata->missing_pubkeys = sigdata.missing_pubkeys;
438 0 : out_sigdata->missing_sigs = sigdata.missing_sigs;
439 0 : out_sigdata->missing_redeem_script = sigdata.missing_redeem_script;
440 0 : out_sigdata->missing_witness_script = sigdata.missing_witness_script;
441 0 : }
442 :
443 0 : return sig_complete;
444 0 : }
445 :
446 0 : void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx, const int& sighash_type)
447 : {
448 : // Only drop non_witness_utxos if sighash_type != SIGHASH_ANYONECANPAY
449 0 : if ((sighash_type & 0x80) != SIGHASH_ANYONECANPAY) {
450 : // Figure out if any non_witness_utxos should be dropped
451 0 : std::vector<unsigned int> to_drop;
452 0 : for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
453 0 : const auto& input = psbtx.inputs.at(i);
454 : int wit_ver;
455 0 : std::vector<unsigned char> wit_prog;
456 0 : if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
457 : // There's a non-segwit input or Segwit v0, so we cannot drop any witness_utxos
458 0 : to_drop.clear();
459 0 : break;
460 : }
461 0 : if (wit_ver == 0) {
462 : // Segwit v0, so we cannot drop any non_witness_utxos
463 0 : to_drop.clear();
464 0 : break;
465 : }
466 0 : if (input.non_witness_utxo) {
467 0 : to_drop.push_back(i);
468 0 : }
469 0 : }
470 :
471 : // Drop the non_witness_utxos that we can drop
472 0 : for (unsigned int i : to_drop) {
473 0 : psbtx.inputs.at(i).non_witness_utxo = nullptr;
474 : }
475 0 : }
476 0 : }
477 :
478 0 : bool FinalizePSBT(PartiallySignedTransaction& psbtx)
479 : {
480 : // Finalize input signatures -- in case we have partial signatures that add up to a complete
481 : // signature, but have not combined them yet (e.g. because the combiner that created this
482 : // PartiallySignedTransaction did not understand them), this will combine them into a final
483 : // script.
484 0 : bool complete = true;
485 0 : const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);
486 0 : for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
487 0 : complete &= SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, SIGHASH_ALL, nullptr, true);
488 0 : }
489 :
490 0 : return complete;
491 0 : }
492 :
493 0 : bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result)
494 : {
495 : // It's not safe to extract a PSBT that isn't finalized, and there's no easy way to check
496 : // whether a PSBT is finalized without finalizing it, so we just do this.
497 0 : if (!FinalizePSBT(psbtx)) {
498 0 : return false;
499 : }
500 :
501 0 : result = *psbtx.tx;
502 0 : for (unsigned int i = 0; i < result.vin.size(); ++i) {
503 0 : result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig;
504 0 : result.vin[i].scriptWitness = psbtx.inputs[i].final_script_witness;
505 0 : }
506 0 : return true;
507 0 : }
508 :
509 0 : TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
510 : {
511 0 : out = psbtxs[0]; // Copy the first one
512 :
513 : // Merge
514 0 : for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
515 0 : if (!out.Merge(*it)) {
516 0 : return TransactionError::PSBT_MISMATCH;
517 : }
518 0 : }
519 0 : return TransactionError::OK;
520 0 : }
521 :
522 0 : std::string PSBTRoleName(PSBTRole role) {
523 0 : switch (role) {
524 0 : case PSBTRole::CREATOR: return "creator";
525 0 : case PSBTRole::UPDATER: return "updater";
526 0 : case PSBTRole::SIGNER: return "signer";
527 0 : case PSBTRole::FINALIZER: return "finalizer";
528 0 : case PSBTRole::EXTRACTOR: return "extractor";
529 : // no default case, so the compiler can warn about missing cases
530 : }
531 0 : assert(false);
532 0 : }
533 :
534 0 : bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
535 : {
536 0 : auto tx_data = DecodeBase64(base64_tx);
537 0 : if (!tx_data) {
538 0 : error = "invalid base64";
539 0 : return false;
540 : }
541 0 : return DecodeRawPSBT(psbt, MakeByteSpan(*tx_data), error);
542 0 : }
543 :
544 0 : bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span<const std::byte> tx_data, std::string& error)
545 : {
546 0 : CDataStream ss_data(tx_data, SER_NETWORK, PROTOCOL_VERSION);
547 : try {
548 0 : ss_data >> psbt;
549 0 : if (!ss_data.empty()) {
550 0 : error = "extra data after PSBT";
551 0 : return false;
552 : }
553 0 : } catch (const std::exception& e) {
554 0 : error = e.what();
555 0 : return false;
556 0 : }
557 0 : return true;
558 0 : }
559 :
560 0 : uint32_t PartiallySignedTransaction::GetVersion() const
561 : {
562 0 : if (m_version != std::nullopt) {
563 0 : return *m_version;
564 : }
565 0 : return 0;
566 0 : }
|