1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_WALLET_WALLETDB_H
7 #define BITCOIN_WALLET_WALLETDB_H
10 #include "wallet/db.h"
19 static const bool DEFAULT_FLUSHWALLET
= true;
22 class CAccountingEntry
;
32 /** Error statuses for the wallet database */
46 static const int CURRENT_VERSION
=1;
48 int64_t nCreateTime
; // 0 means unknown
54 CKeyMetadata(int64_t nCreateTime_
)
56 nVersion
= CKeyMetadata::CURRENT_VERSION
;
57 nCreateTime
= nCreateTime_
;
60 ADD_SERIALIZE_METHODS
;
62 template <typename Stream
, typename Operation
>
63 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
64 READWRITE(this->nVersion
);
65 nVersion
= this->nVersion
;
66 READWRITE(nCreateTime
);
71 nVersion
= CKeyMetadata::CURRENT_VERSION
;
76 /** Access to the wallet database */
77 class CWalletDB
: public CDB
80 CWalletDB(const std::string
& strFilename
, const char* pszMode
= "r+", bool fFlushOnClose
= true) : CDB(strFilename
, pszMode
, fFlushOnClose
)
84 bool WriteName(const std::string
& strAddress
, const std::string
& strName
);
85 bool EraseName(const std::string
& strAddress
);
87 bool WritePurpose(const std::string
& strAddress
, const std::string
& purpose
);
88 bool ErasePurpose(const std::string
& strAddress
);
90 bool WriteTx(const CWalletTx
& wtx
);
91 bool EraseTx(uint256 hash
);
93 bool WriteKey(const CPubKey
& vchPubKey
, const CPrivKey
& vchPrivKey
, const CKeyMetadata
&keyMeta
);
94 bool WriteCryptedKey(const CPubKey
& vchPubKey
, const std::vector
<unsigned char>& vchCryptedSecret
, const CKeyMetadata
&keyMeta
);
95 bool WriteMasterKey(unsigned int nID
, const CMasterKey
& kMasterKey
);
97 bool WriteCScript(const uint160
& hash
, const CScript
& redeemScript
);
99 bool WriteWatchOnly(const CScript
&script
);
100 bool EraseWatchOnly(const CScript
&script
);
102 bool WriteBestBlock(const CBlockLocator
& locator
);
103 bool ReadBestBlock(CBlockLocator
& locator
);
105 bool WriteOrderPosNext(int64_t nOrderPosNext
);
107 bool WriteDefaultKey(const CPubKey
& vchPubKey
);
109 bool ReadPool(int64_t nPool
, CKeyPool
& keypool
);
110 bool WritePool(int64_t nPool
, const CKeyPool
& keypool
);
111 bool ErasePool(int64_t nPool
);
113 bool WriteMinVersion(int nVersion
);
115 /// This writes directly to the database, and will not update the CWallet's cached accounting entries!
116 /// Use wallet.AddAccountingEntry instead, to write *and* update its caches.
117 bool WriteAccountingEntry_Backend(const CAccountingEntry
& acentry
);
118 bool ReadAccount(const std::string
& strAccount
, CAccount
& account
);
119 bool WriteAccount(const std::string
& strAccount
, const CAccount
& account
);
121 /// Write destination data key,value tuple to database
122 bool WriteDestData(const std::string
&address
, const std::string
&key
, const std::string
&value
);
123 /// Erase destination data tuple from wallet database
124 bool EraseDestData(const std::string
&address
, const std::string
&key
);
126 CAmount
GetAccountCreditDebit(const std::string
& strAccount
);
127 void ListAccountCreditDebit(const std::string
& strAccount
, std::list
<CAccountingEntry
>& acentries
);
129 DBErrors
ReorderTransactions(CWallet
* pwallet
);
130 DBErrors
LoadWallet(CWallet
* pwallet
);
131 DBErrors
FindWalletTx(CWallet
* pwallet
, std::vector
<uint256
>& vTxHash
, std::vector
<CWalletTx
>& vWtx
);
132 DBErrors
ZapWalletTx(CWallet
* pwallet
, std::vector
<CWalletTx
>& vWtx
);
133 DBErrors
ZapSelectTx(CWallet
* pwallet
, std::vector
<uint256
>& vHashIn
, std::vector
<uint256
>& vHashOut
);
134 static bool Recover(CDBEnv
& dbenv
, const std::string
& filename
, bool fOnlyKeys
);
135 static bool Recover(CDBEnv
& dbenv
, const std::string
& filename
);
138 CWalletDB(const CWalletDB
&);
139 void operator=(const CWalletDB
&);
141 bool WriteAccountingEntry(const uint64_t nAccEntryNum
, const CAccountingEntry
& acentry
);
144 bool BackupWallet(const CWallet
& wallet
, const std::string
& strDest
);
145 void ThreadFlushWalletDB(const std::string
& strFile
);
147 #endif // BITCOIN_WALLET_WALLETDB_H