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