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.

keystore.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Starwels 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 STARWELS_KEYSTORE_H
  6. #define STARWELS_KEYSTORE_H
  7. #include "key.h"
  8. #include "pubkey.h"
  9. #include "script/script.h"
  10. #include "script/standard.h"
  11. #include "sync.h"
  12. #include <boost/signals2/signal.hpp>
  13. /** A virtual base class for key stores */
  14. class CKeyStore
  15. {
  16. protected:
  17. mutable CCriticalSection cs_KeyStore;
  18. public:
  19. virtual ~CKeyStore() {}
  20. //! Add a key to the store.
  21. virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
  22. virtual bool AddKey(const CKey &key);
  23. //! Check whether a key corresponding to a given address is present in the store.
  24. virtual bool HaveKey(const CKeyID &address) const =0;
  25. virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
  26. virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
  27. virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const =0;
  28. //! Support for BIP 0013 : see https://github.com/starwels/bips/blob/master/bip-0013.mediawiki
  29. virtual bool AddCScript(const CScript& redeemScript) =0;
  30. virtual bool HaveCScript(const CScriptID &hash) const =0;
  31. virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
  32. //! Support for Watch-only addresses
  33. virtual bool AddWatchOnly(const CScript &dest) =0;
  34. virtual bool RemoveWatchOnly(const CScript &dest) =0;
  35. virtual bool HaveWatchOnly(const CScript &dest) const =0;
  36. virtual bool HaveWatchOnly() const =0;
  37. };
  38. typedef std::map<CKeyID, CKey> KeyMap;
  39. typedef std::map<CKeyID, CPubKey> WatchKeyMap;
  40. typedef std::map<CScriptID, CScript > ScriptMap;
  41. typedef std::set<CScript> WatchOnlySet;
  42. /** Basic key store, that keeps keys in an address->secret map */
  43. class CBasicKeyStore : public CKeyStore
  44. {
  45. protected:
  46. KeyMap mapKeys;
  47. WatchKeyMap mapWatchKeys;
  48. ScriptMap mapScripts;
  49. WatchOnlySet setWatchOnly;
  50. public:
  51. bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
  52. bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
  53. bool HaveKey(const CKeyID &address) const override
  54. {
  55. bool result;
  56. {
  57. LOCK(cs_KeyStore);
  58. result = (mapKeys.count(address) > 0);
  59. }
  60. return result;
  61. }
  62. void GetKeys(std::set<CKeyID> &setAddress) const override
  63. {
  64. setAddress.clear();
  65. {
  66. LOCK(cs_KeyStore);
  67. KeyMap::const_iterator mi = mapKeys.begin();
  68. while (mi != mapKeys.end())
  69. {
  70. setAddress.insert((*mi).first);
  71. mi++;
  72. }
  73. }
  74. }
  75. bool GetKey(const CKeyID &address, CKey &keyOut) const override
  76. {
  77. {
  78. LOCK(cs_KeyStore);
  79. KeyMap::const_iterator mi = mapKeys.find(address);
  80. if (mi != mapKeys.end())
  81. {
  82. keyOut = mi->second;
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. virtual bool AddCScript(const CScript& redeemScript) override;
  89. virtual bool HaveCScript(const CScriptID &hash) const override;
  90. virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
  91. virtual bool AddWatchOnly(const CScript &dest) override;
  92. virtual bool RemoveWatchOnly(const CScript &dest) override;
  93. virtual bool HaveWatchOnly(const CScript &dest) const override;
  94. virtual bool HaveWatchOnly() const override;
  95. };
  96. typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
  97. typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
  98. #endif // STARWELS_KEYSTORE_H