Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use the wallet inside a RPC plugin #536

Merged
merged 5 commits into from
Jan 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions neo/Network/RPC/RpcServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
using Akka.Actor;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Akka.Actor;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand All @@ -16,30 +26,21 @@
using Neo.VM;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Neo.Network.RPC
{
public sealed class RpcServer : IDisposable
{
private readonly NeoSystem system;
private Wallet wallet;
public Wallet Wallet;

private IWebHost host;
private Fixed8 maxGasInvoke;
private readonly NeoSystem system;

public RpcServer(NeoSystem system, Wallet wallet = null, Fixed8 maxGasInvoke = default(Fixed8))
{
this.system = system;
this.wallet = wallet;
this.Wallet = wallet;
this.maxGasInvoke = maxGasInvoke;
}

Expand Down Expand Up @@ -86,7 +87,7 @@ private JObject GetInvokeResult(byte[] script)
{
json["stack"] = "error: recursive reference";
}
if (wallet != null)
if (Wallet != null)
{
InvocationTransaction tx = new InvocationTransaction
{
Expand All @@ -97,11 +98,11 @@ private JObject GetInvokeResult(byte[] script)
tx.Gas -= Fixed8.FromDecimal(10);
if (tx.Gas < Fixed8.Zero) tx.Gas = Fixed8.Zero;
tx.Gas = tx.Gas.Ceiling();
tx = wallet.MakeTransaction(tx);
tx = Wallet.MakeTransaction(tx);
if (tx != null)
{
ContractParametersContext context = new ContractParametersContext(tx);
wallet.Sign(context);
Wallet.Sign(context);
if (context.Completed)
tx.Witnesses = context.GetWitnesses();
else
Expand Down Expand Up @@ -133,20 +134,20 @@ private static JObject GetRelayResult(RelayResultReason reason)

public void OpenWallet(Wallet wallet)
{
this.wallet = wallet;
this.Wallet = wallet;
}

private JObject Process(string method, JArray _params)
{
switch (method)
{
case "dumpprivkey":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
UInt160 scriptHash = _params[0].AsString().ToScriptHash();
WalletAccount account = wallet.GetAccount(scriptHash);
WalletAccount account = Wallet.GetAccount(scriptHash);
return account.GetKey().Export();
}
case "getaccountstate":
Expand All @@ -162,18 +163,18 @@ private JObject Process(string method, JArray _params)
return asset?.ToJson() ?? throw new RpcException(-100, "Unknown asset");
}
case "getbalance":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied.");
else
{
JObject json = new JObject();
switch (UIntBase.Parse(_params[0].AsString()))
{
case UInt160 asset_id_160: //NEP-5 balance
json["balance"] = wallet.GetAvailable(asset_id_160).ToString();
json["balance"] = Wallet.GetAvailable(asset_id_160).ToString();
break;
case UInt256 asset_id_256: //Global Assets balance
IEnumerable<Coin> coins = wallet.GetCoins().Where(p => !p.State.HasFlag(CoinState.Spent) && p.Output.AssetId.Equals(asset_id_256));
IEnumerable<Coin> coins = Wallet.GetCoins().Where(p => !p.State.HasFlag(CoinState.Spent) && p.Output.AssetId.Equals(asset_id_256));
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();
break;
Expand Down Expand Up @@ -267,12 +268,12 @@ private JObject Process(string method, JArray _params)
return contract?.ToJson() ?? throw new RpcException(-100, "Unknown contract");
}
case "getnewaddress":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
WalletAccount account = wallet.CreateAccount();
if (wallet is NEP6Wallet nep6)
WalletAccount account = Wallet.CreateAccount();
if (Wallet is NEP6Wallet nep6)
nep6.Save();
return account.Address;
}
Expand Down Expand Up @@ -366,10 +367,10 @@ private JObject Process(string method, JArray _params)
return json;
}
case "getwalletheight":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied.");
else
return (wallet.WalletHeight > 0) ? wallet.WalletHeight - 1 : 0;
return (Wallet.WalletHeight > 0) ? Wallet.WalletHeight - 1 : 0;
case "invoke":
{
UInt160 script_hash = UInt160.Parse(_params[0].AsString());
Expand Down Expand Up @@ -399,10 +400,10 @@ private JObject Process(string method, JArray _params)
return GetInvokeResult(script);
}
case "listaddress":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied.");
else
return wallet.GetAccounts().Select(p =>
return Wallet.GetAccounts().Select(p =>
{
JObject account = new JObject();
account["address"] = p.Address;
Expand All @@ -412,7 +413,7 @@ private JObject Process(string method, JArray _params)
return account;
}).ToArray();
case "sendfrom":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
Expand All @@ -427,7 +428,7 @@ private JObject Process(string method, JArray _params)
if (fee < Fixed8.Zero)
throw new RpcException(-32602, "Invalid params");
UInt160 change_address = _params.Count >= 6 ? _params[5].AsString().ToScriptHash() : null;
Transaction tx = wallet.MakeTransaction(null, new[]
Transaction tx = Wallet.MakeTransaction(null, new[]
{
new TransferOutput
{
Expand All @@ -439,11 +440,11 @@ private JObject Process(string method, JArray _params)
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
ContractParametersContext context = new ContractParametersContext(tx);
wallet.Sign(context);
Wallet.Sign(context);
if (context.Completed)
{
tx.Witnesses = context.GetWitnesses();
wallet.ApplyTransaction(tx);
Wallet.ApplyTransaction(tx);
system.LocalNode.Tell(new LocalNode.Relay { Inventory = tx });
return tx.ToJson();
}
Expand All @@ -453,7 +454,7 @@ private JObject Process(string method, JArray _params)
}
}
case "sendmany":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
Expand All @@ -478,15 +479,15 @@ private JObject Process(string method, JArray _params)
if (fee < Fixed8.Zero)
throw new RpcException(-32602, "Invalid params");
UInt160 change_address = _params.Count >= 3 ? _params[2].AsString().ToScriptHash() : null;
Transaction tx = wallet.MakeTransaction(null, outputs, change_address: change_address, fee: fee);
Transaction tx = Wallet.MakeTransaction(null, outputs, change_address: change_address, fee: fee);
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
ContractParametersContext context = new ContractParametersContext(tx);
wallet.Sign(context);
Wallet.Sign(context);
if (context.Completed)
{
tx.Witnesses = context.GetWitnesses();
wallet.ApplyTransaction(tx);
Wallet.ApplyTransaction(tx);
system.LocalNode.Tell(new LocalNode.Relay { Inventory = tx });
return tx.ToJson();
}
Expand All @@ -502,7 +503,7 @@ private JObject Process(string method, JArray _params)
return GetRelayResult(reason);
}
case "sendtoaddress":
if (wallet == null)
if (Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
Expand All @@ -516,7 +517,7 @@ private JObject Process(string method, JArray _params)
if (fee < Fixed8.Zero)
throw new RpcException(-32602, "Invalid params");
UInt160 change_address = _params.Count >= 5 ? _params[4].AsString().ToScriptHash() : null;
Transaction tx = wallet.MakeTransaction(null, new[]
Transaction tx = Wallet.MakeTransaction(null, new[]
{
new TransferOutput
{
Expand All @@ -528,11 +529,11 @@ private JObject Process(string method, JArray _params)
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
ContractParametersContext context = new ContractParametersContext(tx);
wallet.Sign(context);
Wallet.Sign(context);
if (context.Completed)
{
tx.Witnesses = context.GetWitnesses();
wallet.ApplyTransaction(tx);
Wallet.ApplyTransaction(tx);
system.LocalNode.Tell(new LocalNode.Relay { Inventory = tx });
return tx.ToJson();
}
Expand Down