From fba987466c29d36b073b86eadda39f39f08fc2b5 Mon Sep 17 00:00:00 2001 From: PurpleSoft <33333411+purplesoft-io@users.noreply.github.com> Date: Tue, 15 May 2018 10:37:11 +0200 Subject: [PATCH] Neo cli with daemon unlock config json (#152) --- neo-cli/Program.cs | 2 +- neo-cli/Settings.cs | 19 ++++++++++++++ neo-cli/Shell/MainService.cs | 51 ++++++++++++++++++++---------------- neo-cli/config.json | 5 ++++ neo-cli/config.mainnet.json | 5 ++++ neo-cli/config.testnet.json | 5 ++++ 6 files changed, 64 insertions(+), 23 deletions(-) diff --git a/neo-cli/Program.cs b/neo-cli/Program.cs index 01c8a38a4..084d0787e 100644 --- a/neo-cli/Program.cs +++ b/neo-cli/Program.cs @@ -7,7 +7,7 @@ namespace Neo { static class Program { - internal static Wallet Wallet; + internal static Wallet Wallet; private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { diff --git a/neo-cli/Settings.cs b/neo-cli/Settings.cs index 80eac8a3c..9a62a74a8 100644 --- a/neo-cli/Settings.cs +++ b/neo-cli/Settings.cs @@ -8,6 +8,7 @@ internal class Settings public PathsSettings Paths { get; } public P2PSettings P2P { get; } public RPCSettings RPC { get; } + public UnlockWalletSettings UnlockWallet { get; set; } public static Settings Default { get; } @@ -22,6 +23,7 @@ public Settings(IConfigurationSection section) this.Paths = new PathsSettings(section.GetSection("Paths")); this.P2P = new P2PSettings(section.GetSection("P2P")); this.RPC = new RPCSettings(section.GetSection("RPC")); + this.UnlockWallet = new UnlockWalletSettings(section.GetSection("UnlockWallet")); } } @@ -62,4 +64,21 @@ public RPCSettings(IConfigurationSection section) this.SslCertPassword = section.GetSection("SslCertPassword").Value; } } + + public class UnlockWalletSettings + { + public string Path { get; } + public string Password { get; } + public bool IsActive { get; } + + public UnlockWalletSettings(IConfigurationSection section) + { + if (section.Value != null) + { + this.Path = section.GetSection("WalletPath").Value; + this.Password = section.GetSection("WalletPassword").Value; + this.IsActive = bool.Parse(section.GetSection("IsActive").Value); + } + } + } } diff --git a/neo-cli/Shell/MainService.cs b/neo-cli/Shell/MainService.cs index 6a19669d7..21c9a6c17 100644 --- a/neo-cli/Shell/MainService.cs +++ b/neo-cli/Shell/MainService.cs @@ -589,31 +589,13 @@ private bool OnOpenWalletCommand(string[] args) Console.WriteLine("cancelled"); return true; } - if (Path.GetExtension(path) == ".db3") + try { - try - { - Program.Wallet = UserWallet.Open(path, password); - } - catch (CryptographicException) - { - Console.WriteLine($"failed to open file \"{path}\""); - return true; - } + Program.Wallet = OpenWallet(path, password); } - else + catch (CryptographicException) { - NEP6Wallet nep6wallet = new NEP6Wallet(path); - try - { - nep6wallet.Unlock(password); - } - catch (CryptographicException) - { - Console.WriteLine($"failed to open file \"{path}\""); - return true; - } - Program.Wallet = nep6wallet; + Console.WriteLine($"failed to open file \"{path}\""); } return true; } @@ -916,6 +898,17 @@ protected internal override void OnStart(string[] args) } } LocalNode.Start(Settings.Default.P2P.Port, Settings.Default.P2P.WsPort); + if (Settings.Default.UnlockWallet.IsActive) + { + try + { + Program.Wallet = OpenWallet(Settings.Default.UnlockWallet.Path, Settings.Default.UnlockWallet.Password); + } + catch (CryptographicException) + { + Console.WriteLine($"failed to open file \"{Settings.Default.UnlockWallet.Path}\""); + } + } if (useRPC) { rpc = new RpcServerWithWallet(LocalNode); @@ -1003,6 +996,20 @@ private bool OnUpgradeWalletCommand(string[] args) return true; } + private static Wallet OpenWallet(string path, string password) + { + if (Path.GetExtension(path) == ".db3") + { + return UserWallet.Open(path, password); + } + else + { + NEP6Wallet nep6wallet = new NEP6Wallet(path); + nep6wallet.Unlock(password); + return nep6wallet; + } + } + private void LevelDBBlockchain_ApplicationExecuted(object sender, ApplicationExecutedEventArgs e) { JObject json = new JObject(); diff --git a/neo-cli/config.json b/neo-cli/config.json index 235a4263d..2e10ebdc9 100644 --- a/neo-cli/config.json +++ b/neo-cli/config.json @@ -12,6 +12,11 @@ "Port": 10332, "SslCert": "", "SslCertPassword": "" + }, + "UnlockWallet": { + "Path": "", + "Password": "", + "IsActive": false } } } diff --git a/neo-cli/config.mainnet.json b/neo-cli/config.mainnet.json index 235a4263d..02c0a2877 100644 --- a/neo-cli/config.mainnet.json +++ b/neo-cli/config.mainnet.json @@ -12,6 +12,11 @@ "Port": 10332, "SslCert": "", "SslCertPassword": "" + }, + "UnlockWallet": { + "Path": "", + "Password": "", + "IsActive": false } } } diff --git a/neo-cli/config.testnet.json b/neo-cli/config.testnet.json index 5ca87762a..4ac0fc1a7 100644 --- a/neo-cli/config.testnet.json +++ b/neo-cli/config.testnet.json @@ -12,6 +12,11 @@ "Port": 20332, "SslCert": "", "SslCertPassword": "" + }, + "UnlockWallet": { + "Path": "", + "Password": "", + "IsActive": false } } }