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

Validate functionAbi using ethers ABI parser #228

Merged
merged 4 commits into from
Jun 28, 2023

Conversation

piotr-roslaniec
Copy link
Contributor

@piotr-roslaniec piotr-roslaniec commented Jun 16, 2023

Type of PR:

  • Feature

Required reviews:

  • 1

What this does:

  • Uses ethers to validate functionABI

Issues fixed/closed:

  • Fixes #...

Why it's needed:

  • We've implemented somewhat basic validation for functionABI, but it's still possible to pass mangled/illegal JSON ABI into your conditions

Notes for reviewers:

  • I need some input on this - It looks like there is going to be some overlap between Joi and ethers, and we have to implement some checks and balances ourselves on top of that.

@codecov-commenter
Copy link

codecov-commenter commented Jun 28, 2023

Codecov Report

Merging #228 (5bd27b4) into tdec-epic (50dd01c) will decrease coverage by 0.23%.
The diff coverage is 66.66%.

@@              Coverage Diff              @@
##           tdec-epic     #228      +/-   ##
=============================================
- Coverage      83.47%   83.24%   -0.23%     
=============================================
  Files             37       37              
  Lines            968      979      +11     
  Branches         121      123       +2     
=============================================
+ Hits             808      815       +7     
- Misses           154      158       +4     
  Partials           6        6              
Impacted Files Coverage Δ
src/conditions/base/contract.ts 79.31% <66.66%> (-9.58%) ⬇️

@piotr-roslaniec piotr-roslaniec marked this pull request as ready for review June 28, 2023 08:24
@github-actions
Copy link

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

Copy link
Contributor

@theref theref left a comment

Choose a reason for hiding this comment

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

Great change

});
}

if (!asInterface.fragments) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we be using .functions instead of .fragments? Just reading from here - https://docs.ethers.org/v5/api/utils/abi/interface/#Interface--properties.

Same for various logic below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✔️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially avoided that since the keys in .functions are function signatures, not function names.

Copy link
Member

Choose a reason for hiding this comment

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

Where are the key values used? L44?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we use them to fetch the FunctionFragments

return helper.message({
custom: '"method" must be the same as "functionAbi.name"',
custom: '"functionAbi" does not contain the method specified as "method"',
Copy link
Member

Choose a reason for hiding this comment

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

Can we output the actual method name. So something like (?):

Suggested change
custom: '"functionAbi" does not contain the method specified as "method"',
custom: `"functionAbi" not valid for method, "${method}"`,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✔️

});
}

// Validate nr of parameters
const parameters = helper.state.ancestors[0].parameters;
if (functionAbi.inputs?.length !== parameters.length) {
if (functionFragment.inputs.length !== parameters.length) {
Copy link
Member

Choose a reason for hiding this comment

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

👍 Nice

@@ -7,7 +7,7 @@ import {
import { ContractCondition } from '../../../../src/conditions/base';
Copy link
Member

@derekpierre derekpierre Jun 28, 2023

Choose a reason for hiding this comment

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

I'd love to see some comprehensive testing around validation of many different functionAbis, both valid and invalid, and correct processing/failure as part of this PR.

Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

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

See comment about testing.

@github-actions
Copy link

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

1 similar comment
@github-actions
Copy link

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

])(
'rejects malformed functionAbi',
({ method, expectedError, functionAbi }) => {
expect(() =>
Copy link
Member

Choose a reason for hiding this comment

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

Love this! 😍

Comment on lines 43 to 48
const abiMethodName = Object.keys(asInterface.functions).find((name) =>
name.startsWith(`${method}(`)
);
const functionFragment = abiMethodName
? asInterface.functions[abiMethodName]
: null;
Copy link
Member

@derekpierre derekpierre Jun 28, 2023

Choose a reason for hiding this comment

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

Could this be reduced to the following (https://docs.ethers.org/v5/api/utils/abi/interface/#Interface--fragments)?

Suggested change
const abiMethodName = Object.keys(asInterface.functions).find((name) =>
name.startsWith(`${method}(`)
);
const functionFragment = abiMethodName
? asInterface.functions[abiMethodName]
: null;
const functionFragment = asInterface.getFunction(method);

It seems to possibly throw an ArgumentError, https://github.com/ethers-io/ethers.js/blob/master/packages/abi/src.ts/interface.ts#L193, but do we care?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it makes sense given our conversation about the ambiguity of functions. Fixed.

Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

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

🎸 - good stuff. Minor nitpick suggestion.

Co-authored-by: Derek Pierre <derek.pierre@gmail.com>
@github-actions
Copy link

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

@piotr-roslaniec piotr-roslaniec merged commit d9d957e into tdec-epic Jun 28, 2023
12 checks passed
@piotr-roslaniec piotr-roslaniec deleted the validate-abi-w-ethers branch June 28, 2023 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants