dedupkey.go 609 Bytes
Newer Older
Hannah Howard's avatar
Hannah Howard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
package dedupkey

import (
	"github.com/ipfs/go-graphsync/ipldutil"
	basicnode "github.com/ipld/go-ipld-prime/node/basic"
)

// EncodeDedupKey returns encoded cbor data for string key
func EncodeDedupKey(key string) ([]byte, error) {
	nb := basicnode.Style.String.NewBuilder()
	err := nb.AssignString(key)
	if err != nil {
		return nil, err
	}
	nd := nb.Build()
	return ipldutil.EncodeNode(nd)
}

// DecodeDedupKey returns a string key decoded from cbor data
func DecodeDedupKey(data []byte) (string, error) {
	nd, err := ipldutil.DecodeNode(data)
	if err != nil {
		return "", err
	}
	return nd.AsString()
}