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

Sign the transaction in the InvokeContractVerify method #436

Merged
merged 39 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
19866ff
Merge pull request #14 from neo-project/master
Nov 25, 2020
6dd0c37
[RpcServer] Querying contracts by ID/name (#378)
Nov 26, 2020
c7d1961
MPT in StateService from core (#410)
ZhangTao1596 Nov 27, 2020
0799d85
Unify GetUnclaimedGas and GetWalletUnclaimedGas with decimal (#413)
superboyiii Nov 27, 2020
b522dce
Fixed UT (Neo CI01089) (#416)
Dec 2, 2020
bd80d9e
Nep17 (#412)
Dec 7, 2020
3f1e022
a
Dec 17, 2020
0cb8a83
merge
Dec 17, 2020
f05644f
clear
Dec 17, 2020
615b974
Transfer bug fix (#429)
Dec 17, 2020
33141c1
Preview4 (#430)
superboyiii Dec 18, 2020
c5af4ea
Merge pull request #22 from neo-project/master
chenzhitong Dec 22, 2020
215a8cc
dotnet 5.0 (#407)
chenzhitong Dec 22, 2020
8780e1a
CI01123 (#435)
erikzhang Dec 22, 2020
e55cff7
Sign the transaction in the InvokeContractVerify method
Dec 22, 2020
bfee848
Merge branch 'master' into InvokeContractVerify2
chenzhitong Dec 22, 2020
417002a
Merge pull request #24 from neo-project/master
chenzhitong Dec 22, 2020
6ae4d3e
Merge branch 'master' into InvokeContractVerify2
superboyiii Dec 28, 2020
6a873ab
Update RpcServer.Wallet.cs
chenzhitong Dec 29, 2020
7da4178
Merge branch 'InvokeContractVerify2' of https:/chenzhiton…
chenzhitong Dec 29, 2020
5774c81
Fix dotnet5 (#442)
Ashuaidehao Dec 25, 2020
c88b717
Merge pull request #28 from neo-project/master
chenzhitong Dec 29, 2020
5e0a013
Merge branch 'origin3' into InvokeContractVerify2
chenzhitong Dec 30, 2020
7dcf459
Merge pull request #29 from neo-project/master
chenzhitong Dec 30, 2020
42b30b0
merge
chenzhitong Dec 30, 2020
29637de
update
chenzhitong Dec 30, 2020
906b08b
Update src/RpcServer/RpcServer.Wallet.cs
chenzhitong Dec 30, 2020
7983903
Merge branch 'master' into InvokeContractVerify2
chenzhitong Dec 30, 2020
68e26be
Update RpcServer.Wallet.cs
chenzhitong Dec 31, 2020
6f2d05a
Merge branch 'InvokeContractVerify2' of https:/chenzhiton…
chenzhitong Dec 31, 2020
dd4216c
Update src/RpcServer/RpcServer.Wallet.cs
chenzhitong Jan 4, 2021
5b0d7aa
Merge branch 'master' into InvokeContractVerify2
superboyiii Jan 5, 2021
43ecc9f
Merge branch 'master' into InvokeContractVerify2
shargon Jan 11, 2021
26b84bd
Remove LoadContract
shargon Jan 11, 2021
c776ade
Merge branch 'master' into InvokeContractVerify2
vncoelho Jan 13, 2021
ebc3c38
Update src/RpcServer/RpcServer.Wallet.cs
chenzhitong Jan 14, 2021
6769321
Merge branch 'master' into InvokeContractVerify2
shargon Jan 14, 2021
dcaafcc
Merge branch 'master' into InvokeContractVerify2
vncoelho Jan 14, 2021
f1d2c3e
Merge branch 'master' into InvokeContractVerify2
superboyiii Jan 15, 2021
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
51 changes: 0 additions & 51 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,57 +107,6 @@ private static Signers SignersFromJson(JArray _params)
return ret;
}

private JObject GetVerificationResult(UInt160 scriptHash, ContractParameter[] args, Signers signers = null)
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var contract = NativeContract.ContractManagement.GetContract(snapshot, scriptHash);
if (contract is null)
{
throw new RpcException(-100, "Unknown contract");
}
var methodName = "verify";

Transaction tx = signers == null ? null : new Transaction
{
Signers = signers.GetSigners(),
Attributes = Array.Empty<TransactionAttribute>(),
Witnesses = signers.Witnesses,
};

using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.Clone());
engine.LoadContract(contract, methodName, CallFlags.None, true, 0);

engine.LoadScript(new ScriptBuilder().EmitDynamicCall(scriptHash, methodName, args).ToArray());

JObject json = new JObject();
json["script"] = Convert.ToBase64String(contract.Script);
json["state"] = engine.Execute();
json["gasconsumed"] = new BigDecimal(engine.GasConsumed, NativeContract.GAS.Decimals).ToString();
json["exception"] = GetExceptionMessage(engine.FaultException);
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToJson()));
}
catch (InvalidOperationException)
{
json["stack"] = "error: recursive reference";
}
if (engine.State != VMState.FAULT)
{
ProcessInvokeWithWallet(json);
}
return json;
}

[RpcMethod]
protected virtual JObject InvokeContractVerify(JArray _params)
{
UInt160 script_hash = UInt160.Parse(_params[0].AsString());
ContractParameter[] args = _params.Count >= 2 ? ((JArray)_params[1]).Select(p => ContractParameter.FromJson(p)).ToArray() : new ContractParameter[0];
Signers signers = _params.Count >= 3 ? SignersFromJson((JArray)_params[2]) : null;
return GetVerificationResult(script_hash, args, signers);
}

[RpcMethod]
protected virtual JObject InvokeFunction(JArray _params)
{
Expand Down
49 changes: 49 additions & 0 deletions src/RpcServer/RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using Neo.Wallets.SQLite;
Expand Down Expand Up @@ -324,6 +325,54 @@ protected virtual JObject SendToAddress(JArray _params)
return SignAndRelay(tx);
}

[RpcMethod]
protected virtual JObject InvokeContractVerify(JArray _params)
{
CheckWallet();
UInt160 script_hash = UInt160.Parse(_params[0].AsString());
ContractParameter[] args = _params.Count >= 2 ? ((JArray)_params[1]).Select(p => ContractParameter.FromJson(p)).ToArray() : new ContractParameter[0];
Signers signers = _params.Count >= 3 ? SignersFromJson((JArray)_params[2]) : null;
return GetVerificationResult(script_hash, args, signers);
}

private JObject GetVerificationResult(UInt160 scriptHash, ContractParameter[] args, Signers signers = null)
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var contract = NativeContract.ContractManagement.GetContract(snapshot, scriptHash);
if (contract is null)
{
throw new RpcException(-100, "Unknown contract");
}
var methodName = "verify";

Transaction tx = signers == null ? null : new Transaction
{
Signers = signers.GetSigners(),
Attributes = Array.Empty<TransactionAttribute>()
};
ContractParametersContext context = new ContractParametersContext(tx);
wallet.Sign(context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to check if these signers are know or unknown as @cloud8little tested?

tx.Witnesses = context.Completed ? context.GetWitnesses() : null;

using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.Clone());
engine.LoadScript(new ScriptBuilder().EmitDynamicCall(scriptHash, methodName, true, args).ToArray(), (ushort)args.Length, 1);

chenzhitong marked this conversation as resolved.
Show resolved Hide resolved
JObject json = new JObject();
json["script"] = Convert.ToBase64String(contract.Script);
json["state"] = engine.Execute();
json["gasconsumed"] = new BigDecimal(engine.GasConsumed, NativeContract.GAS.Decimals).ToString();
json["exception"] = GetExceptionMessage(engine.FaultException);
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToJson()));
}
catch (InvalidOperationException)
{
json["stack"] = "error: recursive reference";
}
return json;
}

private JObject SignAndRelay(Transaction tx)
{
ContractParametersContext context = new ContractParametersContext(tx);
Expand Down