diff --git a/src/serial_bridge_index.cpp b/src/serial_bridge_index.cpp index ef7eda6..156569f 100644 --- a/src/serial_bridge_index.cpp +++ b/src/serial_bridge_index.cpp @@ -780,4 +780,28 @@ string serial_bridge::derivation_to_scalar(const string &args_string) // return ret_json_from_root(root); } - +string serial_bridge::encrypt_payment_id(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::hash8 payment_id; + if (!epee::string_tools::hex_to_pod(json_root.get("payment_id"), payment_id)) { + return error_ret_json_from_message("Invalid 'payment_id'"); + } + crypto::public_key public_key; + if (!epee::string_tools::hex_to_pod(json_root.get("public_key"), public_key)) { + return error_ret_json_from_message("Invalid 'public_key'"); + } + crypto::secret_key secret_key; + if (!epee::string_tools::hex_to_pod(json_root.get("secret_key"), secret_key)) { + return error_ret_json_from_message("Invalid 'secret_key'"); + } + hw::device &hwdev = hw::get_device("default"); + hwdev.encrypt_payment_id(payment_id, public_key, secret_key); + boost::property_tree::ptree root; + root.put(ret_json_key__generic_retVal(), epee::string_tools::pod_to_hex(payment_id)); + return ret_json_from_root(root); +} diff --git a/src/serial_bridge_index.hpp b/src/serial_bridge_index.hpp index 9262752..51c36eb 100644 --- a/src/serial_bridge_index.hpp +++ b/src/serial_bridge_index.hpp @@ -72,6 +72,7 @@ namespace serial_bridge string derivation_to_scalar(const string &args_string); string decodeRct(const string &args_string); string decodeRctSimple(const string &args_string); + string encrypt_payment_id(const string &args_string); } #endif /* serial_bridge_index_hpp */ diff --git a/test/test_all.cpp b/test/test_all.cpp index 4b771ed..447fb73 100644 --- a/test/test_all.cpp +++ b/test/test_all.cpp @@ -1247,6 +1247,31 @@ BOOST_AUTO_TEST_CASE(bridged__decodeRctSimple) cout << "bridged__decodeRctSimple: amount_string: " << amount_string << endl; BOOST_REQUIRE(amount_string == "10000000000"); } +// +BOOST_AUTO_TEST_CASE(bridged__encrypt_payment_id) +{ + using namespace serial_bridge; + // + boost::property_tree::ptree root; + root.put("payment_id", "f0756322689f8299"); + root.put("public_key", "9c8bd8a9ff8703ddd5e28a36dc5c5586d2ec0b4bfd9190adeea825db5808ead2"); + root.put("secret_key", "74f277a60613a4efa33258b9814c78e0ff7a53cf8d2cd248ee921ac7f607f800"); + // + auto ret_string = serial_bridge::encrypt_payment_id(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); + cout << "bridged__encrypt_payment_id: " << *str << endl; + BOOST_REQUIRE(*str == "6565253f11d43de5"); +} // //