Merge #9993: Initialize nRelockTime
[bitcoinplatinum.git] / src / keystore.h
blobd9290722e1b1aae500d8e9a0363a22a77d3233ee
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>
16 #include <boost/variant.hpp>
18 /** A virtual base class for key stores */
19 class CKeyStore
21 protected:
22 mutable CCriticalSection cs_KeyStore;
24 public:
25 virtual ~CKeyStore() {}
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);
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;
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;
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;
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;
54 /** Basic key store, that keeps keys in an address->secret map */
55 class CBasicKeyStore : public CKeyStore
57 protected:
58 KeyMap mapKeys;
59 WatchKeyMap mapWatchKeys;
60 ScriptMap mapScripts;
61 WatchOnlySet setWatchOnly;
63 public:
64 bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
65 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
66 bool HaveKey(const CKeyID &address) const
68 bool result;
70 LOCK(cs_KeyStore);
71 result = (mapKeys.count(address) > 0);
73 return result;
75 void GetKeys(std::set<CKeyID> &setAddress) const
77 setAddress.clear();
79 LOCK(cs_KeyStore);
80 KeyMap::const_iterator mi = mapKeys.begin();
81 while (mi != mapKeys.end())
83 setAddress.insert((*mi).first);
84 mi++;
88 bool GetKey(const CKeyID &address, CKey &keyOut) const
91 LOCK(cs_KeyStore);
92 KeyMap::const_iterator mi = mapKeys.find(address);
93 if (mi != mapKeys.end())
95 keyOut = mi->second;
96 return true;
99 return false;
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;
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;
111 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
112 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
114 #endif // BITCOIN_KEYSTORE_H