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