Bitcoin Core  22.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-2021 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 <consensus/amount.h>
9 #include <key.h>
10 #include <policy/policy.h>
11 #include <primitives/transaction.h>
12 #include <script/keyorigin.h>
13 #include <script/signingprovider.h>
14 #include <script/standard.h>
15 #include <uint256.h>
16 #include <util/translation.h>
17 #include <util/vector.h>
18 
19 typedef std::vector<unsigned char> valtype;
20 
21 MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* tx, unsigned int input_idx, const CAmount& amount, int hash_type)
22  : txTo{tx}, nIn{input_idx}, nHashType{hash_type}, amount{amount}, checker{txTo, nIn, amount, MissingDataBehavior::FAIL},
23  m_txdata(nullptr)
24 {
25 }
26 
27 MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* tx, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type)
28  : txTo{tx}, nIn{input_idx}, nHashType{hash_type}, amount{amount},
29  checker{txdata ? MutableTransactionSignatureChecker{txTo, nIn, amount, *txdata, MissingDataBehavior::FAIL} :
31  m_txdata(txdata)
32 {
33 }
34 
35 bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& address, const CScript& scriptCode, SigVersion sigversion) const
36 {
37  assert(sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0);
38 
39  CKey key;
40  if (!provider.GetKey(address, key))
41  return false;
42 
43  // Signing with uncompressed keys is disabled in witness scripts
44  if (sigversion == SigVersion::WITNESS_V0 && !key.IsCompressed())
45  return false;
46 
47  // Signing without known amount does not work in witness scripts.
48  if (sigversion == SigVersion::WITNESS_V0 && !MoneyRange(amount)) return false;
49 
50  // BASE/WITNESS_V0 signatures don't support explicit SIGHASH_DEFAULT, use SIGHASH_ALL instead.
51  const int hashtype = nHashType == SIGHASH_DEFAULT ? SIGHASH_ALL : nHashType;
52 
53  uint256 hash = SignatureHash(scriptCode, *txTo, nIn, hashtype, amount, sigversion, m_txdata);
54  if (!key.Sign(hash, vchSig))
55  return false;
56  vchSig.push_back((unsigned char)hashtype);
57  return true;
58 }
59 
60 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
61 {
62  assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT);
63 
64  CKey key;
65  if (!provider.GetKeyByXOnly(pubkey, key)) return false;
66 
67  // BIP341/BIP342 signing needs lots of precomputed transaction data. While some
68  // (non-SIGHASH_DEFAULT) sighash modes exist that can work with just some subset
69  // of data present, for now, only support signing when everything is provided.
71 
72  ScriptExecutionData execdata;
73  execdata.m_annex_init = true;
74  execdata.m_annex_present = false; // Only support annex-less signing for now.
75  if (sigversion == SigVersion::TAPSCRIPT) {
76  execdata.m_codeseparator_pos_init = true;
77  execdata.m_codeseparator_pos = 0xFFFFFFFF; // Only support non-OP_CODESEPARATOR BIP342 signing for now.
78  if (!leaf_hash) return false; // BIP342 signing needs leaf hash.
79  execdata.m_tapleaf_hash_init = true;
80  execdata.m_tapleaf_hash = *leaf_hash;
81  }
82  uint256 hash;
83  if (!SignatureHashSchnorr(hash, execdata, *txTo, nIn, nHashType, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return false;
84  sig.resize(64);
85  // Use uint256{} as aux_rnd for now.
86  if (!key.SignSchnorr(hash, sig, merkle_root, {})) return false;
87  if (nHashType) sig.push_back(nHashType);
88  return true;
89 }
90 
91 static bool GetCScript(const SigningProvider& provider, const SignatureData& sigdata, const CScriptID& scriptid, CScript& script)
92 {
93  if (provider.GetCScript(scriptid, script)) {
94  return true;
95  }
96  // Look for scripts in SignatureData
97  if (CScriptID(sigdata.redeem_script) == scriptid) {
98  script = sigdata.redeem_script;
99  return true;
100  } else if (CScriptID(sigdata.witness_script) == scriptid) {
101  script = sigdata.witness_script;
102  return true;
103  }
104  return false;
105 }
106 
107 static bool GetPubKey(const SigningProvider& provider, const SignatureData& sigdata, const CKeyID& address, CPubKey& pubkey)
108 {
109  // Look for pubkey in all partial sigs
110  const auto it = sigdata.signatures.find(address);
111  if (it != sigdata.signatures.end()) {
112  pubkey = it->second.first;
113  return true;
114  }
115  // Look for pubkey in pubkey list
116  const auto& pk_it = sigdata.misc_pubkeys.find(address);
117  if (pk_it != sigdata.misc_pubkeys.end()) {
118  pubkey = pk_it->second.first;
119  return true;
120  }
121  // Query the underlying provider
122  return provider.GetPubKey(address, pubkey);
123 }
124 
125 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)
126 {
127  CKeyID keyid = pubkey.GetID();
128  const auto it = sigdata.signatures.find(keyid);
129  if (it != sigdata.signatures.end()) {
130  sig_out = it->second.second;
131  return true;
132  }
133  KeyOriginInfo info;
134  if (provider.GetKeyOrigin(keyid, info)) {
135  sigdata.misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info)));
136  }
137  if (creator.CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) {
138  auto i = sigdata.signatures.emplace(keyid, SigPair(pubkey, sig_out));
139  assert(i.second);
140  return true;
141  }
142  // Could not make signature or signature not found, add keyid to missing
143  sigdata.missing_sigs.push_back(keyid);
144  return false;
145 }
146 
147 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)
148 {
149  auto lookup_key = std::make_pair(pubkey, leaf_hash);
150  auto it = sigdata.taproot_script_sigs.find(lookup_key);
151  if (it != sigdata.taproot_script_sigs.end()) {
152  sig_out = it->second;
153  }
154  if (creator.CreateSchnorrSig(provider, sig_out, pubkey, &leaf_hash, nullptr, sigversion)) {
155  sigdata.taproot_script_sigs[lookup_key] = sig_out;
156  return true;
157  }
158  return false;
159 }
160 
161 static bool SignTaprootScript(const SigningProvider& provider, const BaseSignatureCreator& creator, SignatureData& sigdata, int leaf_version, const CScript& script, std::vector<valtype>& result)
162 {
163  // Only BIP342 tapscript signing is supported for now.
164  if (leaf_version != TAPROOT_LEAF_TAPSCRIPT) return false;
165  SigVersion sigversion = SigVersion::TAPSCRIPT;
166 
167  uint256 leaf_hash = (CHashWriter(HASHER_TAPLEAF) << uint8_t(leaf_version) << script).GetSHA256();
168 
169  // <xonly pubkey> OP_CHECKSIG
170  if (script.size() == 34 && script[33] == OP_CHECKSIG && script[0] == 0x20) {
171  XOnlyPubKey pubkey{Span{script}.subspan(1, 32)};
172  std::vector<unsigned char> sig;
173  if (CreateTaprootScriptSig(creator, sigdata, provider, sig, pubkey, leaf_hash, sigversion)) {
174  result = Vector(std::move(sig));
175  return true;
176  }
177  }
178 
179  return false;
180 }
181 
182 static bool SignTaproot(const SigningProvider& provider, const BaseSignatureCreator& creator, const WitnessV1Taproot& output, SignatureData& sigdata, std::vector<valtype>& result)
183 {
184  TaprootSpendData spenddata;
185 
186  // Gather information about this output.
187  if (provider.GetTaprootSpendData(output, spenddata)) {
188  sigdata.tr_spenddata.Merge(spenddata);
189  }
190 
191  // Try key path spending.
192  {
193  std::vector<unsigned char> sig;
194  if (sigdata.taproot_key_path_sig.size() == 0) {
195  if (creator.CreateSchnorrSig(provider, sig, spenddata.internal_key, nullptr, &spenddata.merkle_root, SigVersion::TAPROOT)) {
196  sigdata.taproot_key_path_sig = sig;
197  }
198  }
199  if (sigdata.taproot_key_path_sig.size()) {
200  result = Vector(sigdata.taproot_key_path_sig);
201  return true;
202  }
203  }
204 
205  // Try script path spending.
206  std::vector<std::vector<unsigned char>> smallest_result_stack;
207  for (const auto& [key, control_blocks] : sigdata.tr_spenddata.scripts) {
208  const auto& [script, leaf_ver] = key;
209  std::vector<std::vector<unsigned char>> result_stack;
210  if (SignTaprootScript(provider, creator, sigdata, leaf_ver, script, result_stack)) {
211  result_stack.emplace_back(std::begin(script), std::end(script)); // Push the script
212  result_stack.push_back(*control_blocks.begin()); // Push the smallest control block
213  if (smallest_result_stack.size() == 0 ||
214  GetSerializeSize(result_stack, PROTOCOL_VERSION) < GetSerializeSize(smallest_result_stack, PROTOCOL_VERSION)) {
215  smallest_result_stack = std::move(result_stack);
216  }
217  }
218  }
219  if (smallest_result_stack.size() != 0) {
220  result = std::move(smallest_result_stack);
221  return true;
222  }
223 
224  return false;
225 }
226 
233 static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey,
234  std::vector<valtype>& ret, TxoutType& whichTypeRet, SigVersion sigversion, SignatureData& sigdata)
235 {
236  CScript scriptRet;
237  uint160 h160;
238  ret.clear();
239  std::vector<unsigned char> sig;
240 
241  std::vector<valtype> vSolutions;
242  whichTypeRet = Solver(scriptPubKey, vSolutions);
243 
244  switch (whichTypeRet) {
248  return false;
249  case TxoutType::PUBKEY:
250  if (!CreateSig(creator, sigdata, provider, sig, CPubKey(vSolutions[0]), scriptPubKey, sigversion)) return false;
251  ret.push_back(std::move(sig));
252  return true;
253  case TxoutType::PUBKEYHASH: {
254  CKeyID keyID = CKeyID(uint160(vSolutions[0]));
255  CPubKey pubkey;
256  if (!GetPubKey(provider, sigdata, keyID, pubkey)) {
257  // Pubkey could not be found, add to missing
258  sigdata.missing_pubkeys.push_back(keyID);
259  return false;
260  }
261  if (!CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) return false;
262  ret.push_back(std::move(sig));
263  ret.push_back(ToByteVector(pubkey));
264  return true;
265  }
267  h160 = uint160(vSolutions[0]);
268  if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
269  ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
270  return true;
271  }
272  // Could not find redeemScript, add to missing
273  sigdata.missing_redeem_script = h160;
274  return false;
275 
276  case TxoutType::MULTISIG: {
277  size_t required = vSolutions.front()[0];
278  ret.push_back(valtype()); // workaround CHECKMULTISIG bug
279  for (size_t i = 1; i < vSolutions.size() - 1; ++i) {
280  CPubKey pubkey = CPubKey(vSolutions[i]);
281  // We need to always call CreateSig in order to fill sigdata with all
282  // possible signatures that we can create. This will allow further PSBT
283  // processing to work as it needs all possible signature and pubkey pairs
284  if (CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) {
285  if (ret.size() < required + 1) {
286  ret.push_back(std::move(sig));
287  }
288  }
289  }
290  bool ok = ret.size() == required + 1;
291  for (size_t i = 0; i + ret.size() < required + 1; ++i) {
292  ret.push_back(valtype());
293  }
294  return ok;
295  }
297  ret.push_back(vSolutions[0]);
298  return true;
299 
301  CRIPEMD160().Write(vSolutions[0].data(), vSolutions[0].size()).Finalize(h160.begin());
302  if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
303  ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
304  return true;
305  }
306  // Could not find witnessScript, add to missing
307  sigdata.missing_witness_script = uint256(vSolutions[0]);
308  return false;
309 
311  return SignTaproot(provider, creator, WitnessV1Taproot(XOnlyPubKey{vSolutions[0]}), sigdata, ret);
312  } // no default case, so the compiler can warn about missing cases
313  assert(false);
314 }
315 
316 static CScript PushAll(const std::vector<valtype>& values)
317 {
318  CScript result;
319  for (const valtype& v : values) {
320  if (v.size() == 0) {
321  result << OP_0;
322  } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
323  result << CScript::EncodeOP_N(v[0]);
324  } else if (v.size() == 1 && v[0] == 0x81) {
325  result << OP_1NEGATE;
326  } else {
327  result << v;
328  }
329  }
330  return result;
331 }
332 
333 bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& fromPubKey, SignatureData& sigdata)
334 {
335  if (sigdata.complete) return true;
336 
337  std::vector<valtype> result;
338  TxoutType whichType;
339  bool solved = SignStep(provider, creator, fromPubKey, result, whichType, SigVersion::BASE, sigdata);
340  bool P2SH = false;
341  CScript subscript;
342 
343  if (solved && whichType == TxoutType::SCRIPTHASH)
344  {
345  // Solver returns the subscript that needs to be evaluated;
346  // the final scriptSig is the signatures from that
347  // and then the serialized subscript:
348  subscript = CScript(result[0].begin(), result[0].end());
349  sigdata.redeem_script = subscript;
350  solved = solved && SignStep(provider, creator, subscript, result, whichType, SigVersion::BASE, sigdata) && whichType != TxoutType::SCRIPTHASH;
351  P2SH = true;
352  }
353 
354  if (solved && whichType == TxoutType::WITNESS_V0_KEYHASH)
355  {
356  CScript witnessscript;
357  witnessscript << OP_DUP << OP_HASH160 << ToByteVector(result[0]) << OP_EQUALVERIFY << OP_CHECKSIG;
358  TxoutType subType;
359  solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata);
360  sigdata.scriptWitness.stack = result;
361  sigdata.witness = true;
362  result.clear();
363  }
364  else if (solved && whichType == TxoutType::WITNESS_V0_SCRIPTHASH)
365  {
366  CScript witnessscript(result[0].begin(), result[0].end());
367  sigdata.witness_script = witnessscript;
368  TxoutType subType;
369  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;
370  result.push_back(std::vector<unsigned char>(witnessscript.begin(), witnessscript.end()));
371  sigdata.scriptWitness.stack = result;
372  sigdata.witness = true;
373  result.clear();
374  } else if (whichType == TxoutType::WITNESS_V1_TAPROOT && !P2SH) {
375  sigdata.witness = true;
376  if (solved) {
377  sigdata.scriptWitness.stack = std::move(result);
378  }
379  result.clear();
380  } else if (solved && whichType == TxoutType::WITNESS_UNKNOWN) {
381  sigdata.witness = true;
382  }
383 
384  if (!sigdata.witness) sigdata.scriptWitness.stack.clear();
385  if (P2SH) {
386  result.push_back(std::vector<unsigned char>(subscript.begin(), subscript.end()));
387  }
388  sigdata.scriptSig = PushAll(result);
389 
390  // Test solution
391  sigdata.complete = solved && VerifyScript(sigdata.scriptSig, fromPubKey, &sigdata.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, creator.Checker());
392  return sigdata.complete;
393 }
394 
395 namespace {
396 class SignatureExtractorChecker final : public DeferringSignatureChecker
397 {
398 private:
399  SignatureData& sigdata;
400 
401 public:
402  SignatureExtractorChecker(SignatureData& sigdata, BaseSignatureChecker& checker) : DeferringSignatureChecker(checker), sigdata(sigdata) {}
403 
404  bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override
405  {
406  if (m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion)) {
407  CPubKey pubkey(vchPubKey);
408  sigdata.signatures.emplace(pubkey.GetID(), SigPair(pubkey, scriptSig));
409  return true;
410  }
411  return false;
412  }
413 };
414 
415 struct Stacks
416 {
417  std::vector<valtype> script;
418  std::vector<valtype> witness;
419 
420  Stacks() = delete;
421  Stacks(const Stacks&) = delete;
422  explicit Stacks(const SignatureData& data) : witness(data.scriptWitness.stack) {
424  }
425 };
426 }
427 
428 // Extracts signatures and scripts from incomplete scriptSigs. Please do not extend this, use PSBT instead
429 SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout)
430 {
431  SignatureData data;
432  assert(tx.vin.size() > nIn);
433  data.scriptSig = tx.vin[nIn].scriptSig;
434  data.scriptWitness = tx.vin[nIn].scriptWitness;
435  Stacks stack(data);
436 
437  // Get signatures
439  SignatureExtractorChecker extractor_checker(data, tx_checker);
440  if (VerifyScript(data.scriptSig, txout.scriptPubKey, &data.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, extractor_checker)) {
441  data.complete = true;
442  return data;
443  }
444 
445  // Get scripts
446  std::vector<std::vector<unsigned char>> solutions;
447  TxoutType script_type = Solver(txout.scriptPubKey, solutions);
448  SigVersion sigversion = SigVersion::BASE;
449  CScript next_script = txout.scriptPubKey;
450 
451  if (script_type == TxoutType::SCRIPTHASH && !stack.script.empty() && !stack.script.back().empty()) {
452  // Get the redeemScript
453  CScript redeem_script(stack.script.back().begin(), stack.script.back().end());
454  data.redeem_script = redeem_script;
455  next_script = std::move(redeem_script);
456 
457  // Get redeemScript type
458  script_type = Solver(next_script, solutions);
459  stack.script.pop_back();
460  }
461  if (script_type == TxoutType::WITNESS_V0_SCRIPTHASH && !stack.witness.empty() && !stack.witness.back().empty()) {
462  // Get the witnessScript
463  CScript witness_script(stack.witness.back().begin(), stack.witness.back().end());
464  data.witness_script = witness_script;
465  next_script = std::move(witness_script);
466 
467  // Get witnessScript type
468  script_type = Solver(next_script, solutions);
469  stack.witness.pop_back();
470  stack.script = std::move(stack.witness);
471  stack.witness.clear();
472  sigversion = SigVersion::WITNESS_V0;
473  }
474  if (script_type == TxoutType::MULTISIG && !stack.script.empty()) {
475  // Build a map of pubkey -> signature by matching sigs to pubkeys:
476  assert(solutions.size() > 1);
477  unsigned int num_pubkeys = solutions.size()-2;
478  unsigned int last_success_key = 0;
479  for (const valtype& sig : stack.script) {
480  for (unsigned int i = last_success_key; i < num_pubkeys; ++i) {
481  const valtype& pubkey = solutions[i+1];
482  // We either have a signature for this pubkey, or we have found a signature and it is valid
483  if (data.signatures.count(CPubKey(pubkey).GetID()) || extractor_checker.CheckECDSASignature(sig, pubkey, next_script, sigversion)) {
484  last_success_key = i + 1;
485  break;
486  }
487  }
488  }
489  }
490 
491  return data;
492 }
493 
494 void UpdateInput(CTxIn& input, const SignatureData& data)
495 {
496  input.scriptSig = data.scriptSig;
497  input.scriptWitness = data.scriptWitness;
498 }
499 
501 {
502  if (complete) return;
503  if (sigdata.complete) {
504  *this = std::move(sigdata);
505  return;
506  }
507  if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
508  redeem_script = sigdata.redeem_script;
509  }
510  if (witness_script.empty() && !sigdata.witness_script.empty()) {
511  witness_script = sigdata.witness_script;
512  }
513  signatures.insert(std::make_move_iterator(sigdata.signatures.begin()), std::make_move_iterator(sigdata.signatures.end()));
514 }
515 
516 bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType)
517 {
518  assert(nIn < txTo.vin.size());
519 
520  MutableTransactionSignatureCreator creator(&txTo, nIn, amount, nHashType);
521 
522  SignatureData sigdata;
523  bool ret = ProduceSignature(provider, creator, fromPubKey, sigdata);
524  UpdateInput(txTo.vin.at(nIn), sigdata);
525  return ret;
526 }
527 
528 bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType)
529 {
530  assert(nIn < txTo.vin.size());
531  const CTxIn& txin = txTo.vin[nIn];
532  assert(txin.prevout.n < txFrom.vout.size());
533  const CTxOut& txout = txFrom.vout[txin.prevout.n];
534 
535  return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType);
536 }
537 
538 namespace {
540 class DummySignatureChecker final : public BaseSignatureChecker
541 {
542 public:
543  DummySignatureChecker() {}
544  bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override { return true; }
545  bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const override { return true; }
546 };
547 const DummySignatureChecker DUMMY_CHECKER;
548 
549 class DummySignatureCreator final : public BaseSignatureCreator {
550 private:
551  char m_r_len = 32;
552  char m_s_len = 32;
553 public:
554  DummySignatureCreator(char r_len, char s_len) : m_r_len(r_len), m_s_len(s_len) {}
555  const BaseSignatureChecker& Checker() const override { return DUMMY_CHECKER; }
556  bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override
557  {
558  // Create a dummy signature that is a valid DER-encoding
559  vchSig.assign(m_r_len + m_s_len + 7, '\000');
560  vchSig[0] = 0x30;
561  vchSig[1] = m_r_len + m_s_len + 4;
562  vchSig[2] = 0x02;
563  vchSig[3] = m_r_len;
564  vchSig[4] = 0x01;
565  vchSig[4 + m_r_len] = 0x02;
566  vchSig[5 + m_r_len] = m_s_len;
567  vchSig[6 + m_r_len] = 0x01;
568  vchSig[6 + m_r_len + m_s_len] = SIGHASH_ALL;
569  return true;
570  }
571  bool CreateSchnorrSig(const SigningProvider& provider, std::vector<unsigned char>& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* tweak, SigVersion sigversion) const override
572  {
573  sig.assign(64, '\000');
574  return true;
575  }
576 };
577 
578 }
579 
580 const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR = DummySignatureCreator(32, 32);
581 const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR = DummySignatureCreator(33, 32);
582 
583 bool IsSolvable(const SigningProvider& provider, const CScript& script)
584 {
585  // This check is to make sure that the script we created can actually be solved for and signed by us
586  // if we were to have the private keys. This is just to make sure that the script is valid and that,
587  // if found in a transaction, we would still accept and relay that transaction. In particular,
588  // it will reject witness outputs that require signing with an uncompressed public key.
589  SignatureData sigs;
590  // Make sure that STANDARD_SCRIPT_VERIFY_FLAGS includes SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, the most
591  // important property this function is designed to test for.
592  static_assert(STANDARD_SCRIPT_VERIFY_FLAGS & SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, "IsSolvable requires standard script flags to include WITNESS_PUBKEYTYPE");
593  if (ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, script, sigs)) {
594  // VerifyScript check is just defensive, and should never fail.
595  bool verified = VerifyScript(sigs.scriptSig, script, &sigs.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, DUMMY_CHECKER);
596  assert(verified);
597  return true;
598  }
599  return false;
600 }
601 
602 bool IsSegWitOutput(const SigningProvider& provider, const CScript& script)
603 {
604  int version;
605  valtype program;
606  if (script.IsWitnessProgram(version, program)) return true;
607  if (script.IsPayToScriptHash()) {
608  std::vector<valtype> solutions;
609  auto whichtype = Solver(script, solutions);
610  if (whichtype == TxoutType::SCRIPTHASH) {
611  auto h160 = uint160(solutions[0]);
612  CScript subscript;
613  if (provider.GetCScript(CScriptID{h160}, subscript)) {
614  if (subscript.IsWitnessProgram(version, program)) return true;
615  }
616  }
617  }
618  return false;
619 }
620 
621 bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, int nHashType, std::map<int, bilingual_str>& input_errors)
622 {
623  bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
624 
625  // Use CTransaction for the constant parts of the
626  // transaction to avoid rehashing.
627  const CTransaction txConst(mtx);
628 
630  std::vector<CTxOut> spent_outputs;
631  for (unsigned int i = 0; i < mtx.vin.size(); ++i) {
632  CTxIn& txin = mtx.vin[i];
633  auto coin = coins.find(txin.prevout);
634  if (coin == coins.end() || coin->second.IsSpent()) {
635  txdata.Init(txConst, /* spent_outputs */ {}, /* force */ true);
636  break;
637  } else {
638  spent_outputs.emplace_back(coin->second.out.nValue, coin->second.out.scriptPubKey);
639  }
640  }
641  if (spent_outputs.size() == mtx.vin.size()) {
642  txdata.Init(txConst, std::move(spent_outputs), true);
643  }
644 
645  // Sign what we can:
646  for (unsigned int i = 0; i < mtx.vin.size(); ++i) {
647  CTxIn& txin = mtx.vin[i];
648  auto coin = coins.find(txin.prevout);
649  if (coin == coins.end() || coin->second.IsSpent()) {
650  input_errors[i] = _("Input not found or already spent");
651  continue;
652  }
653  const CScript& prevPubKey = coin->second.out.scriptPubKey;
654  const CAmount& amount = coin->second.out.nValue;
655 
656  SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
657  // Only sign SIGHASH_SINGLE if there's a corresponding output:
658  if (!fHashSingle || (i < mtx.vout.size())) {
659  ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, &txdata, nHashType), prevPubKey, sigdata);
660  }
661 
662  UpdateInput(txin, sigdata);
663 
664  // amount must be specified for valid segwit signature
665  if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
666  input_errors[i] = _("Missing amount");
667  continue;
668  }
669 
670  ScriptError serror = SCRIPT_ERR_OK;
671  if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) {
672  if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
673  // Unable to sign input and verification failed (possible attempt to partially sign).
674  input_errors[i] = Untranslated("Unable to sign input, invalid stack size (possibly missing key)");
675  } else if (serror == SCRIPT_ERR_SIG_NULLFAIL) {
676  // Verification failed (possibly due to insufficient signatures).
677  input_errors[i] = Untranslated("CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");
678  } else {
679  input_errors[i] = Untranslated(ScriptErrorString(serror));
680  }
681  } else {
682  // If this input succeeds, make sure there is no error set for it
683  input_errors.erase(i);
684  }
685  }
686  return input_errors.empty();
687 }
ScriptErrorString
std::string ScriptErrorString(const ScriptError serror)
Definition: script_error.cpp:10
SigVersion
SigVersion
Definition: interpreter.h:188
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1086
CTxIn
An input of a transaction.
Definition: transaction.h:65
CKey::IsCompressed
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
SigningProvider::GetKeyByXOnly
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
Definition: signingprovider.h:29
policy.h
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:366
CScriptWitness::IsNull
bool IsNull() const
Definition: script.h:566
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
assert
assert(!tx.IsCoinBase())
SignatureData::missing_redeem_script
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
Definition: sign.h:77
Solver
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:144
BaseSignatureCreator::CreateSig
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.
SignatureData::scriptWitness
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
Definition: sign.h:69
CRIPEMD160
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
SignTransaction
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, int nHashType, std::map< int, bilingual_str > &input_errors)
Sign the CMutableTransaction.
Definition: sign.cpp:621
SigVersion::BASE
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
TaprootSpendData::scripts
std::map< std::pair< CScript, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
Definition: standard.h:219
TxoutType
TxoutType
Definition: standard.h:59
SCRIPT_ERR_OK
@ SCRIPT_ERR_OK
Definition: script_error.h:13
TxoutType::NONSTANDARD
@ NONSTANDARD
TxoutType::WITNESS_UNKNOWN
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
DUMMY_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:580
SignatureData::MergeSignatureData
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:500
transaction.h
CRIPEMD160::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
SCRIPT_ERR_SIG_NULLFAIL
@ SCRIPT_ERR_SIG_NULLFAIL
Definition: script_error.h:54
BaseSignatureChecker::CheckECDSASignature
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Definition: interpreter.h:246
SignStep
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:233
uint256.h
ScriptExecutionData::m_codeseparator_pos
uint32_t m_codeseparator_pos
Opcode position of the last executed OP_CODESEPARATOR (or 0xFFFFFFFF if none executed).
Definition: interpreter.h:206
CScript::IsWitnessProgram
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:220
GenericTransactionSignatureChecker
Definition: interpreter.h:282
SigningProvider
An interface to be implemented by keystores that support signing.
Definition: signingprovider.h:17
SIGHASH_SINGLE
@ SIGHASH_SINGLE
Definition: interpreter.h:30
ScriptExecutionData
Definition: interpreter.h:196
MutableTransactionSignatureCreator
A signature creator for transactions.
Definition: sign.h:37
SignTaprootScript
static bool SignTaprootScript(const SigningProvider &provider, const BaseSignatureCreator &creator, SignatureData &sigdata, int leaf_version, const CScript &script, std::vector< valtype > &result)
Definition: sign.cpp:161
MoneyRange
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
MutableTransactionSignatureCreator::nIn
unsigned int nIn
Definition: sign.h:39
CScript::EncodeOP_N
static opcodetype EncodeOP_N(int n)
Definition: script.h:505
BaseSignatureChecker
Definition: interpreter.h:243
XOnlyPubKey
Definition: pubkey.h:224
ScriptExecutionData::m_annex_present
bool m_annex_present
Whether an annex is present.
Definition: interpreter.h:211
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
CScript::IsPayToScriptHash
bool IsPayToScriptHash() const
Definition: script.cpp:201
MissingDataBehavior::FAIL
@ FAIL
Just act as if the signature was invalid.
SigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: signingprovider.h:23
SigVersion::TAPROOT
@ TAPROOT
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, key path spending; see BIP 341.
VerifyScript
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
Definition: interpreter.cpp:1972
TxoutType::WITNESS_V1_TAPROOT
@ WITNESS_V1_TAPROOT
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:279
BaseSignatureChecker::CheckSchnorrSignature
virtual bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror=nullptr) const
Definition: interpreter.h:251
ScriptError
enum ScriptError_t ScriptError
CTxIn::scriptWitness
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:71
CTxOut::nValue
CAmount nValue
Definition: transaction.h:151
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
signingprovider.h
prevector::end
iterator end()
Definition: prevector.h:292
SigPair
std::pair< CPubKey, std::vector< unsigned char > > SigPair
Definition: sign.h:58
PushAll
static CScript PushAll(const std::vector< valtype > &values)
Definition: sign.cpp:316
OP_0
@ OP_0
Definition: script.h:69
TxoutType::WITNESS_V0_SCRIPTHASH
@ WITNESS_V0_SCRIPTHASH
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
TxoutType::PUBKEY
@ PUBKEY
DUMMY_MAXIMUM_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:581
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
MutableTransactionSignatureCreator::CreateSig
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:35
SignatureData
Definition: sign.h:63
MutableTransactionSignatureCreator::amount
CAmount amount
Definition: sign.h:41
SigningProvider::GetKeyOrigin
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
Definition: signingprovider.h:26
SIGHASH_ANYONECANPAY
@ SIGHASH_ANYONECANPAY
Definition: interpreter.h:31
CTxOut
An output of a transaction.
Definition: transaction.h:148
SignSignature
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType)
Produce a script signature for a transaction.
Definition: sign.cpp:516
values
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Definition: scriptnum_tests.cpp:17
CreateSig
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:125
TaprootSpendData
Definition: standard.h:207
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:291
ScriptExecutionData::m_tapleaf_hash
uint256 m_tapleaf_hash
The tapleaf hash.
Definition: interpreter.h:201
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:152
ScriptExecutionData::m_annex_init
bool m_annex_init
Whether m_annex_present and (when needed) m_annex_hash are initialized.
Definition: interpreter.h:209
PrecomputedTransactionData::Init
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
Definition: interpreter.cpp:1423
IsSegWitOutput
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
Definition: sign.cpp:602
ScriptExecutionData::m_codeseparator_pos_init
bool m_codeseparator_pos_init
Whether m_codeseparator_pos is initialized.
Definition: interpreter.h:204
SignatureData::redeem_script
CScript redeem_script
The redeemScript (if any) for the input.
Definition: sign.h:67
CRIPEMD160::Write
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
SignatureData::scriptSig
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format.
Definition: sign.h:66
SignatureData::witness
bool witness
Stores whether the input this SigData corresponds to is a witness input.
Definition: sign.h:65
sign.h
CKey::Sign
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:213
TaprootSpendData::merkle_root
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
Definition: standard.h:212
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
standard.h
PrecomputedTransactionData::m_spent_outputs_ready
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
Definition: interpreter.h:170
MutableTransactionSignatureCreator::m_txdata
const PrecomputedTransactionData * m_txdata
Definition: sign.h:43
SigVersion::WITNESS_V0
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
SignatureData::signatures
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
Definition: sign.h:71
SCRIPT_ERR_INVALID_STACK_OPERATION
@ SCRIPT_ERR_INVALID_STACK_OPERATION
Definition: script_error.h:36
SignatureData::missing_sigs
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Definition: sign.h:76
SignatureData::witness_script
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
Definition: sign.h:68
PrecomputedTransactionData
Definition: interpreter.h:151
SignatureData::taproot_key_path_sig
std::vector< unsigned char > taproot_key_path_sig
Definition: sign.h:73
TxoutType::SCRIPTHASH
@ SCRIPTHASH
SigningProvider::GetTaprootSpendData
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
Definition: signingprovider.h:27
SignatureData::complete
bool complete
Stores whether the scriptSig and scriptWitness are complete.
Definition: sign.h:64
uint256
256-bit opaque blob.
Definition: uint256.h:126
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator
MutableTransactionSignatureCreator(const CMutableTransaction *tx, unsigned int input_idx, const CAmount &amount, int hash_type)
Definition: sign.cpp:21
MutableTransactionSignatureCreator::nHashType
int nHashType
Definition: sign.h:40
SigningProvider::GetCScript
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Definition: signingprovider.h:21
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
@ SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
Definition: interpreter.h:124
SignTaproot
static bool SignTaproot(const SigningProvider &provider, const BaseSignatureCreator &creator, const WitnessV1Taproot &output, SignatureData &sigdata, std::vector< valtype > &result)
Definition: sign.cpp:182
OP_HASH160
@ OP_HASH160
Definition: script.h:180
TaprootSpendData::internal_key
XOnlyPubKey internal_key
The BIP341 internal key.
Definition: standard.h:210
TxoutType::NULL_DATA
@ NULL_DATA
unspendable OP_RETURN script that carries data
OP_1NEGATE
@ OP_1NEGATE
Definition: script.h:74
ScriptExecutionData::m_tapleaf_hash_init
bool m_tapleaf_hash_init
Whether m_tapleaf_hash is initialized.
Definition: interpreter.h:199
SignatureData::tr_spenddata
TaprootSpendData tr_spenddata
Taproot spending data.
Definition: sign.h:70
TxoutType::PUBKEYHASH
@ PUBKEYHASH
BaseSignatureCreator::CreateSchnorrSig
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
ProduceSignature
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:333
MutableTransactionSignatureCreator::txTo
const CMutableTransaction * txTo
Definition: sign.h:38
SignatureData::missing_pubkeys
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
Definition: sign.h:75
MutableTransactionSignatureCreator::CreateSchnorrSig
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:60
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:367
key.h
SIGHASH_ALL
@ SIGHASH_ALL
Definition: interpreter.h:28
uint160
160-bit opaque blob.
Definition: uint256.h:115
CPubKey
An encapsulated public key.
Definition: pubkey.h:33
HASHER_TAPLEAF
const CHashWriter HASHER_TAPLEAF
Hasher with tag "TapLeaf" pre-fed to it.
Definition: interpreter.cpp:1487
CKey
An encapsulated private key.
Definition: key.h:26
SignatureHash
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
Definition: interpreter.cpp:1593
translation.h
IsSolvable
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:583
CKey::SignSchnorr
bool SignSchnorr(const uint256 &hash, 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:278
GetCScript
static bool GetCScript(const SigningProvider &provider, const SignatureData &sigdata, const CScriptID &scriptid, CScript &script)
Definition: sign.cpp:91
COutPoint::n
uint32_t n
Definition: transaction.h:30
SignatureData::misc_pubkeys
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
Definition: sign.h:72
Vector
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:20
vector.h
SigVersion::TAPSCRIPT
@ TAPSCRIPT
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, script path spending, leaf version 0xc0; see...
EvalScript
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
Definition: interpreter.cpp:431
DeferringSignatureChecker::CheckECDSASignature
bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
Definition: interpreter.h:315
DeferringSignatureChecker
Definition: interpreter.h:307
CTxIn::prevout
COutPoint prevout
Definition: transaction.h:68
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
prevector::size
size_type size() const
Definition: prevector.h:282
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:69
prevector::begin
iterator begin()
Definition: prevector.h:290
TxoutType::MULTISIG
@ MULTISIG
TAPROOT_LEAF_TAPSCRIPT
static constexpr uint8_t TAPROOT_LEAF_TAPSCRIPT
Definition: interpreter.h:230
SigningProvider::GetKey
virtual bool GetKey(const CKeyID &address, CKey &key) const
Definition: signingprovider.h:24
OP_EQUALVERIFY
@ OP_EQUALVERIFY
Definition: script.h:140
prevector::empty
bool empty() const
Definition: prevector.h:286
BaseSignatureCreator::Checker
virtual const BaseSignatureChecker & Checker() const =0
valtype
std::vector< unsigned char > valtype
Definition: interpreter.cpp:15
MAX_MONEY
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
UpdateInput
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:494
PrecomputedTransactionData::m_bip341_taproot_ready
bool m_bip341_taproot_ready
Whether the 5 fields above are initialized.
Definition: interpreter.h:161
SignatureData::missing_witness_script
uint256 missing_witness_script
SHA256 of the missing witnessScript (if any)
Definition: sign.h:78
STANDARD_SCRIPT_VERIFY_FLAGS
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
Definition: policy.h:60
TxoutType::WITNESS_V0_KEYHASH
@ WITNESS_V0_KEYHASH
keyorigin.h
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:364
SignatureHashSchnorr
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)
Definition: interpreter.cpp:1503
BaseSignatureCreator
Interface for signature creators.
Definition: sign.h:26
SignatureData::taproot_script_sigs
std::map< std::pair< XOnlyPubKey, uint256 >, std::vector< unsigned char > > taproot_script_sigs
Schnorr signature for key path spending.
Definition: sign.h:74
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:25
DataFromTransaction
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:429
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:183
amount.h
GetPubKey
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:107
base_blob::begin
unsigned char * begin()
Definition: uint256.h:60
OP_DUP
@ OP_DUP
Definition: script.h:118
KeyOriginInfo
Definition: keyorigin.h:11
CScriptWitness::stack
std::vector< std::vector< unsigned char > > stack
Definition: script.h:561
valtype
std::vector< unsigned char > valtype
Definition: sign.cpp:19
TaprootSpendData::Merge
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
Definition: standard.cpp:358
CreateTaprootScriptSig
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:147
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
SCRIPT_VERIFY_STRICTENC
@ SCRIPT_VERIFY_STRICTENC
Definition: interpreter.h:52
Span::subspan
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:194
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
WitnessV1Taproot
Definition: standard.h:118
SIGHASH_DEFAULT
@ SIGHASH_DEFAULT
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.
Definition: interpreter.h:33