Commit 1cd5bf08 authored by lmittmann's avatar lmittmann Committed by Felix Lange

common: unify hex prefix check code (#19937)

parent 7c229941
...@@ -43,10 +43,8 @@ func ToHexArray(b [][]byte) []string { ...@@ -43,10 +43,8 @@ func ToHexArray(b [][]byte) []string {
// 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) > 1 { if has0xPrefix(s) {
if s[0:2] == "0x" || s[0:2] == "0X" { s = s[2:]
s = s[2:]
}
} }
if len(s)%2 == 1 { if len(s)%2 == 1 {
s = "0" + s s = "0" + s
...@@ -65,8 +63,8 @@ func CopyBytes(b []byte) (copiedBytes []byte) { ...@@ -65,8 +63,8 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
return return
} }
// hasHexPrefix validates str begins with '0x' or '0X'. // has0xPrefix validates str begins with '0x' or '0X'.
func hasHexPrefix(str string) bool { func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
} }
......
...@@ -193,7 +193,7 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } ...@@ -193,7 +193,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 hasHexPrefix(s) { if has0xPrefix(s) {
s = s[2:] s = s[2:]
} }
return len(s) == 2*AddressLength && isHex(s) return len(s) == 2*AddressLength && isHex(s)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment