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