rpc: add getnetworkinfo
[novacoin.git] / src / keystore.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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.
5 #ifndef BITCOIN_KEYSTORE_H
6 #define BITCOIN_KEYSTORE_H
7
8 #include "crypter.h"
9 #include "sync.h"
10 #include <boost/signals2/signal.hpp>
11 #include <boost/variant.hpp>
12
13 class CScript;
14
15 class CNoDestination {
16 public:
17     friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
18     friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
19 };
20
21 /** A txout script template with a specific destination. It is either:
22   * CNoDestination: no destination set
23   * CKeyID: TX_PUBKEYHASH destination
24   * CScriptID: TX_SCRIPTHASH destination
25   *
26   * A CTxDestination is the internal data type encoded in a CBitcoinAddress.
27   */
28 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
29
30 /** A virtual base class for key stores */
31 class CKeyStore
32 {
33 protected:
34     mutable CCriticalSection cs_KeyStore;
35
36 public:
37     virtual ~CKeyStore() {}
38
39     // Add a key to the store.
40     virtual bool AddKey(const CKey& key) =0;
41
42     // Add a malleable key to store.
43     virtual bool AddMalleableKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) =0;
44     virtual bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const =0;
45
46     // Check whether a key corresponding to a given address is present in the store.
47     virtual bool HaveKey(const CKeyID &address) const =0;
48     virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
49     virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
50     virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
51
52     // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
53     virtual bool AddCScript(const CScript& redeemScript) =0;
54     virtual bool HaveCScript(const CScriptID &hash) const =0;
55     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
56
57     // Support for Watch-only addresses
58     virtual bool AddWatchOnly(const CScript &dest) =0;
59     virtual bool RemoveWatchOnly(const CScript &dest) =0;
60     virtual bool HaveWatchOnly(const CScript &dest) const =0;
61     virtual bool HaveWatchOnly() const =0;
62
63     virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const;
64
65     virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
66     virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const =0;
67     virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
68     virtual void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const =0;
69 };
70
71 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
72 typedef std::map<CScriptID, CScript > ScriptMap;
73 typedef std::set<CScript> WatchOnlySet;
74 typedef std::map<CMalleableKeyView, CSecret> MalleableKeyMap;
75
76 /** Basic key store, that keeps keys in an address->secret map */
77 class CBasicKeyStore : public CKeyStore
78 {
79 protected:
80     KeyMap mapKeys;
81     MalleableKeyMap mapMalleableKeys;
82
83     ScriptMap mapScripts;
84     WatchOnlySet setWatchOnly;
85
86 public:
87     bool AddKey(const CKey& key);
88     bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
89     bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
90     bool HaveKey(const CKeyID &address) const;
91     void GetKeys(std::set<CKeyID> &setAddress) const;
92     bool GetKey(const CKeyID &address, CKey &keyOut) const;
93     virtual bool AddCScript(const CScript& redeemScript);
94     virtual bool HaveCScript(const CScriptID &hash) const;
95     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
96
97     virtual bool AddWatchOnly(const CScript &dest);
98     virtual bool RemoveWatchOnly(const CScript &dest);
99     virtual bool HaveWatchOnly(const CScript &dest) const;
100     virtual bool HaveWatchOnly() const;
101
102     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const;
103     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const;
104     bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
105     void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const;
106     bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view);
107 };
108
109 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
110 typedef std::map<CMalleableKeyView, std::vector<unsigned char> > CryptedMalleableKeyMap;
111
112 /** Keystore which keeps the private keys encrypted.
113  * It derives from the basic key store, which is used if no encryption is active.
114  */
115 class CCryptoKeyStore : public CBasicKeyStore
116 {
117 private:
118     CryptedKeyMap mapCryptedKeys;
119     CryptedMalleableKeyMap mapCryptedMalleableKeys;
120
121     CKeyingMaterial vMasterKey;
122
123     // if fUseCrypto is true, mapKeys must be empty
124     // if fUseCrypto is false, vMasterKey must be empty
125     bool fUseCrypto;
126
127     // keeps track of whether Unlock has run a thorough check before
128     bool fDecryptionThoroughlyChecked;
129
130 protected:
131     bool SetCrypted();
132
133     // will encrypt previously unencrypted keys
134     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
135     bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
136
137     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
138
139 public:
140     CCryptoKeyStore();
141
142     bool IsCrypted() const;
143     bool IsLocked() const;
144     bool Lock();
145
146     virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
147     virtual bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char>  &vchCryptedSecretH);
148
149     bool AddKey(const CKey& key);
150     bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
151     bool HaveKey(const CKeyID &address) const;
152     bool GetKey(const CKeyID &address, CKey& keyOut) const;
153     bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
154     void GetKeys(std::set<CKeyID> &setAddress) const;
155     bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
156     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const;
157     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const;
158     bool CheckOwnership(const CMalleablePubKey &mpk);
159     bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
160     void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const;
161     bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view);
162     /* Wallet status (encrypted, locked) changed.
163      * Note: Called without locks held.
164      */
165     boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
166 };
167
168 #endif