Use unique_ptr for pdbCopy (Db) and fix potential memory leak
[bitcoinplatinum.git] / src / keystore.h
blob9b85ddb0ecb113b97a8a373281abcb678a31caa2
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.
6 #ifndef BITCOIN_KEYSTORE_H
7 #define BITCOIN_KEYSTORE_H
9 #include "key.h"
10 #include "pubkey.h"
11 #include "script/script.h"
12 #include "script/standard.h"
13 #include "sync.h"
15 #include <boost/signals2/signal.hpp>
17 /** A virtual base class for key stores */
18 class CKeyStore
20 protected:
21 mutable CCriticalSection cs_KeyStore;
23 public:
24 virtual ~CKeyStore() {}
26 //! Add a key to the store.
27 virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
28 virtual bool AddKey(const CKey &key);
30 //! Check whether a key corresponding to a given address is present in the store.
31 virtual bool HaveKey(const CKeyID &address) const =0;
32 virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
33 virtual std::set<CKeyID> GetKeys() const =0;
34 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const =0;
36 //! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
37 virtual bool AddCScript(const CScript& redeemScript) =0;
38 virtual bool HaveCScript(const CScriptID &hash) const =0;
39 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
41 //! Support for Watch-only addresses
42 virtual bool AddWatchOnly(const CScript &dest) =0;
43 virtual bool RemoveWatchOnly(const CScript &dest) =0;
44 virtual bool HaveWatchOnly(const CScript &dest) const =0;
45 virtual bool HaveWatchOnly() const =0;
48 typedef std::map<CKeyID, CKey> KeyMap;
49 typedef std::map<CKeyID, CPubKey> WatchKeyMap;
50 typedef std::map<CScriptID, CScript > ScriptMap;
51 typedef std::set<CScript> WatchOnlySet;
53 /** Basic key store, that keeps keys in an address->secret map */
54 class CBasicKeyStore : public CKeyStore
56 protected:
57 KeyMap mapKeys;
58 WatchKeyMap mapWatchKeys;
59 ScriptMap mapScripts;
60 WatchOnlySet setWatchOnly;
62 public:
63 bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
64 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
65 bool HaveKey(const CKeyID &address) const override
67 bool result;
69 LOCK(cs_KeyStore);
70 result = (mapKeys.count(address) > 0);
72 return result;
74 std::set<CKeyID> GetKeys() const override
76 LOCK(cs_KeyStore);
77 std::set<CKeyID> set_address;
78 for (const auto& mi : mapKeys) {
79 set_address.insert(mi.first);
81 return set_address;
83 bool GetKey(const CKeyID &address, CKey &keyOut) const override
86 LOCK(cs_KeyStore);
87 KeyMap::const_iterator mi = mapKeys.find(address);
88 if (mi != mapKeys.end())
90 keyOut = mi->second;
91 return true;
94 return false;
96 bool AddCScript(const CScript& redeemScript) override;
97 bool HaveCScript(const CScriptID &hash) const override;
98 bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
100 bool AddWatchOnly(const CScript &dest) override;
101 bool RemoveWatchOnly(const CScript &dest) override;
102 bool HaveWatchOnly(const CScript &dest) const override;
103 bool HaveWatchOnly() const override;
106 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
107 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
109 #endif // BITCOIN_KEYSTORE_H