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

Commit

Permalink
Neo cli with daemon unlock config json (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiz-sharp authored and erikzhang committed May 15, 2018
1 parent 150b8f1 commit fba9874
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
2 changes: 1 addition & 1 deletion neo-cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 19 additions & 0 deletions neo-cli/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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"));
}
}

Expand Down Expand Up @@ -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);
}
}
}
}
51 changes: 29 additions & 22 deletions neo-cli/Shell/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions neo-cli/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"Port": 10332,
"SslCert": "",
"SslCertPassword": ""
},
"UnlockWallet": {
"Path": "",
"Password": "",
"IsActive": false
}
}
}
5 changes: 5 additions & 0 deletions neo-cli/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"Port": 10332,
"SslCert": "",
"SslCertPassword": ""
},
"UnlockWallet": {
"Path": "",
"Password": "",
"IsActive": false
}
}
}
5 changes: 5 additions & 0 deletions neo-cli/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"Port": 20332,
"SslCert": "",
"SslCertPassword": ""
},
"UnlockWallet": {
"Path": "",
"Password": "",
"IsActive": false
}
}
}

0 comments on commit fba9874

Please sign in to comment.