From 8f3fc0143a26ea7cf1747a944da73475e461b857 Mon Sep 17 00:00:00 2001 From: Owen Zhang <38493437+superboyiii@users.noreply.github.com> Date: Wed, 25 Aug 2021 13:55:18 +0800 Subject: [PATCH] Enable private key import in initial wallet creation (#815) * Enable empty wallet creation. * test * fix workflow * fix * improve * enable private key import in initial wallet creation * format * fix expect * improve * add error msg * revert NoWallet() * Add notice for duplicate address. * Update MainService.Wallet.cs * Fix gui * remove double check in OpenWallet Co-authored-by: Erik Zhang --- .github/workflows/test-neo-cli.expect | 27 ++++++++++++++++--- neo-cli/CLI/MainService.Wallet.cs | 37 +++++++++++++++------------ neo-cli/CLI/MainService.cs | 34 ++++++++++-------------- 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/.github/workflows/test-neo-cli.expect b/.github/workflows/test-neo-cli.expect index d726413b3..dbf41526b 100755 --- a/.github/workflows/test-neo-cli.expect +++ b/.github/workflows/test-neo-cli.expect @@ -5,7 +5,7 @@ set timeout 10 # Start neo-cli -spawn dotnet out/neo-cli.dll --rpc +spawn dotnet out/neo-cli.dll # Expect the main input prompt expect { @@ -17,12 +17,31 @@ expect { # # Test 'create wallet' # -send "create wallet test-wallet.json\n" +send "create wallet test-wallet1.json\n" expect { - "*(yes|no)" { send "yes\n"} + "password:" { send "asd\n" } + "error" { exit 2 } + timeout { exit 1 } +} + +expect { + "password:" { send "asd\n" } + "error" { exit 2 } + timeout { exit 1 } +} + +expect { + " Address:" { } + "error" { exit 2 } + timeout { exit 1 } } +# +# Test 'create wallet' +# +send "create wallet test-wallet2.json L2ArHTuiDL4FHu4nfyhamrG8XVYB4QyRbmhj7vD6hFMB5iAMSTf6\n" + expect { "password:" { send "asd\n" } "error" { exit 2 } @@ -36,7 +55,7 @@ expect { } expect { - " Address:" { } + "Address: NUj249PQg9EMJfAuxKizdJwMG7GSBzYX2Y" { } "error" { exit 2 } timeout { exit 1 } } diff --git a/neo-cli/CLI/MainService.Wallet.cs b/neo-cli/CLI/MainService.Wallet.cs index 8c8931b1e..b5e1b9478 100644 --- a/neo-cli/CLI/MainService.Wallet.cs +++ b/neo-cli/CLI/MainService.Wallet.cs @@ -63,11 +63,7 @@ private void OnOpenWallet(string path) [ConsoleCommand("close wallet", Category = "Wallet Commands")] private void OnCloseWalletCommand() { - if (CurrentWallet == null) - { - Console.WriteLine($"Wallet is not opened"); - return; - } + if (NoWallet()) return; CurrentWallet = null; Console.WriteLine($"Wallet is closed"); } @@ -112,7 +108,6 @@ private void OnUpgradeWalletCommand(string path) private void OnCreateAddressCommand(ushort count = 1) { if (NoWallet()) return; - string path = "address.txt"; if (File.Exists(path)) { @@ -187,7 +182,7 @@ private void OnExportKeyCommand(string path = null, UInt160 scriptHash = null) /// Process "create wallet" command /// [ConsoleCommand("create wallet", Category = "Wallet Commands")] - private void OnCreateWalletCommand(string path) + private void OnCreateWalletCommand(string path, string wifOrFile = null) { string password = ReadUserInput("password", true); if (password.Length == 0) @@ -195,20 +190,20 @@ private void OnCreateWalletCommand(string path) Console.WriteLine("Cancelled"); return; } - string password2 = ReadUserInput("password", true); + string password2 = ReadUserInput("repeat password", true); if (password != password2) { Console.WriteLine("Error"); return; } - if (!File.Exists(path)) - { - CreateWallet(path, password); - } - else + if (File.Exists(path)) { Console.WriteLine("This wallet already exists, please create another one."); + return; } + bool createDefaultAccount = wifOrFile is null; + CreateWallet(path, password, createDefaultAccount); + if (!createDefaultAccount) OnImportKeyCommand(wifOrFile); } /// @@ -220,7 +215,6 @@ private void OnCreateWalletCommand(string path) private void OnImportMultisigAddress(ushort m, ECPoint[] publicKeys) { if (NoWallet()) return; - int n = publicKeys.Length; if (m < 1 || m > n || n > 1024) @@ -245,6 +239,7 @@ private void OnImportMultisigAddress(ushort m, ECPoint[] publicKeys) [ConsoleCommand("import key", Category = "Wallet Commands")] private void OnImportKeyCommand(string wifOrFile) { + if (NoWallet()) return; byte[] prikey = null; try { @@ -301,6 +296,7 @@ private void OnImportKeyCommand(string wifOrFile) [ConsoleCommand("import watchonly", Category = "Wallet Commands")] private void OnImportWatchOnlyCommand(string addressOrFile) { + if (NoWallet()) return; UInt160 address = null; try { @@ -338,8 +334,16 @@ private void OnImportWatchOnlyCommand(string addressOrFile) } else { - WalletAccount account = CurrentWallet.CreateAccount(address); - Console.WriteLine($"Address: {account.Address}"); + WalletAccount account = CurrentWallet.GetAccount(address); + if (account is not null) + { + Console.WriteLine("This address is already in your wallet"); + } + else + { + account = CurrentWallet.CreateAccount(address); + Console.WriteLine($"Address: {account.Address}"); + } } if (CurrentWallet is NEP6Wallet wallet) wallet.Save(); @@ -352,7 +356,6 @@ private void OnImportWatchOnlyCommand(string addressOrFile) private void OnListAddressCommand() { if (NoWallet()) return; - var snapshot = NeoSystem.StoreView; foreach (var account in CurrentWallet.GetAccounts()) { diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index ccd426613..27f389766 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -133,36 +133,30 @@ public override void RunConsole() base.RunConsole(); } - public void CreateWallet(string path, string password) + public void CreateWallet(string path, string password, bool createDefaultAccount = true) { switch (Path.GetExtension(path)) { case ".db3": - { - UserWallet wallet = UserWallet.Create(path, password, NeoSystem.Settings); - WalletAccount account = wallet.CreateAccount(); - Console.WriteLine($" Address: {account.Address}"); - Console.WriteLine($" Pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); - Console.WriteLine($"ScriptHash: {account.ScriptHash}"); - CurrentWallet = wallet; - } + CurrentWallet = UserWallet.Create(path, password, NeoSystem.Settings); break; case ".json": - { - NEP6Wallet wallet = new NEP6Wallet(path, NeoSystem.Settings); - wallet.Unlock(password); - WalletAccount account = wallet.CreateAccount(); - wallet.Save(); - Console.WriteLine($" Address: {account.Address}"); - Console.WriteLine($" Pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); - Console.WriteLine($"ScriptHash: {account.ScriptHash}"); - CurrentWallet = wallet; - } + CurrentWallet = new NEP6Wallet(path, NeoSystem.Settings); + ((NEP6Wallet)CurrentWallet).Unlock(password); break; default: Console.WriteLine("Wallet files in that format are not supported, please use a .json or .db3 file extension."); - break; + return; + } + if (createDefaultAccount) + { + WalletAccount account = CurrentWallet.CreateAccount(); + Console.WriteLine($" Address: {account.Address}"); + Console.WriteLine($" Pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); + Console.WriteLine($"ScriptHash: {account.ScriptHash}"); } + if (CurrentWallet is NEP6Wallet wallet) + wallet.Save(); } private IEnumerable GetBlocks(Stream stream, bool read_start = false)