From 0c05e37996c65692f964ee978ca43d96c9cb4d35 Mon Sep 17 00:00:00 2001 From: lmittmann Date: Sun, 11 Aug 2019 12:59:43 +0200 Subject: [PATCH 1/4] unifying hex prefix check --- common/bytes.go | 17 +++++++---------- common/hexutil/hexutil.go | 7 ++++--- common/math/big.go | 4 +++- common/math/integer.go | 4 +++- common/types.go | 2 +- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index c82e61624187..bdaad5e963d0 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -17,7 +17,11 @@ // Package common contains various helper functions. package common -import "encoding/hex" +import ( + "encoding/hex" + + "github.com/ethereum/go-ethereum/common/hexutil" +) // ToHex returns the hex representation of b, prefixed with '0x'. // For empty slices, the return value is "0x0". @@ -43,10 +47,8 @@ func ToHexArray(b [][]byte) []string { // FromHex returns the bytes represented by the hexadecimal string s. // s may be prefixed with "0x". func FromHex(s string) []byte { - if len(s) > 1 { - if s[0:2] == "0x" || s[0:2] == "0X" { - s = s[2:] - } + if hexutil.Has0xPrefix(s) { + s = s[2:] } if len(s)%2 == 1 { s = "0" + s @@ -65,11 +67,6 @@ func CopyBytes(b []byte) (copiedBytes []byte) { return } -// hasHexPrefix validates str begins with '0x' or '0X'. -func hasHexPrefix(str string) bool { - return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') -} - // isHexCharacter returns bool of c being a valid hexadecimal. func isHexCharacter(c byte) bool { return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go index 46223a2815a2..f519f0fdd608 100644 --- a/common/hexutil/hexutil.go +++ b/common/hexutil/hexutil.go @@ -61,7 +61,7 @@ func Decode(input string) ([]byte, error) { if len(input) == 0 { return nil, ErrEmptyString } - if !has0xPrefix(input) { + if !Has0xPrefix(input) { return nil, ErrMissingPrefix } b, err := hex.DecodeString(input[2:]) @@ -185,7 +185,8 @@ func EncodeBig(bigint *big.Int) string { return fmt.Sprintf("%#x", bigint) } -func has0xPrefix(input string) bool { +// Has0xPrefix validates input begins with '0x' or '0X'. +func Has0xPrefix(input string) bool { return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X') } @@ -193,7 +194,7 @@ func checkNumber(input string) (raw string, err error) { if len(input) == 0 { return "", ErrEmptyString } - if !has0xPrefix(input) { + if !Has0xPrefix(input) { return "", ErrMissingPrefix } input = input[2:] diff --git a/common/math/big.go b/common/math/big.go index d31c59af10ab..5796778bd2b5 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -20,6 +20,8 @@ package math import ( "fmt" "math/big" + + "github.com/ethereum/go-ethereum/common/hexutil" ) // Various big integer limit values. @@ -75,7 +77,7 @@ func ParseBig256(s string) (*big.Int, bool) { } var bigint *big.Int var ok bool - if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") { + if hexutil.Has0xPrefix(s) { bigint, ok = new(big.Int).SetString(s[2:], 16) } else { bigint, ok = new(big.Int).SetString(s, 10) diff --git a/common/math/integer.go b/common/math/integer.go index 93b1d036ddbb..2214415c33a1 100644 --- a/common/math/integer.go +++ b/common/math/integer.go @@ -19,6 +19,8 @@ package math import ( "fmt" "strconv" + + "github.com/ethereum/go-ethereum/common/hexutil" ) // Integer limit values. @@ -61,7 +63,7 @@ func ParseUint64(s string) (uint64, bool) { if s == "" { return 0, true } - if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") { + if hexutil.Has0xPrefix(s) { v, err := strconv.ParseUint(s[2:], 16, 64) return v, err == nil } diff --git a/common/types.go b/common/types.go index 98c83edd4fa2..4f3180dc05c4 100644 --- a/common/types.go +++ b/common/types.go @@ -193,7 +193,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 hasHexPrefix(s) { + if hexutil.Has0xPrefix(s) { s = s[2:] } return len(s) == 2*AddressLength && isHex(s) From d9258162ca0b2972e8009cdbd914fac2fc733275 Mon Sep 17 00:00:00 2001 From: lmittmann Date: Thu, 15 Aug 2019 12:33:56 +0200 Subject: [PATCH 2/4] Revert "unifying hex prefix check" This reverts commit 0c05e37996c65692f964ee978ca43d96c9cb4d35. --- common/bytes.go | 17 ++++++++++------- common/hexutil/hexutil.go | 7 +++---- common/math/big.go | 4 +--- common/math/integer.go | 4 +--- common/types.go | 2 +- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index bdaad5e963d0..c82e61624187 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -17,11 +17,7 @@ // Package common contains various helper functions. package common -import ( - "encoding/hex" - - "github.com/ethereum/go-ethereum/common/hexutil" -) +import "encoding/hex" // ToHex returns the hex representation of b, prefixed with '0x'. // For empty slices, the return value is "0x0". @@ -47,8 +43,10 @@ func ToHexArray(b [][]byte) []string { // FromHex returns the bytes represented by the hexadecimal string s. // s may be prefixed with "0x". func FromHex(s string) []byte { - if hexutil.Has0xPrefix(s) { - s = s[2:] + if len(s) > 1 { + if s[0:2] == "0x" || s[0:2] == "0X" { + s = s[2:] + } } if len(s)%2 == 1 { s = "0" + s @@ -67,6 +65,11 @@ func CopyBytes(b []byte) (copiedBytes []byte) { return } +// hasHexPrefix validates str begins with '0x' or '0X'. +func hasHexPrefix(str string) bool { + return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') +} + // isHexCharacter returns bool of c being a valid hexadecimal. func isHexCharacter(c byte) bool { return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go index f519f0fdd608..46223a2815a2 100644 --- a/common/hexutil/hexutil.go +++ b/common/hexutil/hexutil.go @@ -61,7 +61,7 @@ func Decode(input string) ([]byte, error) { if len(input) == 0 { return nil, ErrEmptyString } - if !Has0xPrefix(input) { + if !has0xPrefix(input) { return nil, ErrMissingPrefix } b, err := hex.DecodeString(input[2:]) @@ -185,8 +185,7 @@ func EncodeBig(bigint *big.Int) string { return fmt.Sprintf("%#x", bigint) } -// Has0xPrefix validates input begins with '0x' or '0X'. -func Has0xPrefix(input string) bool { +func has0xPrefix(input string) bool { return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X') } @@ -194,7 +193,7 @@ func checkNumber(input string) (raw string, err error) { if len(input) == 0 { return "", ErrEmptyString } - if !Has0xPrefix(input) { + if !has0xPrefix(input) { return "", ErrMissingPrefix } input = input[2:] diff --git a/common/math/big.go b/common/math/big.go index 5796778bd2b5..d31c59af10ab 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -20,8 +20,6 @@ package math import ( "fmt" "math/big" - - "github.com/ethereum/go-ethereum/common/hexutil" ) // Various big integer limit values. @@ -77,7 +75,7 @@ func ParseBig256(s string) (*big.Int, bool) { } var bigint *big.Int var ok bool - if hexutil.Has0xPrefix(s) { + if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") { bigint, ok = new(big.Int).SetString(s[2:], 16) } else { bigint, ok = new(big.Int).SetString(s, 10) diff --git a/common/math/integer.go b/common/math/integer.go index 2214415c33a1..93b1d036ddbb 100644 --- a/common/math/integer.go +++ b/common/math/integer.go @@ -19,8 +19,6 @@ package math import ( "fmt" "strconv" - - "github.com/ethereum/go-ethereum/common/hexutil" ) // Integer limit values. @@ -63,7 +61,7 @@ func ParseUint64(s string) (uint64, bool) { if s == "" { return 0, true } - if hexutil.Has0xPrefix(s) { + if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") { v, err := strconv.ParseUint(s[2:], 16, 64) return v, err == nil } diff --git a/common/types.go b/common/types.go index 4f3180dc05c4..98c83edd4fa2 100644 --- a/common/types.go +++ b/common/types.go @@ -193,7 +193,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 hexutil.Has0xPrefix(s) { + if hasHexPrefix(s) { s = s[2:] } return len(s) == 2*AddressLength && isHex(s) From 5b2834856eebbe0656c01c3c40756b5372219ca2 Mon Sep 17 00:00:00 2001 From: lmittmann Date: Thu, 15 Aug 2019 12:40:58 +0200 Subject: [PATCH 3/4] use hasHexPrefix for 0x prefix check --- common/bytes.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index c82e61624187..b1cca130af90 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -43,10 +43,8 @@ func ToHexArray(b [][]byte) []string { // FromHex returns the bytes represented by the hexadecimal string s. // s may be prefixed with "0x". func FromHex(s string) []byte { - if len(s) > 1 { - if s[0:2] == "0x" || s[0:2] == "0X" { - s = s[2:] - } + if hasHexPrefix(s) { + s = s[2:] } if len(s)%2 == 1 { s = "0" + s From 873bc9427a352a251f29d8baaf9348b6c4bc7957 Mon Sep 17 00:00:00 2001 From: lmittmann Date: Thu, 15 Aug 2019 12:41:49 +0200 Subject: [PATCH 4/4] renamed hasHexPrefix -> has0xPrefix --- common/bytes.go | 6 +++--- common/types.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index b1cca130af90..910c97d3c111 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -43,7 +43,7 @@ func ToHexArray(b [][]byte) []string { // FromHex returns the bytes represented by the hexadecimal string s. // s may be prefixed with "0x". func FromHex(s string) []byte { - if hasHexPrefix(s) { + if has0xPrefix(s) { s = s[2:] } if len(s)%2 == 1 { @@ -63,8 +63,8 @@ func CopyBytes(b []byte) (copiedBytes []byte) { return } -// hasHexPrefix validates str begins with '0x' or '0X'. -func hasHexPrefix(str string) bool { +// 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') } diff --git a/common/types.go b/common/types.go index 98c83edd4fa2..5cba4e9f3d1f 100644 --- a/common/types.go +++ b/common/types.go @@ -193,7 +193,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 hasHexPrefix(s) { + if has0xPrefix(s) { s = s[2:] } return len(s) == 2*AddressLength && isHex(s)