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

Commit

Permalink
rebrand to NEO
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Jul 13, 2017
1 parent f1b0039 commit 0e2b178
Show file tree
Hide file tree
Showing 14 changed files with 1,372 additions and 0 deletions.
22 changes: 22 additions & 0 deletions neo-cli.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "neo-cli", "neo-cli\neo-cli.csproj", "{900CA179-AEF0-43F3-9833-5DB060272D8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{900CA179-AEF0-43F3-9833-5DB060272D8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
54 changes: 54 additions & 0 deletions neo-cli/Consensus/ConsensusWithPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Neo.Core;
using Neo.Network;
using Neo.Wallets;
using System;
using System.IO;
using System.Linq;

namespace Neo.Consensus
{
internal class ConsensusWithPolicy : ConsensusService
{
private string log_dictionary;

public ConsensusWithPolicy(LocalNode localNode, Wallet wallet, string log_dictionary)
: base(localNode, wallet)
{
this.log_dictionary = log_dictionary;
}

protected override bool CheckPolicy(Transaction tx)
{
switch (Policy.Default.PolicyLevel)
{
case PolicyLevel.AllowAll:
return true;
case PolicyLevel.AllowList:
return tx.Scripts.All(p => Policy.Default.List.Contains(p.VerificationScript.ToScriptHash())) || tx.Outputs.All(p => Policy.Default.List.Contains(p.ScriptHash));
case PolicyLevel.DenyList:
return tx.Scripts.All(p => !Policy.Default.List.Contains(p.VerificationScript.ToScriptHash())) && tx.Outputs.All(p => !Policy.Default.List.Contains(p.ScriptHash));
default:
return base.CheckPolicy(tx);
}
}

protected override void Log(string message)
{
DateTime now = DateTime.Now;
string line = $"[{now.TimeOfDay:hh\\:mm\\:ss}] {message}";
Console.WriteLine(line);
if (string.IsNullOrEmpty(log_dictionary)) return;
lock (log_dictionary)
{
Directory.CreateDirectory(log_dictionary);
string path = Path.Combine(log_dictionary, $"{now:yyyy-MM-dd}.log");
File.AppendAllLines(path, new[] { line });
}
}

public void RefreshPolicy()
{
Policy.Default.Refresh();
}
}
}
38 changes: 38 additions & 0 deletions neo-cli/Consensus/Policy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Neo.Wallets;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Consensus
{
internal class Policy
{
public PolicyLevel PolicyLevel { get; private set; }
public HashSet<UInt160> List { get; private set; }

public static Policy Default { get; private set; }

static Policy()
{
Default = new Policy();
Default.Refresh();
}

public void Refresh()
{
if (File.Exists("policy.json"))
{
IConfigurationSection section = new ConfigurationBuilder().AddJsonFile("policy.json").Build().GetSection("PolicyConfiguration");
PolicyLevel = (PolicyLevel)Enum.Parse(typeof(PolicyLevel), section.GetSection("PolicyLevel").Value, true);
List = new HashSet<UInt160>(section.GetSection("List").GetChildren().Select(p => Wallet.ToScriptHash(p.Value)));
}
else
{
PolicyLevel = PolicyLevel.AllowAll;
List = new HashSet<UInt160>();
}
}
}
}
10 changes: 10 additions & 0 deletions neo-cli/Consensus/PolicyLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Neo.Consensus
{
internal enum PolicyLevel : byte
{
AllowAll,
DenyAll,
AllowList,
DenyList
}
}
39 changes: 39 additions & 0 deletions neo-cli/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Runtime.InteropServices;
using System.Security;

namespace Neo
{
internal static class Helper
{
public static bool CompareTo(this SecureString s1, SecureString s2)
{
if (s1.Length != s2.Length)
return false;
IntPtr p1 = IntPtr.Zero;
IntPtr p2 = IntPtr.Zero;
try
{
p1 = SecureStringMarshal.SecureStringToGlobalAllocAnsi(s1);
p2 = SecureStringMarshal.SecureStringToGlobalAllocAnsi(s2);
int i = 0;
while (true)
{
byte b1 = Marshal.ReadByte(p1, i);
byte b2 = Marshal.ReadByte(p2, i++);
if (b1 == 0 && b2 == 0)
return true;
if (b1 != b2)
return false;
if (b1 == 0 || b2 == 0)
return false;
}
}
finally
{
Marshal.ZeroFreeGlobalAllocAnsi(p1);
Marshal.ZeroFreeGlobalAllocAnsi(p2);
}
}
}
}
95 changes: 95 additions & 0 deletions neo-cli/Network/RPC/RpcServerWithWallet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Neo.Core;
using Neo.IO.Json;
using Neo.Wallets;
using System.Collections.Generic;
using System.Linq;

namespace Neo.Network.RPC
{
internal class RpcServerWithWallet : RpcServer
{
public RpcServerWithWallet(LocalNode localNode)
: base(localNode)
{
}

protected override JObject Process(string method, JArray _params)
{
switch (method)
{
case "getbalance":
if (Program.Wallet == null)
throw new RpcException(-400, "Access denied.");
else
{
UInt256 assetId = UInt256.Parse(_params[0].AsString());
IEnumerable<Coin> coins = Program.Wallet.GetCoins().Where(p => !p.State.HasFlag(CoinState.Spent) && p.Output.AssetId.Equals(assetId));
JObject json = new JObject();
json["balance"] = coins.Sum(p => p.Output.Value).ToString();
json["confirmed"] = coins.Where(p => p.State.HasFlag(CoinState.Confirmed)).Sum(p => p.Output.Value).ToString();
return json;
}
case "sendtoaddress":
if (Program.Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
UInt256 assetId = UInt256.Parse(_params[0].AsString());
UInt160 scriptHash = Wallet.ToScriptHash(_params[1].AsString());
Fixed8 value = Fixed8.Parse(_params[2].AsString());
Fixed8 fee = _params.Count >= 4 ? Fixed8.Parse(_params[3].AsString()) : Fixed8.Zero;
UInt160 change_address = _params.Count >= 5 ? Wallet.ToScriptHash(_params[4].AsString()) : null;
if (value <= Fixed8.Zero)
throw new RpcException(-32602, "Invalid params");
ContractTransaction tx = Program.Wallet.MakeTransaction(new ContractTransaction
{
Outputs = new[]
{
new TransactionOutput
{
AssetId = assetId,
Value = value,
ScriptHash = scriptHash
}
}
}, change_address: change_address, fee: fee);
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
SignatureContext context = new SignatureContext(tx);
Program.Wallet.Sign(context);
if (context.Completed)
{
tx.Scripts = context.GetScripts();
Program.Wallet.SaveTransaction(tx);
LocalNode.Relay(tx);
return tx.ToJson();
}
else
{
return context.ToJson();
}
}
case "getnewaddress":
if (Program.Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
KeyPair key = Program.Wallet.CreateKey();
Contract contract = Program.Wallet.GetContracts(key.PublicKeyHash).First(p => p.IsStandard);
return contract.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();
}
default:
return base.Process(method, _params);
}
}
}
}
42 changes: 42 additions & 0 deletions neo-cli/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Neo.Implementations.Wallets.EntityFramework;
using Neo.Shell;
using System;
using System.IO;

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

// private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
// {
//#if DEBUG
// Exception ex = (Exception)e.ExceptionObject;
// using (FileStream fs = new FileStream("error.log", FileMode.Create, FileAccess.Write, FileShare.None))
// using (StreamWriter w = new StreamWriter(fs))
// {
// w.WriteLine(ex.GetType());
// w.WriteLine(ex.Message);
// w.WriteLine(ex.StackTrace);
// AggregateException ex2 = ex as AggregateException;
// if (ex2 != null)
// {
// foreach (Exception inner in ex2.InnerExceptions)
// {
// w.WriteLine();
// w.WriteLine(inner.Message);
// w.WriteLine(inner.StackTrace);
// }
// }
// }
//#endif
// }

static void Main(string[] args)
{
//AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
new MainService().Run(args);
}
}
}
Loading

0 comments on commit 0e2b178

Please sign in to comment.