diff --git a/common/bytes.go b/common/bytes.go index 2dd86f3d76..ee516976c8 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -20,7 +20,6 @@ package common import ( "encoding/hex" "errors" - "strings" "github.com/ethereum/go-ethereum/common/hexutil" ) @@ -28,7 +27,7 @@ import ( // FromHex returns the bytes represented by the hexadecimal string s. // s may be prefixed with "0x". func FromHex(s string) []byte { - if len(s) >= 2 && strings.HasPrefix(strings.ToLower(s), "0x") { + if has0xPrefix(s) { s = s[2:] } if len(s)&1 == 1 { @@ -37,6 +36,11 @@ func FromHex(s string) []byte { return Hex2Bytes(s) } +// 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') +} + // CopyBytes returns an exact copy of the provided bytes. func CopyBytes(b []byte) (copiedBytes []byte) { if b == nil { diff --git a/common/types.go b/common/types.go index 13dc65d8a3..db4de8bcbd 100644 --- a/common/types.go +++ b/common/types.go @@ -231,7 +231,7 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } // IsHexAddress verifies whether a string can represent a valid hex-encoded // Ethereum address or not. func IsHexAddress(s string) bool { - if len(s) >= 2 && strings.HasPrefix(strings.ToLower(s), "0x") { + if has0xPrefix(s) { s = s[2:] } return len(s) == 2*AddressLength && isHex(s) diff --git a/consensus/misc/eip4844/eip4844.go b/consensus/misc/eip4844/eip4844.go index e14d129561..faeb696e22 100644 --- a/consensus/misc/eip4844/eip4844.go +++ b/consensus/misc/eip4844/eip4844.go @@ -215,4 +215,4 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int { accum.Div(accum, big.NewInt(int64(i))) } return output.Div(output, denominator) -} +} \ No newline at end of file