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

refactor(evm)!: replace HexAddr with EIP55Addr #2004

Merged
merged 5 commits into from
Aug 16, 2024
Merged

Conversation

k-yang
Copy link
Member

@k-yang k-yang commented Aug 15, 2024

Context

Please see https://eips.ethereum.org/EIPS/eip-55 for the EIP-55 spec.

Purpose / Abstract

Instead of having a HexAddr type that "sublasses" the primitive string type, we introduce an EIP55Addr type which "subclasses" gethcommon.Address. EIP55Addr is a stricter type than HexAddr, because only valid representations of gethcommon.Address are valid EIP55Addrs. We also still inherit from sdk.CustomProtobufType, keeping the benefit of using EIP55Addr as a custom proto field and allowing it to be serialized into collections state, but avoiding the pitfall described below:

HexAddr was dangerous because one could do addr := eth.HexAddr("KEVIN") and bypass the validation checks in HexAddr.Valid(). EIP55Addr requires the creator to use NewEIP55AddrFromStr or pass a valid gethcommon.Address to EIP55Addr{Address: addr}, and gethcommon.Address is a canonical 20-byte array, so it can never have alternative representations (e.g. mixed-case alphanumeric characters, or alternative lengths).

The underlying gethcommon.Address can be accessed via eip55Addr.Address, providing a field lookup instead of re-calculating the entire gethcommon.Address from a string. We also inherit all of the gethcommon.Address methods, like String() and Bytes(), which are helpful for proto marshalling.

The PR also refactors tests to use the new EIP55Addr type.

BREAKING CHANGE: changes underlying proto types from HexAddr to EIP55Addr.

Summary by CodeRabbit

  • New Features

    • Introduced support for EIP-55 compliant Ethereum addresses, enhancing address validation and integrity.
  • Bug Fixes

    • Improved error handling and validation for Ethereum addresses across various functions, reducing potential address format-related errors.
  • Refactor

    • Replaced all instances of HexAddr with EIP55Addr in relevant components for consistent address handling.
  • Tests

    • Added comprehensive tests to validate EIP-55 address functionality and ensure correct formatting and parsing across multiple use cases.

Copy link
Contributor

coderabbitai bot commented Aug 15, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes introduce a significant refactor of Ethereum address handling in the codebase, transitioning from the HexAddr type to a more robust EIP55Addr type. This update enhances address validation and compliance with the EIP-55 standard, ensuring addresses are checked for integrity and consistency throughout the application. The modifications improve overall code clarity, reduce potential errors, and standardize how Ethereum addresses are processed across different modules.

Changes

Files Change Summary
src/evm/types.go, eth/eip55.go, eth/eip55_test.go Replaced the HexAddr type with EIP55Addr, implementing enhanced address validation and serialization methods.
proto/eth/evm/v1/evm.proto, proto/eth/evm/v1/tx.proto Updated address fields to use EIP55Addr instead of HexAddr, ensuring compliance with the EIP-55 standard for ERC20 token addresses.
x/evm/cli/tx.go, x/evm/evm.go, x/evm/evm_test.go, x/evm/keeper/* Adjusted various functions to utilize EIP55Addr, improving address handling and validation across multiple features and tests.
x/evm/msg.go Enhanced validation for the ToEthAddr field in MsgConvertCoinToEvm, checking for both presence and size.
x/evm/precompile/funtoken_test.go Updated address handling in test cases to use EIP55Addr, ensuring consistency and reducing reliance on conversion methods.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Keeper
    participant EVM
    participant AddressValidator

    User->>CLI: Create FunToken
    CLI->>Keeper: Call CreateFunToken()
    Keeper->>AddressValidator: Validate EIP55Addr
    AddressValidator-->>Keeper: Return Validity
    Keeper->>EVM: Process Token Creation
    EVM-->>Keeper: Confirmation of Creation
    Keeper-->>CLI: Return Success
    CLI-->>User: FunToken Created Successfully
Loading

🐰 In the code, a new address flows,
With EIP55, the wisdom grows.
A hop, a skip, and all is right,
Validations shine, oh what a sight!
Cheers to the changes, let’s celebrate,
For clearer paths, we’ll all relate! 🎉


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.

@k-yang k-yang changed the title refactor(evm): replace HexAddr with EIP55Addr refactor(evm)!: replace HexAddr with EIP55Addr Aug 15, 2024
Copy link

codecov bot commented Aug 15, 2024

Codecov Report

Attention: Patch coverage is 74.07407% with 14 lines in your changes missing coverage. Please review.

Project coverage is 65.84%. Comparing base (ccb4763) to head (c1096bb).
Report is 1 commits behind head on main.

Files Patch % Lines
eth/eip55.go 77.41% 5 Missing and 2 partials ⚠️
x/evm/evm.go 0.00% 5 Missing ⚠️
x/evm/keeper/msg_server.go 88.88% 1 Missing ⚠️
x/evm/msg.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2004      +/-   ##
==========================================
- Coverage   65.85%   65.84%   -0.02%     
==========================================
  Files         261      261              
  Lines       16435    16413      -22     
==========================================
- Hits        10824    10807      -17     
+ Misses       4802     4798       -4     
+ Partials      809      808       -1     
Files Coverage Δ
x/evm/keeper/funtoken_from_coin.go 78.94% <100.00%> (+1.16%) ⬆️
x/evm/keeper/funtoken_from_erc20.go 77.35% <100.00%> (ø)
x/evm/keeper/funtoken_state.go 92.00% <100.00%> (ø)
x/evm/keeper/msg_server.go 73.85% <88.88%> (-0.08%) ⬇️
x/evm/msg.go 65.63% <0.00%> (ø)
x/evm/evm.go 35.29% <0.00%> (-11.77%) ⬇️
eth/eip55.go 77.41% <77.41%> (ø)

@Unique-Divine
Copy link
Member

Unique-Divine commented Aug 16, 2024

HexAddr was dangerous because one could do addr := eth.HexAddr("KEVIN") and bypass the validation checks in HexAddr.Valid(). EIP55Addr requires the creator to use NewEIP55AddrFromStr or pass a valid gethcommon.Address to EIP55Addr{Address: addr}, and gethcommon.Address is a canonical 20-byte array, so it can never have alternative representations (e.g. mixed-case alphanumeric characters, or alternative lengths).

There isn't actually a tradeoff in safety here.
You can bypass the validation of any type by casting in the way you've described.

Writing eth.HexAddr("KEVIN") is just as dangerous as writing gethcommon.Address([]byte("KEVIN"))

Copy link
Member

@Unique-Divine Unique-Divine left a comment

Choose a reason for hiding this comment

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

Nice, this accomplishes what we wanted 🙌

@k-yang k-yang marked this pull request as ready for review August 16, 2024 14:32
@k-yang k-yang requested a review from a team as a code owner August 16, 2024 14:32
@k-yang k-yang merged commit ba151df into main Aug 16, 2024
20 checks passed
@k-yang k-yang deleted the refactor/evm/hex-addr branch August 16, 2024 14:32
@k-yang
Copy link
Member Author

k-yang commented Aug 16, 2024

HexAddr was dangerous because one could do addr := eth.HexAddr("KEVIN") and bypass the validation checks in HexAddr.Valid(). EIP55Addr requires the creator to use NewEIP55AddrFromStr or pass a valid gethcommon.Address to EIP55Addr{Address: addr}, and gethcommon.Address is a canonical 20-byte array, so it can never have alternative representations (e.g. mixed-case alphanumeric characters, or alternative lengths).

There isn't actually a tradeoff in safety here. You can bypass the validation of any type by casting in the way you've described.

Writing eth.HexAddr("KEVIN") is just as dangerous as writing gethcommon.Address([]byte("KEVIN"))

For documentation purposes:
gethcommon.Address([]byte(...)) is safer than eth.HexAddr(...) because gethcommon.Address forces a 20-byte array, whereas eth.HexAddr() can be of any string length, so gethcommon.Address([]byte("KEVIN")) will fail compilation whereas eth.HexAddr("KEVIN") will pass compilation, but it'll fail eth.HexAddr.Valid().

Since any 20-byte array is a valid Ethereum address, gethcommon.Address([]byte("KEVINKEVINKEVINKEVIN")) will compile and the letters will be parsed as UTF-8 and converted into bytes, whereas eth.HexAddr("KEVINKEVINKEVINKEVIN") passes compilation but will fail eth.HexAddr.Valid() because the string is not hex-encoded.

@k-yang k-yang restored the refactor/evm/hex-addr branch January 3, 2025 16:09
@k-yang k-yang deleted the refactor/evm/hex-addr branch January 3, 2025 16:14
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.

2 participants