Raven Core  3.0.0
P2P Digital Currency
key.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_KEY_H
8 #define RAVEN_KEY_H
9 
10 #include "pubkey.h"
11 #include "serialize.h"
13 #include "uint256.h"
14 
15 #include <stdexcept>
16 #include <vector>
17 
18 
33 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
34 
36 class CKey
37 {
38 private:
41  bool fValid;
42 
45 
47  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
48 
50  bool static Check(const unsigned char* vch);
51 
52 public:
54  CKey() : fValid(false), fCompressed(false)
55  {
56  // Important: vch must be 32 bytes in length to not break serialization
57  keydata.resize(32);
58  }
59 
60  friend bool operator==(const CKey& a, const CKey& b)
61  {
62  return a.fCompressed == b.fCompressed &&
63  a.size() == b.size() &&
64  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
65  }
66 
68  template <typename T>
69  void Set(const T pbegin, const T pend, bool fCompressedIn)
70  {
71  if (size_t(pend - pbegin) != keydata.size()) {
72  fValid = false;
73  } else if (Check(&pbegin[0])) {
74  memcpy(keydata.data(), (unsigned char*)&pbegin[0], keydata.size());
75  fValid = true;
76  fCompressed = fCompressedIn;
77  } else {
78  fValid = false;
79  }
80  }
81 
83  unsigned int size() const { return (fValid ? keydata.size() : 0); }
84  const unsigned char* begin() const { return keydata.data(); }
85  const unsigned char* end() const { return keydata.data() + size(); }
86 
88  bool IsValid() const { return fValid; }
89 
91  bool IsCompressed() const { return fCompressed; }
92 
94  void MakeNewKey(bool fCompressed);
95 
100  CPrivKey GetPrivKey() const;
101 
106  CPubKey GetPubKey() const;
107 
112  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
113 
121  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
122 
124  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
125 
130  bool VerifyPubKey(const CPubKey& vchPubKey) const;
131 
133  bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
134 };
135 
136 struct CExtKey {
137  unsigned char nDepth;
138  unsigned char vchFingerprint[4];
139  unsigned int nChild;
142 
143  friend bool operator==(const CExtKey& a, const CExtKey& b)
144  {
145  return a.nDepth == b.nDepth &&
146  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
147  a.nChild == b.nChild &&
148  a.chaincode == b.chaincode &&
149  a.key == b.key;
150  }
151 
152  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
153  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
154  bool Derive(CExtKey& out, unsigned int nChild) const;
155  CExtPubKey Neuter() const;
156  void SetSeed(const unsigned char* seed, unsigned int nSeedLen);
157  template <typename Stream>
158  void Serialize(Stream& s) const
159  {
160  unsigned int len = BIP32_EXTKEY_SIZE;
161  ::WriteCompactSize(s, len);
162  unsigned char code[BIP32_EXTKEY_SIZE];
163  Encode(code);
164  s.write((const char *)&code[0], len);
165  }
166  template <typename Stream>
167  void Unserialize(Stream& s)
168  {
169  unsigned int len = ::ReadCompactSize(s);
170  unsigned char code[BIP32_EXTKEY_SIZE];
171  if (len != BIP32_EXTKEY_SIZE)
172  throw std::runtime_error("Invalid extended key size\n");
173  s.read((char *)&code[0], len);
174  Decode(code);
175  }
176 };
177 
179 void ECC_Start(void);
180 
182 void ECC_Stop(void);
183 
185 bool ECC_InitSanityCheck(void);
186 
187 #endif // RAVEN_KEY_H
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:135
CKey key
Definition: key.h:141
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:257
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:961
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:176
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:148
Definition: key.h:136
unsigned char vchFingerprint[4]
Definition: key.h:138
void Unserialize(Stream &s)
Definition: key.h:167
const unsigned char * begin() const
Definition: key.h:84
bool fValid
Whether this private key is valid.
Definition: key.h:41
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:161
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:190
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:33
unsigned char nDepth
Definition: key.h:137
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:143
An encapsulated public key.
Definition: pubkey.h:40
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:127
unsigned int nChild
Definition: key.h:139
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:83
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:91
ChainCode chaincode
Definition: key.h:140
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:69
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:205
256-bit opaque blob.
Definition: uint256.h:123
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:217
void ECC_Start(void)
Initialize the elliptic curve support.
Definition: key.cpp:292
void * memcpy(void *a, const void *b, size_t c)
const unsigned char * end() const
Definition: key.h:85
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
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:44
CKey()
Construct an invalid private key.
Definition: key.h:54
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:47
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:123
An encapsulated private key.
Definition: key.h:36
void Serialize(Stream &s) const
Definition: key.h:158
void ECC_Stop(void)
Deinitialize the elliptic curve support.
Definition: key.cpp:309
bool ECC_InitSanityCheck(void)
Check that required EC support is available at runtime.
Definition: key.cpp:285
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:60
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:88