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

import (
	"fmt"

6 7 8 9
	"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"
10
	path "github.com/ipfs/go-ipfs/path"
11 12
	routing "github.com/ipfs/go-ipfs/routing"
	u "github.com/ipfs/go-ipfs/util"
13 14
)

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
// NewRoutingResolver constructs a name resolver using the IPFS Routing system
// to implement SFS-like naming on top.
func NewRoutingResolver(route routing.IpfsRouting) Resolver {
25 26 27 28
	if route == nil {
		panic("attempt to create resolver with nil routing system")
	}

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

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

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

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

60
	entry := new(pb.IpnsEntry)
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
Jeromy's avatar
Jeromy committed
67 68 69
	pubkey, err := routing.GetPublicKey(r.routing, ctx, hash)
	if err != nil {
		return "", err
70
	}
71 72

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

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

	// ok sig checks out. this is a valid name.
81 82 83 84 85 86 87 88 89 90 91

	// 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")
		return path.FromKey(u.Key(valh)), nil
	}
92
}