kubo/fuse/ipns/common.go
Hector Sanjuan 3db9551f79 Extract the namesys and the keystore submodules
Namesys is a very useful submodule. Given a ValueStore and a Datastore it can
resolve and publish /ipns/ paths.

This functionality does not need to be sequestered inside go-ipfs as it can
and should be used without IPFS, for example, for implementing lightweight
IPNS publishing services or for resolving /ipns/ paths.

"keystore" extraction was necessary, as there is a dependency to it in
namesys. Keystore is also a useful module by itself within the stack.

Fixes #6537
2021-03-12 14:09:36 -05:00

35 lines
738 B
Go

package ipns
import (
"context"
"github.com/ipfs/go-ipfs/core"
nsys "github.com/ipfs/go-namesys"
path "github.com/ipfs/go-path"
ft "github.com/ipfs/go-unixfs"
ci "github.com/libp2p/go-libp2p-core/crypto"
)
// InitializeKeyspace sets the ipns record for the given key to
// point to an empty directory.
func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {
ctx, cancel := context.WithCancel(n.Context())
defer cancel()
emptyDir := ft.EmptyDirNode()
err := n.Pinning.Pin(ctx, emptyDir, false)
if err != nil {
return err
}
err = n.Pinning.Flush(ctx)
if err != nil {
return err
}
pub := nsys.NewIpnsPublisher(n.Routing, n.Repo.Datastore())
return pub.Publish(ctx, key, path.FromCid(emptyDir.Cid()))
}