routing.go 2.3 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 9 10 11 12

	ci "github.com/jbenet/go-ipfs/crypto"
	mdag "github.com/jbenet/go-ipfs/merkledag"
	"github.com/jbenet/go-ipfs/routing"
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
15 16
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
var log = u.Logger("namesys", logging.ERROR)
18

19 20 21
// RoutingResolver implements NSResolver for the main IPFS SFS-like naming
type RoutingResolver struct {
	routing routing.IpfsRouting
Jeromy's avatar
Jeromy committed
22 23 24 25 26 27 28 29
	dag     *mdag.DAGService
}

func NewRoutingResolver(route routing.IpfsRouting, dagservice *mdag.DAGService) *RoutingResolver {
	return &RoutingResolver{
		routing: route,
		dag:     dagservice,
	}
30 31
}

Jeromy's avatar
Jeromy committed
32 33 34 35 36
func (r *RoutingResolver) Matches(name string) bool {
	_, err := mh.FromB58String(name)
	return err == nil
}

37
func (r *RoutingResolver) Resolve(name string) (string, error) {
38
	log.Debug("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>
Jeromy's avatar
Jeromy committed
49
	h, err := u.Hash([]byte("/ipns/" + name))
50 51 52 53
	if err != nil {
		return "", err
	}

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

Jeromy's avatar
Jeromy committed
61
	entry := new(IpnsEntry)
62 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>
	key := u.Key(hash)
70
	pkval, err := r.routing.GetValue(ctx, key)
71
	if err != nil {
72
		log.Warning("RoutingResolve PubKey Get failed.")
73 74 75 76
		return "", err
	}

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

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