Fabcoin Core  0.16.2
P2P Digital Currency
KeyManager.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
22 #pragma once
23 
24 #include <functional>
25 #include <mutex>
26 #include <libdevcore/FileSystem.h>
27 #include <libdevcore/CommonData.h>
29 
30 namespace dev
31 {
32 namespace eth
33 {
34 class PasswordUnknown: public Exception {};
35 
36 struct KeyInfo
37 {
38  KeyInfo() = default;
39  KeyInfo(h256 const& _passHash, std::string const& _accountName, std::string const& _passwordHint = std::string()): passHash(_passHash), accountName(_accountName), passwordHint(_passwordHint) {}
40 
44  std::string accountName;
46  std::string passwordHint;
47 };
48 
49 static h256 const UnknownPassword;
51 static auto const DontKnowThrow = [](){ throw PasswordUnknown(); return std::string(); };
52 
53 enum class SemanticPassword
54 {
55  Existing,
56  Master
57 };
58 
59 // TODO: This one is specifically for Ethereum, but we can make it generic in due course.
60 // TODO: hidden-partition style key-store.
73 {
74 public:
75  enum class NewKeyType { DirectICAP = 0, NoVanity, FirstTwo, FirstTwoNextTwo, FirstThree, FirstFour };
76 
77  KeyManager(std::string const& _keysFile = defaultPath(), std::string const& _secretsPath = SecretStore::defaultPath());
78  ~KeyManager();
79 
80  void setSecretsPath(std::string const& _secretsPath) { m_store.setPath(_secretsPath); }
81  void setKeysFile(std::string const& _keysFile) { m_keysFile = _keysFile; }
82  std::string const& keysFile() const { return m_keysFile; }
83 
84  bool exists() const;
85  void create(std::string const& _pass);
86  bool load(std::string const& _pass);
87  void save(std::string const& _pass) const { write(_pass, m_keysFile); }
88 
89  void notePassword(std::string const& _pass) { m_cachedPasswords[hashPassword(_pass)] = _pass; }
90  void noteHint(std::string const& _pass, std::string const& _hint) { if (!_hint.empty()) m_passwordHint[hashPassword(_pass)] = _hint; }
91  bool haveHint(std::string const& _pass) const { auto h = hashPassword(_pass); return m_cachedPasswords.count(h) && !m_cachedPasswords.at(h).empty(); }
92 
94  Addresses accounts() const;
96  AddressHash accountsHash() const { return AddressHash() + accounts(); }
97  bool hasAccount(Address const& _address) const;
99  std::string const& accountName(Address const& _address) const;
101  std::string const& passwordHint(Address const& _address) const;
103  void changeName(Address const& _address, std::string const& _name);
104 
107  bool haveKey(Address const& _a) const { return m_addrLookup.count(_a); }
109  h128 uuid(Address const& _a) const;
111  Address address(h128 const& _uuid) const;
112 
113  h128 import(Secret const& _s, std::string const& _accountName, std::string const& _pass, std::string const& _passwordHint);
114  h128 import(Secret const& _s, std::string const& _accountName) { return import(_s, _accountName, defaultPassword(), std::string()); }
115  Address importBrain(std::string const& _seed, std::string const& _accountName, std::string const& _seedHint);
116  void importExistingBrain(Address const& _a, std::string const& _accountName, std::string const& _seedHint);
117 
118  SecretStore& store() { return m_store; }
119  void importExisting(h128 const& _uuid, std::string const& _accountName, std::string const& _pass, std::string const& _passwordHint);
120  void importExisting(h128 const& _uuid, std::string const& _accountName) { importExisting(_uuid, _accountName, defaultPassword(), std::string()); }
121  void importExisting(h128 const& _uuid, std::string const& _accountName, Address const& _addr, h256 const& _passHash = h256(), std::string const& _passwordHint = std::string());
122 
125  Secret secret(Address const& _address, std::function<std::string()> const& _pass = DontKnowThrow, bool _usePasswordCache = true) const;
128  Secret secret(h128 const& _uuid, std::function<std::string()> const& _pass = DontKnowThrow, bool _usePasswordCache = true) const;
129 
130  bool recode(Address const& _address, SemanticPassword _newPass, std::function<std::string()> const& _pass = DontKnowThrow, KDF _kdf = KDF::Scrypt);
131  bool recode(Address const& _address, std::string const& _newPass, std::string const& _hint, std::function<std::string()> const& _pass = DontKnowThrow, KDF _kdf = KDF::Scrypt);
132 
133  void kill(h128 const& _id) { kill(address(_id)); }
134  void kill(Address const& _a);
135 
136  static std::string defaultPath() { return getDataDir("ethereum") + "/keys.info"; }
137 
139  static KeyPair presaleSecret(std::string const& _json, std::function<std::string(bool)> const& _password);
140 
142  static Secret brain(std::string const& _seed);
143 
145  static Secret subkey(Secret const& _s, unsigned _index);
146 
148  static KeyPair newKeyPair(NewKeyType _type);
149 private:
150  std::string getPassword(h128 const& _uuid, std::function<std::string()> const& _pass = DontKnowThrow) const;
151  std::string getPassword(h256 const& _passHash, std::function<std::string()> const& _pass = DontKnowThrow) const;
152  std::string defaultPassword(std::function<std::string()> const& _pass = DontKnowThrow) const { return getPassword(m_master, _pass); }
153  h256 hashPassword(std::string const& _pass) const;
154 
156  void cachePassword(std::string const& _password) const;
157 
158  // Only use if previously loaded ok.
159  // @returns false if wasn't previously loaded ok.
160  bool write() const { return write(m_keysFile); }
161  bool write(std::string const& _keysFile) const;
162  void write(std::string const& _pass, std::string const& _keysFile) const; // TODO: all passwords should be a secure string.
163  void write(SecureFixedHash<16> const& _key, std::string const& _keysFile) const;
164 
165  // Ethereum keys.
166 
168  std::unordered_map<h128, Address> m_uuidLookup;
170  std::unordered_map<Address, h128> m_addrLookup;
172  std::unordered_map<Address, KeyInfo> m_keyInfo;
174  std::unordered_map<h256, std::string> m_passwordHint;
175 
176  // Passwords that we're storing. Mapping password hash -> password.
177  mutable std::unordered_map<h256, std::string> m_cachedPasswords;
178 
179  // DEPRECATED.
180  // Used to be the default password for keys in the keystore, stored in the keys file.
181  // Now the default password is based off the key of the keys file directly, so this is redundant
182  // except for the fact that people have existing keys stored with it. Leave for now until/unless
183  // we have an upgrade strategy.
185 
186  mutable std::string m_keysFile;
188  mutable h256 m_master;
190 };
191 
192 }
193 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
AddressHash accountsHash() const
Definition: KeyManager.h:96
#define function(a, b, c, d, k, s)
SecretStore m_store
Definition: KeyManager.h:189
std::string m_keysFile
Definition: KeyManager.h:186
#define h(i)
Definition: sha.cpp:736
void notePassword(std::string const &_pass)
Definition: KeyManager.h:89
void noteHint(std::string const &_pass, std::string const &_hint)
Definition: KeyManager.h:90
Simple class that represents a "key pair".
Definition: Common.h:150
std::string const & keysFile() const
Definition: KeyManager.h:82
bool write() const
Definition: KeyManager.h:160
std::string getDataDir(std::string _prefix="ethereum")
void setSecretsPath(std::string const &_secretsPath)
Definition: KeyManager.h:80
std::string accountName
Name of the key, or JSON key info if begins with &#39;{&#39;.
Definition: KeyManager.h:44
void setKeysFile(std::string const &_keysFile)
Definition: KeyManager.h:81
SemanticPassword
Definition: KeyManager.h:53
Base class for all exceptions.
Definition: Exceptions.h:39
High-level manager of password-encrypted keys for Ethereum.
Definition: KeyManager.h:72
std::string passwordHint
Hint of the password. Alternative place for storage than the hash-based lookup.
Definition: KeyManager.h:46
void kill(h128 const &_id)
Definition: KeyManager.h:133
static std::string defaultPath()
Definition: KeyManager.h:136
FixedHash< 32 > h256
Definition: FixedHash.h:340
void importExisting(h128 const &_uuid, std::string const &_accountName)
Definition: KeyManager.h:120
bool haveKey(Address const &_a) const
Definition: KeyManager.h:107
std::string m_defaultPasswordDeprecated
Definition: KeyManager.h:184
h160s Addresses
A vector of Ethereum addresses.
Definition: Common.h:68
void save(std::string const &_pass) const
Definition: KeyManager.h:87
h256 passHash
Hash of the password or h256() / UnknownPassword if unknown.
Definition: KeyManager.h:42
std::unordered_map< h256, std::string > m_passwordHint
Mapping password hash -> password hint.
Definition: KeyManager.h:174
SecureFixedHash< 16 > m_keysFileKey
Definition: KeyManager.h:187
Manages encrypted keys stored in a certain directory on disk.
Definition: SecretStore.h:46
std::unordered_map< Address, KeyInfo > m_keyInfo
Mapping address -> key info.
Definition: KeyManager.h:172
bool haveHint(std::string const &_pass) const
Definition: KeyManager.h:91
std::unordered_map< h256, std::string > m_cachedPasswords
Definition: KeyManager.h:177
std::string getPassword(std::string const &_prompt)
Requests the user to enter a password on the console.
Definition: CommonIO.cpp:135
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
KeyInfo(h256 const &_passHash, std::string const &_accountName, std::string const &_passwordHint=std::string())
Definition: KeyManager.h:39
std::unordered_map< Address, h128 > m_addrLookup
Mapping address -> key uuid.
Definition: KeyManager.h:170
static std::string defaultPath()
Definition: SecretStore.h:122
SecretStore & store()
Definition: KeyManager.h:118
std::string defaultPassword(std::function< std::string()> const &_pass=DontKnowThrow) const
Definition: KeyManager.h:152
std::unordered_map< h128, Address > m_uuidLookup
Mapping key uuid -> address.
Definition: KeyManager.h:168
std::unordered_set< h160 > AddressHash
A hash set of Ethereum addresses.
Definition: Common.h:71