hdac SDK
SDK for hdac blockchain development
pubkey.h
1 #ifndef PUBKEY_H
2 #define PUBKEY_H
3 
4 #include <structs/hash.h>
5 #include <utils/serialize.h>
6 #include <structs/uint256.h>
7 #include <vector>
8 
9 #include <cstring>
10 #include <script/standard.h>
11 
22 typedef uint256 ChainCode;
23 
25 class CPubKey
26 {
27 private:
28 
33  unsigned char vch[65];
34 
36  unsigned int static GetLen(unsigned char chHeader)
37  {
38  if (chHeader == 2 || chHeader == 3)
39  return 33;
40  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
41  return 65;
42  return 0;
43  }
44 
46  void Invalidate()
47  {
48  vch[0] = 0xFF;
49  }
50 
51 public:
54  {
55  Invalidate();
56  }
57 
59  template <typename T>
60  void Set(const T pbegin, const T pend)
61  {
62  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
63  if (len && len == (pend - pbegin))
64  memcpy(vch, (unsigned char*)&pbegin[0], len);
65  else
66  Invalidate();
67  }
68 
70  template <typename T>
71  CPubKey(const T pbegin, const T pend)
72  {
73  Set(pbegin, pend);
74  }
75 
77  CPubKey(const std::vector<unsigned char>& vch)
78  {
79  Set(vch.begin(), vch.end());
80  }
81 
83  unsigned int size() const { return GetLen(vch[0]); }
84  const unsigned char* begin() const { return vch; }
85  const unsigned char* end() const { return vch + size(); }
86  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
87 
89  friend bool operator==(const CPubKey& a, const CPubKey& b)
90  {
91  return a.vch[0] == b.vch[0] &&
92  memcmp(a.vch, b.vch, a.size()) == 0;
93  }
94  friend bool operator!=(const CPubKey& a, const CPubKey& b)
95  {
96  return !(a == b);
97  }
98  friend bool operator<(const CPubKey& a, const CPubKey& b)
99  {
100  return a.vch[0] < b.vch[0] ||
101  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
102  }
103 
105  unsigned int GetSerializeSize(int nType, int nVersion) const
106  {
107  return size() + 1;
108  }
109  template <typename Stream>
110  void Serialize(Stream& s, int nType, int nVersion) const
111  {
112  unsigned int len = size();
113  ::WriteCompactSize(s, len);
114  s.write((char*)vch, len);
115  }
116  template <typename Stream>
117  void Unserialize(Stream& s, int nType, int nVersion)
118  {
119  unsigned int len = ::ReadCompactSize(s);
120  if (len <= 65) {
121  s.read((char*)vch, len);
122  } else {
123  // invalid pubkey, skip available data
124  char dummy;
125  while (len--)
126  s.read(&dummy, 1);
127  Invalidate();
128  }
129  }
130 
132  CKeyID GetID() const
133  {
134  return CKeyID(Hash160(vch, vch + size()));
135  }
136 
138  uint256 GetHash() const
139  {
140  return Hash(vch, vch + size());
141  }
142 
143  /*
144  * Check syntactic correctness.
145  *
146  * Note that this is consensus critical as CheckSig() calls it!
147  */
148  bool IsValid() const
149  {
150  return size() > 0;
151  }
152 
154  bool IsFullyValid() const;
155 
157  bool IsCompressed() const
158  {
159  return size() == 33;
160  }
161 
166  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
167 
171  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
172 
174  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
175 
177  bool Decompress();
178 
180  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
181 };
182 
186 {
187  static int refcount;
188 
189 public:
190  ECCVerifyHandle();
191  ~ECCVerifyHandle();
192 };
193 
194 #endif // PUBKEY_H
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:83
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:60
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Definition: pubkey.cpp:249
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:89
Definition: pubkey.h:185
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:184
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:53
Definition: pubkey.h:25
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:138
CPubKey(const std::vector< unsigned char > &vch)
Construct a public key from a byte vector.
Definition: pubkey.h:77
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:157
Definition: uint256.h:294
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:71
Definition: standard.h:8
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:204
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
Definition: pubkey.cpp:226
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Definition: pubkey.cpp:163
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:132
unsigned int GetSerializeSize(int nType, int nVersion) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:105
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:212