Bitcoin Core  0.21.1
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 SIZE = 279;
34  static const unsigned int COMPRESSED_SIZE = 214;
39  static_assert(
40  SIZE >= COMPRESSED_SIZE,
41  "COMPRESSED_SIZE is larger than SIZE");
42 
43 private:
46  bool fValid;
47 
49  bool fCompressed;
50 
52  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
53 
55  bool static Check(const unsigned char* vch);
56 
57 public:
59  CKey() : fValid(false), fCompressed(false)
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 
102  bool Negate();
103 
108  CPrivKey GetPrivKey() const;
109 
114  CPubKey GetPubKey() const;
115 
120  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, bool grind = true, uint32_t test_case = 0) const;
121 
129  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
130 
132  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
133 
138  bool VerifyPubKey(const CPubKey& vchPubKey) const;
139 
141  bool Load(const CPrivKey& privkey, const CPubKey& vchPubKey, bool fSkipCheck);
142 };
143 
144 struct CExtKey {
145  unsigned char nDepth;
146  unsigned char vchFingerprint[4];
147  unsigned int nChild;
150 
151  friend bool operator==(const CExtKey& a, const CExtKey& b)
152  {
153  return a.nDepth == b.nDepth &&
154  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
155  a.nChild == b.nChild &&
156  a.chaincode == b.chaincode &&
157  a.key == b.key;
158  }
159 
160  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
161  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
162  bool Derive(CExtKey& out, unsigned int nChild) const;
163  CExtPubKey Neuter() const;
164  void SetSeed(const unsigned char* seed, unsigned int nSeedLen);
165 };
166 
168 void ECC_Start();
169 
171 void ECC_Stop();
172 
174 bool ECC_InitSanityCheck();
175 
176 #endif // BITCOIN_KEY_H
CExtPubKey Neuter() const
Definition: key.cpp:312
CKey key
Definition: key.h:149
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:585
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:365
Definition: key.h:144
unsigned char vchFingerprint[4]
Definition: key.h:146
static const unsigned int SIZE
secp256k1:
Definition: key.h:33
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:293
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
static const unsigned int COMPRESSED_SIZE
Definition: key.h:34
unsigned char nDepth
Definition: key.h:145
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:151
An encapsulated public key.
Definition: pubkey.h:31
unsigned int nChild
Definition: key.h:147
ChainCode chaincode
Definition: key.h:148
void SetSeed(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:301
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:333
256-bit opaque blob.
Definition: uint256.h:124
void * memcpy(void *a, const void *b, size_t c)
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:52
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:18
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:341
An encapsulated private key.
Definition: key.h:27
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:322
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:348