From 0fda8b46fd4411b6f0e4260b54c08a900c849d99 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Fri, 21 Oct 2016 18:43:23 +0800 Subject: [PATCH] add more commands for cli wallet; adjust ui; --- AntSharesDaemon/Shell/MainService.cs | 120 +++++++++++++++++++++++++-- AntSharesUI/UI/MainForm.cs | 7 +- AntSharesUI/UI/MainForm.resx | 2 +- AntSharesUI/UI/MainForm.zh-Hans.resx | 2 +- 4 files changed, 123 insertions(+), 8 deletions(-) diff --git a/AntSharesDaemon/Shell/MainService.cs b/AntSharesDaemon/Shell/MainService.cs index 64b3e9bf6b..913b21e370 100644 --- a/AntSharesDaemon/Shell/MainService.cs +++ b/AntSharesDaemon/Shell/MainService.cs @@ -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; @@ -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": @@ -49,6 +56,8 @@ private bool OnCreateCommand(string[] args) { switch (args[1].ToLower()) { + case "address": + return OnCreateAddressCommand(args); case "wallet": return OnCreateWalletCommand(args); default: @@ -56,6 +65,37 @@ private bool OnCreateCommand(string[] 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 addresses = new List(); + 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) @@ -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 \n" + + "\topen wallet \n" + + "\trebuild index\n" + + "\tlist account\n" + + "\tlist address\n" + + "\tlist asset\n" + + "\tcreate address [n=1]\n" + + "\timport key \n" + + "\tsend
[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()) @@ -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; } @@ -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) diff --git a/AntSharesUI/UI/MainForm.cs b/AntSharesUI/UI/MainForm.cs index 902ea3e4f1..0e03e8e4a2 100644 --- a/AntSharesUI/UI/MainForm.cs +++ b/AntSharesUI/UI/MainForm.cs @@ -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; @@ -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) diff --git a/AntSharesUI/UI/MainForm.resx b/AntSharesUI/UI/MainForm.resx index 109b529052..4876920bc7 100644 --- a/AntSharesUI/UI/MainForm.resx +++ b/AntSharesUI/UI/MainForm.resx @@ -153,7 +153,7 @@ 261, 22 - Wallet Database &Reconstruction + &Rebuild Index 258, 6 diff --git a/AntSharesUI/UI/MainForm.zh-Hans.resx b/AntSharesUI/UI/MainForm.zh-Hans.resx index 88a393318d..ab9ccbc4b5 100644 --- a/AntSharesUI/UI/MainForm.zh-Hans.resx +++ b/AntSharesUI/UI/MainForm.zh-Hans.resx @@ -143,7 +143,7 @@ 187, 22 - 重建钱包数据库(&R) + 重建钱包索引(&R) 184, 6