-
Notifications
You must be signed in to change notification settings - Fork 650
/
Copy pathwallet_api_impl.cpp
487 lines (430 loc) · 17.4 KB
/
wallet_api_impl.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
* Copyright (c) 2017 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <boost/algorithm/string/replace.hpp>
#include <boost/range/adaptors.hpp>
#include <fc/rpc/api_connection.hpp>
#include <fc/popcount.hpp>
#include <fc/git_revision.hpp>
#include <fc/thread/scoped_lock.hpp>
#include <fc/io/fstream.hpp>
#include <graphene/wallet/wallet.hpp>
#include "wallet_api_impl.hpp"
#include <graphene/utilities/git_revision.hpp>
#ifndef WIN32
# include <sys/types.h>
# include <sys/stat.h>
#endif
// explicit instantiation for later use
namespace fc {
template class api<graphene::wallet::wallet_api, identity_member_with_optionals>;
}
/****
* General methods for wallet impl object (ctor, info, about, wallet file, etc.)
*/
namespace graphene { namespace wallet { namespace detail {
wallet_api_impl::wallet_api_impl( wallet_api& s, const wallet_data& initial_data, fc::api<login_api> rapi )
: self(s),
_chain_id(initial_data.chain_id),
_remote_api(rapi),
_remote_db(rapi->database()),
_remote_net_broadcast(rapi->network_broadcast()),
_remote_hist(rapi->history())
{
try {
_custom_operations = rapi->custom_operations();
}
catch(const fc::exception& e)
{
wlog("Custom operations API is not active on server.");
}
chain_id_type remote_chain_id = _remote_db->get_chain_id();
if( remote_chain_id != _chain_id )
{
FC_THROW( "Remote server gave us an unexpected chain_id",
("remote_chain_id", remote_chain_id)
("chain_id", _chain_id) );
}
init_prototype_ops();
_remote_db->set_block_applied_callback( [this](const variant& block_id )
{
on_block_applied( block_id );
} );
_wallet.chain_id = _chain_id;
_wallet.ws_server = initial_data.ws_server;
_wallet.ws_user = initial_data.ws_user;
_wallet.ws_password = initial_data.ws_password;
}
wallet_api_impl::~wallet_api_impl()
{
try
{
_remote_db->cancel_all_subscriptions();
}
catch (const fc::exception& e)
{
// Right now the wallet_api has no way of knowing if the connection to the
// witness has already disconnected (via the witness node exiting first).
// If it has exited, cancel_all_subscriptsions() will throw and there's
// nothing we can do about it.
// dlog("Caught exception ${e} while canceling database subscriptions", ("e", e));
}
}
fc::variant wallet_api_impl::info() const
{
auto chain_props = get_chain_properties();
auto global_props = get_global_properties();
auto dynamic_props = get_dynamic_global_properties();
fc::mutable_variant_object result;
result["head_block_num"] = dynamic_props.head_block_number;
result["head_block_id"] = fc::variant(dynamic_props.head_block_id, 1);
result["head_block_age"] = fc::get_approximate_relative_time_string(dynamic_props.time,
time_point_sec(time_point::now()),
" old");
result["next_maintenance_time"] =
fc::get_approximate_relative_time_string(dynamic_props.next_maintenance_time);
result["chain_id"] = chain_props.chain_id;
stringstream participation;
participation << fixed << std::setprecision(2) << (100.0*fc::popcount(dynamic_props.recent_slots_filled)) / 128.0;
result["participation"] = participation.str();
result["active_witnesses"] = fc::variant(global_props.active_witnesses, GRAPHENE_MAX_NESTED_OBJECTS);
result["active_committee_members"] =
fc::variant(global_props.active_committee_members, GRAPHENE_MAX_NESTED_OBJECTS);
return result;
}
/***
* @brief return basic information about this program
*/
fc::variant_object wallet_api_impl::about() const
{
string client_version( graphene::utilities::git_revision_description );
const size_t pos = client_version.find( '/' );
if( pos != string::npos && client_version.size() > pos )
client_version = client_version.substr( pos + 1 );
fc::mutable_variant_object result;
//result["blockchain_name"] = BLOCKCHAIN_NAME;
//result["blockchain_description"] = BTS_BLOCKCHAIN_DESCRIPTION;
result["client_version"] = client_version;
result["graphene_revision"] = graphene::utilities::git_revision_sha;
result["graphene_revision_age"] = fc::get_approximate_relative_time_string( fc::time_point_sec(
graphene::utilities::git_revision_unix_timestamp ) );
result["fc_revision"] = fc::git_revision_sha;
result["fc_revision_age"] = fc::get_approximate_relative_time_string( fc::time_point_sec(
fc::git_revision_unix_timestamp ) );
result["compile_date"] = "compiled on " __DATE__ " at " __TIME__;
result["boost_version"] = boost::replace_all_copy(std::string(BOOST_LIB_VERSION), "_", ".");
result["openssl_version"] = OPENSSL_VERSION_TEXT;
std::string bitness = boost::lexical_cast<std::string>(8 * sizeof(int*)) + "-bit";
#if defined(__APPLE__)
std::string os = "osx";
#elif defined(__linux__)
std::string os = "linux";
#elif defined(_MSC_VER)
std::string os = "win32";
#else
std::string os = "other";
#endif
result["build"] = os + " " + bitness;
return result;
}
void wallet_api_impl::quit()
{
ilog( "Quitting Cli Wallet ..." );
throw fc::canceled_exception();
}
chain_property_object wallet_api_impl::get_chain_properties() const
{
return _remote_db->get_chain_properties();
}
global_property_object wallet_api_impl::get_global_properties() const
{
return _remote_db->get_global_properties();
}
dynamic_global_property_object wallet_api_impl::get_dynamic_global_properties() const
{
return _remote_db->get_dynamic_global_properties();
}
void wallet_api_impl::on_block_applied( const variant& block_id )
{
fc::async([this]{resync();}, "Resync after block");
}
void wallet_api_impl::set_operation_fees( signed_transaction& tx, const fee_schedule& s )
{
for( auto& op : tx.operations )
s.set_fee(op);
}
operation wallet_api_impl::get_prototype_operation( string operation_name )
{
auto it = _prototype_ops.find( operation_name );
if( it == _prototype_ops.end() )
FC_THROW("Unsupported operation: \"${operation_name}\"", ("operation_name", operation_name));
return it->second;
}
void wallet_api_impl::init_prototype_ops()
{
operation op;
for( int t=0; t<op.count(); t++ )
{
op.set_which( t );
op.visit( op_prototype_visitor(t, _prototype_ops) );
}
return;
}
int wallet_api_impl::find_first_unused_derived_key_index(const fc::ecc::private_key& parent_key)
{
int first_unused_index = 0;
int number_of_consecutive_unused_keys = 0;
for (int key_index = 0; ; ++key_index)
{
fc::ecc::private_key derived_private_key = derive_private_key(key_to_wif(parent_key), key_index);
graphene::chain::public_key_type derived_public_key = derived_private_key.get_public_key();
if( _keys.find(derived_public_key) == _keys.end() )
{
if (number_of_consecutive_unused_keys)
{
++number_of_consecutive_unused_keys;
if (number_of_consecutive_unused_keys > 5)
return first_unused_index;
}
else
{
first_unused_index = key_index;
number_of_consecutive_unused_keys = 1;
}
}
else
{
// key_index is used
first_unused_index = 0;
number_of_consecutive_unused_keys = 0;
}
}
}
void wallet_api_impl::enable_umask_protection()
{
#ifdef __unix__
_old_umask = umask( S_IRWXG | S_IRWXO );
#endif
}
void wallet_api_impl::disable_umask_protection()
{
#ifdef __unix__
umask( _old_umask );
#endif
}
bool wallet_api_impl::copy_wallet_file( string destination_filename )
{
fc::path src_path = get_wallet_filename();
if( !fc::exists( src_path ) )
return false;
fc::path dest_path = destination_filename + _wallet_filename_extension;
int suffix = 0;
while( fc::exists(dest_path) )
{
++suffix;
dest_path = destination_filename + "-" + to_string( suffix ) + _wallet_filename_extension;
}
wlog( "backing up wallet ${src} to ${dest}",
("src", src_path)
("dest", dest_path) );
fc::path dest_parent = fc::absolute(dest_path).parent_path();
try
{
enable_umask_protection();
if( !fc::exists( dest_parent ) )
fc::create_directories( dest_parent );
fc::copy( src_path, dest_path );
disable_umask_protection();
}
catch(...)
{
disable_umask_protection();
throw;
}
return true;
}
/***
* @brief returns true if the wallet is unlocked
*/
bool wallet_api_impl::is_locked()const
{
return _checksum == fc::sha512();
}
void wallet_api_impl::resync()
{
fc::scoped_lock<fc::mutex> lock(_resync_mutex);
// this method is used to update wallet_data annotations
// e.g. wallet has been restarted and was not notified
// of events while it was down
//
// everything that is done "incremental style" when a push
// notification is received, should also be done here
// "batch style" by querying the blockchain
if( !_wallet.pending_account_registrations.empty() )
{
// make a vector of the account names pending registration
std::vector<string> pending_account_names =
boost::copy_range<std::vector<string> >(boost::adaptors::keys(_wallet.pending_account_registrations));
// look those up on the blockchain
std::vector<fc::optional<graphene::chain::account_object >>
pending_account_objects = _remote_db->lookup_account_names( pending_account_names );
// if any of them exist, claim them
for( const fc::optional<graphene::chain::account_object>& optional_account : pending_account_objects )
if( optional_account )
claim_registered_account(*optional_account);
}
if (!_wallet.pending_witness_registrations.empty())
{
// make a vector of the owner accounts for witnesses pending registration
std::vector<string> pending_witness_names =
boost::copy_range<std::vector<string> >(boost::adaptors::keys(_wallet.pending_witness_registrations));
// look up the owners on the blockchain
std::vector<fc::optional<graphene::chain::account_object>> owner_account_objects =
_remote_db->lookup_account_names(pending_witness_names);
// if any of them have registered witnesses, claim them
for( const fc::optional<graphene::chain::account_object>& optional_account : owner_account_objects )
if (optional_account)
{
std::string account_id = account_id_to_string(optional_account->id);
fc::optional<witness_object> witness_obj = _remote_db->get_witness_by_account(account_id);
if (witness_obj)
claim_registered_witness(optional_account->name);
}
}
}
string wallet_api_impl::get_wallet_filename() const
{
return _wallet_filename;
}
bool wallet_api_impl::load_wallet_file(string wallet_filename)
{
// TODO: Merge imported wallet with existing wallet,
// instead of replacing it
if( wallet_filename == "" )
wallet_filename = _wallet_filename;
if( ! fc::exists( wallet_filename ) )
return false;
_wallet = fc::json::from_file( wallet_filename ).as< wallet_data >( 2 * GRAPHENE_MAX_NESTED_OBJECTS );
if( _wallet.chain_id != _chain_id )
FC_THROW( "Wallet chain ID does not match",
("wallet.chain_id", _wallet.chain_id)
("chain_id", _chain_id) );
size_t account_pagination = 100;
vector< std::string > account_ids_to_send;
size_t n = _wallet.my_accounts.size();
account_ids_to_send.reserve( std::min( account_pagination, n ) );
auto it = _wallet.my_accounts.begin();
for( size_t start=0; start<n; start+=account_pagination )
{
size_t end = std::min( start+account_pagination, n );
assert( end > start );
account_ids_to_send.clear();
std::vector< account_object > old_accounts;
for( size_t i=start; i<end; i++ )
{
assert( it != _wallet.my_accounts.end() );
old_accounts.push_back( *it );
std::string account_id = account_id_to_string(old_accounts.back().id);
account_ids_to_send.push_back( account_id );
++it;
}
std::vector< optional< account_object > > accounts = _remote_db->get_accounts(account_ids_to_send, {});
// server response should be same length as request
FC_ASSERT( accounts.size() == account_ids_to_send.size() );
size_t i = 0;
for( const optional< account_object >& acct : accounts )
{
account_object& old_acct = old_accounts[i];
if( !acct.valid() )
{
elog( "Could not find account ${id} : \"${name}\" does not exist on the chain!",
("id", old_acct.id)("name", old_acct.name) );
i++;
continue;
}
// this check makes sure the server didn't send results
// in a different order, or accounts we didn't request
FC_ASSERT( acct->id == old_acct.id );
if( fc::json::to_string(*acct) != fc::json::to_string(old_acct) )
{
wlog( "Account ${id} : \"${name}\" updated on chain", ("id", acct->id)("name", acct->name) );
}
_wallet.update_account( *acct );
i++;
}
}
return true;
}
void wallet_api_impl::save_wallet_file(string wallet_filename)
{
//
// Serialize in memory, then save to disk
//
// This approach lessens the risk of a partially written wallet
// if exceptions are thrown in serialization
//
encrypt_keys();
if( wallet_filename == "" )
wallet_filename = _wallet_filename;
wlog( "saving wallet to file ${fn}", ("fn", wallet_filename) );
string data = fc::json::to_pretty_string( _wallet );
try
{
enable_umask_protection();
//
// Parentheses on the following declaration fails to compile,
// due to the Most Vexing Parse. Thanks, C++
//
// http://en.wikipedia.org/wiki/Most_vexing_parse
//
std::string tmp_wallet_filename = wallet_filename + ".tmp";
fc::ofstream outfile{ fc::path( tmp_wallet_filename ) };
outfile.write( data.c_str(), data.length() );
outfile.flush();
outfile.close();
wlog( "saved successfully wallet to tmp file ${fn}", ("fn", tmp_wallet_filename) );
std::string wallet_file_content;
fc::read_file_contents(tmp_wallet_filename, wallet_file_content);
if (wallet_file_content == data) {
wlog( "validated successfully tmp wallet file ${fn}", ("fn", tmp_wallet_filename) );
fc::rename( tmp_wallet_filename, wallet_filename );
wlog( "renamed successfully tmp wallet file ${fn}", ("fn", tmp_wallet_filename) );
}
else
{
FC_THROW("tmp wallet file cannot be validated ${fn}", ("fn", tmp_wallet_filename) );
}
wlog( "successfully saved wallet to file ${fn}", ("fn", wallet_filename) );
disable_umask_protection();
}
catch(...)
{
string ws_password = _wallet.ws_password;
_wallet.ws_password = "";
wlog("wallet file content is next: ${data}", ("data", fc::json::to_pretty_string( _wallet ) ) );
_wallet.ws_password = ws_password;
disable_umask_protection();
throw;
}
}
}}} // namespace graphene::wallet::detail