From 4aaef3ddd9257baabeb8c88fbfdfd097178dae51 Mon Sep 17 00:00:00 2001 From: Paul Shapiro Date: Sat, 18 Aug 2018 15:21:00 -0400 Subject: [PATCH] seed_and_keys_from_mnemonic --- src/serial_bridge_index.cpp | 32 ++++++++++++++++++++++++++-- test/test_all.cpp | 42 ++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/serial_bridge_index.cpp b/src/serial_bridge_index.cpp index 11c38c2..cce0527 100644 --- a/src/serial_bridge_index.cpp +++ b/src/serial_bridge_index.cpp @@ -264,7 +264,6 @@ string serial_bridge::newly_created_wallet(const string &args_string) } string serial_bridge::mnemonic_from_seed(const string &args_string) { - // seed_string, wordset_name boost::property_tree::ptree json_root; if (!parsed_json_root(args_string, json_root)) { // it will already have thrown an exception @@ -286,7 +285,36 @@ string serial_bridge::mnemonic_from_seed(const string &args_string) } string serial_bridge::seed_and_keys_from_mnemonic(const string &args_string) { - + boost::property_tree::ptree json_root; + if (!parsed_json_root(args_string, json_root)) { + // it will already have thrown an exception + return error_ret_json_from_message("Invalid JSON"); + } + monero_wallet_utils::WalletDescriptionRetVals retVals; + bool r = monero_wallet_utils::wallet_with( + json_root.get("mnemonic_string"), + json_root.get("wordset_name"), + retVals + ); + bool did_error = retVals.did_error; + if (!r) { + THROW_WALLET_EXCEPTION_IF(!did_error, error::wallet_internal_error, "Illegal fail flag but !did_error"); + return error_ret_json_from_message(*retVals.err_string); + } + monero_wallet_utils::WalletDescription walletDescription = *(retVals.optl__desc); + THROW_WALLET_EXCEPTION_IF(did_error, error::wallet_internal_error, "Illegal success flag but did_error"); + // + boost::property_tree::ptree root; + root.put(ret_json_key__sec_seed_string(), (*(retVals.optl__desc)).sec_seed_string); + root.put(ret_json_key__address_string(), (*(retVals.optl__desc)).address_string); + root.put(ret_json_key__pub_viewKey_string(), epee::string_tools::pod_to_hex((*(retVals.optl__desc)).pub_viewKey)); + root.put(ret_json_key__sec_viewKey_string(), epee::string_tools::pod_to_hex((*(retVals.optl__desc)).sec_viewKey)); + root.put(ret_json_key__pub_spendKey_string(), epee::string_tools::pod_to_hex((*(retVals.optl__desc)).pub_spendKey)); + root.put(ret_json_key__sec_spendKey_string(), epee::string_tools::pod_to_hex((*(retVals.optl__desc)).sec_spendKey)); + stringstream ret_ss; + boost::property_tree::write_json(ret_ss, root); + // + return ret_ss.str(); } string serial_bridge::verified_components_for_login(const string &args_string) { diff --git a/test/test_all.cpp b/test/test_all.cpp index 22ffafa..a7965b5 100644 --- a/test/test_all.cpp +++ b/test/test_all.cpp @@ -689,7 +689,47 @@ BOOST_AUTO_TEST_CASE(bridged__mnemonic_from_seed) } BOOST_AUTO_TEST_CASE(bridged__seed_and_keys_from_mnemonic) { - + using namespace serial_bridge; + // + boost::property_tree::ptree root; + root.put("mnemonic_string", "foxe selfish hum nexus juven dodeg pepp ember biscuti elap jazz vibrate biscui"); + root.put("wordset_name", "English"); + // + stringstream args_ss; + boost::property_tree::write_json(args_ss, root); + auto ret_string = serial_bridge::seed_and_keys_from_mnemonic(args_ss.str()); + stringstream ret_stream; + ret_stream << ret_string; + boost::property_tree::ptree ret_tree; + boost::property_tree::read_json(ret_stream, ret_tree); + optional err_string = ret_tree.get_optional(ret_json_key__any__err_msg()); + if (err_string != none) { + BOOST_REQUIRE_MESSAGE(false, *err_string); + } + optional sec_seed_string = ret_tree.get_optional(ret_json_key__sec_seed_string()); + BOOST_REQUIRE(sec_seed_string != none); + BOOST_REQUIRE((*sec_seed_string).size() > 0); + cout << "bridged: seed_and_keys_from_mnemonic: sec_seed: " << *sec_seed_string << endl; + optional address_string = ret_tree.get_optional(ret_json_key__address_string()); + BOOST_REQUIRE(address_string != none); + BOOST_REQUIRE((*address_string).size() > 0); + cout << "bridged: seed_and_keys_from_mnemonic: address: " << *address_string << endl; + optional pub_viewKey_string = ret_tree.get_optional(ret_json_key__pub_viewKey_string()); + BOOST_REQUIRE(pub_viewKey_string != none); + BOOST_REQUIRE((*pub_viewKey_string).size() > 0); + cout << "bridged: seed_and_keys_from_mnemonic: pub_viewKey_string: " << *pub_viewKey_string << endl; + optional pub_spendKey_string = ret_tree.get_optional(ret_json_key__pub_spendKey_string()); + BOOST_REQUIRE(pub_spendKey_string != none); + BOOST_REQUIRE((*pub_spendKey_string).size() > 0); + cout << "bridged: seed_and_keys_from_mnemonic: pub_spendKey_string: " << *pub_spendKey_string << endl; + optional sec_viewKey_string = ret_tree.get_optional(ret_json_key__sec_viewKey_string()); + BOOST_REQUIRE(sec_viewKey_string != none); + BOOST_REQUIRE((*sec_viewKey_string).size() > 0); + cout << "bridged: seed_and_keys_from_mnemonic: sec_viewKey_string: " << *sec_viewKey_string << endl; + optional sec_spendKey_string = ret_tree.get_optional(ret_json_key__sec_spendKey_string()); + BOOST_REQUIRE(sec_spendKey_string != none); + BOOST_REQUIRE((*sec_spendKey_string).size() > 0); + cout << "bridged: seed_and_keys_from_mnemonic: sec_spendKey_string: " << *sec_spendKey_string << endl; } BOOST_AUTO_TEST_CASE(bridged__verified_components_for_login) {