diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index f02cbbe922..0c090c770f 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -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; @@ -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; } @@ -86,7 +87,7 @@ private JObject GetInvokeResult(byte[] script) { json["stack"] = "error: recursive reference"; } - if (wallet != null) + if (Wallet != null) { InvocationTransaction tx = new InvocationTransaction { @@ -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 @@ -133,7 +134,7 @@ private static JObject GetRelayResult(RelayResultReason reason) public void OpenWallet(Wallet wallet) { - this.wallet = wallet; + this.Wallet = wallet; } private JObject Process(string method, JArray _params) @@ -141,12 +142,12 @@ 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": @@ -162,7 +163,7 @@ 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 { @@ -170,10 +171,10 @@ private JObject Process(string method, JArray _params) 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 coins = wallet.GetCoins().Where(p => !p.State.HasFlag(CoinState.Spent) && p.Output.AssetId.Equals(asset_id_256)); + IEnumerable 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; @@ -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; } @@ -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()); @@ -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; @@ -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 { @@ -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 { @@ -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(); } @@ -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 { @@ -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(); } @@ -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 { @@ -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 { @@ -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(); }