ViewingKey -> ReceivingKey per zcash/zips#117

This commit is contained in:
Jack Grigg
2017-02-23 18:27:41 +00:00
parent 89aaa67e9e
commit 642a1caf93
15 changed files with 44 additions and 44 deletions

View File

@@ -89,7 +89,7 @@ void test_full_api(ZCJoinSplit* js)
// Recipient should decrypt // Recipient should decrypt
// Now the recipient should spend the money again // Now the recipient should spend the money again
auto h_sig = js->h_sig(randomSeed, nullifiers, pubKeyHash); auto h_sig = js->h_sig(randomSeed, nullifiers, pubKeyHash);
ZCNoteDecryption decryptor(recipient_key.viewing_key()); ZCNoteDecryption decryptor(recipient_key.receiving_key());
auto note_pt = NotePlaintext::decrypt( auto note_pt = NotePlaintext::decrypt(
decryptor, decryptor,

View File

@@ -43,7 +43,7 @@ TEST(keystore_tests, store_and_retrieve_note_decryptor) {
keyStore.AddSpendingKey(sk); keyStore.AddSpendingKey(sk);
EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut)); EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut));
EXPECT_EQ(ZCNoteDecryption(sk.viewing_key()), decOut); EXPECT_EQ(ZCNoteDecryption(sk.receiving_key()), decOut);
} }
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
@@ -72,13 +72,13 @@ TEST(keystore_tests, store_and_retrieve_spending_key_in_encrypted_store) {
ASSERT_TRUE(keyStore.GetSpendingKey(addr, keyOut)); ASSERT_TRUE(keyStore.GetSpendingKey(addr, keyOut));
ASSERT_EQ(sk, keyOut); ASSERT_EQ(sk, keyOut);
EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut)); EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut));
EXPECT_EQ(ZCNoteDecryption(sk.viewing_key()), decOut); EXPECT_EQ(ZCNoteDecryption(sk.receiving_key()), decOut);
ASSERT_TRUE(keyStore.EncryptKeys(vMasterKey)); ASSERT_TRUE(keyStore.EncryptKeys(vMasterKey));
ASSERT_TRUE(keyStore.HaveSpendingKey(addr)); ASSERT_TRUE(keyStore.HaveSpendingKey(addr));
ASSERT_FALSE(keyStore.GetSpendingKey(addr, keyOut)); ASSERT_FALSE(keyStore.GetSpendingKey(addr, keyOut));
EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut)); EXPECT_TRUE(keyStore.GetNoteDecryptor(addr, decOut));
EXPECT_EQ(ZCNoteDecryption(sk.viewing_key()), decOut); EXPECT_EQ(ZCNoteDecryption(sk.receiving_key()), decOut);
// Unlocking with a random key should fail // Unlocking with a random key should fail
uint256 r2 {GetRandHash()}; uint256 r2 {GetRandHash()};
@@ -109,19 +109,19 @@ TEST(keystore_tests, store_and_retrieve_spending_key_in_encrypted_store) {
ASSERT_TRUE(keyStore.GetSpendingKey(addr2, keyOut)); ASSERT_TRUE(keyStore.GetSpendingKey(addr2, keyOut));
ASSERT_EQ(sk2, keyOut); ASSERT_EQ(sk2, keyOut);
EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut)); EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut));
EXPECT_EQ(ZCNoteDecryption(sk2.viewing_key()), decOut); EXPECT_EQ(ZCNoteDecryption(sk2.receiving_key()), decOut);
ASSERT_TRUE(keyStore.Lock()); ASSERT_TRUE(keyStore.Lock());
ASSERT_TRUE(keyStore.HaveSpendingKey(addr2)); ASSERT_TRUE(keyStore.HaveSpendingKey(addr2));
ASSERT_FALSE(keyStore.GetSpendingKey(addr2, keyOut)); ASSERT_FALSE(keyStore.GetSpendingKey(addr2, keyOut));
EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut)); EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut));
EXPECT_EQ(ZCNoteDecryption(sk2.viewing_key()), decOut); EXPECT_EQ(ZCNoteDecryption(sk2.receiving_key()), decOut);
ASSERT_TRUE(keyStore.Unlock(vMasterKey)); ASSERT_TRUE(keyStore.Unlock(vMasterKey));
ASSERT_TRUE(keyStore.GetSpendingKey(addr2, keyOut)); ASSERT_TRUE(keyStore.GetSpendingKey(addr2, keyOut));
ASSERT_EQ(sk2, keyOut); ASSERT_EQ(sk2, keyOut);
EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut)); EXPECT_TRUE(keyStore.GetNoteDecryptor(addr2, decOut));
EXPECT_EQ(ZCNoteDecryption(sk2.viewing_key()), decOut); EXPECT_EQ(ZCNoteDecryption(sk2.receiving_key()), decOut);
keyStore.GetPaymentAddresses(addrs); keyStore.GetPaymentAddresses(addrs);
ASSERT_EQ(2, addrs.size()); ASSERT_EQ(2, addrs.size());

View File

@@ -89,6 +89,6 @@ bool CBasicKeyStore::AddSpendingKey(const libzcash::SpendingKey &sk)
LOCK(cs_SpendingKeyStore); LOCK(cs_SpendingKeyStore);
auto address = sk.address(); auto address = sk.address();
mapSpendingKeys[address] = sk; mapSpendingKeys[address] = sk;
mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(sk.viewing_key()))); mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(sk.receiving_key())));
return true; return true;
} }

View File

@@ -63,7 +63,7 @@ CWalletTx GetValidReceive(ZCJoinSplit& params,
libzcash::Note GetNote(ZCJoinSplit& params, libzcash::Note GetNote(ZCJoinSplit& params,
const libzcash::SpendingKey& sk, const libzcash::SpendingKey& sk,
const CTransaction& tx, size_t js, size_t n) { const CTransaction& tx, size_t js, size_t n) {
ZCNoteDecryption decryptor {sk.viewing_key()}; ZCNoteDecryption decryptor {sk.receiving_key()};
auto hSig = tx.vjoinsplit[js].h_sig(params, tx.joinSplitPubKey); auto hSig = tx.vjoinsplit[js].h_sig(params, tx.joinSplitPubKey);
auto note_pt = libzcash::NotePlaintext::decrypt( auto note_pt = libzcash::NotePlaintext::decrypt(
decryptor, decryptor,

View File

@@ -555,7 +555,7 @@ bool AsyncRPCOperation_sendmany::main_impl() {
intermediates.insert(std::make_pair(tree.root(), tree)); // chained js are interstitial (found in between block boundaries) intermediates.insert(std::make_pair(tree.root(), tree)); // chained js are interstitial (found in between block boundaries)
// Decrypt the change note's ciphertext to retrieve some data we need // Decrypt the change note's ciphertext to retrieve some data we need
ZCNoteDecryption decryptor(spendingkey_.viewing_key()); ZCNoteDecryption decryptor(spendingkey_.receiving_key());
auto hSig = prevJoinSplit.h_sig(*pzcashParams, tx_.joinSplitPubKey); auto hSig = prevJoinSplit.h_sig(*pzcashParams, tx_.joinSplitPubKey);
try { try {
NotePlaintext plaintext = NotePlaintext::decrypt( NotePlaintext plaintext = NotePlaintext::decrypt(

View File

@@ -316,14 +316,14 @@ bool CCryptoKeyStore::AddSpendingKey(const libzcash::SpendingKey &sk)
if (!EncryptSecret(vMasterKey, vchSecret, address.GetHash(), vchCryptedSecret)) if (!EncryptSecret(vMasterKey, vchSecret, address.GetHash(), vchCryptedSecret))
return false; return false;
if (!AddCryptedSpendingKey(address, sk.viewing_key(), vchCryptedSecret)) if (!AddCryptedSpendingKey(address, sk.receiving_key(), vchCryptedSecret))
return false; return false;
} }
return true; return true;
} }
bool CCryptoKeyStore::AddCryptedSpendingKey(const libzcash::PaymentAddress &address, bool CCryptoKeyStore::AddCryptedSpendingKey(const libzcash::PaymentAddress &address,
const libzcash::ViewingKey &vk, const libzcash::ReceivingKey &rk,
const std::vector<unsigned char> &vchCryptedSecret) const std::vector<unsigned char> &vchCryptedSecret)
{ {
{ {
@@ -332,7 +332,7 @@ bool CCryptoKeyStore::AddCryptedSpendingKey(const libzcash::PaymentAddress &addr
return false; return false;
mapCryptedSpendingKeys[address] = vchCryptedSecret; mapCryptedSpendingKeys[address] = vchCryptedSecret;
mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(vk))); mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(rk)));
} }
return true; return true;
} }
@@ -384,7 +384,7 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
std::vector<unsigned char> vchCryptedSecret; std::vector<unsigned char> vchCryptedSecret;
if (!EncryptSecret(vMasterKeyIn, vchSecret, address.GetHash(), vchCryptedSecret)) if (!EncryptSecret(vMasterKeyIn, vchSecret, address.GetHash(), vchCryptedSecret))
return false; return false;
if (!AddCryptedSpendingKey(address, sk.viewing_key(), vchCryptedSecret)) if (!AddCryptedSpendingKey(address, sk.receiving_key(), vchCryptedSecret))
return false; return false;
} }
mapSpendingKeys.clear(); mapSpendingKeys.clear();

View File

@@ -201,7 +201,7 @@ public:
} }
} }
virtual bool AddCryptedSpendingKey(const libzcash::PaymentAddress &address, virtual bool AddCryptedSpendingKey(const libzcash::PaymentAddress &address,
const libzcash::ViewingKey &vk, const libzcash::ReceivingKey &rk,
const std::vector<unsigned char> &vchCryptedSecret); const std::vector<unsigned char> &vchCryptedSecret);
bool AddSpendingKey(const libzcash::SpendingKey &sk); bool AddSpendingKey(const libzcash::SpendingKey &sk);
bool HaveSpendingKey(const libzcash::PaymentAddress &address) const bool HaveSpendingKey(const libzcash::PaymentAddress &address) const

View File

@@ -328,7 +328,7 @@ TEST(wallet_tests, GetNoteNullifier) {
auto sk = libzcash::SpendingKey::random(); auto sk = libzcash::SpendingKey::random();
auto address = sk.address(); auto address = sk.address();
auto dec = ZCNoteDecryption(sk.viewing_key()); auto dec = ZCNoteDecryption(sk.receiving_key());
auto wtx = GetValidReceive(sk, 10, true); auto wtx = GetValidReceive(sk, 10, true);
auto note = GetNote(sk, wtx, 0, 1); auto note = GetNote(sk, wtx, 0, 1);

View File

@@ -2667,7 +2667,7 @@ UniValue zc_raw_receive(const UniValue& params, bool fHelp)
} }
} }
ZCNoteDecryption decryptor(k.viewing_key()); ZCNoteDecryption decryptor(k.receiving_key());
NotePlaintext npt = NotePlaintext::decrypt( NotePlaintext npt = NotePlaintext::decrypt(
decryptor, decryptor,
@@ -2908,20 +2908,20 @@ UniValue zc_raw_keygen(const UniValue& params, bool fHelp)
auto k = SpendingKey::random(); auto k = SpendingKey::random();
auto addr = k.address(); auto addr = k.address();
auto viewing_key = k.viewing_key(); auto receiving_key = k.receiving_key();
CDataStream viewing(SER_NETWORK, PROTOCOL_VERSION); CDataStream receiving(SER_NETWORK, PROTOCOL_VERSION);
viewing << viewing_key; receiving << receiving_key;
CZCPaymentAddress pubaddr(addr); CZCPaymentAddress pubaddr(addr);
CZCSpendingKey spendingkey(k); CZCSpendingKey spendingkey(k);
std::string viewing_hex = HexStr(viewing.begin(), viewing.end()); std::string receiving_hex = HexStr(receiving.begin(), receiving.end());
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
result.push_back(Pair("zcaddress", pubaddr.ToString())); result.push_back(Pair("zcaddress", pubaddr.ToString()));
result.push_back(Pair("zcsecretkey", spendingkey.ToString())); result.push_back(Pair("zcsecretkey", spendingkey.ToString()));
result.push_back(Pair("zcviewingkey", viewing_hex)); result.push_back(Pair("zcviewingkey", receiving_hex));
return result; return result;
} }

View File

@@ -190,10 +190,10 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
bool CWallet::AddCryptedSpendingKey(const libzcash::PaymentAddress &address, bool CWallet::AddCryptedSpendingKey(const libzcash::PaymentAddress &address,
const libzcash::ViewingKey &vk, const libzcash::ReceivingKey &rk,
const std::vector<unsigned char> &vchCryptedSecret) const std::vector<unsigned char> &vchCryptedSecret)
{ {
if (!CCryptoKeyStore::AddCryptedSpendingKey(address, vk, vchCryptedSecret)) if (!CCryptoKeyStore::AddCryptedSpendingKey(address, rk, vchCryptedSecret))
return false; return false;
if (!fFileBacked) if (!fFileBacked)
return true; return true;
@@ -201,12 +201,12 @@ bool CWallet::AddCryptedSpendingKey(const libzcash::PaymentAddress &address,
LOCK(cs_wallet); LOCK(cs_wallet);
if (pwalletdbEncryption) { if (pwalletdbEncryption) {
return pwalletdbEncryption->WriteCryptedZKey(address, return pwalletdbEncryption->WriteCryptedZKey(address,
vk, rk,
vchCryptedSecret, vchCryptedSecret,
mapZKeyMetadata[address]); mapZKeyMetadata[address]);
} else { } else {
return CWalletDB(strWalletFile).WriteCryptedZKey(address, return CWalletDB(strWalletFile).WriteCryptedZKey(address,
vk, rk,
vchCryptedSecret, vchCryptedSecret,
mapZKeyMetadata[address]); mapZKeyMetadata[address]);
} }
@@ -236,9 +236,9 @@ bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigne
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
} }
bool CWallet::LoadCryptedZKey(const libzcash::PaymentAddress &addr, const libzcash::ViewingKey &vk, const std::vector<unsigned char> &vchCryptedSecret) bool CWallet::LoadCryptedZKey(const libzcash::PaymentAddress &addr, const libzcash::ReceivingKey &rk, const std::vector<unsigned char> &vchCryptedSecret)
{ {
return CCryptoKeyStore::AddCryptedSpendingKey(addr, vk, vchCryptedSecret); return CCryptoKeyStore::AddCryptedSpendingKey(addr, rk, vchCryptedSecret);
} }
bool CWallet::LoadZKey(const libzcash::SpendingKey &key) bool CWallet::LoadZKey(const libzcash::SpendingKey &key)

View File

@@ -950,9 +950,9 @@ public:
//! Load spending key metadata (used by LoadWallet) //! Load spending key metadata (used by LoadWallet)
bool LoadZKeyMetadata(const libzcash::PaymentAddress &addr, const CKeyMetadata &meta); bool LoadZKeyMetadata(const libzcash::PaymentAddress &addr, const CKeyMetadata &meta);
//! Adds an encrypted spending key to the store, without saving it to disk (used by LoadWallet) //! Adds an encrypted spending key to the store, without saving it to disk (used by LoadWallet)
bool LoadCryptedZKey(const libzcash::PaymentAddress &addr, const libzcash::ViewingKey &vk, const std::vector<unsigned char> &vchCryptedSecret); bool LoadCryptedZKey(const libzcash::PaymentAddress &addr, const libzcash::ReceivingKey &rk, const std::vector<unsigned char> &vchCryptedSecret);
//! Adds an encrypted spending key to the store, and saves it to disk (virtual method, declared in crypter.h) //! Adds an encrypted spending key to the store, and saves it to disk (virtual method, declared in crypter.h)
bool AddCryptedSpendingKey(const libzcash::PaymentAddress &address, const libzcash::ViewingKey &vk, const std::vector<unsigned char> &vchCryptedSecret); bool AddCryptedSpendingKey(const libzcash::PaymentAddress &address, const libzcash::ReceivingKey &rk, const std::vector<unsigned char> &vchCryptedSecret);
/** /**
* Increment the next transaction order id * Increment the next transaction order id

View File

@@ -106,7 +106,7 @@ bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey,
} }
bool CWalletDB::WriteCryptedZKey(const libzcash::PaymentAddress & addr, bool CWalletDB::WriteCryptedZKey(const libzcash::PaymentAddress & addr,
const libzcash::ViewingKey &vk, const libzcash::ReceivingKey &rk,
const std::vector<unsigned char>& vchCryptedSecret, const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata &keyMeta) const CKeyMetadata &keyMeta)
{ {
@@ -116,7 +116,7 @@ bool CWalletDB::WriteCryptedZKey(const libzcash::PaymentAddress & addr,
if (!Write(std::make_pair(std::string("zkeymeta"), addr), keyMeta)) if (!Write(std::make_pair(std::string("zkeymeta"), addr), keyMeta))
return false; return false;
if (!Write(std::make_pair(std::string("czkey"), addr), std::make_pair(vk, vchCryptedSecret), false)) if (!Write(std::make_pair(std::string("czkey"), addr), std::make_pair(rk, vchCryptedSecret), false))
return false; return false;
if (fEraseUnencryptedKey) if (fEraseUnencryptedKey)
{ {
@@ -585,14 +585,14 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
libzcash::PaymentAddress addr; libzcash::PaymentAddress addr;
ssKey >> addr; ssKey >> addr;
// Deserialization of a pair is just one item after another // Deserialization of a pair is just one item after another
uint256 vkValue; uint256 rkValue;
ssValue >> vkValue; ssValue >> rkValue;
libzcash::ViewingKey vk(vkValue); libzcash::ReceivingKey rk(rkValue);
vector<unsigned char> vchCryptedSecret; vector<unsigned char> vchCryptedSecret;
ssValue >> vchCryptedSecret; ssValue >> vchCryptedSecret;
wss.nCKeys++; wss.nCKeys++;
if (!pwallet->LoadCryptedZKey(addr, vk, vchCryptedSecret)) if (!pwallet->LoadCryptedZKey(addr, rk, vchCryptedSecret))
{ {
strErr = "Error reading wallet database: LoadCryptedZKey failed"; strErr = "Error reading wallet database: LoadCryptedZKey failed";
return false; return false;

View File

@@ -136,7 +136,7 @@ public:
/// Write spending key to wallet database, where key is payment address and value is spending key. /// Write spending key to wallet database, where key is payment address and value is spending key.
bool WriteZKey(const libzcash::PaymentAddress& addr, const libzcash::SpendingKey& key, const CKeyMetadata &keyMeta); bool WriteZKey(const libzcash::PaymentAddress& addr, const libzcash::SpendingKey& key, const CKeyMetadata &keyMeta);
bool WriteCryptedZKey(const libzcash::PaymentAddress & addr, bool WriteCryptedZKey(const libzcash::PaymentAddress & addr,
const libzcash::ViewingKey & vk, const libzcash::ReceivingKey & rk,
const std::vector<unsigned char>& vchCryptedSecret, const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata &keyMeta); const CKeyMetadata &keyMeta);

View File

@@ -12,12 +12,12 @@ uint256 PaymentAddress::GetHash() const {
return Hash(ss.begin(), ss.end()); return Hash(ss.begin(), ss.end());
} }
uint256 ViewingKey::pk_enc() { uint256 ReceivingKey::pk_enc() {
return ZCNoteEncryption::generate_pubkey(*this); return ZCNoteEncryption::generate_pubkey(*this);
} }
ViewingKey SpendingKey::viewing_key() const { ReceivingKey SpendingKey::receiving_key() const {
return ViewingKey(ZCNoteEncryption::generate_privkey(*this)); return ReceivingKey(ZCNoteEncryption::generate_privkey(*this));
} }
SpendingKey SpendingKey::random() { SpendingKey SpendingKey::random() {
@@ -25,7 +25,7 @@ SpendingKey SpendingKey::random() {
} }
PaymentAddress SpendingKey::address() const { PaymentAddress SpendingKey::address() const {
return PaymentAddress(PRF_addr_a_pk(*this), viewing_key().pk_enc()); return PaymentAddress(PRF_addr_a_pk(*this), receiving_key().pk_enc());
} }
} }

View File

@@ -38,9 +38,9 @@ public:
} }
}; };
class ViewingKey : public uint256 { class ReceivingKey : public uint256 {
public: public:
ViewingKey(uint256 sk_enc) : uint256(sk_enc) { } ReceivingKey(uint256 sk_enc) : uint256(sk_enc) { }
uint256 pk_enc(); uint256 pk_enc();
}; };
@@ -52,7 +52,7 @@ public:
static SpendingKey random(); static SpendingKey random();
ViewingKey viewing_key() const; ReceivingKey receiving_key() const;
PaymentAddress address() const; PaymentAddress address() const;
}; };