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

feat(eip-712): Add EIP-712 package. #13

Merged
merged 8 commits into from
Aug 1, 2024
Merged

feat(eip-712): Add EIP-712 package. #13

merged 8 commits into from
Aug 1, 2024

Conversation

MalteHerrmann
Copy link
Collaborator

@MalteHerrmann MalteHerrmann commented Jul 31, 2024

This PR adds the EIP-712 implementation from the Evmos repository, which could potentially be used by evmOS customers.

Summary by CodeRabbit

  • New Features

    • Introduced EIP-712 support for seamless integration of Cosmos SDK transactions with Ethereum's signing mechanisms.
    • Added functionality for Ledger-signed transaction processing, enhancing compatibility with hardware wallets.
    • Implemented mechanisms for creating and managing EIP-712 types to facilitate structured data signing.
  • Bug Fixes

    • Improved error handling and validation across transaction processing and encoding functions.
  • Documentation

    • Updated changelog to reflect recent additions and enhancements, including the incorporation of the EIP-712 package.
  • Tests

    • Expanded test coverage with a suite for validating EIP-712 functionality and Ledger transaction preprocessing.
  • Chores

    • Established a new go.mod file for dependency management tailored to the Evmos OS module.

@MalteHerrmann MalteHerrmann requested a review from a team as a code owner July 31, 2024 14:26
@MalteHerrmann MalteHerrmann requested review from hanchon and GAtom22 and removed request for a team July 31, 2024 14:26
Copy link

coderabbitai bot commented Jul 31, 2024

Walkthrough

The recent changes enhance EIP-712 compatibility and improve Ledger transaction processing in the Cosmos SDK. Significant updates include new functions for creating EIP-712 domains and types, alongside robust testing frameworks for validating these features. Configuration files have been optimized for better change detection and spell-checking. Overall, these enhancements strengthen the integration between Cosmos and Ethereum, improving interoperability and transaction handling.

Changes

Files Change Summary
.clconfig.json Updated change_types and expected_spellings; refined regex patterns.
CHANGELOG.md Added entry for EIP-712 package integration and CI workflow improvements.
Makefile Modified check-licenses target to download and execute license checker.
ethereum/eip712/ Introduced new files for EIP-712 message handling, encoding, and testing.
go.mod Established module dependencies and replaced specific packages.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Ledger
    participant TxBuilder
    participant EIP712

    User->>Ledger: Sign transaction
    Ledger->>TxBuilder: Submit signed transaction
    TxBuilder->>EIP712: Preprocess for EIP-712
    EIP712->>TxBuilder: Validate and format transaction
    TxBuilder->>User: Return processed transaction
Loading

🐰 In the meadow, hops a rabbit bright,
With changes fresh, oh what a delight!
EIP-712 sings a tune,
Ledger’s dance beneath the moon.
Interoperability leaps and bounds,
In blockchain fields, joy abounds! 🌼✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the tests label Jul 31, 2024
@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@github-actions github-actions bot added the build label Jul 31, 2024
.markdownlintignore Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 18

Outside diff range, codebase verification and nitpick comments (15)
ethereum/eip712/eip712.go (1)

1-2: Update License Identifier.

The SPDX license identifier should be SPDX-License-Identifier: ENCL-1.0 for consistency.

- // SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)
+ // SPDX-License-Identifier: ENCL-1.0
ethereum/eip712/preprocess.go (9)

1-2: Update License Identifier.

The SPDX license identifier should be SPDX-License-Identifier: ENCL-1.0 for consistency.

- // SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)
+ // SPDX-License-Identifier: ENCL-1.0

18-22: Add comment for early return.

Adding a comment explaining why the function returns early for non-Ledger transactions improves readability.

// Only process Ledger transactions
if keyType != cosmoskr.TypeLedger {
    return nil
}

25-28: Improve error message clarity.

The error message can be more descriptive to help with debugging.

- return fmt.Errorf("cannot cast TxBuilder to ExtensionOptionsTxBuilder")
+ return fmt.Errorf("txBuilder does not implement ExtensionOptionsTxBuilder interface")

31-34: Improve error message clarity.

The error message can be more descriptive to help with debugging.

- return fmt.Errorf("could not get signatures: %w", err)
+ return fmt.Errorf("failed to get signatures from TxBuilder: %w", err)

37-39: Improve error message clarity.

The error message can be more descriptive to help with debugging.

- return fmt.Errorf("invalid number of signatures, expected 1 and got %v", len(sigs))
+ return fmt.Errorf("invalid number of signatures: expected 1, got %v", len(sigs))

43-45: Improve error message clarity.

The error message can be more descriptive to help with debugging.

- return fmt.Errorf("unexpected signature type, expected SingleSignatureData")
+ return fmt.Errorf("unexpected signature type: expected SingleSignatureData")

49-51: Improve error message clarity.

The error message can be more descriptive to help with debugging.

- return fmt.Errorf("could not parse chain id: %w", err)
+ return fmt.Errorf("failed to parse chain ID: %w", err)

56-63: Improve error message clarity.

The error message can be more descriptive to help with debugging.

- return fmt.Errorf("could not set extension as any: %w", err)
+ return fmt.Errorf("failed to set extension as Any: %w", err)

79-82: Improve error message clarity.

The error message can be more descriptive to help with debugging.

- return fmt.Errorf("unable to set signatures on payload: %w", err)
+ return fmt.Errorf("failed to set signatures on TxBuilder: %w", err)
ethereum/eip712/message.go (1)

1-2: Update License Identifier.

The SPDX license identifier should be SPDX-License-Identifier: ENCL-1.0 for consistency.

- // SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)
+ // SPDX-License-Identifier: ENCL-1.0
ethereum/eip712/types.go (4)

35-87: Add detailed comments for better readability.

While the function is clear, adding more comments explaining the purpose of each section would improve readability.


65-65: Consider handling the removal of timeout_height more explicitly.

The comment mentions that timeout_height was removed because it was not getting filled with the legacyTx. It might be helpful to log this removal or handle it more explicitly.


115-128: Add comments to explain the logic.

Adding comments to explain the purpose of each step in the function would improve readability and maintainability.


185-193: Handle unsupported JSON types more explicitly.

The function currently ignores unsupported JSON types (e.g., nested arrays). Consider logging or handling these cases more explicitly.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cfa87f2 and 0906400.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (15)
  • .clconfig.json (1 hunks)
  • CHANGELOG.md (1 hunks)
  • Makefile (1 hunks)
  • ethereum/eip712/domain.go (1 hunks)
  • ethereum/eip712/eip712.go (1 hunks)
  • ethereum/eip712/eip712_fuzzer_test.go (1 hunks)
  • ethereum/eip712/eip712_legacy.go (1 hunks)
  • ethereum/eip712/eip712_test.go (1 hunks)
  • ethereum/eip712/encoding.go (1 hunks)
  • ethereum/eip712/encoding_legacy.go (1 hunks)
  • ethereum/eip712/message.go (1 hunks)
  • ethereum/eip712/preprocess.go (1 hunks)
  • ethereum/eip712/preprocess_test.go (1 hunks)
  • ethereum/eip712/types.go (1 hunks)
  • go.mod (1 hunks)
Additional comments not posted (72)
CHANGELOG.md (1)

11-11: Changelog entry looks good.

The entry correctly documents the addition of the EIP-712 package.

ethereum/eip712/domain.go (2)

1-2: Correct license information.

The license information is correctly added at the top of the file.


10-21: Function createEIP712Domain looks good.

The function correctly creates and returns an EIP-712 domain. Ensure that the chainID is always a valid uint64 value.

.clconfig.json (3)

25-25: Regex pattern update looks good.

The regex pattern for "API Breaking" is correctly updated.


35-35: Repositioning of "Cosmos SDK" entry looks good.

The entry for "Cosmos SDK" is correctly repositioned without changing the regex pattern.


46-46: Addition of "evmOS" entry looks good.

The entry for "evmOS" is correctly added to the expected_spellings section.

ethereum/eip712/eip712.go (1)

26-35: LGTM!

The function is well-implemented and follows best practices for creating EIP712-compatible TypedData.

ethereum/eip712/preprocess.go (1)

84-85: LGTM!

The function is well-implemented and follows best practices for preprocessing Ledger-signed transactions.

ethereum/eip712/message.go (7)

48-49: LGTM!

The function is well-implemented and follows best practices for creating EIP-712 message payloads.


64-65: LGTM!

The function is well-implemented and follows best practices for unmarshalling JSON objects.


91-92: LGTM!

The function is well-implemented and follows best practices for flattening payload messages.


106-107: LGTM!

The function is well-implemented and follows best practices for processing payload messages.


130-131: LGTM!

The function is well-implemented and follows best practices for updating payload objects with new messages.


136-137: LGTM!

The function is well-implemented and follows best practices for generating payload fields.


147-148: LGTM!

The function is well-implemented and follows best practices for updating payloads.

ethereum/eip712/eip712_fuzzer_test.go (8)

13-21: LGTM!

The type definition for EIP712FuzzTestParams looks good.


23-29: LGTM!

The constant definitions for the fuzz tests look good.

Also applies to: 31-37


39-47: LGTM!

The initialization of the params variable looks good.


49-71: LGTM!

The implementation of the TestRandomPayloadFlattening function looks good.


73-86: LGTM!

The implementation of the generateRandomPayload function looks good.


88-103: LGTM!

The implementation of the createRandomJSONObject function looks good.


105-121: LGTM!

The implementation of the createRandomJSONField function looks good.


124-192: LGTM!

The implementations of the remaining functions look good.

ethereum/eip712/preprocess_test.go (7)

27-33: LGTM!

The definitions of constants and variables for testing purposes look good.

Also applies to: 34-34


36-44: LGTM!

The type definition for TestCaseStruct looks good.


46-108: LGTM!

The implementation of the TestLedgerPreprocessing function looks good.


111-121: LGTM!

The implementation of the TestBlankTxBuilder function looks good.


123-133: LGTM!

The implementation of the TestNonLedgerTxBuilder function looks good.


135-145: LGTM!

The implementation of the TestInvalidChainId function looks good.


147-227: LGTM!

The implementations of the helper functions createBasicTestCase and createPopulatedTestCase look good.

ethereum/eip712/encoding.go (8)

23-30: LGTM!

The implementation of the SetEncodingConfig function looks good.


32-46: LGTM!

The implementation of the GetEIP712BytesForMsg function looks good.


48-63: LGTM!

The implementation of the GetEIP712TypedDataForMsg function looks good.


65-69: LGTM!

The implementation of the isValidEIP712Payload function looks good.


71-116: LGTM!

The implementation of the decodeAminoSignDoc function looks good.


119-199: LGTM!

The implementation of the decodeProtobufSignDoc function looks good.


202-210: LGTM!

The implementation of the validateCodecInit function looks good.


212-237: LGTM!

The implementation of the validatePayloadMessages function looks good.

ethereum/eip712/encoding_legacy.go (6)

22-36: LGTM!

The LegacyGetEIP712BytesForMsg function is well-implemented with proper error handling.


38-53: LGTM!

The LegacyGetEIP712TypedDataForMsg function handles multiple decoding attempts and provides informative error messages.


55-113: LGTM!

The legacyDecodeAminoSignDoc function performs thorough validation and error handling.


115-207: LGTM!

The legacyDecodeProtobufSignDoc function is well-implemented with thorough validation and error handling.


209-245: LGTM!

The legacyValidatePayloadMessages function performs comprehensive validation checks and provides informative error messages.


247-265: LGTM!

The getMsgType function is well-implemented with proper error handling.

Makefile (1)

272-277: LGTM!

The modifications to the check-licenses target ensure the latest version of the script is used and maintain a clean working environment.

ethereum/eip712/eip712_legacy.go (7)

32-84: LGTM!

The LegacyWrapTxToTypedData function is well-implemented with proper error handling and construction of the TypedData object.


86-140: LGTM!

The extractMsgTypes function is well-implemented with correct type extraction and construction of the type map.


142-161: LGTM!

The walkFields function is well-implemented with correct field traversal and construction of the type definitions.


168-337: LGTM!

The legacyTraverseFields function is well-implemented with correct field traversal and construction of the type definitions.


339-343: LGTM!

The jsonNameFromTag function is well-implemented with correct tag extraction.


345-364: LGTM!

The unpackAny function is well-implemented with correct type unpacking and robust error handling.


377-438: LGTM!

The typToEth function is well-implemented with comprehensive and correct type mapping.

ethereum/eip712/types.go (8)

77-84: Check for potential issues with large payloads.

The loop iterating over messagePayload.numPayloadMsgs could potentially lead to performance issues with very large payloads. Consider adding a check or limit.


91-111: Ensure comprehensive error handling.

The function uses defer to handle panics, which is good practice. Ensure that all potential errors are properly logged and handled.


130-137: LGTM!

The function is straightforward and performs its task efficiently.


222-244: LGTM!

The function is well-implemented and includes necessary error handling.


246-254: LGTM!

The function is straightforward and performs its task efficiently.


256-263: LGTM!

The function is straightforward and performs its task efficiently.


265-269: LGTM!

The function is straightforward and performs its task efficiently.


300-302: Ensure the limit for duplicate type definitions is sufficient.

The limit for duplicate type definitions is set to 1000. Ensure this limit is sufficient for all use cases.

go.mod (3)

5-17: LGTM!

The direct dependencies are relevant to the project's scope and appear to be correctly specified.


19-218: LGTM!

The indirect dependencies are relevant to the project's scope and appear to be correctly specified.


220-233: LGTM!

The dependency replacements are necessary for compatibility and security reasons.

ethereum/eip712/eip712_test.go (9)

58-65: LGTM!

The function sets up and runs the test suite efficiently.


67-74: LGTM!

The function sets up the test environment efficiently.


76-85: LGTM!

The function creates random test addresses efficiently.


87-98: LGTM!

The function creates a random key pair efficiently and verifies the implementation of the public key interface.


100-108: LGTM!

The function creates an instance of sdk.Coins efficiently.


384-418: LGTM!

The function verifies the EIP-712 signature efficiently and includes necessary checks.


420-430: LGTM!

The function tests the flattening algorithm efficiently.


432-486: LGTM!

The function compares the payload against its flattened counterpart efficiently.


488-505: LGTM!

The function performs basic verification on the TypedData generation efficiently.

ethereum/eip712/types.go Show resolved Hide resolved
ethereum/eip712/types.go Show resolved Hide resolved
ethereum/eip712/eip712_test.go Show resolved Hide resolved
ethereum/eip712/eip712.go Show resolved Hide resolved
ethereum/eip712/eip712.go Show resolved Hide resolved
ethereum/eip712/message.go Show resolved Hide resolved
ethereum/eip712/message.go Show resolved Hide resolved
ethereum/eip712/message.go Show resolved Hide resolved
ethereum/eip712/message.go Show resolved Hide resolved
ethereum/eip712/message.go Show resolved Hide resolved
"encoding/json"
"fmt"
"math/big"
"reflect" // #nosec G702 for sensitive import

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism
ethereum/eip712/types.go Dismissed Show dismissed Hide dismissed
@github-actions github-actions bot added the CI label Aug 1, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0906400 and 1a925c8.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (3)
  • .github/workflows/semgrep.yml (1 hunks)
  • ethereum/eip712/eip712_test.go (1 hunks)
  • go.mod (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • ethereum/eip712/eip712_test.go
  • go.mod
Additional comments not posted (1)
.github/workflows/semgrep.yml (1)

18-18: Verify the correctness of the updated path.

The path in the git config command has been updated from /__w/evmos/os to /__w/os/os. Ensure that this change aligns with the actual directory structure used in the repository.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0906400 and 1a925c8.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (3)
  • .github/workflows/semgrep.yml (1 hunks)
  • ethereum/eip712/eip712_test.go (1 hunks)
  • go.mod (1 hunks)
Files skipped from review due to trivial changes (1)
  • .github/workflows/semgrep.yml
Files skipped from review as they are similar to previous changes (2)
  • ethereum/eip712/eip712_test.go
  • go.mod

@MalteHerrmann MalteHerrmann merged commit 51d63cd into main Aug 1, 2024
19 of 25 checks passed
@MalteHerrmann MalteHerrmann deleted the add-eip-712 branch August 1, 2024 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants