Dash Core  0.12.2.1
P2P Digital Currency
pubkey.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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_PUBKEY_H
7 #define BITCOIN_PUBKEY_H
8 
9 #include "hash.h"
10 #include "serialize.h"
11 #include "uint256.h"
12 
13 #include <stdexcept>
14 #include <vector>
15 
27 class CKeyID : public uint160
28 {
29 public:
30  CKeyID() : uint160() {}
31  CKeyID(const uint160& in) : uint160(in) {}
32 };
33 
35 
37 class CPubKey
38 {
39 private:
40 
45  unsigned char vch[65];
46 
48  unsigned int static GetLen(unsigned char chHeader)
49  {
50  if (chHeader == 2 || chHeader == 3)
51  return 33;
52  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
53  return 65;
54  return 0;
55  }
56 
58  void Invalidate()
59  {
60  vch[0] = 0xFF;
61  }
62 
63 public:
66  {
67  Invalidate();
68  }
69 
71  template <typename T>
72  void Set(const T pbegin, const T pend)
73  {
74  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
75  if (len && len == (pend - pbegin))
76  memcpy(vch, (unsigned char*)&pbegin[0], len);
77  else
78  Invalidate();
79  }
80 
82  template <typename T>
83  CPubKey(const T pbegin, const T pend)
84  {
85  Set(pbegin, pend);
86  }
87 
89  CPubKey(const std::vector<unsigned char>& vch)
90  {
91  Set(vch.begin(), vch.end());
92  }
93 
95  unsigned int size() const { return GetLen(vch[0]); }
96  const unsigned char* begin() const { return vch; }
97  const unsigned char* end() const { return vch + size(); }
98  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
99 
101  friend bool operator==(const CPubKey& a, const CPubKey& b)
102  {
103  return a.vch[0] == b.vch[0] &&
104  memcmp(a.vch, b.vch, a.size()) == 0;
105  }
106  friend bool operator!=(const CPubKey& a, const CPubKey& b)
107  {
108  return !(a == b);
109  }
110  friend bool operator<(const CPubKey& a, const CPubKey& b)
111  {
112  return a.vch[0] < b.vch[0] ||
113  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
114  }
115 
117  unsigned int GetSerializeSize(int nType, int nVersion) const
118  {
119  return size() + 1;
120  }
121  template <typename Stream>
122  void Serialize(Stream& s, int nType, int nVersion) const
123  {
124  unsigned int len = size();
125  ::WriteCompactSize(s, len);
126  s.write((char*)vch, len);
127  }
128  template <typename Stream>
129  void Unserialize(Stream& s, int nType, int nVersion)
130  {
131  unsigned int len = ::ReadCompactSize(s);
132  if (len <= 65) {
133  s.read((char*)vch, len);
134  } else {
135  // invalid pubkey, skip available data
136  char dummy;
137  while (len--)
138  s.read(&dummy, 1);
139  Invalidate();
140  }
141  }
142 
144  CKeyID GetID() const
145  {
146  return CKeyID(Hash160(vch, vch + size()));
147  }
148 
150  uint256 GetHash() const
151  {
152  return Hash(vch, vch + size());
153  }
154 
155  /*
156  * Check syntactic correctness.
157  *
158  * Note that this is consensus critical as CheckSig() calls it!
159  */
160  bool IsValid() const
161  {
162  return size() > 0;
163  }
164 
166  bool IsFullyValid() const;
167 
169  bool IsCompressed() const
170  {
171  return size() == 33;
172  }
173 
178  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
179 
183  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
184 
186  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
187 
189  bool Decompress();
190 
192  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
193 };
194 
195 struct CExtPubKey {
196  unsigned char nDepth;
197  unsigned char vchFingerprint[4];
198  unsigned int nChild;
201 
202  friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
203  {
204  return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
205  a.chaincode == b.chaincode && a.pubkey == b.pubkey;
206  }
207 
208  void Encode(unsigned char code[74]) const;
209  void Decode(const unsigned char code[74]);
210  bool Derive(CExtPubKey& out, unsigned int nChild) const;
211 
212  unsigned int GetSerializeSize(int nType, int nVersion) const
213  {
214  return 74+1; //add one byte for the size (compact int)
215  }
216  template <typename Stream>
217  void Serialize(Stream& s, int nType, int nVersion) const
218  {
219  unsigned int len = 74;
220  ::WriteCompactSize(s, len);
221  unsigned char code[74];
222  Encode(code);
223  s.write((const char *)&code[0], len);
224  }
225  template <typename Stream>
226  void Unserialize(Stream& s, int nType, int nVersion)
227  {
228  unsigned int len = ::ReadCompactSize(s);
229  unsigned char code[74];
230  if (len != 74)
231  throw std::runtime_error("Invalid extended key size\n");
232  s.read((char *)&code[0], len);
233  Decode(code);
234  }
235 };
236 
240 {
241  static int refcount;
242 
243 public:
244  ECCVerifyHandle();
246 };
247 
248 #endif // BITCOIN_PUBKEY_H
void Decode(const unsigned char code[74])
Definition: pubkey.cpp:259
unsigned int nChild
Definition: pubkey.h:198
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: pubkey.h:212
uint256 ChainCode
Definition: pubkey.h:34
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
Definition: pubkey.cpp:228
unsigned int GetSerializeSize(int nType, int nVersion) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:117
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:98
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:95
void Serialize(Stream &s, int nType, int nVersion) const
Definition: pubkey.h:217
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:288
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:106
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:150
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:207
CKeyID()
Definition: pubkey.h:30
Definition: pubkey.h:27
uint160 Hash160(const T1 pbegin, const T1 pend)
Definition: hash.h:214
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:144
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:58
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:187
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:214
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:48
const unsigned char * begin() const
Definition: pubkey.h:96
void Unserialize(Stream &s, int nType, int nVersion)
Definition: pubkey.h:226
void Encode(unsigned char code[74]) const
Definition: pubkey.cpp:249
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:267
ChainCode chaincode
Definition: pubkey.h:199
unsigned char vchFingerprint[4]
Definition: pubkey.h:197
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:123
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Definition: pubkey.cpp:167
const unsigned char * end() const
Definition: pubkey.h:97
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:110
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:72
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:83
CPubKey pubkey
Definition: pubkey.h:200
static int refcount
Definition: pubkey.h:241
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:169
CKeyID(const uint160 &in)
Definition: pubkey.h:31
void * memcpy(void *a, const void *b, size_t c)
void Serialize(Stream &s, int nType, int nVersion) const
Definition: pubkey.h:122
CPubKey(const std::vector< unsigned char > &vch)
Construct a public key from a byte vector.
Definition: pubkey.h:89
bool IsValid() const
Definition: pubkey.h:160
Definition: pubkey.h:37
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:101
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:65
void WriteCompactSize(Stream &os, uint64_t nSize)
Definition: serialize.h:263
unsigned char nDepth
Definition: pubkey.h:196
unsigned char vch[65]
Definition: pubkey.h:45
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Definition: pubkey.cpp:275
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:202
void Unserialize(Stream &s, int nType, int nVersion)
Definition: pubkey.h:129