PIVX Core  5.6.99
P2P Digital Currency
crypter_sapling.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2020 The ZCash developers
2 // Copyright (c) 2020-2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
5 
6 #include "crypter.h"
7 
8 #include "script/script.h"
9 #include "script/standard.h"
10 #include "util/system.h"
11 #include "uint256.h"
12 
15  const std::vector<unsigned char> &vchCryptedSecret)
16 {
18  if (!SetCrypted()) {
19  return false;
20  }
21 
22  // if extfvk is not in SaplingFullViewingKeyMap, add it
23  if (!AddSaplingFullViewingKey(extfvk)) {
24  return false;
25  }
26 
27  mapCryptedSaplingSpendingKeys[extfvk] = vchCryptedSecret;
28  return true;
29 }
30 
31 static bool DecryptSaplingSpendingKey(const CKeyingMaterial& vMasterKey,
32  const std::vector<unsigned char>& vchCryptedSecret,
35 {
36  CKeyingMaterial vchSecret;
37  if (!DecryptSecret(vMasterKey, vchCryptedSecret, extfvk.fvk.GetFingerprint(), vchSecret))
38  return false;
39 
40  if (vchSecret.size() != ZIP32_XSK_SIZE)
41  return false;
42 
43  CSecureDataStream ss(vchSecret, SER_NETWORK, PROTOCOL_VERSION);
44  ss >> sk;
45  return sk.expsk.full_viewing_key() == extfvk.fvk;
46 }
47 
51 {
53  if (!IsCrypted()) {
54  return CBasicKeyStore::GetSaplingSpendingKey(extfvk, skOut);
55  }
56 
57  auto it = mapCryptedSaplingSpendingKeys.find(extfvk);
58  if (it == mapCryptedSaplingSpendingKeys.end()) return false;
59  const std::vector<unsigned char>& vchCryptedSecret = it->second;
60  return DecryptSaplingSpendingKey(vMasterKey, vchCryptedSecret, it->first, skOut);
61 }
62 
64 {
66  if (!IsCrypted()) {
68  }
69  return mapCryptedSaplingSpendingKeys.count(extfvk) > 0;
70 }
71 
72 bool CCryptoKeyStore::UnlockSaplingKeys(const CKeyingMaterial& vMasterKeyIn, bool fDecryptionThoroughlyChecked)
73 {
74  if (mapCryptedSaplingSpendingKeys.empty()) {
75  LogPrintf("%s: mapCryptedSaplingSpendingKeys empty. No need to unlock anything.\n", __func__);
76  return true;
77  }
78 
79  bool keyFail = false;
80  bool keyPass = false;
81  CryptedSaplingSpendingKeyMap::const_iterator miSapling = mapCryptedSaplingSpendingKeys.begin();
82  for (; miSapling != mapCryptedSaplingSpendingKeys.end(); ++miSapling) {
83  const libzcash::SaplingExtendedFullViewingKey &extfvk = (*miSapling).first;
84  const std::vector<unsigned char> &vchCryptedSecret = (*miSapling).second;
86  if (!DecryptSaplingSpendingKey(vMasterKeyIn, vchCryptedSecret, extfvk, sk)) {
87  keyFail = true;
88  break;
89  }
90  keyPass = true;
91  if (fDecryptionThoroughlyChecked)
92  break;
93  }
94 
95  if (keyPass && keyFail) {
96  LogPrintf("Sapling wallet is probably corrupted: Some keys decrypt but not all.\n");
97  throw std::runtime_error("Error unlocking sapling wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
98  }
99  if (keyFail || !keyPass)
100  return false;
101 
102  return true;
103 }
bool HaveSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk) const
Definition: keystore.cpp:189
bool GetSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, libzcash::SaplingExtendedSpendingKey &skOut) const
Definition: keystore.cpp:204
virtual bool AddSaplingFullViewingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk)
Support for Sapling full viewing keys.
Definition: keystore.cpp:164
CryptedSaplingSpendingKeyMap mapCryptedSaplingSpendingKeys
Definition: crypter.h:137
bool HaveSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk) const override
virtual bool AddCryptedSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, const std::vector< unsigned char > &vchCryptedSecret)
Sapling.
bool UnlockSaplingKeys(const CKeyingMaterial &vMasterKeyIn, bool fDecryptionThoroughlyChecked)
bool GetSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, libzcash::SaplingExtendedSpendingKey &skOut) const override
CKeyingMaterial vMasterKey
Definition: crypter.h:135
bool IsCrypted() const
Definition: crypter.h:154
bool SetCrypted()
Definition: crypter.cpp:137
RecursiveMutex cs_KeyStore
Definition: keystore.h:26
SaplingFullViewingKey full_viewing_key() const
Definition: address.cpp:29
uint256 GetFingerprint() const
Get the fingerprint of this full viewing key (as defined in ZIP 32).
Definition: address.cpp:57
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:114
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:63
@ LOCK
Definition: lockunlock.h:16
@ SER_NETWORK
Definition: serialize.h:174
libzcash::SaplingFullViewingKey fvk
Definition: zip32.h:56
libzcash::SaplingExpandedSpendingKey expsk
Definition: zip32.h:92
const size_t ZIP32_XSK_SIZE
Definition: zip32.h:18