dedupkey.go 610 Bytes
Newer Older
Hannah Howard's avatar
Hannah Howard committed
1 2 3 4
package dedupkey

import (
	basicnode "github.com/ipld/go-ipld-prime/node/basic"
5 6

	"github.com/ipfs/go-graphsync/ipldutil"
Hannah Howard's avatar
Hannah Howard committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
)

// 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()
}