Browse Source

Add crypted keystore sapling add key

pull/4/head
Jay Graber 6 years ago
parent
commit
2173767365
  1. 3
      src/keystore.h
  2. 2
      src/test/key_tests.cpp
  3. 44
      src/wallet/crypter.cpp
  4. 8
      src/wallet/crypter.h
  5. 23
      src/wallet/gtest/test_wallet_zkeys.cpp
  6. 6
      src/zcash/Address.cpp
  7. 3
      src/zcash/Address.hpp

3
src/keystore.h

@ -262,4 +262,7 @@ typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMate
typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
typedef std::map<libzcash::SproutPaymentAddress, std::vector<unsigned char> > CryptedSpendingKeyMap;
//! Sapling
typedef std::map<libzcash::SaplingFullViewingKey, std::vector<unsigned char> > CryptedSaplingSpendingKeyMap;
#endif // BITCOIN_KEYSTORE_H

2
src/test/key_tests.cpp

@ -239,7 +239,7 @@ BOOST_AUTO_TEST_CASE(zs_address_test)
{
auto addr = sk.default_address();
std::string addr_string = EncodePaymentAddress(*addr);
std::string addr_string = EncodePaymentAddress(addr);
BOOST_CHECK(addr_string.compare(0, 2, Params().Bech32HRP(CChainParams::SAPLING_PAYMENT_ADDRESS)) == 0);
auto paymentaddr2 = DecodePaymentAddress(addr_string);

44
src/wallet/crypter.cpp

@ -322,6 +322,35 @@ bool CCryptoKeyStore::AddSpendingKey(const libzcash::SproutSpendingKey &sk)
return true;
}
bool CCryptoKeyStore::AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk)
{
{
LOCK(cs_SpendingKeyStore);
if (!IsCrypted()) {
return CBasicKeyStore::AddSaplingSpendingKey(sk);
}
if (IsLocked()) {
return false;
}
std::vector<unsigned char> vchCryptedSecret;
CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << sk;
CKeyingMaterial vchSecret(ss.begin(), ss.end());
auto address = sk.default_address();
auto fvk = sk.full_viewing_key();
if (!EncryptSecret(vMasterKey, vchSecret, address.GetHash(), vchCryptedSecret)) {
return false;
}
if (!AddCryptedSaplingSpendingKey(fvk, vchCryptedSecret)) {
return false;
}
}
return true;
}
bool CCryptoKeyStore::AddCryptedSpendingKey(const libzcash::SproutPaymentAddress &address,
const libzcash::ReceivingKey &rk,
const std::vector<unsigned char> &vchCryptedSecret)
@ -337,6 +366,21 @@ bool CCryptoKeyStore::AddCryptedSpendingKey(const libzcash::SproutPaymentAddress
return true;
}
// TODO: Handle note decryptors
bool CCryptoKeyStore::AddCryptedSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk,
const std::vector<unsigned char> &vchCryptedSecret)
{
{
LOCK(cs_SpendingKeyStore);
if (!SetCrypted()) {
return false;
}
mapCryptedSaplingSpendingKeys[fvk] = vchCryptedSecret;
}
return true;
}
bool CCryptoKeyStore::GetSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const
{
{

8
src/wallet/crypter.h

@ -129,7 +129,9 @@ class CCryptoKeyStore : public CBasicKeyStore
private:
CryptedKeyMap mapCryptedKeys;
CryptedSpendingKeyMap mapCryptedSpendingKeys;
CryptedSaplingSpendingKeyMap mapCryptedSaplingSpendingKeys;
CKeyingMaterial vMasterKey;
//! if fUseCrypto is true, mapKeys and mapSpendingKeys must be empty
@ -230,6 +232,10 @@ public:
mi++;
}
}
//! Sapling
virtual bool AddCryptedSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk,
const std::vector<unsigned char> &vchCryptedSecret);
bool AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk);
/**
* Wallet status (encrypted, locked) changed.

23
src/wallet/gtest/test_wallet_zkeys.cpp

@ -7,6 +7,29 @@
#include <boost/filesystem.hpp>
/**
* This test covers Sapling methods on CWallet
* GenerateNewSaplingZKey()
*/
TEST(wallet_zkeys_tests, store_and_load_sapling_zkeys) {
SelectParams(CBaseChainParams::MAIN);
CWallet wallet;
auto address = wallet.GenerateNewSaplingZKey();
// verify wallet has incoming viewing key for the address
ASSERT_TRUE(wallet.HaveSaplingIncomingViewingKey(address));
// manually add new spending key to wallet
auto sk = libzcash::SaplingSpendingKey::random();
ASSERT_TRUE(wallet.AddSaplingZKey(sk));
// verify wallet did add it
auto fvk = sk.full_viewing_key();
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
}
/**
* This test covers methods on CWallet
* GenerateNewZKey()

6
src/zcash/Address.cpp

@ -39,6 +39,12 @@ SproutPaymentAddress SproutSpendingKey::address() const {
}
//! Sapling
uint256 SaplingPaymentAddress::GetHash() const {
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << *this;
return Hash(ss.begin(), ss.end());
}
SaplingFullViewingKey SaplingExpandedSpendingKey::full_viewing_key() const {
uint256 ak;
uint256 nk;

3
src/zcash/Address.hpp

@ -111,6 +111,9 @@ public:
READWRITE(d);
READWRITE(pk_d);
}
//! Get the 256-bit SHA256d hash of this payment address.
uint256 GetHash() const;
friend inline bool operator==(const SaplingPaymentAddress& a, const SaplingPaymentAddress& b) {
return a.d == b.d && a.pk_d == b.pk_d;

Loading…
Cancel
Save