Fabcoin Core  0.16.2
P2P Digital Currency
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_KEY_H
7 #define FABCOIN_KEY_H
8 
9 #include <pubkey.h>
10 #include <serialize.h>
12 #include <uint256.h>
13 
14 #include <stdexcept>
15 #include <vector>
16 
17 
32 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
33 
35 class CKey
36 {
37 private:
40  bool fValid;
41 
44 
46  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
47 
49  bool static Check(const unsigned char* vch);
50 
51 public:
53  CKey() : fValid(false), fCompressed(false)
54  {
55  // Important: vch must be 32 bytes in length to not break serialization
56  keydata.resize(32);
57  }
58 
61  {
62  }
63 
64  friend bool operator==(const CKey& a, const CKey& b)
65  {
66  return a.fCompressed == b.fCompressed &&
67  a.size() == b.size() &&
68  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
69  }
70 
72  template <typename T>
73  void Set(const T pbegin, const T pend, bool fCompressedIn)
74  {
75  if (size_t(pend - pbegin) != keydata.size()) {
76  fValid = false;
77  } else if (Check(&pbegin[0])) {
78  memcpy(keydata.data(), (unsigned char*)&pbegin[0], keydata.size());
79  fValid = true;
80  fCompressed = fCompressedIn;
81  } else {
82  fValid = false;
83  }
84  }
85 
87  unsigned int size() const { return (fValid ? keydata.size() : 0); }
88  const unsigned char* begin() const { return keydata.data(); }
89  const unsigned char* end() const { return keydata.data() + size(); }
90 
92  bool IsValid() const { return fValid; }
93 
95  bool IsCompressed() const { return fCompressed; }
96 
98  void MakeNewKey(bool fCompressed);
99 
104  CPrivKey GetPrivKey() const;
105 
110  CPubKey GetPubKey() const;
111 
116  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
117 
125  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
126 
128  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
129 
134  bool VerifyPubKey(const CPubKey& vchPubKey) const;
135 
137  bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
138 };
139 
140 struct CExtKey {
141  unsigned char nDepth;
142  unsigned char vchFingerprint[4];
143  unsigned int nChild;
146 
147  friend bool operator==(const CExtKey& a, const CExtKey& b)
148  {
149  return a.nDepth == b.nDepth &&
150  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
151  a.nChild == b.nChild &&
152  a.chaincode == b.chaincode &&
153  a.key == b.key;
154  }
155 
156  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
157  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
158  bool Derive(CExtKey& out, unsigned int nChild) const;
159  CExtPubKey Neuter() const;
160  void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
161  template <typename Stream>
162  void Serialize(Stream& s) const
163  {
164  unsigned int len = BIP32_EXTKEY_SIZE;
165  ::WriteCompactSize(s, len);
166  unsigned char code[BIP32_EXTKEY_SIZE];
167  Encode(code);
168  s.write((const char *)&code[0], len);
169  }
170  template <typename Stream>
171  void Unserialize(Stream& s)
172  {
173  unsigned int len = ::ReadCompactSize(s);
174  unsigned char code[BIP32_EXTKEY_SIZE];
175  if (len != BIP32_EXTKEY_SIZE)
176  throw std::runtime_error("Invalid extended key size\n");
177  s.read((char *)&code[0], len);
178  Decode(code);
179  }
180 };
181 
183 void ECC_Start(void);
184 
186 void ECC_Stop(void);
187 
189 bool ECC_InitSanityCheck(void);
190 
191 #endif // FABCOIN_KEY_H
const unsigned char * begin() const
Definition: key.h:88
CKey key
Definition: key.h:145
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:175
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:273
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:983
#define T(i, x)
const unsigned char * end() const
Definition: key.h:89
Definition: key.h:140
unsigned char vchFingerprint[4]
Definition: key.h:142
void Unserialize(Stream &s)
Definition: key.h:171
bytes code
Definition: SmartVM.cpp:45
bool fValid
Whether this private key is valid.
Definition: key.h:40
void Serialize(Stream &s) const
Definition: key.h:162
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:92
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
Definition: key.h:32
#define a(i)
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:95
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:147
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:134
unsigned char nDepth
Definition: key.h:141
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:147
An encapsulated public key.
Definition: pubkey.h:39
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:126
unsigned int nChild
Definition: key.h:143
~CKey()
Destructor (again necessary because of memlocking).
Definition: key.h:60
ChainCode chaincode
Definition: key.h:144
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:204
#define b(i, j)
256-bit opaque blob.
Definition: uint256.h:132
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key...
Definition: key.cpp:189
void ECC_Start(void)
Initialize the elliptic curve support.
Definition: key.cpp:291
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:26
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:43
CKey()
Construct an invalid private key.
Definition: key.h:53
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:46
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:122
An encapsulated private key.
Definition: key.h:35
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:87
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:216
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:160
void ECC_Stop(void)
Deinitialize the elliptic curve support.
Definition: key.cpp:308
bool ECC_InitSanityCheck(void)
Check that required EC support is available at runtime.
Definition: key.cpp:284
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:64