BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
sign.h
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 #ifndef BITCOIN_SCRIPT_SIGN_H
7 #define BITCOIN_SCRIPT_SIGN_H
8 
9 #include <boost/optional.hpp>
10 #include <hash.h>
11 #include <pubkey.h>
12 #include <script/interpreter.h>
13 #include <streams.h>
14 
15 class CKey;
16 class CKeyID;
17 class CScript;
18 class CScriptID;
19 class CTransaction;
20 
21 struct CMutableTransaction;
22 
24 {
25  unsigned char fingerprint[4];
26  std::vector<uint32_t> path;
27 };
28 
31 {
32 public:
33  virtual ~SigningProvider() {}
34  virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; }
35  virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; }
36  virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; }
37  virtual bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return false; }
38 };
39 
41 
43 {
44 private:
45  const bool m_hide_secret;
46  const bool m_hide_origin;
48 
49 public:
50  HidingSigningProvider(const SigningProvider* provider, bool hide_secret, bool hide_origin) : m_hide_secret(hide_secret), m_hide_origin(hide_origin), m_provider(provider) {}
51  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
52  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
53  bool GetKey(const CKeyID& keyid, CKey& key) const override;
54  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
55 };
56 
57 struct FlatSigningProvider final : public SigningProvider
58 {
59  std::map<CScriptID, CScript> scripts;
60  std::map<CKeyID, CPubKey> pubkeys;
61  std::map<CKeyID, KeyOriginInfo> origins;
62  std::map<CKeyID, CKey> keys;
63 
64  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
65  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
66  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
67  bool GetKey(const CKeyID& keyid, CKey& key) const override;
68 };
69 
71 
74 public:
75  virtual ~BaseSignatureCreator() {}
76  virtual const BaseSignatureChecker& Checker() const =0;
77 
79  virtual bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0;
80 };
81 
85  unsigned int nIn;
86  int nHashType;
89 
90 public:
91  MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn = SIGHASH_ALL);
92  const BaseSignatureChecker& Checker() const override { return checker; }
93  bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
94 };
95 
100 
101 typedef std::pair<CPubKey, std::vector<unsigned char>> SigPair;
102 
103 // This struct contains information from a transaction input and also contains signatures for that input.
104 // The information contained here can be used to create a signature and is also filled by ProduceSignature
105 // in order to construct final scriptSigs and scriptWitnesses.
107  bool complete = false;
108  bool witness = false;
113  std::map<CKeyID, SigPair> signatures;
114  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
115 
117  explicit SignatureData(const CScript& script) : scriptSig(script) {}
118  void MergeSignatureData(SignatureData sigdata);
119 };
120 
121 // Magic bytes
122 static constexpr uint8_t PSBT_MAGIC_BYTES[5] = {'p', 's', 'b', 't', 0xff};
123 
124 // Global types
125 static constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX = 0x00;
126 
127 // Input types
128 static constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO = 0x00;
129 static constexpr uint8_t PSBT_IN_WITNESS_UTXO = 0x01;
130 static constexpr uint8_t PSBT_IN_PARTIAL_SIG = 0x02;
131 static constexpr uint8_t PSBT_IN_SIGHASH = 0x03;
132 static constexpr uint8_t PSBT_IN_REDEEMSCRIPT = 0x04;
133 static constexpr uint8_t PSBT_IN_WITNESSSCRIPT = 0x05;
134 static constexpr uint8_t PSBT_IN_BIP32_DERIVATION = 0x06;
135 static constexpr uint8_t PSBT_IN_SCRIPTSIG = 0x07;
136 static constexpr uint8_t PSBT_IN_SCRIPTWITNESS = 0x08;
137 
138 // Output types
139 static constexpr uint8_t PSBT_OUT_REDEEMSCRIPT = 0x00;
140 static constexpr uint8_t PSBT_OUT_WITNESSSCRIPT = 0x01;
141 static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02;
142 
143 // The separator is 0x00. Reading this in means that the unserializer can interpret it
144 // as a 0 length key which indicates that this is the separator. The separator has no value.
145 static constexpr uint8_t PSBT_SEPARATOR = 0x00;
146 
147 // Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream
148 // The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other.
149 template<typename Stream, typename... X>
150 void SerializeToVector(Stream& s, const X&... args)
151 {
152  WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
153  SerializeMany(s, args...);
154 }
155 
156 // Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
157 template<typename Stream, typename... X>
158 void UnserializeFromVector(Stream& s, X&... args)
159 {
160  size_t expected_size = ReadCompactSize(s);
161  size_t remaining_before = s.size();
162  UnserializeMany(s, args...);
163  size_t remaining_after = s.size();
164  if (remaining_after + expected_size != remaining_before) {
165  throw std::ios_base::failure("Size of value was not the stated size");
166  }
167 }
168 
169 // Deserialize HD keypaths into a map
170 template<typename Stream>
171 void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
172 {
173  // Make sure that the key is the size of pubkey + 1
174  if (key.size() != CPubKey::PUBLIC_KEY_SIZE + 1 && key.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1) {
175  throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
176  }
177  // Read in the pubkey from key
178  CPubKey pubkey(key.begin() + 1, key.end());
179  if (!pubkey.IsFullyValid()) {
180  throw std::ios_base::failure("Invalid pubkey");
181  }
182  if (hd_keypaths.count(pubkey) > 0) {
183  throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
184  }
185 
186  // Read in key path
187  uint64_t value_len = ReadCompactSize(s);
188  if (value_len % 4 || value_len == 0) {
189  throw std::ios_base::failure("Invalid length for HD key path");
190  }
191 
192  KeyOriginInfo keypath;
193  s >> keypath.fingerprint;
194  for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
195  uint32_t index;
196  s >> index;
197  keypath.path.push_back(index);
198  }
199 
200  // Add to map
201  hd_keypaths.emplace(pubkey, std::move(keypath));
202 }
203 
204 // Serialize HD keypaths to a stream from a map
205 template<typename Stream>
206 void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, uint8_t type)
207 {
208  for (auto keypath_pair : hd_keypaths) {
209  SerializeToVector(s, type, MakeSpan(keypath_pair.first));
210  WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t));
211  s << keypath_pair.second.fingerprint;
212  for (const auto& path : keypath_pair.second.path) {
213  s << path;
214  }
215  }
216 }
217 
219 struct PSBTInput
220 {
227  std::map<CPubKey, KeyOriginInfo> hd_keypaths;
228  std::map<CKeyID, SigPair> partial_sigs;
229  std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
230  int sighash_type = 0;
231 
232  bool IsNull() const;
233  void FillSignatureData(SignatureData& sigdata) const;
234  void FromSignatureData(const SignatureData& sigdata);
235  void Merge(const PSBTInput& input);
236  bool IsSane() const;
238 
239  template <typename Stream>
240  inline void Serialize(Stream& s) const {
241  // Write the utxo
242  // If there is a non-witness utxo, then don't add the witness one.
243  if (non_witness_utxo) {
244  SerializeToVector(s, PSBT_IN_NON_WITNESS_UTXO);
245  OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
247  } else if (!witness_utxo.IsNull()) {
248  SerializeToVector(s, PSBT_IN_WITNESS_UTXO);
250  }
251 
253  // Write any partial signatures
254  for (auto sig_pair : partial_sigs) {
255  SerializeToVector(s, PSBT_IN_PARTIAL_SIG, MakeSpan(sig_pair.second.first));
256  s << sig_pair.second.second;
257  }
258 
259  // Write the sighash type
260  if (sighash_type > 0) {
261  SerializeToVector(s, PSBT_IN_SIGHASH);
263  }
264 
265  // Write the redeem script
266  if (!redeem_script.empty()) {
267  SerializeToVector(s, PSBT_IN_REDEEMSCRIPT);
268  s << redeem_script;
269  }
270 
271  // Write the witness script
272  if (!witness_script.empty()) {
273  SerializeToVector(s, PSBT_IN_WITNESSSCRIPT);
274  s << witness_script;
275  }
276 
277  // Write any hd keypaths
278  SerializeHDKeypaths(s, hd_keypaths, PSBT_IN_BIP32_DERIVATION);
279  }
280 
281  // Write script sig
282  if (!final_script_sig.empty()) {
283  SerializeToVector(s, PSBT_IN_SCRIPTSIG);
284  s << final_script_sig;
285  }
286  // write script witness
287  if (!final_script_witness.IsNull()) {
288  SerializeToVector(s, PSBT_IN_SCRIPTWITNESS);
290  }
291 
292  // Write unknown things
293  for (auto& entry : unknown) {
294  s << entry.first;
295  s << entry.second;
296  }
297 
298  s << PSBT_SEPARATOR;
299  }
300 
301 
302  template <typename Stream>
303  inline void Unserialize(Stream& s) {
304  // Read loop
305  bool found_sep = false;
306  while(!s.empty()) {
307  // Read
308  std::vector<unsigned char> key;
309  s >> key;
310 
311  // the key is empty if that was actually a separator byte
312  // This is a special case for key lengths 0 as those are not allowed (except for separator)
313  if (key.empty()) {
314  found_sep = true;
315  break;
316  }
317 
318  // First byte of key is the type
319  unsigned char type = key[0];
320 
321  // Do stuff based on type
322  switch(type) {
323  case PSBT_IN_NON_WITNESS_UTXO:
324  {
325  if (non_witness_utxo) {
326  throw std::ios_base::failure("Duplicate Key, input non-witness utxo already provided");
327  } else if (key.size() != 1) {
328  throw std::ios_base::failure("Non-witness utxo key is more than one byte type");
329  }
330  // Set the stream to unserialize with witness since this is always a valid network transaction
331  OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() & ~SERIALIZE_TRANSACTION_NO_WITNESS);
333  break;
334  }
335  case PSBT_IN_WITNESS_UTXO:
336  if (!witness_utxo.IsNull()) {
337  throw std::ios_base::failure("Duplicate Key, input witness utxo already provided");
338  } else if (key.size() != 1) {
339  throw std::ios_base::failure("Witness utxo key is more than one byte type");
340  }
342  break;
343  case PSBT_IN_PARTIAL_SIG:
344  {
345  // Make sure that the key is the size of pubkey + 1
346  if (key.size() != CPubKey::PUBLIC_KEY_SIZE + 1 && key.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1) {
347  throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey");
348  }
349  // Read in the pubkey from key
350  CPubKey pubkey(key.begin() + 1, key.end());
351  if (!pubkey.IsFullyValid()) {
352  throw std::ios_base::failure("Invalid pubkey");
353  }
354  if (partial_sigs.count(pubkey.GetID()) > 0) {
355  throw std::ios_base::failure("Duplicate Key, input partial signature for pubkey already provided");
356  }
357 
358  // Read in the signature from value
359  std::vector<unsigned char> sig;
360  s >> sig;
361 
362  // Add to list
363  partial_sigs.emplace(pubkey.GetID(), SigPair(pubkey, std::move(sig)));
364  break;
365  }
366  case PSBT_IN_SIGHASH:
367  if (sighash_type > 0) {
368  throw std::ios_base::failure("Duplicate Key, input sighash type already provided");
369  } else if (key.size() != 1) {
370  throw std::ios_base::failure("Sighash type key is more than one byte type");
371  }
373  break;
374  case PSBT_IN_REDEEMSCRIPT:
375  {
376  if (!redeem_script.empty()) {
377  throw std::ios_base::failure("Duplicate Key, input redeemScript already provided");
378  } else if (key.size() != 1) {
379  throw std::ios_base::failure("Input redeemScript key is more than one byte type");
380  }
381  s >> redeem_script;
382  break;
383  }
384  case PSBT_IN_WITNESSSCRIPT:
385  {
386  if (!witness_script.empty()) {
387  throw std::ios_base::failure("Duplicate Key, input witnessScript already provided");
388  } else if (key.size() != 1) {
389  throw std::ios_base::failure("Input witnessScript key is more than one byte type");
390  }
391  s >> witness_script;
392  break;
393  }
394  case PSBT_IN_BIP32_DERIVATION:
395  {
397  break;
398  }
399  case PSBT_IN_SCRIPTSIG:
400  {
401  if (!final_script_sig.empty()) {
402  throw std::ios_base::failure("Duplicate Key, input final scriptSig already provided");
403  } else if (key.size() != 1) {
404  throw std::ios_base::failure("Final scriptSig key is more than one byte type");
405  }
406  s >> final_script_sig;
407  break;
408  }
409  case PSBT_IN_SCRIPTWITNESS:
410  {
411  if (!final_script_witness.IsNull()) {
412  throw std::ios_base::failure("Duplicate Key, input final scriptWitness already provided");
413  } else if (key.size() != 1) {
414  throw std::ios_base::failure("Final scriptWitness key is more than one byte type");
415  }
417  break;
418  }
419  // Unknown stuff
420  default:
421  if (unknown.count(key) > 0) {
422  throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
423  }
424  // Read in the value
425  std::vector<unsigned char> val_bytes;
426  s >> val_bytes;
427  unknown.emplace(std::move(key), std::move(val_bytes));
428  break;
429  }
430  }
431 
432  if (!found_sep) {
433  throw std::ios_base::failure("Separator is missing at the end of an input map");
434  }
435  }
436 
437  template <typename Stream>
439  Unserialize(s);
440  }
441 };
442 
445 {
448  std::map<CPubKey, KeyOriginInfo> hd_keypaths;
449  std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
450 
451  bool IsNull() const;
452  void FillSignatureData(SignatureData& sigdata) const;
453  void FromSignatureData(const SignatureData& sigdata);
454  void Merge(const PSBTOutput& output);
455  bool IsSane() const;
457 
458  template <typename Stream>
459  inline void Serialize(Stream& s) const {
460  // Write the redeem script
461  if (!redeem_script.empty()) {
462  SerializeToVector(s, PSBT_OUT_REDEEMSCRIPT);
463  s << redeem_script;
464  }
465 
466  // Write the witness script
467  if (!witness_script.empty()) {
468  SerializeToVector(s, PSBT_OUT_WITNESSSCRIPT);
469  s << witness_script;
470  }
471 
472  // Write any hd keypaths
473  SerializeHDKeypaths(s, hd_keypaths, PSBT_OUT_BIP32_DERIVATION);
474 
475  // Write unknown things
476  for (auto& entry : unknown) {
477  s << entry.first;
478  s << entry.second;
479  }
480 
481  s << PSBT_SEPARATOR;
482  }
483 
484 
485  template <typename Stream>
486  inline void Unserialize(Stream& s) {
487  // Read loop
488  bool found_sep = false;
489  while(!s.empty()) {
490  // Read
491  std::vector<unsigned char> key;
492  s >> key;
493 
494  // the key is empty if that was actually a separator byte
495  // This is a special case for key lengths 0 as those are not allowed (except for separator)
496  if (key.empty()) {
497  found_sep = true;
498  break;
499  }
500 
501  // First byte of key is the type
502  unsigned char type = key[0];
503 
504  // Do stuff based on type
505  switch(type) {
506  case PSBT_OUT_REDEEMSCRIPT:
507  {
508  if (!redeem_script.empty()) {
509  throw std::ios_base::failure("Duplicate Key, output redeemScript already provided");
510  } else if (key.size() != 1) {
511  throw std::ios_base::failure("Output redeemScript key is more than one byte type");
512  }
513  s >> redeem_script;
514  break;
515  }
516  case PSBT_OUT_WITNESSSCRIPT:
517  {
518  if (!witness_script.empty()) {
519  throw std::ios_base::failure("Duplicate Key, output witnessScript already provided");
520  } else if (key.size() != 1) {
521  throw std::ios_base::failure("Output witnessScript key is more than one byte type");
522  }
523  s >> witness_script;
524  break;
525  }
526  case PSBT_OUT_BIP32_DERIVATION:
527  {
529  break;
530  }
531  // Unknown stuff
532  default: {
533  if (unknown.count(key) > 0) {
534  throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
535  }
536  // Read in the value
537  std::vector<unsigned char> val_bytes;
538  s >> val_bytes;
539  unknown.emplace(std::move(key), std::move(val_bytes));
540  break;
541  }
542  }
543  }
544 
545  if (!found_sep) {
546  throw std::ios_base::failure("Separator is missing at the end of an output map");
547  }
548  }
549 
550  template <typename Stream>
552  Unserialize(s);
553  }
554 };
555 
558 {
559  boost::optional<CMutableTransaction> tx;
560  std::vector<PSBTInput> inputs;
561  std::vector<PSBTOutput> outputs;
562  std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
563 
564  bool IsNull() const;
565  void Merge(const PartiallySignedTransaction& psbt);
566  bool IsSane() const;
568  PartiallySignedTransaction(const PartiallySignedTransaction& psbt_in) : tx(psbt_in.tx), inputs(psbt_in.inputs), outputs(psbt_in.outputs), unknown(psbt_in.unknown) {}
569 
570  // Only checks if they refer to the same transaction
572  {
573  return a.tx->GetHash() == b.tx->GetHash();
574  }
576  {
577  return !(a == b);
578  }
579 
580  template <typename Stream>
581  inline void Serialize(Stream& s) const {
582 
583  // magic bytes
584  s << PSBT_MAGIC_BYTES;
585 
586  // unsigned tx flag
587  SerializeToVector(s, PSBT_GLOBAL_UNSIGNED_TX);
588 
589  // Write serialized tx to a stream
590  OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
591  SerializeToVector(os, *tx);
592 
593  // Write the unknown things
594  for (auto& entry : unknown) {
595  s << entry.first;
596  s << entry.second;
597  }
598 
599  // Separator
600  s << PSBT_SEPARATOR;
601 
602  // Write inputs
603  for (const PSBTInput& input : inputs) {
604  s << input;
605  }
606  // Write outputs
607  for (const PSBTOutput& output : outputs) {
608  s << output;
609  }
610  }
611 
612 
613  template <typename Stream>
614  inline void Unserialize(Stream& s) {
615  // Read the magic bytes
616  uint8_t magic[5];
617  s >> magic;
618  if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) {
619  throw std::ios_base::failure("Invalid PSBT magic bytes");
620  }
621 
622  // Read global data
623  bool found_sep = false;
624  while(!s.empty()) {
625  // Read
626  std::vector<unsigned char> key;
627  s >> key;
628 
629  // the key is empty if that was actually a separator byte
630  // This is a special case for key lengths 0 as those are not allowed (except for separator)
631  if (key.empty()) {
632  found_sep = true;
633  break;
634  }
635 
636  // First byte of key is the type
637  unsigned char type = key[0];
638 
639  // Do stuff based on type
640  switch(type) {
641  case PSBT_GLOBAL_UNSIGNED_TX:
642  {
643  if (tx) {
644  throw std::ios_base::failure("Duplicate Key, unsigned tx already provided");
645  } else if (key.size() != 1) {
646  throw std::ios_base::failure("Global unsigned tx key is more than one byte type");
647  }
649  // Set the stream to serialize with non-witness since this should always be non-witness
650  OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
651  UnserializeFromVector(os, mtx);
652  tx = std::move(mtx);
653  // Make sure that all scriptSigs and scriptWitnesses are empty
654  for (const CTxIn& txin : tx->vin) {
655  if (!txin.scriptSig.empty() || !txin.scriptWitness.IsNull()) {
656  throw std::ios_base::failure("Unsigned tx does not have empty scriptSigs and scriptWitnesses.");
657  }
658  }
659  break;
660  }
661  // Unknown stuff
662  default: {
663  if (unknown.count(key) > 0) {
664  throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
665  }
666  // Read in the value
667  std::vector<unsigned char> val_bytes;
668  s >> val_bytes;
669  unknown.emplace(std::move(key), std::move(val_bytes));
670  }
671  }
672  }
673 
674  if (!found_sep) {
675  throw std::ios_base::failure("Separator is missing at the end of the global map");
676  }
677 
678  // Make sure that we got an unsigned tx
679  if (!tx) {
680  throw std::ios_base::failure("No unsigned transcation was provided");
681  }
682 
683  // Read input data
684  unsigned int i = 0;
685  while (!s.empty() && i < tx->vin.size()) {
686  PSBTInput input;
687  s >> input;
688  inputs.push_back(input);
689 
690  // Make sure the non-witness utxo matches the outpoint
691  if (input.non_witness_utxo && input.non_witness_utxo->GetHash() != tx->vin[i].prevout.hash) {
692  throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash");
693  }
694  ++i;
695  }
696  // Make sure that the number of inputs matches the number of inputs in the transaction
697  if (inputs.size() != tx->vin.size()) {
698  throw std::ios_base::failure("Inputs provided does not match the number of inputs in transaction.");
699  }
700 
701  // Read output data
702  i = 0;
703  while (!s.empty() && i < tx->vout.size()) {
704  PSBTOutput output;
705  s >> output;
706  outputs.push_back(output);
707  ++i;
708  }
709  // Make sure that the number of outputs matches the number of outputs in the transaction
710  if (outputs.size() != tx->vout.size()) {
711  throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction.");
712  }
713  // Sanity check
714  if (!IsSane()) {
715  throw std::ios_base::failure("PSBT is not sane.");
716  }
717  }
718 
719  template <typename Stream>
721  Unserialize(s);
722  }
723 };
724 
726 bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
727 
729 bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType);
730 bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType);
731 
733 bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& tx, PSBTInput& input, int index, int sighash = SIGHASH_ALL);
734 
736 SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout);
737 void UpdateInput(CTxIn& input, const SignatureData& data);
738 
739 /* Check whether we know how to sign for an output like this, assuming we
740  * have all private keys. While this function does not need private keys, the passed
741  * provider is used to look up public keys and redeemscripts by hash.
742  * Solvability is unrelated to whether we consider this output to be ours. */
743 bool IsSolvable(const SigningProvider& provider, const CScript& script);
744 
745 #endif // BITCOIN_SCRIPT_SIGN_H
virtual ~SigningProvider()
Definition: sign.h:33
bool IsNull() const
Definition: sign.cpp:656
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:497
unsigned char fingerprint[4]
Definition: sign.h:25
friend bool operator==(const PartiallySignedTransaction &a, const PartiallySignedTransaction &b)
Definition: sign.h:571
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
Definition: sign.h:111
friend bool operator!=(const PartiallySignedTransaction &a, const PartiallySignedTransaction &b)
Definition: sign.h:575
boost::optional< CMutableTransaction > tx
Definition: sign.h:559
void SerializeToVector(Stream &s, const X &... args)
Definition: sign.h:150
const SigningProvider * m_provider
Definition: sign.h:47
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:278
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.
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:975
const MutableTransactionSignatureChecker checker
Definition: sign.h:88
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:337
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format...
Definition: sign.h:109
std::map< CKeyID, CKey > keys
Definition: sign.h:62
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:67
static constexpr unsigned int PUBLIC_KEY_SIZE
secp256k1:
Definition: pubkey.h:36
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:493
Interface for signature creators.
Definition: sign.h:73
bool IsSane() const
Definition: sign.cpp:532
const BaseSignatureChecker & Checker() const override
Definition: sign.h:92
std::map< CKeyID, KeyOriginInfo > origins
Definition: sign.h:61
PSBTInput(deserialize_type, Stream &s)
Definition: sign.h:438
std::vector< std::vector< unsigned char > > stack
Definition: script.h:566
void Merge(const PSBTOutput &output)
Definition: sign.cpp:661
void Serialize(Stream &s) const
Definition: sign.h:581
void FromSignatureData(const SignatureData &sigdata)
Definition: sign.cpp:571
A version of CTransaction with the PSBT format.
Definition: sign.h:557
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
Definition: sign.h:114
CTxOut witness_utxo
Definition: sign.h:222
A signature creator for transactions.
Definition: sign.h:83
bool IsNull() const
Definition: script.h:571
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:55
void Serialize(Stream &s) const
Definition: sign.h:459
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
Definition: sign.h:171
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Definition: sign.cpp:670
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:402
void UnserializeMany(Stream &s)
Definition: serialize.h:946
void Serialize(Stream &s) const
Definition: sign.h:240
CScript redeem_script
Definition: sign.h:223
Dummy data type to identify deserializing constructors.
Definition: serialize.h:40
void UnserializeFromVector(Stream &s, X &... args)
Definition: sign.h:158
void Merge(const PSBTInput &input)
Definition: sign.cpp:600
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE
Definition: pubkey.h:37
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
Definition: sign.cpp:694
void FromSignatureData(const SignatureData &sigdata)
Definition: sign.cpp:643
bool IsNull() const
Definition: transaction.h:158
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
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &scriptPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition: sign.cpp:186
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
SignatureData(const CScript &script)
Definition: sign.h:117
A structure for PSBTs which contains per output information.
Definition: sign.h:444
std::vector< PSBTOutput > outputs
Definition: sign.h:561
std::map< CPubKey, KeyOriginInfo > hd_keypaths
Definition: sign.h:448
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:402
const bool m_hide_origin
Definition: sign.h:46
bool SignPSBTInput(const SigningProvider &provider, const CMutableTransaction &tx, PSBTInput &input, int index, int sighash=SIGHASH_ALL)
Signs a PSBTInput, verifying that all provided data matches what is being signed. ...
Definition: sign.cpp:242
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: sign.h:35
std::map< CScriptID, CScript > scripts
Definition: sign.h:59
An input of a transaction.
Definition: transaction.h:61
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
Definition: sign.h:229
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
Definition: sign.h:37
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: sign.cpp:675
std::map< CPubKey, KeyOriginInfo > hd_keypaths
Definition: sign.h:227
bool IsNull() const
Definition: sign.cpp:516
std::map< CKeyID, SigPair > partial_sigs
Definition: sign.h:228
An encapsulated public key.
Definition: pubkey.h:30
bool GetKey(const CKeyID &keyid, CKey &key) const override
Definition: sign.cpp:680
std::map< CKeyID, CPubKey > pubkeys
Definition: sign.h:60
std::pair< CPubKey, std::vector< unsigned char > > SigPair
Definition: sign.h:101
CScriptWitness final_script_witness
Definition: sign.h:226
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
Definition: sign.h:449
A structure for PSBTs which contain per-input information.
Definition: sign.h:219
void Unserialize(Stream &s)
Definition: sign.h:614
An output of a transaction.
Definition: transaction.h:131
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:408
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:494
PSBTInput()
Definition: sign.h:237
void SerializeMany(Stream &s)
Definition: serialize.h:934
PSBTOutput()
Definition: sign.h:456
std::vector< PSBTInput > inputs
Definition: sign.h:560
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Definition: sign.h:34
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
Definition: sign.h:112
virtual bool GetKey(const CKeyID &address, CKey &key) const
Definition: sign.h:36
const SigningProvider & DUMMY_SIGNING_PROVIDER
Definition: sign.cpp:495
int sighash_type
Definition: sign.h:230
CScript scriptSig
Definition: transaction.h:65
const bool m_hide_secret
Definition: sign.h:45
#define X(name)
Definition: net.cpp:685
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Definition: sign.cpp:692
bool IsSane() const
Definition: sign.cpp:618
bool GetKey(const CKeyID &keyid, CKey &key) const override
Definition: sign.cpp:695
void FillSignatureData(SignatureData &sigdata) const
Definition: sign.cpp:630
An interface to be implemented by keystores that support signing.
Definition: sign.h:30
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:424
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, uint8_t type)
Definition: sign.h:206
virtual ~BaseSignatureCreator()
Definition: sign.h:75
PartiallySignedTransaction(const PartiallySignedTransaction &psbt_in)
Definition: sign.h:568
CScript witness_script
Definition: sign.h:447
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
CScript redeem_script
Definition: sign.h:446
bool empty() const
Definition: prevector.h:297
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
Definition: sign.cpp:686
CTransactionRef non_witness_utxo
Definition: sign.h:221
A reference to a CKey: the Hash360 of its serialized public key.
Definition: pubkey.h:20
bool IsSane() const
size_t GetSerializeSizeMany(int nVersion, const T &... t)
Definition: serialize.h:987
void Unserialize(Stream &s)
Definition: sign.h:486
void Unserialize(Stream &s)
Definition: sign.h:303
SignatureData()
Definition: sign.h:116
A reference to a CScript: the Hash360 of its serialization (see script.h)
Definition: standard.h:22
A mutable version of CTransaction.
Definition: transaction.h:360
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
Definition: sign.h:50
PSBTOutput(deserialize_type, Stream &s)
Definition: sign.h:551
An encapsulated private key.
Definition: key.h:27
CScript final_script_sig
Definition: sign.h:225
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:264
void Merge(const PartiallySignedTransaction &psbt)
Definition: sign.cpp:521
void FillSignatureData(SignatureData &sigdata) const
Definition: sign.cpp:545
MutableTransactionSignatureCreator(const CMutableTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, int nHashTypeIn=SIGHASH_ALL)
Definition: sign.cpp:16
bool complete
Stores whether the scriptSig and scriptWitness are complete.
Definition: sign.h:107
CScript witness_script
Definition: sign.h:224
PartiallySignedTransaction(deserialize_type, Stream &s)
Definition: sign.h:720
std::vector< uint32_t > path
Definition: sign.h:26
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: sign.cpp:693
CScript redeem_script
The redeemScript (if any) for the input.
Definition: sign.h:110
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
Definition: sign.h:562
bool witness
Stores whether the input this SigData corresponds to is a witness input.
Definition: sign.h:108
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
Definition: sign.cpp:697
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
Definition: sign.h:113
bool IsNull() const
Definition: sign.cpp:540
virtual const BaseSignatureChecker & Checker() const =0
const CMutableTransaction * txTo
Definition: sign.h:84
SigVersion
Definition: interpreter.h:131