Move CKeyMetadata back to wallet.h

This commit is contained in:
Kris Nuttycombe 2021-10-29 12:28:47 -06:00
parent dbcdc560de
commit 30517a002b
4 changed files with 76 additions and 86 deletions

View File

@ -159,7 +159,10 @@ std::pair<SaplingExtendedSpendingKey, bool> CWallet::GenerateLegacySaplingZKey(u
auto xsk = libzcash::SaplingExtendedSpendingKey::Legacy(seed, BIP44CoinType(), addrIndex);
if (!HaveSaplingSpendingKey(xsk.first.ToXFVK())) {
auto ivk = xsk.first.expsk.full_viewing_key().in_viewing_key();
mapSaplingZKeyMetadata[ivk] = xsk.second;
CKeyMetadata keyMeta(GetTime());
keyMeta.hdKeypath = xsk.second;
keyMeta.seedFp = seed.Fingerprint();
mapSaplingZKeyMetadata[ivk] = keyMeta;
if (!AddSaplingZKey(xsk.first)) {
throw std::runtime_error("CWallet::GenerateLegacySaplingZKey(): AddSaplingZKey failed.");
@ -264,7 +267,7 @@ CPubKey CWallet::GenerateNewKey()
auto seed = seedOpt.value();
if (!mnemonicHDChain.has_value()) {
mnemonicHDChain = CHDChain(seedOpt.value().Fingerprint(), GetTime());
mnemonicHDChain = CHDChain(seed.Fingerprint(), GetTime());
}
CHDChain& hdChain = mnemonicHDChain.value();
@ -272,7 +275,7 @@ CPubKey CWallet::GenerateNewKey()
// a valid spending key for the account ZCASH_LEGACY_ACCOUNT;
// therefore, the `value()` call here is safe.
BIP32AccountChains accountChains = BIP32AccountChains::ForAccount(
seedOpt.value(),
seed,
BIP44CoinType(),
ZCASH_LEGACY_ACCOUNT).value();
@ -287,7 +290,9 @@ CPubKey CWallet::GenerateNewKey()
assert(secret.VerifyPubKey(pubkey));
// Create new metadata
const CKeyMetadata& keyMeta = extKey.value().second;
CKeyMetadata keyMeta(GetTime());
keyMeta.hdKeypath = extKey.value().second;
keyMeta.seedFp = seed.Fingerprint();
mapKeyMetadata[pubkey.GetID()] = keyMeta;
if (nTimeFirstKey == 0 || keyMeta.nCreateTime < nTimeFirstKey)
nTimeFirstKey = keyMeta.nCreateTime;

View File

@ -129,6 +129,49 @@ public:
}
};
class CKeyMetadata
{
public:
static const int VERSION_BASIC=1;
static const int VERSION_WITH_HDDATA=10;
static const int CURRENT_VERSION=VERSION_WITH_HDDATA;
int nVersion;
int64_t nCreateTime; // 0 means unknown
std::string hdKeypath; //optional HD/zip32 keypath
uint256 seedFp;
CKeyMetadata()
{
SetNull();
}
CKeyMetadata(int64_t nCreateTime_)
{
SetNull();
nCreateTime = nCreateTime_;
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(this->nVersion);
READWRITE(nCreateTime);
if (this->nVersion >= VERSION_WITH_HDDATA)
{
READWRITE(hdKeypath);
READWRITE(seedFp);
}
}
void SetNull()
{
nVersion = CKeyMetadata::CURRENT_VERSION;
nCreateTime = 0;
hdKeypath.clear();
seedFp.SetNull();
}
};
/** Access to the wallet database */
class CWalletDB : public CDB
{

View File

@ -76,7 +76,7 @@ namespace libzcash {
// Transparent
//
std::optional<std::pair<CExtKey, CKeyMetadata>> DeriveZip32TransparentAccountKey(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId) {
std::optional<std::pair<CExtKey, HDKeyPath>> DeriveZip32TransparentAccountKey(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId) {
auto rawSeed = seed.RawSeed();
auto m = CExtKey::Master(rawSeed.data(), rawSeed.size());
@ -93,12 +93,9 @@ std::optional<std::pair<CExtKey, CKeyMetadata>> DeriveZip32TransparentAccountKey
auto result = m_32h_cth.value().Derive(accountId | ZIP32_HARDENED_KEY_LIMIT);
if (!result.has_value()) return std::nullopt;
int64_t nCreationTime = GetTime();
auto keyMeta = CKeyMetadata(nCreationTime);
keyMeta.hdKeypath = "m/32'/" + std::to_string(bip44CoinType) + "'/" + std::to_string(accountId) + "'";
keyMeta.seedFp = seed.Fingerprint();
auto hdKeypath = "m/32'/" + std::to_string(bip44CoinType) + "'/" + std::to_string(accountId) + "'";
return std::make_pair(result.value(), keyMeta);
return std::make_pair(result.value(), hdKeypath);
}
std::optional<BIP32AccountChains> BIP32AccountChains::ForAccount(
@ -117,36 +114,30 @@ std::optional<BIP32AccountChains> BIP32AccountChains::ForAccount(
return BIP32AccountChains(seed.Fingerprint(), bip44CoinType, accountId, external.value(), internal.value());
}
std::optional<std::pair<CExtKey, CKeyMetadata>> BIP32AccountChains::DeriveExternal(uint32_t addrIndex) {
std::optional<std::pair<CExtKey, HDKeyPath>> BIP32AccountChains::DeriveExternal(uint32_t addrIndex) {
auto childKey = external.Derive(addrIndex);
if (!childKey.has_value()) return std::nullopt;
int64_t nCreationTime = GetTime();
auto keyMeta = CKeyMetadata(nCreationTime);
keyMeta.hdKeypath = "m/32'/"
auto hdKeypath = "m/32'/"
+ std::to_string(bip44CoinType) + "'/"
+ std::to_string(accountId) + "'/"
+ "0/"
+ std::to_string(addrIndex);
keyMeta.seedFp = seedFp;
return std::make_pair(childKey.value(), keyMeta);
return std::make_pair(childKey.value(), hdKeypath);
}
std::optional<std::pair<CExtKey, CKeyMetadata>> BIP32AccountChains::DeriveInternal(uint32_t addrIndex) {
std::optional<std::pair<CExtKey, HDKeyPath>> BIP32AccountChains::DeriveInternal(uint32_t addrIndex) {
auto childKey = internal.Derive(addrIndex);
if (!childKey.has_value()) return std::nullopt;
int64_t nCreationTime = GetTime();
auto keyMeta = CKeyMetadata(nCreationTime);
keyMeta.hdKeypath = "m/32'/"
auto hdKeypath = "m/32'/"
+ std::to_string(bip44CoinType) + "'/"
+ std::to_string(accountId) + "'/"
+ "1/"
+ std::to_string(addrIndex);
keyMeta.seedFp = seedFp;
return std::make_pair(childKey.value(), keyMeta);
return std::make_pair(childKey.value(), hdKeypath);
}
//
@ -251,7 +242,7 @@ SaplingExtendedSpendingKey SaplingExtendedSpendingKey::Derive(uint32_t i) const
return xsk_i;
}
std::pair<SaplingExtendedSpendingKey, CKeyMetadata> SaplingExtendedSpendingKey::ForAccount(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId) {
std::pair<SaplingExtendedSpendingKey, HDKeyPath> SaplingExtendedSpendingKey::ForAccount(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId) {
auto m = Master(seed);
// We use a fixed keypath scheme of m/32'/coin_type'/account'
@ -264,15 +255,12 @@ std::pair<SaplingExtendedSpendingKey, CKeyMetadata> SaplingExtendedSpendingKey::
auto xsk = m_32h_cth.Derive(accountId | ZIP32_HARDENED_KEY_LIMIT);
// Create new metadata
int64_t nCreationTime = GetTime();
CKeyMetadata keyMeta(nCreationTime);
keyMeta.hdKeypath = "m/32'/" + std::to_string(bip44CoinType) + "'/" + std::to_string(accountId) + "'";
keyMeta.seedFp = seed.Fingerprint();
auto hdKeypath = "m/32'/" + std::to_string(bip44CoinType) + "'/" + std::to_string(accountId) + "'";
return std::make_pair(xsk, keyMeta);
return std::make_pair(xsk, hdKeypath);
}
std::pair<SaplingExtendedSpendingKey, CKeyMetadata> SaplingExtendedSpendingKey::Legacy(const HDSeed& seed, uint32_t bip44CoinType, uint32_t addressIndex) {
std::pair<SaplingExtendedSpendingKey, HDKeyPath> SaplingExtendedSpendingKey::Legacy(const HDSeed& seed, uint32_t bip44CoinType, uint32_t addressIndex) {
auto m = Master(seed);
// We use a fixed keypath scheme of m/32'/coin_type'/account'/addressIndex'
@ -289,15 +277,12 @@ std::pair<SaplingExtendedSpendingKey, CKeyMetadata> SaplingExtendedSpendingKey::
auto xsk = m_32h_cth_l.Derive(addressIndex | ZIP32_HARDENED_KEY_LIMIT);
// Create new metadata
int64_t nCreationTime = GetTime();
CKeyMetadata metadata(nCreationTime);
metadata.hdKeypath = "m/32'/"
auto hdKeypath = "m/32'/"
+ std::to_string(bip44CoinType) + "'/"
+ std::to_string(ZCASH_LEGACY_ACCOUNT) + "'/"
+ std::to_string(addressIndex) + "'";
metadata.seedFp = seed.Fingerprint();
return std::make_pair(xsk, metadata);
return std::make_pair(xsk, hdKeypath);
}
SaplingExtendedFullViewingKey SaplingExtendedSpendingKey::ToXFVK() const
@ -316,7 +301,7 @@ SaplingExtendedFullViewingKey SaplingExtendedSpendingKey::ToXFVK() const
// Unified
//
std::optional<std::pair<UnifiedSpendingKey, CKeyMetadata>> UnifiedSpendingKey::ForAccount(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId) {
std::optional<std::pair<UnifiedSpendingKey, HDKeyPath>> UnifiedSpendingKey::ForAccount(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId) {
UnifiedSpendingKey usk;
usk.accountId = accountId;

View File

@ -30,6 +30,8 @@ const uint32_t ZCASH_LEGACY_ACCOUNT = 0x7FFFFFFF;
typedef std::vector<unsigned char, secure_allocator<unsigned char>> RawHDSeed;
typedef std::string HDKeyPath;
class HDSeed {
protected:
RawHDSeed seed;
@ -163,51 +165,6 @@ public:
// This is not part of ZIP 32, but is here because it's linked to the HD seed.
uint256 ovkForShieldingFromTaddr(HDSeed& seed);
// Key derivation metadata
class CKeyMetadata
{
public:
static const int VERSION_BASIC=1;
static const int VERSION_WITH_HDDATA=10;
static const int CURRENT_VERSION=VERSION_WITH_HDDATA;
int nVersion;
int64_t nCreateTime; // 0 means unknown
std::string hdKeypath; //optional HD/zip32 keypath
uint256 seedFp;
CKeyMetadata()
{
SetNull();
}
CKeyMetadata(int64_t nCreateTime_)
{
SetNull();
nCreateTime = nCreateTime_;
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(this->nVersion);
READWRITE(nCreateTime);
if (this->nVersion >= VERSION_WITH_HDDATA)
{
READWRITE(hdKeypath);
READWRITE(seedFp);
}
}
void SetNull()
{
nVersion = CKeyMetadata::CURRENT_VERSION;
nCreateTime = 0;
hdKeypath.clear();
seedFp.SetNull();
}
};
namespace libzcash {
/**
@ -329,8 +286,8 @@ struct SaplingExtendedSpendingKey {
}
static SaplingExtendedSpendingKey Master(const HDSeed& seed);
static std::pair<SaplingExtendedSpendingKey, CKeyMetadata> ForAccount(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId);
static std::pair<SaplingExtendedSpendingKey, CKeyMetadata> Legacy(const HDSeed& seed, uint32_t bip44CoinType, uint32_t addressIndex);
static std::pair<SaplingExtendedSpendingKey, HDKeyPath> ForAccount(const HDSeed& seed, uint32_t bip44CoinType, uint32_t accountId);
static std::pair<SaplingExtendedSpendingKey, HDKeyPath> Legacy(const HDSeed& seed, uint32_t bip44CoinType, uint32_t addressIndex);
SaplingExtendedSpendingKey Derive(uint32_t i) const;
@ -408,7 +365,7 @@ private:
UnifiedSpendingKey() {}
public:
static std::optional<std::pair<UnifiedSpendingKey, CKeyMetadata>> ForAccount(
static std::optional<std::pair<UnifiedSpendingKey, HDKeyPath>> ForAccount(
const HDSeed& seed,
uint32_t bip44CoinType,
uint32_t accountId);
@ -426,7 +383,7 @@ public:
std::optional<unsigned long> ParseZip32KeypathAccount(const std::string& keyPath);
std::optional<std::pair<CExtKey, CKeyMetadata>> DeriveZip32TransparentMasterKey(
std::optional<std::pair<CExtKey, HDKeyPath>> DeriveZip32TransparentMasterKey(
const HDSeed& seed,
uint32_t bip44CoinType,
uint32_t accountId);
@ -447,8 +404,8 @@ public:
uint32_t bip44CoinType,
uint32_t accountId);
std::optional<std::pair<CExtKey, CKeyMetadata>> DeriveExternal(uint32_t addrIndex);
std::optional<std::pair<CExtKey, CKeyMetadata>> DeriveInternal(uint32_t addrIndex);
std::optional<std::pair<CExtKey, HDKeyPath>> DeriveExternal(uint32_t addrIndex);
std::optional<std::pair<CExtKey, HDKeyPath>> DeriveInternal(uint32_t addrIndex);
};
}