c141db83098ebcbc32950f8f682a926e723ff7d6
[novacoin.git] / src / walletdb.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_WALLETDB_H
6 #define BITCOIN_WALLETDB_H
7
8 #include "db.h"
9 #include "keystore.h"
10
11 using namespace std;
12
13 class CKeyPool;
14 class CAccount;
15 class CAccountingEntry;
16
17 // Error statuses for the wallet database
18 enum DBErrors
19 {
20     DB_LOAD_OK,
21     DB_CORRUPT,
22     DB_NONCRITICAL_ERROR,
23     DB_TOO_NEW,
24     DB_LOAD_FAIL,
25     DB_NEED_REWRITE
26 };
27
28 class CKeyMetadata
29 {
30 public:
31     static const int CURRENT_VERSION=1;
32     int nVersion;
33     int64_t nCreateTime; // 0 means unknown
34
35     CKeyMetadata();
36     CKeyMetadata(int64_t nCreateTime_);
37
38     IMPLEMENT_SERIALIZE
39     (
40         READWRITE(this->nVersion);
41         nVersion = this->nVersion;
42         READWRITE(nCreateTime);
43     )
44
45     void SetNull();
46 };
47
48 // Access to the wallet database (wallet.dat)
49 class CWalletDB : public CDB
50 {
51 public:
52     CWalletDB(string strFilename, const char* pszMode="r+");
53 private:
54     CWalletDB(const CWalletDB&);
55     void operator=(const CWalletDB&);
56 public:
57     bool WriteName(const string& strAddress, const string& strName);
58     bool EraseName(const string& strAddress);
59     bool WriteTx(uint256 hash, const CWalletTx& wtx);
60     bool EraseTx(uint256 hash);
61     bool WriteKey(const CPubKey& key, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
62     bool WriteMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH, const CKeyMetadata &keyMeta);
63     bool WriteCryptedMalleableKey(const CMalleableKeyView& keyView, const vector<unsigned char>& vchCryptedSecretH, const CKeyMetadata &keyMeta);
64     bool WriteCryptedKey(const CPubKey& key, const vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
65     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
66     bool EraseMasterKey(unsigned int nID);
67     bool EraseCryptedKey(const CPubKey& key);
68     bool EraseCryptedMalleableKey(const CMalleableKeyView& keyView);
69     bool WriteCScript(const uint160& hash, const CScript& redeemScript);
70     bool WriteWatchOnly(const CScript &dest);
71     bool EraseWatchOnly(const CScript &dest);
72     bool WriteBestBlock(const CBlockLocator& locator);
73     bool ReadBestBlock(CBlockLocator& locator);
74     bool WriteOrderPosNext(int64_t nOrderPosNext);
75     bool WriteDefaultKey(const CPubKey& key);
76     bool ReadPool(int64_t nPool, CKeyPool& keypool);
77     bool WritePool(int64_t nPool, const CKeyPool& keypool);
78     bool ErasePool(int64_t nPool);
79     bool WriteMinVersion(int nVersion);
80     bool ReadAccount(const string& strAccount, CAccount& account);
81     bool WriteAccount(const string& strAccount, const CAccount& account);
82 private:
83     bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
84 public:
85     bool WriteAccountingEntry(const CAccountingEntry& acentry);
86     int64_t GetAccountCreditDebit(const string& strAccount);
87     void ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& acentries);
88
89     DBErrors ReorderTransactions(CWallet*);
90     DBErrors LoadWallet(CWallet* pwallet);
91     DBErrors FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash);
92     DBErrors ZapWalletTx(CWallet* pwallet);
93
94     static bool Recover(CDBEnv& dbenv, string filename, bool fOnlyKeys);
95     static bool Recover(CDBEnv& dbenv, string filename);
96 };
97
98 #endif // BITCOIN_WALLETDB_H