Branch data Line data Source code
1 : : // Copyright (c) 2009-present 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 <common/types.h>
8 : : #include <node/types.h>
9 : : #include <policy/policy.h>
10 : : #include <script/signingprovider.h>
11 : : #include <util/check.h>
12 : : #include <util/strencodings.h>
13 : :
14 : : using common::PSBTError;
15 : :
16 [ - + ]: 4149 : PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx) : tx(tx)
17 : : {
18 [ - + + - ]: 4149 : inputs.resize(tx.vin.size());
19 [ - + + - ]: 4149 : outputs.resize(tx.vout.size());
20 [ - - ]: 4149 : }
21 : :
22 : 3484 : bool PartiallySignedTransaction::IsNull() const
23 : : {
24 [ - + - - : 3484 : return !tx && inputs.empty() && outputs.empty() && unknown.empty();
- - - - ]
25 : : }
26 : :
27 : 10429 : bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
28 : : {
29 : : // Prohibited to merge two PSBTs over different transactions
30 [ + + ]: 10429 : if (tx->GetHash() != psbt.tx->GetHash()) {
31 : : return false;
32 : : }
33 : :
34 [ - + + + ]: 29473 : for (unsigned int i = 0; i < inputs.size(); ++i) {
35 : 19446 : inputs[i].Merge(psbt.inputs[i]);
36 : : }
37 [ - + + + ]: 34928 : for (unsigned int i = 0; i < outputs.size(); ++i) {
38 : 24901 : outputs[i].Merge(psbt.outputs[i]);
39 : : }
40 [ + + ]: 13803 : for (auto& xpub_pair : psbt.m_xpubs) {
41 [ + + ]: 3776 : if (m_xpubs.count(xpub_pair.first) == 0) {
42 : 596 : m_xpubs[xpub_pair.first] = xpub_pair.second;
43 : : } else {
44 : 3180 : m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
45 : : }
46 : : }
47 : 10027 : unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
48 : :
49 : 10027 : return true;
50 : : }
51 : :
52 : 8501 : bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin)
53 : : {
54 [ + + ]: 8501 : if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) {
55 : : return false;
56 : : }
57 : 1699 : tx->vin.push_back(txin);
58 : 1699 : psbtin.partial_sigs.clear();
59 : 1699 : psbtin.final_script_sig.clear();
60 : 1699 : psbtin.final_script_witness.SetNull();
61 : 1699 : inputs.push_back(psbtin);
62 : 1699 : return true;
63 : : }
64 : :
65 : 28143 : bool PartiallySignedTransaction::AddOutput(const CTxOut& txout, const PSBTOutput& psbtout)
66 : : {
67 : 28143 : tx->vout.push_back(txout);
68 : 28143 : outputs.push_back(psbtout);
69 : 28143 : return true;
70 : : }
71 : :
72 : 41906 : bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const
73 : : {
74 [ + + ]: 41906 : const PSBTInput& input = inputs[input_index];
75 [ + + ]: 41906 : uint32_t prevout_index = tx->vin[input_index].prevout.n;
76 [ + + ]: 41906 : if (input.non_witness_utxo) {
77 [ - + + - ]: 670 : if (prevout_index >= input.non_witness_utxo->vout.size()) {
78 : : return false;
79 : : }
80 [ + - ]: 670 : if (input.non_witness_utxo->GetHash() != tx->vin[input_index].prevout.hash) {
81 : : return false;
82 : : }
83 : 670 : utxo = input.non_witness_utxo->vout[prevout_index];
84 [ + + ]: 41236 : } else if (!input.witness_utxo.IsNull()) {
85 : 23141 : utxo = input.witness_utxo;
86 : : } else {
87 : : return false;
88 : : }
89 : : return true;
90 : : }
91 : :
92 : 6871 : bool PSBTInput::IsNull() const
93 : : {
94 [ + + + + : 6888 : return !non_witness_utxo && witness_utxo.IsNull() && partial_sigs.empty() && unknown.empty() && hd_keypaths.empty() && redeem_script.empty() && witness_script.empty();
+ + + + +
+ + + + +
+ + + + ]
95 : : }
96 : :
97 : 24217 : void PSBTInput::FillSignatureData(SignatureData& sigdata) const
98 : : {
99 [ + + + + ]: 28949 : if (!final_script_sig.empty()) {
100 : 5225 : sigdata.scriptSig = final_script_sig;
101 : 5225 : sigdata.complete = true;
102 : : }
103 [ + + ]: 24217 : if (!final_script_witness.IsNull()) {
104 : 359 : sigdata.scriptWitness = final_script_witness;
105 : 359 : sigdata.complete = true;
106 : : }
107 [ + + ]: 24217 : if (sigdata.complete) {
108 : : return;
109 : : }
110 : :
111 : 18640 : sigdata.signatures.insert(partial_sigs.begin(), partial_sigs.end());
112 [ + + + + ]: 18856 : if (!redeem_script.empty()) {
113 : 555 : sigdata.redeem_script = redeem_script;
114 : : }
115 [ + + + + ]: 18962 : if (!witness_script.empty()) {
116 : 836 : sigdata.witness_script = witness_script;
117 : : }
118 [ + + ]: 20990 : for (const auto& key_pair : hd_keypaths) {
119 : 2350 : sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
120 : : }
121 [ + + ]: 18640 : if (!m_tap_key_sig.empty()) {
122 : 410 : sigdata.taproot_key_path_sig = m_tap_key_sig;
123 : : }
124 [ + + ]: 21241 : for (const auto& [pubkey_leaf, sig] : m_tap_script_sigs) {
125 : 2601 : sigdata.taproot_script_sigs.emplace(pubkey_leaf, sig);
126 : : }
127 [ + + ]: 37280 : if (!m_tap_internal_key.IsNull()) {
128 : 191 : sigdata.tr_spenddata.internal_key = m_tap_internal_key;
129 : : }
130 [ + + ]: 37280 : if (!m_tap_merkle_root.IsNull()) {
131 : 176 : sigdata.tr_spenddata.merkle_root = m_tap_merkle_root;
132 : : }
133 [ + + ]: 28371 : for (const auto& [leaf_script, control_block] : m_tap_scripts) {
134 : 9731 : sigdata.tr_spenddata.scripts.emplace(leaf_script, control_block);
135 : : }
136 [ + + ]: 20423 : for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
137 : 1783 : sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
138 : 1783 : sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
139 : : }
140 [ + + ]: 19794 : for (const auto& [hash, preimage] : ripemd160_preimages) {
141 [ + - ]: 2308 : sigdata.ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
142 : : }
143 [ + + ]: 23615 : for (const auto& [hash, preimage] : sha256_preimages) {
144 [ + - ]: 9950 : sigdata.sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
145 : : }
146 [ + + ]: 21806 : for (const auto& [hash, preimage] : hash160_preimages) {
147 [ + - ]: 6332 : sigdata.hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
148 : : }
149 [ + + ]: 20549 : for (const auto& [hash, preimage] : hash256_preimages) {
150 [ + - ]: 3818 : sigdata.hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
151 : : }
152 : : }
153 : :
154 : 4492 : void PSBTInput::FromSignatureData(const SignatureData& sigdata)
155 : : {
156 [ + + ]: 4492 : if (sigdata.complete) {
157 : 479 : partial_sigs.clear();
158 : 479 : hd_keypaths.clear();
159 : 479 : redeem_script.clear();
160 : 479 : witness_script.clear();
161 : :
162 [ + + + + ]: 632 : if (!sigdata.scriptSig.empty()) {
163 : 233 : final_script_sig = sigdata.scriptSig;
164 : : }
165 [ + + ]: 479 : if (!sigdata.scriptWitness.IsNull()) {
166 : 264 : final_script_witness = sigdata.scriptWitness;
167 : : }
168 : 479 : return;
169 : : }
170 : :
171 : 4013 : partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end());
172 [ + + + + : 4115 : if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
+ + + + ]
173 : 8 : redeem_script = sigdata.redeem_script;
174 : : }
175 [ + + + + : 4097 : if (witness_script.empty() && !sigdata.witness_script.empty()) {
- + + + ]
176 : 2 : witness_script = sigdata.witness_script;
177 : : }
178 [ + + ]: 5657 : for (const auto& entry : sigdata.misc_pubkeys) {
179 : 1644 : hd_keypaths.emplace(entry.second);
180 : : }
181 [ + + ]: 4013 : if (!sigdata.taproot_key_path_sig.empty()) {
182 : 191 : m_tap_key_sig = sigdata.taproot_key_path_sig;
183 : : }
184 [ + + ]: 4135 : for (const auto& [pubkey_leaf, sig] : sigdata.taproot_script_sigs) {
185 : 122 : m_tap_script_sigs.emplace(pubkey_leaf, sig);
186 : : }
187 [ + + ]: 8026 : if (!sigdata.tr_spenddata.internal_key.IsNull()) {
188 : 17 : m_tap_internal_key = sigdata.tr_spenddata.internal_key;
189 : : }
190 [ + + ]: 8026 : if (!sigdata.tr_spenddata.merkle_root.IsNull()) {
191 : 98 : m_tap_merkle_root = sigdata.tr_spenddata.merkle_root;
192 : : }
193 [ + + ]: 6123 : for (const auto& [leaf_script, control_block] : sigdata.tr_spenddata.scripts) {
194 : 2110 : m_tap_scripts.emplace(leaf_script, control_block);
195 : : }
196 [ + + ]: 4428 : for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
197 : 415 : m_tap_bip32_paths.emplace(pubkey, leaf_origin);
198 : : }
199 : : }
200 : :
201 : 19446 : void PSBTInput::Merge(const PSBTInput& input)
202 : : {
203 [ + + + + ]: 19446 : if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
204 [ + + + + ]: 19446 : if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
205 : 85 : witness_utxo = input.witness_utxo;
206 : : }
207 : :
208 : 19446 : partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end());
209 : 19446 : ripemd160_preimages.insert(input.ripemd160_preimages.begin(), input.ripemd160_preimages.end());
210 : 19446 : sha256_preimages.insert(input.sha256_preimages.begin(), input.sha256_preimages.end());
211 : 19446 : hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end());
212 : 19446 : hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end());
213 : 19446 : hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
214 : 19446 : unknown.insert(input.unknown.begin(), input.unknown.end());
215 : 19446 : m_tap_script_sigs.insert(input.m_tap_script_sigs.begin(), input.m_tap_script_sigs.end());
216 : 19446 : m_tap_scripts.insert(input.m_tap_scripts.begin(), input.m_tap_scripts.end());
217 : 19446 : m_tap_bip32_paths.insert(input.m_tap_bip32_paths.begin(), input.m_tap_bip32_paths.end());
218 : :
219 [ + + + + : 19648 : if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script;
+ + + + ]
220 [ + + + + : 19704 : if (witness_script.empty() && !input.witness_script.empty()) witness_script = input.witness_script;
+ + + + ]
221 [ + + + + : 22210 : if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig;
+ + + + ]
222 [ + + + + ]: 19446 : if (final_script_witness.IsNull() && !input.final_script_witness.IsNull()) final_script_witness = input.final_script_witness;
223 [ + + + + ]: 19446 : if (m_tap_key_sig.empty() && !input.m_tap_key_sig.empty()) m_tap_key_sig = input.m_tap_key_sig;
224 [ + + + + ]: 57000 : if (m_tap_internal_key.IsNull() && !input.m_tap_internal_key.IsNull()) m_tap_internal_key = input.m_tap_internal_key;
225 [ + + + + ]: 57783 : if (m_tap_merkle_root.IsNull() && !input.m_tap_merkle_root.IsNull()) m_tap_merkle_root = input.m_tap_merkle_root;
226 : 19446 : }
227 : :
228 : 917 : void PSBTOutput::FillSignatureData(SignatureData& sigdata) const
229 : : {
230 [ + + + + ]: 945 : if (!redeem_script.empty()) {
231 : 64 : sigdata.redeem_script = redeem_script;
232 : : }
233 [ + + + + ]: 937 : if (!witness_script.empty()) {
234 : 42 : sigdata.witness_script = witness_script;
235 : : }
236 [ + + ]: 1261 : for (const auto& key_pair : hd_keypaths) {
237 : 344 : sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
238 : : }
239 [ + + + + ]: 917 : if (!m_tap_tree.empty() && m_tap_internal_key.IsFullyValid()) {
240 : 24 : TaprootBuilder builder;
241 [ - + + + ]: 93 : for (const auto& [depth, leaf_ver, script] : m_tap_tree) {
242 [ - + + - ]: 69 : builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true);
243 : : }
244 [ - + ]: 24 : assert(builder.IsComplete());
245 [ + - ]: 24 : builder.Finalize(m_tap_internal_key);
246 [ + - ]: 24 : TaprootSpendData spenddata = builder.GetSpendData();
247 : :
248 : 24 : sigdata.tr_spenddata.internal_key = m_tap_internal_key;
249 [ + - + - ]: 48 : sigdata.tr_spenddata.Merge(spenddata);
250 : 24 : }
251 [ + + ]: 1075 : for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
252 : 158 : sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
253 : 158 : sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
254 : : }
255 : 917 : }
256 : :
257 : 917 : void PSBTOutput::FromSignatureData(const SignatureData& sigdata)
258 : : {
259 [ + + + + : 945 : if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
- + - + ]
260 : 0 : redeem_script = sigdata.redeem_script;
261 : : }
262 [ + + + + : 937 : if (witness_script.empty() && !sigdata.witness_script.empty()) {
- + - + ]
263 : 0 : witness_script = sigdata.witness_script;
264 : : }
265 [ + + ]: 1262 : for (const auto& entry : sigdata.misc_pubkeys) {
266 : 345 : hd_keypaths.emplace(entry.second);
267 : : }
268 [ + + ]: 1834 : if (!sigdata.tr_spenddata.internal_key.IsNull()) {
269 : 24 : m_tap_internal_key = sigdata.tr_spenddata.internal_key;
270 : : }
271 [ - + - - ]: 917 : if (sigdata.tr_builder.has_value() && sigdata.tr_builder->HasScripts()) {
272 : 0 : m_tap_tree = sigdata.tr_builder->GetTreeTuples();
273 : : }
274 [ + + ]: 1075 : for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
275 : 158 : m_tap_bip32_paths.emplace(pubkey, leaf_origin);
276 : : }
277 : 917 : }
278 : :
279 : 8744 : bool PSBTOutput::IsNull() const
280 : : {
281 [ + + + + : 9159 : return redeem_script.empty() && witness_script.empty() && hd_keypaths.empty() && unknown.empty();
+ + + + +
+ + + ]
282 : : }
283 : :
284 : 24901 : void PSBTOutput::Merge(const PSBTOutput& output)
285 : : {
286 : 24901 : hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end());
287 : 24901 : unknown.insert(output.unknown.begin(), output.unknown.end());
288 : 24901 : m_tap_bip32_paths.insert(output.m_tap_bip32_paths.begin(), output.m_tap_bip32_paths.end());
289 : :
290 [ + + + + : 25365 : if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
+ + + + ]
291 [ + + + + : 26131 : if (witness_script.empty() && !output.witness_script.empty()) witness_script = output.witness_script;
+ + + + ]
292 [ + + + + ]: 74081 : if (m_tap_internal_key.IsNull() && !output.m_tap_internal_key.IsNull()) m_tap_internal_key = output.m_tap_internal_key;
293 [ + + + + ]: 24901 : if (m_tap_tree.empty() && !output.m_tap_tree.empty()) m_tap_tree = output.m_tap_tree;
294 : 24901 : }
295 : :
296 : 17610 : bool PSBTInputSigned(const PSBTInput& input)
297 : : {
298 [ + + + + : 20179 : return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
+ + ]
299 : : }
300 : :
301 : 32711 : bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
302 : : {
303 : 32711 : CTxOut utxo;
304 [ - + - + ]: 32711 : assert(psbt.inputs.size() >= input_index);
305 [ + + ]: 32711 : const PSBTInput& input = psbt.inputs[input_index];
306 : :
307 [ + + ]: 32711 : if (input.non_witness_utxo) {
308 : : // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
309 [ - + ]: 533 : COutPoint prevout = psbt.tx->vin[input_index].prevout;
310 [ - + + - ]: 533 : if (prevout.n >= input.non_witness_utxo->vout.size()) {
311 : : return false;
312 : : }
313 [ + - ]: 533 : if (input.non_witness_utxo->GetHash() != prevout.hash) {
314 : : return false;
315 : : }
316 : 533 : utxo = input.non_witness_utxo->vout[prevout.n];
317 [ + + ]: 32178 : } else if (!input.witness_utxo.IsNull()) {
318 : 18941 : utxo = input.witness_utxo;
319 : : } else {
320 : : return false;
321 : : }
322 : :
323 [ + + ]: 19474 : if (txdata) {
324 [ + - ]: 17532 : 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});
325 : : } else {
326 [ + - ]: 1942 : 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});
327 : : }
328 : 32711 : }
329 : :
330 : 3484 : size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
331 : 3484 : size_t count = 0;
332 [ + + ]: 10355 : for (const auto& input : psbt.inputs) {
333 [ + + ]: 6871 : if (!PSBTInputSigned(input)) {
334 : 5364 : count++;
335 : : }
336 : : }
337 : :
338 : 3484 : return count;
339 : : }
340 : :
341 : 917 : void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
342 : : {
343 : 917 : CMutableTransaction& tx = *Assert(psbt.tx);
344 : 917 : const CTxOut& out = tx.vout.at(index);
345 : 917 : PSBTOutput& psbt_out = psbt.outputs.at(index);
346 : :
347 : : // Fill a SignatureData with output info
348 : 917 : SignatureData sigdata;
349 [ + - ]: 917 : psbt_out.FillSignatureData(sigdata);
350 : :
351 : : // Construct a would-be spend of this output, to update sigdata with.
352 : : // Note that ProduceSignature is used to fill in metadata (not actual signatures),
353 : : // so provider does not need to provide any private keys (it can be a HidingSigningProvider).
354 [ + - ]: 917 : MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, SIGHASH_ALL);
355 [ + - ]: 917 : ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
356 : :
357 : : // Put redeem_script, witness_script, key paths, into PSBTOutput.
358 [ + - ]: 917 : psbt_out.FromSignatureData(sigdata);
359 : 917 : }
360 : :
361 : 16307 : PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& psbt)
362 : : {
363 [ - + ]: 16307 : const CMutableTransaction& tx = *psbt.tx;
364 : 16307 : bool have_all_spent_outputs = true;
365 [ - + ]: 16307 : std::vector<CTxOut> utxos(tx.vin.size());
366 [ - + + + ]: 42511 : for (size_t idx = 0; idx < tx.vin.size(); ++idx) {
367 [ + - + + ]: 26204 : if (!psbt.GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs = false;
368 : : }
369 : 16307 : PrecomputedTransactionData txdata;
370 [ + + ]: 16307 : if (have_all_spent_outputs) {
371 [ + - ]: 11527 : txdata.Init(tx, std::move(utxos), true);
372 : : } else {
373 [ + - ]: 4780 : txdata.Init(tx, {}, true);
374 : : }
375 : 16307 : return txdata;
376 : 16307 : }
377 : :
378 : 25025 : PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, std::optional<int> sighash, SignatureData* out_sigdata, bool finalize)
379 : : {
380 : 25025 : PSBTInput& input = psbt.inputs.at(index);
381 : 25025 : const CMutableTransaction& tx = *psbt.tx;
382 : :
383 [ + - + + ]: 25025 : if (PSBTInputSignedAndVerified(psbt, index, txdata)) {
384 : : return PSBTError::OK;
385 : : }
386 : :
387 : : // Fill SignatureData with input info
388 : 24217 : SignatureData sigdata;
389 [ + - ]: 24217 : input.FillSignatureData(sigdata);
390 : :
391 : : // Get UTXO
392 : 24217 : bool require_witness_sig = false;
393 : 24217 : CTxOut utxo;
394 : :
395 [ + + ]: 24217 : if (input.non_witness_utxo) {
396 : : // If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
397 [ - + ]: 420 : COutPoint prevout = tx.vin[index].prevout;
398 [ - + + - ]: 420 : if (prevout.n >= input.non_witness_utxo->vout.size()) {
399 : : return PSBTError::MISSING_INPUTS;
400 : : }
401 [ + - ]: 420 : if (input.non_witness_utxo->GetHash() != prevout.hash) {
402 : : return PSBTError::MISSING_INPUTS;
403 : : }
404 : 420 : utxo = input.non_witness_utxo->vout[prevout.n];
405 [ + + ]: 23797 : } else if (!input.witness_utxo.IsNull()) {
406 : 13977 : utxo = input.witness_utxo;
407 : : // When we're taking our information from a witness UTXO, we can't verify it is actually data from
408 : : // the output being spent. This is safe in case a witness signature is produced (which includes this
409 : : // information directly in the hash), but not for non-witness signatures. Remember that we require
410 : : // a witness signature in this situation.
411 : 13977 : require_witness_sig = true;
412 : : } else {
413 : : return PSBTError::MISSING_INPUTS;
414 : : }
415 : :
416 : : // Get the sighash type
417 : : // If both the field and the parameter are provided, they must match
418 : : // If only the parameter is provided, use it and add it to the PSBT if it is other than SIGHASH_DEFAULT
419 : : // for all input types, and not SIGHASH_ALL for non-taproot input types.
420 : : // If neither are provided, use SIGHASH_DEFAULT if it is taproot, and SIGHASH_ALL for everything else.
421 [ + + + - : 26122 : if (!sighash) sighash = utxo.scriptPubKey.IsPayToTaproot() ? SIGHASH_DEFAULT : SIGHASH_ALL;
+ + ]
422 [ + - ]: 14397 : Assert(sighash.has_value());
423 : : // For user safety, the desired sighash must be provided if the PSBT wants something other than the default set in the previous line.
424 [ + + + - ]: 38614 : if (input.sighash_type && input.sighash_type != sighash) {
425 : : return PSBTError::SIGHASH_MISMATCH;
426 : : }
427 : : // Set the PSBT sighash field when sighash is not DEFAULT or ALL
428 : : // DEFAULT is allowed for non-taproot inputs since DEFAULT may be passed for them (e.g. the psbt being signed also has taproot inputs)
429 : : // Note that signing already aliases DEFAULT to ALL for non-taproot inputs.
430 [ + - + + : 14359 : if (utxo.scriptPubKey.IsPayToTaproot() ? sighash != SIGHASH_DEFAULT :
+ - ]
431 [ + - + - ]: 25148 : (sighash != SIGHASH_DEFAULT && sighash != SIGHASH_ALL)) {
432 : 830 : input.sighash_type = sighash;
433 : : }
434 : :
435 : : // Check all existing signatures use the sighash type
436 [ + - ]: 14359 : if (sighash == SIGHASH_DEFAULT) {
437 [ + + - + : 1814 : if (!input.m_tap_key_sig.empty() && input.m_tap_key_sig.size() != 64) {
+ + ]
438 : : return PSBTError::SIGHASH_MISMATCH;
439 : : }
440 [ - + + + ]: 1901 : for (const auto& [_, sig] : input.m_tap_script_sigs) {
441 [ - + + + ]: 126 : if (sig.size() != 64) return PSBTError::SIGHASH_MISMATCH;
442 : : }
443 : : } else {
444 [ + + - + : 12545 : if (!input.m_tap_key_sig.empty() && (input.m_tap_key_sig.size() != 65 || input.m_tap_key_sig.back() != *sighash)) {
+ + + + ]
445 : : return PSBTError::SIGHASH_MISMATCH;
446 : : }
447 [ - + + + ]: 12521 : for (const auto& [_, sig] : input.m_tap_script_sigs) {
448 [ - + + + : 499 : if (sig.size() != 65 || sig.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
+ + ]
449 : : }
450 [ + + + + ]: 13420 : for (const auto& [_, sig] : input.partial_sigs) {
451 [ + + ]: 1412 : if (sig.second.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
452 : : }
453 : : }
454 : :
455 : 13783 : sigdata.witness = false;
456 : 13783 : bool sig_complete;
457 [ + + ]: 13783 : if (txdata == nullptr) {
458 [ + - ]: 1747 : sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
459 : : } else {
460 [ + - ]: 12036 : MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, *sighash);
461 [ + - ]: 12036 : sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
462 : 12036 : }
463 : : // Verify that a witness signature was produced in case one was required.
464 [ + + + + ]: 13783 : if (require_witness_sig && !sigdata.witness) return PSBTError::INCOMPLETE;
465 : :
466 : : // If we are not finalizing, set sigdata.complete to false to not set the scriptWitness
467 [ + + - + ]: 4492 : if (!finalize && sigdata.complete) sigdata.complete = false;
468 : :
469 [ + - ]: 4492 : input.FromSignatureData(sigdata);
470 : :
471 : : // If we have a witness signature, put a witness UTXO.
472 [ + + ]: 4492 : if (sigdata.witness) {
473 : 4218 : input.witness_utxo = utxo;
474 : : // We can remove the non_witness_utxo if and only if there are no non-segwit or segwit v0
475 : : // inputs in this transaction. Since this requires inspecting the entire transaction, this
476 : : // is something for the caller to deal with (i.e. FillPSBT).
477 : : }
478 : :
479 : : // Fill in the missing info
480 [ + + ]: 4492 : if (out_sigdata) {
481 [ + - ]: 1356 : out_sigdata->missing_pubkeys = sigdata.missing_pubkeys;
482 [ + - ]: 1356 : out_sigdata->missing_sigs = sigdata.missing_sigs;
483 : 1356 : out_sigdata->missing_redeem_script = sigdata.missing_redeem_script;
484 : 1356 : out_sigdata->missing_witness_script = sigdata.missing_witness_script;
485 : : }
486 : :
487 [ + + ]: 4492 : return sig_complete ? PSBTError::OK : PSBTError::INCOMPLETE;
488 : 24217 : }
489 : :
490 : 262 : void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx)
491 : : {
492 : : // Figure out if any non_witness_utxos should be dropped
493 : 262 : std::vector<unsigned int> to_drop;
494 [ - + + + ]: 409 : for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
495 [ + - ]: 306 : const auto& input = psbtx.inputs.at(i);
496 : 306 : int wit_ver;
497 : 306 : std::vector<unsigned char> wit_prog;
498 [ + + + - : 306 : if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
+ + ]
499 : : // There's a non-segwit input, so we cannot drop any non_witness_utxos
500 [ - + ]: 156 : to_drop.clear();
501 : : break;
502 : : }
503 [ + + ]: 150 : if (wit_ver == 0) {
504 : : // Segwit v0, so we cannot drop any non_witness_utxos
505 [ - + ]: 3 : to_drop.clear();
506 : : break;
507 : : }
508 : : // non_witness_utxos cannot be dropped if the sighash type includes SIGHASH_ANYONECANPAY
509 : : // Since callers should have called SignPSBTInput which updates the sighash type in the PSBT, we only
510 : : // need to look at that field. If it is not present, then we can assume SIGHASH_DEFAULT or SIGHASH_ALL.
511 [ - + - - ]: 147 : if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) == SIGHASH_ANYONECANPAY) {
512 [ - - ]: 159 : to_drop.clear();
513 : : break;
514 : : }
515 : :
516 [ - + ]: 147 : if (input.non_witness_utxo) {
517 [ # # ]: 0 : to_drop.push_back(i);
518 : : }
519 : 306 : }
520 : :
521 : : // Drop the non_witness_utxos that we can drop
522 [ - + ]: 262 : for (unsigned int i : to_drop) {
523 [ # # # # ]: 0 : psbtx.inputs.at(i).non_witness_utxo = nullptr;
524 : : }
525 : 262 : }
526 : :
527 : 7195 : bool FinalizePSBT(PartiallySignedTransaction& psbtx)
528 : : {
529 : : // Finalize input signatures -- in case we have partial signatures that add up to a complete
530 : : // signature, but have not combined them yet (e.g. because the combiner that created this
531 : : // PartiallySignedTransaction did not understand them), this will combine them into a final
532 : : // script.
533 : 7195 : bool complete = true;
534 : 7195 : const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);
535 [ - + + + ]: 21463 : for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
536 [ + - ]: 14268 : PSBTInput& input = psbtx.inputs.at(i);
537 [ + - ]: 14268 : complete &= (SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, input.sighash_type, nullptr, true) == PSBTError::OK);
538 : : }
539 : :
540 : 7195 : return complete;
541 : 7195 : }
542 : :
543 : 3711 : bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result)
544 : : {
545 : : // It's not safe to extract a PSBT that isn't finalized, and there's no easy way to check
546 : : // whether a PSBT is finalized without finalizing it, so we just do this.
547 [ + + ]: 3711 : if (!FinalizePSBT(psbtx)) {
548 : : return false;
549 : : }
550 : :
551 : 682 : result = *psbtx.tx;
552 [ - + + + ]: 994 : for (unsigned int i = 0; i < result.vin.size(); ++i) {
553 : 312 : result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig;
554 : 312 : result.vin[i].scriptWitness = psbtx.inputs[i].final_script_witness;
555 : : }
556 : : return true;
557 : : }
558 : :
559 : 3812 : bool CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
560 : : {
561 : 3812 : out = psbtxs[0]; // Copy the first one
562 : :
563 : : // Merge
564 [ + + ]: 10523 : for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
565 [ + + ]: 6945 : if (!out.Merge(*it)) {
566 : : return false;
567 : : }
568 : : }
569 : : return true;
570 : : }
571 : :
572 : 11026 : std::string PSBTRoleName(PSBTRole role) {
573 [ + + + + : 11026 : switch (role) {
+ - ]
574 : 1452 : case PSBTRole::CREATOR: return "creator";
575 : 8850 : case PSBTRole::UPDATER: return "updater";
576 : 187 : case PSBTRole::SIGNER: return "signer";
577 : 87 : case PSBTRole::FINALIZER: return "finalizer";
578 : 450 : case PSBTRole::EXTRACTOR: return "extractor";
579 : : // no default case, so the compiler can warn about missing cases
580 : : }
581 : 0 : assert(false);
582 : : }
583 : :
584 : 13289 : bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
585 : : {
586 [ - + ]: 13289 : auto tx_data = DecodeBase64(base64_tx);
587 [ + + ]: 13289 : if (!tx_data) {
588 [ + - ]: 13289 : error = "invalid base64";
589 : : return false;
590 : : }
591 [ + - ]: 13213 : return DecodeRawPSBT(psbt, MakeByteSpan(*tx_data), error);
592 : 13289 : }
593 : :
594 : 20927 : bool DecodeRawPSBT(PartiallySignedTransaction& psbt, std::span<const std::byte> tx_data, std::string& error)
595 : : {
596 : 20927 : DataStream ss_data{tx_data};
597 : 20927 : try {
598 [ + + ]: 20927 : ss_data >> psbt;
599 [ - + + + ]: 16083 : if (!ss_data.empty()) {
600 [ + - ]: 20927 : error = "extra data after PSBT";
601 : : return false;
602 : : }
603 [ - + ]: 4844 : } catch (const std::exception& e) {
604 [ + - ]: 4844 : error = e.what();
605 : 4844 : return false;
606 : 4844 : }
607 : : return true;
608 : 20927 : }
609 : :
610 : 33984 : uint32_t PartiallySignedTransaction::GetVersion() const
611 : : {
612 [ + + ]: 33984 : if (m_version != std::nullopt) {
613 : 67 : return *m_version;
614 : : }
615 : : return 0;
616 : : }
|