revert changes for has0xprefix func

This commit is contained in:
Sahil-4555 2025-09-27 09:10:05 +05:30
parent a1d860e0ef
commit 5061a08452
3 changed files with 8 additions and 4 deletions

View file

@ -20,7 +20,6 @@ package common
import ( import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
) )
@ -28,7 +27,7 @@ import (
// FromHex returns the bytes represented by the hexadecimal string s. // FromHex returns the bytes represented by the hexadecimal string s.
// s may be prefixed with "0x". // s may be prefixed with "0x".
func FromHex(s string) []byte { func FromHex(s string) []byte {
if len(s) >= 2 && strings.HasPrefix(strings.ToLower(s), "0x") { if has0xPrefix(s) {
s = s[2:] s = s[2:]
} }
if len(s)&1 == 1 { if len(s)&1 == 1 {
@ -37,6 +36,11 @@ func FromHex(s string) []byte {
return Hex2Bytes(s) 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. // CopyBytes returns an exact copy of the provided bytes.
func CopyBytes(b []byte) (copiedBytes []byte) { func CopyBytes(b []byte) (copiedBytes []byte) {
if b == nil { if b == nil {

View file

@ -231,7 +231,7 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
// IsHexAddress verifies whether a string can represent a valid hex-encoded // IsHexAddress verifies whether a string can represent a valid hex-encoded
// Ethereum address or not. // Ethereum address or not.
func IsHexAddress(s string) bool { func IsHexAddress(s string) bool {
if len(s) >= 2 && strings.HasPrefix(strings.ToLower(s), "0x") { if has0xPrefix(s) {
s = s[2:] s = s[2:]
} }
return len(s) == 2*AddressLength && isHex(s) return len(s) == 2*AddressLength && isHex(s)

View file

@ -215,4 +215,4 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
accum.Div(accum, big.NewInt(int64(i))) accum.Div(accum, big.NewInt(int64(i)))
} }
return output.Div(output, denominator) return output.Div(output, denominator)
} }