Rename `ZcashdUnifiedSpendingKeyMetadata` -> `ZcashdUnifiedAccount`

Also, fix a couple of minor documentation issues and
make UA generation tests a little more robust.
This commit is contained in:
Kris Nuttycombe 2021-12-22 13:57:37 -07:00
parent 24e46a16f1
commit 900cd50741
8 changed files with 44 additions and 31 deletions

View File

@ -107,6 +107,16 @@ public:
const libzcash::ZcashdUnifiedFullViewingKey &ufvk
) = 0;
/**
* Add the transparent component of the unified address, if any,
* to the keystore to make it possible to identify the unified
* full viewing key from which a transparent address was derived.
* It is not necessary for implementations to add shielded address
* components to the keystore because those will be automatically
* reconstructed when scanning the chain with a shielded incoming
* viewing key upon discovery of the address as having received
* funds.
*/
virtual void AddUnifiedAddress(
const libzcash::UFVKId& keyId,
const std::pair<libzcash::UnifiedAddress, libzcash::diversifier_index_t>& ua
@ -347,10 +357,6 @@ public:
virtual bool AddUnifiedFullViewingKey(
const libzcash::ZcashdUnifiedFullViewingKey &ufvk);
/**
* Add the transparent component of the unified address, if any,
* to the keystore to make it possible to identify the
*/
virtual void AddUnifiedAddress(
const libzcash::UFVKId& keyId,
const std::pair<libzcash::UnifiedAddress, libzcash::diversifier_index_t>& ua);

View File

@ -358,15 +358,12 @@ extern "C" {
* to obtain the diversifier index `j` at which the diversivier was
* derived.
*
* Returns `true` if the diversifier decrypted successfully to an index,
* `false` otherwise.
*
* Arguments:
* - dk: [c_uchar; 32] the byte representation of a Sapling diversifier key
* - addr: [c_uchar; 11] the bytes of the diversifier
* - j_ret: [c_uchar; 11] array that will store the resulting diversifier index
*/
bool librustzcash_sapling_diversifier_index(
void librustzcash_sapling_diversifier_index(
const unsigned char *dk,
const unsigned char *d,
unsigned char *j_ret

View File

@ -1136,14 +1136,13 @@ pub extern "C" fn librustzcash_sapling_diversifier_index(
dk: *const [c_uchar; 32],
d: *const [c_uchar; 11],
j_ret: *mut [c_uchar; 11],
) -> bool {
) {
let dk = zip32::DiversifierKey(unsafe { *dk });
let diversifier = sapling::Diversifier(unsafe { *d });
let j_ret = unsafe { &mut *j_ret };
let j = dk.diversifier_index(&diversifier);
j_ret.copy_from_slice(&j.0);
true
}
#[no_mangle]

View File

@ -2205,9 +2205,12 @@ TEST(WalletTests, GenerateUnifiedAddress) {
bool success = std::holds_alternative<std::pair<UnifiedAddress, ZcashdUnifiedAddressMetadata>>(uaResult);
EXPECT_TRUE(success);
auto zufvk = skpair.first.ToFullViewingKey();
auto ufvk = skpair.first.ToFullViewingKey();
auto addrpair = std::get<std::pair<UnifiedAddress, ZcashdUnifiedAddressMetadata>>(uaResult);
EXPECT_TRUE(addrpair.first.GetSaplingReceiver().has_value());
EXPECT_EQ(
addrpair.first.GetSaplingReceiver(),
ufvk.GetSaplingKey().value().Address(addrpair.second.GetDiversifierIndex()));
auto u4r = wallet.GetUnifiedForReceiver(addrpair.first.GetSaplingReceiver().value());
EXPECT_EQ(u4r, addrpair.first);

View File

@ -423,7 +423,7 @@ bool CWallet::AddCryptedSaplingSpendingKey(const libzcash::SaplingExtendedFullVi
return false;
}
std::pair<ZcashdUnifiedSpendingKey, ZcashdUnifiedSpendingKeyMetadata> CWallet::GenerateNewUnifiedSpendingKey() {
std::pair<ZcashdUnifiedSpendingKey, ZcashdUnifiedAccount> CWallet::GenerateNewUnifiedSpendingKey() {
AssertLockHeld(cs_wallet);
if (!mnemonicHDChain.has_value()) {
@ -448,7 +448,7 @@ std::pair<ZcashdUnifiedSpendingKey, ZcashdUnifiedSpendingKeyMetadata> CWallet::G
}
}
std::optional<std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedSpendingKeyMetadata>>
std::optional<std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedAccount>>
CWallet::GenerateUnifiedSpendingKeyForAccount(libzcash::AccountId accountId) {
AssertLockHeld(cs_wallet); // mapUnifiedSpendingKeyMeta
@ -463,7 +463,7 @@ std::optional<std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedSpendin
auto ufvk = sk.ToFullViewingKey();
auto ufvkid = ufvk.GetKeyID(Params());
ZcashdUnifiedSpendingKeyMetadata skmeta(seed.value().Fingerprint(), BIP44CoinType(), accountId, ufvkid);
ZcashdUnifiedAccount skmeta(seed.value().Fingerprint(), BIP44CoinType(), accountId, ufvkid);
// We don't store the spending key directly; instead, we store each of
// the spending key's components, in order to not violate invariants
@ -503,7 +503,7 @@ std::optional<std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedSpendin
if (fFileBacked) {
auto walletdb = CWalletDB(strWalletFile);
if (!( walletdb.WriteUnifiedFullViewingKey(ufvk) &&
walletdb.WriteUnifiedSpendingKeyMetadata(skmeta)
walletdb.WriteUnifiedAccount(skmeta)
)) {
throw std::runtime_error("CWalletDB::GenerateUnifiedSpendingKeyForAccount(): walletdb write failed.");
}
@ -645,7 +645,7 @@ bool CWallet::LoadUnifiedFullViewingKey(const libzcash::UnifiedFullViewingKey &k
return CCryptoKeyStore::AddUnifiedFullViewingKey(zufvk);
}
bool CWallet::LoadUnifiedKeyMetadata(const ZcashdUnifiedSpendingKeyMetadata &skmeta)
bool CWallet::LoadUnifiedAccount(const ZcashdUnifiedAccount &skmeta)
{
AssertLockHeld(cs_wallet); // mapUnifiedSpendingKeyMeta
auto metaKey = std::make_pair(skmeta.GetSeedFingerprint(), skmeta.GetAccountId());

View File

@ -889,7 +889,7 @@ public:
std::map<libzcash::SproutPaymentAddress, CKeyMetadata> mapSproutZKeyMetadata;
std::map<libzcash::SaplingIncomingViewingKey, CKeyMetadata> mapSaplingZKeyMetadata;
std::map<std::pair<libzcash::SeedFingerprint, libzcash::AccountId>, ZcashdUnifiedSpendingKeyMetadata> mapUnifiedSpendingKeyMeta;
std::map<std::pair<libzcash::SeedFingerprint, libzcash::AccountId>, ZcashdUnifiedAccount> mapUnifiedSpendingKeyMeta;
std::map<libzcash::UFVKId, ZcashdUnifiedFullViewingKeyMetadata> mapUnifiedFullViewingKeyMeta;
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
@ -1168,12 +1168,12 @@ public:
//! Generate the unified spending key from the wallet's mnemonic seed
//! for the next unused account identifier.
std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedSpendingKeyMetadata>
std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedAccount>
GenerateNewUnifiedSpendingKey();
//! Generate the next available unified spending key from the wallet's
//! mnemonic seed.
std::optional<std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedSpendingKeyMetadata>>
std::optional<std::pair<libzcash::ZcashdUnifiedSpendingKey, ZcashdUnifiedAccount>>
GenerateUnifiedSpendingKeyForAccount(libzcash::AccountId accountId);
//! Retrieves the UFVK derived from the wallet's mnemonic seed for the specified account.
@ -1192,7 +1192,7 @@ public:
bool AddUnifiedFullViewingKey(const libzcash::UnifiedFullViewingKey &ufvk);
bool LoadUnifiedFullViewingKey(const libzcash::UnifiedFullViewingKey &ufvk);
bool LoadUnifiedKeyMetadata(const ZcashdUnifiedSpendingKeyMetadata &skmeta);
bool LoadUnifiedAccount(const ZcashdUnifiedAccount &skmeta);
bool LoadUnifiedAddressMetadata(const ZcashdUnifiedAddressMetadata &addrmeta);
/**

View File

@ -221,11 +221,11 @@ bool CWalletDB::EraseSaplingExtendedFullViewingKey(
// Unified address & key storage
//
bool CWalletDB::WriteUnifiedSpendingKeyMetadata(const ZcashdUnifiedSpendingKeyMetadata& keymeta)
bool CWalletDB::WriteUnifiedAccount(const ZcashdUnifiedAccount& keymeta)
{
nWalletDBUpdateCounter++;
auto ufvkId = keymeta.GetKeyID();
return Write(std::make_pair(std::string("unifiedskmeta"), ufvkId), keymeta);
return Write(std::make_pair(std::string("unifiedaccount"), keymeta), 0x00);
}
bool CWalletDB::WriteUnifiedFullViewingKey(const libzcash::UnifiedFullViewingKey& ufvk)
@ -686,10 +686,18 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
return false;
}
}
else if (strType == "unifiedskmeta")
else if (strType == "unifiedaccount")
{
auto keymeta = ZcashdUnifiedSpendingKeyMetadata::Read(ssValue);
if (!pwallet->LoadUnifiedKeyMetadata(keymeta)) {
auto acct = ZcashdUnifiedAccount::Read(ssKey);
uint8_t value;
ssValue >> value;
if (value != 0x00) {
strErr = "Error reading wallet database: invalid unified account value.";
return false;
}
if (!pwallet->LoadUnifiedAccount(acct)) {
strErr = "Error reading wallet database: account ID mismatch for unified spending key.";
return false;
};

View File

@ -172,16 +172,16 @@ public:
}
};
class ZcashdUnifiedSpendingKeyMetadata {
class ZcashdUnifiedAccount {
private:
libzcash::SeedFingerprint seedFp;
uint32_t bip44CoinType;
libzcash::AccountId accountId;
libzcash::UFVKId ufvkId;
ZcashdUnifiedSpendingKeyMetadata() {}
ZcashdUnifiedAccount() {}
public:
ZcashdUnifiedSpendingKeyMetadata(
ZcashdUnifiedAccount(
libzcash::SeedFingerprint seedFp,
uint32_t bip44CoinType,
libzcash::AccountId accountId,
@ -216,8 +216,8 @@ public:
}
template <typename Stream>
static ZcashdUnifiedSpendingKeyMetadata Read(Stream& stream) {
ZcashdUnifiedSpendingKeyMetadata meta;
static ZcashdUnifiedAccount Read(Stream& stream) {
ZcashdUnifiedAccount meta;
stream >> meta;
return meta;
}
@ -386,7 +386,7 @@ public:
/// Unified key support.
bool WriteUnifiedSpendingKeyMetadata(const ZcashdUnifiedSpendingKeyMetadata& keymeta);
bool WriteUnifiedAccount(const ZcashdUnifiedAccount& keymeta);
bool WriteUnifiedFullViewingKey(const libzcash::UnifiedFullViewingKey& ufvk);
bool WriteUnifiedAddressMetadata(const ZcashdUnifiedAddressMetadata& addrmeta);