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.2KB

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