Add wallet privkey encryption.
[novacoin.git] / src / db.h
index 290981c..c8d46d8 100644 (file)
--- a/src/db.h
+++ b/src/db.h
@@ -1,34 +1,36 @@
 // Copyright (c) 2009-2010 Satoshi Nakamoto
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
+#ifndef BITCOIN_DB_H
+#define BITCOIN_DB_H
+
+#include "key.h"
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include <db_cxx.h>
 
-class CTransaction;
 class CTxIndex;
 class CDiskBlockIndex;
 class CDiskTxPos;
 class COutPoint;
-class CUser;
-class CReview;
 class CAddress;
 class CWalletTx;
+class CWallet;
 class CAccount;
 class CAccountingEntry;
 class CBlockLocator;
 
-extern map<string, string> mapAddressBook;
-extern CCriticalSection cs_mapAddressBook;
-extern vector<unsigned char> vchDefaultKey;
-extern bool fClient;
-extern int nBestHeight;
-
 
 extern unsigned int nWalletDBUpdated;
 extern DbEnv dbenv;
 
 
 extern void DBFlush(bool fShutdown);
-extern vector<unsigned char> GetKeyFromKeyPool();
-extern int64 GetOldestKeyPoolTime();
+void ThreadFlushWalletDB(void* parg);
+bool BackupWallet(const CWallet& wallet, const std::string& strDest);
 
 
 
@@ -37,8 +39,8 @@ class CDB
 {
 protected:
     Db* pdb;
-    string strFile;
-    vector<DbTxn*> vTxn;
+    std::string strFile;
+    std::vector<DbTxn*> vTxn;
     bool fReadOnly;
 
     explicit CDB(const char* pszFile, const char* pszMode="r+");
@@ -247,12 +249,12 @@ public:
     bool ReadVersion(int& nVersion)
     {
         nVersion = 0;
-        return Read(string("version"), nVersion);
+        return Read(std::string("version"), nVersion);
     }
 
     bool WriteVersion(int nVersion)
     {
-        return Write(string("version"), nVersion);
+        return Write(std::string("version"), nVersion);
     }
 };
 
@@ -276,7 +278,7 @@ public:
     bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
     bool EraseTxIndex(const CTransaction& tx);
     bool ContainsTx(uint256 hash);
-    bool ReadOwnerTxes(uint160 hash160, int nHeight, vector<CTransaction>& vtx);
+    bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector<CTransaction>& vtx);
     bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
     bool ReadDiskTx(uint256 hash, CTransaction& tx);
     bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
@@ -311,21 +313,18 @@ bool LoadAddresses();
 
 
 
-
-
-
 class CKeyPool
 {
 public:
     int64 nTime;
-    vector<unsigned char> vchPubKey;
+    std::vector<unsigned char> vchPubKey;
 
     CKeyPool()
     {
         nTime = GetTime();
     }
 
-    CKeyPool(const vector<unsigned char>& vchPubKeyIn)
+    CKeyPool(const std::vector<unsigned char>& vchPubKeyIn)
     {
         nTime = GetTime();
         vchPubKey = vchPubKeyIn;
@@ -346,169 +345,131 @@ public:
 class CWalletDB : public CDB
 {
 public:
-    CWalletDB(const char* pszMode="r+") : CDB("wallet.dat", pszMode)
+    CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
     {
     }
 private:
     CWalletDB(const CWalletDB&);
     void operator=(const CWalletDB&);
 public:
-    bool ReadName(const string& strAddress, string& strName)
+    bool ReadName(const std::string& strAddress, std::string& strName)
     {
         strName = "";
-        return Read(make_pair(string("name"), strAddress), strName);
+        return Read(std::make_pair(std::string("name"), strAddress), strName);
     }
 
-    bool WriteName(const string& strAddress, const string& strName)
-    {
-        CRITICAL_BLOCK(cs_mapAddressBook)
-            mapAddressBook[strAddress] = strName;
-        nWalletDBUpdated++;
-        return Write(make_pair(string("name"), strAddress), strName);
-    }
+    bool WriteName(const std::string& strAddress, const std::string& strName);
 
-    bool EraseName(const string& strAddress)
-    {
-        // This should only be used for sending addresses, never for receiving addresses,
-        // receiving addresses must always have an address book entry if they're not change return.
-        CRITICAL_BLOCK(cs_mapAddressBook)
-            mapAddressBook.erase(strAddress);
-        nWalletDBUpdated++;
-        return Erase(make_pair(string("name"), strAddress));
-    }
+    bool EraseName(const std::string& strAddress);
 
     bool ReadTx(uint256 hash, CWalletTx& wtx)
     {
-        return Read(make_pair(string("tx"), hash), wtx);
+        return Read(std::make_pair(std::string("tx"), hash), wtx);
     }
 
     bool WriteTx(uint256 hash, const CWalletTx& wtx)
     {
         nWalletDBUpdated++;
-        return Write(make_pair(string("tx"), hash), wtx);
+        return Write(std::make_pair(std::string("tx"), hash), wtx);
     }
 
     bool EraseTx(uint256 hash)
     {
         nWalletDBUpdated++;
-        return Erase(make_pair(string("tx"), hash));
+        return Erase(std::make_pair(std::string("tx"), hash));
     }
 
-    bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
+    bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
     {
         vchPrivKey.clear();
-        return Read(make_pair(string("key"), vchPubKey), vchPrivKey);
+        return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
     }
 
-    bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
+    bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
     {
         nWalletDBUpdated++;
-        return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false);
+        return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
     }
 
-    bool WriteBestBlock(const CBlockLocator& locator)
+    bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
     {
         nWalletDBUpdated++;
-        return Write(string("bestblock"), locator);
+        if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
+            return false;
+        if (fEraseUnencryptedKey)
+        {
+            Erase(std::make_pair(std::string("key"), vchPubKey));
+            Erase(std::make_pair(std::string("wkey"), vchPubKey));
+        }
+        return true;
     }
 
-    bool ReadBestBlock(CBlockLocator& locator)
+    bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
     {
-        return Read(string("bestblock"), locator);
+        nWalletDBUpdated++;
+        return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
     }
 
-    bool ReadDefaultKey(vector<unsigned char>& vchPubKey)
+    bool WriteBestBlock(const CBlockLocator& locator)
     {
-        vchPubKey.clear();
-        return Read(string("defaultkey"), vchPubKey);
+        nWalletDBUpdated++;
+        return Write(std::string("bestblock"), locator);
     }
 
-    bool WriteDefaultKey(const vector<unsigned char>& vchPubKey)
+    bool ReadBestBlock(CBlockLocator& locator)
     {
-        vchDefaultKey = vchPubKey;
-        nWalletDBUpdated++;
-        return Write(string("defaultkey"), vchPubKey);
+        return Read(std::string("bestblock"), locator);
     }
 
-    template<typename T>
-    bool ReadSetting(const string& strKey, T& value)
+    bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
     {
-        return Read(make_pair(string("setting"), strKey), value);
+        vchPubKey.clear();
+        return Read(std::string("defaultkey"), vchPubKey);
     }
 
-    template<typename T>
-    bool WriteSetting(const string& strKey, const T& value)
+    bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
     {
         nWalletDBUpdated++;
-        return Write(make_pair(string("setting"), strKey), value);
+        return Write(std::string("defaultkey"), vchPubKey);
     }
 
-    bool ReadAccount(const string& strAccount, CAccount& account);
-    bool WriteAccount(const string& strAccount, const CAccount& account);
-    bool WriteAccountingEntry(const CAccountingEntry& acentry);
-    int64 GetAccountCreditDebit(const string& strAccount);
-    void ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& acentries);
-
-    bool LoadWallet();
-protected:
-    void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool);
-    void KeepKey(int64 nIndex);
-    static void ReturnKey(int64 nIndex);
-    friend class CReserveKey;
-    friend vector<unsigned char> GetKeyFromKeyPool();
-    friend int64 GetOldestKeyPoolTime();
-};
-
-bool LoadWallet(bool& fFirstRunRet);
-void BackupWallet(const string& strDest);
-
-inline bool SetAddressBookName(const string& strAddress, const string& strName)
-{
-    return CWalletDB().WriteName(strAddress, strName);
-}
-
-class CReserveKey
-{
-protected:
-    int64 nIndex;
-    vector<unsigned char> vchPubKey;
-public:
-    CReserveKey()
+    bool ReadPool(int64 nPool, CKeyPool& keypool)
     {
-        nIndex = -1;
+        return Read(std::make_pair(std::string("pool"), nPool), keypool);
     }
 
-    ~CReserveKey()
+    bool WritePool(int64 nPool, const CKeyPool& keypool)
     {
-        if (!fShutdown)
-            ReturnKey();
+        nWalletDBUpdated++;
+        return Write(std::make_pair(std::string("pool"), nPool), keypool);
     }
 
-    vector<unsigned char> GetReservedKey()
+    bool ErasePool(int64 nPool)
     {
-        if (nIndex == -1)
-        {
-            CKeyPool keypool;
-            CWalletDB().ReserveKeyFromKeyPool(nIndex, keypool);
-            vchPubKey = keypool.vchPubKey;
-        }
-        assert(!vchPubKey.empty());
-        return vchPubKey;
+        nWalletDBUpdated++;
+        return Erase(std::make_pair(std::string("pool"), nPool));
     }
 
-    void KeepKey()
+    template<typename T>
+    bool ReadSetting(const std::string& strKey, T& value)
     {
-        if (nIndex != -1)
-            CWalletDB().KeepKey(nIndex);
-        nIndex = -1;
-        vchPubKey.clear();
+        return Read(std::make_pair(std::string("setting"), strKey), value);
     }
 
-    void ReturnKey()
+    template<typename T>
+    bool WriteSetting(const std::string& strKey, const T& value)
     {
-        if (nIndex != -1)
-            CWalletDB::ReturnKey(nIndex);
-        nIndex = -1;
-        vchPubKey.clear();
+        nWalletDBUpdated++;
+        return Write(std::make_pair(std::string("setting"), strKey), value);
     }
+
+    bool ReadAccount(const std::string& strAccount, CAccount& account);
+    bool WriteAccount(const std::string& strAccount, const CAccount& account);
+    bool WriteAccountingEntry(const CAccountingEntry& acentry);
+    int64 GetAccountCreditDebit(const std::string& strAccount);
+    void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
+
+    bool LoadWallet(CWallet* pwallet);
 };
+
+#endif