X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fwallet.cpp;h=5c46a1b55670e4b560a742e2db903591c4eda7f6;hb=7f70ddc68f4afa4a87a15e620ba519afbc5c8b15;hp=97648c88cad84afb5221ec2ca40f43114fc1111b;hpb=a348ee6f5081803ad1ae860c29075b08d772dd08;p=novacoin.git diff --git a/src/wallet.cpp b/src/wallet.cpp index 97648c8..5c46a1b 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -57,6 +57,28 @@ CPubKey CWallet::GenerateNewKey() return key.GetPubKey(); } +CMalleableKeyView CWallet::GenerateNewMalleableKey() +{ + RandAddSeedPerfmon(); + + // Compressed public keys were introduced in version 0.6.0 + SetMinVersion(FEATURE_MALLKEY); + + CMalleableKey mKey; + mKey.MakeNewKeys(); + const CMalleableKeyView &keyView(mKey); + + // Create new metadata + int64_t nCreationTime = GetTime(); + mapMalleableKeyMetadata[keyView] = CKeyMetadata(nCreationTime); + if (!nTimeFirstKey || nCreationTime < nTimeFirstKey) + nTimeFirstKey = nCreationTime; + + if (!AddMalleableKey(mKey)) + throw std::runtime_error("CWallet::GenerateNewMalleableKey() : AddMalleableKey failed"); + return CMalleableKeyView(mKey); +} + bool CWallet::AddKey(const CKey& key) { CPubKey pubkey = key.GetPubKey(); @@ -69,6 +91,18 @@ bool CWallet::AddKey(const CKey& key) return true; } +bool CWallet::AddMalleableKey(const CMalleableKey& mKey) +{ + CMalleableKeyView keyView = CMalleableKeyView(mKey); + if (!CCryptoKeyStore::AddMalleableKey(mKey)) + return false; + if (!fFileBacked) + return true; + if (!IsCrypted()) + return CWalletDB(strWalletFile).WriteMalleableKey(keyView, mKey, mapMalleableKeyMetadata[keyView]); + return true; +} + bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector &vchCryptedSecret) { if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret)) @@ -101,6 +135,15 @@ bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta) return true; } +bool CWallet::LoadMalleableKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetadata &metadata) +{ + if (metadata.nCreateTime && (!nTimeFirstKey || metadata.nCreateTime < nTimeFirstKey)) + nTimeFirstKey = metadata.nCreateTime; + + mapMalleableKeyMetadata[keyView] = metadata; + return true; +} + bool CWallet::AddCScript(const CScript& redeemScript) { if (!CCryptoKeyStore::AddCScript(redeemScript)) @@ -204,11 +247,13 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, { int64_t nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod); - pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime))); + double nFirstMultiplier = 1e2 / (GetTimeMillis() - nStartTime); + pMasterKey.second.nDeriveIterations = (uint32_t)(pMasterKey.second.nDeriveIterations *nFirstMultiplier); nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod); - pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2; + double nSecondMultiplier = 1e2 / (GetTimeMillis() - nStartTime); + pMasterKey.second.nDeriveIterations = (uint32_t)((pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * nSecondMultiplier) / 2); if (pMasterKey.second.nDeriveIterations < 25000) pMasterKey.second.nDeriveIterations = 25000; @@ -305,11 +350,13 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) CCrypter crypter; int64_t nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod); - kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime)); + int64_t nDivider = GetTimeMillis() - nStartTime; + kMasterKey.nDeriveIterations = (uint32_t)(25e5 / (double)(nDivider)); nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod); - kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2; + double nMultiplier = 1e2 / (GetTimeMillis() - nStartTime); + kMasterKey.nDeriveIterations = (uint32_t)((kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * nMultiplier) / 2); if (kMasterKey.nDeriveIterations < 25000) kMasterKey.nDeriveIterations = 25000; @@ -664,7 +711,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl uint256 hash = tx.GetHash(); { LOCK(cs_wallet); - bool fExisted = mapWallet.count(hash); + bool fExisted = mapWallet.count(hash) != 0; if (fExisted && !fUpdate) return false; if (fExisted || IsMine(tx) || IsFromMe(tx)) { @@ -1079,7 +1126,7 @@ void CWallet::ResendWalletTransactions() { // Do this infrequently and randomly to avoid giving away // that these are our transactions. - static int64_t nNextTime; + static int64_t nNextTime = GetRand(GetTime() + 30 * 60); if (GetTime() < nNextTime) return; bool fFirst = (nNextTime == 0); @@ -1088,7 +1135,7 @@ void CWallet::ResendWalletTransactions() return; // Only do it if there's been a new block since last time - static int64_t nLastTime; + static int64_t nLastTime = 0; if (nTimeBestReceived < nLastTime) return; nLastTime = GetTime(); @@ -1684,7 +1731,7 @@ bool CWallet::CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& void CWallet::GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight) { - int64_t nTimeWeight = GetWeight(nTime, (int64_t)GetTime()); + int64_t nTimeWeight = GetWeight(nTime, GetTime()); // If time weight is lower or equal to zero then weight is zero. if (nTimeWeight <= 0) @@ -1693,7 +1740,7 @@ void CWallet::GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValu return; } - CBigNum bnCoinDayWeight = CBigNum(nValue) * nTimeWeight / COIN / (24 * 60 * 60); + CBigNum bnCoinDayWeight = CBigNum(nValue) * nTimeWeight / COIN / nOneDay; nWeight = bnCoinDayWeight.getuint64(); } @@ -2161,7 +2208,7 @@ bool CWallet::SetAddressBookName(const CTxDestination& address, const string& st { std::map::iterator mi = mapAddressBook.find(address); mapAddressBook[address] = strName; - NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED); + NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != MINE_NO, (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED); if (!fFileBacked) return false; return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName); @@ -2170,7 +2217,7 @@ bool CWallet::SetAddressBookName(const CTxDestination& address, const string& st bool CWallet::DelAddressBookName(const CTxDestination& address) { mapAddressBook.erase(address); - NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), CT_DELETED); + NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address) != MINE_NO, CT_DELETED); if (!fFileBacked) return false; return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString()); @@ -2540,7 +2587,7 @@ void CWallet::FixSpentCoins(int& nMismatchFound, int64_t& nBalanceInQuestion, bo { if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull())) { - printf("FixSpentCoins found lost coin %sppc %s[%d], %s\n", + printf("FixSpentCoins found lost coin %sppc %s[%u], %s\n", FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing"); nMismatchFound++; nBalanceInQuestion += pcoin->vout[n].nValue; @@ -2552,7 +2599,7 @@ void CWallet::FixSpentCoins(int& nMismatchFound, int64_t& nBalanceInQuestion, bo } else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull())) { - printf("FixSpentCoins found spent coin %sppc %s[%d], %s\n", + printf("FixSpentCoins found spent coin %sppc %s[%u], %s\n", FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing"); nMismatchFound++; nBalanceInQuestion += pcoin->vout[n].nValue;