Skip to content

Commit afb8154

Browse files
stevenroosefjl
authored andcommitted
common: improve IsHexAddress and add tests (#15551)
Also unexport isHex, hasHexPrefix because IsHexAddress is the only caller. Fixes #15550
1 parent 1d06e41 commit afb8154

4 files changed

Lines changed: 65 additions & 28 deletions

File tree

‎common/bytes.go‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
// Package common contains various helper functions.
1818
package common
1919

20-
import (
21-
"encoding/hex"
22-
)
20+
import "encoding/hex"
2321

2422
func ToHex(b []byte) string {
2523
hex := Bytes2Hex(b)
@@ -55,14 +53,24 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
5553
return
5654
}
5755

58-
func HasHexPrefix(str string) bool {
59-
l := len(str)
60-
return l >= 2 && str[0:2] == "0x"
56+
func hasHexPrefix(str string) bool {
57+
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
6158
}
6259

63-
func IsHex(str string) bool {
64-
l := len(str)
65-
return l >= 4 && l%2 == 0 && str[0:2] == "0x"
60+
func isHexCharacter(c byte) bool {
61+
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
62+
}
63+
64+
func isHex(str string) bool {
65+
if len(str)%2 != 0 {
66+
return false
67+
}
68+
for _, c := range []byte(str) {
69+
if !isHexCharacter(c) {
70+
return false
71+
}
72+
}
73+
return true
6674
}
6775

6876
func Bytes2Hex(d []byte) string {

‎common/bytes_test.go‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,6 @@ func (s *BytesSuite) TestCopyBytes(c *checker.C) {
3434
c.Assert(res1, checker.DeepEquals, exp1)
3535
}
3636

37-
func (s *BytesSuite) TestIsHex(c *checker.C) {
38-
data1 := "a9e67e"
39-
exp1 := false
40-
res1 := IsHex(data1)
41-
c.Assert(res1, checker.DeepEquals, exp1)
42-
43-
data2 := "0xa9e67e00"
44-
exp2 := true
45-
res2 := IsHex(data2)
46-
c.Assert(res2, checker.DeepEquals, exp2)
47-
48-
}
49-
5037
func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
5138
val1 := []byte{1, 2, 3, 4}
5239
exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
@@ -78,6 +65,27 @@ func TestFromHex(t *testing.T) {
7865
}
7966
}
8067

68+
func TestIsHex(t *testing.T) {
69+
tests := []struct {
70+
input string
71+
ok bool
72+
}{
73+
{"", true},
74+
{"0", false},
75+
{"00", true},
76+
{"a9e67e", true},
77+
{"A9E67E", true},
78+
{"0xa9e67e", false},
79+
{"a9e67e001", false},
80+
{"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false},
81+
}
82+
for _, test := range tests {
83+
if ok := isHex(test.input); ok != test.ok {
84+
t.Errorf("isHex(%q) = %v, want %v", test.input, ok, test.ok)
85+
}
86+
}
87+
}
88+
8189
func TestFromHexOddLength(t *testing.T) {
8290
input := "0x1"
8391
expected := []byte{1}

‎common/types.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,10 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
150150
// IsHexAddress verifies whether a string can represent a valid hex-encoded
151151
// Ethereum address or not.
152152
func IsHexAddress(s string) bool {
153-
if len(s) == 2+2*AddressLength && IsHex(s) {
154-
return true
153+
if hasHexPrefix(s) {
154+
s = s[2:]
155155
}
156-
if len(s) == 2*AddressLength && IsHex("0x"+s) {
157-
return true
158-
}
159-
return false
156+
return len(s) == 2*AddressLength && isHex(s)
160157
}
161158

162159
// Get the string representation of the underlying address

‎common/types_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ func TestBytesConversion(t *testing.T) {
3535
}
3636
}
3737

38+
func TestIsHexAddress(t *testing.T) {
39+
tests := []struct {
40+
str string
41+
exp bool
42+
}{
43+
{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
44+
{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
45+
{"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
46+
{"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
47+
{"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
48+
{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
49+
{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
50+
{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
51+
{"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
52+
}
53+
54+
for _, test := range tests {
55+
if result := IsHexAddress(test.str); result != test.exp {
56+
t.Errorf("IsHexAddress(%s) == %v; expected %v",
57+
test.str, result, test.exp)
58+
}
59+
}
60+
}
61+
3862
func TestHashJsonValidation(t *testing.T) {
3963
var tests = []struct {
4064
Prefix string

0 commit comments

Comments
 (0)