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