diff --git a/common/string.go b/common/string.go index a4d221b74..f053f0e87 100644 --- a/common/string.go +++ b/common/string.go @@ -1,6 +1,7 @@ package common import ( + "encoding/hex" "fmt" "strings" ) @@ -22,3 +23,22 @@ func LeftPadString(s string, totalLength int) string { } return s } + +// Returns true for non-empty hex-string prefixed with "0x" +func IsHex(s string) bool { + if len(s) > 2 && s[:2] == "0x" { + _, err := hex.DecodeString(s[2:]) + if err != nil { + return false + } + return true + } + return false +} + +func StripHex(s string) string { + if IsHex(s) { + return s[2:] + } + return s +}