/root/bitcoin/src/script/sign.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin Core developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #include <script/sign.h> |
7 | | |
8 | | #include <addresstype.h> |
9 | | #include <coins.h> |
10 | | #include <consensus/amount.h> |
11 | | #include <hash.h> |
12 | | #include <key.h> |
13 | | #include <musig.h> |
14 | | #include <policy/policy.h> |
15 | | #include <prevector.h> |
16 | | #include <primitives/transaction.h> |
17 | | #include <script/keyorigin.h> |
18 | | #include <script/miniscript.h> |
19 | | #include <script/script.h> |
20 | | #include <script/script_error.h> |
21 | | #include <script/signingprovider.h> |
22 | | #include <script/solver.h> |
23 | | #include <script/verify_flags.h> |
24 | | #include <serialize.h> |
25 | | #include <uint256.h> |
26 | | #include <util/check.h> |
27 | | #include <util/translation.h> |
28 | | #include <util/vector.h> |
29 | | |
30 | | #include <algorithm> |
31 | | #include <array> |
32 | | #include <cstddef> |
33 | | #include <functional> |
34 | | #include <iterator> |
35 | | #include <span> |
36 | | #include <string> |
37 | | |
38 | | typedef std::vector<unsigned char> valtype; |
39 | | |
40 | | MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction& tx, unsigned int input_idx, const CAmount& amount, const SignOptions& options) |
41 | 0 | : m_txto{tx}, nIn{input_idx}, m_options{options}, amount{amount}, checker{&m_txto, nIn, amount, MissingDataBehavior::FAIL}, |
42 | 0 | m_txdata(nullptr) |
43 | 0 | { |
44 | 0 | } |
45 | | |
46 | | MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction& tx, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, const SignOptions& options) |
47 | 0 | : m_txto{tx}, nIn{input_idx}, m_options{options}, amount{amount}, |
48 | 0 | checker{txdata ? MutableTransactionSignatureChecker{&m_txto, nIn, amount, *txdata, MissingDataBehavior::FAIL} : Branch (48:15): [True: 0, False: 0]
|
49 | 0 | MutableTransactionSignatureChecker{&m_txto, nIn, amount, MissingDataBehavior::FAIL}}, |
50 | 0 | m_txdata(txdata) |
51 | 0 | { |
52 | 0 | } |
53 | | |
54 | | bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& address, const CScript& scriptCode, SigVersion sigversion) const |
55 | 0 | { |
56 | 0 | assert(sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0); Branch (56:5): [True: 0, False: 0]
Branch (56:5): [True: 0, False: 0]
Branch (56:5): [True: 0, False: 0]
|
57 | | |
58 | 0 | CKey key; |
59 | 0 | if (!provider.GetKey(address, key)) Branch (59:9): [True: 0, False: 0]
|
60 | 0 | return false; |
61 | | |
62 | | // Signing with uncompressed keys is disabled in witness scripts |
63 | 0 | if (sigversion == SigVersion::WITNESS_V0 && !key.IsCompressed()) Branch (63:9): [True: 0, False: 0]
Branch (63:49): [True: 0, False: 0]
|
64 | 0 | return false; |
65 | | |
66 | | // Signing without known amount does not work in witness scripts. |
67 | 0 | if (sigversion == SigVersion::WITNESS_V0 && !MoneyRange(amount)) return false; Branch (67:9): [True: 0, False: 0]
Branch (67:49): [True: 0, False: 0]
|
68 | | |
69 | | // BASE/WITNESS_V0 signatures don't support explicit SIGHASH_DEFAULT, use SIGHASH_ALL instead. |
70 | 0 | const int hashtype = m_options.sighash_type == SIGHASH_DEFAULT ? SIGHASH_ALL : m_options.sighash_type; Branch (70:26): [True: 0, False: 0]
|
71 | | |
72 | | // If an input is signed with SIGHASH_SINGLE but there is no output at the same index, the |
73 | | // signature commits to no output at all. Which means such a signature stays valid if the |
74 | | // output is swapped, which is a footgun. So don't produce it. |
75 | 0 | if ((hashtype & SIGHASH_OUTPUT_MASK) == SIGHASH_SINGLE && nIn >= m_txto.vout.size()) return false; Branch (75:9): [True: 0, False: 0]
Branch (75:63): [True: 0, False: 0]
|
76 | | |
77 | 0 | uint256 hash = SignatureHash(scriptCode, m_txto, nIn, hashtype, amount, sigversion, m_txdata); |
78 | 0 | if (!key.Sign(hash, vchSig)) Branch (78:9): [True: 0, False: 0]
|
79 | 0 | return false; |
80 | 0 | vchSig.push_back((unsigned char)hashtype); |
81 | 0 | return true; |
82 | 0 | } |
83 | | |
84 | | std::optional<uint256> MutableTransactionSignatureCreator::ComputeSchnorrSignatureHash(const uint256* leaf_hash, SigVersion sigversion) const |
85 | 0 | { |
86 | 0 | assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); Branch (86:5): [True: 0, False: 0]
Branch (86:5): [True: 0, False: 0]
Branch (86:5): [True: 0, False: 0]
|
87 | | |
88 | | // BIP341/BIP342 signing needs lots of precomputed transaction data. While some |
89 | | // (non-SIGHASH_DEFAULT) sighash modes exist that can work with just some subset |
90 | | // of data present, for now, only support signing when everything is provided. |
91 | 0 | if (!m_txdata || !m_txdata->m_bip341_taproot_ready || !m_txdata->m_spent_outputs_ready) return std::nullopt; Branch (91:9): [True: 0, False: 0]
Branch (91:22): [True: 0, False: 0]
Branch (91:59): [True: 0, False: 0]
|
92 | | |
93 | 0 | ScriptExecutionData execdata; |
94 | 0 | execdata.m_annex_init = true; |
95 | 0 | execdata.m_annex_present = false; // Only support annex-less signing for now. |
96 | 0 | if (sigversion == SigVersion::TAPSCRIPT) { Branch (96:9): [True: 0, False: 0]
|
97 | 0 | execdata.m_codeseparator_pos_init = true; |
98 | 0 | execdata.m_codeseparator_pos = 0xFFFFFFFF; // Only support non-OP_CODESEPARATOR BIP342 signing for now. |
99 | 0 | if (!leaf_hash) return std::nullopt; // BIP342 signing needs leaf hash. Branch (99:13): [True: 0, False: 0]
|
100 | 0 | execdata.m_tapleaf_hash_init = true; |
101 | 0 | execdata.m_tapleaf_hash = *leaf_hash; |
102 | 0 | } |
103 | 0 | uint256 hash; |
104 | 0 | if (!SignatureHashSchnorr(hash, execdata, m_txto, nIn, m_options.sighash_type, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return std::nullopt; Branch (104:9): [True: 0, False: 0]
|
105 | 0 | return hash; |
106 | 0 | } |
107 | | |
108 | | bool MutableTransactionSignatureCreator::CreateSchnorrSig(const SigningProvider& provider, std::vector<unsigned char>& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion) const |
109 | 0 | { |
110 | 0 | CKey key; |
111 | 0 | if (!provider.GetKeyByXOnly(pubkey, key)) return false; Branch (111:9): [True: 0, False: 0]
|
112 | | |
113 | 0 | std::optional<uint256> hash = ComputeSchnorrSignatureHash(leaf_hash, sigversion); |
114 | 0 | if (!hash.has_value()) return false; Branch (114:9): [True: 0, False: 0]
|
115 | | |
116 | 0 | sig.resize(64); |
117 | | // Use uint256{} as aux_rnd for now. |
118 | 0 | if (!key.SignSchnorr(*hash, sig, merkle_root, {})) return false; Branch (118:9): [True: 0, False: 0]
|
119 | 0 | if (m_options.sighash_type) sig.push_back(m_options.sighash_type); Branch (119:9): [True: 0, False: 0]
|
120 | 0 | return true; |
121 | 0 | } |
122 | | |
123 | | std::vector<uint8_t> MutableTransactionSignatureCreator::CreateMuSig2Nonce(const SigningProvider& provider, const CPubKey& aggregate_pubkey, const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion, const SignatureData& sigdata) const |
124 | 0 | { |
125 | 0 | assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); Branch (125:5): [True: 0, False: 0]
Branch (125:5): [True: 0, False: 0]
Branch (125:5): [True: 0, False: 0]
|
126 | | |
127 | | // Retrieve the private key |
128 | 0 | CKey key; |
129 | 0 | if (!provider.GetKey(part_pubkey.GetID(), key)) return {}; Branch (129:9): [True: 0, False: 0]
|
130 | | |
131 | | // Retrieve participant pubkeys |
132 | 0 | auto it = sigdata.musig2_pubkeys.find(aggregate_pubkey); |
133 | 0 | if (it == sigdata.musig2_pubkeys.end()) return {}; Branch (133:9): [True: 0, False: 0]
|
134 | 0 | const std::vector<CPubKey>& pubkeys = it->second; |
135 | 0 | if (std::find(pubkeys.begin(), pubkeys.end(), part_pubkey) == pubkeys.end()) return {}; Branch (135:9): [True: 0, False: 0]
|
136 | | |
137 | | // Compute sighash |
138 | 0 | std::optional<uint256> sighash = ComputeSchnorrSignatureHash(leaf_hash, sigversion); |
139 | 0 | if (!sighash.has_value()) return {}; Branch (139:9): [True: 0, False: 0]
|
140 | | |
141 | 0 | MuSig2SecNonce secnonce; |
142 | 0 | std::vector<uint8_t> out = ::CreateMuSig2Nonce(secnonce, *sighash, key, aggregate_pubkey, pubkeys); |
143 | 0 | if (out.empty()) return {}; Branch (143:9): [True: 0, False: 0]
|
144 | | |
145 | | // Store the secnonce in the SigningProvider |
146 | 0 | provider.SetMuSig2SecNonce(MuSig2SessionID(script_pubkey, part_pubkey, *sighash, out), std::move(secnonce)); |
147 | |
|
148 | 0 | return out; |
149 | 0 | } |
150 | | |
151 | | bool MutableTransactionSignatureCreator::CreateMuSig2PartialSig(const SigningProvider& provider, uint256& partial_sig, const CPubKey& aggregate_pubkey, const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256* leaf_hash, const std::vector<std::pair<uint256, bool>>& tweaks, SigVersion sigversion, const SignatureData& sigdata) const |
152 | 0 | { |
153 | 0 | assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); Branch (153:5): [True: 0, False: 0]
Branch (153:5): [True: 0, False: 0]
Branch (153:5): [True: 0, False: 0]
|
154 | | |
155 | | // Retrieve private key |
156 | 0 | CKey key; |
157 | 0 | if (!provider.GetKey(part_pubkey.GetID(), key)) return false; Branch (157:9): [True: 0, False: 0]
|
158 | | |
159 | | // Retrieve participant pubkeys |
160 | 0 | auto it = sigdata.musig2_pubkeys.find(aggregate_pubkey); |
161 | 0 | if (it == sigdata.musig2_pubkeys.end()) return false; Branch (161:9): [True: 0, False: 0]
|
162 | 0 | const std::vector<CPubKey>& pubkeys = it->second; |
163 | 0 | if (std::find(pubkeys.begin(), pubkeys.end(), part_pubkey) == pubkeys.end()) return {}; Branch (163:9): [True: 0, False: 0]
|
164 | | |
165 | | // Retrieve pubnonces |
166 | 0 | auto this_leaf_aggkey = std::make_pair(script_pubkey, leaf_hash ? *leaf_hash : uint256()); Branch (166:59): [True: 0, False: 0]
|
167 | 0 | auto pubnonce_it = sigdata.musig2_pubnonces.find(this_leaf_aggkey); |
168 | 0 | if (pubnonce_it == sigdata.musig2_pubnonces.end()) return false; Branch (168:9): [True: 0, False: 0]
|
169 | 0 | const std::map<CPubKey, std::vector<uint8_t>>& pubnonces = pubnonce_it->second; |
170 | | |
171 | | // Check if enough pubnonces |
172 | 0 | if (pubnonces.size() != pubkeys.size()) return false; Branch (172:9): [True: 0, False: 0]
|
173 | | |
174 | | // Compute sighash |
175 | 0 | std::optional<uint256> sighash = ComputeSchnorrSignatureHash(leaf_hash, sigversion); |
176 | 0 | if (!sighash.has_value()) return false; Branch (176:9): [True: 0, False: 0]
|
177 | | |
178 | | // Retrieve the secnonce |
179 | 0 | auto part_pubnonce_it = pubnonces.find(part_pubkey); |
180 | 0 | if (part_pubnonce_it == pubnonces.end()) return false; Branch (180:9): [True: 0, False: 0]
|
181 | 0 | uint256 session_id = MuSig2SessionID(script_pubkey, part_pubkey, *sighash, part_pubnonce_it->second); |
182 | 0 | std::optional<std::reference_wrapper<MuSig2SecNonce>> secnonce = provider.GetMuSig2SecNonce(session_id); |
183 | 0 | if (!secnonce || !secnonce->get().IsValid()) return false; Branch (183:9): [True: 0, False: 0]
Branch (183:22): [True: 0, False: 0]
|
184 | | |
185 | | // Compute the sig |
186 | 0 | std::optional<uint256> sig = ::CreateMuSig2PartialSig(*sighash, key, aggregate_pubkey, pubkeys, pubnonces, *secnonce, tweaks); |
187 | 0 | if (!sig) return false; Branch (187:9): [True: 0, False: 0]
|
188 | 0 | partial_sig = std::move(*sig); |
189 | | |
190 | | // Delete the secnonce now that we're done with it |
191 | 0 | assert(!secnonce->get().IsValid()); Branch (191:5): [True: 0, False: 0]
|
192 | 0 | provider.DeleteMuSig2Session(session_id); |
193 | |
|
194 | 0 | return true; |
195 | 0 | } |
196 | | |
197 | | bool MutableTransactionSignatureCreator::CreateMuSig2AggregateSig(const std::vector<CPubKey>& participants, std::vector<uint8_t>& sig, const CPubKey& aggregate_pubkey, const CPubKey& script_pubkey, const uint256* leaf_hash, const std::vector<std::pair<uint256, bool>>& tweaks, SigVersion sigversion, const SignatureData& sigdata) const |
198 | 0 | { |
199 | 0 | assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); Branch (199:5): [True: 0, False: 0]
Branch (199:5): [True: 0, False: 0]
Branch (199:5): [True: 0, False: 0]
|
200 | 0 | if (!participants.size()) return false; Branch (200:9): [True: 0, False: 0]
|
201 | | |
202 | | // Retrieve pubnonces and partial sigs |
203 | 0 | auto this_leaf_aggkey = std::make_pair(script_pubkey, leaf_hash ? *leaf_hash : uint256()); Branch (203:59): [True: 0, False: 0]
|
204 | 0 | auto pubnonce_it = sigdata.musig2_pubnonces.find(this_leaf_aggkey); |
205 | 0 | if (pubnonce_it == sigdata.musig2_pubnonces.end()) return false; Branch (205:9): [True: 0, False: 0]
|
206 | 0 | const std::map<CPubKey, std::vector<uint8_t>>& pubnonces = pubnonce_it->second; |
207 | 0 | auto partial_sigs_it = sigdata.musig2_partial_sigs.find(this_leaf_aggkey); |
208 | 0 | if (partial_sigs_it == sigdata.musig2_partial_sigs.end()) return false; Branch (208:9): [True: 0, False: 0]
|
209 | 0 | const std::map<CPubKey, uint256>& partial_sigs = partial_sigs_it->second; |
210 | | |
211 | | // Check if enough pubnonces and partial sigs |
212 | 0 | if (pubnonces.size() != participants.size()) return false; Branch (212:9): [True: 0, False: 0]
|
213 | 0 | if (partial_sigs.size() != participants.size()) return false; Branch (213:9): [True: 0, False: 0]
|
214 | | |
215 | | // Compute sighash |
216 | 0 | std::optional<uint256> sighash = ComputeSchnorrSignatureHash(leaf_hash, sigversion); |
217 | 0 | if (!sighash.has_value()) return false; Branch (217:9): [True: 0, False: 0]
|
218 | | |
219 | 0 | std::optional<std::vector<uint8_t>> res = ::CreateMuSig2AggregateSig(participants, aggregate_pubkey, tweaks, *sighash, pubnonces, partial_sigs); |
220 | 0 | if (!res) return false; Branch (220:9): [True: 0, False: 0]
|
221 | 0 | sig = res.value(); |
222 | 0 | if (m_options.sighash_type) sig.push_back(m_options.sighash_type); Branch (222:9): [True: 0, False: 0]
|
223 | |
|
224 | 0 | return true; |
225 | 0 | } |
226 | | |
227 | | static bool GetCScript(const SigningProvider& provider, const SignatureData& sigdata, const CScriptID& scriptid, CScript& script) |
228 | 0 | { |
229 | 0 | if (provider.GetCScript(scriptid, script)) { Branch (229:9): [True: 0, False: 0]
|
230 | 0 | return true; |
231 | 0 | } |
232 | | // Look for scripts in SignatureData |
233 | 0 | if (CScriptID(sigdata.redeem_script) == scriptid) { Branch (233:9): [True: 0, False: 0]
|
234 | 0 | script = sigdata.redeem_script; |
235 | 0 | return true; |
236 | 0 | } else if (CScriptID(sigdata.witness_script) == scriptid) { Branch (236:16): [True: 0, False: 0]
|
237 | 0 | script = sigdata.witness_script; |
238 | 0 | return true; |
239 | 0 | } |
240 | 0 | return false; |
241 | 0 | } |
242 | | |
243 | | static bool GetPubKey(const SigningProvider& provider, const SignatureData& sigdata, const CKeyID& address, CPubKey& pubkey) |
244 | 0 | { |
245 | | // Look for pubkey in all partial sigs |
246 | 0 | const auto it = sigdata.signatures.find(address); |
247 | 0 | if (it != sigdata.signatures.end()) { Branch (247:9): [True: 0, False: 0]
|
248 | 0 | pubkey = it->second.first; |
249 | 0 | return true; |
250 | 0 | } |
251 | | // Look for pubkey in pubkey lists |
252 | 0 | const auto& pk_it = sigdata.misc_pubkeys.find(address); |
253 | 0 | if (pk_it != sigdata.misc_pubkeys.end()) { Branch (253:9): [True: 0, False: 0]
|
254 | 0 | pubkey = pk_it->second.first; |
255 | 0 | return true; |
256 | 0 | } |
257 | 0 | const auto& tap_pk_it = sigdata.tap_pubkeys.find(address); |
258 | 0 | if (tap_pk_it != sigdata.tap_pubkeys.end()) { Branch (258:9): [True: 0, False: 0]
|
259 | 0 | pubkey = tap_pk_it->second.GetEvenCorrespondingCPubKey(); |
260 | 0 | return true; |
261 | 0 | } |
262 | | // Query the underlying provider |
263 | 0 | return provider.GetPubKey(address, pubkey); |
264 | 0 | } |
265 | | |
266 | | static bool CreateSig(const BaseSignatureCreator& creator, SignatureData& sigdata, const SigningProvider& provider, std::vector<unsigned char>& sig_out, const CPubKey& pubkey, const CScript& scriptcode, SigVersion sigversion) |
267 | 0 | { |
268 | 0 | CKeyID keyid = pubkey.GetID(); |
269 | 0 | const auto it = sigdata.signatures.find(keyid); |
270 | 0 | if (it != sigdata.signatures.end()) { Branch (270:9): [True: 0, False: 0]
|
271 | 0 | sig_out = it->second.second; |
272 | 0 | return true; |
273 | 0 | } |
274 | 0 | KeyOriginInfo info; |
275 | 0 | if (provider.GetKeyOrigin(keyid, info)) { Branch (275:9): [True: 0, False: 0]
|
276 | 0 | sigdata.misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info))); |
277 | 0 | } |
278 | 0 | if (creator.CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) { Branch (278:9): [True: 0, False: 0]
|
279 | 0 | auto i = sigdata.signatures.emplace(keyid, SigPair(pubkey, sig_out)); |
280 | 0 | assert(i.second); Branch (280:9): [True: 0, False: 0]
|
281 | 0 | return true; |
282 | 0 | } |
283 | | // Could not make signature or signature not found, add keyid to missing |
284 | 0 | sigdata.missing_sigs.push_back(keyid); |
285 | 0 | return false; |
286 | 0 | } |
287 | | |
288 | | static bool SignMuSig2(const BaseSignatureCreator& creator, SignatureData& sigdata, const SigningProvider& provider, std::vector<unsigned char>& sig_out, const XOnlyPubKey& script_pubkey, const uint256* merkle_root, const uint256* leaf_hash, SigVersion sigversion) |
289 | 0 | { |
290 | 0 | Assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); |
291 | | |
292 | | // Lookup derivation paths for the script pubkey |
293 | 0 | KeyOriginInfo agg_info; |
294 | 0 | auto misc_pk_it = sigdata.taproot_misc_pubkeys.find(script_pubkey); |
295 | 0 | if (misc_pk_it != sigdata.taproot_misc_pubkeys.end()) { Branch (295:9): [True: 0, False: 0]
|
296 | 0 | agg_info = misc_pk_it->second.second; |
297 | 0 | } |
298 | |
|
299 | 0 | for (const auto& [agg_pub, part_pks] : sigdata.musig2_pubkeys) { Branch (299:42): [True: 0, False: 0]
|
300 | 0 | if (part_pks.empty()) continue; Branch (300:13): [True: 0, False: 0]
|
301 | | |
302 | | // Fill participant derivation path info |
303 | 0 | for (const auto& part_pk : part_pks) { Branch (303:34): [True: 0, False: 0]
|
304 | 0 | KeyOriginInfo part_info; |
305 | 0 | if (provider.GetKeyOrigin(part_pk.GetID(), part_info)) { Branch (305:17): [True: 0, False: 0]
|
306 | 0 | XOnlyPubKey xonly_part(part_pk); |
307 | 0 | auto it = sigdata.taproot_misc_pubkeys.find(xonly_part); |
308 | 0 | if (it == sigdata.taproot_misc_pubkeys.end()) { Branch (308:21): [True: 0, False: 0]
|
309 | 0 | it = sigdata.taproot_misc_pubkeys.emplace(xonly_part, std::make_pair(std::set<uint256>(), part_info)).first; |
310 | 0 | } |
311 | 0 | if (leaf_hash) it->second.first.insert(*leaf_hash); Branch (311:21): [True: 0, False: 0]
|
312 | 0 | } |
313 | 0 | } |
314 | | |
315 | | // The pubkey in the script may not be the actual aggregate of the participants, but derived from it. |
316 | | // Check the derivation, and compute the BIP 32 derivation tweaks |
317 | 0 | std::vector<std::pair<uint256, bool>> tweaks; |
318 | 0 | CPubKey plain_pub = agg_pub; |
319 | 0 | if (XOnlyPubKey(agg_pub) != script_pubkey) { Branch (319:13): [True: 0, False: 0]
|
320 | 0 | if (agg_info.path.empty()) continue; Branch (320:17): [True: 0, False: 0]
|
321 | 0 | if (agg_info.fingerprint != agg_pub.GetID().fingerprint()) { Branch (321:17): [True: 0, False: 0]
|
322 | 0 | continue; |
323 | 0 | } |
324 | | // Get the BIP32 derivation tweaks |
325 | 0 | CExtPubKey extpub = CreateMuSig2SyntheticXpub(agg_pub); |
326 | 0 | for (const uint32_t i : agg_info.path) { Branch (326:35): [True: 0, False: 0]
|
327 | 0 | if (i >> 31) return false; // Hardened derivation is not possible from a public key Branch (327:21): [True: 0, False: 0]
|
328 | 0 | auto& [t, xonly] = tweaks.emplace_back(); |
329 | 0 | xonly = false; |
330 | 0 | if (!extpub.Derive(extpub, i, &t)) { Branch (330:21): [True: 0, False: 0]
|
331 | 0 | return false; |
332 | 0 | } |
333 | 0 | } |
334 | 0 | if (XOnlyPubKey(extpub.pubkey) != script_pubkey) continue; Branch (334:17): [True: 0, False: 0]
|
335 | 0 | plain_pub = extpub.pubkey; |
336 | 0 | } |
337 | | |
338 | | // Add the merkle root tweak |
339 | 0 | if (sigversion == SigVersion::TAPROOT && merkle_root) { Branch (339:13): [True: 0, False: 0]
Branch (339:50): [True: 0, False: 0]
|
340 | 0 | tweaks.emplace_back(script_pubkey.ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root), true); Branch (340:67): [True: 0, False: 0]
|
341 | 0 | std::optional<std::pair<XOnlyPubKey, bool>> tweaked = script_pubkey.CreateTapTweak(merkle_root->IsNull() ? nullptr : merkle_root); Branch (341:96): [True: 0, False: 0]
|
342 | 0 | if (!Assume(tweaked)) return false; Branch (342:17): [True: 0, False: 0]
|
343 | 0 | plain_pub = tweaked->first.GetCPubKeys().at(tweaked->second ? 1 : 0); Branch (343:57): [True: 0, False: 0]
|
344 | 0 | } |
345 | | |
346 | | // First try to aggregate |
347 | 0 | if (creator.CreateMuSig2AggregateSig(part_pks, sig_out, agg_pub, plain_pub, leaf_hash, tweaks, sigversion, sigdata)) { Branch (347:13): [True: 0, False: 0]
|
348 | 0 | if (sigversion == SigVersion::TAPROOT) { Branch (348:17): [True: 0, False: 0]
|
349 | 0 | sigdata.taproot_key_path_sig = sig_out; |
350 | 0 | } else { |
351 | 0 | auto lookup_key = std::make_pair(script_pubkey, leaf_hash ? *leaf_hash : uint256()); Branch (351:65): [True: 0, False: 0]
|
352 | 0 | sigdata.taproot_script_sigs[lookup_key] = sig_out; |
353 | 0 | } |
354 | 0 | continue; |
355 | 0 | } |
356 | | // Cannot aggregate, try making partial sigs for every participant |
357 | 0 | auto pub_key_leaf_hash = std::make_pair(plain_pub, leaf_hash ? *leaf_hash : uint256()); Branch (357:60): [True: 0, False: 0]
|
358 | 0 | for (const CPubKey& part_pk : part_pks) { Branch (358:37): [True: 0, False: 0]
|
359 | 0 | uint256 partial_sig; |
360 | 0 | if (creator.CreateMuSig2PartialSig(provider, partial_sig, agg_pub, plain_pub, part_pk, leaf_hash, tweaks, sigversion, sigdata) && Assume(!partial_sig.IsNull())) { Branch (360:17): [True: 0, False: 0]
Branch (360:17): [True: 0, False: 0]
|
361 | 0 | sigdata.musig2_partial_sigs[pub_key_leaf_hash].emplace(part_pk, partial_sig); |
362 | 0 | } |
363 | 0 | } |
364 | | // If there are any partial signatures, continue with next aggregate pubkey |
365 | 0 | auto partial_sigs_it = sigdata.musig2_partial_sigs.find(pub_key_leaf_hash); |
366 | 0 | if (partial_sigs_it != sigdata.musig2_partial_sigs.end() && !partial_sigs_it->second.empty()) { Branch (366:13): [True: 0, False: 0]
Branch (366:13): [True: 0, False: 0]
Branch (366:69): [True: 0, False: 0]
|
367 | 0 | continue; |
368 | 0 | } |
369 | | // No partial sigs, try to make pubnonces |
370 | 0 | std::map<CPubKey, std::vector<uint8_t>>& pubnonces = sigdata.musig2_pubnonces[pub_key_leaf_hash]; |
371 | 0 | for (const CPubKey& part_pk : part_pks) { Branch (371:37): [True: 0, False: 0]
|
372 | 0 | if (pubnonces.contains(part_pk)) continue; Branch (372:17): [True: 0, False: 0]
|
373 | 0 | std::vector<uint8_t> pubnonce = creator.CreateMuSig2Nonce(provider, agg_pub, plain_pub, part_pk, leaf_hash, merkle_root, sigversion, sigdata); |
374 | 0 | if (pubnonce.empty()) continue; Branch (374:17): [True: 0, False: 0]
|
375 | 0 | pubnonces[part_pk] = std::move(pubnonce); |
376 | 0 | } |
377 | 0 | } |
378 | 0 | return true; |
379 | 0 | } |
380 | | |
381 | | static bool CreateTaprootScriptSig(const BaseSignatureCreator& creator, SignatureData& sigdata, const SigningProvider& provider, std::vector<unsigned char>& sig_out, const XOnlyPubKey& pubkey, const uint256& leaf_hash, SigVersion sigversion) |
382 | 0 | { |
383 | 0 | KeyOriginInfo info; |
384 | 0 | if (provider.GetKeyOriginByXOnly(pubkey, info)) { Branch (384:9): [True: 0, False: 0]
|
385 | 0 | auto it = sigdata.taproot_misc_pubkeys.find(pubkey); |
386 | 0 | if (it == sigdata.taproot_misc_pubkeys.end()) { Branch (386:13): [True: 0, False: 0]
|
387 | 0 | sigdata.taproot_misc_pubkeys.emplace(pubkey, std::make_pair(std::set<uint256>({leaf_hash}), info)); |
388 | 0 | } else { |
389 | 0 | it->second.first.insert(leaf_hash); |
390 | 0 | } |
391 | 0 | } |
392 | |
|
393 | 0 | auto lookup_key = std::make_pair(pubkey, leaf_hash); |
394 | 0 | auto it = sigdata.taproot_script_sigs.find(lookup_key); |
395 | 0 | if (it != sigdata.taproot_script_sigs.end()) { Branch (395:9): [True: 0, False: 0]
|
396 | 0 | sig_out = it->second; |
397 | 0 | return true; |
398 | 0 | } |
399 | | |
400 | 0 | if (creator.CreateSchnorrSig(provider, sig_out, pubkey, &leaf_hash, nullptr, sigversion)) { Branch (400:9): [True: 0, False: 0]
|
401 | 0 | sigdata.taproot_script_sigs[lookup_key] = sig_out; |
402 | 0 | } else if (!SignMuSig2(creator, sigdata, provider, sig_out, pubkey, /*merkle_root=*/nullptr, &leaf_hash, sigversion)) { Branch (402:16): [True: 0, False: 0]
|
403 | 0 | return false; |
404 | 0 | } |
405 | | |
406 | 0 | return sigdata.taproot_script_sigs.contains(lookup_key); |
407 | 0 | } |
408 | | |
409 | | template<typename M, typename K, typename V> |
410 | | miniscript::Availability MsLookupHelper(const M& map, const K& key, V& value) |
411 | 0 | { |
412 | 0 | auto it = map.find(key); |
413 | 0 | if (it != map.end()) { Branch (413:9): [True: 0, False: 0]
|
414 | 0 | value = it->second; |
415 | 0 | return miniscript::Availability::YES; |
416 | 0 | } |
417 | 0 | return miniscript::Availability::NO; |
418 | 0 | } |
419 | | |
420 | | /** |
421 | | * Context for solving a Miniscript. |
422 | | * If enough material (access to keys, hash preimages, ..) is given, produces a valid satisfaction. |
423 | | */ |
424 | | template<typename Pk> |
425 | | struct Satisfier { |
426 | | using Key = Pk; |
427 | | |
428 | | const SigningProvider& m_provider; |
429 | | SignatureData& m_sig_data; |
430 | | const BaseSignatureCreator& m_creator; |
431 | | const CScript& m_witness_script; |
432 | | //! The context of the script we are satisfying (either P2WSH or Tapscript). |
433 | | const miniscript::MiniscriptContext m_script_ctx; |
434 | | |
435 | | explicit Satisfier(const SigningProvider& provider LIFETIMEBOUND, SignatureData& sig_data LIFETIMEBOUND, |
436 | | const BaseSignatureCreator& creator LIFETIMEBOUND, |
437 | | const CScript& witscript LIFETIMEBOUND, |
438 | 0 | miniscript::MiniscriptContext script_ctx) : m_provider(provider), |
439 | 0 | m_sig_data(sig_data), |
440 | 0 | m_creator(creator), |
441 | 0 | m_witness_script(witscript), |
442 | 0 | m_script_ctx(script_ctx) {}Unexecuted instantiation: _ZN9SatisfierI11XOnlyPubKeyEC2ERK15SigningProviderR13SignatureDataRK20BaseSignatureCreatorRK7CScriptN10miniscript17MiniscriptContextE Unexecuted instantiation: _ZN9SatisfierI7CPubKeyEC2ERK15SigningProviderR13SignatureDataRK20BaseSignatureCreatorRK7CScriptN10miniscript17MiniscriptContextE |
443 | | |
444 | 0 | static bool KeyCompare(const Key& a, const Key& b) { |
445 | 0 | return a < b; |
446 | 0 | } Unexecuted instantiation: _ZN9SatisfierI11XOnlyPubKeyE10KeyCompareERKS0_S3_ Unexecuted instantiation: _ZN9SatisfierI7CPubKeyE10KeyCompareERKS0_S3_ |
447 | | |
448 | | //! Get a CPubKey from a key hash. Note the key hash may be of an xonly pubkey. |
449 | | template<typename I> |
450 | 0 | std::optional<CPubKey> CPubFromPKHBytes(I first, I last) const { |
451 | 0 | assert(last - first == 20); Branch (451:9): [True: 0, False: 0]
Branch (451:9): [True: 0, False: 0]
|
452 | 0 | CPubKey pubkey; |
453 | 0 | CKeyID key_id; |
454 | 0 | std::copy(first, last, key_id.begin()); |
455 | 0 | if (GetPubKey(m_provider, m_sig_data, key_id, pubkey)) return pubkey; Branch (455:13): [True: 0, False: 0]
Branch (455:13): [True: 0, False: 0]
|
456 | 0 | m_sig_data.missing_pubkeys.push_back(key_id); |
457 | 0 | return {}; |
458 | 0 | } Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE16CPubFromPKHBytesIN9__gnu_cxx17__normal_iteratorIPhSt6vectorIhSaIhEEEEEESt8optionalI7CPubKeyET_SD_ Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE16CPubFromPKHBytesIN9__gnu_cxx17__normal_iteratorIPhSt6vectorIhSaIhEEEEEESt8optionalIS0_ET_SC_ |
459 | | |
460 | | //! Conversion to raw public key. |
461 | 0 | std::vector<unsigned char> ToPKBytes(const Key& key) const { return {key.begin(), key.end()}; }Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE9ToPKBytesERKS0_ Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE9ToPKBytesERKS0_ |
462 | | |
463 | | //! Time lock satisfactions. |
464 | 0 | bool CheckAfter(uint32_t value) const { return m_creator.Checker().CheckLockTime(CScriptNum(value)); }Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE10CheckAfterEj Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE10CheckAfterEj |
465 | 0 | bool CheckOlder(uint32_t value) const { return m_creator.Checker().CheckSequence(CScriptNum(value)); }Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE10CheckOlderEj Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE10CheckOlderEj |
466 | | |
467 | | //! Hash preimage satisfactions. |
468 | 0 | miniscript::Availability SatSHA256(const std::vector<unsigned char>& hash, std::vector<unsigned char>& preimage) const { |
469 | 0 | return MsLookupHelper(m_sig_data.sha256_preimages, hash, preimage); |
470 | 0 | } Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE9SatSHA256ERKSt6vectorIhSaIhEERS4_ Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE9SatSHA256ERKSt6vectorIhSaIhEERS4_ |
471 | 0 | miniscript::Availability SatRIPEMD160(const std::vector<unsigned char>& hash, std::vector<unsigned char>& preimage) const { |
472 | 0 | return MsLookupHelper(m_sig_data.ripemd160_preimages, hash, preimage); |
473 | 0 | } Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE12SatRIPEMD160ERKSt6vectorIhSaIhEERS4_ Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE12SatRIPEMD160ERKSt6vectorIhSaIhEERS4_ |
474 | 0 | miniscript::Availability SatHASH256(const std::vector<unsigned char>& hash, std::vector<unsigned char>& preimage) const { |
475 | 0 | return MsLookupHelper(m_sig_data.hash256_preimages, hash, preimage); |
476 | 0 | } Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE10SatHASH256ERKSt6vectorIhSaIhEERS4_ Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE10SatHASH256ERKSt6vectorIhSaIhEERS4_ |
477 | 0 | miniscript::Availability SatHASH160(const std::vector<unsigned char>& hash, std::vector<unsigned char>& preimage) const { |
478 | 0 | return MsLookupHelper(m_sig_data.hash160_preimages, hash, preimage); |
479 | 0 | } Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE10SatHASH160ERKSt6vectorIhSaIhEERS4_ Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE10SatHASH160ERKSt6vectorIhSaIhEERS4_ |
480 | | |
481 | 0 | miniscript::MiniscriptContext MsContext() const { |
482 | 0 | return m_script_ctx; |
483 | 0 | } Unexecuted instantiation: _ZNK9SatisfierI11XOnlyPubKeyE9MsContextEv Unexecuted instantiation: _ZNK9SatisfierI7CPubKeyE9MsContextEv |
484 | | }; |
485 | | |
486 | | /** Miniscript satisfier specific to P2WSH context. */ |
487 | | struct WshSatisfier: Satisfier<CPubKey> { |
488 | | explicit WshSatisfier(const SigningProvider& provider LIFETIMEBOUND, SignatureData& sig_data LIFETIMEBOUND, |
489 | | const BaseSignatureCreator& creator LIFETIMEBOUND, const CScript& witscript LIFETIMEBOUND) |
490 | 0 | : Satisfier(provider, sig_data, creator, witscript, miniscript::MiniscriptContext::P2WSH) {} |
491 | | |
492 | | //! Conversion from a raw compressed public key. |
493 | | template <typename I> |
494 | 0 | std::optional<CPubKey> FromPKBytes(I first, I last) const { |
495 | 0 | CPubKey pubkey{first, last}; |
496 | 0 | if (pubkey.IsValid()) return pubkey; Branch (496:13): [True: 0, False: 0]
|
497 | 0 | return {}; |
498 | 0 | } |
499 | | |
500 | | //! Conversion from a raw compressed public key hash. |
501 | | template<typename I> |
502 | 0 | std::optional<CPubKey> FromPKHBytes(I first, I last) const { |
503 | 0 | return Satisfier::CPubFromPKHBytes(first, last); |
504 | 0 | } |
505 | | |
506 | | //! Satisfy an ECDSA signature check. |
507 | 0 | miniscript::Availability Sign(const CPubKey& key, std::vector<unsigned char>& sig) const { |
508 | 0 | if (CreateSig(m_creator, m_sig_data, m_provider, sig, key, m_witness_script, SigVersion::WITNESS_V0)) { Branch (508:13): [True: 0, False: 0]
|
509 | 0 | return miniscript::Availability::YES; |
510 | 0 | } |
511 | 0 | return miniscript::Availability::NO; |
512 | 0 | } |
513 | | }; |
514 | | |
515 | | /** Miniscript satisfier specific to Tapscript context. */ |
516 | | struct TapSatisfier: Satisfier<XOnlyPubKey> { |
517 | | const uint256& m_leaf_hash; |
518 | | |
519 | | explicit TapSatisfier(const SigningProvider& provider LIFETIMEBOUND, SignatureData& sig_data LIFETIMEBOUND, |
520 | | const BaseSignatureCreator& creator LIFETIMEBOUND, const CScript& script LIFETIMEBOUND, |
521 | | const uint256& leaf_hash LIFETIMEBOUND) |
522 | 0 | : Satisfier(provider, sig_data, creator, script, miniscript::MiniscriptContext::TAPSCRIPT), |
523 | 0 | m_leaf_hash(leaf_hash) {} |
524 | | |
525 | | //! Conversion from a raw xonly public key. |
526 | | template <typename I> |
527 | 0 | std::optional<XOnlyPubKey> FromPKBytes(I first, I last) const { |
528 | 0 | if (last - first != 32) return {}; Branch (528:13): [True: 0, False: 0]
|
529 | 0 | XOnlyPubKey pubkey; |
530 | 0 | std::copy(first, last, pubkey.begin()); |
531 | 0 | return pubkey; |
532 | 0 | } |
533 | | |
534 | | //! Conversion from a raw xonly public key hash. |
535 | | template<typename I> |
536 | 0 | std::optional<XOnlyPubKey> FromPKHBytes(I first, I last) const { |
537 | 0 | if (auto pubkey = Satisfier::CPubFromPKHBytes(first, last)) return XOnlyPubKey{*pubkey}; Branch (537:18): [True: 0, False: 0]
|
538 | 0 | return {}; |
539 | 0 | } |
540 | | |
541 | | //! Satisfy a BIP340 signature check. |
542 | 0 | miniscript::Availability Sign(const XOnlyPubKey& key, std::vector<unsigned char>& sig) const { |
543 | 0 | if (CreateTaprootScriptSig(m_creator, m_sig_data, m_provider, sig, key, m_leaf_hash, SigVersion::TAPSCRIPT)) { Branch (543:13): [True: 0, False: 0]
|
544 | 0 | return miniscript::Availability::YES; |
545 | 0 | } |
546 | 0 | return miniscript::Availability::NO; |
547 | 0 | } |
548 | | }; |
549 | | |
550 | | static bool SignTaprootScript(const SigningProvider& provider, const BaseSignatureCreator& creator, SignatureData& sigdata, int leaf_version, std::span<const unsigned char> script_bytes, std::vector<valtype>& result) |
551 | 0 | { |
552 | | // Only BIP342 tapscript signing is supported for now. |
553 | 0 | if (leaf_version != TAPROOT_LEAF_TAPSCRIPT) return false; Branch (553:9): [True: 0, False: 0]
|
554 | | |
555 | 0 | uint256 leaf_hash = ComputeTapleafHash(leaf_version, script_bytes); |
556 | 0 | CScript script = CScript(script_bytes.begin(), script_bytes.end()); |
557 | |
|
558 | 0 | TapSatisfier ms_satisfier{provider, sigdata, creator, script, leaf_hash}; |
559 | 0 | const auto ms = miniscript::FromScript(script, ms_satisfier); |
560 | 0 | return ms && ms->Satisfy(ms_satisfier, result) == miniscript::Availability::YES; Branch (560:12): [True: 0, False: 0]
Branch (560:18): [True: 0, False: 0]
|
561 | 0 | } |
562 | | |
563 | | static bool SignTaproot(const SigningProvider& provider, const BaseSignatureCreator& creator, const WitnessV1Taproot& output, SignatureData& sigdata, std::vector<valtype>& result) |
564 | 0 | { |
565 | 0 | TaprootSpendData spenddata; |
566 | 0 | TaprootBuilder builder; |
567 | | |
568 | | // Gather information about this output. |
569 | 0 | if (provider.GetTaprootSpendData(output, spenddata)) { Branch (569:9): [True: 0, False: 0]
|
570 | 0 | sigdata.tr_spenddata.Merge(spenddata); |
571 | 0 | } |
572 | 0 | if (provider.GetTaprootBuilder(output, builder)) { Branch (572:9): [True: 0, False: 0]
|
573 | 0 | sigdata.tr_builder = builder; |
574 | 0 | } |
575 | 0 | if (auto agg_keys = provider.GetAllMuSig2ParticipantPubkeys(); !agg_keys.empty()) { Branch (575:68): [True: 0, False: 0]
|
576 | 0 | sigdata.musig2_pubkeys.insert(agg_keys.begin(), agg_keys.end()); |
577 | 0 | } |
578 | | |
579 | | |
580 | | // Try key path spending. |
581 | 0 | { |
582 | 0 | KeyOriginInfo internal_key_info; |
583 | 0 | if (provider.GetKeyOriginByXOnly(sigdata.tr_spenddata.internal_key, internal_key_info)) { Branch (583:13): [True: 0, False: 0]
|
584 | 0 | auto it = sigdata.taproot_misc_pubkeys.find(sigdata.tr_spenddata.internal_key); |
585 | 0 | if (it == sigdata.taproot_misc_pubkeys.end()) { Branch (585:17): [True: 0, False: 0]
|
586 | 0 | sigdata.taproot_misc_pubkeys.emplace(sigdata.tr_spenddata.internal_key, std::make_pair(std::set<uint256>(), internal_key_info)); |
587 | 0 | } |
588 | 0 | } |
589 | |
|
590 | 0 | KeyOriginInfo output_key_info; |
591 | 0 | if (provider.GetKeyOriginByXOnly(output, output_key_info)) { Branch (591:13): [True: 0, False: 0]
|
592 | 0 | auto it = sigdata.taproot_misc_pubkeys.find(output); |
593 | 0 | if (it == sigdata.taproot_misc_pubkeys.end()) { Branch (593:17): [True: 0, False: 0]
|
594 | 0 | sigdata.taproot_misc_pubkeys.emplace(output, std::make_pair(std::set<uint256>(), output_key_info)); |
595 | 0 | } |
596 | 0 | } |
597 | |
|
598 | 0 | auto make_keypath_sig = [&](const XOnlyPubKey& pk, const uint256* merkle_root) { |
599 | 0 | std::vector<unsigned char> sig; |
600 | 0 | if (creator.CreateSchnorrSig(provider, sig, pk, nullptr, merkle_root, SigVersion::TAPROOT)) { Branch (600:17): [True: 0, False: 0]
|
601 | 0 | sigdata.taproot_key_path_sig = sig; |
602 | 0 | } else { |
603 | 0 | SignMuSig2(creator, sigdata, provider, sig, pk, merkle_root, /*leaf_hash=*/nullptr, SigVersion::TAPROOT); |
604 | 0 | } |
605 | 0 | }; |
606 | | |
607 | | // First try signing with internal key |
608 | 0 | if (sigdata.taproot_key_path_sig.size() == 0) { Branch (608:13): [True: 0, False: 0]
|
609 | 0 | make_keypath_sig(sigdata.tr_spenddata.internal_key, &sigdata.tr_spenddata.merkle_root); |
610 | 0 | } |
611 | | // Try signing with output key if still no signature |
612 | 0 | if (sigdata.taproot_key_path_sig.size() == 0) { Branch (612:13): [True: 0, False: 0]
|
613 | 0 | make_keypath_sig(output, nullptr); |
614 | 0 | } |
615 | 0 | if (sigdata.taproot_key_path_sig.size()) { Branch (615:13): [True: 0, False: 0]
|
616 | 0 | result = Vector(sigdata.taproot_key_path_sig); |
617 | 0 | return true; |
618 | 0 | } |
619 | 0 | } |
620 | | |
621 | | // Try script path spending. |
622 | 0 | std::vector<std::vector<unsigned char>> smallest_result_stack; |
623 | 0 | for (const auto& [key, control_blocks] : sigdata.tr_spenddata.scripts) { Branch (623:44): [True: 0, False: 0]
|
624 | 0 | const auto& [script, leaf_ver] = key; |
625 | 0 | std::vector<std::vector<unsigned char>> result_stack; |
626 | 0 | if (SignTaprootScript(provider, creator, sigdata, leaf_ver, script, result_stack)) { Branch (626:13): [True: 0, False: 0]
|
627 | 0 | result_stack.emplace_back(std::begin(script), std::end(script)); // Push the script |
628 | 0 | result_stack.push_back(*control_blocks.begin()); // Push the smallest control block |
629 | 0 | if (smallest_result_stack.size() == 0 || Branch (629:17): [True: 0, False: 0]
|
630 | 0 | GetSerializeSize(result_stack) < GetSerializeSize(smallest_result_stack)) { Branch (630:17): [True: 0, False: 0]
|
631 | 0 | smallest_result_stack = std::move(result_stack); |
632 | 0 | } |
633 | 0 | } |
634 | 0 | } |
635 | 0 | if (smallest_result_stack.size() != 0) { Branch (635:9): [True: 0, False: 0]
|
636 | 0 | result = std::move(smallest_result_stack); |
637 | 0 | return true; |
638 | 0 | } |
639 | | |
640 | 0 | return false; |
641 | 0 | } |
642 | | |
643 | | /** |
644 | | * Sign scriptPubKey using signature made with creator. |
645 | | * Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed), |
646 | | * unless whichTypeRet is TxoutType::SCRIPTHASH, in which case scriptSigRet is the redemption script. |
647 | | * Returns false if scriptPubKey could not be completely satisfied. |
648 | | */ |
649 | | static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, |
650 | | std::vector<valtype>& ret, TxoutType& whichTypeRet, SigVersion sigversion, SignatureData& sigdata) |
651 | 0 | { |
652 | 0 | CScript scriptRet; |
653 | 0 | ret.clear(); |
654 | 0 | std::vector<unsigned char> sig; |
655 | |
|
656 | 0 | std::vector<valtype> vSolutions; |
657 | 0 | whichTypeRet = Solver(scriptPubKey, vSolutions); |
658 | |
|
659 | 0 | switch (whichTypeRet) { Branch (659:13): [True: 0, False: 0]
|
660 | 0 | case TxoutType::NONSTANDARD: Branch (660:5): [True: 0, False: 0]
|
661 | 0 | case TxoutType::NULL_DATA: Branch (661:5): [True: 0, False: 0]
|
662 | 0 | case TxoutType::WITNESS_UNKNOWN: Branch (662:5): [True: 0, False: 0]
|
663 | 0 | return false; |
664 | 0 | case TxoutType::PUBKEY: Branch (664:5): [True: 0, False: 0]
|
665 | 0 | if (!CreateSig(creator, sigdata, provider, sig, CPubKey(vSolutions[0]), scriptPubKey, sigversion)) return false; Branch (665:13): [True: 0, False: 0]
|
666 | 0 | ret.push_back(std::move(sig)); |
667 | 0 | return true; |
668 | 0 | case TxoutType::PUBKEYHASH: { Branch (668:5): [True: 0, False: 0]
|
669 | 0 | CKeyID keyID = CKeyID(uint160(vSolutions[0])); |
670 | 0 | CPubKey pubkey; |
671 | 0 | if (!GetPubKey(provider, sigdata, keyID, pubkey)) { Branch (671:13): [True: 0, False: 0]
|
672 | | // Pubkey could not be found, add to missing |
673 | 0 | sigdata.missing_pubkeys.push_back(keyID); |
674 | 0 | return false; |
675 | 0 | } |
676 | 0 | if (!CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) return false; Branch (676:13): [True: 0, False: 0]
|
677 | 0 | ret.push_back(std::move(sig)); |
678 | 0 | ret.push_back(ToByteVector(pubkey)); |
679 | 0 | return true; |
680 | 0 | } |
681 | 0 | case TxoutType::SCRIPTHASH: { Branch (681:5): [True: 0, False: 0]
|
682 | 0 | uint160 h160{vSolutions[0]}; |
683 | 0 | if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) { Branch (683:13): [True: 0, False: 0]
|
684 | 0 | ret.emplace_back(scriptRet.begin(), scriptRet.end()); |
685 | 0 | return true; |
686 | 0 | } |
687 | | // Could not find redeemScript, add to missing |
688 | 0 | sigdata.missing_redeem_script = h160; |
689 | 0 | return false; |
690 | 0 | } |
691 | 0 | case TxoutType::MULTISIG: { Branch (691:5): [True: 0, False: 0]
|
692 | 0 | size_t required = vSolutions.front()[0]; |
693 | 0 | ret.emplace_back(); // workaround CHECKMULTISIG bug |
694 | 0 | for (size_t i = 1; i < vSolutions.size() - 1; ++i) { Branch (694:28): [True: 0, False: 0]
|
695 | 0 | CPubKey pubkey = CPubKey(vSolutions[i]); |
696 | | // We need to always call CreateSig in order to fill sigdata with all |
697 | | // possible signatures that we can create. This will allow further PSBT |
698 | | // processing to work as it needs all possible signature and pubkey pairs |
699 | 0 | if (CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) { Branch (699:17): [True: 0, False: 0]
|
700 | 0 | if (ret.size() < required + 1) { Branch (700:21): [True: 0, False: 0]
|
701 | 0 | ret.push_back(std::move(sig)); |
702 | 0 | } |
703 | 0 | } |
704 | 0 | } |
705 | 0 | bool ok = ret.size() == required + 1; |
706 | 0 | for (size_t i = 0; i + ret.size() < required + 1; ++i) { Branch (706:28): [True: 0, False: 0]
|
707 | 0 | ret.emplace_back(); |
708 | 0 | } |
709 | 0 | return ok; |
710 | 0 | } |
711 | 0 | case TxoutType::WITNESS_V0_KEYHASH: Branch (711:5): [True: 0, False: 0]
|
712 | 0 | ret.push_back(vSolutions[0]); |
713 | 0 | return true; |
714 | | |
715 | 0 | case TxoutType::WITNESS_V0_SCRIPTHASH: Branch (715:5): [True: 0, False: 0]
|
716 | 0 | if (GetCScript(provider, sigdata, CScriptID{RIPEMD160(vSolutions[0])}, scriptRet)) { Branch (716:13): [True: 0, False: 0]
|
717 | 0 | ret.emplace_back(scriptRet.begin(), scriptRet.end()); |
718 | 0 | return true; |
719 | 0 | } |
720 | | // Could not find witnessScript, add to missing |
721 | 0 | sigdata.missing_witness_script = uint256(vSolutions[0]); |
722 | 0 | return false; |
723 | | |
724 | 0 | case TxoutType::WITNESS_V1_TAPROOT: Branch (724:5): [True: 0, False: 0]
|
725 | 0 | return SignTaproot(provider, creator, WitnessV1Taproot(XOnlyPubKey{vSolutions[0]}), sigdata, ret); |
726 | | |
727 | 0 | case TxoutType::ANCHOR: Branch (727:5): [True: 0, False: 0]
|
728 | 0 | return true; |
729 | 0 | } // no default case, so the compiler can warn about missing cases |
730 | 0 | assert(false); Branch (730:5): [Folded, False: 0]
|
731 | 0 | } |
732 | | |
733 | | static CScript PushAll(const std::vector<valtype>& values) |
734 | 0 | { |
735 | 0 | CScript result; |
736 | 0 | for (const valtype& v : values) { Branch (736:27): [True: 0, False: 0]
|
737 | 0 | if (v.size() == 0) { Branch (737:13): [True: 0, False: 0]
|
738 | 0 | result << OP_0; |
739 | 0 | } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) { Branch (739:20): [True: 0, False: 0]
Branch (739:37): [True: 0, False: 0]
Branch (739:50): [True: 0, False: 0]
|
740 | 0 | result << CScript::EncodeOP_N(v[0]); |
741 | 0 | } else if (v.size() == 1 && v[0] == 0x81) { Branch (741:20): [True: 0, False: 0]
Branch (741:37): [True: 0, False: 0]
|
742 | 0 | result << OP_1NEGATE; |
743 | 0 | } else { |
744 | 0 | result << v; |
745 | 0 | } |
746 | 0 | } |
747 | 0 | return result; |
748 | 0 | } |
749 | | |
750 | | bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& fromPubKey, SignatureData& sigdata) |
751 | 0 | { |
752 | 0 | if (sigdata.complete) return true; Branch (752:9): [True: 0, False: 0]
|
753 | | |
754 | 0 | std::vector<valtype> result; |
755 | 0 | TxoutType whichType; |
756 | 0 | bool solved = SignStep(provider, creator, fromPubKey, result, whichType, SigVersion::BASE, sigdata); |
757 | 0 | bool P2SH = false; |
758 | 0 | CScript subscript; |
759 | |
|
760 | 0 | if (solved && whichType == TxoutType::SCRIPTHASH) Branch (760:9): [True: 0, False: 0]
Branch (760:19): [True: 0, False: 0]
|
761 | 0 | { |
762 | | // Solver returns the subscript that needs to be evaluated; |
763 | | // the final scriptSig is the signatures from that |
764 | | // and then the serialized subscript: |
765 | 0 | subscript = CScript(result[0].begin(), result[0].end()); |
766 | 0 | sigdata.redeem_script = subscript; |
767 | 0 | solved = solved && SignStep(provider, creator, subscript, result, whichType, SigVersion::BASE, sigdata) && whichType != TxoutType::SCRIPTHASH; Branch (767:18): [True: 0, False: 0]
Branch (767:28): [True: 0, False: 0]
Branch (767:116): [True: 0, False: 0]
|
768 | 0 | P2SH = true; |
769 | 0 | } |
770 | |
|
771 | 0 | if (solved && whichType == TxoutType::WITNESS_V0_KEYHASH) Branch (771:9): [True: 0, False: 0]
Branch (771:19): [True: 0, False: 0]
|
772 | 0 | { |
773 | 0 | CScript witnessscript; |
774 | 0 | witnessscript << OP_DUP << OP_HASH160 << ToByteVector(result[0]) << OP_EQUALVERIFY << OP_CHECKSIG; |
775 | 0 | TxoutType subType; |
776 | 0 | solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata); Branch (776:18): [True: 0, False: 0]
Branch (776:28): [True: 0, False: 0]
|
777 | 0 | sigdata.scriptWitness.stack = result; |
778 | 0 | sigdata.witness = true; |
779 | 0 | result.clear(); |
780 | 0 | } |
781 | 0 | else if (solved && whichType == TxoutType::WITNESS_V0_SCRIPTHASH) Branch (781:14): [True: 0, False: 0]
Branch (781:24): [True: 0, False: 0]
|
782 | 0 | { |
783 | 0 | CScript witnessscript(result[0].begin(), result[0].end()); |
784 | 0 | sigdata.witness_script = witnessscript; |
785 | |
|
786 | 0 | TxoutType subType{TxoutType::NONSTANDARD}; |
787 | 0 | solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata) && subType != TxoutType::SCRIPTHASH && subType != TxoutType::WITNESS_V0_SCRIPTHASH && subType != TxoutType::WITNESS_V0_KEYHASH; Branch (787:18): [True: 0, False: 0]
Branch (787:28): [True: 0, False: 0]
Branch (787:124): [True: 0, False: 0]
Branch (787:160): [True: 0, False: 0]
Branch (787:207): [True: 0, False: 0]
|
788 | | |
789 | | // If we couldn't find a solution with the legacy satisfier, try satisfying the script using Miniscript. |
790 | | // Note we need to check if the result stack is empty before, because it might be used even if the Script |
791 | | // isn't fully solved. For instance the CHECKMULTISIG satisfaction in SignStep() pushes partial signatures |
792 | | // and the extractor relies on this behaviour to combine witnesses. |
793 | 0 | if (!solved && result.empty()) { Branch (793:13): [True: 0, False: 0]
Branch (793:24): [True: 0, False: 0]
|
794 | 0 | WshSatisfier ms_satisfier{provider, sigdata, creator, witnessscript}; |
795 | 0 | const auto ms = miniscript::FromScript(witnessscript, ms_satisfier); |
796 | 0 | solved = ms && ms->Satisfy(ms_satisfier, result) == miniscript::Availability::YES; Branch (796:22): [True: 0, False: 0]
Branch (796:28): [True: 0, False: 0]
|
797 | 0 | } |
798 | 0 | result.emplace_back(witnessscript.begin(), witnessscript.end()); |
799 | |
|
800 | 0 | sigdata.scriptWitness.stack = result; |
801 | 0 | sigdata.witness = true; |
802 | 0 | result.clear(); |
803 | 0 | } else if (whichType == TxoutType::WITNESS_V1_TAPROOT && !P2SH) { Branch (803:16): [True: 0, False: 0]
Branch (803:62): [True: 0, False: 0]
|
804 | 0 | sigdata.witness = true; |
805 | 0 | if (solved) { Branch (805:13): [True: 0, False: 0]
|
806 | 0 | sigdata.scriptWitness.stack = std::move(result); |
807 | 0 | } |
808 | 0 | result.clear(); |
809 | 0 | } else if (solved && whichType == TxoutType::WITNESS_UNKNOWN) { Branch (809:16): [True: 0, False: 0]
Branch (809:26): [True: 0, False: 0]
|
810 | 0 | sigdata.witness = true; |
811 | 0 | } |
812 | |
|
813 | 0 | if (!sigdata.witness) sigdata.scriptWitness.stack.clear(); Branch (813:9): [True: 0, False: 0]
|
814 | 0 | if (P2SH) { Branch (814:9): [True: 0, False: 0]
|
815 | 0 | result.emplace_back(subscript.begin(), subscript.end()); |
816 | 0 | } |
817 | 0 | sigdata.scriptSig = PushAll(result); |
818 | | |
819 | | // Test solution |
820 | 0 | sigdata.complete = solved && VerifyScript(sigdata.scriptSig, fromPubKey, &sigdata.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, creator.Checker()); Branch (820:24): [True: 0, False: 0]
Branch (820:34): [True: 0, False: 0]
|
821 | 0 | return sigdata.complete; |
822 | 0 | } |
823 | | |
824 | | namespace { |
825 | | class SignatureExtractorChecker final : public DeferringSignatureChecker |
826 | | { |
827 | | private: |
828 | | SignatureData& sigdata; |
829 | | |
830 | | public: |
831 | 0 | SignatureExtractorChecker(SignatureData& sigdata, BaseSignatureChecker& checker) : DeferringSignatureChecker(checker), sigdata(sigdata) {} |
832 | | |
833 | | bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override |
834 | 0 | { |
835 | 0 | if (m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion)) { Branch (835:13): [True: 0, False: 0]
|
836 | 0 | CPubKey pubkey(vchPubKey); |
837 | 0 | sigdata.signatures.emplace(pubkey.GetID(), SigPair(pubkey, scriptSig)); |
838 | 0 | return true; |
839 | 0 | } |
840 | 0 | return false; |
841 | 0 | } |
842 | | }; |
843 | | |
844 | | struct Stacks |
845 | | { |
846 | | std::vector<valtype> script; |
847 | | std::vector<valtype> witness; |
848 | | |
849 | | Stacks() = delete; |
850 | | Stacks(const Stacks&) = delete; |
851 | 0 | explicit Stacks(const SignatureData& data) : witness(data.scriptWitness.stack) { |
852 | 0 | EvalScript(script, data.scriptSig, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SigVersion::BASE); |
853 | 0 | } |
854 | | }; |
855 | | } |
856 | | |
857 | | // Extracts signatures and scripts from incomplete scriptSigs. Please do not extend this, use PSBT instead |
858 | | SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout) |
859 | 0 | { |
860 | 0 | SignatureData data; |
861 | 0 | assert(tx.vin.size() > nIn); Branch (861:5): [True: 0, False: 0]
|
862 | 0 | data.scriptSig = tx.vin[nIn].scriptSig; |
863 | 0 | data.scriptWitness = tx.vin[nIn].scriptWitness; |
864 | 0 | Stacks stack(data); |
865 | | |
866 | | // Get signatures |
867 | 0 | MutableTransactionSignatureChecker tx_checker(&tx, nIn, txout.nValue, MissingDataBehavior::FAIL); |
868 | 0 | SignatureExtractorChecker extractor_checker(data, tx_checker); |
869 | 0 | if (VerifyScript(data.scriptSig, txout.scriptPubKey, &data.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, extractor_checker)) { Branch (869:9): [True: 0, False: 0]
|
870 | 0 | data.complete = true; |
871 | 0 | return data; |
872 | 0 | } |
873 | | |
874 | | // Get scripts |
875 | 0 | std::vector<std::vector<unsigned char>> solutions; |
876 | 0 | TxoutType script_type = Solver(txout.scriptPubKey, solutions); |
877 | 0 | SigVersion sigversion = SigVersion::BASE; |
878 | 0 | CScript next_script = txout.scriptPubKey; |
879 | |
|
880 | 0 | if (script_type == TxoutType::SCRIPTHASH && !stack.script.empty() && !stack.script.back().empty()) { Branch (880:9): [True: 0, False: 0]
Branch (880:49): [True: 0, False: 0]
Branch (880:74): [True: 0, False: 0]
|
881 | | // Get the redeemScript |
882 | 0 | CScript redeem_script(stack.script.back().begin(), stack.script.back().end()); |
883 | 0 | data.redeem_script = redeem_script; |
884 | 0 | next_script = std::move(redeem_script); |
885 | | |
886 | | // Get redeemScript type |
887 | 0 | script_type = Solver(next_script, solutions); |
888 | 0 | stack.script.pop_back(); |
889 | 0 | } |
890 | 0 | if (script_type == TxoutType::WITNESS_V0_SCRIPTHASH && !stack.witness.empty() && !stack.witness.back().empty()) { Branch (890:9): [True: 0, False: 0]
Branch (890:60): [True: 0, False: 0]
Branch (890:86): [True: 0, False: 0]
|
891 | | // Get the witnessScript |
892 | 0 | CScript witness_script(stack.witness.back().begin(), stack.witness.back().end()); |
893 | 0 | data.witness_script = witness_script; |
894 | 0 | next_script = std::move(witness_script); |
895 | | |
896 | | // Get witnessScript type |
897 | 0 | script_type = Solver(next_script, solutions); |
898 | 0 | stack.witness.pop_back(); |
899 | 0 | stack.script = std::move(stack.witness); |
900 | 0 | stack.witness.clear(); |
901 | 0 | sigversion = SigVersion::WITNESS_V0; |
902 | 0 | } |
903 | 0 | if (script_type == TxoutType::MULTISIG && !stack.script.empty()) { Branch (903:9): [True: 0, False: 0]
Branch (903:47): [True: 0, False: 0]
|
904 | | // Build a map of pubkey -> signature by matching sigs to pubkeys: |
905 | 0 | assert(solutions.size() > 1); Branch (905:9): [True: 0, False: 0]
|
906 | 0 | unsigned int num_pubkeys = solutions.size()-2; |
907 | 0 | unsigned int last_success_key = 0; |
908 | 0 | for (const valtype& sig : stack.script) { Branch (908:33): [True: 0, False: 0]
|
909 | 0 | for (unsigned int i = last_success_key; i < num_pubkeys; ++i) { Branch (909:53): [True: 0, False: 0]
|
910 | 0 | const valtype& pubkey = solutions[i+1]; |
911 | | // We either have a signature for this pubkey, or we have found a signature and it is valid |
912 | 0 | if (data.signatures.contains(CPubKey(pubkey).GetID()) || extractor_checker.CheckECDSASignature(sig, pubkey, next_script, sigversion)) { Branch (912:21): [True: 0, False: 0]
Branch (912:21): [True: 0, False: 0]
Branch (912:74): [True: 0, False: 0]
|
913 | 0 | last_success_key = i + 1; |
914 | 0 | break; |
915 | 0 | } |
916 | 0 | } |
917 | 0 | } |
918 | 0 | } |
919 | | |
920 | 0 | return data; |
921 | 0 | } |
922 | | |
923 | | void UpdateInput(CTxIn& input, const SignatureData& data) |
924 | 0 | { |
925 | 0 | input.scriptSig = data.scriptSig; |
926 | 0 | input.scriptWitness = data.scriptWitness; |
927 | 0 | } |
928 | | |
929 | | void SignatureData::MergeSignatureData(SignatureData sigdata) |
930 | 0 | { |
931 | 0 | if (complete) return; Branch (931:9): [True: 0, False: 0]
|
932 | 0 | if (sigdata.complete) { Branch (932:9): [True: 0, False: 0]
|
933 | 0 | *this = std::move(sigdata); |
934 | 0 | return; |
935 | 0 | } |
936 | 0 | if (redeem_script.empty() && !sigdata.redeem_script.empty()) { Branch (936:9): [True: 0, False: 0]
Branch (936:34): [True: 0, False: 0]
|
937 | 0 | redeem_script = sigdata.redeem_script; |
938 | 0 | } |
939 | 0 | if (witness_script.empty() && !sigdata.witness_script.empty()) { Branch (939:9): [True: 0, False: 0]
Branch (939:35): [True: 0, False: 0]
|
940 | 0 | witness_script = sigdata.witness_script; |
941 | 0 | } |
942 | 0 | signatures.insert(std::make_move_iterator(sigdata.signatures.begin()), std::make_move_iterator(sigdata.signatures.end())); |
943 | 0 | } |
944 | | |
945 | | namespace { |
946 | | /** Dummy signature checker which accepts all signatures. */ |
947 | | class DummySignatureChecker final : public BaseSignatureChecker |
948 | | { |
949 | | public: |
950 | 0 | DummySignatureChecker() = default; |
951 | 0 | bool CheckECDSASignature(const std::vector<unsigned char>& sig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override { return sig.size() != 0; } |
952 | 0 | bool CheckSchnorrSignature(std::span<const unsigned char> sig, std::span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const override { return sig.size() != 0; } |
953 | 0 | bool CheckLockTime(const CScriptNum& nLockTime) const override { return true; } |
954 | 0 | bool CheckSequence(const CScriptNum& nSequence) const override { return true; } |
955 | | }; |
956 | | } |
957 | | |
958 | | const BaseSignatureChecker& DUMMY_CHECKER = DummySignatureChecker(); |
959 | | |
960 | | namespace { |
961 | | class DummySignatureCreator final : public BaseSignatureCreator { |
962 | | private: |
963 | | char m_r_len = 32; |
964 | | char m_s_len = 32; |
965 | | public: |
966 | 0 | DummySignatureCreator(char r_len, char s_len) : m_r_len(r_len), m_s_len(s_len) {} |
967 | 0 | const BaseSignatureChecker& Checker() const override { return DUMMY_CHECKER; } |
968 | | bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override |
969 | 0 | { |
970 | | // Create a dummy signature that is a valid DER-encoding |
971 | 0 | vchSig.assign(m_r_len + m_s_len + 7, '\000'); |
972 | 0 | vchSig[0] = 0x30; |
973 | 0 | vchSig[1] = m_r_len + m_s_len + 4; |
974 | 0 | vchSig[2] = 0x02; |
975 | 0 | vchSig[3] = m_r_len; |
976 | 0 | vchSig[4] = 0x01; |
977 | 0 | vchSig[4 + m_r_len] = 0x02; |
978 | 0 | vchSig[5 + m_r_len] = m_s_len; |
979 | 0 | vchSig[6 + m_r_len] = 0x01; |
980 | 0 | vchSig[6 + m_r_len + m_s_len] = SIGHASH_ALL; |
981 | 0 | return true; |
982 | 0 | } |
983 | | bool CreateSchnorrSig(const SigningProvider& provider, std::vector<unsigned char>& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* tweak, SigVersion sigversion) const override |
984 | 0 | { |
985 | 0 | sig.assign(64, '\000'); |
986 | 0 | return true; |
987 | 0 | } |
988 | | std::vector<uint8_t> CreateMuSig2Nonce(const SigningProvider& provider, const CPubKey& aggregate_pubkey, const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion, const SignatureData& sigdata) const override |
989 | 0 | { |
990 | 0 | std::vector<uint8_t> out; |
991 | 0 | out.assign(MUSIG2_PUBNONCE_SIZE, '\000'); |
992 | 0 | return out; |
993 | 0 | } |
994 | | bool CreateMuSig2PartialSig(const SigningProvider& provider, uint256& partial_sig, const CPubKey& aggregate_pubkey, const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256* leaf_hash, const std::vector<std::pair<uint256, bool>>& tweaks, SigVersion sigversion, const SignatureData& sigdata) const override |
995 | 0 | { |
996 | 0 | partial_sig = uint256::ONE; |
997 | 0 | return true; |
998 | 0 | } |
999 | | bool CreateMuSig2AggregateSig(const std::vector<CPubKey>& participants, std::vector<uint8_t>& sig, const CPubKey& aggregate_pubkey, const CPubKey& script_pubkey, const uint256* leaf_hash, const std::vector<std::pair<uint256, bool>>& tweaks, SigVersion sigversion, const SignatureData& sigdata) const override |
1000 | 0 | { |
1001 | 0 | sig.assign(64, '\000'); |
1002 | 0 | return true; |
1003 | 0 | } |
1004 | | }; |
1005 | | |
1006 | | } |
1007 | | |
1008 | | const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR = DummySignatureCreator(32, 32); |
1009 | | const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR = DummySignatureCreator(33, 32); |
1010 | | |
1011 | | bool IsSegWitOutput(const SigningProvider& provider, const CScript& script) |
1012 | 0 | { |
1013 | 0 | int version; |
1014 | 0 | valtype program; |
1015 | 0 | if (script.IsWitnessProgram(version, program)) return true; Branch (1015:9): [True: 0, False: 0]
|
1016 | 0 | if (script.IsPayToScriptHash()) { Branch (1016:9): [True: 0, False: 0]
|
1017 | 0 | std::vector<valtype> solutions; |
1018 | 0 | auto whichtype = Solver(script, solutions); |
1019 | 0 | if (whichtype == TxoutType::SCRIPTHASH) { Branch (1019:13): [True: 0, False: 0]
|
1020 | 0 | auto h160 = uint160(solutions[0]); |
1021 | 0 | CScript subscript; |
1022 | 0 | if (provider.GetCScript(CScriptID{h160}, subscript)) { Branch (1022:17): [True: 0, False: 0]
|
1023 | 0 | if (subscript.IsWitnessProgram(version, program)) return true; Branch (1023:21): [True: 0, False: 0]
|
1024 | 0 | } |
1025 | 0 | } |
1026 | 0 | } |
1027 | 0 | return false; |
1028 | 0 | } |
1029 | | |
1030 | | bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const SignOptions& options, std::map<int, bilingual_str>& input_errors) |
1031 | 0 | { |
1032 | | // Use CTransaction for the constant parts of the |
1033 | | // transaction to avoid rehashing. |
1034 | 0 | const CTransaction txConst(mtx); |
1035 | |
|
1036 | 0 | PrecomputedTransactionData txdata; |
1037 | 0 | std::vector<CTxOut> spent_outputs; |
1038 | 0 | for (unsigned int i = 0; i < mtx.vin.size(); ++i) { Branch (1038:30): [True: 0, False: 0]
|
1039 | 0 | CTxIn& txin = mtx.vin[i]; |
1040 | 0 | auto coin = coins.find(txin.prevout); |
1041 | 0 | if (coin == coins.end() || coin->second.IsSpent()) { Branch (1041:13): [True: 0, False: 0]
Branch (1041:13): [True: 0, False: 0]
Branch (1041:36): [True: 0, False: 0]
|
1042 | 0 | txdata.Init(txConst, /*spent_outputs=*/{}, /*force=*/true); |
1043 | 0 | break; |
1044 | 0 | } else { |
1045 | 0 | spent_outputs.emplace_back(coin->second.out.nValue, coin->second.out.scriptPubKey); |
1046 | 0 | } |
1047 | 0 | } |
1048 | 0 | if (spent_outputs.size() == mtx.vin.size()) { Branch (1048:9): [True: 0, False: 0]
|
1049 | 0 | txdata.Init(txConst, std::move(spent_outputs), true); |
1050 | 0 | } |
1051 | | |
1052 | | // Sign what we can: |
1053 | 0 | for (unsigned int i = 0; i < mtx.vin.size(); ++i) { Branch (1053:30): [True: 0, False: 0]
|
1054 | 0 | CTxIn& txin = mtx.vin[i]; |
1055 | 0 | auto coin = coins.find(txin.prevout); |
1056 | 0 | if (coin == coins.end() || coin->second.IsSpent()) { Branch (1056:13): [True: 0, False: 0]
Branch (1056:13): [True: 0, False: 0]
Branch (1056:36): [True: 0, False: 0]
|
1057 | 0 | input_errors[i] = _("Input not found or already spent"); |
1058 | 0 | continue; |
1059 | 0 | } |
1060 | 0 | const CScript& prevPubKey = coin->second.out.scriptPubKey; |
1061 | 0 | const CAmount& amount = coin->second.out.nValue; |
1062 | |
|
1063 | 0 | SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out); |
1064 | 0 | ProduceSignature(*keystore, MutableTransactionSignatureCreator(mtx, i, amount, &txdata, options), prevPubKey, sigdata); |
1065 | |
|
1066 | 0 | UpdateInput(txin, sigdata); |
1067 | | |
1068 | | // amount must be specified for valid segwit signature |
1069 | 0 | if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) { Branch (1069:13): [True: 0, False: 0]
Branch (1069:36): [True: 0, False: 0]
|
1070 | 0 | input_errors[i] = _("Missing amount"); |
1071 | 0 | continue; |
1072 | 0 | } |
1073 | | |
1074 | 0 | ScriptError serror = SCRIPT_ERR_OK; |
1075 | 0 | if (!sigdata.complete && !VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) { Branch (1075:13): [True: 0, False: 0]
Branch (1075:13): [True: 0, False: 0]
Branch (1075:34): [True: 0, False: 0]
|
1076 | 0 | if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) { Branch (1076:17): [True: 0, False: 0]
|
1077 | | // Unable to sign input and verification failed (possible attempt to partially sign). |
1078 | 0 | input_errors[i] = Untranslated("Unable to sign input, invalid stack size (possibly missing key)"); |
1079 | 0 | } else if (serror == SCRIPT_ERR_SIG_NULLFAIL) { Branch (1079:24): [True: 0, False: 0]
|
1080 | | // Verification failed (possibly due to insufficient signatures). |
1081 | 0 | input_errors[i] = Untranslated("CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)"); |
1082 | 0 | } else { |
1083 | 0 | input_errors[i] = Untranslated(ScriptErrorString(serror)); |
1084 | 0 | } |
1085 | 0 | } else { |
1086 | | // If this input succeeds, make sure there is no error set for it |
1087 | 0 | input_errors.erase(i); |
1088 | 0 | } |
1089 | 0 | } |
1090 | 0 | return input_errors.empty(); |
1091 | 0 | } |