Ethereum  PoC-8
The C++ Implementation of Ethereum
SecretStore.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/FixedHash.h>
27 #include <libdevcore/FileSystem.h>
28 #include "Common.h"
29 
30 #include <boost/filesystem.hpp>
31 
32 namespace dev
33 {
34 
35 enum class KDF {
37  Scrypt,
38 };
39 
49 {
50 public:
51  struct EncryptedKey
52  {
53  std::string encryptedKey;
54  boost::filesystem::path filename;
56  };
57 
60  SecretStore() = default;
61 
63  SecretStore(boost::filesystem::path const& _path);
64 
66  void setPath(boost::filesystem::path const& _path);
67 
71  bytesSec secret(h128 const& _uuid, std::function<std::string()> const& _pass, bool _useCache = true) const;
74  static bytesSec secret(std::string const& _content, std::string const& _pass);
77  bytesSec secret(Address const& _address, std::function<std::string()> const& _pass) const;
79  h128 importKey(std::string const& _file) { auto ret = readKey(_file, false); if (ret) save(); return ret; }
82  h128 importKeyContent(std::string const& _content) { auto ret = readKeyContent(_content, std::string()); if (ret) save(); return ret; }
85  h128 importSecret(bytesSec const& _s, std::string const& _pass);
86  h128 importSecret(bytesConstRef _s, std::string const& _pass);
88  bool recode(h128 const& _uuid, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt);
90  bool recode(Address const& _address, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt);
92  void kill(h128 const& _uuid);
93 
95  std::vector<h128> keys() const { return keysOf(m_keys); }
96 
98  bool contains(h128 const& _k) const { return m_keys.count(_k); }
99 
102  void clearCache() const;
103 
107  h128 readKey(boost::filesystem::path const& _file, bool _takeFileOwnership);
112  h128 readKeyContent(std::string const& _content, boost::filesystem::path const& _file = boost::filesystem::path());
113 
115  void save(boost::filesystem::path const& _keysPath);
117  void save() { save(m_path); }
119  bool noteAddress(h128 const& _uuid, Address const& _address);
121  Address address(h128 const& _uuid) const { return m_keys.at(_uuid).address; }
122 
124  static boost::filesystem::path defaultPath() { return getDataDir("web3") / boost::filesystem::path("keys"); }
125 
126 private:
128  void load(boost::filesystem::path const& _keysPath);
129  void load() { load(m_path); }
131  static std::string encrypt(bytesConstRef _v, std::string const& _pass, KDF _kdf = KDF::Scrypt);
133  static bytesSec decrypt(std::string const& _v, std::string const& _pass);
135  std::pair<h128 const, EncryptedKey> const* key(Address const& _address) const;
136  std::pair<h128 const, EncryptedKey>* key(Address const& _address);
138  mutable std::unordered_map<h128, bytesSec> m_cached;
140  std::unordered_map<h128, EncryptedKey> m_keys;
141 
142  boost::filesystem::path m_path;
143 };
144 
145 }
146 
dev::SecretStore::save
void save()
Store all keys in the managed directory.
Definition: SecretStore.h:117
dev::SecretStore::importKey
h128 importKey(std::string const &_file)
Imports the (encrypted) key stored in the file _file and copies it to the managed directory.
Definition: SecretStore.h:79
dev::vector_ref< byte const >
dev::SecretStore::EncryptedKey::encryptedKey
std::string encryptedKey
Definition: SecretStore.h:53
dev::SecretStore::noteAddress
bool noteAddress(h128 const &_uuid, Address const &_address)
Definition: SecretStore.cpp:205
dev::KDF
KDF
Definition: SecretStore.h:35
dev::SecretStore::EncryptedKey::filename
boost::filesystem::path filename
Definition: SecretStore.h:54
FixedHash.h
dev::SecretStore::EncryptedKey::address
Address address
Definition: SecretStore.h:55
dev::FixedHash< 20 >
dev::SecretStore::recode
bool recode(Address const &_address, std::string const &_newPass, std::function< std::string()> const &_pass, KDF _kdf=KDF::Scrypt)
Decrypts and re-encrypts the key identified by _address.
dev::SecretStore::keys
std::vector< h128 > keys() const
Returns the uuids of all stored keys.
Definition: SecretStore.h:95
Common.h
dev::secure_vector
Definition: Common.h:78
dev::SecretStore::readKey
h128 readKey(boost::filesystem::path const &_file, bool _takeFileOwnership)
Definition: SecretStore.cpp:227
dev::SecretStore::defaultPath
static boost::filesystem::path defaultPath()
Definition: SecretStore.h:124
dev::KDF::Scrypt
@ Scrypt
dev::SecretStore::importKeyContent
h128 importKeyContent(std::string const &_content)
Definition: SecretStore.h:82
dev::bytesConstRef
vector_ref< byte const > bytesConstRef
Definition: Common.h:74
dev::SecretStore::importSecret
h128 importSecret(bytesSec const &_s, std::string const &_pass)
dev::getDataDir
boost::filesystem::path getDataDir(std::string _prefix="ethereum")
dev::SecretStore::contains
bool contains(h128 const &_k) const
Definition: SecretStore.h:98
FileSystem.h
dev::bytesSec
secure_vector< byte > bytesSec
Definition: Common.h:115
dev::SecretStore::EncryptedKey
Definition: SecretStore.h:52
dev::SecretStore::kill
void kill(h128 const &_uuid)
Removes the key specified by _uuid from both memory and disk.
Definition: SecretStore.cpp:168
dev::keysOf
std::vector< T > keysOf(std::map< T, U > const &_m)
Definition: CommonData.h:286
dev::SecretStore::save
void save(boost::filesystem::path const &_keysPath)
Store all keys in the directory _keysPath.
dev::SecretStore::setPath
void setPath(boost::filesystem::path const &_path)
Set a path for finding secrets.
Definition: SecretStore.cpp:99
dev::SecretStore::secret
bytesSec secret(Address const &_address, std::function< std::string()> const &_pass) const
dev
Definition: Address.cpp:21
dev::SecretStore::clearCache
void clearCache() const
Definition: SecretStore.cpp:178
dev::SecretStore::SecretStore
SecretStore()=default
dev::SecretStore::secret
static bytesSec secret(std::string const &_content, std::string const &_pass)
dev::SecretStore::importSecret
h128 importSecret(bytesConstRef _s, std::string const &_pass)
dev::SecretStore::SecretStore
SecretStore(boost::filesystem::path const &_path)
Construct a new SecretStore and read all keys in the given directory.
dev::SecretStore::readKeyContent
h128 readKeyContent(std::string const &_content, boost::filesystem::path const &_file=boost::filesystem::path())
Definition: SecretStore.cpp:233
dev::SecretStore
Definition: SecretStore.h:49
dev::Address
h160 Address
Definition: Address.h:30
dev::SecretStore::address
Address address(h128 const &_uuid) const
Definition: SecretStore.h:121
dev::SecretStore::recode
bool recode(h128 const &_uuid, std::string const &_newPass, std::function< std::string()> const &_pass, KDF _kdf=KDF::Scrypt)
Decrypts and re-encrypts the key identified by _uuid.
dev::KDF::PBKDF2_SHA256
@ PBKDF2_SHA256
dev::SecretStore::secret
bytesSec secret(h128 const &_uuid, std::function< std::string()> const &_pass, bool _useCache=true) const