2012-04-15 23:39:49 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2014-08-28 22:21:03 +02:00
|
|
|
|
2015-03-21 18:15:31 +01:00
|
|
|
#ifndef BITCOIN_WALLET_WALLETDB_H
|
|
|
|
#define BITCOIN_WALLET_WALLETDB_H
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
#include "amount.h"
|
2015-11-06 01:32:04 +01:00
|
|
|
#include "primitives/transaction.h"
|
2015-02-03 21:09:47 +01:00
|
|
|
#include "wallet/db.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "key.h"
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2015-06-27 21:21:41 +02:00
|
|
|
static const bool DEFAULT_FLUSHWALLET = true;
|
|
|
|
|
2012-04-15 23:39:49 +02:00
|
|
|
class CAccount;
|
|
|
|
class CAccountingEntry;
|
2013-11-11 01:15:43 +01:00
|
|
|
struct CBlockLocator;
|
2013-04-13 07:13:08 +02:00
|
|
|
class CKeyPool;
|
|
|
|
class CMasterKey;
|
|
|
|
class CScript;
|
2013-01-06 13:30:00 +01:00
|
|
|
class CWallet;
|
|
|
|
class CWalletTx;
|
2013-04-13 07:13:08 +02:00
|
|
|
class uint160;
|
|
|
|
class uint256;
|
2012-04-15 23:39:49 +02:00
|
|
|
|
|
|
|
/** Error statuses for the wallet database */
|
|
|
|
enum DBErrors
|
|
|
|
{
|
|
|
|
DB_LOAD_OK,
|
|
|
|
DB_CORRUPT,
|
2012-09-18 20:30:47 +02:00
|
|
|
DB_NONCRITICAL_ERROR,
|
2012-04-15 23:39:49 +02:00
|
|
|
DB_TOO_NEW,
|
|
|
|
DB_LOAD_FAIL,
|
|
|
|
DB_NEED_REWRITE
|
|
|
|
};
|
|
|
|
|
2016-07-15 10:34:08 +02:00
|
|
|
/* simple HD chain data model */
|
2016-01-02 12:34:08 +01:00
|
|
|
class CHDChain
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint32_t nExternalChainCounter;
|
|
|
|
CKeyID masterKeyID; //!< master key hash160
|
|
|
|
|
|
|
|
static const int CURRENT_VERSION = 1;
|
|
|
|
int nVersion;
|
|
|
|
|
|
|
|
CHDChain() { SetNull(); }
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
template <typename Stream, typename Operation>
|
2016-10-29 01:29:17 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
2016-01-02 12:34:08 +01:00
|
|
|
{
|
|
|
|
READWRITE(this->nVersion);
|
|
|
|
READWRITE(nExternalChainCounter);
|
|
|
|
READWRITE(masterKeyID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull()
|
|
|
|
{
|
|
|
|
nVersion = CHDChain::CURRENT_VERSION;
|
|
|
|
nExternalChainCounter = 0;
|
|
|
|
masterKeyID.SetNull();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-10 15:36:29 +02:00
|
|
|
class CKeyMetadata
|
|
|
|
{
|
|
|
|
public:
|
2016-07-09 11:41:01 +02:00
|
|
|
static const int VERSION_BASIC=1;
|
|
|
|
static const int VERSION_WITH_HDDATA=10;
|
|
|
|
static const int CURRENT_VERSION=VERSION_WITH_HDDATA;
|
2013-06-10 15:36:29 +02:00
|
|
|
int nVersion;
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t nCreateTime; // 0 means unknown
|
2016-07-09 11:41:01 +02:00
|
|
|
std::string hdKeypath; //optional HD/bip32 keypath
|
2016-07-15 10:34:08 +02:00
|
|
|
CKeyID hdMasterKeyID; //id of the HD masterkey used to derive this key
|
2013-06-10 15:36:29 +02:00
|
|
|
|
|
|
|
CKeyMetadata()
|
|
|
|
{
|
|
|
|
SetNull();
|
|
|
|
}
|
2013-04-13 07:13:08 +02:00
|
|
|
CKeyMetadata(int64_t nCreateTime_)
|
2013-06-10 15:36:29 +02:00
|
|
|
{
|
2016-07-15 10:33:25 +02:00
|
|
|
SetNull();
|
2013-06-10 15:36:29 +02:00
|
|
|
nCreateTime = nCreateTime_;
|
|
|
|
}
|
|
|
|
|
2014-09-02 09:58:09 +02:00
|
|
|
ADD_SERIALIZE_METHODS;
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 08:42:31 +02:00
|
|
|
|
2014-08-20 22:44:38 +02:00
|
|
|
template <typename Stream, typename Operation>
|
2016-10-29 01:29:17 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2013-06-10 15:36:29 +02:00
|
|
|
READWRITE(this->nVersion);
|
|
|
|
READWRITE(nCreateTime);
|
2016-07-09 11:41:01 +02:00
|
|
|
if (this->nVersion >= VERSION_WITH_HDDATA)
|
|
|
|
{
|
|
|
|
READWRITE(hdKeypath);
|
|
|
|
READWRITE(hdMasterKeyID);
|
|
|
|
}
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 08:42:31 +02:00
|
|
|
}
|
2013-06-10 15:36:29 +02:00
|
|
|
|
|
|
|
void SetNull()
|
|
|
|
{
|
|
|
|
nVersion = CKeyMetadata::CURRENT_VERSION;
|
2013-06-20 01:13:55 +02:00
|
|
|
nCreateTime = 0;
|
2016-07-09 11:41:01 +02:00
|
|
|
hdKeypath.clear();
|
2016-07-15 10:33:25 +02:00
|
|
|
hdMasterKeyID.SetNull();
|
2013-06-10 15:36:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-26 12:48:53 +01:00
|
|
|
/** Access to the wallet database */
|
2012-04-15 23:39:49 +02:00
|
|
|
class CWalletDB : public CDB
|
|
|
|
{
|
|
|
|
public:
|
2014-08-31 05:55:27 +02:00
|
|
|
CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
|
2012-04-15 23:39:49 +02:00
|
|
|
{
|
|
|
|
}
|
2014-09-16 15:18:33 +02:00
|
|
|
|
2012-04-15 23:39:49 +02:00
|
|
|
bool WriteName(const std::string& strAddress, const std::string& strName);
|
|
|
|
bool EraseName(const std::string& strAddress);
|
|
|
|
|
2013-07-22 08:50:39 +02:00
|
|
|
bool WritePurpose(const std::string& strAddress, const std::string& purpose);
|
|
|
|
bool ErasePurpose(const std::string& strAddress);
|
|
|
|
|
2016-05-09 09:15:12 +02:00
|
|
|
bool WriteTx(const CWalletTx& wtx);
|
2013-04-13 07:13:08 +02:00
|
|
|
bool EraseTx(uint256 hash);
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
|
|
|
|
bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
|
|
|
|
bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
bool WriteCScript(const uint160& hash, const CScript& redeemScript);
|
2013-06-10 15:36:29 +02:00
|
|
|
|
2014-06-09 21:11:59 +02:00
|
|
|
bool WriteWatchOnly(const CScript &script);
|
2014-07-26 21:05:11 +02:00
|
|
|
bool EraseWatchOnly(const CScript &script);
|
2013-07-26 01:06:01 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
bool WriteBestBlock(const CBlockLocator& locator);
|
|
|
|
bool ReadBestBlock(CBlockLocator& locator);
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
bool WriteOrderPosNext(int64_t nOrderPosNext);
|
2013-06-10 15:36:29 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
bool WriteDefaultKey(const CPubKey& vchPubKey);
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
bool ReadPool(int64_t nPool, CKeyPool& keypool);
|
|
|
|
bool WritePool(int64_t nPool, const CKeyPool& keypool);
|
|
|
|
bool ErasePool(int64_t nPool);
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
bool WriteMinVersion(int nVersion);
|
2012-04-15 23:39:49 +02:00
|
|
|
|
2015-10-19 11:19:38 +02:00
|
|
|
/// This writes directly to the database, and will not update the CWallet's cached accounting entries!
|
|
|
|
/// Use wallet.AddAccountingEntry instead, to write *and* update its caches.
|
2016-09-28 17:57:25 +02:00
|
|
|
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
|
2015-10-19 11:19:38 +02:00
|
|
|
bool WriteAccountingEntry_Backend(const CAccountingEntry& acentry);
|
2012-04-15 23:39:49 +02:00
|
|
|
bool ReadAccount(const std::string& strAccount, CAccount& account);
|
|
|
|
bool WriteAccount(const std::string& strAccount, const CAccount& account);
|
2013-11-18 16:55:54 +01:00
|
|
|
|
|
|
|
/// 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);
|
2014-09-16 15:18:33 +02:00
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount GetAccountCreditDebit(const std::string& strAccount);
|
2012-04-15 23:39:49 +02:00
|
|
|
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
|
|
|
|
|
2012-09-18 20:30:47 +02:00
|
|
|
DBErrors LoadWallet(CWallet* pwallet);
|
2014-02-14 18:27:15 +01:00
|
|
|
DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
|
|
|
|
DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
|
2016-03-07 14:51:06 +01:00
|
|
|
DBErrors ZapSelectTx(CWallet* pwallet, std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
|
2015-03-21 18:40:51 +01:00
|
|
|
static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys);
|
|
|
|
static bool Recover(CDBEnv& dbenv, const std::string& filename);
|
2014-09-16 15:18:33 +02:00
|
|
|
|
2016-01-02 12:34:08 +01:00
|
|
|
//! write the hdchain model (external chain child index counter)
|
|
|
|
bool WriteHDChain(const CHDChain& chain);
|
|
|
|
|
2014-09-16 15:18:33 +02:00
|
|
|
private:
|
|
|
|
CWalletDB(const CWalletDB&);
|
|
|
|
void operator=(const CWalletDB&);
|
|
|
|
|
2012-04-15 23:39:49 +02:00
|
|
|
};
|
|
|
|
|
2015-04-08 14:31:53 +02:00
|
|
|
void ThreadFlushWalletDB(const std::string& strFile);
|
2013-01-06 13:30:00 +01:00
|
|
|
|
2015-03-21 18:15:31 +01:00
|
|
|
#endif // BITCOIN_WALLET_WALLETDB_H
|