-
Notifications
You must be signed in to change notification settings - Fork 37
Added validator class for NEP-17 contract #311
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
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
771aff8
Added validator class for NEP-17 contract
cschuchardt88 0978603
comment info for validator
cschuchardt88 f91dd0c
Merge branch 'master' into issue-194
Jim8y 5365335
Revert
cschuchardt88 8014267
Merge branch 'issue-194' of https://github.com/cschuchardt88/neo-expr…
cschuchardt88 1074d54
Added method and event checking for nep-17 spec on contract deployment
cschuchardt88 a32996c
Added try catch.
cschuchardt88 051f5ed
Changed exception in try catch
cschuchardt88 398bcf5
Initial classes
cschuchardt88 bb937e5
removed unused namespaces
cschuchardt88 1287212
Add IsValidSymbol method
cschuchardt88 f21f608
Merge branch 'master' into issue-194
cschuchardt88 d29ea1c
Merge branch 'master' into issue-194
Jim8y 5d2eaaf
Added symbol check for whitespace, contract characters and ascii
cschuchardt88 44f8ce9
Merge branch 'issue-194' of https://github.com/cschuchardt88/neo-expr…
cschuchardt88 97f7e8f
Add method HasValidMethods for validator nep17
cschuchardt88 3b10feb
Merge branch 'master' into issue-194
cschuchardt88 5f468a6
Add detailed error output.
cschuchardt88 4677386
Merge branch 'issue-194' of https://github.com/cschuchardt88/neo-expr…
cschuchardt88 3dc7062
Add extension class for manifest
cschuchardt88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (C) 2015-2023 The Neo Project. | ||
// | ||
// The neo is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using McMaster.Extensions.CommandLineUtils; | ||
using Neo; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace NeoExpress.Commands; | ||
|
||
internal partial class ContractCommand | ||
{ | ||
[Command("validate", Description = "Checks a contract for compliance with proposal specification")] | ||
internal class Validate | ||
{ | ||
readonly ExpressChainManagerFactory chainManagerFactory; | ||
|
||
public Validate(ExpressChainManagerFactory chainManagerFactory) | ||
{ | ||
this.chainManagerFactory = chainManagerFactory; | ||
} | ||
|
||
[Argument(0, Description = "Path to contract .nef file")] | ||
[Required] | ||
internal string ContractHash { get; init; } = string.Empty; | ||
|
||
[Option(Description = "Path to neo-express data file")] | ||
internal string Input { get; init; } = string.Empty; | ||
|
||
internal async Task<int> OnExecuteAsync(CommandLineApplication app, IConsole console) | ||
{ | ||
try | ||
{ | ||
if (UInt160.TryParse(ContractHash, out var scriptHash) == false) | ||
throw new Exception($"{ContractHash} is invalid ScriptHash."); | ||
|
||
var (chainManager, _) = chainManagerFactory.LoadChain(Input); | ||
using var expressNode = chainManager.GetExpressNode(); | ||
var nep17 = await expressNode.IsNep17CompliantAsync(scriptHash).ConfigureAwait(false); | ||
|
||
if (nep17) | ||
await console.Out.WriteLineAsync($"{scriptHash} is NEP-17 compliant."); | ||
|
||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
app.WriteException(ex?.InnerException! ?? ex!); | ||
return 1; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (C) 2015-2023 The Neo Project. | ||
// | ||
// The neo is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using Neo.SmartContract; | ||
using Neo.SmartContract.Manifest; | ||
|
||
namespace NeoExpress; | ||
|
||
internal static class ContractManifestExtensions | ||
{ | ||
public static bool IsNep17Compliant(this ContractManifest manifest) | ||
{ | ||
try | ||
{ | ||
var symbolMethod = manifest.Abi.GetMethod("symbol", 0); | ||
var decimalsMethod = manifest.Abi.GetMethod("decimals", 0); | ||
var totalSupplyMethod = manifest.Abi.GetMethod("totalSupply", 0); | ||
var balanceOfMethod = manifest.Abi.GetMethod("balanceOf", 1); | ||
var transferMethod = manifest.Abi.GetMethod("transfer", 4); | ||
|
||
var symbolValid = symbolMethod.Safe == true && | ||
symbolMethod.ReturnType == ContractParameterType.String; | ||
var decimalsValid = decimalsMethod.Safe == true && | ||
decimalsMethod.ReturnType == ContractParameterType.Integer; | ||
var totalSupplyValid = totalSupplyMethod.Safe == true && | ||
totalSupplyMethod.ReturnType == ContractParameterType.Integer; | ||
var balanceOfValid = balanceOfMethod.Safe == true && | ||
balanceOfMethod.ReturnType == ContractParameterType.Integer && | ||
balanceOfMethod.Parameters[0].Type == ContractParameterType.Hash160; | ||
var transferValid = transferMethod.Safe == false && | ||
transferMethod.ReturnType == ContractParameterType.Boolean && | ||
transferMethod.Parameters[0].Type == ContractParameterType.Hash160 && | ||
transferMethod.Parameters[1].Type == ContractParameterType.Hash160 && | ||
transferMethod.Parameters[2].Type == ContractParameterType.Integer && | ||
transferMethod.Parameters[3].Type == ContractParameterType.Any; | ||
var transferEvent = manifest.Abi.Events.SingleOrDefault(s => | ||
s.Name == "transfer" && | ||
s.Parameters.Length == 3 && | ||
s.Parameters[0].Type == ContractParameterType.Hash160 && | ||
s.Parameters[1].Type == ContractParameterType.Hash160 && | ||
s.Parameters[2].Type == ContractParameterType.Integer) != null; | ||
|
||
return (symbolValid && | ||
decimalsValid && | ||
totalSupplyValid && | ||
balanceOfValid && | ||
transferValid && | ||
transferEvent); | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (C) 2015-2023 The Neo Project. | ||
// | ||
// The neo is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using Neo; | ||
using Neo.Persistence; | ||
|
||
namespace NeoExpress.Validators; | ||
|
||
internal class Nep17Token : TokenBase | ||
{ | ||
public Nep17Token( | ||
ProtocolSettings protocolSettings, | ||
DataCache snapshot, | ||
UInt160 scriptHash) : base(protocolSettings, snapshot, scriptHash) | ||
{ | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright (C) 2015-2023 The Neo Project. | ||
// | ||
// The neo is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
using Neo; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.Persistence; | ||
using Neo.VM; | ||
using System.Numerics; | ||
|
||
namespace NeoExpress.Validators; | ||
|
||
internal abstract class TokenBase | ||
{ | ||
public UInt160 ScriptHash { get; private init; } | ||
public string Symbol { get; private set; } | ||
public byte Decimals { get; private set; } | ||
|
||
protected readonly DataCache _snapshot; | ||
protected readonly ProtocolSettings _protocolSettings; | ||
|
||
public TokenBase( | ||
ProtocolSettings protocolSettings, | ||
DataCache snapshot, | ||
UInt160 scriptHash) | ||
{ | ||
_protocolSettings = protocolSettings; | ||
_snapshot = snapshot; | ||
ScriptHash = scriptHash; | ||
|
||
using var builder = new ScriptBuilder(); | ||
builder.EmitDynamicCall(ScriptHash, "decimals"); | ||
builder.EmitDynamicCall(ScriptHash, "symbol"); | ||
|
||
using var engine = builder.Invoke(_protocolSettings, _snapshot); | ||
if (engine.State != VMState.HALT) | ||
throw new NotSupportedException($"{ScriptHash} is not NEP-17 compliant."); | ||
|
||
var results = engine.ResultStack; | ||
Symbol = results.Pop().GetString()!; | ||
Decimals = checked((byte)results.Pop().GetInteger()); | ||
} | ||
|
||
public bool HasValidMethods() | ||
{ | ||
using var builder = new ScriptBuilder(); | ||
builder.EmitDynamicCall(ScriptHash, "decimals"); | ||
builder.EmitDynamicCall(ScriptHash, "symbol"); | ||
builder.EmitDynamicCall(ScriptHash, "totalSupply"); | ||
builder.EmitDynamicCall(ScriptHash, "balanceOf", UInt160.Zero); | ||
builder.EmitDynamicCall(ScriptHash, "transfer", UInt160.Zero, UInt160.Zero, 0, null); | ||
|
||
// You need to add this line for transaction | ||
// for ApplicationEngine not to crash | ||
// see https://github.com/neo-project/neo/issues/2952 | ||
var tx = new Transaction() { Signers = new[] { new Signer() { Account = UInt160.Zero } }, Attributes = Array.Empty<TransactionAttribute>() }; | ||
|
||
using var engine = builder.Invoke(_protocolSettings, _snapshot, tx); | ||
if (engine.State == VMState.FAULT) | ||
throw engine.FaultException; | ||
|
||
return engine.State == VMState.HALT; | ||
} | ||
|
||
public bool IsBalanceOfValid() | ||
{ | ||
return BalanceOf(UInt160.Zero) == 0; | ||
} | ||
|
||
public bool IsDecimalsValid() | ||
{ | ||
using var builder = new ScriptBuilder(); | ||
builder.EmitDynamicCall(ScriptHash, "decimals"); | ||
|
||
byte? dec = null; | ||
int x = 0; | ||
|
||
// This loop checks for constant byte of decimals | ||
while (x <= 5) | ||
{ | ||
using var appEng = builder.Invoke(_protocolSettings, _snapshot); | ||
if (appEng.State != VMState.HALT) | ||
throw appEng.FaultException; | ||
try | ||
{ | ||
if (dec.HasValue == false) | ||
dec = (byte)appEng.ResultStack.Pop().GetInteger(); | ||
else | ||
{ | ||
if (dec == (byte)appEng.ResultStack.Pop().GetInteger()) | ||
x++; | ||
else | ||
return false; | ||
} | ||
} | ||
catch (OverflowException) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public bool IsSymbolValid() | ||
{ | ||
using var builder = new ScriptBuilder(); | ||
builder.EmitDynamicCall(ScriptHash, "symbol"); | ||
|
||
var symbol = string.Empty; | ||
int x = 0; | ||
|
||
// This loop checks for constant string of symbol | ||
while (x <= 5) | ||
{ | ||
using var appEng = builder.Invoke(_protocolSettings, _snapshot); | ||
if (appEng.State != VMState.HALT) | ||
throw appEng.FaultException; | ||
|
||
if (string.IsNullOrEmpty(symbol)) | ||
symbol = appEng.ResultStack.Pop().GetString()!; | ||
else | ||
{ | ||
if (symbol == appEng.ResultStack.Pop().GetString()!) | ||
x++; | ||
else | ||
return false; | ||
} | ||
} | ||
|
||
if (symbol.Any(a => char.IsWhiteSpace(a) || char.IsControl(a) || char.IsAscii(a) == false)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
public BigInteger TotalSupply() | ||
{ | ||
using var builder = new ScriptBuilder(); | ||
builder.EmitDynamicCall(ScriptHash, "totalSupply"); | ||
|
||
using var appEng = builder.Invoke(_protocolSettings, _snapshot); | ||
if (appEng.State == VMState.HALT) | ||
return appEng.ResultStack.Pop().GetInteger(); | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
public BigInteger BalanceOf(UInt160 owner) | ||
{ | ||
using var builder = new ScriptBuilder(); | ||
builder.EmitDynamicCall(ScriptHash, "balanceOf", owner); | ||
|
||
using var appEng = builder.Invoke(_protocolSettings, _snapshot); | ||
if (appEng.State == VMState.HALT) | ||
return appEng.ResultStack.Pop().GetInteger(); | ||
throw new InvalidOperationException(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.