void wallet_db::add_account( const account_record& blockchain_account, const variant& private_data ) { try { wallet_account_record war; account_record& tmp = war; tmp = blockchain_account; war.private_data = private_data; war.account_address = address(blockchain_account.owner_key); war.wallet_record_index = new_wallet_record_index(); if (has_private_key(war.account_address)) war.is_my_account = true; store_record( war ); auto current_key = lookup_key( blockchain_account.owner_key ); if( current_key.valid() ) { current_key->account_address = address(blockchain_account.owner_key); store_record( *current_key ); } else { wallet_key_record new_key; new_key.wallet_record_index = new_wallet_record_index(); new_key.account_address = address(blockchain_account.owner_key); new_key.public_key = blockchain_account.active_key(); my->load_key_record( new_key, false ); store_key( new_key ); } } FC_CAPTURE_AND_RETHROW( (blockchain_account) ) }
void wallet_db::remove_contact_account( const string& account_name ) { auto opt_account = lookup_account( account_name ); FC_ASSERT( opt_account.valid() ); auto acct = *opt_account; FC_ASSERT( ! has_private_key(address(acct.owner_key)), "you can only remove contact accounts"); accounts.erase( acct.wallet_record_index ); remove_item( acct.wallet_record_index ); keys.erase( address(acct.owner_key) ); address_to_account_wallet_record_index.erase( address(acct.owner_key) ); for( const auto& time_key_pair : acct.active_key_history ) { keys.erase( address(time_key_pair.second) ); address_to_account_wallet_record_index.erase( address(time_key_pair.second) ); } name_to_account_wallet_record_index.erase( account_name ); }
void wallet_db::add_account( const string& new_account_name, const public_key_type& new_account_key, const variant& private_data ) { auto current_account_itr = name_to_account_wallet_record_index.find( new_account_name ); FC_ASSERT( current_account_itr == name_to_account_wallet_record_index.end(), "Account with name ${name} already exists", ("name",new_account_name) ); auto current_address_itr = address_to_account_wallet_record_index.find( new_account_key ); FC_ASSERT( current_address_itr == address_to_account_wallet_record_index.end(), "Account with address ${address} already exists", ("name",new_account_key) ); wallet_account_record war; war.name = new_account_name; war.id = 0; war.account_address = address( new_account_key ); war.owner_key = new_account_key; war.set_active_key( blockchain::now(), new_account_key ); war.private_data = private_data; war.wallet_record_index = new_wallet_record_index(); if (has_private_key(war.account_address)) war.is_my_account = true; store_record( war ); auto current_key = lookup_key( new_account_key ); if( current_key ) { current_key->account_address = address(new_account_key); store_record( *current_key, true ); } else { wallet_key_record new_key; new_key.wallet_record_index = new_wallet_record_index(); new_key.account_address = address(new_account_key); new_key.public_key = new_account_key; my->load_key_record( new_key, false ); store_key( new_key ); } }
private_keys wallet_db::get_account_private_keys( const fc::sha512& password ) { try { private_keys keys; keys.reserve( accounts.size() ); for( const auto& item : accounts ) { auto key_rec = lookup_key(item.second.account_address); if( key_rec.valid() && key_rec->has_private_key() ) { try { keys.push_back( key_rec->decrypt_private_key( password ) ); } catch ( const fc::exception& e ) { wlog( "error decrypting private key: ${e}", ("e", e.to_detail_string() ) ); throw; // TODO... don't thtrow here, just log } } } return keys; } FC_RETHROW_EXCEPTIONS( warn, "" ) }
// Only returns private keys corresponding to owner and active keys map<PrivateKeyType, string> WalletDb::get_account_private_keys( const fc::sha512& password )const { try { map<PublicKeyType, string> public_keys; for( const auto& account_item : accounts ) { const auto& account = account_item.second; public_keys[ account.owner_key ] = account.name; for( const auto& active_key_item : account.active_key_history ) { const auto& active_key = active_key_item.second; if( active_key != PublicKeyType() ) public_keys[ active_key ] = account.name; } } map<PrivateKeyType, string> private_keys; for( const auto& public_key_item : public_keys ) { const auto& public_key = public_key_item.first; const auto& account_name = public_key_item.second; const auto key_entry = lookup_key( public_key ); if( !key_entry.valid() || !key_entry->has_private_key() ) continue; try { private_keys[ key_entry->decrypt_private_key( password ) ] = account_name; } catch( const fc::exception& e ) { elog( "Error decrypting private key: ${e}", ("e",e.to_detail_string()) ); } } return private_keys; } FC_CAPTURE_AND_RETHROW() }