aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorYondon Fu <yondon.fu@gmail.com>2017-12-19 06:17:41 +0800
committerYondon Fu <yondon.fu@gmail.com>2017-12-19 06:17:41 +0800
commit3857cdc267e3192697f561df0a0f827f65dfb6b5 (patch)
tree401c52c4972a68229ea283a394a0b0a5f3cfdc8e /common
parenta5330fe0c569b75cb8a524f60f7e8dc06498262b (diff)
parentfe070ab5c32702033489f1b9d1655ea1b894c29e (diff)
downloaddexon-3857cdc267e3192697f561df0a0f827f65dfb6b5.tar
dexon-3857cdc267e3192697f561df0a0f827f65dfb6b5.tar.gz
dexon-3857cdc267e3192697f561df0a0f827f65dfb6b5.tar.bz2
dexon-3857cdc267e3192697f561df0a0f827f65dfb6b5.tar.lz
dexon-3857cdc267e3192697f561df0a0f827f65dfb6b5.tar.xz
dexon-3857cdc267e3192697f561df0a0f827f65dfb6b5.tar.zst
dexon-3857cdc267e3192697f561df0a0f827f65dfb6b5.zip
Merge branch 'master' into abi-offset-fixed-arrays
Diffstat (limited to 'common')
-rw-r--r--common/bytes.go35
-rw-r--r--common/bytes_test.go47
-rw-r--r--common/hexutil/hexutil.go4
-rw-r--r--common/hexutil/json.go2
-rw-r--r--common/types.go9
-rw-r--r--common/types_test.go24
6 files changed, 82 insertions, 39 deletions
diff --git a/common/bytes.go b/common/bytes.go
index 66577bbfd..ba00e8a4b 100644
--- a/common/bytes.go
+++ b/common/bytes.go
@@ -17,9 +17,7 @@
// Package common contains various helper functions.
package common
-import (
- "encoding/hex"
-)
+import "encoding/hex"
func ToHex(b []byte) string {
hex := Bytes2Hex(b)
@@ -35,12 +33,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
@@ -56,14 +53,24 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
return
}
-func HasHexPrefix(str string) bool {
- l := len(str)
- return l >= 2 && str[0:2] == "0x"
+func hasHexPrefix(str string) bool {
+ return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
+}
+
+func isHexCharacter(c byte) bool {
+ return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
}
-func IsHex(str string) bool {
- l := len(str)
- return l >= 4 && l%2 == 0 && str[0:2] == "0x"
+func isHex(str string) bool {
+ if len(str)%2 != 0 {
+ return false
+ }
+ for _, c := range []byte(str) {
+ if !isHexCharacter(c) {
+ return false
+ }
+ }
+ return true
}
func Bytes2Hex(d []byte) string {
diff --git a/common/bytes_test.go b/common/bytes_test.go
index fc164b13d..97dd34d15 100644
--- a/common/bytes_test.go
+++ b/common/bytes_test.go
@@ -34,19 +34,6 @@ func (s *BytesSuite) TestCopyBytes(c *checker.C) {
c.Assert(res1, checker.DeepEquals, exp1)
}
-func (s *BytesSuite) TestIsHex(c *checker.C) {
- data1 := "a9e67e"
- exp1 := false
- res1 := IsHex(data1)
- c.Assert(res1, checker.DeepEquals, exp1)
-
- data2 := "0xa9e67e00"
- exp2 := true
- res2 := IsHex(data2)
- c.Assert(res2, checker.DeepEquals, exp2)
-
-}
-
func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
val1 := []byte{1, 2, 3, 4}
exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
@@ -74,7 +61,28 @@ 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)
+ }
+}
+
+func TestIsHex(t *testing.T) {
+ tests := []struct {
+ input string
+ ok bool
+ }{
+ {"", true},
+ {"0", false},
+ {"00", true},
+ {"a9e67e", true},
+ {"A9E67E", true},
+ {"0xa9e67e", false},
+ {"a9e67e001", false},
+ {"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false},
+ }
+ for _, test := range tests {
+ if ok := isHex(test.input); ok != test.ok {
+ t.Errorf("isHex(%q) = %v, want %v", test.input, ok, test.ok)
+ }
}
}
@@ -83,6 +91,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)
}
}
diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go
index 582a67c22..02c488a3f 100644
--- a/common/hexutil/hexutil.go
+++ b/common/hexutil/hexutil.go
@@ -53,9 +53,7 @@ var (
type decError struct{ msg string }
-func (err decError) Error() string {
- return string(err.msg)
-}
+func (err decError) Error() string { return err.msg }
// Decode decodes a hex string with 0x prefix.
func Decode(input string) ([]byte, error) {
diff --git a/common/hexutil/json.go b/common/hexutil/json.go
index 11e14cae7..fbc21241c 100644
--- a/common/hexutil/json.go
+++ b/common/hexutil/json.go
@@ -223,7 +223,7 @@ func (b *Uint64) UnmarshalText(input []byte) error {
return ErrSyntax
}
dec *= 16
- dec += uint64(nib)
+ dec += nib
}
*b = Uint64(dec)
return nil
diff --git a/common/types.go b/common/types.go
index d31bbf741..fdc67480c 100644
--- a/common/types.go
+++ b/common/types.go
@@ -150,13 +150,10 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
// IsHexAddress verifies whether a string can represent a valid hex-encoded
// Ethereum address or not.
func IsHexAddress(s string) bool {
- if len(s) == 2+2*AddressLength && IsHex(s) {
- return true
+ if hasHexPrefix(s) {
+ s = s[2:]
}
- if len(s) == 2*AddressLength && IsHex("0x"+s) {
- return true
- }
- return false
+ return len(s) == 2*AddressLength && isHex(s)
}
// Get the string representation of the underlying address
diff --git a/common/types_test.go b/common/types_test.go
index 6f3b31576..db636812c 100644
--- a/common/types_test.go
+++ b/common/types_test.go
@@ -35,6 +35,30 @@ func TestBytesConversion(t *testing.T) {
}
}
+func TestIsHexAddress(t *testing.T) {
+ tests := []struct {
+ str string
+ exp bool
+ }{
+ {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
+ {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
+ {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
+ {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
+ {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
+ {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
+ {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
+ {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
+ {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
+ }
+
+ for _, test := range tests {
+ if result := IsHexAddress(test.str); result != test.exp {
+ t.Errorf("IsHexAddress(%s) == %v; expected %v",
+ test.str, result, test.exp)
+ }
+ }
+}
+
func TestHashJsonValidation(t *testing.T) {
var tests = []struct {
Prefix string