diff --git a/src/RpcClient/ContractClient.cs b/src/RpcClient/ContractClient.cs index 7f340a676..18a1820f2 100644 --- a/src/RpcClient/ContractClient.cs +++ b/src/RpcClient/ContractClient.cs @@ -51,7 +51,7 @@ public Task TestInvokeAsync(UInt160 scriptHash, string operatio /// /// Deploy Contract, return signed transaction /// - /// contract script + /// neo contract executable file /// contract manifest /// sender KeyPair /// diff --git a/src/RpcClient/Utility.cs b/src/RpcClient/Utility.cs index fcabc7ef4..159af4046 100644 --- a/src/RpcClient/Utility.cs +++ b/src/RpcClient/Utility.cs @@ -69,16 +69,12 @@ public static KeyPair GetKeyPair(string key) if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } if (key.StartsWith("0x")) { key = key[2..]; } - if (key.Length == 52) + return key.Length switch { - return new KeyPair(Wallet.GetPrivateKeyFromWIF(key)); - } - else if (key.Length == 64) - { - return new KeyPair(key.HexToBytes()); - } - - throw new FormatException(); + 52 => new KeyPair(Wallet.GetPrivateKeyFromWIF(key)), + 64 => new KeyPair(key.HexToBytes()), + _ => throw new FormatException() + }; } /// @@ -86,27 +82,20 @@ public static KeyPair GetKeyPair(string key) /// /// account address, scripthash or public key string /// Example: address ("Ncm9TEzrp8SSer6Wa3UCSLTRnqzwVhCfuE"), scripthash ("0xb0a31817c80ad5f87b6ed390ecb3f9d312f7ceb8"), public key ("02f9ec1fd0a98796cf75b586772a4ddd41a0af07a1dbdf86a7238f74fb72503575") + /// The protocol settings /// public static UInt160 GetScriptHash(string account, ProtocolSettings protocolSettings) { if (string.IsNullOrEmpty(account)) { throw new ArgumentNullException(nameof(account)); } if (account.StartsWith("0x")) { account = account[2..]; } - if (account.Length == 34) - { - return Wallets.Helper.ToScriptHash(account, protocolSettings.AddressVersion); - } - else if (account.Length == 40) - { - return UInt160.Parse(account); - } - else if (account.Length == 66) + return account.Length switch { - var pubKey = ECPoint.Parse(account, ECCurve.Secp256r1); - return Contract.CreateSignatureRedeemScript(pubKey).ToScriptHash(); - } - - throw new FormatException(); + 34 => account.ToScriptHash(protocolSettings.AddressVersion), + 40 => UInt160.Parse(account), + 66 => Contract.CreateSignatureRedeemScript(ECPoint.Parse(account, ECCurve.Secp256r1)).ToScriptHash(), + _ => throw new FormatException(), + }; } /// @@ -232,29 +221,19 @@ public static WitnessRule RuleFromJson(JObject json, ProtocolSettings protocolSe public static WitnessCondition RuleExpressionFromJson(JObject json, ProtocolSettings protocolSettings) { - switch (json["type"].AsString()) + return json["type"].AsString() switch { - case "Or": - return new OrCondition() { Expressions = ((JArray)json["expressions"])?.Select(p => RuleExpressionFromJson((JObject)p, protocolSettings)).ToArray() }; - case "And": - return new AndCondition() { Expressions = ((JArray)json["expressions"])?.Select(p => RuleExpressionFromJson((JObject)p, protocolSettings)).ToArray() }; - case "Boolean": - return new BooleanCondition() { Expression = json["expression"].AsBoolean() }; - case "Not": - return new NotCondition() { Expression = RuleExpressionFromJson((JObject)json["expression"], protocolSettings) }; - case "Group": - return new GroupCondition() { Group = ECPoint.Parse(json["group"].AsString(), ECCurve.Secp256r1) }; - case "CalledByContract": - return new CalledByContractCondition() { Hash = json["hash"].ToScriptHash(protocolSettings) }; - case "ScriptHash": - return new ScriptHashCondition() { Hash = json["hash"].ToScriptHash(protocolSettings) }; - case "CalledByEntry": - return new CalledByEntryCondition(); - case "CalledByGroup": - return new CalledByGroupCondition() { Group = ECPoint.Parse(json["group"].AsString(), ECCurve.Secp256r1) }; - } - - throw new FormatException("Wrong rule's condition type"); + "Or" => new OrCondition { Expressions = ((JArray)json["expressions"])?.Select(p => RuleExpressionFromJson((JObject)p, protocolSettings)).ToArray() }, + "And" => new AndCondition { Expressions = ((JArray)json["expressions"])?.Select(p => RuleExpressionFromJson((JObject)p, protocolSettings)).ToArray() }, + "Boolean" => new BooleanCondition { Expression = json["expression"].AsBoolean() }, + "Not" => new NotCondition { Expression = RuleExpressionFromJson((JObject)json["expression"], protocolSettings) }, + "Group" => new GroupCondition { Group = ECPoint.Parse(json["group"].AsString(), ECCurve.Secp256r1) }, + "CalledByContract" => new CalledByContractCondition { Hash = json["hash"].ToScriptHash(protocolSettings) }, + "ScriptHash" => new ScriptHashCondition { Hash = json["hash"].ToScriptHash(protocolSettings) }, + "CalledByEntry" => new CalledByEntryCondition(), + "CalledByGroup" => new CalledByGroupCondition { Group = ECPoint.Parse(json["group"].AsString(), ECCurve.Secp256r1) }, + _ => throw new FormatException("Wrong rule's condition type"), + }; } public static StackItem StackItemFromJson(JObject json) @@ -292,8 +271,9 @@ public static StackItem StackItemFromJson(JObject json) return new Pointer(null, (int)json["value"].AsNumber()); case StackItemType.InteropInterface: return new InteropInterface(json); + default: + return json["value"]?.AsString() ?? StackItem.Null; } - return json["value"] is null ? StackItem.Null : json["value"].AsString(); } public static string GetIteratorId(this StackItem item) diff --git a/src/RpcServer/RpcServer.cs b/src/RpcServer/RpcServer.cs index ccc581441..d28e07e57 100644 --- a/src/RpcServer/RpcServer.cs +++ b/src/RpcServer/RpcServer.cs @@ -15,7 +15,6 @@ using Microsoft.AspNetCore.ResponseCompression; using Microsoft.AspNetCore.Server.Kestrel.Https; using Microsoft.Extensions.DependencyInjection; -using Neo.IO; using Neo.Json; using Neo.Network.P2P; using System;