generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 79
/
key.go
51 lines (45 loc) · 1.6 KB
/
key.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Package dshelp provides utilities for parsing and creating
// datastore keys used by go-ipfs
package dshelp
import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/multiformats/go-base32"
mh "github.com/multiformats/go-multihash"
)
// NewKeyFromBinary creates a new key from a byte slice.
func NewKeyFromBinary(rawKey []byte) datastore.Key {
buf := make([]byte, 1+base32.RawStdEncoding.EncodedLen(len(rawKey)))
buf[0] = '/'
base32.RawStdEncoding.Encode(buf[1:], rawKey)
return datastore.RawKey(string(buf))
}
// BinaryFromDsKey returns the byte slice corresponding to the given Key.
func BinaryFromDsKey(k datastore.Key) ([]byte, error) {
return base32.RawStdEncoding.DecodeString(k.String()[1:])
}
// MultihashToDsKey creates a Key from the given Multihash.
// If working with Cids, you can call cid.Hash() to obtain
// the multihash. Note that different CIDs might represent
// the same multihash.
func MultihashToDsKey(k mh.Multihash) datastore.Key {
return NewKeyFromBinary(k)
}
// DsKeyToMultihash converts a dsKey to the corresponding Multihash.
func DsKeyToMultihash(dsKey datastore.Key) (mh.Multihash, error) {
kb, err := BinaryFromDsKey(dsKey)
if err != nil {
return nil, err
}
return mh.Cast(kb)
}
// DsKeyToCidV1Raw converts the given Key (which should be a raw multihash
// key) to a Cid V1 of the given type (see
// https://godoc.org/github.com/ipfs/go-cid#pkg-constants).
func DsKeyToCidV1(dsKey datastore.Key, codecType uint64) (cid.Cid, error) {
hash, err := DsKeyToMultihash(dsKey)
if err != nil {
return cid.Cid{}, err
}
return cid.NewCidV1(codecType, hash), nil
}