aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2017-11-29 17:27:24 +0800
committerGitHub <noreply@github.com>2017-11-29 17:27:24 +0800
commite37f7be97e47a032d723db16d8b195998547805a (patch)
treeefb7b8e936fe001180067703fcb226c90779023a
parentbe12392fbad9f4a861130a347e6bcf07a5c34974 (diff)
parentb33a5294eaf06a3e952efe1ce4ef810af37d1062 (diff)
downloaddexon-e37f7be97e47a032d723db16d8b195998547805a.tar.gz
dexon-e37f7be97e47a032d723db16d8b195998547805a.tar.zst
dexon-e37f7be97e47a032d723db16d8b195998547805a.zip
Merge pull request #15577 from karalabe/common-hexconvert-singlebyte
common: fix hex utils to handle 1 byte address conversions
-rw-r--r--common/bytes.go9
-rw-r--r--common/bytes_test.go13
2 files changed, 15 insertions, 7 deletions
diff --git a/common/bytes.go b/common/bytes.go
index 66577bbfd..bb40ac1d7 100644
--- a/common/bytes.go
+++ b/common/bytes.go
@@ -35,12 +35,11 @@ func FromHex(s string) []byte {
if s[0:2] == "0x" || s[0:2] == "0X" {
s = s[2:]
}
- if len(s)%2 == 1 {
- s = "0" + s
- }
- return Hex2Bytes(s)
}
- return nil
+ if len(s)%2 == 1 {
+ s = "0" + s
+ }
+ return Hex2Bytes(s)
}
// Copy bytes
diff --git a/common/bytes_test.go b/common/bytes_test.go
index fc164b13d..71631e6dd 100644
--- a/common/bytes_test.go
+++ b/common/bytes_test.go
@@ -74,7 +74,7 @@ func TestFromHex(t *testing.T) {
expected := []byte{1}
result := FromHex(input)
if !bytes.Equal(expected, result) {
- t.Errorf("Expected % x got % x", expected, result)
+ t.Errorf("Expected %x got %x", expected, result)
}
}
@@ -83,6 +83,15 @@ func TestFromHexOddLength(t *testing.T) {
expected := []byte{1}
result := FromHex(input)
if !bytes.Equal(expected, result) {
- t.Errorf("Expected % x got % x", expected, result)
+ t.Errorf("Expected %x got %x", expected, result)
+ }
+}
+
+func TestNoPrefixShortHexOddLength(t *testing.T) {
+ input := "1"
+ expected := []byte{1}
+ result := FromHex(input)
+ if !bytes.Equal(expected, result) {
+ t.Errorf("Expected %x got %x", expected, result)
}
}