forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountManager.cpp
249 lines (239 loc) · 6.8 KB
/
AccountManager.cpp
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
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file AccountManager.cpp
* @author Yann <yann@ethdev.com>
* @date 2016
*/
#include <libdevcore/SHA3.h>
#include <libdevcore/FileSystem.h>
#include <libethcore/ICAP.h>
#include <libethcore/KeyManager.h>
#include <libdevcore/easylog.h>
#include "AccountManager.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
void AccountManager::streamAccountHelp(ostream& _out)
{
_out
<< " account list List all keys available in wallet." << endl
<< " account new Create a new key and add it to the wallet." << endl
<< " account update [<uuid>|<address> , ... ] Decrypt and re-encrypt given keys." << endl
<< " account import [<uuid>|<file>|<secret-hex>] Import keys from given source and place in wallet." << endl;
}
void AccountManager::streamWalletHelp(ostream& _out)
{
_out
<< " wallet import <file> Import a presale wallet." << endl;
}
bool AccountManager::execute(int argc, char** argv)
{
if (string(argv[1]) == "wallet")
{
if (3 < argc && string(argv[2]) == "import")
{
if (!openWallet())
return false;
string file = argv[3];
string name = "presale wallet";
string pw;
KeyPair k;
try
{
k = m_keyManager->presaleSecret(
contentsString(file),
[&](bool){ return (pw = getPassword("Enter the passphrase for the presale key: "));}
);
}
catch (Exception const& _e)
{
if (auto err = boost::get_error_info<errinfo_comment>(_e))
cout << " Decryption failed: " << *err << endl;
else
cout << " Decryption failed: Unknown reason." << endl;
return false;
}
m_keyManager->import(k.secret(), name, pw, "Same passphrase as used for presale key");
cout << " Address: {" << k.address().hex() << "}" << endl;
}
else
streamWalletHelp(cout);
return true;
}
else if (string(argv[1]) == "account")
{
if (argc < 3 || string(argv[2]) == "list")
{
openWallet();
if (m_keyManager->store().keys().empty())
cout << "No keys found." << endl;
else
{
vector<u128> bare;
AddressHash got;
int k = 0;
for (auto const& u: m_keyManager->store().keys())
{
if (Address a = m_keyManager->address(u))
{
got.insert(a);
cout << "Account #" << k << ": {" << a.hex() << "}" << endl;
k++;
}
else
bare.push_back(u);
}
for (auto const& a: m_keyManager->accounts())
if (!got.count(a))
{
cout << "Account #" << k << ": {" << a.hex() << "}" << " (Brain)" << endl;
k++;
}
for (auto const& u: bare)
{
cout << "Account #" << k << ": " << toUUID(u) << " (Bare)" << endl;
k++;
}
}
}
else if (2 < argc && string(argv[2]) == "new")
{
openWallet();
string name;
string lock;
string lockHint;
lock = createPassword("Enter a passphrase with which to secure this account:");
auto k = makeKey();
h128 u = m_keyManager->import(k.secret(), name, lock, lockHint);
cout << "Created key " << toUUID(u) << endl;
cout << " ICAP: " << ICAP(k.address()).encoded() << endl;
cout << " Address: {" << k.address().hex() << "}" << endl;
}
else if (3 < argc && string(argv[2]) == "import")
{
openWallet();
h128 u = m_keyManager->store().importKey(argv[3]);
if (!u)
{
LOG(ERROR) << "Error: reading key file failed" << endl;
return false;
}
string pw;
bytesSec s = m_keyManager->store().secret(u, [&](){ return (pw = getPassword("Enter the passphrase for the key: ")); });
if (s.empty())
{
LOG(ERROR) << "Error: couldn't decode key or invalid secret size." << endl;
return false;
}
else
{
string lockHint;
string name;
m_keyManager->importExisting(u, name, pw, lockHint);
auto a = m_keyManager->address(u);
cout << "Imported key " << toUUID(u) << endl;
cout << " ICAP: " << ICAP(a).encoded() << endl;
cout << " Address: {" << a.hex() << "}" << endl;
}
}
else if (3 < argc && string(argv[2]) == "update")
{
openWallet();
for (int k = 3; k < argc; k++)
{
string i = argv[k];
h128 u = fromUUID(i);
if (isHex(i) || u != h128())
{
string newP = createPassword("Enter the new passphrase for the account " + i);
auto oldP = [&](){ return getPassword("Enter the current passphrase for the account " + i + ": "); };
bool recoded = false;
if (isHex(i))
{
recoded = m_keyManager->store().recode(
Address(i),
newP,
oldP,
dev::KDF::Scrypt
);
}
else if (u != h128())
{
recoded = m_keyManager->store().recode(
u,
newP,
oldP,
dev::KDF::Scrypt
);
}
if (recoded)
LOG(ERROR) << "Re-encoded " << i << endl;
else
LOG(ERROR) << "Couldn't re-encode " << i << "; key does not exist, corrupt or incorrect passphrase supplied." << endl;
}
else
LOG(ERROR) << "Couldn't re-encode " << i << "; does not represent an address or uuid." << endl;
}
}
else
streamAccountHelp(cout);
return true;
}
else
return false;
}
string AccountManager::createPassword(string const& _prompt) const
{
string ret;
while (true)
{
ret = getPassword(_prompt);
string confirm = getPassword("Please confirm the passphrase by entering it again: ");
if (ret == confirm)
break;
cout << "Passwords were different. Try again." << endl;
}
return ret;
}
KeyPair AccountManager::makeKey() const
{
bool icap = true;
KeyPair k(Secret::random());
while (icap && k.address()[0])
k = KeyPair(Secret(sha3(k.secret().ref())));
return k;
}
bool AccountManager::openWallet()
{
if (!m_keyManager)
{
m_keyManager.reset(new KeyManager());
if (m_keyManager->exists())
{
if (m_keyManager->load(std::string()) || m_keyManager->load(getPassword("Please enter your MASTER passphrase: ")))
return true;
else
{
LOG(ERROR) << "Couldn't open wallet. Please check passphrase." << endl;
return false;
}
}
else
{
LOG(ERROR) << "Couldn't open wallet. Does it exist?" << endl;
return false;
}
}
return true;
}