mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
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
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package coreapi
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
gopath "path"
|
|
|
|
"github.com/ipfs/go-namesys/resolve"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
ipld "github.com/ipfs/go-ipld-format"
|
|
ipfspath "github.com/ipfs/go-path"
|
|
"github.com/ipfs/go-path/resolver"
|
|
uio "github.com/ipfs/go-unixfs/io"
|
|
coreiface "github.com/ipfs/interface-go-ipfs-core"
|
|
path "github.com/ipfs/interface-go-ipfs-core/path"
|
|
)
|
|
|
|
// ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
|
|
// resolved Node.
|
|
func (api *CoreAPI) ResolveNode(ctx context.Context, p path.Path) (ipld.Node, error) {
|
|
rp, err := api.ResolvePath(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
node, err := api.dag.Get(ctx, rp.Cid())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return node, nil
|
|
}
|
|
|
|
// ResolvePath resolves the path `p` using Unixfs resolver, returns the
|
|
// resolved path.
|
|
func (api *CoreAPI) ResolvePath(ctx context.Context, p path.Path) (path.Resolved, error) {
|
|
if _, ok := p.(path.Resolved); ok {
|
|
return p.(path.Resolved), nil
|
|
}
|
|
if err := p.IsValid(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ipath := ipfspath.Path(p.String())
|
|
ipath, err := resolve.ResolveIPNS(ctx, api.namesys, ipath)
|
|
if err == resolve.ErrNoNamesys {
|
|
return nil, coreiface.ErrOffline
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var resolveOnce resolver.ResolveOnce
|
|
|
|
switch ipath.Segments()[0] {
|
|
case "ipfs":
|
|
resolveOnce = uio.ResolveUnixfsOnce
|
|
case "ipld":
|
|
resolveOnce = resolver.ResolveSingle
|
|
default:
|
|
return nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace())
|
|
}
|
|
|
|
r := &resolver.Resolver{
|
|
DAG: api.dag,
|
|
ResolveOnce: resolveOnce,
|
|
}
|
|
|
|
node, rest, err := r.ResolveToLastNode(ctx, ipath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
root, err := cid.Parse(ipath.Segments()[1])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return path.NewResolvedPath(ipath, node, root, gopath.Join(rest...)), nil
|
|
}
|