Skip to content

Commit

Permalink
add check for eth address
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Apr 16, 2024
1 parent a1a54d3 commit 1ec49e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 1 addition & 3 deletions modules/token/types/v1/msgs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1

import (
"encoding/hex"
fmt "fmt"
"regexp"

Expand Down Expand Up @@ -421,8 +420,7 @@ func (m *MsgSwapToERC20) ValidateBasic() error {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}

_, err := hex.DecodeString(m.Receiver)
if err != nil {
if tokentypes.IsValidEthAddress(m.Receiver) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "expecting a hex address of 0x, got %s", m.Receiver)
}

Expand Down
11 changes: 11 additions & 0 deletions modules/token/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ var (

regexpMinUintFmt = fmt.Sprintf("^[a-z][a-z0-9]{%d,%d}$", MinimumMinUnitLen-1, MaximumMinUnitLen-1)
regexpMinUint = regexp.MustCompile(regexpMinUintFmt).MatchString

regexpEthAddressLowerStr = "^0x[0-9a-f]{40}$"
regexpEthAddressUpperStr = "^0x[0-9A-F]{40}$"
regexpEthAddressLower = regexp.MustCompile(regexpEthAddressLowerStr).MatchString
regexpEthAddressUpper = regexp.MustCompile(regexpEthAddressUpperStr).MatchString
)

// ValidateInitialSupply verifies whether the initial supply is legal
Expand Down Expand Up @@ -115,3 +120,9 @@ func ValidateCoin(coin sdk.Coin) error {
}
return ValidateMinUnit(coin.Denom)
}

// IsValidEthAddress checks if the given address is valid ethereum address
func IsValidEthAddress(address string) bool {
address = strings.ToLower(address)
return regexpEthAddressLower(address) || regexpEthAddressUpper(address)
}

0 comments on commit 1ec49e0

Please sign in to comment.