Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2017 The Zcash 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 BITCOIN_KEY_H
8 #define BITCOIN_KEY_H
9 
10 #include <pubkey.h>
11 #include <serialize.h>
13 #include <uint256.h>
14 
15 #include <stdexcept>
16 #include <vector>
17 
18 
24 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
25 
27 class CKey
28 {
29 public:
33  static const unsigned int PRIVATE_KEY_SIZE = 279;
34  static const unsigned int COMPRESSED_PRIVATE_KEY_SIZE = 214;
39  static_assert(
41  "COMPRESSED_PRIVATE_KEY_SIZE is larger than PRIVATE_KEY_SIZE");
42 
43 private:
46  bool fValid;
47 
50 
52  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
53 
55  bool static Check(const unsigned char* vch);
56 
57 public:
60  {
61  // Important: vch must be 32 bytes in length to not break serialization
62  keydata.resize(32);
63  }
64 
65  friend bool operator==(const CKey& a, const CKey& b)
66  {
67  return a.fCompressed == b.fCompressed &&
68  a.size() == b.size() &&
69  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
70  }
71 
73  template <typename T>
74  void Set(const T pbegin, const T pend, bool fCompressedIn)
75  {
76  if (size_t(pend - pbegin) != keydata.size()) {
77  fValid = false;
78  } else if (Check(&pbegin[0])) {
79  memcpy(keydata.data(), (unsigned char*)&pbegin[0], keydata.size());
80  fValid = true;
81  fCompressed = fCompressedIn;
82  } else {
83  fValid = false;
84  }
85  }
86 
88  unsigned int size() const { return (fValid ? keydata.size() : 0); }
89  const unsigned char* begin() const { return keydata.data(); }
90  const unsigned char* end() const { return keydata.data() + size(); }
91 
93  bool IsValid() const { return fValid; }
94 
96  bool IsCompressed() const { return fCompressed; }
97 
99  void MakeNewKey(bool fCompressed);
100 
105  CPrivKey GetPrivKey() const;
106 
111  CPubKey GetPubKey() const;
112 
117  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
118 
126  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
127 
129  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
130 
135  bool VerifyPubKey(const CPubKey& vchPubKey) const;
136 
138  bool Load(const CPrivKey& privkey, const CPubKey& vchPubKey, bool fSkipCheck);
139 };
140 
141 struct CExtKey {
142  unsigned char nDepth;
143  unsigned char vchFingerprint[4];
144  unsigned int nChild;
147 
148  friend bool operator==(const CExtKey& a, const CExtKey& b)
149  {
150  return a.nDepth == b.nDepth &&
151  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
152  a.nChild == b.nChild &&
153  a.chaincode == b.chaincode &&
154  a.key == b.key;
155  }
156 
157  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
158  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
159  bool Derive(CExtKey& out, unsigned int nChild) const;
160  CExtPubKey Neuter() const;
161  void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
162  template <typename Stream>
163  void Serialize(Stream& s) const
164  {
165  unsigned int len = BIP32_EXTKEY_SIZE;
166  ::WriteCompactSize(s, len);
167  unsigned char code[BIP32_EXTKEY_SIZE];
168  Encode(code);
169  s.write((const char *)&code[0], len);
170  }
171  template <typename Stream>
172  void Unserialize(Stream& s)
173  {
174  unsigned int len = ::ReadCompactSize(s);
175  unsigned char code[BIP32_EXTKEY_SIZE];
176  if (len != BIP32_EXTKEY_SIZE)
177  throw std::runtime_error("Invalid extended key size\n");
178  s.read((char *)&code[0], len);
179  Decode(code);
180  }
181 };
182 
184 void ECC_Start(void);
185 
187 void ECC_Stop(void);
188 
190 bool ECC_InitSanityCheck(void);
191 
192 #endif // BITCOIN_KEY_H
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:166
CKey key
Definition: key.h:146
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:268
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:261
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1289
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:207
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:179
static const unsigned int COMPRESSED_PRIVATE_KEY_SIZE
Definition: key.h:34
Definition: key.h:141
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:297
unsigned char vchFingerprint[4]
Definition: key.h:143
void Unserialize(Stream &s)
Definition: key.h:172
const unsigned char * begin() const
Definition: key.h:89
bool fValid
see www.keylength.com script supports up to 75 for single byte push
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:192
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:221
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:24
false
Definition: bls_dkg.cpp:168
static const unsigned int PRIVATE_KEY_SIZE
secp256k1:
Definition: key.h:33
unsigned char nDepth
Definition: key.h:142
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:148
An encapsulated public key.
Definition: pubkey.h:30
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:158
unsigned int nChild
Definition: key.h:144
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:88
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
ChainCode chaincode
Definition: key.h:145
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:74
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:308
256-bit opaque blob.
Definition: uint256.h:123
CExtPubKey Neuter() const
Definition: key.cpp:287
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:248
void ECC_Start(void)
Initialize the elliptic curve support.
Definition: key.cpp:323
void * memcpy(void *a, const void *b, size_t c)
const unsigned char * end() const
Definition: key.h:90
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:17
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:49
CKey()
Construct an invalid private key.
Definition: key.h:59
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:276
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:52
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:154
An encapsulated private key.
Definition: key.h:27
void Serialize(Stream &s) const
Definition: key.h:163
void ECC_Stop(void)
Deinitialize the elliptic curve support.
Definition: key.cpp:340
bool ECC_InitSanityCheck(void)
Check that required EC support is available at runtime.
Definition: key.cpp:316
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:236
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:65
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:93
Released under the MIT license