keystore.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_KEYSTORE_H
  3. #define ECOIN_KEYSTORE_H
  4. #include "crypter.h"
  5. #include "sync.h"
  6. #include <boost/signals2/signal.hpp>
  7. class CScript;
  8. class CKeyStore
  9. {
  10. protected:
  11. mutable CCriticalSection cs_KeyStore;
  12. public:
  13. virtual ~CKeyStore() {}
  14. virtual bool AddKey(const CKey& key) =0;
  15. virtual bool HaveKey(const CKeyID &address) const =0;
  16. virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
  17. virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
  18. virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
  19. virtual bool AddCScript(const CScript& redeemScript) =0;
  20. virtual bool HaveCScript(const CScriptID &hash) const =0;
  21. virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
  22. virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const
  23. {
  24. CKey key;
  25. if (!GetKey(address, key))
  26. return false;
  27. vchSecret = key.GetSecret(fCompressed);
  28. return true;
  29. }
  30. };
  31. typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
  32. typedef std::map<CScriptID, CScript > ScriptMap;
  33. class CBasicKeyStore : public CKeyStore
  34. {
  35. protected:
  36. KeyMap mapKeys;
  37. ScriptMap mapScripts;
  38. public:
  39. bool AddKey(const CKey& key);
  40. bool HaveKey(const CKeyID &address) const
  41. {
  42. bool result;
  43. {
  44. LOCK(cs_KeyStore);
  45. result = (mapKeys.count(address) > 0);
  46. }
  47. return result;
  48. }
  49. void GetKeys(std::set<CKeyID> &setAddress) const
  50. {
  51. setAddress.clear();
  52. {
  53. LOCK(cs_KeyStore);
  54. KeyMap::const_iterator mi = mapKeys.begin();
  55. while (mi != mapKeys.end())
  56. {
  57. setAddress.insert((*mi).first);
  58. mi++;
  59. }
  60. }
  61. }
  62. bool GetKey(const CKeyID &address, CKey &keyOut) const
  63. {
  64. {
  65. LOCK(cs_KeyStore);
  66. KeyMap::const_iterator mi = mapKeys.find(address);
  67. if (mi != mapKeys.end())
  68. {
  69. keyOut.Reset();
  70. keyOut.SetSecret((*mi).second.first, (*mi).second.second);
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. virtual bool AddCScript(const CScript& redeemScript);
  77. virtual bool HaveCScript(const CScriptID &hash) const;
  78. virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
  79. };
  80. typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
  81. class CCryptoKeyStore : public CBasicKeyStore
  82. {
  83. private:
  84. CryptedKeyMap mapCryptedKeys;
  85. CKeyingMaterial vMasterKey;
  86. bool fUseCrypto;
  87. protected:
  88. bool SetCrypted();
  89. bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
  90. bool Unlock(const CKeyingMaterial& vMasterKeyIn);
  91. public:
  92. CCryptoKeyStore() : fUseCrypto(false)
  93. {
  94. }
  95. bool IsCrypted() const
  96. {
  97. return fUseCrypto;
  98. }
  99. bool IsLocked() const
  100. {
  101. if (!IsCrypted())
  102. return false;
  103. bool result;
  104. {
  105. LOCK(cs_KeyStore);
  106. result = vMasterKey.empty();
  107. }
  108. return result;
  109. }
  110. bool Lock();
  111. virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
  112. bool AddKey(const CKey& key);
  113. bool HaveKey(const CKeyID &address) const
  114. {
  115. {
  116. LOCK(cs_KeyStore);
  117. if (!IsCrypted())
  118. return CBasicKeyStore::HaveKey(address);
  119. return mapCryptedKeys.count(address) > 0;
  120. }
  121. return false;
  122. }
  123. bool GetKey(const CKeyID &address, CKey& keyOut) const;
  124. bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
  125. void GetKeys(std::set<CKeyID> &setAddress) const
  126. {
  127. if (!IsCrypted())
  128. {
  129. CBasicKeyStore::GetKeys(setAddress);
  130. return;
  131. }
  132. setAddress.clear();
  133. CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
  134. while (mi != mapCryptedKeys.end())
  135. {
  136. setAddress.insert((*mi).first);
  137. mi++;
  138. }
  139. }
  140. boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
  141. };
  142. #endif