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

fix add signature in invokefunction and invokescript #280

Merged
merged 11 commits into from
Jul 9, 2020
21 changes: 13 additions & 8 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ partial class RpcServer
{
private class CheckWitnessHashes : IVerifiable
{
private readonly UInt160[] _scriptHashesForVerifying;
private readonly Cosigner[] _cosigners;
public Witness[] Witnesses { get; set; }
public int Size { get; }

public CheckWitnessHashes(UInt160[] scriptHashesForVerifying)
public CheckWitnessHashes(Cosigner[] cosigners)
{
_scriptHashesForVerifying = scriptHashesForVerifying;
_cosigners = cosigners;
}

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

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

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

public void SerializeUnsigned(BinaryWriter writer)
Expand All @@ -55,7 +60,7 @@ public void SerializeUnsigned(BinaryWriter writer)
}
}

private JObject GetInvokeResult(byte[] script, IVerifiable checkWitnessHashes = null)
private JObject GetInvokeResult(byte[] script, CheckWitnessHashes checkWitnessHashes = null)
{
using ApplicationEngine engine = ApplicationEngine.Run(script, checkWitnessHashes, extraGAS: settings.MaxGasInvoke);
JObject json = new JObject();
Expand All @@ -70,7 +75,7 @@ private JObject GetInvokeResult(byte[] script, IVerifiable checkWitnessHashes =
{
json["stack"] = "error: recursive reference";
}
ProcessInvokeWithWallet(json);
ProcessInvokeWithWallet(json, checkWitnessHashes);
return json;
}

Expand All @@ -80,7 +85,7 @@ 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];
CheckWitnessHashes checkWitnessHashes = _params.Count >= 4 ? new CheckWitnessHashes(((JArray)_params[3]).Select(u => UInt160.Parse(u.AsString())).ToArray()) : null;
CheckWitnessHashes checkWitnessHashes = _params.Count >= 4 ? new CheckWitnessHashes(((JArray)_params[3]).Select(u => new Cosigner() { Account = UInt160.Parse(u["account"].AsString()), Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), u["scopes"].AsString()) }).ToArray()) : null;
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
Expand All @@ -93,7 +98,7 @@ private JObject InvokeFunction(JArray _params)
private JObject InvokeScript(JArray _params)
{
byte[] script = _params[0].AsString().HexToBytes();
CheckWitnessHashes checkWitnessHashes = _params.Count >= 2 ? new CheckWitnessHashes(((JArray)_params[1]).Select(u => UInt160.Parse(u.AsString())).ToArray()) : null;
CheckWitnessHashes checkWitnessHashes = _params.Count >= 2 ? new CheckWitnessHashes(((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, checkWitnessHashes);
}

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

private void ProcessInvokeWithWallet(JObject result)
private void ProcessInvokeWithWallet(JObject result, CheckWitnessHashes checkWitnessHashes = null)
{
if (wallet != null)
if (wallet != null && checkWitnessHashes != null)
{
Transaction tx = wallet.MakeTransaction(result["script"].AsString().HexToBytes());
UInt160[] accounts = wallet.GetAccounts().Where(p => !p.Lock && !p.WatchOnly).Select(p => p.ScriptHash).ToArray();
Cosigner[] cosigners = checkWitnessHashes.GetCosigners().Where(p => accounts.Contains(p.Account)).ToArray();
shargon marked this conversation as resolved.
Show resolved Hide resolved

Transaction tx = wallet.MakeTransaction(result["script"].AsString().HexToBytes(), null, cosigners);
ContractParametersContext context = new ContractParametersContext(tx);
wallet.Sign(context);
if (context.Completed)
Expand Down