hdac SDK
SDK for hdac blockchain development
key.h
1 #ifndef KEYS_H
2 #define KEYS_H
3 
4 #include "keys_global.h"
5 
6 #include <utils/allocators.h>
7 #include "pubkey.h"
8 
9 #include <vector>
10 
11 
26 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
27 
29 class CKey
30 {
31 private:
34  bool fValid;
35 
37  bool fCompressed;
38 
40  unsigned char vch[32];
41 
43  bool static Check(const unsigned char* vch);
44 
45 public:
47  CKey() : fValid(false), fCompressed(false)
48  {
49  LockObject(vch);
50  }
51 
53  CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
54  {
55  LockObject(vch);
56  memcpy(vch, secret.vch, sizeof(vch));
57  }
58 
61  {
62  UnlockObject(vch);
63  }
64 
65  friend bool operator==(const CKey& a, const CKey& b)
66  {
67  return a.fCompressed == b.fCompressed && a.size() == b.size() &&
68  memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
69  }
70 
72  template <typename T>
73  void Set(const T pbegin, const T pend, bool fCompressedIn)
74  {
75  if (pend - pbegin != 32) {
76  fValid = false;
77  return;
78  }
79  if (Check(&pbegin[0])) {
80  memcpy(vch, (unsigned char*)&pbegin[0], 32);
81  fValid = true;
82  fCompressed = fCompressedIn;
83  } else {
84  fValid = false;
85  }
86  }
87 
89  unsigned int size() const { return (fValid ? 32 : 0); }
90  const unsigned char* begin() const { return vch; }
91  const unsigned char* end() const { return vch + size(); }
92 
94  bool IsValid() const { return fValid; }
95 
97  bool IsCompressed() const { return fCompressed; }
98 
100  bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
101 
103  void MakeNewKey(bool fCompressed);
104 
109  CPrivKey GetPrivKey() const;
110 
115  CPubKey GetPubKey() const;
116 
121  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
122 
130  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
131 
133  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
134 
139  bool VerifyPubKey(const CPubKey& vchPubKey) const;
140 
142  bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
143 
145  static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
146 };
147 
149 void ECC_Start(void);
150 
152 void ECC_Stop(void);
153 
155 bool ECC_InitSanityCheck(void);
156 
157 #endif // KEYS_H
bool VerifyPubKey(const CPubKey &vchPubKey) const
Definition: key.cpp:180
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:94
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:97
CPubKey GetPubKey() const
Definition: key.cpp:152
CPrivKey GetPrivKey() const
Definition: key.cpp:137
Definition: pubkey.h:25
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:119
~CKey()
Destructor (again necessary because of memlocking).
Definition: key.h:60
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:209
Definition: uint256.h:294
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Initialize from a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:129
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:194
static bool CheckSignatureElement(const unsigned char *vch, int len, bool half)
Check whether an element of a signature (r or s) is valid.
CKey()
Construct an invalid private key.
Definition: key.h:47
CKey(const CKey &secret)
Copy constructor. This is necessary because of memlocking.
Definition: key.h:53
Definition: key.h:29
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:89
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:221
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Definition: key.cpp:165