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