package core import ( "context" "errors" "strings" namesys "github.com/ipfs/go-ipfs/namesys" path "github.com/ipfs/go-ipfs/path" resolver "github.com/ipfs/go-ipfs/path/resolver" ipld "gx/ipfs/QmWi2BYBL5gJ3CiAiQchg6rn1A8iBsrWy51EYxvHVjFvLb/go-ipld-format" cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid" logging "gx/ipfs/Qmbi1CTJsbnBZjCEgc2otwu8cUFPsGpzWXG7edVCLZ7Gvk/go-log" ) // ErrNoNamesys is an explicit error for when an IPFS node doesn't // (yet) have a name system var ErrNoNamesys = errors.New( "core/resolve: no Namesys on IpfsNode - can't resolve ipns entry") // Resolve resolves the given path by parsing out protocol-specific // entries (e.g. /ipns/) and then going through the /ipfs/ // entries and returning the final node. func Resolve(ctx context.Context, nsys namesys.NameSystem, r *resolver.Resolver, p path.Path) (ipld.Node, error) { if strings.HasPrefix(p.String(), "/ipns/") { evt := log.EventBegin(ctx, "resolveIpnsPath") defer evt.Done() // resolve ipns paths // TODO(cryptix): we should be able to query the local cache for the path if nsys == nil { evt.Append(logging.LoggableMap{"error": ErrNoNamesys.Error()}) return nil, ErrNoNamesys } seg := p.Segments() if len(seg) < 2 || seg[1] == "" { // just "/" without further segments evt.Append(logging.LoggableMap{"error": path.ErrNoComponents.Error()}) return nil, path.ErrNoComponents } extensions := seg[2:] resolvable, err := path.FromSegments("/", seg[0], seg[1]) if err != nil { evt.Append(logging.LoggableMap{"error": err.Error()}) return nil, err } respath, err := nsys.Resolve(ctx, resolvable.String()) if err != nil { evt.Append(logging.LoggableMap{"error": err.Error()}) return nil, err } segments := append(respath.Segments(), extensions...) p, err = path.FromSegments("/", segments...) if err != nil { evt.Append(logging.LoggableMap{"error": err.Error()}) return nil, err } } // ok, we have an IPFS path now (or what we'll treat as one) return r.ResolvePath(ctx, p) } // ResolveToCid resolves a path to a cid. // // It first checks if the path is already in the form of just a cid ( or // /ipfs/) and returns immediately if so. Otherwise, it falls back onto // Resolve to perform resolution of the dagnode being referenced. func ResolveToCid(ctx context.Context, nsys namesys.NameSystem, r *resolver.Resolver, p path.Path) (*cid.Cid, error) { // If the path is simply a cid, parse and return it. Parsed paths are already // normalized (read: prepended with /ipfs/ if needed), so segment[1] should // always be the key. if p.IsJustAKey() { return cid.Decode(p.Segments()[1]) } // Fall back onto regular dagnode resolution. Retrieve the second-to-last // segment of the path and resolve its link to the last segment. head, tail, err := p.PopLastSegment() if err != nil { return nil, err } dagnode, err := Resolve(ctx, nsys, r, head) if err != nil { return nil, err } // Extract and return the cid of the link to the target dag node. link, _, err := dagnode.ResolveLink([]string{tail}) if err != nil { return nil, err } return link.Cid, nil }