mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 14:34:24 +08:00
move dht record code into new package
This commit is contained in:
@ -17,6 +17,7 @@ import (
|
|||||||
routing "github.com/jbenet/go-ipfs/routing"
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
||||||
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
||||||
|
record "github.com/jbenet/go-ipfs/routing/record"
|
||||||
"github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
"github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
|
|
||||||
@ -253,7 +254,7 @@ func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rec, err := MakePutRecord(sk, key, value)
|
rec, err := record.MakePutRecord(sk, key, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
routing "github.com/jbenet/go-ipfs/routing"
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
||||||
|
record "github.com/jbenet/go-ipfs/routing/record"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
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)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,11 @@ import (
|
|||||||
"strings"
|
"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/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"
|
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
"github.com/jbenet/go-ipfs/p2p/peer"
|
"github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
||||||
|
record "github.com/jbenet/go-ipfs/routing/record"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
ctxutil "github.com/jbenet/go-ipfs/util/ctx"
|
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))
|
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) {
|
func (dht *IpfsDHT) getPublicKeyOnline(ctx context.Context, p peer.ID) (ci.PubKey, error) {
|
||||||
log.Debugf("getPublicKey for: %s", p)
|
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.
|
// it might be useful for users to have access to.
|
||||||
func (dht *IpfsDHT) verifyRecord(r *pb.Record, pk ci.PubKey) error {
|
func (dht *IpfsDHT) verifyRecord(r *pb.Record, pk ci.PubKey) error {
|
||||||
// First, validate the signature
|
// First, validate the signature
|
||||||
blob := RecordBlobForSig(r)
|
blob := record.RecordBlobForSig(r)
|
||||||
ok, err := pk.Verify(blob, r.GetSignature())
|
ok, err := pk.Verify(blob, r.GetSignature())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Signature verify failed.")
|
log.Error("Signature verify failed.")
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/jbenet/go-ipfs/routing"
|
"github.com/jbenet/go-ipfs/routing"
|
||||||
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
||||||
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
||||||
|
record "github.com/jbenet/go-ipfs/routing/record"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
errors "github.com/jbenet/go-ipfs/util/debugerror"
|
errors "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
pset "github.com/jbenet/go-ipfs/util/peerset"
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rec, err := MakePutRecord(sk, key, value)
|
rec, err := record.MakePutRecord(sk, key, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Creation of record failed!")
|
log.Error("Creation of record failed!")
|
||||||
return err
|
return err
|
||||||
|
@ -10,8 +10,8 @@ import (
|
|||||||
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
"github.com/jbenet/go-ipfs/p2p/peer"
|
"github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
routing "github.com/jbenet/go-ipfs/routing"
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
dht "github.com/jbenet/go-ipfs/routing/dht"
|
|
||||||
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
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"
|
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
42
routing/record/record.go
Normal file
42
routing/record/record.go
Normal file
@ -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{})
|
||||||
|
}
|
Reference in New Issue
Block a user