From 7f502187e61bcf121e5d5ab705de81de804ffabc Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 3 Jan 2024 03:47:16 -0500 Subject: [PATCH] feat: base helpers --- internal/bases/bases.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 internal/bases/bases.go diff --git a/internal/bases/bases.go b/internal/bases/bases.go new file mode 100644 index 0000000..9a6aa47 --- /dev/null +++ b/internal/bases/bases.go @@ -0,0 +1,30 @@ +package bases + +import "github.com/multiformats/go-multibase" + +func ToBase64Url(data []byte) (string, error) { + return ToBase(data, "base64url") +} + +func ToBase58BTC(data []byte) (string, error) { + return ToBase(data, "base58btc") +} + +func ToBase32(data []byte) (string, error) { + return ToBase(data, "base32") +} + +func ToHex(data []byte) (string, error) { + return ToBase(data, "base16") +} + +func ToBase(data []byte, base string) (string, error) { + baseEncoder, _ := multibase.EncoderByName(base) + + ret, err := multibase.Encode(baseEncoder.Encoding(), data) + if err != nil { + return "", err + } + + return ret, nil +}