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

Accept seed in RPC wallet_create #1567

Merged
merged 4 commits into from Jan 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions nano/core_test/rpc.cpp
Expand Up @@ -708,6 +708,38 @@ TEST (rpc, wallet_create)
ASSERT_NE (system.nodes[0]->wallets.items.end (), system.nodes[0]->wallets.items.find (wallet_id));
}

TEST (rpc, wallet_create_seed)
{
nano::system system (24000, 1);
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "wallet_create");
nano::keypair seed;
request.put ("seed", seed.pub.to_string ());
test_response response (request, rpc, system.io_ctx);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
std::string wallet_text (response.json.get<std::string> ("wallet"));
nano::uint256_union wallet_id;
ASSERT_FALSE (wallet_id.decode_hex (wallet_text));
auto wallet (system.nodes[0]->wallets.items.find (wallet_id));
ASSERT_NE (system.nodes[0]->wallets.items.end (), wallet);
{
auto transaction (system.nodes[0]->wallets.tx_begin_read ());
nano::raw_key seed0;
wallet->second->store.seed (seed0, transaction);
ASSERT_EQ (seed.pub, seed0.data);
}
auto account_text (response.json.get<std::string> ("account"));
nano::uint256_union account;
ASSERT_FALSE (account.decode_account (account_text));
ASSERT_TRUE (system.wallet (0)->exists (account));
}

TEST (rpc, wallet_export)
{
nano::system system (24000, 1);
Expand Down
30 changes: 22 additions & 8 deletions nano/node/rpc.cpp
Expand Up @@ -3243,17 +3243,31 @@ void nano::rpc_handler::wallet_create ()
rpc_control_impl ();
if (!ec)
{
nano::keypair wallet_id;
node.wallets.create (wallet_id.pub);
auto transaction (node.store.tx_begin_read ());
auto existing (node.wallets.items.find (wallet_id.pub));
if (existing != node.wallets.items.end ())
nano::raw_key seed;
auto seed_text (request.get_optional<std::string> ("seed"));
if (seed_text.is_initialized () && seed.data.decode_hex (seed_text.get ()))
{
response_l.put ("wallet", wallet_id.pub.to_string ());
ec = nano::error_common::bad_seed;
}
else
if (!ec)
{
ec = nano::error_common::wallet_lmdb_max_dbs;
nano::keypair wallet_id;
auto wallet (node.wallets.create (wallet_id.pub));
auto existing (node.wallets.items.find (wallet_id.pub));
if (existing != node.wallets.items.end ())
{
response_l.put ("wallet", wallet_id.pub.to_string ());
}
else
{
ec = nano::error_common::wallet_lmdb_max_dbs;
}
if (!ec && seed_text.is_initialized ())
{
auto transaction (node.wallets.tx_begin_write ());
nano::public_key account (wallet->change_seed (transaction, seed));
response_l.put ("account", account.to_account ());
}
}
}
response_errors ();
Expand Down