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

update signers for invokefunction and invokescript #286

Merged
merged 5 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
41 changes: 27 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,42 @@ 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)
{
return new Signers(_params.Select(u => new Signer()
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
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());
}

[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