-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwallet_manager.cpp
More file actions
312 lines (270 loc) · 10.5 KB
/
wallet_manager.cpp
File metadata and controls
312 lines (270 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <appbase/application.hpp>
#include <eosio/wallet_plugin/wallet_manager.hpp>
#include <eosio/wallet_plugin/wallet.hpp>
#include <eosio/wallet_plugin/se_wallet.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/to_string.hpp>
#include <boost/algorithm/string.hpp>
namespace eosio {
namespace wallet {
constexpr auto file_ext = ".wallet";
constexpr auto password_prefix = "PW";
std::string gen_password() {
auto key = private_key_type::generate();
return password_prefix + key.to_string();
}
bool valid_filename(const string& name) {
if (name.empty()) return false;
if (std::find_if(name.begin(), name.end(), !boost::algorithm::is_alnum() && !boost::algorithm::is_any_of("._-")) != name.end()) return false;
return boost::filesystem::path(name).filename().string() == name;
}
wallet_manager::wallet_manager() {
#ifdef __APPLE__
try {
wallets.emplace("SecureEnclave", std::make_unique<se_wallet>());
} catch(const std::exception& ) {}
#endif
}
wallet_manager::~wallet_manager() {
//not really required, but may spook users
if(wallet_dir_lock)
boost::filesystem::remove(lock_path);
}
void wallet_manager::set_timeout(const std::chrono::seconds& t) {
timeout = t;
auto now = std::chrono::system_clock::now();
timeout_time = now + timeout;
EOS_ASSERT(timeout_time >= now && timeout_time.time_since_epoch().count() > 0, invalid_lock_timeout_exception, "Overflow on timeout_time, specified {t}, now {now}, timeout_time {timeout_time}",
("t", t.count())("now", now.time_since_epoch().count())("timeout_time", timeout_time.time_since_epoch().count()));
}
void wallet_manager::check_timeout() {
if (timeout_time != timepoint_t::max()) {
const auto& now = std::chrono::system_clock::now();
if (now >= timeout_time) {
lock_all();
}
timeout_time = now + timeout;
}
}
std::string wallet_manager::create(const std::string& name) {
check_timeout();
EOS_ASSERT(valid_filename(name), wallet_exception, "Invalid filename, path not allowed in wallet name {n}", ("n", name));
auto wallet_filename = dir / (name + file_ext);
if (fc::exists(wallet_filename)) {
EOS_THROW(chain::wallet_exist_exception, "Wallet with name: '{n}' already exists at {path}", ("n", name)("path",fc::path(wallet_filename).filename().generic_string()));
}
std::string password = gen_password();
wallet_data d;
auto wallet = make_unique<soft_wallet>(d);
wallet->set_password(password);
wallet->set_wallet_filename(wallet_filename.string());
wallet->unlock(password);
wallet->lock();
wallet->unlock(password);
// Explicitly save the wallet file here, to ensure it now exists.
wallet->save_wallet_file();
// If we have name in our map then remove it since we want the emplace below to replace.
// This can happen if the wallet file is removed while eos-walletd is running.
auto it = wallets.find(name);
if (it != wallets.end()) {
wallets.erase(it);
}
wallets.emplace(name, std::move(wallet));
return password;
}
void wallet_manager::open(const std::string& name) {
check_timeout();
EOS_ASSERT(valid_filename(name), wallet_exception, "Invalid filename, path not allowed in wallet name {n}", ("n", name));
wallet_data d;
auto wallet = std::make_unique<soft_wallet>(d);
auto wallet_filename = dir / (name + file_ext);
wallet->set_wallet_filename(wallet_filename.string());
if (!wallet->load_wallet_file()) {
EOS_THROW(chain::wallet_nonexistent_exception, "Unable to open file: {f}", ("f", wallet_filename.string()));
}
// If we have name in our map then remove it since we want the emplace below to replace.
// This can happen if the wallet file is added while eos-walletd is running.
auto it = wallets.find(name);
if (it != wallets.end()) {
wallets.erase(it);
}
wallets.emplace(name, std::move(wallet));
}
std::vector<std::string> wallet_manager::list_wallets() {
check_timeout();
std::vector<std::string> result;
for (const auto& i : wallets) {
if (i.second->is_locked()) {
result.emplace_back(i.first);
} else {
result.emplace_back(i.first + " *");
}
}
return result;
}
map<public_key_type,private_key_type> wallet_manager::list_keys(const string& name, const string& pw) {
check_timeout();
if (wallets.count(name) == 0)
EOS_THROW(chain::wallet_nonexistent_exception, "Wallet not found: {w}", ("w", name));
auto& w = wallets.at(name);
if (w->is_locked())
EOS_THROW(chain::wallet_locked_exception, "Wallet is locked: {w}", ("w", name));
w->check_password(pw); //throws if bad password
return w->list_keys();
}
flat_set<public_key_type> wallet_manager::get_public_keys() {
check_timeout();
EOS_ASSERT(!wallets.empty(), wallet_not_available_exception, "You don't have any wallet!");
flat_set<public_key_type> result;
bool is_all_wallet_locked = true;
for (const auto& i : wallets) {
if (!i.second->is_locked()) {
result.merge(i.second->list_public_keys());
}
is_all_wallet_locked &= i.second->is_locked();
}
EOS_ASSERT(!is_all_wallet_locked, wallet_locked_exception, "You don't have any unlocked wallet!");
return result;
}
void wallet_manager::lock_all() {
// no call to check_timeout since we are locking all anyway
for (auto& i : wallets) {
if (!i.second->is_locked()) {
i.second->lock();
}
}
}
void wallet_manager::lock(const std::string& name) {
check_timeout();
if (wallets.count(name) == 0) {
EOS_THROW(chain::wallet_nonexistent_exception, "Wallet not found: {w}", ("w", name));
}
auto& w = wallets.at(name);
if (w->is_locked()) {
return;
}
w->lock();
}
void wallet_manager::unlock(const std::string& name, const std::string& password) {
check_timeout();
if (wallets.count(name) == 0) {
open( name );
}
auto& w = wallets.at(name);
if (!w->is_locked()) {
EOS_THROW(chain::wallet_unlocked_exception, "Wallet is already unlocked: {w}", ("w", name));
return;
}
w->unlock(password);
}
void wallet_manager::import_key(const std::string& name, const std::string& wif_key) {
check_timeout();
if (wallets.count(name) == 0) {
EOS_THROW(chain::wallet_nonexistent_exception, "Wallet not found: {w}", ("w", name));
}
auto& w = wallets.at(name);
if (w->is_locked()) {
EOS_THROW(chain::wallet_locked_exception, "Wallet is locked: {w}", ("w", name));
}
w->import_key(wif_key);
}
void wallet_manager::remove_key(const std::string& name, const std::string& password, const std::string& key) {
check_timeout();
if (wallets.count(name) == 0) {
EOS_THROW(chain::wallet_nonexistent_exception, "Wallet not found: {w}", ("w", name));
}
auto& w = wallets.at(name);
if (w->is_locked()) {
EOS_THROW(chain::wallet_locked_exception, "Wallet is locked: {w}", ("w", name));
}
w->check_password(password); //throws if bad password
w->remove_key(key);
}
string wallet_manager::create_key(const std::string& name, const std::string& key_type) {
check_timeout();
if (wallets.count(name) == 0) {
EOS_THROW(chain::wallet_nonexistent_exception, "Wallet not found: {w}", ("w", name));
}
auto& w = wallets.at(name);
if (w->is_locked()) {
EOS_THROW(chain::wallet_locked_exception, "Wallet is locked: {w}", ("w", name));
}
string upper_key_type = boost::to_upper_copy<std::string>(key_type);
return w->create_key(upper_key_type);
}
chain::signed_transaction
wallet_manager::sign_transaction(const chain::signed_transaction& txn, const flat_set<public_key_type>& keys, const chain::chain_id_type& id) {
check_timeout();
chain::signed_transaction stxn(txn);
for (const auto& pk : keys) {
bool found = false;
for (const auto& i : wallets) {
if (!i.second->is_locked()) {
std::optional<signature_type> sig = i.second->try_sign_digest(stxn.sig_digest(id, stxn.context_free_data), pk);
if (sig) {
stxn.signatures.push_back(*sig);
found = true;
break; // inner for
}
}
}
if (!found) {
EOS_THROW(chain::wallet_missing_pub_key_exception, "Public key not found in unlocked wallets {k}", ("k", pk.to_string()));
}
}
return stxn;
}
chain::signature_type
wallet_manager::sign_digest(const chain::digest_type& digest, const public_key_type& key) {
check_timeout();
try {
for (const auto& i : wallets) {
if (!i.second->is_locked()) {
std::optional<signature_type> sig = i.second->try_sign_digest(digest, key);
if (sig)
return *sig;
}
}
} FC_LOG_AND_RETHROW();
EOS_THROW(chain::wallet_missing_pub_key_exception, "Public key not found in unlocked wallets {k}", ("k", key.to_string()));
}
void wallet_manager::own_and_use_wallet(const string& name, std::unique_ptr<wallet_api>&& wallet) {
if(wallets.find(name) != wallets.end())
EOS_THROW(wallet_exception, "Tried to use wallet name that already exists.");
wallets.emplace(name, std::move(wallet));
}
void wallet_manager::start_lock_watch(std::shared_ptr<boost::asio::deadline_timer> t)
{
t->async_wait([t, this](const boost::system::error_code& /*ec*/)
{
namespace bfs = boost::filesystem;
boost::system::error_code ec;
auto rc = bfs::status(lock_path, ec);
if(ec != boost::system::error_code()) {
if(rc.type() == bfs::file_not_found) {
appbase::app().quit();
EOS_THROW(wallet_exception, "Lock file removed while keosd still running. Terminating.");
}
}
t->expires_from_now(boost::posix_time::seconds(1));
start_lock_watch(t);
});
}
void wallet_manager::initialize_lock() {
//This is technically somewhat racy in here -- if multiple keosd are in this function at once.
//I've considered that an acceptable tradeoff to maintain cross-platform boost constructs here
lock_path = dir / "wallet.lock";
{
std::ofstream x(lock_path.string());
EOS_ASSERT(!x.fail(), wallet_exception, "Failed to open wallet lock file at {f}", ("f", lock_path.string()));
}
wallet_dir_lock = std::make_unique<boost::interprocess::file_lock>(lock_path.string().c_str());
if(!wallet_dir_lock->try_lock()) {
wallet_dir_lock.reset();
EOS_THROW(wallet_exception, "Failed to lock access to wallet directory; is another keosd running?");
}
auto timer = std::make_shared<boost::asio::deadline_timer>(appbase::app().get_io_service(), boost::posix_time::seconds(1));
start_lock_watch(timer);
}
} // namespace wallet
} // namespace eosio