Skip to content

Commit

Permalink
address comments from the PR #45
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Sep 15, 2014
1 parent 025ce30 commit 3d8b1c2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 19 deletions.
3 changes: 3 additions & 0 deletions crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type PubKey interface {

// Bytes returns a serialized, storeable representation of this key
Bytes() ([]byte, error)

// Hash returns the hash of this public key (for creating peer ID's)
Hash() ([]byte, error)
}

// Given a public key, generates the shared key.
Expand Down
9 changes: 9 additions & 0 deletions crypto/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
u "github.com/jbenet/go-ipfs/util"
)

type RsaPrivateKey struct {
Expand Down Expand Up @@ -41,6 +42,14 @@ func (pk *RsaPublicKey) Bytes() ([]byte, error) {
return proto.Marshal(pbmes)
}

func (pk *RsaPublicKey) Hash() ([]byte, error) {
pkb, err := pk.Bytes()
if err != nil {
return nil, err
}
return u.Hash(pkb)
}

func (sk *RsaPrivateKey) GenSecret() []byte {
buf := make([]byte, 16)
rand.Read(buf)
Expand Down
4 changes: 4 additions & 0 deletions namesys/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type DNSResolver struct {
// cache would need a timeout
}

func (r *DNSResolver) Matches(name string) bool {
return strings.Contains(name, ".")
}

func (r *DNSResolver) Resolve(name string) (string, error) {
txt, err := net.LookupTXT(name)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions namesys/nsresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package namesys

type Resolver interface {
Resolve(string) (string, error)
Matches(string) bool
}
5 changes: 5 additions & 0 deletions namesys/proquint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ var _ = proquint.Encode

type ProquintResolver struct{}

func (r *ProquintResolver) Matches(name string) bool {
ok, err := proquint.IsProquint(name)
return err == nil && ok
}

func (r *ProquintResolver) Resolve(name string) (string, error) {
ok, err := proquint.IsProquint(name)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions namesys/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *IpnsPublisher) Publish(k ci.PrivKey, value u.Key) error {
}
namekey := u.Key(nameb).Pretty()

ipnskey, err := u.Hash([]byte("ipns:" + namekey))
ipnskey, err := u.Hash([]byte("/ipns/" + namekey))
if err != nil {
return err
}
Expand All @@ -43,7 +43,7 @@ func (p *IpnsPublisher) Publish(k ci.PrivKey, value u.Key) error {
return err
}

// Store ipns entry at h("ipns:"+b58(h(pubkey)))
// Store ipns entry at h("/ipns/"+b58(h(pubkey)))
err = p.routing.PutValue(u.Key(ipnskey), data)
if err != nil {
return err
Expand Down
34 changes: 21 additions & 13 deletions namesys/resolver.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
package namesys

import (
"strings"
"errors"

mdag "github.com/jbenet/go-ipfs/merkledag"
"github.com/jbenet/go-ipfs/routing"
)

var ErrCouldntResolve = errors.New("could not resolve name.")

type MasterResolver struct {
dns *DNSResolver
routing *RoutingResolver
pro *ProquintResolver
res []Resolver
}

func NewMasterResolver(r routing.IpfsRouting, dag *mdag.DAGService) *MasterResolver {
mr := new(MasterResolver)
mr.dns = new(DNSResolver)
mr.pro = new(ProquintResolver)
mr.routing = NewRoutingResolver(r, dag)
mr.res = []Resolver{
new(DNSResolver),
new(ProquintResolver),
NewRoutingResolver(r, dag),
}
return mr
}

func (mr *MasterResolver) Resolve(name string) (string, error) {
if strings.Contains(name, ".") {
return mr.dns.Resolve(name)
for _, r := range mr.res {
if r.Matches(name) {
return r.Resolve(name)
}
}
return "", ErrCouldntResolve
}

if strings.Contains(name, "-") {
return mr.pro.Resolve(name)
func (mr *MasterResolver) Matches(name string) bool {
for _, r := range mr.res {
if r.Matches(name) {
return true
}
}

return mr.routing.Resolve(name)
return false
}
13 changes: 9 additions & 4 deletions namesys/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func NewRoutingResolver(route routing.IpfsRouting, dagservice *mdag.DAGService)
}
}

func (r *RoutingResolver) Matches(name string) bool {
_, err := mh.FromB58String(name)
return err == nil
}

func (r *RoutingResolver) Resolve(name string) (string, error) {
hash, err := mh.FromB58String(name)
if err != nil {
Expand All @@ -36,13 +41,13 @@ func (r *RoutingResolver) Resolve(name string) (string, error) {

// use the routing system to get the name.
// /ipns/<name>
h, err := u.Hash([]byte("ipns:" + name))
h, err := u.Hash([]byte("/ipns/" + name))
if err != nil {
return "", err
}

inpsKey := u.Key(h)
val, err := r.routing.GetValue(inpsKey, time.Second*10)
ipnsKey := u.Key(h)
val, err := r.routing.GetValue(ipnsKey, time.Second*10)
if err != nil {
u.DOut("RoutingResolve get failed.\n")
return "", err
Expand Down Expand Up @@ -70,7 +75,7 @@ func (r *RoutingResolver) Resolve(name string) (string, error) {
}

// check sig with pk
if ok, err := pk.Verify(entry.GetValue(), entry.GetSignature()); err != nil && ok {
if ok, err := pk.Verify(entry.GetValue(), entry.GetSignature()); err != nil || !ok {
return "", fmt.Errorf("Invalid value. Not signed by PrivateKey corresponding to %v", pk)
}

Expand Down

0 comments on commit 3d8b1c2

Please sign in to comment.