mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
Merge a6bcbe696a into 7dde2b902c
This commit is contained in:
commit
18f4f58e6c
2 changed files with 17 additions and 9 deletions
|
|
@ -23,9 +23,14 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
hexRe = regexp.MustCompile("^(0x)?([a-fA-f0-9]{2})+$")
|
||||||
|
)
|
||||||
|
|
||||||
func ToHex(b []byte) string {
|
func ToHex(b []byte) string {
|
||||||
hex := Bytes2Hex(b)
|
hex := Bytes2Hex(b)
|
||||||
// Prefer output of "0x0" instead of "0x"
|
// Prefer output of "0x0" instead of "0x"
|
||||||
|
|
@ -139,8 +144,14 @@ func HasHexPrefix(str string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsHex(str string) bool {
|
func IsHex(str string) bool {
|
||||||
l := len(str)
|
return hexRe.MatchString(str)
|
||||||
return l >= 4 && l%2 == 0 && str[0:2] == "0x"
|
}
|
||||||
|
|
||||||
|
func NormaliseHex(str string) (string, bool) {
|
||||||
|
if HasHexPrefix(str) {
|
||||||
|
str = str[2:]
|
||||||
|
}
|
||||||
|
return str, IsHex(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Bytes2Hex(d []byte) string {
|
func Bytes2Hex(d []byte) string {
|
||||||
|
|
@ -202,8 +213,8 @@ func ParseData(data ...interface{}) (ret []byte) {
|
||||||
switch t := item.(type) {
|
switch t := item.(type) {
|
||||||
case string:
|
case string:
|
||||||
var str []byte
|
var str []byte
|
||||||
if IsHex(t) {
|
if n, ok := NormaliseHex(t); ok {
|
||||||
str = Hex2Bytes(t[2:])
|
str = Hex2Bytes(n)
|
||||||
} else {
|
} else {
|
||||||
str = []byte(t)
|
str = []byte(t)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,11 +95,8 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
|
||||||
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
||||||
// Ethereum address or not.
|
// Ethereum address or not.
|
||||||
func IsHexAddress(s string) bool {
|
func IsHexAddress(s string) bool {
|
||||||
if len(s) == 2+2*AddressLength && IsHex(s[2:]) {
|
if s, ok := NormaliseHex(s); ok {
|
||||||
return true
|
return len(s) == 2*AddressLength
|
||||||
}
|
|
||||||
if len(s) == 2*AddressLength && IsHex(s) {
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue