Skip to content

Commit

Permalink
common: fix hex utils to handle 1 byte address conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Nov 29, 2017
1 parent be12392 commit b33a529
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 4 additions & 5 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions common/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

0 comments on commit b33a529

Please sign in to comment.