Skip to content

Commit 306aa86

Browse files
committed
Get the account id for a public key
1 parent 8c2959e commit 306aa86

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/OfflineTool.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <RippleKey.h>
2323
#include <Serialize.h>
2424

25+
#include <ripple/basics/Slice.h>
26+
#include <ripple/basics/StringUtilities.h>
2527
#include <ripple/beast/core/SemanticVersion.h>
2628
#include <ripple/beast/unit_test.h>
2729
#include <ripple/protocol/BuildInfo.h>
@@ -285,6 +287,33 @@ doCreateKeyfile(
285287
return EXIT_SUCCESS;
286288
}
287289

290+
int
291+
doPublicKey(std::string const& publicKeyHex)
292+
{
293+
auto error = [&publicKeyHex]() -> boost::optional<char const*> {
294+
using namespace ripple;
295+
if (auto blob = strUnHex(publicKeyHex))
296+
{
297+
auto const slice = makeSlice(*blob);
298+
if (!publicKeyType(slice))
299+
return "Bad public key format.";
300+
PublicKey const publicKey(slice);
301+
auto const idSigner = calcAccountID(publicKey);
302+
std::cout << "Public key: " << publicKey
303+
<< "\nAccount ID: " << idSigner << std::endl;
304+
return {};
305+
}
306+
else
307+
{
308+
return "Invalid hex";
309+
}
310+
}();
311+
if (error)
312+
std::cerr << "Invalid public key " << publicKeyHex << " " << *error
313+
<< std::endl;
314+
return EXIT_FAILURE;
315+
}
316+
288317
std::string
289318
getStdin()
290319
{
@@ -347,6 +376,10 @@ runCommand(
347376
auto const argumenterror = []() {
348377
throw std::runtime_error("Syntax error: Wrong number of arguments");
349378
};
379+
auto const publickey =
380+
[](auto const& publicKeyHex, auto const&, auto const&) {
381+
return doPublicKey(*publicKeyHex);
382+
};
350383
static map<string, commandParams> const commandArgs = {
351384
{"serialize", {false, serialize}},
352385
{"deserialize", {false, deserialize}},
@@ -355,6 +388,7 @@ runCommand(
355388
{"asign", {false, asign}},
356389
{"txhash", {false, txhash}},
357390
{"createkeyfile", {true, createkeyfile}},
391+
{"publickey", {false, publickey}},
358392
};
359393

360394
auto const iArgs = commandArgs.find(command);
@@ -428,6 +462,10 @@ printHelp(
428462
createkeyfile [<key>|--stdin] Create keyfile. A random
429463
seed will be used if no <key> is provided on the command line
430464
or from standard input using --stdin.
465+
Miscellaneous:
466+
publickey <hex public key> Get the account id from
467+
a public key.
468+
431469
432470
Default keyfile is: )"
433471
<< defaultKeyfile << "\n";

0 commit comments

Comments
 (0)