Skip to content

Commit

Permalink
planet apv verify command
Browse files Browse the repository at this point in the history
[changelog skip]
  • Loading branch information
dahlia committed Apr 25, 2020
1 parent c0b8c17 commit bd85583
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 15 deletions.
116 changes: 103 additions & 13 deletions Libplanet.Tools/Apv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Bencodex;
using Bencodex.Types;
using Cocona;
using Libplanet.Crypto;
using Libplanet.KeyStore;
using Libplanet.Net;

namespace Libplanet.Tools
Expand Down Expand Up @@ -99,31 +101,102 @@ public void Sign(
Console.WriteLine(v.Token);
}

[Command(Description = "Parse and analyze a given app protocol version token.")]
public void Analyze(
[Command(Description = "Verify a given app protocol version token's signature.")]
public void Verify(
[Argument(
Name = "APV-TOKEN",
Description = "An app protocol version token to analyze. " +
Description = "An app protocol version token to verify. " +
"Read from the standard input if omitted."
)]
string? token = null
string? token = null,
[Option(
'p',
ValueName = "PUBLIC-KEY",
Description = "Public key(s) to be used for verification. " +
"Can be applied multiple times."
)]
string[]? publicKey = null,
[Option(
'K',
Description = "Do not use any keys in the key store, " +
"but only -p/--public-key options."
)]
bool noKeyStore = false
)
{
if (token is null)
{
token = Console.ReadLine();
}
AppProtocolVersion v = ParseAppProtocolVersionToken(token);

AppProtocolVersion v;
try
if (publicKey is string[] pubKeyHexes)
{
v = AppProtocolVersion.FromToken(token.Trim());
foreach (string pubKeyHex in pubKeyHexes)
{
string opt = $"-p/--public-key=\"{pubKeyHex}\"";
PublicKey pubKey;
try
{
pubKey = new PublicKey(ByteUtil.ParseHex(pubKeyHex));
}
catch (Exception e)
{
throw Utils.Error($"The {opt} is not a valid public key. {e.Message}");
}

if (v.Verify(pubKey))
{
Console.Error.WriteLine(
"The signature successfully was verified using the {0}.",
opt
);
return;
}

Console.Error.WriteLine(
"The signature was failed to verify using the {0}.",
opt
);
}
}
catch (FormatException e)

if (!noKeyStore)
{
throw Utils.Error("Not a valid app protocol version token. " + e);
Key keyInstance = new Key();
IEnumerable<Tuple<Guid, ProtectedPrivateKey>> ppks = keyInstance.KeyStore.List()
.Where(pair => pair.Item2.Address.Equals(v.Signer));
foreach (Tuple<Guid, ProtectedPrivateKey> pair in ppks)
{
pair.Deconstruct(out Guid keyId, out ProtectedPrivateKey ppk);
PublicKey pubKey = keyInstance.UnprotectKey(keyId).PublicKey;
if (v.Verify(pubKey))
{
Console.Error.WriteLine(
"The signature successfully was verified using the key {0}.",
keyId
);
return;
}

Console.Error.WriteLine(
"The signature was failed to verify using the key {0}.",
keyId
);
}
}

throw Utils.Error("Failed to verify.");
}

[Command(Description = "Parse and analyze a given app protocol version token.")]
public void Analyze(
[Argument(
Name = "APV-TOKEN",
Description = "An app protocol version token to analyze. " +
"Read from the standard input if omitted."
)]
string? token = null
)
{
AppProtocolVersion v = ParseAppProtocolVersionToken(token);

var data = new List<(string, string)>
{
("version", v.Version.ToString(CultureInfo.InvariantCulture)),
Expand Down Expand Up @@ -192,5 +265,22 @@ void TreeIntoTable(IValue tree, List<(string, string)> table, string key)

Utils.PrintTable(("Field", "Value"), data);
}

private AppProtocolVersion ParseAppProtocolVersionToken(string? token)
{
if (token is null)
{
token = Console.ReadLine();
}

try
{
return AppProtocolVersion.FromToken(token.Trim());
}
catch (FormatException e)
{
throw Utils.Error($"Not a valid app protocol version token. {e.Message}");
}
}
}
}
4 changes: 2 additions & 2 deletions Libplanet.Tools/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ public void Generate(

public PrivateKey UnprotectKey(Guid keyId, string? passphrase = null)
{
passphrase ??= ConsolePasswordReader.Read("Passphrase: ");

ProtectedPrivateKey ppk;
try
{
Expand All @@ -187,6 +185,8 @@ public PrivateKey UnprotectKey(Guid keyId, string? passphrase = null)
throw Utils.Error($"No such key ID: {keyId}");
}

passphrase ??= ConsolePasswordReader.Read($"Passphrase (of {keyId}): ");

try
{
return ppk.Unprotect(passphrase);
Expand Down

0 comments on commit bd85583

Please sign in to comment.