Skip to content

Commit

Permalink
Output to stdout is now default behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
Akamig committed Jun 16, 2022
1 parent 154870a commit 3f29c38
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions Libplanet.Extensions.Cocona/Commands/KeyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public KeyCommand()
[Option(Description = "Do not add to the key store, but only show the created key.")]
bool dryRun = false,
[Option(Description = "Path to export key as Web3 Secret Storage Format")]
string path = "key.json"
string path = ""
) =>
Add(string.Empty, passphrase, json, dryRun, path, true);

Expand Down Expand Up @@ -100,7 +100,7 @@ public KeyCommand()
[Option(Description = "Export a Web3 Secret Storage Formatted json to path.")]
bool json = false,
[Option(Description = "Path to export json key.")]
string path = "key.json"
string path = ""
)
{
PrivateKey key = UnprotectKey(keyId, passphrase);
Expand All @@ -110,9 +110,9 @@ public KeyCommand()
using Stream stdout = Console.OpenStandardOutput();
stdout.Write(rawKey, 0, rawKey.Length);
}
else if (json && path != string.Empty)
else if (json)
{
FileStream fs = File.Create(path);
Stream fs = PathHandler(path);
var ppk = KeyStore.Get(keyId);
ppk.WriteJson(fs, keyId);
fs.WriteByte(0x0a);
Expand Down Expand Up @@ -252,16 +252,15 @@ public KeyCommand()
bool create
)
{
string passphraseValue = passphrase.Take("Passphrase: ", "Retype passphrase: ");
string path = Path.GetFullPath(pathString);
Stream fs = PathHandler(pathString);
if (create)
{
string passphraseValue = passphrase.Take("Passphrase: ", "Retype passphrase: ");
PrivateKey pkey = new PrivateKey();
ProtectedPrivateKey ppk = ProtectedPrivateKey.Protect(pkey, passphraseValue);
Guid keyId = dryRun ? Guid.NewGuid() : KeyStore.Add(ppk);
if (json)
{
FileStream fs = File.Create(path);
ppk.WriteJson(fs, keyId);
fs.WriteByte(0x0a);
fs.Close();
Expand All @@ -271,18 +270,20 @@ bool create
{
if (json)
{
if (File.Exists(path))
try
{
ProtectedPrivateKey ppk = ProtectedPrivateKey.FromJson(File.ReadAllText(path));
ProtectedPrivateKey ppk = ProtectedPrivateKey.FromJson(
new StreamReader(fs).ReadToEnd());
Guid keyId = dryRun ? Guid.NewGuid() : KeyStore.Add(ppk);
}
else
catch (Exception)
{
Utils.Error("File not exists.");
Utils.Error("This is not valid json file or file does not exists.");
}
}
else
{
string passphraseValue = passphrase.Take("Passphrase: ", "Retype passphrase: ");
PrivateKey privateKey = ValidateRawHex(key);
ProtectedPrivateKey ppk = ProtectedPrivateKey.Protect(
privateKey, passphraseValue);
Expand Down Expand Up @@ -321,5 +322,18 @@ private PrivateKey ValidateRawHex(string rawKeyHex)

return key;
}

private Stream PathHandler(string pathString)
{
if (pathString == string.Empty)
{
return System.Console.OpenStandardOutput();
}
else
{
string path = Path.GetFullPath(pathString);
return File.Open(path, FileMode.OpenOrCreate);
}
}
}
}

0 comments on commit 3f29c38

Please sign in to comment.