routing.go 2.56 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
	pb "github.com/jbenet/go-ipfs/namesys/internal/pb"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	ci "github.com/jbenet/go-ipfs/p2p/crypto"
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
// 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
}

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

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

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

62
	entry := new(pb.IpnsEntry)
63 64 65 66 67 68 69
	err = proto.Unmarshal(val, entry)
	if err != nil {
		return "", err
	}

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

	// get PublicKey from node.Data
Jeromy's avatar
Jeromy committed
78
	pk, err := ci.UnmarshalPublicKey(pkval)
79 80 81
	if err != nil {
		return "", err
	}
Jeromy's avatar
Jeromy committed
82 83
	hsh, _ := pk.Hash()
	log.Debugf("pk hash = %s", u.Key(hsh))
84 85

	// check sig with pk
Jeromy's avatar
Jeromy committed
86
	if ok, err := pk.Verify(ipnsEntryDataForSig(entry), entry.GetSignature()); err != nil || !ok {
87 88 89 90 91 92
		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
}