Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed saving keys in wallet #1613

Merged
merged 3 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,10 @@ class wallet_api_impl
private:
void claim_registered_account(const account_object& account)
{
bool import_keys = false;
auto it = _wallet.pending_account_registrations.find( account.name );
FC_ASSERT( it != _wallet.pending_account_registrations.end() );
for (const std::string& wif_key : it->second)
for (const std::string& wif_key : it->second) {
if( !import_key( account.name, wif_key ) )
{
// somebody else beat our pending registration, there is
Expand All @@ -280,8 +281,15 @@ class wallet_api_impl
// possibility of migrating to a fork where the
// name is available, the user can always
// manually re-register)
} else {
import_keys = true;
}
}
_wallet.pending_account_registrations.erase( it );

if( import_keys ) {
save_wallet_file();
}
}

// after a witness registration succeeds, this saves the private key in the wallet permanently
Expand Down
40 changes: 40 additions & 0 deletions tests/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <fc/rpc/websocket_api.hpp>
#include <fc/rpc/cli.hpp>

#include <fc/crypto/aes.hpp>

#ifdef _WIN32
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
Expand Down Expand Up @@ -565,3 +567,41 @@ BOOST_FIXTURE_TEST_CASE( account_history_pagination, cli_fixture )
throw;
}
}

graphene::wallet::plain_keys decrypt_keys( const std::string& password, const vector<char>& cipher_keys )
{
auto pw = fc::sha512::hash( password.c_str(), password.size() );
vector<char> decrypted = fc::aes_decrypt( pw, cipher_keys );
return fc::raw::unpack<graphene::wallet::plain_keys>( decrypted );
}

BOOST_AUTO_TEST_CASE( saving_keys_wallet_test ) {
cli_fixture cli;

cli.con.wallet_api_ptr->import_balance( "nathan", cli.nathan_keys, true );
cli.con.wallet_api_ptr->upgrade_account( "nathan", true );
std::string brain_key( "FICTIVE WEARY MINIBUS LENS HAWKIE MAIDISH MINTY GLYPH GYTE KNOT COCKSHY LENTIGO PROPS BIFORM KHUTBAH BRAZIL" );
cli.con.wallet_api_ptr->create_account_with_brain_key( brain_key, "account1", "nathan", "nathan", true );

BOOST_CHECK_NO_THROW( cli.con.wallet_api_ptr->transfer( "nathan", "account1", "9000", "1.3.0", "", true ) );

std::string path( cli.app_dir.path().generic_string() + "/wallet.json" );
graphene::wallet::wallet_data wallet = fc::json::from_file( path ).as<graphene::wallet::wallet_data>( 2 * GRAPHENE_MAX_NESTED_OBJECTS );
BOOST_CHECK( wallet.extra_keys.size() == 1 ); // nathan
BOOST_CHECK( wallet.pending_account_registrations.size() == 1 ); // account1
BOOST_CHECK( wallet.pending_account_registrations["account1"].size() == 2 ); // account1 active key + account1 memo key

graphene::wallet::plain_keys pk = decrypt_keys( "supersecret", wallet.cipher_keys );
BOOST_CHECK( pk.keys.size() == 1 ); // nathan key

BOOST_CHECK( generate_block( cli.app1 ) );
fc::usleep( fc::seconds(1) );

wallet = fc::json::from_file( path ).as<graphene::wallet::wallet_data>( 2 * GRAPHENE_MAX_NESTED_OBJECTS );
BOOST_CHECK( wallet.extra_keys.size() == 2 ); // nathan + account1
BOOST_CHECK( wallet.pending_account_registrations.empty() );
BOOST_CHECK_NO_THROW( cli.con.wallet_api_ptr->transfer( "account1", "nathan", "1000", "1.3.0", "", true ) );

pk = decrypt_keys( "supersecret", wallet.cipher_keys );
BOOST_CHECK( pk.keys.size() == 3 ); // nathan key + account1 active key + account1 memo key
}