Bitcoin Core  0.20.99
P2P Digital Currency
sign.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <key.h>
9 #include <policy/policy.h>
10 #include <primitives/transaction.h>
11 #include <script/signingprovider.h>
12 #include <script/standard.h>
13 #include <uint256.h>
14 
15 typedef std::vector<unsigned char> valtype;
16 
17 MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn) : txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn) {}
18 
19 bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& address, const CScript& scriptCode, SigVersion sigversion) const
20 {
21  CKey key;
22  if (!provider.GetKey(address, key))
23  return false;
24 
25  // Signing with uncompressed keys is disabled in witness scripts
26  if (sigversion == SigVersion::WITNESS_V0 && !key.IsCompressed())
27  return false;
28 
29  uint256 hash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion);
30  if (!key.Sign(hash, vchSig))
31  return false;
32  vchSig.push_back((unsigned char)nHashType);
33  return true;
34 }
35 
36 static bool GetCScript(const SigningProvider& provider, const SignatureData& sigdata, const CScriptID& scriptid, CScript& script)
37 {
38  if (provider.GetCScript(scriptid, script)) {
39  return true;
40  }
41  // Look for scripts in SignatureData
42  if (CScriptID(sigdata.redeem_script) == scriptid) {
43  script = sigdata.redeem_script;
44  return true;
45  } else if (CScriptID(sigdata.witness_script) == scriptid) {
46  script = sigdata.witness_script;
47  return true;
48  }
49  return false;
50 }
51 
52 static bool GetPubKey(const SigningProvider& provider, const SignatureData& sigdata, const CKeyID& address, CPubKey& pubkey)
53 {
54  // Look for pubkey in all partial sigs
55  const auto it = sigdata.signatures.find(address);
56  if (it != sigdata.signatures.end()) {
57  pubkey = it->second.first;
58  return true;
59  }
60  // Look for pubkey in pubkey list
61  const auto& pk_it = sigdata.misc_pubkeys.find(address);
62  if (pk_it != sigdata.misc_pubkeys.end()) {
63  pubkey = pk_it->second.first;
64  return true;
65  }
66  // Query the underlying provider
67  return provider.GetPubKey(address, pubkey);
68 }
69 
70 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)
71 {
72  CKeyID keyid = pubkey.GetID();
73  const auto it = sigdata.signatures.find(keyid);
74  if (it != sigdata.signatures.end()) {
75  sig_out = it->second.second;
76  return true;
77  }
78  KeyOriginInfo info;
79  if (provider.GetKeyOrigin(keyid, info)) {
80  sigdata.misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info)));
81  }
82  if (creator.CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) {
83  auto i = sigdata.signatures.emplace(keyid, SigPair(pubkey, sig_out));
84  assert(i.second);
85  return true;
86  }
87  // Could not make signature or signature not found, add keyid to missing
88  sigdata.missing_sigs.push_back(keyid);
89  return false;
90 }
91 
98 static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey,
99  std::vector<valtype>& ret, TxoutType& whichTypeRet, SigVersion sigversion, SignatureData& sigdata)
100 {
101  CScript scriptRet;
102  uint160 h160;
103  ret.clear();
104  std::vector<unsigned char> sig;
105 
106  std::vector<valtype> vSolutions;
107  whichTypeRet = Solver(scriptPubKey, vSolutions);
108 
109  switch (whichTypeRet)
110  {
115  return false;
116  case TxoutType::PUBKEY:
117  if (!CreateSig(creator, sigdata, provider, sig, CPubKey(vSolutions[0]), scriptPubKey, sigversion)) return false;
118  ret.push_back(std::move(sig));
119  return true;
120  case TxoutType::PUBKEYHASH: {
121  CKeyID keyID = CKeyID(uint160(vSolutions[0]));
122  CPubKey pubkey;
123  if (!GetPubKey(provider, sigdata, keyID, pubkey)) {
124  // Pubkey could not be found, add to missing
125  sigdata.missing_pubkeys.push_back(keyID);
126  return false;
127  }
128  if (!CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) return false;
129  ret.push_back(std::move(sig));
130  ret.push_back(ToByteVector(pubkey));
131  return true;
132  }
134  h160 = uint160(vSolutions[0]);
135  if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
136  ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
137  return true;
138  }
139  // Could not find redeemScript, add to missing
140  sigdata.missing_redeem_script = h160;
141  return false;
142 
143  case TxoutType::MULTISIG: {
144  size_t required = vSolutions.front()[0];
145  ret.push_back(valtype()); // workaround CHECKMULTISIG bug
146  for (size_t i = 1; i < vSolutions.size() - 1; ++i) {
147  CPubKey pubkey = CPubKey(vSolutions[i]);
148  // We need to always call CreateSig in order to fill sigdata with all
149  // possible signatures that we can create. This will allow further PSBT
150  // processing to work as it needs all possible signature and pubkey pairs
151  if (CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) {
152  if (ret.size() < required + 1) {
153  ret.push_back(std::move(sig));
154  }
155  }
156  }
157  bool ok = ret.size() == required + 1;
158  for (size_t i = 0; i + ret.size() < required + 1; ++i) {
159  ret.push_back(valtype());
160  }
161  return ok;
162  }
164  ret.push_back(vSolutions[0]);
165  return true;
166 
168  CRIPEMD160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160.begin());
169  if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
170  ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
171  return true;
172  }
173  // Could not find witnessScript, add to missing
174  sigdata.missing_witness_script = uint256(vSolutions[0]);
175  return false;
176 
177  default:
178  return false;
179  }
180 }
181 
182 static CScript PushAll(const std::vector<valtype>& values)
183 {
184  CScript result;
185  for (const valtype& v : values) {
186  if (v.size() == 0) {
187  result << OP_0;
188  } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
189  result << CScript::EncodeOP_N(v[0]);
190  } else if (v.size() == 1 && v[0] == 0x81) {
191  result << OP_1NEGATE;
192  } else {
193  result << v;
194  }
195  }
196  return result;
197 }
198 
199 bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& fromPubKey, SignatureData& sigdata)
200 {
201  if (sigdata.complete) return true;
202 
203  std::vector<valtype> result;
204  TxoutType whichType;
205  bool solved = SignStep(provider, creator, fromPubKey, result, whichType, SigVersion::BASE, sigdata);
206  bool P2SH = false;
207  CScript subscript;
208  sigdata.scriptWitness.stack.clear();
209 
210  if (solved && whichType == TxoutType::SCRIPTHASH)
211  {
212  // Solver returns the subscript that needs to be evaluated;
213  // the final scriptSig is the signatures from that
214  // and then the serialized subscript:
215  subscript = CScript(result[0].begin(), result[0].end());
216  sigdata.redeem_script = subscript;
217  solved = solved && SignStep(provider, creator, subscript, result, whichType, SigVersion::BASE, sigdata) && whichType != TxoutType::SCRIPTHASH;
218  P2SH = true;
219  }
220 
221  if (solved && whichType == TxoutType::WITNESS_V0_KEYHASH)
222  {
223  CScript witnessscript;
224  witnessscript << OP_DUP << OP_HASH160 << ToByteVector(result[0]) << OP_EQUALVERIFY << OP_CHECKSIG;
225  TxoutType subType;
226  solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata);
227  sigdata.scriptWitness.stack = result;
228  sigdata.witness = true;
229  result.clear();
230  }
231  else if (solved && whichType == TxoutType::WITNESS_V0_SCRIPTHASH)
232  {
233  CScript witnessscript(result[0].begin(), result[0].end());
234  sigdata.witness_script = witnessscript;
235  TxoutType subType;
236  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;
237  result.push_back(std::vector<unsigned char>(witnessscript.begin(), witnessscript.end()));
238  sigdata.scriptWitness.stack = result;
239  sigdata.witness = true;
240  result.clear();
241  } else if (solved && whichType == TxoutType::WITNESS_UNKNOWN) {
242  sigdata.witness = true;
243  }
244 
245  if (P2SH) {
246  result.push_back(std::vector<unsigned char>(subscript.begin(), subscript.end()));
247  }
248  sigdata.scriptSig = PushAll(result);
249 
250  // Test solution
251  sigdata.complete = solved && VerifyScript(sigdata.scriptSig, fromPubKey, &sigdata.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, creator.Checker());
252  return sigdata.complete;
253 }
254 
255 namespace {
256 class SignatureExtractorChecker final : public BaseSignatureChecker
257 {
258 private:
259  SignatureData& sigdata;
261 
262 public:
263  SignatureExtractorChecker(SignatureData& sigdata, BaseSignatureChecker& checker) : sigdata(sigdata), checker(checker) {}
264  bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override
265  {
266  if (checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion)) {
267  CPubKey pubkey(vchPubKey);
268  sigdata.signatures.emplace(pubkey.GetID(), SigPair(pubkey, scriptSig));
269  return true;
270  }
271  return false;
272  }
273 };
274 
275 struct Stacks
276 {
277  std::vector<valtype> script;
278  std::vector<valtype> witness;
279 
280  Stacks() = delete;
281  Stacks(const Stacks&) = delete;
282  explicit Stacks(const SignatureData& data) : witness(data.scriptWitness.stack) {
284  }
285 };
286 }
287 
288 // Extracts signatures and scripts from incomplete scriptSigs. Please do not extend this, use PSBT instead
289 SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout)
290 {
291  SignatureData data;
292  assert(tx.vin.size() > nIn);
293  data.scriptSig = tx.vin[nIn].scriptSig;
294  data.scriptWitness = tx.vin[nIn].scriptWitness;
295  Stacks stack(data);
296 
297  // Get signatures
298  MutableTransactionSignatureChecker tx_checker(&tx, nIn, txout.nValue);
299  SignatureExtractorChecker extractor_checker(data, tx_checker);
300  if (VerifyScript(data.scriptSig, txout.scriptPubKey, &data.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, extractor_checker)) {
301  data.complete = true;
302  return data;
303  }
304 
305  // Get scripts
306  std::vector<std::vector<unsigned char>> solutions;
307  TxoutType script_type = Solver(txout.scriptPubKey, solutions);
308  SigVersion sigversion = SigVersion::BASE;
309  CScript next_script = txout.scriptPubKey;
310 
311  if (script_type == TxoutType::SCRIPTHASH && !stack.script.empty() && !stack.script.back().empty()) {
312  // Get the redeemScript
313  CScript redeem_script(stack.script.back().begin(), stack.script.back().end());
314  data.redeem_script = redeem_script;
315  next_script = std::move(redeem_script);
316 
317  // Get redeemScript type
318  script_type = Solver(next_script, solutions);
319  stack.script.pop_back();
320  }
321  if (script_type == TxoutType::WITNESS_V0_SCRIPTHASH && !stack.witness.empty() && !stack.witness.back().empty()) {
322  // Get the witnessScript
323  CScript witness_script(stack.witness.back().begin(), stack.witness.back().end());
324  data.witness_script = witness_script;
325  next_script = std::move(witness_script);
326 
327  // Get witnessScript type
328  script_type = Solver(next_script, solutions);
329  stack.witness.pop_back();
330  stack.script = std::move(stack.witness);
331  stack.witness.clear();
332  sigversion = SigVersion::WITNESS_V0;
333  }
334  if (script_type == TxoutType::MULTISIG && !stack.script.empty()) {
335  // Build a map of pubkey -> signature by matching sigs to pubkeys:
336  assert(solutions.size() > 1);
337  unsigned int num_pubkeys = solutions.size()-2;
338  unsigned int last_success_key = 0;
339  for (const valtype& sig : stack.script) {
340  for (unsigned int i = last_success_key; i < num_pubkeys; ++i) {
341  const valtype& pubkey = solutions[i+1];
342  // We either have a signature for this pubkey, or we have found a signature and it is valid
343  if (data.signatures.count(CPubKey(pubkey).GetID()) || extractor_checker.CheckECDSASignature(sig, pubkey, next_script, sigversion)) {
344  last_success_key = i + 1;
345  break;
346  }
347  }
348  }
349  }
350 
351  return data;
352 }
353 
354 void UpdateInput(CTxIn& input, const SignatureData& data)
355 {
356  input.scriptSig = data.scriptSig;
357  input.scriptWitness = data.scriptWitness;
358 }
359 
361 {
362  if (complete) return;
363  if (sigdata.complete) {
364  *this = std::move(sigdata);
365  return;
366  }
367  if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
368  redeem_script = sigdata.redeem_script;
369  }
370  if (witness_script.empty() && !sigdata.witness_script.empty()) {
371  witness_script = sigdata.witness_script;
372  }
373  signatures.insert(std::make_move_iterator(sigdata.signatures.begin()), std::make_move_iterator(sigdata.signatures.end()));
374 }
375 
376 bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType)
377 {
378  assert(nIn < txTo.vin.size());
379 
380  MutableTransactionSignatureCreator creator(&txTo, nIn, amount, nHashType);
381 
382  SignatureData sigdata;
383  bool ret = ProduceSignature(provider, creator, fromPubKey, sigdata);
384  UpdateInput(txTo.vin.at(nIn), sigdata);
385  return ret;
386 }
387 
388 bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType)
389 {
390  assert(nIn < txTo.vin.size());
391  CTxIn& txin = txTo.vin[nIn];
392  assert(txin.prevout.n < txFrom.vout.size());
393  const CTxOut& txout = txFrom.vout[txin.prevout.n];
394 
395  return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType);
396 }
397 
398 namespace {
400 class DummySignatureChecker final : public BaseSignatureChecker
401 {
402 public:
403  DummySignatureChecker() {}
404  bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override { return true; }
405 };
406 const DummySignatureChecker DUMMY_CHECKER;
407 
408 class DummySignatureCreator final : public BaseSignatureCreator {
409 private:
410  char m_r_len = 32;
411  char m_s_len = 32;
412 public:
413  DummySignatureCreator(char r_len, char s_len) : m_r_len(r_len), m_s_len(s_len) {}
414  const BaseSignatureChecker& Checker() const override { return DUMMY_CHECKER; }
415  bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override
416  {
417  // Create a dummy signature that is a valid DER-encoding
418  vchSig.assign(m_r_len + m_s_len + 7, '\000');
419  vchSig[0] = 0x30;
420  vchSig[1] = m_r_len + m_s_len + 4;
421  vchSig[2] = 0x02;
422  vchSig[3] = m_r_len;
423  vchSig[4] = 0x01;
424  vchSig[4 + m_r_len] = 0x02;
425  vchSig[5 + m_r_len] = m_s_len;
426  vchSig[6 + m_r_len] = 0x01;
427  vchSig[6 + m_r_len + m_s_len] = SIGHASH_ALL;
428  return true;
429  }
430 };
431 
432 }
433 
434 const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR = DummySignatureCreator(32, 32);
435 const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR = DummySignatureCreator(33, 32);
436 
437 bool IsSolvable(const SigningProvider& provider, const CScript& script)
438 {
439  // This check is to make sure that the script we created can actually be solved for and signed by us
440  // if we were to have the private keys. This is just to make sure that the script is valid and that,
441  // if found in a transaction, we would still accept and relay that transaction. In particular,
442  // it will reject witness outputs that require signing with an uncompressed public key.
443  SignatureData sigs;
444  // Make sure that STANDARD_SCRIPT_VERIFY_FLAGS includes SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, the most
445  // important property this function is designed to test for.
446  static_assert(STANDARD_SCRIPT_VERIFY_FLAGS & SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, "IsSolvable requires standard script flags to include WITNESS_PUBKEYTYPE");
447  if (ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, script, sigs)) {
448  // VerifyScript check is just defensive, and should never fail.
449  bool verified = VerifyScript(sigs.scriptSig, script, &sigs.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, DUMMY_CHECKER);
450  assert(verified);
451  return true;
452  }
453  return false;
454 }
455 
456 bool IsSegWitOutput(const SigningProvider& provider, const CScript& script)
457 {
458  std::vector<valtype> solutions;
459  auto whichtype = Solver(script, solutions);
460  if (whichtype == TxoutType::WITNESS_V0_SCRIPTHASH || whichtype == TxoutType::WITNESS_V0_KEYHASH || whichtype == TxoutType::WITNESS_UNKNOWN) return true;
461  if (whichtype == TxoutType::SCRIPTHASH) {
462  auto h160 = uint160(solutions[0]);
463  CScript subscript;
464  if (provider.GetCScript(CScriptID{h160}, subscript)) {
465  whichtype = Solver(subscript, solutions);
466  if (whichtype == TxoutType::WITNESS_V0_SCRIPTHASH || whichtype == TxoutType::WITNESS_V0_KEYHASH || whichtype == TxoutType::WITNESS_UNKNOWN) return true;
467  }
468  }
469  return false;
470 }
471 
472 bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, int nHashType, std::map<int, std::string>& input_errors)
473 {
474  bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
475 
476  // Use CTransaction for the constant parts of the
477  // transaction to avoid rehashing.
478  const CTransaction txConst(mtx);
479  // Sign what we can:
480  for (unsigned int i = 0; i < mtx.vin.size(); i++) {
481  CTxIn& txin = mtx.vin[i];
482  auto coin = coins.find(txin.prevout);
483  if (coin == coins.end() || coin->second.IsSpent()) {
484  input_errors[i] = "Input not found or already spent";
485  continue;
486  }
487  const CScript& prevPubKey = coin->second.out.scriptPubKey;
488  const CAmount& amount = coin->second.out.nValue;
489 
490  SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
491  // Only sign SIGHASH_SINGLE if there's a corresponding output:
492  if (!fHashSingle || (i < mtx.vout.size())) {
493  ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, nHashType), prevPubKey, sigdata);
494  }
495 
496  UpdateInput(txin, sigdata);
497 
498  // amount must be specified for valid segwit signature
499  if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
500  input_errors[i] = "Missing amount";
501  continue;
502  }
503 
504  ScriptError serror = SCRIPT_ERR_OK;
505  if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount), &serror)) {
506  if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
507  // Unable to sign input and verification failed (possible attempt to partially sign).
508  input_errors[i] = "Unable to sign input, invalid stack size (possibly missing key)";
509  } else if (serror == SCRIPT_ERR_SIG_NULLFAIL) {
510  // Verification failed (possibly due to insufficient signatures).
511  input_errors[i] = "CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)";
512  } else {
513  input_errors[i] = ScriptErrorString(serror);
514  }
515  } else {
516  // If this input succeeds, make sure there is no error set for it
517  input_errors.erase(i);
518  }
519  }
520  return input_errors.empty();
521 }
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Definition: interpreter.h:227
CAmount nValue
Definition: transaction.h:131
Witness v0 (P2WPKH and P2WSH); see BIP 141.
std::deque< CInv >::iterator it
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, int nHashType, std::map< int, std::string > &input_errors)
Sign the CMutableTransaction.
Definition: sign.cpp:472
enum ScriptError_t ScriptError
CScript scriptPubKey
Definition: transaction.h:132
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
Definition: sign.h:65
iterator insert(iterator pos, const T &value)
Definition: prevector.h:347
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.
const MutableTransactionSignatureChecker checker
Definition: sign.h:42
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:25
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format...
Definition: sign.h:63
std::vector< CTxIn > vin
Definition: transaction.h:355
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:71
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Definition: sign.h:70
Interface for signature creators.
Definition: sign.h:27
static CScript PushAll(const std::vector< valtype > &values)
Definition: sign.cpp:182
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
Definition: sign.h:69
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:434
const BaseSignatureChecker & Checker() const override
Definition: sign.h:46
std::vector< std::vector< unsigned char > > stack
Definition: script.h:560
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:435
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
Definition: sign.h:68
A signature creator for transactions.
Definition: sign.h:37
bool IsNull() const
Definition: script.h:565
uint256 missing_witness_script
SHA256 of the missing witnessScript (if any)
Definition: sign.h:72
unsigned char * begin()
Definition: uint256.h:58
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
Definition: policy.h:58
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
Definition: sign.cpp:456
unspendable OP_RETURN script that carries data
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:159
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:98
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:376
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:19
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
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:210
iterator end()
Definition: prevector.h:292
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
Definition: sign.h:71
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
An input of a transaction.
Definition: transaction.h:65
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
TxoutType
Definition: standard.h:122
An encapsulated public key.
Definition: pubkey.h:31
std::string ScriptErrorString(const ScriptError serror)
std::pair< CPubKey, std::vector< unsigned char > > SigPair
Definition: sign.h:55
const std::vector< CTxOut > vout
Definition: transaction.h:277
static bool GetCScript(const SigningProvider &provider, const SignatureData &sigdata, const CScriptID &scriptid, CScript &script)
Definition: sign.cpp:36
An output of a transaction.
Definition: transaction.h:128
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:360
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
std::vector< CTxOut > vout
Definition: transaction.h:356
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
Definition: sign.h:66
virtual bool GetKey(const CKeyID &address, CKey &key) const
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:437
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
P2SH redeemScript.
CScript scriptSig
Definition: transaction.h:69
256-bit opaque blob.
Definition: uint256.h:124
An interface to be implemented by keystores that support signing.
static opcodetype EncodeOP_N(int n)
Definition: script.h:504
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:289
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:52
bool empty() const
Definition: prevector.h:286
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:354
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:110
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
Only for Witness versions not already defined above.
160-bit opaque blob.
Definition: uint256.h:113
std::vector< unsigned char > valtype
Definition: interpreter.cpp:15
std::vector< unsigned char > valtype
Definition: sign.cpp:15
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:199
iterator begin()
Definition: prevector.h:290
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:88
A mutable version of CTransaction.
Definition: transaction.h:353
An encapsulated private key.
Definition: key.h:27
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
MutableTransactionSignatureCreator(const CMutableTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, int nHashTypeIn=SIGHASH_ALL)
Definition: sign.cpp:17
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
bool complete
Stores whether the scriptSig and scriptWitness are complete.
Definition: sign.h:61
COutPoint prevout
Definition: transaction.h:68
Definition: script.h:68
CScript redeem_script
The redeemScript (if any) for the input.
Definition: sign.h:64
bool witness
Stores whether the input this SigData corresponds to is a witness input.
Definition: sign.h:62
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
Definition: sign.h:67
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
Definition: script.h:117
virtual const BaseSignatureChecker & Checker() const =0
const CMutableTransaction * txTo
Definition: sign.h:38
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:59
SigVersion
Definition: interpreter.h:176