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

import (
	"fmt"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
8

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
	ci "github.com/jbenet/go-ipfs/crypto"
11
	pb "github.com/jbenet/go-ipfs/namesys/internal/pb"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
	routing "github.com/jbenet/go-ipfs/routing"
13 14 15
	u "github.com/jbenet/go-ipfs/util"
)

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 26
// NewRoutingResolver constructs a name resolver using the IPFS Routing system
// to implement SFS-like naming on top.
func NewRoutingResolver(route routing.IpfsRouting) Resolver {
	return &routingResolver{routing: route}
27 28
}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36 37
// Resolve implements Resolver. Uses the IPFS routing system to resolve SFS-like
// names.
func (r *routingResolver) Resolve(name string) (string, error) {
38
	log.Debugf("RoutingResolve: '%s'", name)
39
	ctx := context.TODO()
40 41
	hash, err := mh.FromB58String(name)
	if err != nil {
42
		log.Warning("RoutingResolve: bad input hash: [%s]\n", name)
43 44 45 46 47 48
		return "", err
	}
	// name should be a multihash. if it isn't, error out here.

	// use the routing system to get the name.
	// /ipns/<name>
49
	h := []byte("/ipns/" + name)
50

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

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

	// name should be a public key retrievable from ipfs
	// /ipfs/<name>
	key := u.Key(hash)
67
	pkval, err := r.routing.GetValue(ctx, key)
68
	if err != nil {
69
		log.Warning("RoutingResolve PubKey Get failed.")
70 71 72 73
		return "", err
	}

	// get PublicKey from node.Data
Jeromy's avatar
Jeromy committed
74
	pk, err := ci.UnmarshalPublicKey(pkval)
75 76 77 78 79
	if err != nil {
		return "", err
	}

	// check sig with pk
Jeromy's avatar
Jeromy committed
80
	if ok, err := pk.Verify(entry.GetValue(), entry.GetSignature()); err != nil || !ok {
81 82 83 84 85 86
		return "", fmt.Errorf("Invalid value. Not signed by PrivateKey corresponding to %v", pk)
	}

	// ok sig checks out. this is a valid name.
	return string(entry.GetValue()), nil
}