Skip to content

Commit

Permalink
chg: optimize formatAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
marcello33 committed Oct 14, 2024
1 parent 3cdd830 commit 9f1ea4c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 38 deletions.
22 changes: 3 additions & 19 deletions codec/address/hex_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ func (bc HexCodec) StringToBytes(hexAddr string) ([]byte, error) {
return []byte{}, errors.New("empty address string is not allowed")
}

hexAddr = strings.ToLower(hexAddr)

if !has0xPrefix(hexAddr) {
hexAddr = "0x" + hexAddr
}
hexAddr = "0x" + strings.TrimPrefix(strings.ToLower(hexAddr), "0x")

bz := common.FromHex(hexAddr)

Expand All @@ -51,21 +47,9 @@ func (bc HexCodec) BytesToString(bz []byte) (string, error) {
return "", err
}

hexAddr := common.Bytes2Hex(bz)

hexAddr = strings.ToLower(hexAddr)

if has0xPrefix(hexAddr) {
return hexAddr, nil
} else {
return "0x" + hexAddr, nil
}

}
hexAddr := "0x" + strings.TrimPrefix(strings.ToLower(common.Bytes2Hex(bz)), "0x")

// has0xPrefix validates str begins with '0x' or '0X'.
func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
return hexAddr, nil
}

// VerifyAddressFormat verifies that the provided bytes form a valid address
Expand Down
22 changes: 3 additions & 19 deletions x/auth/codec/hex_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ func (bc hexCodec) StringToBytes(hexAddr string) ([]byte, error) {
return []byte{}, errors.New("empty address string is not allowed")
}

hexAddr = strings.ToLower(hexAddr)

if !has0xPrefix(hexAddr) {
hexAddr = "0x" + hexAddr
}
hexAddr = "0x" + strings.TrimPrefix(strings.ToLower(hexAddr), "0x")

bz := common.FromHex(hexAddr)

Expand All @@ -51,21 +47,9 @@ func (bc hexCodec) BytesToString(bz []byte) (string, error) {
return "", err
}

hexAddr := common.Bytes2Hex(bz)

hexAddr = strings.ToLower(hexAddr)

if has0xPrefix(hexAddr) {
return hexAddr, nil
} else {
return "0x" + hexAddr, nil
}

}
hexAddr := "0x" + strings.TrimPrefix(strings.ToLower(common.Bytes2Hex(bz)), "0x")

// has0xPrefix validates str begins with '0x' or '0X'.
func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
return hexAddr, nil
}

// VerifyAddressFormat verifies that the provided bytes form a valid address
Expand Down

0 comments on commit 9f1ea4c

Please sign in to comment.