From b33a5294eaf06a3e952efe1ce4ef810af37d1062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 29 Nov 2017 02:21:41 +0200 Subject: common: fix hex utils to handle 1 byte address conversions --- common/bytes.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'common/bytes.go') 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 -- cgit v1.2.3 From afb8154eab2961996b321ac3fe5a21602e3b1aff Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Mon, 4 Dec 2017 19:34:15 +0100 Subject: common: improve IsHexAddress and add tests (#15551) Also unexport isHex, hasHexPrefix because IsHexAddress is the only caller. Fixes #15550 --- common/bytes.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'common/bytes.go') diff --git a/common/bytes.go b/common/bytes.go index bb40ac1d7..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) @@ -55,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 IsHex(str string) bool { - l := len(str) - return l >= 4 && l%2 == 0 && str[0:2] == "0x" +func isHexCharacter(c byte) bool { + return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') +} + +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 { -- cgit v1.2.3