Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
36 lines (31 sloc)
1.02 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Package dshelp provides utilities for parsing and creating | |
| // datastore keys used by go-ipfs | |
| package dshelp | |
| import ( | |
| cid "github.com/ipfs/go-cid" | |
| "github.com/ipfs/go-datastore" | |
| "github.com/multiformats/go-base32" | |
| ) | |
| // 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:]) | |
| } | |
| // CidToDsKey creates a Key from the given Cid. | |
| func CidToDsKey(k cid.Cid) datastore.Key { | |
| return NewKeyFromBinary(k.Bytes()) | |
| } | |
| // DsKeyToCid converts the given Key to its corresponding Cid. | |
| func DsKeyToCid(dsKey datastore.Key) (cid.Cid, error) { | |
| kb, err := BinaryFromDsKey(dsKey) | |
| if err != nil { | |
| return cid.Cid{}, err | |
| } | |
| return cid.Cast(kb) | |
| } |