From 488f8737c09b7757c5649b3d8a3568e3c1d5fe45 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 10 May 2023 14:17:50 -0400 Subject: [PATCH] feat: add EncodeString function --- cid/cid.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cid/cid.go b/cid/cid.go index 111b7a9..a675328 100644 --- a/cid/cid.go +++ b/cid/cid.go @@ -3,6 +3,7 @@ package cid import ( "bytes" "encoding/binary" + "encoding/hex" "errors" "github.com/multiformats/go-multibase" ) @@ -31,6 +32,15 @@ func EncodeFixed(hash [32]byte, size uint64) (string, error) { return multibase.Encode(multibase.Base58BTC, prefixedHash) } +func EncodeString(hash string, size uint64) (string, error) { + hashBytes, err := hex.DecodeString(hash) + if err != nil { + return "", err + } + + return Encode(hashBytes, size) +} + func Valid(cid string) (bool, error) { _, err := maybeDecode(cid) if err != nil {