/root/bitcoin/src/psbt.cpp
Line | Count | Source |
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 <primitives/transaction.h> |
11 | | #include <script/signingprovider.h> |
12 | | #include <util/check.h> |
13 | | #include <util/result.h> |
14 | | #include <util/strencodings.h> |
15 | | |
16 | | #include <algorithm> |
17 | | #include <set> |
18 | | |
19 | | using common::PSBTError; |
20 | | |
21 | 0 | PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx, uint32_t version) : m_version(version) |
22 | 0 | { |
23 | 0 | assert(m_version == 0 || m_version == 2); Branch (23:5): [True: 0, False: 0]
Branch (23:5): [True: 0, False: 0]
Branch (23:5): [True: 0, False: 0]
|
24 | | |
25 | 0 | tx_version = tx.version; |
26 | 0 | fallback_locktime = tx.nLockTime; |
27 | 0 | inputs.reserve(tx.vin.size()); |
28 | 0 | for (const CTxIn& input : tx.vin) { Branch (28:29): [True: 0, False: 0]
|
29 | 0 | inputs.emplace_back(GetVersion(), input.prevout.hash, input.prevout.n, input.nSequence); |
30 | 0 | } |
31 | 0 | outputs.reserve(tx.vout.size()); |
32 | 0 | for (const CTxOut& output : tx.vout) { Branch (32:31): [True: 0, False: 0]
|
33 | 0 | outputs.emplace_back(GetVersion(), output.nValue, output.scriptPubKey); |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | | bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt) |
38 | 0 | { |
39 | | // Prohibited to merge two PSBTs over different transactions |
40 | 0 | std::optional<Txid> this_id = GetUniqueID(); |
41 | 0 | std::optional<Txid> psbt_id = psbt.GetUniqueID(); |
42 | 0 | if (!this_id || !psbt_id || this_id != psbt_id) { Branch (42:9): [True: 0, False: 0]
Branch (42:21): [True: 0, False: 0]
Branch (42:33): [True: 0, False: 0]
|
43 | 0 | return false; |
44 | 0 | } |
45 | 0 | if (GetVersion() != psbt.GetVersion()) { Branch (45:9): [True: 0, False: 0]
|
46 | 0 | return false; |
47 | 0 | } |
48 | | |
49 | 0 | for (unsigned int i = 0; i < inputs.size(); ++i) { Branch (49:30): [True: 0, False: 0]
|
50 | 0 | inputs[i].Merge(psbt.inputs[i]); |
51 | 0 | } |
52 | 0 | for (unsigned int i = 0; i < outputs.size(); ++i) { Branch (52:30): [True: 0, False: 0]
|
53 | 0 | outputs[i].Merge(psbt.outputs[i]); |
54 | 0 | } |
55 | 0 | MergeGlobalXPubs(psbt); |
56 | 0 | if (fallback_locktime == std::nullopt && psbt.fallback_locktime != std::nullopt) fallback_locktime = psbt.fallback_locktime; Branch (56:9): [True: 0, False: 0]
Branch (56:46): [True: 0, False: 0]
|
57 | | |
58 | | // Set m_tx_modifiable only if either PSBT had it set |
59 | 0 | if (m_tx_modifiable.has_value() || psbt.m_tx_modifiable.has_value()) { Branch (59:9): [True: 0, False: 0]
Branch (59:40): [True: 0, False: 0]
|
60 | | // In general, we AND the modifiable flags |
61 | 0 | std::bitset<8> this_modifiable = m_tx_modifiable.value_or(0); |
62 | 0 | std::bitset<8> psbt_modifiable = psbt.m_tx_modifiable.value_or(0); |
63 | 0 | std::bitset<8> final_modifiable = this_modifiable & psbt_modifiable; |
64 | | // SIGHASH_SINGLE Modifiable (bit 2) needs to be bitwise OR'd |
65 | 0 | final_modifiable.set(2, this_modifiable[2] || psbt_modifiable[2]); Branch (65:33): [True: 0, False: 0]
Branch (65:55): [True: 0, False: 0]
|
66 | |
|
67 | 0 | m_tx_modifiable = final_modifiable; |
68 | 0 | } |
69 | |
|
70 | 0 | m_proprietary.insert(psbt.m_proprietary.begin(), psbt.m_proprietary.end()); |
71 | 0 | unknown.insert(psbt.unknown.begin(), psbt.unknown.end()); |
72 | |
|
73 | 0 | return true; |
74 | 0 | } |
75 | | |
76 | | void PartiallySignedTransaction::MergeGlobalXPubs(const PartiallySignedTransaction& psbt) |
77 | 0 | { |
78 | 0 | for (const auto& [origin, xpubs] : psbt.m_xpubs) { Branch (78:38): [True: 0, False: 0]
|
79 | 0 | for (const CExtPubKey& xpub : xpubs) { Branch (79:37): [True: 0, False: 0]
|
80 | 0 | const bool known{std::ranges::any_of(m_xpubs, [&](const auto& entry) { return entry.second.contains(xpub); })}; |
81 | 0 | if (!known) m_xpubs[origin].insert(xpub); Branch (81:17): [True: 0, False: 0]
|
82 | 0 | } |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | std::optional<uint32_t> PartiallySignedTransaction::ComputeTimeLock() const |
87 | 0 | { |
88 | 0 | if (GetVersion() >= 2) { Branch (88:9): [True: 0, False: 0]
|
89 | 0 | std::optional<uint32_t> time_lock{0}; |
90 | 0 | std::optional<uint32_t> height_lock{0}; |
91 | 0 | for (const PSBTInput& input : inputs) { Branch (91:37): [True: 0, False: 0]
|
92 | 0 | if (input.time_locktime.has_value() && !input.height_locktime.has_value()) { Branch (92:17): [True: 0, False: 0]
Branch (92:52): [True: 0, False: 0]
|
93 | 0 | height_lock.reset(); // Transaction can no longer have a height locktime |
94 | 0 | if (!time_lock.has_value()) { Branch (94:21): [True: 0, False: 0]
|
95 | 0 | return std::nullopt; |
96 | 0 | } |
97 | 0 | } else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) { Branch (97:24): [True: 0, False: 0]
Branch (97:60): [True: 0, False: 0]
|
98 | 0 | time_lock.reset(); // Transaction can no longer have a time locktime |
99 | 0 | if (!height_lock.has_value()) { Branch (99:21): [True: 0, False: 0]
|
100 | 0 | return std::nullopt; |
101 | 0 | } |
102 | 0 | } |
103 | 0 | if (input.time_locktime && time_lock.has_value()) { Branch (103:17): [True: 0, False: 0]
Branch (103:40): [True: 0, False: 0]
|
104 | 0 | time_lock = std::max(time_lock, input.time_locktime); |
105 | 0 | } |
106 | 0 | if (input.height_locktime && height_lock.has_value()) { Branch (106:17): [True: 0, False: 0]
Branch (106:42): [True: 0, False: 0]
|
107 | 0 | height_lock = std::max(height_lock, input.height_locktime); |
108 | 0 | } |
109 | 0 | } |
110 | 0 | if (height_lock.has_value() && *height_lock > 0) { Branch (110:13): [True: 0, False: 0]
Branch (110:40): [True: 0, False: 0]
|
111 | 0 | return *height_lock; |
112 | 0 | } |
113 | 0 | if (time_lock.has_value() && *time_lock > 0) { Branch (113:13): [True: 0, False: 0]
Branch (113:38): [True: 0, False: 0]
|
114 | 0 | return *time_lock; |
115 | 0 | } |
116 | 0 | } |
117 | 0 | return fallback_locktime.value_or(0); |
118 | 0 | } |
119 | | |
120 | | std::optional<CMutableTransaction> PartiallySignedTransaction::GetUnsignedTx() const |
121 | 0 | { |
122 | 0 | CMutableTransaction mtx; |
123 | 0 | mtx.version = tx_version; |
124 | 0 | std::optional<uint32_t> locktime = ComputeTimeLock(); |
125 | 0 | if (!locktime) { Branch (125:9): [True: 0, False: 0]
|
126 | 0 | return std::nullopt; |
127 | 0 | } |
128 | 0 | mtx.nLockTime = *locktime; |
129 | 0 | uint32_t max_sequence = CTxIn::SEQUENCE_FINAL; |
130 | 0 | for (const PSBTInput& input : inputs) { Branch (130:33): [True: 0, False: 0]
|
131 | 0 | CTxIn txin; |
132 | 0 | txin.prevout.hash = input.prev_txid; |
133 | 0 | txin.prevout.n = input.prev_out; |
134 | 0 | txin.nSequence = input.sequence.value_or(max_sequence); |
135 | 0 | mtx.vin.push_back(txin); |
136 | 0 | } |
137 | 0 | for (const PSBTOutput& output : outputs) { Branch (137:35): [True: 0, False: 0]
|
138 | 0 | CTxOut txout; |
139 | 0 | txout.nValue = output.amount; |
140 | 0 | txout.scriptPubKey = output.script; |
141 | 0 | mtx.vout.push_back(txout); |
142 | 0 | } |
143 | 0 | return mtx; |
144 | 0 | } |
145 | | |
146 | | std::optional<Txid> PartiallySignedTransaction::GetUniqueID() const |
147 | 0 | { |
148 | | // Get the unsigned transaction |
149 | 0 | std::optional<CMutableTransaction> mtx = GetUnsignedTx(); |
150 | 0 | if (!mtx) { Branch (150:9): [True: 0, False: 0]
|
151 | 0 | return std::nullopt; |
152 | 0 | } |
153 | 0 | if (GetVersion() >= 2) { Branch (153:9): [True: 0, False: 0]
|
154 | 0 | for (CTxIn& txin : mtx->vin) { Branch (154:26): [True: 0, False: 0]
|
155 | 0 | txin.nSequence = 0; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | return mtx->GetHash(); |
159 | 0 | } |
160 | | |
161 | | bool PartiallySignedTransaction::AddInput(const PSBTInput& psbtin) |
162 | 0 | { |
163 | | // The input being added must be for this PSBT's version |
164 | 0 | if (psbtin.GetVersion() != GetVersion()) { Branch (164:9): [True: 0, False: 0]
|
165 | 0 | return false; |
166 | 0 | } |
167 | | |
168 | | // Prevent duplicate inputs |
169 | 0 | if (std::find_if(inputs.begin(), inputs.end(), Branch (169:9): [True: 0, False: 0]
|
170 | 0 | [psbtin](const PSBTInput& psbt) { |
171 | 0 | return psbt.prev_txid == psbtin.prev_txid && psbt.prev_out == psbtin.prev_out; Branch (171:20): [True: 0, False: 0]
Branch (171:58): [True: 0, False: 0]
|
172 | 0 | } |
173 | 0 | ) != inputs.end()) { |
174 | 0 | return false; |
175 | 0 | } |
176 | | |
177 | 0 | if (GetVersion() < 2) { Branch (177:9): [True: 0, False: 0]
|
178 | | // This is a v0 psbt, so do the v0 AddInput |
179 | 0 | inputs.push_back(psbtin); |
180 | 0 | inputs.back().partial_sigs.clear(); |
181 | 0 | inputs.back().final_script_sig.clear(); |
182 | 0 | inputs.back().final_script_witness.SetNull(); |
183 | 0 | return true; |
184 | 0 | } |
185 | | |
186 | | // Check inputs modifiable flag |
187 | 0 | if (!m_tx_modifiable.has_value() || !m_tx_modifiable->test(0)) { Branch (187:9): [True: 0, False: 0]
Branch (187:41): [True: 0, False: 0]
|
188 | 0 | return false; |
189 | 0 | } |
190 | | |
191 | | // Determine if we need to iterate the inputs. |
192 | | // For now, we only do this if the new input has a required time lock. |
193 | | // BIP 370 states that we should also do this if m_tx_modifiable's bit 2 is set |
194 | | // (Has SIGHASH_SINGLE flag) but since we are only adding inputs at the end of the vector, |
195 | | // we don't care about that. |
196 | 0 | bool iterate_inputs = psbtin.time_locktime != std::nullopt || psbtin.height_locktime != std::nullopt; Branch (196:27): [True: 0, False: 0]
Branch (196:67): [True: 0, False: 0]
|
197 | 0 | if (iterate_inputs) { Branch (197:9): [True: 0, False: 0]
|
198 | 0 | std::optional<uint32_t> old_timelock = ComputeTimeLock(); |
199 | 0 | if (!old_timelock) { Branch (199:13): [True: 0, False: 0]
|
200 | 0 | return false; |
201 | 0 | } |
202 | | |
203 | 0 | std::optional<uint32_t> time_lock = psbtin.time_locktime; |
204 | 0 | std::optional<uint32_t> height_lock = psbtin.height_locktime; |
205 | 0 | bool has_sigs = false; |
206 | 0 | for (const PSBTInput& input : inputs) { Branch (206:37): [True: 0, False: 0]
|
207 | 0 | if (input.time_locktime.has_value() && !input.height_locktime.has_value()) { Branch (207:17): [True: 0, False: 0]
Branch (207:52): [True: 0, False: 0]
|
208 | 0 | height_lock.reset(); // Transaction can no longer have a height locktime |
209 | 0 | if (time_lock == std::nullopt) { Branch (209:21): [True: 0, False: 0]
|
210 | 0 | return false; |
211 | 0 | } |
212 | 0 | } else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) { Branch (212:24): [True: 0, False: 0]
Branch (212:60): [True: 0, False: 0]
|
213 | 0 | time_lock.reset(); // Transaction can no longer have a time locktime |
214 | 0 | if (height_lock == std::nullopt) { Branch (214:21): [True: 0, False: 0]
|
215 | 0 | return false; |
216 | 0 | } |
217 | 0 | } |
218 | 0 | if (input.time_locktime && time_lock.has_value()) { Branch (218:17): [True: 0, False: 0]
Branch (218:40): [True: 0, False: 0]
|
219 | 0 | time_lock = std::max(time_lock, input.time_locktime); |
220 | 0 | } |
221 | 0 | if (input.height_locktime && height_lock.has_value()) { Branch (221:17): [True: 0, False: 0]
Branch (221:42): [True: 0, False: 0]
|
222 | 0 | height_lock = std::max(height_lock, input.height_locktime); |
223 | 0 | } |
224 | 0 | if (input.HasSignatures()) { Branch (224:17): [True: 0, False: 0]
|
225 | 0 | has_sigs = true; |
226 | 0 | } |
227 | 0 | } |
228 | 0 | uint32_t new_timelock = fallback_locktime.value_or(0); |
229 | 0 | if (height_lock.has_value() && *height_lock > 0) { Branch (229:13): [True: 0, False: 0]
Branch (229:40): [True: 0, False: 0]
|
230 | 0 | new_timelock = *height_lock; |
231 | 0 | } else if (time_lock.has_value() && *time_lock > 0) { Branch (231:20): [True: 0, False: 0]
Branch (231:45): [True: 0, False: 0]
|
232 | 0 | new_timelock = *time_lock; |
233 | 0 | } |
234 | 0 | if (has_sigs && *old_timelock != new_timelock) { Branch (234:13): [True: 0, False: 0]
Branch (234:25): [True: 0, False: 0]
|
235 | 0 | return false; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | | // Add the input to the end |
240 | 0 | inputs.push_back(psbtin); |
241 | 0 | return true; |
242 | 0 | } |
243 | | |
244 | | bool PartiallySignedTransaction::AddOutput(const PSBTOutput& psbtout) |
245 | 0 | { |
246 | | // The output being added must be for this PSBT's version |
247 | 0 | if (psbtout.GetVersion() != GetVersion()) { Branch (247:9): [True: 0, False: 0]
|
248 | 0 | return false; |
249 | 0 | } |
250 | | |
251 | 0 | if (GetVersion() < 2) { Branch (251:9): [True: 0, False: 0]
|
252 | | // This is a v0 psbt, do the v0 AddOutput |
253 | 0 | outputs.push_back(psbtout); |
254 | 0 | return true; |
255 | 0 | } |
256 | | |
257 | | // No global tx, must be PSBTv2 |
258 | | // Check outputs are modifiable |
259 | 0 | if (!m_tx_modifiable.has_value() || !m_tx_modifiable->test(1)) { Branch (259:9): [True: 0, False: 0]
Branch (259:41): [True: 0, False: 0]
|
260 | 0 | return false; |
261 | 0 | } |
262 | 0 | outputs.push_back(psbtout); |
263 | |
|
264 | 0 | return true; |
265 | 0 | } |
266 | | |
267 | | bool PSBTInput::GetUTXO(CTxOut& utxo) const |
268 | 0 | { |
269 | 0 | if (non_witness_utxo) { Branch (269:9): [True: 0, False: 0]
|
270 | 0 | if (prev_out >= non_witness_utxo->vout.size()) { Branch (270:13): [True: 0, False: 0]
|
271 | 0 | return false; |
272 | 0 | } |
273 | 0 | if (non_witness_utxo->GetHash() != prev_txid) { Branch (273:13): [True: 0, False: 0]
|
274 | 0 | return false; |
275 | 0 | } |
276 | 0 | utxo = non_witness_utxo->vout[prev_out]; |
277 | 0 | } else if (!witness_utxo.IsNull()) { Branch (277:16): [True: 0, False: 0]
|
278 | 0 | utxo = witness_utxo; |
279 | 0 | } else { |
280 | 0 | return false; |
281 | 0 | } |
282 | 0 | return true; |
283 | 0 | } |
284 | | |
285 | | COutPoint PSBTInput::GetOutPoint() const |
286 | 0 | { |
287 | 0 | return COutPoint(prev_txid, prev_out); |
288 | 0 | } |
289 | | |
290 | | void PSBTInput::FillSignatureData(SignatureData& sigdata) const |
291 | 0 | { |
292 | 0 | if (!final_script_sig.empty()) { Branch (292:9): [True: 0, False: 0]
|
293 | 0 | sigdata.scriptSig = final_script_sig; |
294 | 0 | sigdata.complete = true; |
295 | 0 | } |
296 | 0 | if (!final_script_witness.IsNull()) { Branch (296:9): [True: 0, False: 0]
|
297 | 0 | sigdata.scriptWitness = final_script_witness; |
298 | 0 | sigdata.complete = true; |
299 | 0 | } |
300 | 0 | if (sigdata.complete) { Branch (300:9): [True: 0, False: 0]
|
301 | 0 | return; |
302 | 0 | } |
303 | | |
304 | 0 | sigdata.signatures.insert(partial_sigs.begin(), partial_sigs.end()); |
305 | 0 | if (!redeem_script.empty()) { Branch (305:9): [True: 0, False: 0]
|
306 | 0 | sigdata.redeem_script = redeem_script; |
307 | 0 | } |
308 | 0 | if (!witness_script.empty()) { Branch (308:9): [True: 0, False: 0]
|
309 | 0 | sigdata.witness_script = witness_script; |
310 | 0 | } |
311 | 0 | for (const auto& key_pair : hd_keypaths) { Branch (311:31): [True: 0, False: 0]
|
312 | 0 | sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair); |
313 | 0 | } |
314 | 0 | if (!m_tap_key_sig.empty()) { Branch (314:9): [True: 0, False: 0]
|
315 | 0 | sigdata.taproot_key_path_sig = m_tap_key_sig; |
316 | 0 | } |
317 | 0 | for (const auto& [pubkey_leaf, sig] : m_tap_script_sigs) { Branch (317:41): [True: 0, False: 0]
|
318 | 0 | sigdata.taproot_script_sigs.emplace(pubkey_leaf, sig); |
319 | 0 | } |
320 | 0 | if (!m_tap_internal_key.IsNull()) { Branch (320:9): [True: 0, False: 0]
|
321 | 0 | sigdata.tr_spenddata.internal_key = m_tap_internal_key; |
322 | 0 | } |
323 | 0 | if (!m_tap_merkle_root.IsNull()) { Branch (323:9): [True: 0, False: 0]
|
324 | 0 | sigdata.tr_spenddata.merkle_root = m_tap_merkle_root; |
325 | 0 | } |
326 | 0 | for (const auto& [leaf_script, control_block] : m_tap_scripts) { Branch (326:51): [True: 0, False: 0]
|
327 | 0 | sigdata.tr_spenddata.scripts.emplace(leaf_script, control_block); |
328 | 0 | } |
329 | 0 | for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) { Branch (329:44): [True: 0, False: 0]
|
330 | 0 | sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin); |
331 | 0 | sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey); |
332 | 0 | } |
333 | 0 | for (const auto& [hash, preimage] : ripemd160_preimages) { Branch (333:39): [True: 0, False: 0]
|
334 | 0 | sigdata.ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
335 | 0 | } |
336 | 0 | for (const auto& [hash, preimage] : sha256_preimages) { Branch (336:39): [True: 0, False: 0]
|
337 | 0 | sigdata.sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
338 | 0 | } |
339 | 0 | for (const auto& [hash, preimage] : hash160_preimages) { Branch (339:39): [True: 0, False: 0]
|
340 | 0 | sigdata.hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
341 | 0 | } |
342 | 0 | for (const auto& [hash, preimage] : hash256_preimages) { Branch (342:39): [True: 0, False: 0]
|
343 | 0 | sigdata.hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
344 | 0 | } |
345 | 0 | sigdata.musig2_pubkeys.insert(m_musig2_participants.begin(), m_musig2_participants.end()); |
346 | 0 | for (const auto& [agg_key_lh, pubnonces] : m_musig2_pubnonces) { Branch (346:46): [True: 0, False: 0]
|
347 | 0 | sigdata.musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end()); |
348 | 0 | } |
349 | 0 | for (const auto& [agg_key_lh, psigs] : m_musig2_partial_sigs) { Branch (349:42): [True: 0, False: 0]
|
350 | 0 | sigdata.musig2_partial_sigs[agg_key_lh].insert(psigs.begin(), psigs.end()); |
351 | 0 | } |
352 | 0 | } |
353 | | |
354 | | void PSBTInput::FromSignatureData(const SignatureData& sigdata) |
355 | 0 | { |
356 | 0 | if (sigdata.complete) { Branch (356:9): [True: 0, False: 0]
|
357 | 0 | partial_sigs.clear(); |
358 | 0 | hd_keypaths.clear(); |
359 | 0 | redeem_script.clear(); |
360 | 0 | witness_script.clear(); |
361 | |
|
362 | 0 | if (!sigdata.scriptSig.empty()) { Branch (362:13): [True: 0, False: 0]
|
363 | 0 | final_script_sig = sigdata.scriptSig; |
364 | 0 | } |
365 | 0 | if (!sigdata.scriptWitness.IsNull()) { Branch (365:13): [True: 0, False: 0]
|
366 | 0 | final_script_witness = sigdata.scriptWitness; |
367 | 0 | } |
368 | 0 | return; |
369 | 0 | } |
370 | | |
371 | 0 | partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end()); |
372 | 0 | if (redeem_script.empty() && !sigdata.redeem_script.empty()) { Branch (372:9): [True: 0, False: 0]
Branch (372:34): [True: 0, False: 0]
|
373 | 0 | redeem_script = sigdata.redeem_script; |
374 | 0 | } |
375 | 0 | if (witness_script.empty() && !sigdata.witness_script.empty()) { Branch (375:9): [True: 0, False: 0]
Branch (375:35): [True: 0, False: 0]
|
376 | 0 | witness_script = sigdata.witness_script; |
377 | 0 | } |
378 | 0 | for (const auto& entry : sigdata.misc_pubkeys) { Branch (378:28): [True: 0, False: 0]
|
379 | 0 | hd_keypaths.emplace(entry.second); |
380 | 0 | } |
381 | 0 | if (!sigdata.taproot_key_path_sig.empty()) { Branch (381:9): [True: 0, False: 0]
|
382 | 0 | m_tap_key_sig = sigdata.taproot_key_path_sig; |
383 | 0 | } |
384 | 0 | for (const auto& [pubkey_leaf, sig] : sigdata.taproot_script_sigs) { Branch (384:41): [True: 0, False: 0]
|
385 | 0 | m_tap_script_sigs.emplace(pubkey_leaf, sig); |
386 | 0 | } |
387 | 0 | if (!sigdata.tr_spenddata.internal_key.IsNull()) { Branch (387:9): [True: 0, False: 0]
|
388 | 0 | m_tap_internal_key = sigdata.tr_spenddata.internal_key; |
389 | 0 | } |
390 | 0 | if (!sigdata.tr_spenddata.merkle_root.IsNull()) { Branch (390:9): [True: 0, False: 0]
|
391 | 0 | m_tap_merkle_root = sigdata.tr_spenddata.merkle_root; |
392 | 0 | } |
393 | 0 | for (const auto& [leaf_script, control_block] : sigdata.tr_spenddata.scripts) { Branch (393:51): [True: 0, False: 0]
|
394 | 0 | m_tap_scripts.emplace(leaf_script, control_block); |
395 | 0 | } |
396 | 0 | for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) { Branch (396:44): [True: 0, False: 0]
|
397 | 0 | m_tap_bip32_paths.emplace(pubkey, leaf_origin); |
398 | 0 | } |
399 | 0 | m_musig2_participants.insert(sigdata.musig2_pubkeys.begin(), sigdata.musig2_pubkeys.end()); |
400 | 0 | for (const auto& [agg_key_lh, pubnonces] : sigdata.musig2_pubnonces) { Branch (400:46): [True: 0, False: 0]
|
401 | 0 | m_musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end()); |
402 | 0 | } |
403 | 0 | for (const auto& [agg_key_lh, psigs] : sigdata.musig2_partial_sigs) { Branch (403:42): [True: 0, False: 0]
|
404 | 0 | m_musig2_partial_sigs[agg_key_lh].insert(psigs.begin(), psigs.end()); |
405 | 0 | } |
406 | 0 | for (const auto& [hash, preimage] : sigdata.ripemd160_preimages) { Branch (406:39): [True: 0, False: 0]
|
407 | 0 | ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
408 | 0 | } |
409 | 0 | for (const auto& [hash, preimage] : sigdata.sha256_preimages) { Branch (409:39): [True: 0, False: 0]
|
410 | 0 | sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
411 | 0 | } |
412 | 0 | for (const auto& [hash, preimage] : sigdata.hash160_preimages) { Branch (412:39): [True: 0, False: 0]
|
413 | 0 | hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
414 | 0 | } |
415 | 0 | for (const auto& [hash, preimage] : sigdata.hash256_preimages) { Branch (415:39): [True: 0, False: 0]
|
416 | 0 | hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage); |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | | void PSBTInput::Merge(const PSBTInput& input) |
421 | 0 | { |
422 | 0 | if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo; Branch (422:9): [True: 0, False: 0]
Branch (422:30): [True: 0, False: 0]
|
423 | 0 | if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) { Branch (423:9): [True: 0, False: 0]
Branch (423:34): [True: 0, False: 0]
|
424 | 0 | witness_utxo = input.witness_utxo; |
425 | 0 | } |
426 | |
|
427 | 0 | partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end()); |
428 | 0 | ripemd160_preimages.insert(input.ripemd160_preimages.begin(), input.ripemd160_preimages.end()); |
429 | 0 | sha256_preimages.insert(input.sha256_preimages.begin(), input.sha256_preimages.end()); |
430 | 0 | hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end()); |
431 | 0 | hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end()); |
432 | 0 | hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end()); |
433 | 0 | m_proprietary.insert(input.m_proprietary.begin(), input.m_proprietary.end()); |
434 | 0 | unknown.insert(input.unknown.begin(), input.unknown.end()); |
435 | 0 | m_tap_script_sigs.insert(input.m_tap_script_sigs.begin(), input.m_tap_script_sigs.end()); |
436 | | // Merge by control block, the serialized key (BIP 371), to avoid duplicate keys. Keep the |
437 | | // leaf script already present; BIP 174 lets the Combiner pick arbitrarily on conflict. |
438 | 0 | std::set<std::vector<unsigned char>> seen_control_blocks; |
439 | 0 | for (const auto& [_, control_blocks] : m_tap_scripts) { Branch (439:42): [True: 0, False: 0]
|
440 | 0 | seen_control_blocks.insert(control_blocks.begin(), control_blocks.end()); |
441 | 0 | } |
442 | 0 | for (const auto& [leaf, control_blocks] : input.m_tap_scripts) { Branch (442:45): [True: 0, False: 0]
|
443 | 0 | for (const auto& control_block : control_blocks) { Branch (443:40): [True: 0, False: 0]
|
444 | 0 | if (seen_control_blocks.insert(control_block).second) m_tap_scripts[leaf].insert(control_block); Branch (444:17): [True: 0, False: 0]
|
445 | 0 | } |
446 | 0 | } |
447 | 0 | m_tap_bip32_paths.insert(input.m_tap_bip32_paths.begin(), input.m_tap_bip32_paths.end()); |
448 | |
|
449 | 0 | if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script; Branch (449:9): [True: 0, False: 0]
Branch (449:34): [True: 0, False: 0]
|
450 | 0 | if (witness_script.empty() && !input.witness_script.empty()) witness_script = input.witness_script; Branch (450:9): [True: 0, False: 0]
Branch (450:35): [True: 0, False: 0]
|
451 | 0 | if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig; Branch (451:9): [True: 0, False: 0]
Branch (451:37): [True: 0, False: 0]
|
452 | 0 | if (final_script_witness.IsNull() && !input.final_script_witness.IsNull()) final_script_witness = input.final_script_witness; Branch (452:9): [True: 0, False: 0]
Branch (452:42): [True: 0, False: 0]
|
453 | 0 | if (m_tap_key_sig.empty() && !input.m_tap_key_sig.empty()) m_tap_key_sig = input.m_tap_key_sig; Branch (453:9): [True: 0, False: 0]
Branch (453:34): [True: 0, False: 0]
|
454 | 0 | if (m_tap_internal_key.IsNull() && !input.m_tap_internal_key.IsNull()) m_tap_internal_key = input.m_tap_internal_key; Branch (454:9): [True: 0, False: 0]
Branch (454:40): [True: 0, False: 0]
|
455 | 0 | if (m_tap_merkle_root.IsNull() && !input.m_tap_merkle_root.IsNull()) m_tap_merkle_root = input.m_tap_merkle_root; Branch (455:9): [True: 0, False: 0]
Branch (455:39): [True: 0, False: 0]
|
456 | 0 | m_musig2_participants.insert(input.m_musig2_participants.begin(), input.m_musig2_participants.end()); |
457 | 0 | for (const auto& [agg_key_lh, pubnonces] : input.m_musig2_pubnonces) { Branch (457:46): [True: 0, False: 0]
|
458 | 0 | m_musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end()); |
459 | 0 | } |
460 | 0 | for (const auto& [agg_key_lh, psigs] : input.m_musig2_partial_sigs) { Branch (460:42): [True: 0, False: 0]
|
461 | 0 | m_musig2_partial_sigs[agg_key_lh].insert(psigs.begin(), psigs.end()); |
462 | 0 | } |
463 | 0 | if (sighash_type == std::nullopt && input.sighash_type != std::nullopt) sighash_type = input.sighash_type; Branch (463:9): [True: 0, False: 0]
Branch (463:41): [True: 0, False: 0]
|
464 | 0 | if (sequence == std::nullopt && input.sequence != std::nullopt) sequence = input.sequence; Branch (464:9): [True: 0, False: 0]
Branch (464:37): [True: 0, False: 0]
|
465 | 0 | if (time_locktime == std::nullopt && input.time_locktime != std::nullopt) time_locktime = input.time_locktime; Branch (465:9): [True: 0, False: 0]
Branch (465:42): [True: 0, False: 0]
|
466 | 0 | if (height_locktime == std::nullopt && input.height_locktime != std::nullopt) height_locktime = input.height_locktime; Branch (466:9): [True: 0, False: 0]
Branch (466:44): [True: 0, False: 0]
|
467 | 0 | } |
468 | | |
469 | | bool PSBTInput::HasSignatures() const |
470 | 0 | { |
471 | 0 | return !final_script_sig.empty() Branch (471:12): [True: 0, False: 0]
|
472 | 0 | || !final_script_witness.IsNull() Branch (472:15): [True: 0, False: 0]
|
473 | 0 | || !partial_sigs.empty() Branch (473:15): [True: 0, False: 0]
|
474 | 0 | || !m_tap_key_sig.empty() Branch (474:15): [True: 0, False: 0]
|
475 | 0 | || !m_tap_script_sigs.empty() Branch (475:15): [True: 0, False: 0]
|
476 | 0 | || !m_musig2_partial_sigs.empty(); Branch (476:15): [True: 0, False: 0]
|
477 | 0 | } |
478 | | |
479 | | void PSBTOutput::FillSignatureData(SignatureData& sigdata) const |
480 | 0 | { |
481 | 0 | if (!redeem_script.empty()) { Branch (481:9): [True: 0, False: 0]
|
482 | 0 | sigdata.redeem_script = redeem_script; |
483 | 0 | } |
484 | 0 | if (!witness_script.empty()) { Branch (484:9): [True: 0, False: 0]
|
485 | 0 | sigdata.witness_script = witness_script; |
486 | 0 | } |
487 | 0 | for (const auto& key_pair : hd_keypaths) { Branch (487:31): [True: 0, False: 0]
|
488 | 0 | sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair); |
489 | 0 | } |
490 | 0 | if (!m_tap_tree.empty() && m_tap_internal_key.IsFullyValid()) { Branch (490:9): [True: 0, False: 0]
Branch (490:32): [True: 0, False: 0]
|
491 | 0 | TaprootBuilder builder; |
492 | 0 | for (const auto& [depth, leaf_ver, script] : m_tap_tree) { Branch (492:52): [True: 0, False: 0]
|
493 | 0 | builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true); |
494 | 0 | } |
495 | 0 | assert(builder.IsComplete()); Branch (495:9): [True: 0, False: 0]
|
496 | 0 | builder.Finalize(m_tap_internal_key); |
497 | 0 | TaprootSpendData spenddata = builder.GetSpendData(); |
498 | |
|
499 | 0 | sigdata.tr_spenddata.internal_key = m_tap_internal_key; |
500 | 0 | sigdata.tr_spenddata.Merge(spenddata); |
501 | 0 | sigdata.tr_builder = builder; |
502 | 0 | } |
503 | 0 | for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) { Branch (503:44): [True: 0, False: 0]
|
504 | 0 | sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin); |
505 | 0 | sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey); |
506 | 0 | } |
507 | 0 | sigdata.musig2_pubkeys.insert(m_musig2_participants.begin(), m_musig2_participants.end()); |
508 | 0 | } |
509 | | |
510 | | void PSBTOutput::FromSignatureData(const SignatureData& sigdata) |
511 | 0 | { |
512 | 0 | if (redeem_script.empty() && !sigdata.redeem_script.empty()) { Branch (512:9): [True: 0, False: 0]
Branch (512:34): [True: 0, False: 0]
|
513 | 0 | redeem_script = sigdata.redeem_script; |
514 | 0 | } |
515 | 0 | if (witness_script.empty() && !sigdata.witness_script.empty()) { Branch (515:9): [True: 0, False: 0]
Branch (515:35): [True: 0, False: 0]
|
516 | 0 | witness_script = sigdata.witness_script; |
517 | 0 | } |
518 | 0 | for (const auto& entry : sigdata.misc_pubkeys) { Branch (518:28): [True: 0, False: 0]
|
519 | 0 | hd_keypaths.emplace(entry.second); |
520 | 0 | } |
521 | 0 | if (!sigdata.tr_spenddata.internal_key.IsNull()) { Branch (521:9): [True: 0, False: 0]
|
522 | 0 | m_tap_internal_key = sigdata.tr_spenddata.internal_key; |
523 | 0 | } |
524 | 0 | if (sigdata.tr_builder.has_value() && sigdata.tr_builder->HasScripts()) { Branch (524:9): [True: 0, False: 0]
Branch (524:43): [True: 0, False: 0]
|
525 | 0 | m_tap_tree = sigdata.tr_builder->GetTreeTuples(); |
526 | 0 | } |
527 | 0 | for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) { Branch (527:44): [True: 0, False: 0]
|
528 | 0 | m_tap_bip32_paths.emplace(pubkey, leaf_origin); |
529 | 0 | } |
530 | 0 | m_musig2_participants.insert(sigdata.musig2_pubkeys.begin(), sigdata.musig2_pubkeys.end()); |
531 | 0 | } |
532 | | |
533 | | void PSBTOutput::Merge(const PSBTOutput& output) |
534 | 0 | { |
535 | 0 | hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end()); |
536 | 0 | m_proprietary.insert(output.m_proprietary.begin(), output.m_proprietary.end()); |
537 | 0 | unknown.insert(output.unknown.begin(), output.unknown.end()); |
538 | 0 | m_tap_bip32_paths.insert(output.m_tap_bip32_paths.begin(), output.m_tap_bip32_paths.end()); |
539 | |
|
540 | 0 | if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script; Branch (540:9): [True: 0, False: 0]
Branch (540:34): [True: 0, False: 0]
|
541 | 0 | if (witness_script.empty() && !output.witness_script.empty()) witness_script = output.witness_script; Branch (541:9): [True: 0, False: 0]
Branch (541:35): [True: 0, False: 0]
|
542 | 0 | if (m_tap_internal_key.IsNull() && !output.m_tap_internal_key.IsNull()) m_tap_internal_key = output.m_tap_internal_key; Branch (542:9): [True: 0, False: 0]
Branch (542:40): [True: 0, False: 0]
|
543 | 0 | if (m_tap_tree.empty() && !output.m_tap_tree.empty()) m_tap_tree = output.m_tap_tree; Branch (543:9): [True: 0, False: 0]
Branch (543:31): [True: 0, False: 0]
|
544 | 0 | m_musig2_participants.insert(output.m_musig2_participants.begin(), output.m_musig2_participants.end()); |
545 | 0 | } |
546 | | |
547 | | bool PSBTInputSigned(const PSBTInput& input) |
548 | 0 | { |
549 | 0 | return !input.final_script_sig.empty() || !input.final_script_witness.IsNull(); Branch (549:12): [True: 0, False: 0]
Branch (549:47): [True: 0, False: 0]
|
550 | 0 | } |
551 | | |
552 | | bool PSBTInputSignedAndVerified(const PartiallySignedTransaction& psbt, unsigned int input_index, const PrecomputedTransactionData* txdata) |
553 | 0 | { |
554 | 0 | CTxOut utxo; |
555 | 0 | assert(input_index < psbt.inputs.size()); Branch (555:5): [True: 0, False: 0]
|
556 | 0 | const PSBTInput& input = psbt.inputs[input_index]; |
557 | |
|
558 | 0 | if (input.non_witness_utxo) { Branch (558:9): [True: 0, False: 0]
|
559 | | // If we're taking our information from a non-witness UTXO, verify that it matches the prevout. |
560 | 0 | COutPoint prevout = input.GetOutPoint(); |
561 | 0 | if (prevout.n >= input.non_witness_utxo->vout.size()) { Branch (561:13): [True: 0, False: 0]
|
562 | 0 | return false; |
563 | 0 | } |
564 | 0 | if (input.non_witness_utxo->GetHash() != prevout.hash) { Branch (564:13): [True: 0, False: 0]
|
565 | 0 | return false; |
566 | 0 | } |
567 | 0 | utxo = input.non_witness_utxo->vout[prevout.n]; |
568 | 0 | } else if (!input.witness_utxo.IsNull()) { Branch (568:16): [True: 0, False: 0]
|
569 | 0 | utxo = input.witness_utxo; |
570 | 0 | } else { |
571 | 0 | return false; |
572 | 0 | } |
573 | | |
574 | 0 | std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx(); |
575 | 0 | if (!unsigned_tx) { Branch (575:9): [True: 0, False: 0]
|
576 | 0 | return false; |
577 | 0 | } |
578 | 0 | const CMutableTransaction& tx = *unsigned_tx; |
579 | 0 | if (txdata) { Branch (579:9): [True: 0, False: 0]
|
580 | 0 | return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&tx, input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL}); |
581 | 0 | } else { |
582 | 0 | return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&tx, input_index, utxo.nValue, MissingDataBehavior::FAIL}); |
583 | 0 | } |
584 | 0 | } |
585 | | |
586 | 0 | size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) { |
587 | 0 | size_t count = 0; |
588 | 0 | for (const auto& input : psbt.inputs) { Branch (588:28): [True: 0, False: 0]
|
589 | 0 | if (!PSBTInputSigned(input)) { Branch (589:13): [True: 0, False: 0]
|
590 | 0 | count++; |
591 | 0 | } |
592 | 0 | } |
593 | |
|
594 | 0 | return count; |
595 | 0 | } |
596 | | |
597 | | void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index) |
598 | 0 | { |
599 | 0 | std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx(); |
600 | 0 | if (!unsigned_tx) { Branch (600:9): [True: 0, False: 0]
|
601 | 0 | return; |
602 | 0 | } |
603 | 0 | const CTxOut& out = unsigned_tx->vout.at(index); |
604 | 0 | PSBTOutput& psbt_out = psbt.outputs.at(index); |
605 | | |
606 | | // Fill a SignatureData with output info |
607 | 0 | SignatureData sigdata; |
608 | 0 | psbt_out.FillSignatureData(sigdata); |
609 | | |
610 | | // Construct a would-be spend of this output, to update sigdata with. |
611 | | // Note that ProduceSignature is used to fill in metadata (not actual signatures), |
612 | | // so provider does not need to provide any private keys (it can be a HidingSigningProvider). |
613 | 0 | CMutableTransaction tx{}; |
614 | 0 | tx.vin.emplace_back(); |
615 | 0 | MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, {.sighash_type = SIGHASH_ALL}); |
616 | 0 | ProduceSignature(provider, creator, out.scriptPubKey, sigdata); |
617 | | |
618 | | // Put redeem_script, witness_script, key paths, into PSBTOutput. |
619 | 0 | psbt_out.FromSignatureData(sigdata); |
620 | 0 | } |
621 | | |
622 | | std::optional<PrecomputedTransactionData> PrecomputePSBTData(const PartiallySignedTransaction& psbt) |
623 | 0 | { |
624 | 0 | std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx(); |
625 | 0 | if (!unsigned_tx) { Branch (625:9): [True: 0, False: 0]
|
626 | 0 | return std::nullopt; |
627 | 0 | } |
628 | 0 | const CMutableTransaction& tx = *unsigned_tx; |
629 | 0 | bool have_all_spent_outputs = true; |
630 | 0 | std::vector<CTxOut> utxos; |
631 | 0 | for (const PSBTInput& input : psbt.inputs) { Branch (631:33): [True: 0, False: 0]
|
632 | 0 | if (!input.GetUTXO(utxos.emplace_back())) have_all_spent_outputs = false; Branch (632:13): [True: 0, False: 0]
|
633 | 0 | } |
634 | 0 | PrecomputedTransactionData txdata; |
635 | 0 | if (have_all_spent_outputs) { Branch (635:9): [True: 0, False: 0]
|
636 | 0 | txdata.Init(tx, std::move(utxos), true); |
637 | 0 | } else { |
638 | 0 | txdata.Init(tx, {}, true); |
639 | 0 | } |
640 | 0 | return txdata; |
641 | 0 | } |
642 | | |
643 | | util::Expected<void, PSBTError> SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, const common::PSBTFillOptions& options, SignatureData* out_sigdata) |
644 | 0 | { |
645 | 0 | PSBTInput& input = psbt.inputs.at(index); |
646 | 0 | std::optional<CMutableTransaction> unsigned_tx = psbt.GetUnsignedTx(); |
647 | 0 | if (!unsigned_tx) { Branch (647:9): [True: 0, False: 0]
|
648 | 0 | return util::Unexpected{PSBTError::INVALID_TX}; |
649 | 0 | } |
650 | 0 | const CMutableTransaction& tx = *unsigned_tx; |
651 | |
|
652 | 0 | if (PSBTInputSignedAndVerified(psbt, index, txdata)) { Branch (652:9): [True: 0, False: 0]
|
653 | 0 | return {}; |
654 | 0 | } |
655 | | |
656 | | // Fill SignatureData with input info |
657 | 0 | SignatureData sigdata; |
658 | 0 | input.FillSignatureData(sigdata); |
659 | | |
660 | | // Get UTXO |
661 | 0 | bool require_witness_sig = false; |
662 | 0 | CTxOut utxo; |
663 | |
|
664 | 0 | if (input.non_witness_utxo) { Branch (664:9): [True: 0, False: 0]
|
665 | | // If we're taking our information from a non-witness UTXO, verify that it matches the prevout. |
666 | 0 | COutPoint prevout = input.GetOutPoint(); |
667 | 0 | if (prevout.n >= input.non_witness_utxo->vout.size()) { Branch (667:13): [True: 0, False: 0]
|
668 | 0 | return util::Unexpected{PSBTError::MISSING_INPUTS}; |
669 | 0 | } |
670 | 0 | if (input.non_witness_utxo->GetHash() != prevout.hash) { Branch (670:13): [True: 0, False: 0]
|
671 | 0 | return util::Unexpected{PSBTError::MISSING_INPUTS}; |
672 | 0 | } |
673 | 0 | utxo = input.non_witness_utxo->vout[prevout.n]; |
674 | 0 | } else if (!input.witness_utxo.IsNull()) { Branch (674:16): [True: 0, False: 0]
|
675 | 0 | utxo = input.witness_utxo; |
676 | | // When we're taking our information from a witness UTXO, we can't verify it is actually data from |
677 | | // the output being spent. This is safe in case a witness signature is produced (which includes this |
678 | | // information directly in the hash), but not for non-witness signatures. Remember that we require |
679 | | // a witness signature in this situation. |
680 | 0 | require_witness_sig = true; |
681 | 0 | } else { |
682 | 0 | return util::Unexpected{PSBTError::MISSING_INPUTS}; |
683 | 0 | } |
684 | | |
685 | | // Get the sighash type |
686 | | // If both the field and the parameter are provided, they must match |
687 | | // If only the parameter is provided, use it and add it to the PSBT if it is other than SIGHASH_DEFAULT |
688 | | // for all input types, and not SIGHASH_ALL for non-taproot input types. |
689 | | // If neither are provided, use SIGHASH_DEFAULT if it is taproot, and SIGHASH_ALL for everything else. |
690 | 0 | int sighash{options.sighash_type.value_or(utxo.scriptPubKey.IsPayToTaproot() ? SIGHASH_DEFAULT : SIGHASH_ALL)}; Branch (690:47): [True: 0, False: 0]
|
691 | | |
692 | | // For user safety, the desired sighash must be provided if the PSBT wants something other than the default set in the previous line. |
693 | 0 | if (input.sighash_type && input.sighash_type != sighash) { Branch (693:9): [True: 0, False: 0]
Branch (693:31): [True: 0, False: 0]
|
694 | 0 | return util::Unexpected{PSBTError::SIGHASH_MISMATCH}; |
695 | 0 | } |
696 | | // Set the PSBT sighash field when sighash is not DEFAULT or ALL |
697 | | // DEFAULT is allowed for non-taproot inputs since DEFAULT may be passed for them (e.g. the psbt being signed also has taproot inputs) |
698 | | // Note that signing already aliases DEFAULT to ALL for non-taproot inputs. |
699 | 0 | if (utxo.scriptPubKey.IsPayToTaproot() ? sighash != SIGHASH_DEFAULT : Branch (699:9): [True: 0, False: 0]
Branch (699:9): [True: 0, False: 0]
|
700 | 0 | (sighash != SIGHASH_DEFAULT && sighash != SIGHASH_ALL)) { Branch (700:46): [True: 0, False: 0]
Branch (700:76): [True: 0, False: 0]
|
701 | 0 | input.sighash_type = sighash; |
702 | 0 | } |
703 | | |
704 | | // Check all existing signatures use the sighash type |
705 | 0 | if (sighash == SIGHASH_DEFAULT) { Branch (705:9): [True: 0, False: 0]
|
706 | 0 | if (!input.m_tap_key_sig.empty() && input.m_tap_key_sig.size() != 64) { Branch (706:13): [True: 0, False: 0]
Branch (706:45): [True: 0, False: 0]
|
707 | 0 | return util::Unexpected{PSBTError::SIGHASH_MISMATCH}; |
708 | 0 | } |
709 | 0 | for (const auto& [_, sig] : input.m_tap_script_sigs) { Branch (709:35): [True: 0, False: 0]
|
710 | 0 | if (sig.size() != 64) return util::Unexpected{PSBTError::SIGHASH_MISMATCH}; Branch (710:17): [True: 0, False: 0]
|
711 | 0 | } |
712 | 0 | } else { |
713 | 0 | if (!input.m_tap_key_sig.empty() && (input.m_tap_key_sig.size() != 65 || input.m_tap_key_sig.back() != sighash)) { Branch (713:13): [True: 0, False: 0]
Branch (713:46): [True: 0, False: 0]
Branch (713:82): [True: 0, False: 0]
|
714 | 0 | return util::Unexpected{PSBTError::SIGHASH_MISMATCH}; |
715 | 0 | } |
716 | 0 | for (const auto& [_, sig] : input.m_tap_script_sigs) { Branch (716:35): [True: 0, False: 0]
|
717 | 0 | if (sig.size() != 65 || sig.back() != sighash) return util::Unexpected{PSBTError::SIGHASH_MISMATCH}; Branch (717:17): [True: 0, False: 0]
Branch (717:37): [True: 0, False: 0]
|
718 | 0 | } |
719 | 0 | for (const auto& [_, sig] : input.partial_sigs) { Branch (719:35): [True: 0, False: 0]
|
720 | 0 | if (sig.second.back() != sighash) return util::Unexpected{PSBTError::SIGHASH_MISMATCH}; Branch (720:17): [True: 0, False: 0]
|
721 | 0 | } |
722 | 0 | } |
723 | | |
724 | 0 | sigdata.witness = false; |
725 | 0 | bool sig_complete; |
726 | 0 | if (txdata == nullptr) { Branch (726:9): [True: 0, False: 0]
|
727 | 0 | sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata); |
728 | 0 | } else { |
729 | 0 | MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, {.sighash_type = sighash}); |
730 | 0 | sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata); |
731 | 0 | } |
732 | | // Verify that a witness signature was produced in case one was required. |
733 | 0 | if (require_witness_sig && !sigdata.witness) return util::Unexpected{PSBTError::INCOMPLETE}; Branch (733:9): [True: 0, False: 0]
Branch (733:32): [True: 0, False: 0]
|
734 | | |
735 | | // If we are not finalizing, set sigdata.complete to false to not set the scriptWitness |
736 | 0 | if (!options.finalize && sigdata.complete) sigdata.complete = false; Branch (736:9): [True: 0, False: 0]
Branch (736:30): [True: 0, False: 0]
|
737 | |
|
738 | 0 | input.FromSignatureData(sigdata); |
739 | | |
740 | | // If we have a witness signature, put a witness UTXO. |
741 | 0 | if (sigdata.witness) { Branch (741:9): [True: 0, False: 0]
|
742 | 0 | input.witness_utxo = utxo; |
743 | | // We can remove the non_witness_utxo if and only if there are no non-segwit or segwit v0 |
744 | | // inputs in this transaction. Since this requires inspecting the entire transaction, this |
745 | | // is something for the caller to deal with (i.e. FillPSBT). |
746 | 0 | } |
747 | | |
748 | | // Fill in the missing info |
749 | 0 | if (out_sigdata) { Branch (749:9): [True: 0, False: 0]
|
750 | 0 | out_sigdata->missing_pubkeys = sigdata.missing_pubkeys; |
751 | 0 | out_sigdata->missing_sigs = sigdata.missing_sigs; |
752 | 0 | out_sigdata->missing_redeem_script = sigdata.missing_redeem_script; |
753 | 0 | out_sigdata->missing_witness_script = sigdata.missing_witness_script; |
754 | 0 | } |
755 | |
|
756 | 0 | if (!sig_complete) return util::Unexpected{PSBTError::INCOMPLETE}; Branch (756:9): [True: 0, False: 0]
|
757 | 0 | return {}; |
758 | 0 | } |
759 | | |
760 | | void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx) |
761 | 0 | { |
762 | | // Figure out if any non_witness_utxos should be dropped |
763 | 0 | std::vector<unsigned int> to_drop; |
764 | 0 | for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) { Branch (764:30): [True: 0, False: 0]
|
765 | 0 | const auto& input = psbtx.inputs.at(i); |
766 | 0 | int wit_ver; |
767 | 0 | std::vector<unsigned char> wit_prog; |
768 | 0 | if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) { Branch (768:13): [True: 0, False: 0]
Branch (768:44): [True: 0, False: 0]
|
769 | | // There's a non-segwit input, so we cannot drop any non_witness_utxos |
770 | 0 | to_drop.clear(); |
771 | 0 | break; |
772 | 0 | } |
773 | 0 | if (wit_ver == 0) { Branch (773:13): [True: 0, False: 0]
|
774 | | // Segwit v0, so we cannot drop any non_witness_utxos |
775 | 0 | to_drop.clear(); |
776 | 0 | break; |
777 | 0 | } |
778 | | // non_witness_utxos cannot be dropped if the sighash type includes SIGHASH_ANYONECANPAY |
779 | | // Since callers should have called SignPSBTInput which updates the sighash type in the PSBT, we only |
780 | | // need to look at that field. If it is not present, then we can assume SIGHASH_DEFAULT or SIGHASH_ALL. |
781 | 0 | if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) == SIGHASH_ANYONECANPAY) { Branch (781:13): [True: 0, False: 0]
Branch (781:51): [True: 0, False: 0]
|
782 | 0 | to_drop.clear(); |
783 | 0 | break; |
784 | 0 | } |
785 | | |
786 | 0 | if (input.non_witness_utxo) { Branch (786:13): [True: 0, False: 0]
|
787 | 0 | to_drop.push_back(i); |
788 | 0 | } |
789 | 0 | } |
790 | | |
791 | | // Drop the non_witness_utxos that we can drop |
792 | 0 | for (unsigned int i : to_drop) { Branch (792:25): [True: 0, False: 0]
|
793 | 0 | psbtx.inputs.at(i).non_witness_utxo = nullptr; |
794 | 0 | } |
795 | 0 | } |
796 | | |
797 | | bool FinalizePSBT(PartiallySignedTransaction& psbtx) |
798 | 0 | { |
799 | | // Finalize input signatures -- in case we have partial signatures that add up to a complete |
800 | | // signature, but have not combined them yet (e.g. because the combiner that created this |
801 | | // PartiallySignedTransaction did not understand them), this will combine them into a final |
802 | | // script. |
803 | 0 | bool complete = true; |
804 | 0 | std::optional<PrecomputedTransactionData> txdata_res = PrecomputePSBTData(psbtx); |
805 | 0 | if (!txdata_res) { Branch (805:9): [True: 0, False: 0]
|
806 | 0 | return false; |
807 | 0 | } |
808 | 0 | const PrecomputedTransactionData& txdata = *txdata_res; |
809 | 0 | for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) { Branch (809:30): [True: 0, False: 0]
|
810 | 0 | PSBTInput& input = psbtx.inputs.at(i); |
811 | 0 | const auto sign_result = SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, {.sighash_type = input.sighash_type, .finalize = true}, /*out_sigdata=*/nullptr); |
812 | 0 | complete &= sign_result.has_value(); |
813 | 0 | } |
814 | |
|
815 | 0 | return complete; |
816 | 0 | } |
817 | | |
818 | | bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result) |
819 | 0 | { |
820 | | // It's not safe to extract a PSBT that isn't finalized, and there's no easy way to check |
821 | | // whether a PSBT is finalized without finalizing it, so we just do this. |
822 | 0 | if (!FinalizePSBT(psbtx)) { Branch (822:9): [True: 0, False: 0]
|
823 | 0 | return false; |
824 | 0 | } |
825 | | |
826 | 0 | std::optional<CMutableTransaction> unsigned_tx = psbtx.GetUnsignedTx(); |
827 | 0 | if (!unsigned_tx) { Branch (827:9): [True: 0, False: 0]
|
828 | 0 | return false; |
829 | 0 | } |
830 | 0 | result = *unsigned_tx; |
831 | 0 | for (unsigned int i = 0; i < result.vin.size(); ++i) { Branch (831:30): [True: 0, False: 0]
|
832 | 0 | result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig; |
833 | 0 | result.vin[i].scriptWitness = psbtx.inputs[i].final_script_witness; |
834 | 0 | } |
835 | 0 | return true; |
836 | 0 | } |
837 | | |
838 | | std::optional<PartiallySignedTransaction> CombinePSBTs(const std::vector<PartiallySignedTransaction>& psbtxs) |
839 | 0 | { |
840 | 0 | PartiallySignedTransaction out = psbtxs[0]; // Copy the first one |
841 | | |
842 | | // Merge |
843 | 0 | for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) { Branch (843:47): [True: 0, False: 0]
|
844 | 0 | if (!out.Merge(*it)) { Branch (844:13): [True: 0, False: 0]
|
845 | 0 | return std::nullopt; |
846 | 0 | } |
847 | 0 | } |
848 | 0 | return out; |
849 | 0 | } |
850 | | |
851 | 0 | std::string PSBTRoleName(PSBTRole role) { |
852 | 0 | switch (role) { Branch (852:13): [True: 0, False: 0]
|
853 | 0 | case PSBTRole::CREATOR: return "creator"; Branch (853:5): [True: 0, False: 0]
|
854 | 0 | case PSBTRole::UPDATER: return "updater"; Branch (854:5): [True: 0, False: 0]
|
855 | 0 | case PSBTRole::SIGNER: return "signer"; Branch (855:5): [True: 0, False: 0]
|
856 | 0 | case PSBTRole::FINALIZER: return "finalizer"; Branch (856:5): [True: 0, False: 0]
|
857 | 0 | case PSBTRole::EXTRACTOR: return "extractor"; Branch (857:5): [True: 0, False: 0]
|
858 | 0 | } // no default case, so the compiler can warn about missing cases |
859 | 0 | assert(false); Branch (859:5): [Folded, False: 0]
|
860 | 0 | } |
861 | | |
862 | | util::Result<PartiallySignedTransaction> DecodeBase64PSBT(const std::string& base64_tx) |
863 | 0 | { |
864 | 0 | auto tx_data = DecodeBase64(base64_tx); |
865 | 0 | if (!tx_data) { Branch (865:9): [True: 0, False: 0]
|
866 | 0 | return util::Error{Untranslated("invalid base64")}; |
867 | 0 | } |
868 | 0 | return DecodeRawPSBT(MakeByteSpan(*tx_data)); |
869 | 0 | } |
870 | | |
871 | | util::Result<PartiallySignedTransaction> DecodeRawPSBT(std::span<const std::byte> tx_data) |
872 | 0 | { |
873 | 0 | SpanReader ss_data{tx_data}; |
874 | 0 | try { |
875 | 0 | PartiallySignedTransaction psbt(deserialize, ss_data); |
876 | 0 | if (!ss_data.empty()) { Branch (876:13): [True: 0, False: 0]
|
877 | 0 | return util::Error{Untranslated("extra data after PSBT")}; |
878 | 0 | } |
879 | 0 | return psbt; |
880 | 0 | } catch (const std::exception& e) { |
881 | 0 | return util::Error{Untranslated(e.what())}; |
882 | 0 | } |
883 | 0 | } |
884 | | |
885 | | uint32_t PartiallySignedTransaction::GetVersion() const |
886 | 0 | { |
887 | 0 | if (m_version != std::nullopt) { Branch (887:9): [True: 0, False: 0]
|
888 | 0 | return *m_version; |
889 | 0 | } |
890 | 0 | return 0; |
891 | 0 | } |