Raven Core  3.0.0
P2P Digital Currency
pubkey.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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_PUBKEY_H
8 #define RAVEN_PUBKEY_H
9 
10 #include "hash.h"
11 #include "serialize.h"
12 #include "uint256.h"
13 
14 #include <stdexcept>
15 #include <vector>
16 
27 const unsigned int BIP32_EXTKEY_SIZE = 74;
28 
30 class CKeyID : public uint160
31 {
32 public:
33  CKeyID() : uint160() {}
34  explicit CKeyID(const uint160& in) : uint160(in) {}
35 };
36 
38 
40 class CPubKey
41 {
42 private:
43 
48  unsigned char vch[65];
49 
51  unsigned int static GetLen(unsigned char chHeader)
52  {
53  if (chHeader == 2 || chHeader == 3)
54  return 33;
55  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
56  return 65;
57  return 0;
58  }
59 
61  void Invalidate()
62  {
63  vch[0] = 0xFF;
64  }
65 
66 public:
69  {
70  Invalidate();
71  }
72 
74  template <typename T>
75  void Set(const T pbegin, const T pend)
76  {
77  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
78  if (len && len == (pend - pbegin))
79  memcpy(vch, (unsigned char*)&pbegin[0], len);
80  else
81  Invalidate();
82  }
83 
85  template <typename T>
86  CPubKey(const T pbegin, const T pend)
87  {
88  Set(pbegin, pend);
89  }
90 
92  explicit CPubKey(const std::vector<unsigned char>& _vch)
93  {
94  Set(_vch.begin(), _vch.end());
95  }
96 
98  unsigned int size() const { return GetLen(vch[0]); }
99  const unsigned char* begin() const { return vch; }
100  const unsigned char* end() const { return vch + size(); }
101  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
102 
104  friend bool operator==(const CPubKey& a, const CPubKey& b)
105  {
106  return a.vch[0] == b.vch[0] &&
107  memcmp(a.vch, b.vch, a.size()) == 0;
108  }
109  friend bool operator!=(const CPubKey& a, const CPubKey& b)
110  {
111  return !(a == b);
112  }
113  friend bool operator<(const CPubKey& a, const CPubKey& b)
114  {
115  return a.vch[0] < b.vch[0] ||
116  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
117  }
118 
120  template <typename Stream>
121  void Serialize(Stream& s) const
122  {
123  unsigned int len = size();
124  ::WriteCompactSize(s, len);
125  s.write((char*)vch, len);
126  }
127  template <typename Stream>
128  void Unserialize(Stream& s)
129  {
130  unsigned int len = ::ReadCompactSize(s);
131  if (len <= 65) {
132  s.read((char*)vch, len);
133  } else {
134  // invalid pubkey, skip available data
135  char dummy;
136  while (len--)
137  s.read(&dummy, 1);
138  Invalidate();
139  }
140  }
141 
143  CKeyID GetID() const
144  {
145  return CKeyID(Hash160(vch, vch + size()));
146  }
147 
149  uint256 GetHash() const
150  {
151  return Hash(vch, vch + size());
152  }
153 
154  /*
155  * Check syntactic correctness.
156  *
157  * Note that this is consensus critical as CheckSig() calls it!
158  */
159  bool IsValid() const
160  {
161  return size() > 0;
162  }
163 
165  bool IsFullyValid() const;
166 
168  bool IsCompressed() const
169  {
170  return size() == 33;
171  }
172 
177  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
178 
182  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
183 
185  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
186 
188  bool Decompress();
189 
191  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
192 };
193 
194 struct CExtPubKey {
195  unsigned char nDepth;
196  unsigned char vchFingerprint[4];
197  unsigned int nChild;
200 
201  friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
202  {
203  return a.nDepth == b.nDepth &&
204  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
205  a.nChild == b.nChild &&
206  a.chaincode == b.chaincode &&
207  a.pubkey == b.pubkey;
208  }
209 
210  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
211  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
212  bool Derive(CExtPubKey& out, unsigned int nChild) const;
213 
214  void Serialize(CSizeComputer& s) const
215  {
216  // Optimized implementation for ::GetSerializeSize that avoids copying.
217  s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
218  }
219  template <typename Stream>
220  void Serialize(Stream& s) const
221  {
222  unsigned int len = BIP32_EXTKEY_SIZE;
223  ::WriteCompactSize(s, len);
224  unsigned char code[BIP32_EXTKEY_SIZE];
225  Encode(code);
226  s.write((const char *)&code[0], len);
227  }
228  template <typename Stream>
229  void Unserialize(Stream& s)
230  {
231  unsigned int len = ::ReadCompactSize(s);
232  unsigned char code[BIP32_EXTKEY_SIZE];
233  if (len != BIP32_EXTKEY_SIZE)
234  throw std::runtime_error("Invalid extended key size\n");
235  s.read((char *)&code[0], len);
236  Decode(code);
237  }
238 };
239 
243 {
244  static int refcount;
245 
246 public:
247  ECCVerifyHandle();
248  ~ECCVerifyHandle();
249 };
250 
251 #endif // RAVEN_PUBKEY_H
unsigned char vchFingerprint[4]
Definition: pubkey.h:196
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:51
uint256 ChainCode
Definition: pubkey.h:37
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:113
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:257
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:961
CKeyID(const uint160 &in)
Definition: pubkey.h:34
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:61
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:75
static int refcount
Definition: pubkey.h:244
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:149
unsigned char nDepth
Definition: pubkey.h:195
void Serialize(Stream &s) const
Definition: pubkey.h:220
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:101
void Unserialize(Stream &s)
Definition: pubkey.h:128
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:208
ChainCode chaincode
Definition: pubkey.h:198
unsigned int nChild
Definition: pubkey.h:197
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:104
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143
const unsigned char * begin() const
Definition: pubkey.h:99
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:121
void Unserialize(Stream &s)
Definition: pubkey.h:229
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:242
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:201
const unsigned char * end() const
Definition: pubkey.h:100
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:68
bool IsValid() const
Definition: pubkey.h:159
An encapsulated public key.
Definition: pubkey.h:40
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:125
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:98
unsigned int size() const
Definition: uint256.h:77
unsigned char vch[65]
Just store the serialized data.
Definition: pubkey.h:48
CPubKey(const std::vector< unsigned char > &_vch)
Construct a public key from a byte vector.
Definition: pubkey.h:92
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:887
256-bit opaque blob.
Definition: uint256.h:123
CKeyID()
Definition: pubkey.h:33
void * memcpy(void *a, const void *b, size_t c)
const unsigned int BIP32_EXTKEY_SIZE
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
Definition: pubkey.h:27
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:86
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
160-bit opaque blob.
Definition: uint256.h:112
CPubKey pubkey
Definition: pubkey.h:199
void Serialize(CSizeComputer &s) const
Definition: pubkey.h:214
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:109
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:168