Skip to content

Commit b10e147

Browse files
laanwjcozz
authored andcommitted
wallet: add interface for storing generic data on destinations
1 parent dd7c1cf commit b10e147

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/wallet.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,19 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const string& strNam
15341534

15351535
bool CWallet::DelAddressBook(const CTxDestination& address)
15361536
{
1537+
15371538
AssertLockHeld(cs_wallet); // mapAddressBook
1539+
1540+
if(fFileBacked)
1541+
{
1542+
// Delete destdata tuples associated with address
1543+
std::string strAddress = CBitcoinAddress(address).ToString();
1544+
BOOST_FOREACH(const PAIRTYPE(string, string) &item, mapAddressBook[address].destdata)
1545+
{
1546+
CWalletDB(strWalletFile).EraseDestData(strAddress, item.first);
1547+
}
1548+
}
1549+
15381550
mapAddressBook.erase(address);
15391551
NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), "", CT_DELETED);
15401552
if (!fFileBacked)
@@ -2008,3 +2020,42 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
20082020
for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
20092021
mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off
20102022
}
2023+
2024+
bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
2025+
{
2026+
mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
2027+
if (!fFileBacked)
2028+
return true;
2029+
return CWalletDB(strWalletFile).WriteDestData(CBitcoinAddress(dest).ToString(), key, value);
2030+
}
2031+
2032+
bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
2033+
{
2034+
if (!mapAddressBook[dest].destdata.erase(key))
2035+
return false;
2036+
if (!fFileBacked)
2037+
return true;
2038+
return CWalletDB(strWalletFile).EraseDestData(CBitcoinAddress(dest).ToString(), key);
2039+
}
2040+
2041+
bool CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
2042+
{
2043+
mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
2044+
return true;
2045+
}
2046+
2047+
bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const
2048+
{
2049+
std::map<CTxDestination, CAddressBookData>::const_iterator i = mapAddressBook.find(dest);
2050+
if(i != mapAddressBook.end())
2051+
{
2052+
CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
2053+
if(j != i->second.destdata.end())
2054+
{
2055+
if(value)
2056+
*value = j->second;
2057+
return true;
2058+
}
2059+
}
2060+
return false;
2061+
}

src/wallet.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class CAddressBookData
8383
{
8484
purpose = "unknown";
8585
}
86+
87+
typedef std::map<std::string, std::string> StringMap;
88+
StringMap destdata;
8689
};
8790

8891
/** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
@@ -189,6 +192,15 @@ class CWallet : public CCryptoKeyStore, public CWalletInterface
189192
bool AddCScript(const CScript& redeemScript);
190193
bool LoadCScript(const CScript& redeemScript) { return CCryptoKeyStore::AddCScript(redeemScript); }
191194

195+
/// Adds a destination data tuple to the store, and saves it to disk
196+
bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value);
197+
/// Erases a destination data tuple in the store and on disk
198+
bool EraseDestData(const CTxDestination &dest, const std::string &key);
199+
/// Adds a destination data tuple to the store, without saving it to disk
200+
bool LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value);
201+
/// Look up a destination data tuple in the store, return true if found false otherwise
202+
bool GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const;
203+
192204
bool Unlock(const SecureString& strWalletPassphrase);
193205
bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
194206
bool EncryptWallet(const SecureString& strWalletPassphrase);

src/walletdb.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,18 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
564564
{
565565
ssValue >> pwallet->nOrderPosNext;
566566
}
567+
else if (strType == "destdata")
568+
{
569+
std::string strAddress, strKey, strValue;
570+
ssKey >> strAddress;
571+
ssKey >> strKey;
572+
ssValue >> strValue;
573+
if (!pwallet->LoadDestData(CBitcoinAddress(strAddress).Get(), strKey, strValue))
574+
{
575+
strErr = "Error reading wallet database: LoadDestData failed";
576+
return false;
577+
}
578+
}
567579
} catch (...)
568580
{
569581
return false;
@@ -865,3 +877,15 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
865877
{
866878
return CWalletDB::Recover(dbenv, filename, false);
867879
}
880+
881+
bool CWalletDB::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
882+
{
883+
nWalletDBUpdated++;
884+
return Write(boost::make_tuple(std::string("destdata"), address, key), value);
885+
}
886+
887+
bool CWalletDB::EraseDestData(const std::string &address, const std::string &key)
888+
{
889+
nWalletDBUpdated++;
890+
return Erase(boost::make_tuple(string("destdata"), address, key));
891+
}

src/walletdb.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ class CWalletDB : public CDB
124124

125125
bool ReadAccount(const std::string& strAccount, CAccount& account);
126126
bool WriteAccount(const std::string& strAccount, const CAccount& account);
127+
128+
/// Write destination data key,value tuple to database
129+
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
130+
/// Erase destination data tuple from wallet database
131+
bool EraseDestData(const std::string &address, const std::string &key);
127132
private:
128133
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
129134
public:

0 commit comments

Comments
 (0)