mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-06 16:58:11 +08:00
29 lines
743 B
Go
29 lines
743 B
Go
package dshelp
|
|
|
|
import (
|
|
cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
|
|
base32 "gx/ipfs/Qmb1DA2A9LS2wR4FFweB4uEDomFsdmnw1VLawLE1yQzudj/base32"
|
|
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
|
|
)
|
|
|
|
// TODO: put this code into the go-datastore itself
|
|
func NewKeyFromBinary(s string) ds.Key {
|
|
return ds.NewKey(base32.RawStdEncoding.EncodeToString([]byte(s)))
|
|
}
|
|
|
|
func BinaryFromDsKey(k ds.Key) ([]byte, error) {
|
|
return base32.RawStdEncoding.DecodeString(k.String()[1:])
|
|
}
|
|
|
|
func CidToDsKey(k *cid.Cid) ds.Key {
|
|
return NewKeyFromBinary(k.KeyString())
|
|
}
|
|
|
|
func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
|
|
kb, err := BinaryFromDsKey(dsKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cid.Cast(kb)
|
|
}
|