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