mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 10:57:42 +08:00
43 lines
1022 B
Go
43 lines
1022 B
Go
package ipns
|
|
|
|
import (
|
|
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
|
|
|
|
"github.com/ipfs/go-ipfs/core"
|
|
mdag "github.com/ipfs/go-ipfs/merkledag"
|
|
nsys "github.com/ipfs/go-ipfs/namesys"
|
|
path "github.com/ipfs/go-ipfs/path"
|
|
ft "github.com/ipfs/go-ipfs/unixfs"
|
|
ci "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/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 {
|
|
emptyDir := &mdag.Node{Data: ft.FolderPBData()}
|
|
nodek, err := n.DAG.Add(emptyDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(n.Context())
|
|
defer cancel()
|
|
|
|
err = n.Pinning.Pin(ctx, emptyDir, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = n.Pinning.Flush()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pub := nsys.NewRoutingPublisher(n.Routing, n.Repo.Datastore())
|
|
if err := pub.Publish(ctx, key, path.FromKey(nodek)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|