routing.go 3.12 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
	key "github.com/ipfs/go-ipfs/blocks/key"
11
	pb "github.com/ipfs/go-ipfs/namesys/pb"
12
	path "github.com/ipfs/go-ipfs/path"
13
	routing "github.com/ipfs/go-ipfs/routing"
Jeromy's avatar
Jeromy committed
14
	logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
15 16
)

Jeromy's avatar
Jeromy committed
17
var log = logging.Logger("namesys")
18

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

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

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

34 35 36 37 38 39 40
// 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
41 42
}

43
// Resolve implements Resolver.
44
func (r *routingResolver) Resolve(ctx context.Context, name string) (path.Path, error) {
45 46 47 48 49 50 51 52 53 54 55
	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) {
56
	log.Debugf("RoutingResolve: '%s'", name)
57 58
	hash, err := mh.FromB58String(name)
	if err != nil {
59
		log.Warning("RoutingResolve: bad input hash: [%s]\n", name)
60 61 62 63 64 65
		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
66
	h := []byte("/ipns/" + string(hash))
67

68
	ipnsKey := key.Key(h)
69
	val, err := r.routing.GetValue(ctx, ipnsKey)
70
	if err != nil {
71
		log.Warning("RoutingResolve get failed.")
72 73 74
		return "", err
	}

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

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

	hsh, _ := pubkey.Hash()
88
	log.Debugf("pk hash = %s", key.Key(hsh))
89 90

	// check sig with pk
91 92
	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)
93 94 95
	}

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

	// 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")
105
		return path.FromKey(key.Key(valh)), nil
106
	}
107
}