PRCYCoin  2.0.0.7rc1
P2P Digital Currency
hdchain.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2018 The Dash Core developers
2 // Distributed under the MIT software license, see the accompanying
3 
4 #include "base58.h"
5 #include "bip39.h"
6 #include "chainparams.h"
7 #include "hdchain.h"
8 #include "tinyformat.h"
9 #include "util.h"
10 #include "utilstrencodings.h"
11 
13 {
15  id = UINT256_ZERO;
16  fCrypted = false;
17  vchSeed.clear();
18  vchMnemonic.clear();
19  vchMnemonicPassphrase.clear();
20  return IsNull();
21 }
22 
23 bool CHDChain::IsNull() const
24 {
25  return vchSeed.empty() || id == UINT256_ZERO;
26 }
27 
28 void CHDChain::SetCrypted(bool fCryptedIn)
29 {
30  fCrypted = fCryptedIn;
31 }
32 
33 bool CHDChain::IsCrypted() const
34 {
35  return fCrypted;
36 }
37 
38 void CHDChain::Debug(const std::string& strName) const
39 {
40  if (fCrypted) {
41  std::cout << "mnemonic: ***CRYPTED***" << std::endl;
42  std::cout << "mnemonicpassphrase: ***CRYPTED***" << std::endl;
43  std::cout << "seed: ***CRYPTED***" << std::endl;
44  } else {
45  std::cout << "mnemonic: " << std::string(vchMnemonic.begin(), vchMnemonic.end()).c_str() << std::endl;
46  std::cout << "mnemonicpassphrase: " << std::string(vchMnemonicPassphrase.begin(), vchMnemonicPassphrase.end()).c_str() << std::endl;
47  std::cout << "seed: " << HexStr(vchSeed).c_str() << std::endl;
48 
49  CExtKey extkey;
50  extkey.SetMaster(&vchSeed[0], vchSeed.size());
51 
52  CBitcoinExtKey b58extkey;
53  b58extkey.SetKey(extkey);
54  std::cout << "extended private masterkey: " << b58extkey.ToString().c_str() << std::endl;
55 
56  CExtPubKey extpubkey;
57  extpubkey = extkey.Neuter();
58 
59  CBitcoinExtPubKey b58extpubkey;
60  b58extpubkey.SetKey(extpubkey);
61  std::cout << "extended public masterkey: " << b58extpubkey.ToString().c_str() << std::endl;
62  }
63 }
64 
65 bool CHDChain::SetMnemonic(const SecureVector& vchMnemonic, const SecureVector& vchMnemonicPassphrase, bool fUpdateID)
66 {
68 }
69 
70 bool CHDChain::SetMnemonic(const SecureString& ssMnemonic, const SecureString& ssMnemonicPassphrase, bool fUpdateID)
71 {
72  SecureString ssMnemonicTmp = ssMnemonic;
73 
74  if (fUpdateID) {
75  // can't (re)set mnemonic if seed was already set
76  if (!IsNull())
77  return false;
78 
79  // empty mnemonic i.e. "generate a new one"
80  if (ssMnemonic.empty()) {
81  ssMnemonicTmp = CMnemonic::Generate(256);
82  }
83  // NOTE: default mnemonic passphrase is an empty string
84 
85  // printf("mnemonic: %s\n", ssMnemonicTmp.c_str());
86  if (!CMnemonic::Check(ssMnemonicTmp)) {
87  throw std::runtime_error(std::string(__func__) + ": invalid mnemonic: `" + std::string(ssMnemonicTmp.c_str()) + "`");
88  }
89 
90  CMnemonic::ToSeed(ssMnemonicTmp, ssMnemonicPassphrase, vchSeed);
91  id = GetSeedHash();
92  }
93 
94  vchMnemonic = SecureVector(ssMnemonicTmp.begin(), ssMnemonicTmp.end());
95  vchMnemonicPassphrase = SecureVector(ssMnemonicPassphrase.begin(), ssMnemonicPassphrase.end());
96 
97  return !IsNull();
98 }
99 
100 bool CHDChain::GetMnemonic(SecureVector& vchMnemonicRet, SecureVector& vchMnemonicPassphraseRet) const
101 {
102  // mnemonic was not set, fail
103  if (vchMnemonic.empty())
104  return false;
105 
106  vchMnemonicRet = vchMnemonic;
107  vchMnemonicPassphraseRet = vchMnemonicPassphrase;
108  return true;
109 }
110 
111 bool CHDChain::GetMnemonic(SecureString& ssMnemonicRet, SecureString& ssMnemonicPassphraseRet) const
112 {
113  // mnemonic was not set, fail
114  if (vchMnemonic.empty())
115  return false;
116 
117  ssMnemonicRet = SecureString(vchMnemonic.begin(), vchMnemonic.end());
118  ssMnemonicPassphraseRet = SecureString(vchMnemonicPassphrase.begin(), vchMnemonicPassphrase.end());
119 
120  return true;
121 }
122 
123 bool CHDChain::SetSeed(const SecureVector& vchSeedIn, bool fUpdateID)
124 {
125  vchSeed = vchSeedIn;
126 
127  if (fUpdateID) {
128  id = GetSeedHash();
129  }
130 
131  return !IsNull();
132 }
133 
135 {
136  return vchSeed;
137 }
138 
140 {
141  return Hash(vchSeed.begin(), vchSeed.end());
142 }
143 
144 void CHDChain::DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_t nChildIndex, CExtKey& extKeyRet)
145 {
146  // Use BIP44 keypath scheme i.e. m / purpose' / coin_type' / account' / change / address_index
147  CExtKey masterKey; //hd master key
148  CExtKey purposeKey; //key at m/purpose'
149  CExtKey cointypeKey; //key at m/purpose'/coin_type'
150  CExtKey accountKey; //key at m/purpose'/coin_type'/account'
151  CExtKey changeKey; //key at m/purpose'/coin_type'/account'/change
152  CExtKey childKey; //key at m/purpose'/coin_type'/account'/change/address_index
153 
154  masterKey.SetMaster(&vchSeed[0], vchSeed.size());
155 
156  // Use hardened derivation for purpose, coin_type and account
157  // (keys >= 0x80000000 are hardened after bip32)
158 
159  // derive m/purpose'
160  masterKey.Derive(purposeKey, 44 | 0x80000000);
161  // derive m/purpose'/coin_type'
162  purposeKey.Derive(cointypeKey, Params().ExtCoinType());
163  // derive m/purpose'/coin_type'/account'
164  cointypeKey.Derive(accountKey, nAccountIndex | 0x80000000);
165  // derive m/purpose'/coin_type'/account'/change
166  accountKey.Derive(changeKey, fInternal ? 1 : 0);
167  // derive m/purpose'/coin_type'/account'/change/address_index
168  changeKey.Derive(extKeyRet, nChildIndex);
169 }
170 
171 std::string CHDPubKey::GetKeyPath() const
172 {
173  return strprintf("m/44'/%d'/%d'/%d/%d", Params().ExtCoinType() & 0xFFF, nAccountIndex, nChangeIndex, extPubKey.nChild);
174 }
UINT256_ZERO
const uint256 UINT256_ZERO
constant uint256 instances
Definition: uint256.h:129
CHDPubKey::extPubKey
CExtPubKey extPubKey
Definition: hdchain.h:99
CHDChain::DeriveChildExtKey
void DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_t nChildIndex, CExtKey &extKeyRet)
Definition: hdchain.cpp:144
CHDChain::GetSeedHash
uint256 GetSeedHash()
Definition: hdchain.cpp:139
CHDChain::vchMnemonicPassphrase
SecureVector vchMnemonicPassphrase
Definition: hdchain.h:23
CHDChain::IsNull
bool IsNull() const
Definition: hdchain.cpp:23
CHDChain::nVersion
int nVersion
Definition: hdchain.h:15
CBitcoinExtKeyBase::SetKey
void SetKey(const K &key)
Definition: base58.h:148
CExtKey::Derive
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:182
CMnemonic::ToSeed
static void ToSeed(SecureString mnemonic, SecureString passphrase, SecureVector &seedRet)
Definition: bip39.cpp:130
CHDChain::GetSeed
SecureVector GetSeed() const
Definition: hdchain.cpp:134
chainparams.h
CHDChain::SetMnemonic
bool SetMnemonic(const SecureVector &vchMnemonic, const SecureVector &vchMnemonicPassphrase, bool fUpdateID)
Definition: hdchain.cpp:65
CBase58Data::ToString
std::string ToString() const
Definition: base58.cpp:200
tinyformat.h
CExtPubKey::nChild
unsigned int nChild
Definition: pubkey.h:208
CHDChain::fCrypted
bool fCrypted
Definition: hdchain.h:19
CExtKey
Definition: key.h:161
CHDChain::SetNull
bool SetNull()
Definition: hdchain.cpp:12
CHDChain::vchSeed
SecureVector vchSeed
Definition: hdchain.h:21
CHDChain::GetMnemonic
bool GetMnemonic(SecureVector &vchMnemonicRet, SecureVector &vchMnemonicPassphraseRet) const
Definition: hdchain.cpp:100
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:262
HexStr
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Definition: utilstrencodings.h:85
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CMnemonic::Generate
static SecureString Generate(int strength)
Definition: bip39.cpp:12
CMnemonic::Check
static bool Check(SecureString mnemonic)
Definition: bip39.cpp:58
CHDPubKey::nChangeIndex
uint32_t nChangeIndex
Definition: hdchain.h:102
CHDChain::SetCrypted
void SetCrypted(bool fCryptedIn)
Definition: hdchain.cpp:28
CHDChain::IsCrypted
bool IsCrypted() const
Definition: hdchain.cpp:33
strprintf
#define strprintf
Definition: tinyformat.h:1056
SecureVector
std::vector< unsigned char, secure_allocator< unsigned char > > SecureVector
Definition: allocators.h:267
CHDChain::SetSeed
bool SetSeed(const SecureVector &vchSeedIn, bool fUpdateID)
Definition: hdchain.cpp:123
hdchain.h
CHDPubKey::nAccountIndex
uint32_t nAccountIndex
Definition: hdchain.h:101
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:463
CHDChain::CURRENT_VERSION
static const int CURRENT_VERSION
Definition: hdchain.h:14
base58.h
CBitcoinExtKeyBase
Definition: base58.h:145
utilstrencodings.h
CHDChain::Debug
void Debug(const std::string &strName) const
Definition: hdchain.cpp:38
Hash
std::string Hash(std::string input)
Compute the 256-bit hash of a std::string.
Definition: hash.h:122
CExtKey::Neuter
CExtPubKey Neuter() const
Definition: key.cpp:205
bip39.h
util.h
CExtPubKey
Definition: pubkey.h:205
CHDChain::vchMnemonic
SecureVector vchMnemonic
Definition: hdchain.h:22
CExtKey::SetMaster
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:191
CHDPubKey::GetKeyPath
std::string GetKeyPath() const
Definition: hdchain.cpp:171