From 931d786c52033a29aee01062b1a84116965867e9 Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Thu, 17 Jun 2021 23:59:06 -0400 Subject: [PATCH 1/2] Add utility functions from hub --- extras/util/strings.go | 47 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/extras/util/strings.go b/extras/util/strings.go index 46c73c4..9976e24 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,43 @@ func StringSplitArg(stringToSplit, separator string) []interface{} { } return splitInterface } + +func Normalize(s string) string { + c := cases.Fold() + return c.String(norm.NFD.String(s)) +} + +func ReverseBytes(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] + } +} + +// convert txid to txHash +func ToHash(txid string) []byte { + t, err := hex.DecodeString(txid) + if err != nil { + return nil + } + + // reverse the bytes. thanks, Satoshi 😒 + for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 { + t[i], t[j] = t[j], t[i] + } + + return t +} + +// convert txHash to txid +func FromHash(txHash []byte) string { + t := make([]byte, len(txHash)) + copy(t, txHash) + + // reverse the bytes. thanks, Satoshi 😒 + for i, j := 0, len(txHash)-1; i < j; i, j = i+1, j-1 { + txHash[i], txHash[j] = txHash[j], txHash[i] + } + + return hex.EncodeToString(t) + +} -- 2.45.2 From 87bf89a109b48a379b16e4cc3fa744c337b7817b Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Wed, 23 Jun 2021 15:29:23 -0400 Subject: [PATCH 2/2] Cleanup utility functions and add comments --- extras/util/strings.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/extras/util/strings.go b/extras/util/strings.go index 9976e24..9000b29 100644 --- a/extras/util/strings.go +++ b/extras/util/strings.go @@ -16,41 +16,36 @@ func StringSplitArg(stringToSplit, separator string) []interface{} { return splitInterface } -func Normalize(s string) string { +// 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)) } - -func ReverseBytes(s []byte) { +// 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] } } -// convert txid to txHash -func ToHash(txid string) []byte { +// 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 } - // reverse the bytes. thanks, Satoshi 😒 - for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 { - t[i], t[j] = t[j], t[i] - } + ReverseBytesInPlace(t) return t } -// convert txHash to txid -func FromHash(txHash []byte) string { +// 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) - // reverse the bytes. thanks, Satoshi 😒 - for i, j := 0, len(txHash)-1; i < j; i, j = i+1, j-1 { - txHash[i], txHash[j] = txHash[j], txHash[i] - } + ReverseBytesInPlace(t) return hex.EncodeToString(t) -- 2.45.2