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

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