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

import (
	"fmt"

6 7 8 9 10 11
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
	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"
	pb "github.com/ipfs/go-ipfs/namesys/internal/pb"
	routing "github.com/ipfs/go-ipfs/routing"
	u "github.com/ipfs/go-ipfs/util"
12 13
)

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32
// CanResolve implements Resolver. Checks whether name is a b58 encoded string.
func (r *routingResolver) CanResolve(name string) bool {
Jeromy's avatar
Jeromy committed
33 34 35 36
	_, err := mh.FromB58String(name)
	return err == nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38
// Resolve implements Resolver. Uses the IPFS routing system to resolve SFS-like
// names.
39
func (r *routingResolver) Resolve(ctx context.Context, name string) (u.Key, error) {
40
	log.Debugf("RoutingResolve: '%s'", name)
41 42
	hash, err := mh.FromB58String(name)
	if err != nil {
43
		log.Warning("RoutingResolve: bad input hash: [%s]\n", name)
44 45 46 47 48 49
		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
50
	h := []byte("/ipns/" + string(hash))
51

Jeromy's avatar
Jeromy committed
52
	ipnsKey := u.Key(h)
53
	val, err := r.routing.GetValue(ctx, ipnsKey)
54
	if err != nil {
55
		log.Warning("RoutingResolve get failed.")
56 57 58
		return "", err
	}

59
	entry := new(pb.IpnsEntry)
60 61 62 63 64 65
	err = proto.Unmarshal(val, entry)
	if err != nil {
		return "", err
	}

	// name should be a public key retrievable from ipfs
Jeromy's avatar
Jeromy committed
66 67 68
	pubkey, err := routing.GetPublicKey(r.routing, ctx, hash)
	if err != nil {
		return "", err
69
	}
70 71

	hsh, _ := pubkey.Hash()
Jeromy's avatar
Jeromy committed
72
	log.Debugf("pk hash = %s", u.Key(hsh))
73 74

	// check sig with pk
75 76
	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)
77 78 79
	}

	// ok sig checks out. this is a valid name.
80
	return u.Key(entry.GetValue()), nil
81
}