From 306aa86febdcfff0892f55d15961cd319e1f1786 Mon Sep 17 00:00:00 2001 From: Edward Hennis Date: Tue, 23 Jun 2020 14:26:35 -0400 Subject: [PATCH 1/2] Get the account id for a public key --- src/OfflineTool.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/OfflineTool.cpp b/src/OfflineTool.cpp index 1c6f7f4..4443077 100644 --- a/src/OfflineTool.cpp +++ b/src/OfflineTool.cpp @@ -22,6 +22,8 @@ #include #include +#include +#include #include #include #include @@ -285,6 +287,33 @@ doCreateKeyfile( return EXIT_SUCCESS; } +int +doPublicKey(std::string const& publicKeyHex) +{ + auto error = [&publicKeyHex]() -> boost::optional { + using namespace ripple; + if (auto blob = strUnHex(publicKeyHex)) + { + auto const slice = makeSlice(*blob); + if (!publicKeyType(slice)) + return "Bad public key format."; + PublicKey const publicKey(slice); + auto const idSigner = calcAccountID(publicKey); + std::cout << "Public key: " << publicKey + << "\nAccount ID: " << idSigner << std::endl; + return {}; + } + else + { + return "Invalid hex"; + } + }(); + if (error) + std::cerr << "Invalid public key " << publicKeyHex << " " << *error + << std::endl; + return EXIT_FAILURE; +} + std::string getStdin() { @@ -347,6 +376,10 @@ runCommand( auto const argumenterror = []() { throw std::runtime_error("Syntax error: Wrong number of arguments"); }; + auto const publickey = + [](auto const& publicKeyHex, auto const&, auto const&) { + return doPublicKey(*publicKeyHex); + }; static map const commandArgs = { {"serialize", {false, serialize}}, {"deserialize", {false, deserialize}}, @@ -355,6 +388,7 @@ runCommand( {"asign", {false, asign}}, {"txhash", {false, txhash}}, {"createkeyfile", {true, createkeyfile}}, + {"publickey", {false, publickey}}, }; auto const iArgs = commandArgs.find(command); @@ -428,6 +462,10 @@ printHelp( createkeyfile [|--stdin] Create keyfile. A random seed will be used if no is provided on the command line or from standard input using --stdin. + Miscellaneous: + publickey Get the account id from + a public key. + Default keyfile is: )" << defaultKeyfile << "\n"; From 59aaf1fb19e00611c374598e1f254414446a2178 Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Fri, 9 Jan 2026 13:37:35 -0500 Subject: [PATCH 2/2] Add a unit test, and fix the command return value --- src/OfflineTool.cpp | 7 +++++-- src/test/OfflineTool_test.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/OfflineTool.cpp b/src/OfflineTool.cpp index 4443077..3c6df6e 100644 --- a/src/OfflineTool.cpp +++ b/src/OfflineTool.cpp @@ -290,7 +290,7 @@ doCreateKeyfile( int doPublicKey(std::string const& publicKeyHex) { - auto error = [&publicKeyHex]() -> boost::optional { + auto const error = [&publicKeyHex]() -> boost::optional { using namespace ripple; if (auto blob = strUnHex(publicKeyHex)) { @@ -309,9 +309,12 @@ doPublicKey(std::string const& publicKeyHex) } }(); if (error) + { std::cerr << "Invalid public key " << publicKeyHex << " " << *error << std::endl; - return EXIT_FAILURE; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } std::string diff --git a/src/test/OfflineTool_test.cpp b/src/test/OfflineTool_test.cpp index c8aae59..e71e8ed 100644 --- a/src/test/OfflineTool_test.cpp +++ b/src/test/OfflineTool_test.cpp @@ -625,6 +625,35 @@ class OfflineTool_test : public beast::unit_test::suite } } + void + testPublicKey() + { + testcase("Get AccountID from Public Key"); + + std::string const pkHex{ + "036F382B723879499B6067B00B241DDF6FBA7599E9F6BF36A422150CEE75FF08F" + "8"}; + std::string const account{"rGse36vmL3teKPJwHvKaqgxKkrMKEbATGN"}; + + auto const [out, err] = [&pkHex, this]() { + CoutRedirect coutRedirect; + + std::vector args; + args.push_back(pkHex); + + auto const exit = + runCommand("publickey", args, {}, {}, InputType::commandline); + BEAST_EXPECT(exit == EXIT_SUCCESS); + return std::make_pair(coutRedirect.out(), coutRedirect.err()); + }(); + + std::string const expected = + "Public key: " + pkHex + "\nAccount ID: " + account + "\n"; + + BEAST_EXPECTS(err.empty(), err); + BEAST_EXPECTS(out == expected, out); + } + void testRunCommand() { @@ -717,6 +746,7 @@ class OfflineTool_test : public beast::unit_test::suite testSingleSign(); testMultiSign(); testCreateKeyfile(); + testPublicKey(); testRunCommand(); } };