routing.go 3.05 KB
Newer Older
1 2 3 4 5
package namesys

import (
	"fmt"

6
	proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
7 8
	mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9

10
	pb "github.com/ipfs/go-ipfs/namesys/internal/pb"
11
	path "github.com/ipfs/go-ipfs/path"
12 13
	routing "github.com/ipfs/go-ipfs/routing"
	u "github.com/ipfs/go-ipfs/util"
14 15
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
var log = u.Logger("namesys")
17

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19
// routingResolver implements NSResolver for the main IPFS SFS-like naming
type routingResolver struct {
20
	routing routing.IpfsRouting
Jeromy's avatar
Jeromy committed
21 22
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24 25
// NewRoutingResolver constructs a name resolver using the IPFS Routing system
// to implement SFS-like naming on top.
func NewRoutingResolver(route routing.IpfsRouting) Resolver {
26 27 28 29
	if route == nil {
		panic("attempt to create resolver with nil routing system")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
	return &routingResolver{routing: route}
31 32
}

33 34 35 36 37 38 39
// newRoutingResolver returns a resolver instead of a Resolver.
func newRoutingResolver(route routing.IpfsRouting) resolver {
	if route == nil {
		panic("attempt to create resolver with nil routing system")
	}

	return &routingResolver{routing: route}
Jeromy's avatar
Jeromy committed
40 41
}

42
// Resolve implements Resolver.
43
func (r *routingResolver) Resolve(ctx context.Context, name string) (path.Path, error) {
44 45 46 47 48 49 50 51 52 53 54
	return r.ResolveN(ctx, name, DefaultDepthLimit)
}

// ResolveN implements Resolver.
func (r *routingResolver) ResolveN(ctx context.Context, name string, depth int) (path.Path, error) {
	return resolve(ctx, r, name, depth, "/ipns/")
}

// resolveOnce implements resolver. Uses the IPFS routing system to
// resolve SFS-like names.
func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Path, error) {
55
	log.Debugf("RoutingResolve: '%s'", name)
56 57
	hash, err := mh.FromB58String(name)
	if err != nil {
58
		log.Warning("RoutingResolve: bad input hash: [%s]\n", name)
59 60 61 62 63 64
		return "", err
	}
	// name should be a multihash. if it isn't, error out here.

	// use the routing system to get the name.
	// /ipns/<name>
Jeromy's avatar
Jeromy committed
65
	h := []byte("/ipns/" + string(hash))
66

Jeromy's avatar
Jeromy committed
67
	ipnsKey := u.Key(h)
68
	val, err := r.routing.GetValue(ctx, ipnsKey)
69
	if err != nil {
70
		log.Warning("RoutingResolve get failed.")
71 72 73
		return "", err
	}

74
	entry := new(pb.IpnsEntry)
75 76 77 78 79 80
	err = proto.Unmarshal(val, entry)
	if err != nil {
		return "", err
	}

	// name should be a public key retrievable from ipfs
Jeromy's avatar
Jeromy committed
81 82 83
	pubkey, err := routing.GetPublicKey(r.routing, ctx, hash)
	if err != nil {
		return "", err
84
	}
85 86

	hsh, _ := pubkey.Hash()
Jeromy's avatar
Jeromy committed
87
	log.Debugf("pk hash = %s", u.Key(hsh))
88 89

	// check sig with pk
90 91
	if ok, err := pubkey.Verify(ipnsEntryDataForSig(entry), entry.GetSignature()); err != nil || !ok {
		return "", fmt.Errorf("Invalid value. Not signed by PrivateKey corresponding to %v", pubkey)
92 93 94
	}

	// ok sig checks out. this is a valid name.
95 96 97 98 99 100 101 102 103 104 105

	// check for old style record:
	valh, err := mh.Cast(entry.GetValue())
	if err != nil {
		// Not a multihash, probably a new record
		return path.ParsePath(string(entry.GetValue()))
	} else {
		// Its an old style multihash record
		log.Warning("Detected old style multihash record")
		return path.FromKey(u.Key(valh)), nil
	}
106
}