Skip to content

Commit

Permalink
add more commands for cli wallet; adjust ui;
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Oct 21, 2016
1 parent 5080d85 commit 0fda8b4
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 8 deletions.
120 changes: 115 additions & 5 deletions AntSharesDaemon/Shell/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using AntShares.Services;
using AntShares.Wallets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
Expand All @@ -30,10 +31,16 @@ protected override bool OnCommand(string[] args)
{
case "create":
return OnCreateCommand(args);
case "help":
return OnHelpCommand(args);
case "import":
return OnImportCommand(args);
case "list":
return OnListCommand(args);
case "open":
return OnOpenCommand(args);
case "rebuild":
return OnRebuildCommand(args);
case "send":
return OnSendCommand(args);
case "show":
Expand All @@ -49,13 +56,46 @@ private bool OnCreateCommand(string[] args)
{
switch (args[1].ToLower())
{
case "address":
return OnCreateAddressCommand(args);
case "wallet":
return OnCreateWalletCommand(args);
default:
return base.OnCommand(args);
}
}

private bool OnCreateAddressCommand(string[] args)
{
if (wallet == null)
{
Console.WriteLine("You have to open the wallet first.");
return true;
}
if (args.Length > 3)
{
Console.WriteLine("error");
return true;
}
ushort count = 1;
if (args.Length >= 3)
count = ushort.Parse(args[2]);
List<string> addresses = new List<string>();
for (int i = 1; i <= count; i++)
{
Account account = wallet.CreateAccount();
Contract contract = wallet.GetContracts(account.PublicKeyHash).First(p => p.IsStandard);
addresses.Add(contract.Address);
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write($"[{i}/{count}]");
}
Console.WriteLine();
string path = "address.txt";
Console.WriteLine($"export addresses to {path}");
File.WriteAllLines(path, addresses);
return true;
}

private bool OnCreateWalletCommand(string[] args)
{
if (args.Length < 3)
Expand All @@ -72,14 +112,66 @@ private bool OnCreateWalletCommand(string[] args)
return true;
}
wallet = UserWallet.Create(args[2], password);
foreach (Account account in wallet.GetAccounts())
{
Console.WriteLine(account.PublicKey.EncodePoint(true).ToHexString());
}
Contract contract = wallet.GetContracts().First(p => p.IsStandard);
Account account = wallet.GetAccount(contract.PublicKeyHash);
Console.WriteLine($"address: {contract.Address}");
Console.WriteLine($" pubkey: {account.PublicKey.EncodePoint(true).ToHexString()}");
}
return true;
}

private bool OnHelpCommand(string[] args)
{
Console.Write(
"Normal Commands:\n" +
"\tversion\n" +
"\thelp\n" +
"\tclear\n" +
"\texit\n" +
"Wallet Commands:\n" +
"\tcreate wallet <path>\n" +
"\topen wallet <path>\n" +
"\trebuild index\n" +
"\tlist account\n" +
"\tlist address\n" +
"\tlist asset\n" +
"\tcreate address [n=1]\n" +
"\timport key <wif>\n" +
"\tsend <id|alias> <address> <value> [fee=0]\n" +
"Node Commands:\n" +
"\tshow state\n" +
"\tshow node\n" +
"\tshow pool\n" +
"Advanced Commands:\n" +
"\tstart consensus\n");
return true;
}

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

private bool OnImportKeyCommand(string[] args)
{
if (args.Length > 3)
{
Console.WriteLine("error");
return true;
}
Account account = wallet.Import(args[2]);
Contract contract = wallet.GetContracts(account.PublicKeyHash).First(p => p.IsStandard);
Console.WriteLine($"address: {contract.Address}");
Console.WriteLine($" pubkey: {account.PublicKey.EncodePoint(true).ToHexString()}");
return true;
}

private bool OnListCommand(string[] args)
{
switch (args[1].ToLower())
Expand Down Expand Up @@ -110,7 +202,7 @@ private bool OnListAddressCommand(string[] args)
if (wallet == null) return true;
foreach (Contract contract in wallet.GetContracts())
{
Console.WriteLine($"{contract.Address}\t{contract.GetType()}");
Console.WriteLine($"{contract.Address}\t{(contract.IsStandard ? "Standard" : "Nonstandard")}");
}
return true;
}
Expand Down Expand Up @@ -175,6 +267,24 @@ private bool OnOpenWalletCommand(string[] args)
return true;
}

private bool OnRebuildCommand(string[] args)
{
switch (args[1].ToLower())
{
case "index":
return OnRebuildIndexCommand(args);
default:
return base.OnCommand(args);
}
}

private bool OnRebuildIndexCommand(string[] args)
{
if (wallet == null) return true;
wallet.Rebuild();
return true;
}

private bool OnSendCommand(string[] args)
{
if (wallet == null)
Expand Down
7 changes: 6 additions & 1 deletion AntSharesUI/UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
Expand Down Expand Up @@ -502,7 +503,11 @@ private void 查看私钥VToolStripMenuItem_Click(object sender, EventArgs e)

private void 复制到剪贴板CToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetText(listView1.SelectedItems[0].Text);
try
{
Clipboard.SetText(listView1.SelectedItems[0].Text);
}
catch (ExternalException) { }
}

private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion AntSharesUI/UI/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
<value>261, 22</value>
</data>
<data name="重建钱包数据库RToolStripMenuItem.Text" xml:space="preserve">
<value>Wallet Database &amp;Reconstruction</value>
<value>&amp;Rebuild Index</value>
</data>
<data name="toolStripSeparator2.Size" type="System.Drawing.Size, System.Drawing">
<value>258, 6</value>
Expand Down
2 changes: 1 addition & 1 deletion AntSharesUI/UI/MainForm.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<value>187, 22</value>
</data>
<data name="重建钱包数据库RToolStripMenuItem.Text" xml:space="preserve">
<value>重建钱包数据库(&amp;R)</value>
<value>重建钱包索引(&amp;R)</value>
</data>
<data name="toolStripSeparator2.Size" type="System.Drawing.Size, System.Drawing">
<value>184, 6</value>
Expand Down

0 comments on commit 0fda8b4

Please sign in to comment.