Skip to content

Commit

Permalink
fix EIP55Checksum function (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
iCell authored Jul 17, 2024
1 parent e462dc7 commit 348c760
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
25 changes: 19 additions & 6 deletions address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,25 @@ func EIP55Checksum(unchecksummed string) (string, error) {
return "0x" + val, nil
}

func ToEIP55ByCoinID(str string, coinID uint) (string, error) {
switch coinID {
case coin.ETHEREUM, coin.POA, coin.CLASSIC, coin.TOMOCHAIN, coin.CALLISTO,
coin.THUNDERTOKEN, coin.GOCHAIN, coin.WANCHAIN:
return EIP55Checksum(str)
default:
func ToEIP55ByCoinID(str string, coinID uint) (eip55Addr string, err error) {
if !coin.IsEVM(coinID) {
return str, nil
}

// special case for ronin addresses
const roninPrefix, hexPrefix = "ronin:", "0x"
if coinID == coin.RONIN && strings.HasPrefix(str, roninPrefix) {
str = hexPrefix + str[len(roninPrefix):]
defer func() {
// remove 0x prefix, then add roninPrefix
eip55Addr = roninPrefix + eip55Addr[len(hexPrefix):]
}()
}

eip55Addr, err = EIP55Checksum(str)
if err != nil {
return "", err
}

return
}
9 changes: 8 additions & 1 deletion address/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/trustwallet/go-primitives/coin"
)

Expand Down Expand Up @@ -68,7 +69,11 @@ func TestToEIP55ByCoinID(t *testing.T) {
wanAddrLowercase = "0xae96137e0e05681ed2f5d1af272c3ee512939d0f"
wanAddrEIP55Checksum = "0xAe96137E0e05681eD2F5D1AF272C3ee512939D0F"
wanAddrEIP55ChecksumWanchain = "0xAe96137E0e05681eD2F5D1AF272C3ee512939D0F"
tests = []struct {

roninAddr = "ronin:ea674fdde714fd979de3edf0f56aa9716b898ec8"
roninAddrEIP55 = "ronin:EA674fdDe714fd979de3EdF0F56AA9716B898ec8"

tests = []struct {
name, address, expectedAddress string
coinID uint
}{
Expand All @@ -84,6 +89,8 @@ func TestToEIP55ByCoinID(t *testing.T) {
{"Wanchain 2", wanAddrEIP55Checksum, wanAddrEIP55ChecksumWanchain, coin.WANCHAIN},
{"Non Ethereum like chain 1", "", "", coin.TRON},
{"Non Ethereum like chain 2", addr1, addr1, coin.BINANCE},
{"SmartChain", addr1, addr1EIP55, coin.SMARTCHAIN},
{"Ronin", roninAddr, roninAddrEIP55, coin.RONIN},
}
)

Expand Down

0 comments on commit 348c760

Please sign in to comment.