Bitcoin ABC  0.28.12
P2P Digital Currency
sign.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <coins.h>
10 #include <hash.h>
11 #include <pubkey.h>
12 #include <script/interpreter.h>
13 #include <script/keyorigin.h>
14 #include <script/sighashtype.h>
15 #include <streams.h>
16 
17 class CKey;
18 class CKeyID;
20 class CScript;
21 class CScriptID;
22 class CTransaction;
23 class SigningProvider;
24 
27 public:
28  virtual ~BaseSignatureCreator() {}
29  virtual const BaseSignatureChecker &Checker() const = 0;
30 
32  virtual bool CreateSig(const SigningProvider &provider,
33  std::vector<uint8_t> &vchSig, const CKeyID &keyid,
34  const CScript &scriptCode) const = 0;
35 };
36 
40  unsigned int nIn;
44 
45 public:
47  const CMutableTransaction *txToIn, unsigned int nInIn,
48  const Amount &amountIn, SigHashType sigHashTypeIn = SigHashType());
49  const BaseSignatureChecker &Checker() const override { return checker; }
50  bool CreateSig(const SigningProvider &provider,
51  std::vector<uint8_t> &vchSig, const CKeyID &keyid,
52  const CScript &scriptCode) const override;
53 };
54 
59 
60 typedef std::pair<CPubKey, std::vector<uint8_t>> SigPair;
61 
62 // This struct contains information from a transaction input and also contains
63 // signatures for that input. The information contained here can be used to
64 // create a signature and is also filled by ProduceSignature in order to
65 // construct final scriptSigs.
66 struct SignatureData {
68  bool complete = false;
76  std::map<CKeyID, SigPair> signatures;
77  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
79  std::vector<CKeyID> missing_pubkeys;
81  std::vector<CKeyID> missing_sigs;
84 
86  explicit SignatureData(const CScript &script) : scriptSig(script) {}
87  void MergeSignatureData(SignatureData sigdata);
88 };
89 
90 // Takes a stream and multiple arguments and serializes them as if first
91 // serialized into a vector and then into the stream. The resulting output into
92 // the stream has the total serialized length of all of the objects followed by
93 // all objects concatenated with each other.
94 template <typename Stream, typename... X>
95 void SerializeToVector(Stream &s, const X &...args) {
96  WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
97  SerializeMany(s, args...);
98 }
99 
100 // Takes a stream and multiple arguments and unserializes them first as a vector
101 // then each object individually in the order provided in the arguments.
102 template <typename Stream, typename... X>
103 void UnserializeFromVector(Stream &s, X &...args) {
104  size_t expected_size = ReadCompactSize(s);
105  size_t remaining_before = s.size();
106  UnserializeMany(s, args...);
107  size_t remaining_after = s.size();
108  if (remaining_after + expected_size != remaining_before) {
109  throw std::ios_base::failure("Size of value was not the stated size");
110  }
111 }
112 
113 // Deserialize HD keypaths into a map
114 template <typename Stream>
115 void DeserializeHDKeypaths(Stream &s, const std::vector<uint8_t> &key,
116  std::map<CPubKey, KeyOriginInfo> &hd_keypaths) {
117  // Make sure that the key is the size of pubkey + 1
118  if (key.size() != CPubKey::SIZE + 1 &&
119  key.size() != CPubKey::COMPRESSED_SIZE + 1) {
120  throw std::ios_base::failure(
121  "Size of key was not the expected size for the type BIP32 keypath");
122  }
123  // Read in the pubkey from key
124  CPubKey pubkey(key.begin() + 1, key.end());
125  if (!pubkey.IsFullyValid()) {
126  throw std::ios_base::failure("Invalid pubkey");
127  }
128  if (hd_keypaths.count(pubkey) > 0) {
129  throw std::ios_base::failure(
130  "Duplicate Key, pubkey derivation path already provided");
131  }
132 
133  // Read in key path
134  uint64_t value_len = ReadCompactSize(s);
135  if (value_len % 4 || value_len == 0) {
136  throw std::ios_base::failure("Invalid length for HD key path");
137  }
138 
139  KeyOriginInfo keypath;
140  s >> keypath.fingerprint;
141  for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
142  uint32_t index;
143  s >> index;
144  keypath.path.push_back(index);
145  }
146 
147  // Add to map
148  hd_keypaths.emplace(pubkey, std::move(keypath));
149 }
150 
151 // Serialize HD keypaths to a stream from a map
152 template <typename Stream>
153 void SerializeHDKeypaths(Stream &s,
154  const std::map<CPubKey, KeyOriginInfo> &hd_keypaths,
155  uint8_t type) {
156  for (auto keypath_pair : hd_keypaths) {
157  if (!keypath_pair.first.IsValid()) {
158  throw std::ios_base::failure("Invalid CPubKey being serialized");
159  }
160  SerializeToVector(s, type, Span{keypath_pair.first});
161  WriteCompactSize(s, (keypath_pair.second.path.size() + 1) *
162  sizeof(uint32_t));
163  s << keypath_pair.second.fingerprint;
164  for (const auto &path : keypath_pair.second.path) {
165  s << path;
166  }
167  }
168 }
169 
171 bool ProduceSignature(const SigningProvider &provider,
172  const BaseSignatureCreator &creator,
173  const CScript &scriptPubKey, SignatureData &sigdata);
174 
176 bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey,
177  CMutableTransaction &txTo, unsigned int nIn,
178  const Amount amount, SigHashType sigHashType);
179 bool SignSignature(const SigningProvider &provider, const CTransaction &txFrom,
180  CMutableTransaction &txTo, unsigned int nIn,
181  SigHashType sigHashType);
182 
185  unsigned int nIn, const CTxOut &txout);
186 void UpdateInput(CTxIn &input, const SignatureData &data);
187 
194 bool IsSolvable(const SigningProvider &provider, const CScript &script);
195 
197 bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *provider,
198  const std::map<COutPoint, Coin> &coins,
199  SigHashType sigHashType,
200  std::map<int, std::string> &input_errors);
201 
202 #endif // BITCOIN_SCRIPT_SIGN_H
Interface for signature creators.
Definition: sign.h:26
virtual bool CreateSig(const SigningProvider &provider, std::vector< uint8_t > &vchSig, const CKeyID &keyid, const CScript &scriptCode) const =0
Create a singular (non-script) signature.
virtual const BaseSignatureChecker & Checker() const =0
virtual ~BaseSignatureCreator()
Definition: sign.h:28
An encapsulated secp256k1 private key.
Definition: key.h:28
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
A mutable version of CTransaction.
Definition: transaction.h:274
An encapsulated public key.
Definition: pubkey.h:31
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:37
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:36
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:256
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:24
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
An input of a transaction.
Definition: transaction.h:59
An output of a transaction.
Definition: transaction.h:128
A signature creator for transactions.
Definition: sign.h:38
MutableTransactionSignatureCreator(const CMutableTransaction *txToIn, unsigned int nInIn, const Amount &amountIn, SigHashType sigHashTypeIn=SigHashType())
Definition: sign.cpp:18
const BaseSignatureChecker & Checker() const override
Definition: sign.h:49
bool CreateSig(const SigningProvider &provider, std::vector< uint8_t > &vchSig, const CKeyID &keyid, const CScript &scriptCode) const override
Create a singular (non-script) signature.
Definition: sign.cpp:24
const CMutableTransaction * txTo
Definition: sign.h:39
const MutableTransactionSignatureChecker checker
Definition: sign.h:43
Signature hash type wrapper class.
Definition: sighashtype.h:37
An interface to be implemented by keystores that support signing.
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:224
160-bit opaque blob.
Definition: uint256.h:115
void SerializeMany(Stream &s)
Definition: serialize.h:1220
size_t GetSerializeSizeMany(int nVersion, const T &...t)
Definition: serialize.h:1281
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:431
void UnserializeMany(Stream &s)
Definition: serialize.h:1228
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1272
void UnserializeFromVector(Stream &s, X &...args)
Definition: sign.h:103
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:331
void SerializeToVector(Stream &s, const X &...args)
Definition: sign.h:95
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *provider, const std::map< COutPoint, Coin > &coins, SigHashType sigHashType, std::map< int, std::string > &input_errors)
Sign the CMutableTransaction.
Definition: sign.cpp:441
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Check whether we know how to sign for an output like this, assuming we have all private keys.
Definition: sign.cpp:424
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:421
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, uint8_t type)
Definition: sign.h:153
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:275
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:419
void DeserializeHDKeypaths(Stream &s, const std::vector< uint8_t > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
Definition: sign.h:115
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:198
std::pair< CPubKey, std::vector< uint8_t > > SigPair
Definition: sign.h:60
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const Amount amount, SigHashType sigHashType)
Produce a script signature for a transaction.
Definition: sign.cpp:350
Definition: amount.h:19
std::vector< uint32_t > path
Definition: keyorigin.h:14
uint8_t fingerprint[4]
First 32 bits of the Hash160 of the public key at the root of the path.
Definition: keyorigin.h:13
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
Definition: sign.h:83
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Definition: sign.h:81
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:335
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input.
Definition: sign.h:76
SignatureData()
Definition: sign.h:85
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
Definition: sign.h:77
CScript scriptSig
The scriptSig of an input.
Definition: sign.h:71
CScript redeem_script
The redeemScript (if any) for the input.
Definition: sign.h:73
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
Definition: sign.h:79
SignatureData(const CScript &script)
Definition: sign.h:86
bool complete
Stores whether the scriptSig are complete.
Definition: sign.h:68