Skip to content

Commit

Permalink
update format and fix some comments (#796)
Browse files Browse the repository at this point in the history
* update format and fix some fomments

* remove redundant

---------

Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
Jim8y and shargon authored Apr 12, 2023
1 parent 4b3a76e commit 5deb20b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/RpcClient/ContractClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Task<RpcInvokeResult> TestInvokeAsync(UInt160 scriptHash, string operatio
/// <summary>
/// Deploy Contract, return signed transaction
/// </summary>
/// <param name="contractScript">contract script</param>
/// <param name="nefFile">neo contract executable file</param>
/// <param name="manifest">contract manifest</param>
/// <param name="key">sender KeyPair</param>
/// <returns></returns>
Expand Down
72 changes: 26 additions & 46 deletions src/RpcClient/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,33 @@ 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()
};
}

/// <summary>
/// Parse address, scripthash or public key string to UInt160
/// </summary>
/// <param name="account">account address, scripthash or public key string
/// Example: address ("Ncm9TEzrp8SSer6Wa3UCSLTRnqzwVhCfuE"), scripthash ("0xb0a31817c80ad5f87b6ed390ecb3f9d312f7ceb8"), public key ("02f9ec1fd0a98796cf75b586772a4ddd41a0af07a1dbdf86a7238f74fb72503575")</param>
/// <param name="protocolSettings">The protocol settings</param>
/// <returns></returns>
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(),
};
}

/// <summary>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/RpcServer/RpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5deb20b

Please sign in to comment.