common: fix hex utils to handle 1 byte address conversions

This commit is contained in:
Péter Szilágyi 2017-11-29 02:21:41 +02:00
parent be12392fba
commit b33a5294ea
No known key found for this signature in database
GPG Key ID: E9AE538CEDF8293D
2 changed files with 15 additions and 7 deletions

View File

@ -35,12 +35,11 @@ func FromHex(s string) []byte {
if s[0:2] == "0x" || s[0:2] == "0X" { 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
} }
return Hex2Bytes(s) return Hex2Bytes(s)
}
return nil
} }
// Copy bytes // Copy bytes

View File

@ -74,7 +74,7 @@ func TestFromHex(t *testing.T) {
expected := []byte{1} expected := []byte{1}
result := FromHex(input) result := FromHex(input)
if !bytes.Equal(expected, result) { 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} expected := []byte{1}
result := FromHex(input) result := FromHex(input)
if !bytes.Equal(expected, result) { 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)
} }
} }