You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

walletdb.h 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 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.
  5. #ifndef BITCOIN_WALLET_WALLETDB_H
  6. #define BITCOIN_WALLET_WALLETDB_H
  7. #include "amount.h"
  8. #include "wallet/db.h"
  9. #include "key.h"
  10. #include "keystore.h"
  11. #include <list>
  12. #include <stdint.h>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. class CAccount;
  17. class CAccountingEntry;
  18. struct CBlockLocator;
  19. class CKeyPool;
  20. class CMasterKey;
  21. class CScript;
  22. class CWallet;
  23. class CWalletTx;
  24. class uint160;
  25. class uint256;
  26. /** Error statuses for the wallet database */
  27. enum DBErrors
  28. {
  29. DB_LOAD_OK,
  30. DB_CORRUPT,
  31. DB_NONCRITICAL_ERROR,
  32. DB_TOO_NEW,
  33. DB_LOAD_FAIL,
  34. DB_NEED_REWRITE
  35. };
  36. class CKeyMetadata
  37. {
  38. public:
  39. static const int CURRENT_VERSION=1;
  40. int nVersion;
  41. int64_t nCreateTime; // 0 means unknown
  42. CKeyMetadata()
  43. {
  44. SetNull();
  45. }
  46. CKeyMetadata(int64_t nCreateTime_)
  47. {
  48. nVersion = CKeyMetadata::CURRENT_VERSION;
  49. nCreateTime = nCreateTime_;
  50. }
  51. ADD_SERIALIZE_METHODS;
  52. template <typename Stream, typename Operation>
  53. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  54. READWRITE(this->nVersion);
  55. nVersion = this->nVersion;
  56. READWRITE(nCreateTime);
  57. }
  58. void SetNull()
  59. {
  60. nVersion = CKeyMetadata::CURRENT_VERSION;
  61. nCreateTime = 0;
  62. }
  63. };
  64. /** Access to the wallet database (wallet.dat) */
  65. class CWalletDB : public CDB
  66. {
  67. public:
  68. CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
  69. {
  70. }
  71. bool WriteName(const std::string& strAddress, const std::string& strName);
  72. bool EraseName(const std::string& strAddress);
  73. bool WritePurpose(const std::string& strAddress, const std::string& purpose);
  74. bool ErasePurpose(const std::string& strAddress);
  75. bool WriteTx(uint256 hash, const CWalletTx& wtx);
  76. bool EraseTx(uint256 hash);
  77. bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
  78. bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
  79. bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
  80. bool WriteCScript(const uint160& hash, const CScript& redeemScript);
  81. bool WriteWatchOnly(const CScript &script);
  82. bool EraseWatchOnly(const CScript &script);
  83. bool WriteBestBlock(const CBlockLocator& locator);
  84. bool ReadBestBlock(CBlockLocator& locator);
  85. bool WriteOrderPosNext(int64_t nOrderPosNext);
  86. bool WriteDefaultKey(const CPubKey& vchPubKey);
  87. bool ReadPool(int64_t nPool, CKeyPool& keypool);
  88. bool WritePool(int64_t nPool, const CKeyPool& keypool);
  89. bool ErasePool(int64_t nPool);
  90. bool WriteMinVersion(int nVersion);
  91. bool ReadAccount(const std::string& strAccount, CAccount& account);
  92. bool WriteAccount(const std::string& strAccount, const CAccount& account);
  93. /// Write destination data key,value tuple to database
  94. bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
  95. /// Erase destination data tuple from wallet database
  96. bool EraseDestData(const std::string &address, const std::string &key);
  97. bool WriteAccountingEntry(const CAccountingEntry& acentry);
  98. CAmount GetAccountCreditDebit(const std::string& strAccount);
  99. void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
  100. DBErrors ReorderTransactions(CWallet* pwallet);
  101. DBErrors LoadWallet(CWallet* pwallet);
  102. DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
  103. DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
  104. static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys);
  105. static bool Recover(CDBEnv& dbenv, const std::string& filename);
  106. private:
  107. CWalletDB(const CWalletDB&);
  108. void operator=(const CWalletDB&);
  109. bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
  110. };
  111. bool BackupWallet(const CWallet& wallet, const std::string& strDest);
  112. void ThreadFlushWalletDB(const std::string& strFile);
  113. #endif // BITCOIN_WALLET_WALLETDB_H