diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2013-11-18 16:55:54 +0100 |
---|---|---|
committer | Cozz Lovan <cozzlovan@yahoo.com> | 2014-01-19 18:21:54 +0100 |
commit | b10e147096b0e27fdff8c22029bc8b7a1a14f042 (patch) | |
tree | deb52bf797444b448e79df495d4a98906407af44 | |
parent | dd7c1cf534b760e9da55c225d6bb05f2bdb8e2e7 (diff) |
wallet: add interface for storing generic data on destinations
-rw-r--r-- | src/wallet.cpp | 51 | ||||
-rw-r--r-- | src/wallet.h | 12 | ||||
-rw-r--r-- | src/walletdb.cpp | 24 | ||||
-rw-r--r-- | src/walletdb.h | 5 |
4 files changed, 92 insertions, 0 deletions
diff --git a/src/wallet.cpp b/src/wallet.cpp index 9065ba8483..f4c14b437c 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1534,7 +1534,19 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const string& strNam bool CWallet::DelAddressBook(const CTxDestination& address) { + AssertLockHeld(cs_wallet); // mapAddressBook + + if(fFileBacked) + { + // Delete destdata tuples associated with address + std::string strAddress = CBitcoinAddress(address).ToString(); + BOOST_FOREACH(const PAIRTYPE(string, string) &item, mapAddressBook[address].destdata) + { + CWalletDB(strWalletFile).EraseDestData(strAddress, item.first); + } + } + mapAddressBook.erase(address); NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), "", CT_DELETED); if (!fFileBacked) @@ -2008,3 +2020,42 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const { for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++) mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off } + +bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value) +{ + mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); + if (!fFileBacked) + return true; + return CWalletDB(strWalletFile).WriteDestData(CBitcoinAddress(dest).ToString(), key, value); +} + +bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key) +{ + if (!mapAddressBook[dest].destdata.erase(key)) + return false; + if (!fFileBacked) + return true; + return CWalletDB(strWalletFile).EraseDestData(CBitcoinAddress(dest).ToString(), key); +} + +bool CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value) +{ + mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); + return true; +} + +bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const +{ + std::map<CTxDestination, CAddressBookData>::const_iterator i = mapAddressBook.find(dest); + if(i != mapAddressBook.end()) + { + CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key); + if(j != i->second.destdata.end()) + { + if(value) + *value = j->second; + return true; + } + } + return false; +} diff --git a/src/wallet.h b/src/wallet.h index e4452a3093..dc8c007ac8 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -83,6 +83,9 @@ public: { purpose = "unknown"; } + + typedef std::map<std::string, std::string> StringMap; + StringMap destdata; }; /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances, @@ -189,6 +192,15 @@ public: bool AddCScript(const CScript& redeemScript); bool LoadCScript(const CScript& redeemScript) { return CCryptoKeyStore::AddCScript(redeemScript); } + /// Adds a destination data tuple to the store, and saves it to disk + bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value); + /// Erases a destination data tuple in the store and on disk + bool EraseDestData(const CTxDestination &dest, const std::string &key); + /// Adds a destination data tuple to the store, without saving it to disk + bool LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value); + /// Look up a destination data tuple in the store, return true if found false otherwise + bool GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const; + bool Unlock(const SecureString& strWalletPassphrase); bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase); bool EncryptWallet(const SecureString& strWalletPassphrase); diff --git a/src/walletdb.cpp b/src/walletdb.cpp index 9c5bddba60..2e61c6cd58 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -564,6 +564,18 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { ssValue >> pwallet->nOrderPosNext; } + else if (strType == "destdata") + { + std::string strAddress, strKey, strValue; + ssKey >> strAddress; + ssKey >> strKey; + ssValue >> strValue; + if (!pwallet->LoadDestData(CBitcoinAddress(strAddress).Get(), strKey, strValue)) + { + strErr = "Error reading wallet database: LoadDestData failed"; + return false; + } + } } catch (...) { return false; @@ -865,3 +877,15 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename) { return CWalletDB::Recover(dbenv, filename, false); } + +bool CWalletDB::WriteDestData(const std::string &address, const std::string &key, const std::string &value) +{ + nWalletDBUpdated++; + return Write(boost::make_tuple(std::string("destdata"), address, key), value); +} + +bool CWalletDB::EraseDestData(const std::string &address, const std::string &key) +{ + nWalletDBUpdated++; + return Erase(boost::make_tuple(string("destdata"), address, key)); +} diff --git a/src/walletdb.h b/src/walletdb.h index 88ba89f9d5..15af287245 100644 --- a/src/walletdb.h +++ b/src/walletdb.h @@ -124,6 +124,11 @@ public: bool ReadAccount(const std::string& strAccount, CAccount& account); bool WriteAccount(const std::string& strAccount, const CAccount& account); + + /// Write destination data key,value tuple to database + bool WriteDestData(const std::string &address, const std::string &key, const std::string &value); + /// Erase destination data tuple from wallet database + bool EraseDestData(const std::string &address, const std::string &key); private: bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry); public: |