walletdb.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_WALLETDB_H
  3. #define ECOIN_WALLETDB_H
  4. #include "db.h"
  5. #include "base58.h"
  6. class CKeyPool;
  7. class CAccount;
  8. class CAccountingEntry;
  9. /** Error statuses for the wallet database */
  10. enum DBErrors
  11. {
  12. DB_LOAD_OK,
  13. DB_CORRUPT,
  14. DB_NONCRITICAL_ERROR,
  15. DB_TOO_NEW,
  16. DB_LOAD_FAIL,
  17. DB_NEED_REWRITE
  18. };
  19. class CKeyMetadata
  20. {
  21. public:
  22. static const int CURRENT_VERSION=1;
  23. int nVersion;
  24. int64 nCreateTime; // 0 means unknown
  25. CKeyMetadata()
  26. {
  27. SetNull();
  28. }
  29. CKeyMetadata(int64 nCreateTime_)
  30. {
  31. nVersion = CKeyMetadata::CURRENT_VERSION;
  32. nCreateTime = nCreateTime_;
  33. }
  34. IMPLEMENT_SERIALIZE
  35. (
  36. READWRITE(this->nVersion);
  37. nVersion = this->nVersion;
  38. READWRITE(nCreateTime);
  39. )
  40. void SetNull()
  41. {
  42. nVersion = CKeyMetadata::CURRENT_VERSION;
  43. nCreateTime = 0;
  44. }
  45. };
  46. /** Access to the wallet database (wallet.dat) */
  47. class CWalletDB : public CDB
  48. {
  49. public:
  50. CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
  51. {
  52. }
  53. private:
  54. CWalletDB(const CWalletDB&);
  55. void operator=(const CWalletDB&);
  56. public:
  57. bool WriteName(const std::string& strAddress, const std::string& strName);
  58. bool EraseName(const std::string& strAddress);
  59. bool WriteTx(uint256 hash, const CWalletTx& wtx)
  60. {
  61. nWalletDBUpdated++;
  62. return Write(std::make_pair(std::string("tx"), hash), wtx);
  63. }
  64. bool EraseTx(uint256 hash)
  65. {
  66. nWalletDBUpdated++;
  67. return Erase(std::make_pair(std::string("tx"), hash));
  68. }
  69. bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta)
  70. {
  71. nWalletDBUpdated++;
  72. if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
  73. return false;
  74. return Write(std::make_pair(std::string("key"), vchPubKey.Raw()), vchPrivKey, false);
  75. }
  76. bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta)
  77. {
  78. nWalletDBUpdated++;
  79. bool fEraseUnencryptedKey = true;
  80. if(!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta))
  81. return false;
  82. if (!Write(std::make_pair(std::string("ckey"), vchPubKey.Raw()), vchCryptedSecret, false))
  83. return false;
  84. if (fEraseUnencryptedKey)
  85. {
  86. Erase(std::make_pair(std::string("key"), vchPubKey.Raw()));
  87. Erase(std::make_pair(std::string("wkey"), vchPubKey.Raw()));
  88. }
  89. return true;
  90. }
  91. bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
  92. {
  93. nWalletDBUpdated++;
  94. return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
  95. }
  96. bool WriteCScript(const uint160& hash, const CScript& redeemScript)
  97. {
  98. nWalletDBUpdated++;
  99. return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
  100. }
  101. bool WriteBestBlock(const CBlockLocator& locator)
  102. {
  103. nWalletDBUpdated++;
  104. return Write(std::string("bestblock"), locator);
  105. }
  106. bool ReadBestBlock(CBlockLocator& locator)
  107. {
  108. return Read(std::string("bestblock"), locator);
  109. }
  110. bool WriteOrderPosNext(int64 nOrderPosNext)
  111. {
  112. nWalletDBUpdated++;
  113. return Write(std::string("orderposnext"), nOrderPosNext);
  114. }
  115. bool WriteDefaultKey(const CPubKey& vchPubKey)
  116. {
  117. nWalletDBUpdated++;
  118. return Write(std::string("defaultkey"), vchPubKey.Raw());
  119. }
  120. bool ReadPool(int64 nPool, CKeyPool& keypool)
  121. {
  122. return Read(std::make_pair(std::string("pool"), nPool), keypool);
  123. }
  124. bool WritePool(int64 nPool, const CKeyPool& keypool)
  125. {
  126. nWalletDBUpdated++;
  127. return Write(std::make_pair(std::string("pool"), nPool), keypool);
  128. }
  129. bool ErasePool(int64 nPool)
  130. {
  131. nWalletDBUpdated++;
  132. return Erase(std::make_pair(std::string("pool"), nPool));
  133. }
  134. // Settings are no longer stored in wallet.dat; these are
  135. // used only for backwards compatibility:
  136. template<typename T>
  137. bool ReadSetting(const std::string& strKey, T& value)
  138. {
  139. return Read(std::make_pair(std::string("setting"), strKey), value);
  140. }
  141. template<typename T>
  142. bool WriteSetting(const std::string& strKey, const T& value)
  143. {
  144. nWalletDBUpdated++;
  145. return Write(std::make_pair(std::string("setting"), strKey), value);
  146. }
  147. bool EraseSetting(const std::string& strKey)
  148. {
  149. nWalletDBUpdated++;
  150. return Erase(std::make_pair(std::string("setting"), strKey));
  151. }
  152. bool WriteMinVersion(int nVersion)
  153. {
  154. return Write(std::string("minversion"), nVersion);
  155. }
  156. bool ReadAccount(const std::string& strAccount, CAccount& account);
  157. bool WriteAccount(const std::string& strAccount, const CAccount& account);
  158. private:
  159. bool WriteAccountingEntry(const uint64 nAccEntryNum, const CAccountingEntry& acentry);
  160. public:
  161. bool WriteAccountingEntry(const CAccountingEntry& acentry);
  162. int64 GetAccountCreditDebit(const std::string& strAccount);
  163. void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
  164. DBErrors ReorderTransactions(CWallet*);
  165. DBErrors LoadWallet(CWallet* pwallet);
  166. static bool Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys);
  167. static bool Recover(CDBEnv& dbenv, std::string filename);
  168. };
  169. #endif // ECOIN_WALLETDB_H