Skip to content

Commit ada3bb7

Browse files
committed
[Sapling] Add Sapling decryption check to CCryptoKeyStore::Unlock().
Coming from 0adba6789b1b36387ee06248273540fc30dd9d40 .
1 parent c52a018 commit ada3bb7

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/crypter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ class CCryptoKeyStore : public CBasicKeyStore
154154

155155
CryptedKeyMap mapCryptedKeys;
156156

157+
// Unlock Sapling keys
158+
bool UnlockSaplingKeys(const CKeyingMaterial& vMasterKeyIn, bool fDecryptionThoroughlyChecked);
159+
157160
public:
158161
CCryptoKeyStore() : fUseCrypto(false) { }
159162

src/sapling/crypter_sapling.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,33 @@ bool CCryptoKeyStore::EncryptSaplingKeys(CKeyingMaterial& vMasterKeyIn)
119119
}
120120
mapSaplingSpendingKeys.clear();
121121
return true;
122+
}
123+
124+
bool CCryptoKeyStore::UnlockSaplingKeys(const CKeyingMaterial& vMasterKeyIn, bool fDecryptionThoroughlyChecked)
125+
{
126+
bool keyFail = false;
127+
bool keyPass = false;
128+
CryptedSaplingSpendingKeyMap::const_iterator miSapling = mapCryptedSaplingSpendingKeys.begin();
129+
for (; miSapling != mapCryptedSaplingSpendingKeys.end(); ++miSapling) {
130+
const libzcash::SaplingFullViewingKey &fvk = (*miSapling).first;
131+
const std::vector<unsigned char> &vchCryptedSecret = (*miSapling).second;
132+
libzcash::SaplingSpendingKey sk;
133+
if (!DecryptSaplingSpendingKey(vMasterKeyIn, vchCryptedSecret, fvk, sk))
134+
{
135+
keyFail = true;
136+
break;
137+
}
138+
keyPass = true;
139+
if (fDecryptionThoroughlyChecked)
140+
break;
141+
}
142+
143+
if (keyPass && keyFail) {
144+
LogPrintf("Sapling wallet is probably corrupted: Some keys decrypt but not all.");
145+
throw std::runtime_error("Error unlocking sapling wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
146+
}
147+
if (keyFail || !keyPass)
148+
return false;
149+
150+
return true;
122151
}

src/wallet/wallet.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ bool CWallet::Unlock(const CKeyingMaterial& vMasterKeyIn)
338338
if (fDecryptionThoroughlyChecked)
339339
break;
340340
}
341+
342+
// Sapling
343+
if (!UnlockSaplingKeys(vMasterKeyIn, fDecryptionThoroughlyChecked)) {
344+
return false; // only could get here if (keyFail || !keyPass).
345+
}
346+
341347
if (keyPass && keyFail) {
342348
LogPrintf("The wallet is probably corrupted: Some keys decrypt but not all.");
343349
throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");

0 commit comments

Comments
 (0)