2021-04-20 21:33:02 +02:00
|
|
|
// Copyright (c) 2014-2020 The Dash Core developers
|
2017-05-29 13:51:40 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2018-04-02 00:30:17 +02:00
|
|
|
#ifndef BITCOIN_HDCHAIN_H
|
|
|
|
#define BITCOIN_HDCHAIN_H
|
2017-05-29 13:51:40 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <key.h>
|
|
|
|
#include <sync.h>
|
2017-05-29 13:51:40 +02:00
|
|
|
|
|
|
|
/* hd account data model */
|
|
|
|
class CHDAccount
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint32_t nExternalChainCounter;
|
|
|
|
uint32_t nInternalChainCounter;
|
|
|
|
|
|
|
|
CHDAccount() : nExternalChainCounter(0), nInternalChainCounter(0) {}
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
template <typename Stream, typename Operation>
|
2017-09-19 21:36:55 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
2017-05-29 13:51:40 +02:00
|
|
|
{
|
|
|
|
READWRITE(nExternalChainCounter);
|
|
|
|
READWRITE(nInternalChainCounter);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* simple HD chain data model */
|
|
|
|
class CHDChain
|
|
|
|
{
|
|
|
|
private:
|
2021-05-07 18:40:50 +02:00
|
|
|
mutable CCriticalSection cs;
|
|
|
|
|
2017-05-29 13:51:40 +02:00
|
|
|
static const int CURRENT_VERSION = 1;
|
2021-05-07 18:40:50 +02:00
|
|
|
int nVersion GUARDED_BY(cs) {CURRENT_VERSION};
|
2017-05-29 13:51:40 +02:00
|
|
|
|
2021-05-07 18:40:50 +02:00
|
|
|
uint256 id GUARDED_BY(cs);
|
2017-05-29 13:51:40 +02:00
|
|
|
|
2021-05-07 18:40:50 +02:00
|
|
|
bool fCrypted GUARDED_BY(cs) {false};
|
2017-05-29 13:51:40 +02:00
|
|
|
|
2021-05-07 18:40:50 +02:00
|
|
|
SecureVector vchSeed GUARDED_BY(cs);
|
|
|
|
SecureVector vchMnemonic GUARDED_BY(cs);
|
|
|
|
SecureVector vchMnemonicPassphrase GUARDED_BY(cs);
|
2017-05-29 13:51:40 +02:00
|
|
|
|
2021-05-07 18:40:50 +02:00
|
|
|
std::map<uint32_t, CHDAccount> GUARDED_BY(cs) mapAccounts;
|
2017-05-29 13:51:40 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2021-05-07 18:40:50 +02:00
|
|
|
CHDChain() = default;
|
2017-05-29 13:51:40 +02:00
|
|
|
CHDChain(const CHDChain& other) :
|
|
|
|
nVersion(other.nVersion),
|
|
|
|
id(other.id),
|
|
|
|
fCrypted(other.fCrypted),
|
|
|
|
vchSeed(other.vchSeed),
|
|
|
|
vchMnemonic(other.vchMnemonic),
|
|
|
|
vchMnemonicPassphrase(other.vchMnemonicPassphrase),
|
|
|
|
mapAccounts(other.mapAccounts)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
template <typename Stream, typename Operation>
|
2017-09-19 21:36:55 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
2017-05-29 13:51:40 +02:00
|
|
|
{
|
2021-05-07 18:40:50 +02:00
|
|
|
LOCK(cs);
|
2017-05-29 13:51:40 +02:00
|
|
|
READWRITE(this->nVersion);
|
|
|
|
READWRITE(id);
|
|
|
|
READWRITE(fCrypted);
|
|
|
|
READWRITE(vchSeed);
|
|
|
|
READWRITE(vchMnemonic);
|
|
|
|
READWRITE(vchMnemonicPassphrase);
|
|
|
|
READWRITE(mapAccounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(CHDChain& first, CHDChain& second) // nothrow
|
|
|
|
{
|
|
|
|
// enable ADL (not necessary in our case, but good practice)
|
|
|
|
using std::swap;
|
|
|
|
|
|
|
|
// by swapping the members of two classes,
|
|
|
|
// the two classes are effectively swapped
|
|
|
|
swap(first.nVersion, second.nVersion);
|
|
|
|
swap(first.id, second.id);
|
|
|
|
swap(first.fCrypted, second.fCrypted);
|
|
|
|
swap(first.vchSeed, second.vchSeed);
|
|
|
|
swap(first.vchMnemonic, second.vchMnemonic);
|
|
|
|
swap(first.vchMnemonicPassphrase, second.vchMnemonicPassphrase);
|
|
|
|
swap(first.mapAccounts, second.mapAccounts);
|
|
|
|
}
|
|
|
|
CHDChain& operator=(CHDChain from)
|
|
|
|
{
|
|
|
|
swap(*this, from);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetNull();
|
|
|
|
bool IsNull() const;
|
|
|
|
|
|
|
|
void SetCrypted(bool fCryptedIn);
|
|
|
|
bool IsCrypted() const;
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
void Debug(const std::string& strName) const;
|
2017-05-29 13:51:40 +02:00
|
|
|
|
|
|
|
bool SetMnemonic(const SecureVector& vchMnemonic, const SecureVector& vchMnemonicPassphrase, bool fUpdateID);
|
|
|
|
bool SetMnemonic(const SecureString& ssMnemonic, const SecureString& ssMnemonicPassphrase, bool fUpdateID);
|
|
|
|
bool GetMnemonic(SecureVector& vchMnemonicRet, SecureVector& vchMnemonicPassphraseRet) const;
|
|
|
|
bool GetMnemonic(SecureString& ssMnemonicRet, SecureString& ssMnemonicPassphraseRet) const;
|
|
|
|
|
|
|
|
bool SetSeed(const SecureVector& vchSeedIn, bool fUpdateID);
|
|
|
|
SecureVector GetSeed() const;
|
|
|
|
|
2021-05-07 18:40:50 +02:00
|
|
|
uint256 GetID() const { LOCK(cs); return id; }
|
2017-05-29 13:51:40 +02:00
|
|
|
|
|
|
|
uint256 GetSeedHash();
|
|
|
|
void DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_t nChildIndex, CExtKey& extKeyRet);
|
|
|
|
|
|
|
|
void AddAccount();
|
|
|
|
bool GetAccount(uint32_t nAccountIndex, CHDAccount& hdAccountRet);
|
|
|
|
bool SetAccount(uint32_t nAccountIndex, const CHDAccount& hdAccount);
|
|
|
|
size_t CountAccounts();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* hd pubkey data model */
|
|
|
|
class CHDPubKey
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static const int CURRENT_VERSION = 1;
|
|
|
|
int nVersion;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CExtPubKey extPubKey;
|
|
|
|
uint256 hdchainID;
|
|
|
|
uint32_t nAccountIndex;
|
|
|
|
uint32_t nChangeIndex;
|
|
|
|
|
|
|
|
CHDPubKey() : nVersion(CHDPubKey::CURRENT_VERSION), nAccountIndex(0), nChangeIndex(0) {}
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
template <typename Stream, typename Operation>
|
2017-09-19 21:36:55 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
2017-05-29 13:51:40 +02:00
|
|
|
{
|
|
|
|
READWRITE(this->nVersion);
|
|
|
|
READWRITE(extPubKey);
|
|
|
|
READWRITE(hdchainID);
|
|
|
|
READWRITE(nAccountIndex);
|
|
|
|
READWRITE(nChangeIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetKeyPath() const;
|
|
|
|
};
|
|
|
|
|
2018-04-02 00:30:17 +02:00
|
|
|
#endif // BITCOIN_HDCHAIN_H
|