Fix make_change to not create half-satoshis
[bitcoinplatinum.git] / src / keystore.h
blob3b49e282d96040b3e770e4ea3ec3ce41dbe11b08
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 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 "sync.h"
12 #include <boost/signals2/signal.hpp>
13 #include <boost/variant.hpp>
15 class CScript;
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 void GetKeys(std::set<CKeyID> &setAddress) const =0;
34 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
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 HaveWatchOnly(const CScript &dest) const =0;
44 virtual bool HaveWatchOnly() const =0;
47 typedef std::map<CKeyID, CKey> KeyMap;
48 typedef std::map<CScriptID, CScript > ScriptMap;
49 typedef std::set<CScript> WatchOnlySet;
51 /** Basic key store, that keeps keys in an address->secret map */
52 class CBasicKeyStore : public CKeyStore
54 protected:
55 KeyMap mapKeys;
56 ScriptMap mapScripts;
57 WatchOnlySet setWatchOnly;
59 public:
60 bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
61 bool HaveKey(const CKeyID &address) const
63 bool result;
65 LOCK(cs_KeyStore);
66 result = (mapKeys.count(address) > 0);
68 return result;
70 void GetKeys(std::set<CKeyID> &setAddress) const
72 setAddress.clear();
74 LOCK(cs_KeyStore);
75 KeyMap::const_iterator mi = mapKeys.begin();
76 while (mi != mapKeys.end())
78 setAddress.insert((*mi).first);
79 mi++;
83 bool GetKey(const CKeyID &address, CKey &keyOut) const
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 virtual bool AddCScript(const CScript& redeemScript);
97 virtual bool HaveCScript(const CScriptID &hash) const;
98 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
100 virtual bool AddWatchOnly(const CScript &dest);
101 virtual bool HaveWatchOnly(const CScript &dest) const;
102 virtual bool HaveWatchOnly() const;
105 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
106 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
108 #endif // BITCOIN_KEYSTORE_H