routing.go 2.33 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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	routing "github.com/jbenet/go-ipfs/routing"
12 13 14
	u "github.com/jbenet/go-ipfs/util"
)

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 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 {
	return &routingResolver{routing: route}
26 27
}

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

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

	// use the routing system to get the name.
	// /ipns/<name>
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
	h := u.Hash([]byte("/ipns/" + name))
49

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

Jeromy's avatar
Jeromy committed
57
	entry := new(IpnsEntry)
58 59 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
	// /ipfs/<name>
	key := u.Key(hash)
66
	pkval, err := r.routing.GetValue(ctx, key)
67
	if err != nil {
68
		log.Warning("RoutingResolve PubKey Get failed.")
69 70 71 72
		return "", err
	}

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

	// check sig with pk
Jeromy's avatar
Jeromy committed
79
	if ok, err := pk.Verify(entry.GetValue(), entry.GetSignature()); err != nil || !ok {
80 81 82 83 84 85
		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
}