Skip to content

Commit f6cb741

Browse files
committed
[Sapling] Add crypted keystore sapling add key.
1 parent 859d31e commit f6cb741

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

src/crypter.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,48 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) co
213213
return false;
214214
}
215215

216+
bool CCryptoKeyStore::AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk)
217+
{
218+
{
219+
LOCK(cs_SpendingKeyStore);
220+
if (!IsCrypted()) {
221+
return CBasicKeyStore::AddSaplingSpendingKey(sk);
222+
}
223+
224+
if (IsLocked()) {
225+
return false;
226+
}
227+
228+
std::vector<unsigned char> vchCryptedSecret;
229+
CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
230+
ss << sk;
231+
CKeyingMaterial vchSecret(ss.begin(), ss.end());
232+
auto address = sk.default_address();
233+
auto fvk = sk.full_viewing_key();
234+
if (!EncryptSecret(vMasterKey, vchSecret, address.GetHash(), vchCryptedSecret)) {
235+
return false;
236+
}
237+
238+
if (!AddCryptedSaplingSpendingKey(fvk, vchCryptedSecret)) {
239+
return false;
240+
}
241+
}
242+
return true;
243+
}
244+
245+
// TODO: Handle note decryptors
246+
bool CCryptoKeyStore::AddCryptedSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk,
247+
const std::vector<unsigned char> &vchCryptedSecret)
248+
{
249+
LOCK(cs_SpendingKeyStore);
250+
if (!SetCrypted()) {
251+
return false;
252+
}
253+
254+
mapCryptedSaplingSpendingKeys[fvk] = vchCryptedSecret;
255+
return true;
256+
}
257+
216258
bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
217259
{
218260
{

src/crypter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class CCryptoKeyStore : public CBasicKeyStore
141141
protected:
142142
// TODO: In the future, move this variable to the wallet class directly following upstream's structure.
143143
CKeyingMaterial vMasterKey;
144+
// Sapling
145+
CryptedSaplingSpendingKeyMap mapCryptedSaplingSpendingKeys;
144146

145147
bool SetCrypted();
146148

@@ -198,6 +200,11 @@ class CCryptoKeyStore : public CBasicKeyStore
198200
}
199201
}
200202

203+
//! Sapling
204+
virtual bool AddCryptedSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk,
205+
const std::vector<unsigned char> &vchCryptedSecret);
206+
bool AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk);
207+
201208
/**
202209
* Wallet status (encrypted, locked) changed.
203210
* Note: Called without locks held.

src/keystore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,7 @@ class CBasicKeyStore : public CKeyStore
134134

135135
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
136136
typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
137+
//! Sapling
138+
typedef std::map<libzcash::SaplingFullViewingKey, std::vector<unsigned char> > CryptedSaplingSpendingKeyMap;
137139

138140
#endif // BITCOIN_KEYSTORE_H

src/test/librust/wallet_zkeys_tests.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,26 @@ BOOST_FIXTURE_TEST_SUITE(wallet_zkeys_tests, BasicTestingSetup)
2626
* This test covers Sapling methods on CWallet
2727
* GenerateNewSaplingZKey()
2828
*/
29-
BOOST_AUTO_TEST_CASE(testVectors) {
29+
BOOST_AUTO_TEST_CASE(store_and_load_sapling_zkeys) {
3030

3131
CWallet wallet;
32-
// wallet should be empty
33-
// std::set<libzcash::SaplingPaymentAddress> addrs;
34-
// wallet.GetSaplingPaymentAddresses(addrs);
35-
// ASSERT_EQ(0, addrs.size());
36-
37-
// wallet should have one key
38-
auto saplingAddr = wallet.GenerateNewSaplingZKey();
39-
// ASSERT_NE(boost::get<libzcash::SaplingPaymentAddress>(&address), nullptr);
40-
// auto sapling_addr = boost::get<libzcash::SaplingPaymentAddress>(saplingAddr);
41-
// wallet.GetSaplingPaymentAddresses(addrs);
42-
// ASSERT_EQ(1, addrs.size());
4332

33+
auto address = wallet.GenerateNewSaplingZKey();
34+
35+
// verify wallet has incoming viewing key for the address
36+
BOOST_CHECK(wallet.HaveSaplingIncomingViewingKey(address));
37+
38+
// manually add new spending key to wallet
4439
auto sk = libzcash::SaplingSpendingKey::random();
45-
auto full_viewing_key = sk.full_viewing_key();
46-
BOOST_CHECK(wallet.AddSaplingSpendingKey(sk));
40+
BOOST_CHECK(wallet.AddSaplingZKey(sk));
4741

48-
// verify wallet has spending key for the address
49-
BOOST_CHECK(wallet.HaveSaplingSpendingKey(full_viewing_key));
42+
// verify wallet did add it
43+
auto fvk = sk.full_viewing_key();
44+
BOOST_CHECK(wallet.HaveSaplingSpendingKey(fvk));
5045

5146
// check key is the same
5247
libzcash::SaplingSpendingKey keyOut;
53-
wallet.GetSaplingSpendingKey(full_viewing_key, keyOut);
48+
wallet.GetSaplingSpendingKey(fvk, keyOut);
5449
BOOST_CHECK(sk == keyOut);
5550
}
5651

0 commit comments

Comments
 (0)