PIVX Core  5.6.99
P2P Digital Currency
hdchain.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef PIVX_WALLET_HDCHAIN_H
6 #define PIVX_WALLET_HDCHAIN_H
7 
8 #include "key.h"
9 
10 namespace HDChain {
11  namespace ChangeType {
12  static const uint8_t EXTERNAL = 0;
13  static const uint8_t INTERNAL = 1;
14  static const uint8_t STAKING = 2;
15  };
16 
17  namespace ChainCounterType {
18  static const uint8_t Standard = 0;
19  static const uint8_t Sapling = 1;
20  };
21 }
22 
23 /* Simple HD chain data model for regular and sapling addresses */
24 class CHDChain
25 {
26 private:
27  int nVersion;
29 
30 public:
31  // Standard/Sapling hd chain
32  static const int CURRENT_VERSION = 2;
33  // Single account counters.
34  uint32_t nExternalChainCounter{0};
35  uint32_t nInternalChainCounter{0};
36  uint32_t nStakingChainCounter{0};
37  // Chain counter type
38  uint8_t chainType{HDChain::ChainCounterType::Standard};
39 
40  explicit CHDChain(const uint8_t& _chainType = HDChain::ChainCounterType::Standard) : chainType(_chainType) { SetNull(); }
41 
43  {
44  READWRITE(obj.nVersion, obj.seed_id, obj.nExternalChainCounter, obj.nInternalChainCounter, obj.nStakingChainCounter);
45  if (obj.nVersion > 1) READWRITE(obj.chainType);
46  }
47 
48  bool SetNull();
49  bool IsNull() const;
50 
51  bool SetSeed(const CKeyID& seedId);
52  CKeyID GetID() const { return seed_id; }
53 
54  uint32_t& GetChainCounter(const uint8_t& type = HDChain::ChangeType::EXTERNAL) {
55  switch (type) {
56  case HDChain::ChangeType::EXTERNAL:
57  return nExternalChainCounter;
58  case HDChain::ChangeType::INTERNAL:
59  return nInternalChainCounter;
60  case HDChain::ChangeType::STAKING:
61  return nStakingChainCounter;
62  default:
63  throw std::runtime_error("HD chain type doesn't exist.");
64  }
65  }
66 };
67 
68 #endif // PIVX_WALLET_HDCHAIN_H
CHDChain(const uint8_t &_chainType=HDChain::ChainCounterType::Standard)
Definition: hdchain.h:40
SERIALIZE_METHODS(CHDChain, obj)
Definition: hdchain.h:42
bool SetSeed(const CKeyID &seedId)
Definition: hdchain.cpp:28
uint32_t nInternalChainCounter
Definition: hdchain.h:35
bool IsNull() const
Definition: hdchain.cpp:23
CKeyID seed_id
Definition: hdchain.h:28
uint32_t nStakingChainCounter
Definition: hdchain.h:36
CKeyID GetID() const
Definition: hdchain.h:52
int nVersion
Definition: hdchain.h:27
static const int CURRENT_VERSION
Definition: hdchain.h:32
bool SetNull()
Definition: hdchain.cpp:13
uint8_t chainType
Definition: hdchain.h:38
uint32_t & GetChainCounter(const uint8_t &type=HDChain::ChangeType::EXTERNAL)
Definition: hdchain.h:54
uint32_t nExternalChainCounter
Definition: hdchain.h:34
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
ChangeType
General change type (added, updated, removed).
Definition: guiinterface.h:24
#define READWRITE(...)
Definition: serialize.h:183