namesys.go 1.44 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package namesys

import (
4
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	ci "github.com/jbenet/go-ipfs/p2p/crypto"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	routing "github.com/jbenet/go-ipfs/routing"
7
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
)

// ipnsNameSystem implements IPNS naming.
//
// Uses three Resolvers:
// (a) ipfs routing naming: SFS-like PKI names.
// (b) dns domains: resolves using links in DNS TXT records
// (c) proquints: interprets string as the raw byte data.
//
// It can only publish to: (a) ipfs routing naming.
//
type ipns struct {
	resolvers []Resolver
	publisher Publisher
}

// NewNameSystem will construct the IPFS naming system based on Routing
func NewNameSystem(r routing.IpfsRouting) NameSystem {
	return &ipns{
		resolvers: []Resolver{
			new(DNSResolver),
			new(ProquintResolver),
			NewRoutingResolver(r),
		},
		publisher: NewRoutingPublisher(r),
	}
}

// Resolve implements Resolver
37
func (ns *ipns) Resolve(ctx context.Context, name string) (u.Key, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39
	for _, r := range ns.resolvers {
		if r.CanResolve(name) {
40
			return r.Resolve(ctx, name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		}
	}
	return "", ErrResolveFailed
}

// CanResolve implements Resolver
func (ns *ipns) CanResolve(name string) bool {
	for _, r := range ns.resolvers {
		if r.CanResolve(name) {
			return true
		}
	}
	return false
}

// Publish implements Publisher
57 58
func (ns *ipns) Publish(ctx context.Context, name ci.PrivKey, value u.Key) error {
	return ns.publisher.Publish(ctx, name, value)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
}