From c01452e5ea6b03ebeffe76246980d697bb973db7 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sat, 17 Jan 2015 02:50:10 +0000 Subject: [PATCH] move dht record code into new package --- routing/dht/dht.go | 3 ++- routing/dht/ext_test.go | 3 ++- routing/dht/records.go | 36 ++------------------------------ routing/dht/routing.go | 3 ++- routing/offline/offline.go | 4 ++-- routing/record/record.go | 42 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 routing/record/record.go diff --git a/routing/dht/dht.go b/routing/dht/dht.go index 344092057..25be47fac 100644 --- a/routing/dht/dht.go +++ b/routing/dht/dht.go @@ -17,6 +17,7 @@ import ( routing "github.com/jbenet/go-ipfs/routing" pb "github.com/jbenet/go-ipfs/routing/dht/pb" kb "github.com/jbenet/go-ipfs/routing/kbucket" + record "github.com/jbenet/go-ipfs/routing/record" "github.com/jbenet/go-ipfs/thirdparty/eventlog" u "github.com/jbenet/go-ipfs/util" @@ -253,7 +254,7 @@ func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error { return err } - rec, err := MakePutRecord(sk, key, value) + rec, err := record.MakePutRecord(sk, key, value) if err != nil { return err } diff --git a/routing/dht/ext_test.go b/routing/dht/ext_test.go index 808e27b10..e151af5d2 100644 --- a/routing/dht/ext_test.go +++ b/routing/dht/ext_test.go @@ -11,6 +11,7 @@ import ( peer "github.com/jbenet/go-ipfs/p2p/peer" routing "github.com/jbenet/go-ipfs/routing" pb "github.com/jbenet/go-ipfs/routing/dht/pb" + record "github.com/jbenet/go-ipfs/routing/record" u "github.com/jbenet/go-ipfs/util" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" @@ -104,7 +105,7 @@ func TestGetFailures(t *testing.T) { t.Fatal(err) } - rec, err := MakePutRecord(sk, u.Key(str), []byte("blah")) + rec, err := record.MakePutRecord(sk, u.Key(str), []byte("blah")) if err != nil { t.Fatal(err) } diff --git a/routing/dht/records.go b/routing/dht/records.go index 2dc9644cf..71511b978 100644 --- a/routing/dht/records.go +++ b/routing/dht/records.go @@ -7,11 +7,11 @@ import ( "strings" "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" ci "github.com/jbenet/go-ipfs/p2p/crypto" "github.com/jbenet/go-ipfs/p2p/peer" pb "github.com/jbenet/go-ipfs/routing/dht/pb" + record "github.com/jbenet/go-ipfs/routing/record" u "github.com/jbenet/go-ipfs/util" ctxutil "github.com/jbenet/go-ipfs/util/ctx" ) @@ -34,38 +34,6 @@ func KeyForPublicKey(id peer.ID) u.Key { return u.Key("/pk/" + string(id)) } -// RecordBlobForSig returns the blob protected by the record signature -func RecordBlobForSig(r *pb.Record) []byte { - k := []byte(r.GetKey()) - v := []byte(r.GetValue()) - a := []byte(r.GetAuthor()) - return bytes.Join([][]byte{k, v, a}, []byte{}) -} - -// MakePutRecord creates and signs a dht record for the given key/value pair -func MakePutRecord(sk ci.PrivKey, key u.Key, value []byte) (*pb.Record, error) { - record := new(pb.Record) - - record.Key = proto.String(string(key)) - record.Value = value - - pkh, err := sk.GetPublic().Hash() - if err != nil { - return nil, err - } - - record.Author = proto.String(string(pkh)) - blob := RecordBlobForSig(record) - - sig, err := sk.Sign(blob) - if err != nil { - return nil, err - } - - record.Signature = sig - return record, nil -} - func (dht *IpfsDHT) getPublicKeyOnline(ctx context.Context, p peer.ID) (ci.PubKey, error) { log.Debugf("getPublicKey for: %s", p) @@ -179,7 +147,7 @@ func (dht *IpfsDHT) verifyRecordOnline(ctx context.Context, r *pb.Record) error // it might be useful for users to have access to. func (dht *IpfsDHT) verifyRecord(r *pb.Record, pk ci.PubKey) error { // First, validate the signature - blob := RecordBlobForSig(r) + blob := record.RecordBlobForSig(r) ok, err := pk.Verify(blob, r.GetSignature()) if err != nil { log.Error("Signature verify failed.") diff --git a/routing/dht/routing.go b/routing/dht/routing.go index aea4406f1..35d804650 100644 --- a/routing/dht/routing.go +++ b/routing/dht/routing.go @@ -12,6 +12,7 @@ import ( "github.com/jbenet/go-ipfs/routing" pb "github.com/jbenet/go-ipfs/routing/dht/pb" kb "github.com/jbenet/go-ipfs/routing/kbucket" + record "github.com/jbenet/go-ipfs/routing/record" u "github.com/jbenet/go-ipfs/util" errors "github.com/jbenet/go-ipfs/util/debugerror" pset "github.com/jbenet/go-ipfs/util/peerset" @@ -41,7 +42,7 @@ func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte) error return err } - rec, err := MakePutRecord(sk, key, value) + rec, err := record.MakePutRecord(sk, key, value) if err != nil { log.Error("Creation of record failed!") return err diff --git a/routing/offline/offline.go b/routing/offline/offline.go index 41baf50d2..63fb14441 100644 --- a/routing/offline/offline.go +++ b/routing/offline/offline.go @@ -10,8 +10,8 @@ import ( ci "github.com/jbenet/go-ipfs/p2p/crypto" "github.com/jbenet/go-ipfs/p2p/peer" routing "github.com/jbenet/go-ipfs/routing" - dht "github.com/jbenet/go-ipfs/routing/dht" pb "github.com/jbenet/go-ipfs/routing/dht/pb" + record "github.com/jbenet/go-ipfs/routing/record" eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog" u "github.com/jbenet/go-ipfs/util" ) @@ -36,7 +36,7 @@ type offlineRouting struct { } func (c *offlineRouting) PutValue(ctx context.Context, key u.Key, val []byte) error { - rec, err := dht.MakePutRecord(c.sk, key, val) + rec, err := record.MakePutRecord(c.sk, key, val) if err != nil { return err } diff --git a/routing/record/record.go b/routing/record/record.go new file mode 100644 index 000000000..602159694 --- /dev/null +++ b/routing/record/record.go @@ -0,0 +1,42 @@ +package record + +import ( + "bytes" + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" + + ci "github.com/jbenet/go-ipfs/p2p/crypto" + pb "github.com/jbenet/go-ipfs/routing/dht/pb" + u "github.com/jbenet/go-ipfs/util" +) + +// MakePutRecord creates and signs a dht record for the given key/value pair +func MakePutRecord(sk ci.PrivKey, key u.Key, value []byte) (*pb.Record, error) { + record := new(pb.Record) + + record.Key = proto.String(string(key)) + record.Value = value + + pkh, err := sk.GetPublic().Hash() + if err != nil { + return nil, err + } + + record.Author = proto.String(string(pkh)) + blob := RecordBlobForSig(record) + + sig, err := sk.Sign(blob) + if err != nil { + return nil, err + } + + record.Signature = sig + return record, nil +} + +// RecordBlobForSig returns the blob protected by the record signature +func RecordBlobForSig(r *pb.Record) []byte { + k := []byte(r.GetKey()) + v := []byte(r.GetValue()) + a := []byte(r.GetAuthor()) + return bytes.Join([][]byte{k, v, a}, []byte{}) +}