Skip to content

Commit

Permalink
update signers for invokefunction and invokescript (neo-project#286)
Browse files Browse the repository at this point in the history
* update signers for invokefunction and invokescript

* fix format

* Validate signer format

Co-authored-by: Shargon <[email protected]>
  • Loading branch information
2 people authored and 陈志同 committed Oct 13, 2020
1 parent 2b3d225 commit fae212f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
47 changes: 33 additions & 14 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma warning disable IDE0051
#pragma warning disable IDE0060

using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
Expand All @@ -17,15 +19,15 @@ namespace Neo.Plugins
{
partial class RpcServer
{
private class Cosigners : IVerifiable
private class Signers : IVerifiable
{
private readonly Cosigner[] _cosigners;
private readonly Signer[] _signers;
public Witness[] Witnesses { get; set; }
public int Size { get; }

public Cosigners(Cosigner[] cosigners)
public Signers(Signer[] signers)
{
_cosigners = cosigners;
_signers = signers;
}

public void Serialize(BinaryWriter writer)
Expand All @@ -45,12 +47,12 @@ public void DeserializeUnsigned(BinaryReader reader)

public UInt160[] GetScriptHashesForVerifying(StoreView snapshot)
{
return _cosigners.Select(p => p.Account).ToArray();
return _signers.Select(p => p.Account).ToArray();
}

public Cosigner[] GetCosigners()
public Signer[] GetSigners()
{
return _cosigners;
return _signers;
}

public void SerializeUnsigned(BinaryWriter writer)
Expand All @@ -59,9 +61,9 @@ public void SerializeUnsigned(BinaryWriter writer)
}
}

private JObject GetInvokeResult(byte[] script, Cosigners cosigners = null)
private JObject GetInvokeResult(byte[] script, Signers signers = null)
{
using ApplicationEngine engine = ApplicationEngine.Run(script, cosigners, gas: settings.MaxGasInvoke);
using ApplicationEngine engine = ApplicationEngine.Run(script, signers, gas: settings.MaxGasInvoke);
JObject json = new JObject();
json["script"] = script.ToHexString();
json["state"] = engine.State;
Expand All @@ -74,31 +76,48 @@ private JObject GetInvokeResult(byte[] script, Cosigners cosigners = null)
{
json["stack"] = "error: recursive reference";
}
ProcessInvokeWithWallet(json, cosigners);
ProcessInvokeWithWallet(json, signers);
return json;
}

private static Signers SignersFromJson(JArray _params)
{
var ret = new Signers(_params.Select(u => new Signer()
{
Account = UInt160.Parse(u["account"].AsString()),
Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), u["scopes"]?.AsString()),
AllowedContracts = ((JArray)u["contracts"])?.Select(p => UInt160.Parse(p.AsString())).ToArray(),
AllowedGroups = ((JArray)u["groups"])?.Select(p => ECPoint.Parse(p.AsString(), ECCurve.Secp256r1)).ToArray()
}).ToArray());

// Validate format

_ = IO.Helper.ToByteArray(ret.GetSigners()).AsSerializableArray<Signer>();

return ret;
}

[RpcMethod]
private JObject InvokeFunction(JArray _params)
{
UInt160 script_hash = UInt160.Parse(_params[0].AsString());
string operation = _params[1].AsString();
ContractParameter[] args = _params.Count >= 3 ? ((JArray)_params[2]).Select(p => ContractParameter.FromJson(p)).ToArray() : new ContractParameter[0];
Cosigners cosigners = _params.Count >= 4 ? new Cosigners(((JArray)_params[3]).Select(u => new Cosigner() { Account = UInt160.Parse(u["account"].AsString()), Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), u["scopes"].AsString()) }).ToArray()) : null;
Signers signers = _params.Count >= 4 ? SignersFromJson((JArray)_params[3]) : null;
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
script = sb.EmitAppCall(script_hash, operation, args).ToArray();
}
return GetInvokeResult(script, cosigners);
return GetInvokeResult(script, signers);
}

[RpcMethod]
private JObject InvokeScript(JArray _params)
{
byte[] script = _params[0].AsString().HexToBytes();
Cosigners cosigners = _params.Count >= 2 ? new Cosigners(((JArray)_params[1]).Select(u => new Cosigner() { Account = UInt160.Parse(u["account"].AsString()), Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), u["scopes"].AsString()) }).ToArray()) : null;
return GetInvokeResult(script, cosigners);
Signers signers = _params.Count >= 2 ? SignersFromJson((JArray)_params[1]) : null;
return GetInvokeResult(script, signers);
}

[RpcMethod]
Expand Down
6 changes: 3 additions & 3 deletions src/RpcServer/RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ private JObject OpenWallet(JArray _params)
return true;
}

private void ProcessInvokeWithWallet(JObject result, Cosigners cosigners = null)
private void ProcessInvokeWithWallet(JObject result, Signers signers = null)
{
Transaction tx = null;
if (wallet != null && cosigners != null)
if (wallet != null && signers != null)
{
UInt160[] accounts = wallet.GetAccounts().Where(p => !p.Lock && !p.WatchOnly).Select(p => p.ScriptHash).ToArray();
Cosigner[] witnessCosigners = cosigners.GetCosigners().Where(p => accounts.Contains(p.Account)).ToArray();
Signer[] witnessCosigners = signers.GetSigners().Where(p => accounts.Contains(p.Account)).ToArray();
if (witnessCosigners.Count() > 0)
{
tx = wallet.MakeTransaction(result["script"].AsString().HexToBytes(), null, witnessCosigners);
Expand Down
2 changes: 1 addition & 1 deletion src/RpcServer/RpcServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.2.0" />
<PackageReference Include="Neo" Version="3.0.0-CI00966" />
<PackageReference Include="Neo" Version="3.0.0-CI00970" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/StatesDumper/StatesDumper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00966" />
<PackageReference Include="Neo" Version="3.0.0-CI00970" />
<PackageReference Include="Neo.ConsoleService" Version="1.0.0" />
</ItemGroup>

Expand Down

0 comments on commit fae212f

Please sign in to comment.