Skip to content

Commit 0e2b178

Browse files
author
Erik Zhang
committed
rebrand to NEO
1 parent f1b0039 commit 0e2b178

File tree

14 files changed

+1372
-0
lines changed

14 files changed

+1372
-0
lines changed

neo-cli.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26430.15
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "neo-cli", "neo-cli\neo-cli.csproj", "{900CA179-AEF0-43F3-9833-5DB060272D8E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Neo.Core;
2+
using Neo.Network;
3+
using Neo.Wallets;
4+
using System;
5+
using System.IO;
6+
using System.Linq;
7+
8+
namespace Neo.Consensus
9+
{
10+
internal class ConsensusWithPolicy : ConsensusService
11+
{
12+
private string log_dictionary;
13+
14+
public ConsensusWithPolicy(LocalNode localNode, Wallet wallet, string log_dictionary)
15+
: base(localNode, wallet)
16+
{
17+
this.log_dictionary = log_dictionary;
18+
}
19+
20+
protected override bool CheckPolicy(Transaction tx)
21+
{
22+
switch (Policy.Default.PolicyLevel)
23+
{
24+
case PolicyLevel.AllowAll:
25+
return true;
26+
case PolicyLevel.AllowList:
27+
return tx.Scripts.All(p => Policy.Default.List.Contains(p.VerificationScript.ToScriptHash())) || tx.Outputs.All(p => Policy.Default.List.Contains(p.ScriptHash));
28+
case PolicyLevel.DenyList:
29+
return tx.Scripts.All(p => !Policy.Default.List.Contains(p.VerificationScript.ToScriptHash())) && tx.Outputs.All(p => !Policy.Default.List.Contains(p.ScriptHash));
30+
default:
31+
return base.CheckPolicy(tx);
32+
}
33+
}
34+
35+
protected override void Log(string message)
36+
{
37+
DateTime now = DateTime.Now;
38+
string line = $"[{now.TimeOfDay:hh\\:mm\\:ss}] {message}";
39+
Console.WriteLine(line);
40+
if (string.IsNullOrEmpty(log_dictionary)) return;
41+
lock (log_dictionary)
42+
{
43+
Directory.CreateDirectory(log_dictionary);
44+
string path = Path.Combine(log_dictionary, $"{now:yyyy-MM-dd}.log");
45+
File.AppendAllLines(path, new[] { line });
46+
}
47+
}
48+
49+
public void RefreshPolicy()
50+
{
51+
Policy.Default.Refresh();
52+
}
53+
}
54+
}

neo-cli/Consensus/Policy.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Neo.Wallets;
2+
using Microsoft.Extensions.Configuration;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
8+
namespace Neo.Consensus
9+
{
10+
internal class Policy
11+
{
12+
public PolicyLevel PolicyLevel { get; private set; }
13+
public HashSet<UInt160> List { get; private set; }
14+
15+
public static Policy Default { get; private set; }
16+
17+
static Policy()
18+
{
19+
Default = new Policy();
20+
Default.Refresh();
21+
}
22+
23+
public void Refresh()
24+
{
25+
if (File.Exists("policy.json"))
26+
{
27+
IConfigurationSection section = new ConfigurationBuilder().AddJsonFile("policy.json").Build().GetSection("PolicyConfiguration");
28+
PolicyLevel = (PolicyLevel)Enum.Parse(typeof(PolicyLevel), section.GetSection("PolicyLevel").Value, true);
29+
List = new HashSet<UInt160>(section.GetSection("List").GetChildren().Select(p => Wallet.ToScriptHash(p.Value)));
30+
}
31+
else
32+
{
33+
PolicyLevel = PolicyLevel.AllowAll;
34+
List = new HashSet<UInt160>();
35+
}
36+
}
37+
}
38+
}

neo-cli/Consensus/PolicyLevel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Neo.Consensus
2+
{
3+
internal enum PolicyLevel : byte
4+
{
5+
AllowAll,
6+
DenyAll,
7+
AllowList,
8+
DenyList
9+
}
10+
}

neo-cli/Helper.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Security;
4+
5+
namespace Neo
6+
{
7+
internal static class Helper
8+
{
9+
public static bool CompareTo(this SecureString s1, SecureString s2)
10+
{
11+
if (s1.Length != s2.Length)
12+
return false;
13+
IntPtr p1 = IntPtr.Zero;
14+
IntPtr p2 = IntPtr.Zero;
15+
try
16+
{
17+
p1 = SecureStringMarshal.SecureStringToGlobalAllocAnsi(s1);
18+
p2 = SecureStringMarshal.SecureStringToGlobalAllocAnsi(s2);
19+
int i = 0;
20+
while (true)
21+
{
22+
byte b1 = Marshal.ReadByte(p1, i);
23+
byte b2 = Marshal.ReadByte(p2, i++);
24+
if (b1 == 0 && b2 == 0)
25+
return true;
26+
if (b1 != b2)
27+
return false;
28+
if (b1 == 0 || b2 == 0)
29+
return false;
30+
}
31+
}
32+
finally
33+
{
34+
Marshal.ZeroFreeGlobalAllocAnsi(p1);
35+
Marshal.ZeroFreeGlobalAllocAnsi(p2);
36+
}
37+
}
38+
}
39+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Neo.Core;
2+
using Neo.IO.Json;
3+
using Neo.Wallets;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace Neo.Network.RPC
8+
{
9+
internal class RpcServerWithWallet : RpcServer
10+
{
11+
public RpcServerWithWallet(LocalNode localNode)
12+
: base(localNode)
13+
{
14+
}
15+
16+
protected override JObject Process(string method, JArray _params)
17+
{
18+
switch (method)
19+
{
20+
case "getbalance":
21+
if (Program.Wallet == null)
22+
throw new RpcException(-400, "Access denied.");
23+
else
24+
{
25+
UInt256 assetId = UInt256.Parse(_params[0].AsString());
26+
IEnumerable<Coin> coins = Program.Wallet.GetCoins().Where(p => !p.State.HasFlag(CoinState.Spent) && p.Output.AssetId.Equals(assetId));
27+
JObject json = new JObject();
28+
json["balance"] = coins.Sum(p => p.Output.Value).ToString();
29+
json["confirmed"] = coins.Where(p => p.State.HasFlag(CoinState.Confirmed)).Sum(p => p.Output.Value).ToString();
30+
return json;
31+
}
32+
case "sendtoaddress":
33+
if (Program.Wallet == null)
34+
throw new RpcException(-400, "Access denied");
35+
else
36+
{
37+
UInt256 assetId = UInt256.Parse(_params[0].AsString());
38+
UInt160 scriptHash = Wallet.ToScriptHash(_params[1].AsString());
39+
Fixed8 value = Fixed8.Parse(_params[2].AsString());
40+
Fixed8 fee = _params.Count >= 4 ? Fixed8.Parse(_params[3].AsString()) : Fixed8.Zero;
41+
UInt160 change_address = _params.Count >= 5 ? Wallet.ToScriptHash(_params[4].AsString()) : null;
42+
if (value <= Fixed8.Zero)
43+
throw new RpcException(-32602, "Invalid params");
44+
ContractTransaction tx = Program.Wallet.MakeTransaction(new ContractTransaction
45+
{
46+
Outputs = new[]
47+
{
48+
new TransactionOutput
49+
{
50+
AssetId = assetId,
51+
Value = value,
52+
ScriptHash = scriptHash
53+
}
54+
}
55+
}, change_address: change_address, fee: fee);
56+
if (tx == null)
57+
throw new RpcException(-300, "Insufficient funds");
58+
SignatureContext context = new SignatureContext(tx);
59+
Program.Wallet.Sign(context);
60+
if (context.Completed)
61+
{
62+
tx.Scripts = context.GetScripts();
63+
Program.Wallet.SaveTransaction(tx);
64+
LocalNode.Relay(tx);
65+
return tx.ToJson();
66+
}
67+
else
68+
{
69+
return context.ToJson();
70+
}
71+
}
72+
case "getnewaddress":
73+
if (Program.Wallet == null)
74+
throw new RpcException(-400, "Access denied");
75+
else
76+
{
77+
KeyPair key = Program.Wallet.CreateKey();
78+
Contract contract = Program.Wallet.GetContracts(key.PublicKeyHash).First(p => p.IsStandard);
79+
return contract.Address;
80+
}
81+
case "dumpprivkey":
82+
if (Program.Wallet == null)
83+
throw new RpcException(-400, "Access denied");
84+
else
85+
{
86+
UInt160 scriptHash = Wallet.ToScriptHash(_params[0].AsString());
87+
KeyPair key = Program.Wallet.GetKeyByScriptHash(scriptHash);
88+
return key.Export();
89+
}
90+
default:
91+
return base.Process(method, _params);
92+
}
93+
}
94+
}
95+
}

neo-cli/Program.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Neo.Implementations.Wallets.EntityFramework;
2+
using Neo.Shell;
3+
using System;
4+
using System.IO;
5+
6+
namespace Neo
7+
{
8+
static class Program
9+
{
10+
internal static UserWallet Wallet;
11+
12+
// private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
13+
// {
14+
//#if DEBUG
15+
// Exception ex = (Exception)e.ExceptionObject;
16+
// using (FileStream fs = new FileStream("error.log", FileMode.Create, FileAccess.Write, FileShare.None))
17+
// using (StreamWriter w = new StreamWriter(fs))
18+
// {
19+
// w.WriteLine(ex.GetType());
20+
// w.WriteLine(ex.Message);
21+
// w.WriteLine(ex.StackTrace);
22+
// AggregateException ex2 = ex as AggregateException;
23+
// if (ex2 != null)
24+
// {
25+
// foreach (Exception inner in ex2.InnerExceptions)
26+
// {
27+
// w.WriteLine();
28+
// w.WriteLine(inner.Message);
29+
// w.WriteLine(inner.StackTrace);
30+
// }
31+
// }
32+
// }
33+
//#endif
34+
// }
35+
36+
static void Main(string[] args)
37+
{
38+
//AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
39+
new MainService().Run(args);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)