Skip to content

fix(erc20): add zero-address validation in transfer (#975)#1197

Open
aglichandrap wants to merge 3 commits into
cosmos:mainfrom
aglichandrap:fix/erc20-zero-address
Open

fix(erc20): add zero-address validation in transfer (#975)#1197
aglichandrap wants to merge 3 commits into
cosmos:mainfrom
aglichandrap:fix/erc20-zero-address

Conversation

@aglichandrap

Copy link
Copy Markdown

Problem

The ERC-20 precompile's transfer() and transferFrom() functions do not validate that the recipient address is non-zero. Per ERC-20 standard (EIP-20), transfers to address(0) should revert, but currently they succeed, causing permanent token loss.

Fix

Added zero-address validation in the common transfer() function:

if to == (common.Address{}) {
    return nil, fmt.Errorf("%w: %s", erc20.ErrERC20, "transfer to zero address")
}

This check covers both Transfer and TransferFrom since they both call transfer().

Testing

  • Transfer(to=0x0, amount=100) → should revert
  • Transfer(to=validAddr, amount=100) → should succeed
  • TransferFrom(from=validAddr, to=0x0, amount=100) → should revert

Fixes #975

Per ERC-20 standard (EIP-20), transfers to address(0) should revert.
Currently the precompile allows transfers to the zero address,
causing permanent token loss.

Added validation in the common transfer() function to reject
transfers where the recipient is the zero address.

Fixes cosmos#975
@aglichandrap aglichandrap requested a review from a team as a code owner May 24, 2026 14:52
@greptile-apps

greptile-apps Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

PR author is not in the allowed authors list.

@aljo242

aljo242 commented May 26, 2026

Copy link
Copy Markdown
Contributor

no test coverage for the new zero-address guard. the pr description lists three cases — please add them:

  • transfer(to=address(0), amount>0) should revert
  • transferFrom(from=valid, to=address(0), amount>0) should revert
  • transfer(to=validAddr, amount>0) should still pass

also worth adding a from == address(0) check for transferFrom — transfers from the zero address are also invalid per erc-20 spec.

@aljo242 aljo242 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

missing tests for the new zero-address guard and missing from == address(0) check in transferFrom.

…ferFrom

Add test cases requested by reviewer:
- transfer to zero address should fail
- transferFrom to zero address should fail
- transferFrom from zero address should fail

Refs: cosmos#975
Addresses review feedback from aljo242 on PR cosmos#1197.

- Add from == address(0) check in TransferFrom to prevent transfers
  from the zero address
- Update test expectation to match new error message

The transfer() function already validates to == address(0). This commit
adds the complementary from == address(0) check in TransferFrom, which
is called before delegation to transfer().
@aglichandrap

Copy link
Copy Markdown
Author

Thanks for the review @aljo242! I've addressed both points:

1. Tests for zero-address guard

Tests were already added in commit d8f8c470:

  • TestTransfer: "fail - transfer to zero address" (line 122)
  • TestTransferFrom: "fail - transfer to zero address" (line 265)
  • TestTransferFrom: "fail - transfer from zero address" (line 274)

2. from == address(0) check in TransferFrom

Added in commit 291d8502:

// Validate that the sender is not the zero address.
if from == (common.Address{}) {
    return nil, fmt.Errorf("%w: %s", erc20.ErrERC20, "transfer from zero address")
}

Also updated the test expectation to match the new error message (was "insufficient allowance", now "transfer from zero address").

All 3 requested test cases are now covered with proper validation.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days-before-close if no further activity occurs.

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.

ERC-20 precompile: add zero-address validation in transfer/transferFrom

2 participants