Dash Core  0.12.2.1
P2P Digital Currency
hdchain.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2017 The Dash Core developers
2 // Distributed under the MIT software license, see the accompanying
3 #ifndef DASH_HDCHAIN_H
4 #define DASH_HDCHAIN_H
5 
6 #include "key.h"
7 #include "sync.h"
8 
9 /* hd account data model */
11 {
12 public:
15 
17 
19  template <typename Stream, typename Operation>
20  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
21  {
24  }
25 };
26 
27 /* simple HD chain data model */
28 class CHDChain
29 {
30 private:
31  static const int CURRENT_VERSION = 1;
32  int nVersion;
33 
35 
36  bool fCrypted;
37 
41 
42  std::map<uint32_t, CHDAccount> mapAccounts;
43  // critical section to protect mapAccounts
45 
46 public:
47 
49  CHDChain(const CHDChain& other) :
50  nVersion(other.nVersion),
51  id(other.id),
52  fCrypted(other.fCrypted),
53  vchSeed(other.vchSeed),
54  vchMnemonic(other.vchMnemonic),
57  {}
58 
60  template <typename Stream, typename Operation>
61  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
62  {
64  READWRITE(this->nVersion);
65  nVersion = this->nVersion;
66  READWRITE(id);
67  READWRITE(fCrypted);
72  }
73 
74  void swap(CHDChain& first, CHDChain& second) // nothrow
75  {
76  // enable ADL (not necessary in our case, but good practice)
77  using std::swap;
78 
79  // by swapping the members of two classes,
80  // the two classes are effectively swapped
81  swap(first.nVersion, second.nVersion);
82  swap(first.id, second.id);
83  swap(first.fCrypted, second.fCrypted);
84  swap(first.vchSeed, second.vchSeed);
85  swap(first.vchMnemonic, second.vchMnemonic);
87  swap(first.mapAccounts, second.mapAccounts);
88  }
90  {
91  swap(*this, from);
92  return *this;
93  }
94 
95  bool SetNull();
96  bool IsNull() const;
97 
98  void SetCrypted(bool fCryptedIn);
99  bool IsCrypted() const;
100 
101  void Debug(std::string strName) const;
102 
103  bool SetMnemonic(const SecureVector& vchMnemonic, const SecureVector& vchMnemonicPassphrase, bool fUpdateID);
104  bool SetMnemonic(const SecureString& ssMnemonic, const SecureString& ssMnemonicPassphrase, bool fUpdateID);
105  bool GetMnemonic(SecureVector& vchMnemonicRet, SecureVector& vchMnemonicPassphraseRet) const;
106  bool GetMnemonic(SecureString& ssMnemonicRet, SecureString& ssMnemonicPassphraseRet) const;
107 
108  bool SetSeed(const SecureVector& vchSeedIn, bool fUpdateID);
109  SecureVector GetSeed() const;
110 
111  uint256 GetID() const { return id; }
112 
114  void DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_t nChildIndex, CExtKey& extKeyRet);
115 
116  void AddAccount();
117  bool GetAccount(uint32_t nAccountIndex, CHDAccount& hdAccountRet);
118  bool SetAccount(uint32_t nAccountIndex, const CHDAccount& hdAccount);
119  size_t CountAccounts();
120 };
121 
122 /* hd pubkey data model */
124 {
125 private:
126  static const int CURRENT_VERSION = 1;
127  int nVersion;
128 
129 public:
132  uint32_t nAccountIndex;
133  uint32_t nChangeIndex;
134 
136 
138  template <typename Stream, typename Operation>
139  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
140  {
141  READWRITE(this->nVersion);
142  nVersion = this->nVersion;
143  READWRITE(extPubKey);
147  }
148 
149  std::string GetKeyPath() const;
150 };
151 
152 #endif // DASH_HDCHAIN_H
bool SetNull()
Definition: hdchain.cpp:12
SecureVector vchMnemonicPassphrase
Definition: hdchain.h:40
CExtPubKey extPubKey
Definition: hdchain.h:130
SecureVector vchSeed
Definition: hdchain.h:38
#define READWRITE(obj)
Definition: serialize.h:175
uint32_t nChangeIndex
Definition: hdchain.h:133
CHDChain & operator=(CHDChain from)
Definition: hdchain.h:89
ADD_SERIALIZE_METHODS
Definition: hdchain.h:18
ADD_SERIALIZE_METHODS
Definition: hdchain.h:137
bool IsNull() const
Definition: hdchain.cpp:27
bool GetMnemonic(SecureVector &vchMnemonicRet, SecureVector &vchMnemonicPassphraseRet) const
Definition: hdchain.cpp:107
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: hdchain.h:20
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:61
SecureVector GetSeed() const
Definition: hdchain.cpp:141
int nVersion
Definition: hdchain.h:127
CCriticalSection cs_accounts
Definition: hdchain.h:44
static const int CURRENT_VERSION
Definition: hdchain.h:126
uint256 hdchainID
Definition: hdchain.h:131
uint32_t nInternalChainCounter
Definition: hdchain.h:14
void swap(CHDChain &first, CHDChain &second)
Definition: hdchain.h:74
size_t CountAccounts()
Definition: hdchain.cpp:203
void DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_t nChildIndex, CExtKey &extKeyRet)
Definition: hdchain.cpp:151
uint256 id
Definition: hdchain.h:34
bool GetAccount(uint32_t nAccountIndex, CHDAccount &hdAccountRet)
Definition: hdchain.cpp:184
static const int CURRENT_VERSION
Definition: hdchain.h:31
uint32_t nAccountIndex
Definition: hdchain.h:132
void AddAccount()
Definition: hdchain.cpp:178
bool IsCrypted() const
Definition: hdchain.cpp:37
#define LOCK(cs)
Definition: sync.h:168
CHDChain()
Definition: hdchain.h:48
std::vector< unsigned char, secure_allocator< unsigned char > > SecureVector
Definition: secure.h:63
CHDPubKey()
Definition: hdchain.h:135
uint32_t nExternalChainCounter
Definition: hdchain.h:13
std::string GetKeyPath() const
Definition: hdchain.cpp:209
std::map< uint32_t, CHDAccount > mapAccounts
Definition: hdchain.h:42
SecureVector vchMnemonic
Definition: hdchain.h:39
CHDAccount()
Definition: hdchain.h:16
bool fCrypted
Definition: hdchain.h:36
CHDChain(const CHDChain &other)
Definition: hdchain.h:49
int nVersion
Definition: hdchain.h:32
void SetCrypted(bool fCryptedIn)
Definition: hdchain.cpp:32
uint256 GetID() const
Definition: hdchain.h:111
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: hdchain.h:139
bool SetMnemonic(const SecureVector &vchMnemonic, const SecureVector &vchMnemonicPassphrase, bool fUpdateID)
Definition: hdchain.cpp:72
bool SetAccount(uint32_t nAccountIndex, const CHDAccount &hdAccount)
Definition: hdchain.cpp:193
bool SetSeed(const SecureVector &vchSeedIn, bool fUpdateID)
Definition: hdchain.cpp:130
ADD_SERIALIZE_METHODS
Definition: hdchain.h:59
Definition: key.h:154
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: hdchain.h:61
void Debug(std::string strName) const
Definition: hdchain.cpp:42
uint256 GetSeedHash()
Definition: hdchain.cpp:146