Add Sapling Add/Have/Get to keystore

This commit is contained in:
Jay Graber
2018-07-05 14:37:33 -07:00
parent cea065e3d4
commit efb7662d4a
5 changed files with 156 additions and 10 deletions

View File

@@ -100,8 +100,64 @@ libzcash::PaymentAddress CWallet::GenerateNewZKey()
return addr;
}
//! TODO: Should be Sapling address format, SaplingPaymentAddress
// Generate a new Sapling spending key and return its public payment address
SaplingPaymentAddress CWallet::GenerateNewSaplingZKey()
{
AssertLockHeld(cs_wallet); // mapZKeyMetadata
auto sk = SaplingSpendingKey::random();
auto fvk = sk.full_viewing_key();
auto addrOpt = sk.default_address();
if (addrOpt){
auto addr = addrOpt.value();
// Check for collision, even though it is unlikely to ever occur
if (CCryptoKeyStore::HaveSaplingSpendingKey(fvk))
throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): Collision detected");
// Create new metadata
int64_t nCreationTime = GetTime();
mapSaplingZKeyMetadata[addr] = CKeyMetadata(nCreationTime);
if (!AddSaplingZKey(sk)) {
throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): AddSaplingZKey failed");
}
// return default sapling payment address.
return addr;
} else {
throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): default_address() did not return address");
}
}
// Add spending key to keystore
// TODO: persist to disk
bool CWallet::AddSaplingZKey(const libzcash::SaplingSpendingKey &sk)
{
AssertLockHeld(cs_wallet); // mapSaplingZKeyMetadata
auto addr = sk.default_address();
if (!CCryptoKeyStore::AddSaplingSpendingKey(sk)) {
return false;
}
// // check if we need to remove from viewing keys
// if (HaveViewingKey(addr)) {
// RemoveViewingKey(key.viewing_key());
// }
// if (!fFileBacked) {
// return true;
// }
// if (!IsCrypted()) {
// return CWalletDB(strWalletFile).WriteSaplingZKey(addr,
// sk,
// mapSaplingZKeyMetadata[addr]);
// }
return true;
}
// Add spending key to keystore and persist to disk
// TODO: Add Sapling support
bool CWallet::AddZKey(const libzcash::SproutSpendingKey &key)
{
AssertLockHeld(cs_wallet); // mapZKeyMetadata
@@ -176,7 +232,7 @@ bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
const vector<unsigned char> &vchCryptedSecret)
{
if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
return false;
if (!fFileBacked)
@@ -516,7 +572,7 @@ bool CWallet::Verify(const string& walletFile, string& warningString, string& er
} catch (const boost::filesystem::filesystem_error&) {
// failure is ok (well, not really, but it's not worse than what we started with)
}
// try again
if (!bitdb.Open(GetDataDir())) {
// if it still fails, it probably means we can't even create the database env
@@ -525,14 +581,14 @@ bool CWallet::Verify(const string& walletFile, string& warningString, string& er
return true;
}
}
if (GetBoolArg("-salvagewallet", false))
{
// Recover readable keypairs:
if (!CWalletDB::Recover(bitdb, walletFile, true))
return false;
}
if (boost::filesystem::exists(GetDataDir() / walletFile))
{
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover);
@@ -546,7 +602,7 @@ bool CWallet::Verify(const string& walletFile, string& warningString, string& er
if (r == CDBEnv::RECOVER_FAIL)
errorString += _("wallet.dat corrupt, salvage failed");
}
return true;
}
@@ -2523,7 +2579,7 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount &nFeeRet, int& nC
CReserveKey reservekey(this);
CWalletTx wtx;
if (!CreateTransaction(vecSend, wtx, reservekey, nFeeRet, nChangePosRet, strFailReason, &coinControl, false))
return false;
@@ -2590,7 +2646,7 @@ bool CWallet::CreateTransaction(const vector<CRecipient>& vecSend, CWalletTx& wt
if (!NetworkUpgradeActive(nextBlockHeight, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) {
max_tx_size = MAX_TX_SIZE_BEFORE_SAPLING;
}
// Discourage fee sniping.
//
// However because of a off-by-one-error in previous versions we need to
@@ -3062,7 +3118,7 @@ bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
/**
* Mark old keypool keys as used,
* and generate all new keys
* and generate all new keys
*/
bool CWallet::NewKeyPool()
{
@@ -3767,7 +3823,7 @@ void CWallet::GetFilteredNotes(
if (ignoreUnspendable && !HaveSpendingKey(pa)) {
continue;
}
// skip locked notes
if (IsLockedNote(jsop)) {
continue;