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

Commit

Permalink
NEP-6 (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang authored Dec 12, 2017
1 parent 46c5431 commit 5fe57ee
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 78 deletions.
12 changes: 5 additions & 7 deletions neo-cli/Network/RPC/RpcServerWithWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override JObject Process(string method, JArray _params)
if (context.Completed)
{
tx.Scripts = context.GetScripts();
Program.Wallet.SaveTransaction(tx);
Program.Wallet.ApplyTransaction(tx);
LocalNode.Relay(tx);
return tx.ToJson();
}
Expand Down Expand Up @@ -104,7 +104,7 @@ protected override JObject Process(string method, JArray _params)
if (context.Completed)
{
tx.Scripts = context.GetScripts();
Program.Wallet.SaveTransaction(tx);
Program.Wallet.ApplyTransaction(tx);
LocalNode.Relay(tx);
return tx.ToJson();
}
Expand All @@ -118,18 +118,16 @@ protected override JObject Process(string method, JArray _params)
throw new RpcException(-400, "Access denied");
else
{
KeyPair key = Program.Wallet.CreateKey();
VerificationContract contract = Program.Wallet.GetContracts(key.PublicKeyHash).First(p => p.IsStandard);
return contract.Address;
return Program.Wallet.CreateAccount().Address;
}
case "dumpprivkey":
if (Program.Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
UInt160 scriptHash = Wallet.ToScriptHash(_params[0].AsString());
KeyPair key = Program.Wallet.GetKeyByScriptHash(scriptHash);
return key.Export();
WalletAccount account = Program.Wallet.GetAccount(scriptHash);
return account.GetKey().Export();
}
default:
return base.Process(method, _params);
Expand Down
6 changes: 3 additions & 3 deletions neo-cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Neo.Implementations.Wallets.EntityFramework;
using Neo.Shell;
using Neo.Shell;
using Neo.Wallets;
using System;
using System.IO;

namespace Neo
{
static class Program
{
internal static UserWallet Wallet;
internal static Wallet Wallet;

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Expand Down
34 changes: 33 additions & 1 deletion neo-cli/Services/ConsoleServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ protected virtual bool OnCommand(string[] args)

protected internal abstract void OnStop();

public static string ReadPassword(string prompt)
{
const string t = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
StringBuilder sb = new StringBuilder();
ConsoleKeyInfo key;
Console.Write(prompt);
Console.Write(": ");

Console.ForegroundColor = ConsoleColor.Yellow;

do
{
key = Console.ReadKey(true);
if (t.IndexOf(key.KeyChar) != -1)
{
sb.Append(key.KeyChar);
Console.Write('*');
}
else if (key.Key == ConsoleKey.Backspace && sb.Length > 0)
{
sb.Length--;
Console.Write(key.KeyChar);
Console.Write(' ');
Console.Write(key.KeyChar);
}
} while (key.Key != ConsoleKey.Enter);

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
return sb.ToString();
}

public static SecureString ReadSecureString(string prompt)
{
const string t = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
Expand Down Expand Up @@ -117,7 +149,7 @@ private void RunConsole()
}
}

Console.ResetColor();
Console.ResetColor();
}
}
}
2 changes: 1 addition & 1 deletion neo-cli/Shell/Coins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private Transaction SignTransaction(Transaction tx)
if (context.Completed)
{
context.Verifiable.Scripts = context.GetScripts();
current_wallet.SaveTransaction(tx);
current_wallet.ApplyTransaction(tx);

bool relay_result = local_node.Relay(tx);

Expand Down
156 changes: 92 additions & 64 deletions neo-cli/Shell/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.Core;
using Neo.Implementations.Blockchains.LevelDB;
using Neo.Implementations.Wallets.EntityFramework;
using Neo.Implementations.Wallets.NEP6;
using Neo.IO;
using Neo.IO.Json;
using Neo.Network;
Expand All @@ -15,7 +16,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace Neo.Shell
Expand Down Expand Up @@ -117,12 +118,13 @@ private bool OnCreateAddressCommand(string[] args)
List<string> addresses = new List<string>();
for (int i = 1; i <= count; i++)
{
KeyPair key = Program.Wallet.CreateKey();
VerificationContract contract = Program.Wallet.GetContracts(key.PublicKeyHash).First(p => p.IsStandard);
addresses.Add(contract.Address);
WalletAccount account = Program.Wallet.CreateAccount();
addresses.Add(account.Address);
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write($"[{i}/{count}]");
}
if (Program.Wallet is NEP6Wallet wallet)
wallet.Save();
Console.WriteLine();
string path = "address.txt";
Console.WriteLine($"export addresses to {path}");
Expand All @@ -137,20 +139,26 @@ private bool OnCreateWalletCommand(string[] args)
Console.WriteLine("error");
return true;
}
using (SecureString password = ReadSecureString("password"))
using (SecureString password2 = ReadSecureString("password"))
string path = args[2];
if (Path.GetExtension(path) == ".db3")
{
if (!password.CompareTo(password2))
{
Console.WriteLine("error");
return true;
}
Program.Wallet = UserWallet.Create(args[2], password);
Console.WriteLine("The wallet file in the db3 format is not supported.");
return true;
}
string password = ReadPassword("password");
string password2 = ReadPassword("password");
if (password != password2)
{
Console.WriteLine("error");
return true;
}
VerificationContract contract = Program.Wallet.GetContracts().First(p => p.IsStandard);
KeyPair key = Program.Wallet.GetKey(contract.PublicKeyHash);
Console.WriteLine($"address: {contract.Address}");
Console.WriteLine($" pubkey: {key.PublicKey.EncodePoint(true).ToHexString()}");
NEP6Wallet wallet = new NEP6Wallet(path);
wallet.Unlock(password);
WalletAccount account = wallet.CreateAccount();
wallet.Save();
Program.Wallet = wallet;
Console.WriteLine($"address: {account.Address}");
Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}");
return true;
}

Expand Down Expand Up @@ -233,24 +241,22 @@ private bool OnExportKeyCommand(string[] args)
scriptHash = Wallet.ToScriptHash(args[2]);
path = args[3];
}
using (SecureString password = ReadSecureString("password"))
string password = ReadPassword("password");
if (password.Length == 0)
{
if (password.Length == 0)
{
Console.WriteLine("cancelled");
return true;
}
if (!Program.Wallet.VerifyPassword(password))
{
Console.WriteLine("Incorrect password");
return true;
}
Console.WriteLine("cancelled");
return true;
}
if (!Program.Wallet.VerifyPassword(password))
{
Console.WriteLine("Incorrect password");
return true;
}
IEnumerable<KeyPair> keys;
if (scriptHash == null)
keys = Program.Wallet.GetKeys();
keys = Program.Wallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey());
else
keys = new[] { Program.Wallet.GetKeyByScriptHash(scriptHash) };
keys = new[] { Program.Wallet.GetAccount(scriptHash).GetKey() };
if (path == null)
foreach (KeyPair key in keys)
Console.WriteLine(key.Export());
Expand Down Expand Up @@ -326,7 +332,7 @@ private bool OnImportKeyCommand(string[] args)
prikey = lines[i].HexToBytes();
else
prikey = Wallet.GetPrivateKeyFromWIF(lines[i]);
Program.Wallet.CreateKey(prikey);
Program.Wallet.CreateAccount(prikey);
Array.Clear(prikey, 0, prikey.Length);
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write($"[{i + 1}/{lines.Length}]");
Expand All @@ -335,12 +341,13 @@ private bool OnImportKeyCommand(string[] args)
}
else
{
KeyPair key = Program.Wallet.CreateKey(prikey);
WalletAccount account = Program.Wallet.CreateAccount(prikey);
Array.Clear(prikey, 0, prikey.Length);
VerificationContract contract = Program.Wallet.GetContracts(key.PublicKeyHash).First(p => p.IsStandard);
Console.WriteLine($"address: {contract.Address}");
Console.WriteLine($" pubkey: {key.PublicKey.EncodePoint(true).ToHexString()}");
Console.WriteLine($"address: {account.Address}");
Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}");
}
if (Program.Wallet is NEP6Wallet wallet)
wallet.Save();
return true;
}

Expand Down Expand Up @@ -400,7 +407,7 @@ private bool OnShowGasCommand(string[] args)
private bool OnListKeyCommand(string[] args)
{
if (Program.Wallet == null) return true;
foreach (KeyPair key in Program.Wallet.GetKeys())
foreach (KeyPair key in Program.Wallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey()))
{
Console.WriteLine(key.PublicKey);
}
Expand All @@ -410,7 +417,7 @@ private bool OnListKeyCommand(string[] args)
private bool OnListAddressCommand(string[] args)
{
if (Program.Wallet == null) return true;
foreach (VerificationContract contract in Program.Wallet.GetContracts())
foreach (Contract contract in Program.Wallet.GetAccounts().Where(p => !p.WatchOnly).Select(p => p.Contract))
{
Console.WriteLine($"{contract.Address}\t{(contract.IsStandard ? "Standard" : "Nonstandard")}");
}
Expand Down Expand Up @@ -457,27 +464,43 @@ private bool OnOpenWalletCommand(string[] args)
Console.WriteLine("error");
return true;
}
if (!File.Exists(args[2]))
string path = args[2];
if (!File.Exists(path))
{
Console.WriteLine($"File does not exist");
return true;
}
using (SecureString password = ReadSecureString("password"))
string password = ReadPassword("password");
if (password.Length == 0)
{
Console.WriteLine("cancelled");
return true;
}
if (Path.GetExtension(path) == ".db3")
{
if (password.Length == 0)
try
{
Program.Wallet = UserWallet.Open(path, password);
}
catch (CryptographicException)
{
Console.WriteLine("cancelled");
Console.WriteLine($"failed to open file \"{path}\"");
return true;
}
}
else
{
NEP6Wallet nep6wallet = new NEP6Wallet(path);
try
{
Program.Wallet = UserWallet.Open(args[2], password);
nep6wallet.Unlock(password);
}
catch
catch (CryptographicException)
{
Console.WriteLine($"failed to open file \"{args[2]}\"");
Console.WriteLine($"failed to open file \"{path}\"");
return true;
}
Program.Wallet = nep6wallet;
}
return true;
}
Expand All @@ -495,8 +518,7 @@ private bool OnRebuildCommand(string[] args)

private bool OnRebuildIndexCommand(string[] args)
{
if (Program.Wallet == null) return true;
Program.Wallet.Rebuild();
WalletIndexer.RebuildIndex();
return true;
}

Expand Down Expand Up @@ -530,18 +552,16 @@ private bool OnSendCommand(string[] args)
Console.WriteLine("You have to open the wallet first.");
return true;
}
using (SecureString password = ReadSecureString("password"))
string password = ReadPassword("password");
if (password.Length == 0)
{
if (password.Length == 0)
{
Console.WriteLine("cancelled");
return true;
}
if (!Program.Wallet.VerifyPassword(password))
{
Console.WriteLine("Incorrect password");
return true;
}
Console.WriteLine("cancelled");
return true;
}
if (!Program.Wallet.VerifyPassword(password))
{
Console.WriteLine("Incorrect password");
return true;
}
UIntBase assetId;
switch (args[1].ToLower())
Expand Down Expand Up @@ -608,7 +628,7 @@ private bool OnSendCommand(string[] args)
if (context.Completed)
{
tx.Scripts = context.GetScripts();
Program.Wallet.SaveTransaction(tx);
Program.Wallet.ApplyTransaction(tx);
LocalNode.Relay(tx);
Console.WriteLine($"TXID: {tx.Hash}");
}
Expand Down Expand Up @@ -819,17 +839,25 @@ private bool OnUpgradeWalletCommand(string[] args)
return true;
}
string path = args[2];
if (Path.GetExtension(path) != ".db3")
{
Console.WriteLine("Can't upgrade the wallet file.");
return true;
}
if (!File.Exists(path))
{
Console.WriteLine("File does not exist");
Console.WriteLine("File does not exist.");
return true;
}
string password = ReadPassword("password");
if (password.Length == 0)
{
Console.WriteLine("cancelled");
return true;
}
string path_old = Path.ChangeExtension(path, ".old.db3");
string path_new = Path.ChangeExtension(path, ".new.db3");
UserWallet.Migrate(path, path_new);
File.Move(path, path_old);
File.Move(path_new, path);
Console.WriteLine($"Wallet file upgrade complete. Old file has been auto-saved at: {path_old}");
string path_new = Path.ChangeExtension(path, ".json");
NEP6Wallet.Migrate(path_new, path, password).Save();
Console.WriteLine($"Wallet file upgrade complete. New wallet file has been auto-saved at: {path_new}");
return true;
}

Expand Down
Loading

0 comments on commit 5fe57ee

Please sign in to comment.