feat: add EncodeString function

This commit is contained in:
Derrick Hammer 2023-05-10 14:17:50 -04:00
parent 8f3af2084c
commit 488f8737c0
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 10 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package cid
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/hex"
"errors" "errors"
"github.com/multiformats/go-multibase" "github.com/multiformats/go-multibase"
) )
@ -31,6 +32,15 @@ func EncodeFixed(hash [32]byte, size uint64) (string, error) {
return multibase.Encode(multibase.Base58BTC, prefixedHash) 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) { func Valid(cid string) (bool, error) {
_, err := maybeDecode(cid) _, err := maybeDecode(cid)
if err != nil { if err != nil {