Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): introduce planet key derive #1268

Merged
merged 11 commits into from
Apr 29, 2021
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ To be released.
specific peer. [[#1240]]
- Added the option `--no-passphrase` to `planet key remove` command to remove
key without asking passphrase. [[#1213], [#1265]]
- Added `planet key derive` subcommand to derive the address or
public key from a private. [[#1268]]

[#1192]: https://github.com/planetarium/libplanet/issues/1192
[#1197]: https://github.com/planetarium/libplanet/pull/1197
Expand All @@ -165,6 +167,7 @@ To be released.
[#1235]: https://github.com/planetarium/libplanet/pull/1235
[#1240]: https://github.com/planetarium/libplanet/pull/1240
[#1265]: https://github.com/planetarium/libplanet/pull/1265
[#1268]: https://github.com/planetarium/libplanet/pull/1268


Version 0.11.1
Expand Down
47 changes: 47 additions & 0 deletions Libplanet.Extensions.Cocona/Commands/Key/DerivationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using global::Cocona;
using Libplanet.Crypto;

namespace Libplanet.Extensions.Cocona.Commands.Key
{
public class DerivationCommand
{
public void PrivateKey(
[Argument(
"PRIVATE-KEY",
Description = "A private key that the address/public key are derived from."
)]
string privateKeyHex,
[Option('p', Description = "Derives the public key.")]
bool publicKey = false,
[Option('a', Description = "Derives the address.")]
bool address = false)
{
if (publicKey && address)
{
throw new CommandExitedException(
"--public-key and --address are mutually exclusive; turn on one at a time.",
-1
);
}
else if (!publicKey && !address)
{
throw new CommandExitedException(
"One of --public-key and --address should be turned on.",
-1
);
}

var privateKey = new PrivateKey(ByteUtil.ParseHex(privateKeyHex));
if (address)
{
Console.Out.WriteLine(privateKey.ToAddress());
}

if (publicKey)
{
Console.Out.WriteLine(ByteUtil.Hex(privateKey.PublicKey.Format(true)));
}
}
}
}
6 changes: 6 additions & 0 deletions Libplanet.Extensions.Cocona/Commands/KeyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
using System.Linq;
using global::Cocona;
using Libplanet.Crypto;
using Libplanet.Extensions.Cocona.Commands.Key;
using Libplanet.KeyStore;

namespace Libplanet.Extensions.Cocona.Commands
{
[HasSubCommands(
typeof(DerivationCommand),
"derive",
Description = "Derive the address or public key from private key."
)]
public class KeyCommand
{
public KeyCommand()
Expand Down