diff --git a/extras/util/strings.go b/extras/util/strings.go index 46c73c4..9000b29 100644 --- a/extras/util/strings.go +++ b/extras/util/strings.go @@ -1,6 +1,11 @@ package util -import "strings" +import ( + "encoding/hex" + "golang.org/x/text/cases" + "golang.org/x/text/unicode/norm" + "strings" +) func StringSplitArg(stringToSplit, separator string) []interface{} { split := strings.Split(stringToSplit, separator) @@ -10,3 +15,38 @@ func StringSplitArg(stringToSplit, separator string) []interface{} { } return splitInterface } + +// NormalizeName Normalize names to remove weird characters and account to capitalization +func NormalizeName(s string) string { + c := cases.Fold() + return c.String(norm.NFD.String(s)) +} +// ReverseBytesInPlace reverse the bytes. thanks, Satoshi 😒 +func ReverseBytesInPlace(s []byte) { + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + s[i], s[j] = s[j], s[i] + } +} + +// TxIdToTxHash convert the txid to a hash for returning from the hub +func TxIdToTxHash(txid string) []byte { + t, err := hex.DecodeString(txid) + if err != nil { + return nil + } + + ReverseBytesInPlace(t) + + return t +} + +// TxHashToTxId convert the txHash from the response format back to an id +func TxHashToTxId(txHash []byte) string { + t := make([]byte, len(txHash)) + copy(t, txHash) + + ReverseBytesInPlace(t) + + return hex.EncodeToString(t) + +}