From 10ebb31d49655727ceb15e135420118d2b2ff8b8 Mon Sep 17 00:00:00 2001 From: Guten Ye Date: Wed, 21 Nov 2018 13:25:08 +0800 Subject: [PATCH] Added derivation_to_scalar --- src/serial_bridge_index.cpp | 20 ++++++++++++++++++++ src/serial_bridge_index.hpp | 1 + test/test_all.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/serial_bridge_index.cpp b/src/serial_bridge_index.cpp index c340b98..4160e9c 100644 --- a/src/serial_bridge_index.cpp +++ b/src/serial_bridge_index.cpp @@ -761,3 +761,23 @@ string serial_bridge::derive_subaddress_public_key(const string &args_string) // return ret_json_from_root(root); } +string serial_bridge::derivation_to_scalar(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"); + } + crypto::key_derivation derivation; + if (!epee::string_tools::hex_to_pod(json_root.get("derivation"), derivation)) { + return error_ret_json_from_message("Invalid 'derivation'"); + } + std::size_t output_index = stoul(json_root.get("output_index")); + crypto::ec_scalar scalar = AUTO_VAL_INIT(scalar); + crypto::derivation_to_scalar(derivation, output_index, scalar); + boost::property_tree::ptree root; + root.put(ret_json_key__generic_retVal(), epee::string_tools::pod_to_hex(scalar)); + // + return ret_json_from_root(root); +} + diff --git a/src/serial_bridge_index.hpp b/src/serial_bridge_index.hpp index f63570f..f9a9f4d 100644 --- a/src/serial_bridge_index.hpp +++ b/src/serial_bridge_index.hpp @@ -69,6 +69,7 @@ namespace serial_bridge string generate_key_derivation(const string &args_string); string derive_public_key(const string &args_string); string derive_subaddress_public_key(const string &args_string); + string derivation_to_scalar(const string &args_string); string decodeRct(const string &args_string); string decodeRctSimple(const string &args_string); } diff --git a/test/test_all.cpp b/test/test_all.cpp index 6ea7626..65c713b 100644 --- a/test/test_all.cpp +++ b/test/test_all.cpp @@ -1091,6 +1091,30 @@ BOOST_AUTO_TEST_CASE(bridge__mainnet_pubKeyDerivations) } } // +BOOST_AUTO_TEST_CASE(bridged__derivation_to_scalar) +{ + using namespace serial_bridge; + // + boost::property_tree::ptree root; + root.put("derivation", "7a4c13a037d4bd2a7dd99a8c24669e9e04ca4e8a90e5b6703e88e87ad51c1849"); + root.put("output_index", "1"); + // + auto ret_string = serial_bridge::derivation_to_scalar(args_string_from_root(root)); + 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 str = ret_tree.get_optional(ret_json_key__generic_retVal()); + BOOST_REQUIRE(str != none); + BOOST_REQUIRE((*str).size() > 0); + BOOST_REQUIRE(*str == "767a2b9b814b78d55e27ab7fc9bae03253a810c820e7abb4cabadde44b599d04"); + cout << "bridged__derivation_to_scalar: " << *str << endl; +} +// BOOST_AUTO_TEST_CASE(bridged__decodeRct) { using namespace serial_bridge;