Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Enable private key import in initial wallet creation #815

Merged
merged 18 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions .github/workflows/test-neo-cli.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
Expand All @@ -36,7 +55,7 @@ expect {
}

expect {
" Address:" { }
"Address: NUj249PQg9EMJfAuxKizdJwMG7GSBzYX2Y" { }
"error" { exit 2 }
timeout { exit 1 }
}
Expand Down
37 changes: 20 additions & 17 deletions neo-cli/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -187,28 +182,28 @@ private void OnExportKeyCommand(string path = null, UInt160 scriptHash = null)
/// Process "create wallet" command
/// </summary>
[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)
{
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);
}

/// <summary>
Expand All @@ -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)
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand All @@ -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())
{
Expand Down
34 changes: 14 additions & 20 deletions neo-cli/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> GetBlocks(Stream stream, bool read_start = false)
Expand Down