Skip to content

Commit b33a529

Browse files
committed
common: fix hex utils to handle 1 byte address conversions
1 parent be12392 commit b33a529

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎common/bytes.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ func FromHex(s string) []byte {
3535
if s[0:2] == "0x" || s[0:2] == "0X" {
3636
s = s[2:]
3737
}
38-
if len(s)%2 == 1 {
39-
s = "0" + s
40-
}
41-
return Hex2Bytes(s)
4238
}
43-
return nil
39+
if len(s)%2 == 1 {
40+
s = "0" + s
41+
}
42+
return Hex2Bytes(s)
4443
}
4544

4645
// Copy bytes

‎common/bytes_test.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestFromHex(t *testing.T) {
7474
expected := []byte{1}
7575
result := FromHex(input)
7676
if !bytes.Equal(expected, result) {
77-
t.Errorf("Expected % x got % x", expected, result)
77+
t.Errorf("Expected %x got %x", expected, result)
7878
}
7979
}
8080

@@ -83,6 +83,15 @@ func TestFromHexOddLength(t *testing.T) {
8383
expected := []byte{1}
8484
result := FromHex(input)
8585
if !bytes.Equal(expected, result) {
86-
t.Errorf("Expected % x got % x", expected, result)
86+
t.Errorf("Expected %x got %x", expected, result)
87+
}
88+
}
89+
90+
func TestNoPrefixShortHexOddLength(t *testing.T) {
91+
input := "1"
92+
expected := []byte{1}
93+
result := FromHex(input)
94+
if !bytes.Equal(expected, result) {
95+
t.Errorf("Expected %x got %x", expected, result)
8796
}
8897
}

0 commit comments

Comments
 (0)