Skip to content

Commit

Permalink
export keys in cli;
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Oct 22, 2016
1 parent e4d4df5 commit 02597dd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
4 changes: 2 additions & 2 deletions AntSharesCore/Core/RegisterTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ protected override void DeserializeExclusiveData(BinaryReader reader)
Name = reader.ReadVarString();
Amount = reader.ReadSerializable<Fixed8>();
if (Amount == Fixed8.Zero || Amount < -Fixed8.Satoshi) throw new FormatException();
if (AssetType == AssetType.Share && Amount <= Fixed8.Zero)
throw new FormatException();
if (AssetType == AssetType.Invoice && Amount != -Fixed8.Satoshi)
throw new FormatException();
Precision = reader.ReadByte();
if (Precision > 8) throw new FormatException();
if (AssetType == AssetType.Share && Precision != 0)
throw new FormatException();
Issuer = ECPoint.DeserializeFrom(reader, ECCurve.Secp256r1);
Admin = reader.ReadSerializable<UInt160>();
}
Expand Down
70 changes: 70 additions & 0 deletions AntSharesDaemon/Shell/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected override bool OnCommand(string[] args)
{
case "create":
return OnCreateCommand(args);
case "export":
return OnExportCommand(args);
case "help":
return OnHelpCommand(args);
case "import":
Expand Down Expand Up @@ -119,6 +121,73 @@ private bool OnCreateWalletCommand(string[] args)
return true;
}

private bool OnExportCommand(string[] args)
{
switch (args[1].ToLower())
{
case "key":
return OnExportKeyCommand(args);
default:
return base.OnCommand(args);
}
}

private bool OnExportKeyCommand(string[] args)
{
if (Program.Wallet == null)
{
Console.WriteLine("You have to open the wallet first.");
return true;
}
if (args.Length < 2 || args.Length > 4)
{
Console.WriteLine("error");
return true;
}
UInt160 scriptHash = null;
string path = null;
if (args.Length == 3)
{
try
{
scriptHash = Wallet.ToScriptHash(args[2]);
}
catch (FormatException)
{
path = args[2];
}
}
else if (args.Length == 4)
{
scriptHash = Wallet.ToScriptHash(args[2]);
path = args[3];
}
using (SecureString password = ReadSecureString("password"))
{
if (password.Length == 0)
{
Console.WriteLine("cancelled");
return true;
}
if (!Program.Wallet.VerifyPassword(password))
{
Console.WriteLine("Incorrect password");
return true;
}
}
IEnumerable<Account> accounts;
if (scriptHash == null)
accounts = Program.Wallet.GetAccounts();
else
accounts = new[] { Program.Wallet.GetAccountByScriptHash(scriptHash) };
if (path == null)
foreach (Account account in accounts)
Console.WriteLine(account.Export());
else
File.WriteAllLines(path, accounts.Select(p => p.Export()));
return true;
}

private bool OnHelpCommand(string[] args)
{
Console.Write(
Expand All @@ -136,6 +205,7 @@ private bool OnHelpCommand(string[] args)
"\tlist asset\n" +
"\tcreate address [n=1]\n" +
"\timport key <wif|path>\n" +
"\texport key [address] [path]\n" +
"\tsend <id|alias> <address> <value> [fee=0]\n" +
"Node Commands:\n" +
"\tshow state\n" +
Expand Down
4 changes: 1 addition & 3 deletions AntSharesUI/UI/AssetRegisterDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public RegisterTransaction GetTransaction()
AssetType = (AssetType)comboBox1.SelectedItem,
Name = (AssetType)comboBox1.SelectedItem == AssetType.Share ? string.Empty : $"[{{\"lang\":\"{CultureInfo.CurrentCulture.Name}\",\"name\":\"{textBox1.Text}\"}}]",
Amount = checkBox1.Checked ? Fixed8.Parse(textBox2.Text) : -Fixed8.Satoshi,
Precision = 8,
Precision = (AssetType)comboBox1.SelectedItem == AssetType.Share ? (byte)0 : (byte)8,
Issuer = (ECPoint)comboBox2.SelectedItem,
Admin = Wallet.ToScriptHash(comboBox3.Text),
Outputs = new TransactionOutput[0]
Expand All @@ -39,8 +39,6 @@ private void AssetRegisterDialog_Load(object sender, EventArgs e)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Enabled = (AssetType)comboBox1.SelectedItem != AssetType.Share;
checkBox1.Enabled = (AssetType)comboBox1.SelectedItem == AssetType.Token;
if ((AssetType)comboBox1.SelectedItem == AssetType.Share) checkBox1.Checked = true;
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
Expand Down

0 comments on commit 02597dd

Please sign in to comment.