Skip to content

Commit

Permalink
Follow key -> Service key and a single public API Thread key (#272)
Browse files Browse the repository at this point in the history
* keys: (wip) follow -> service and consolidation

Signed-off-by: Sander Pick <[email protected]>

* keys: more updates for new keys

Signed-off-by: Sander Pick <[email protected]>

* keys: fix up thread key to bytes

Signed-off-by: Sander Pick <[email protected]>

* android: fix up client

Signed-off-by: Sander Pick <[email protected]>

* nit: remove uneeded keys in test

Signed-off-by: Sander Pick <[email protected]>

* review: address comments

Signed-off-by: Sander Pick <[email protected]>
  • Loading branch information
sanderpick authored Mar 19, 2020
1 parent 2a96a5a commit 4399371
Show file tree
Hide file tree
Showing 44 changed files with 828 additions and 768 deletions.
6 changes: 2 additions & 4 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
ma "github.com/multiformats/go-multiaddr"
pb "github.com/textileio/go-threads/api/pb"
"github.com/textileio/go-threads/core/thread"
"github.com/textileio/go-threads/crypto/symmetric"
"github.com/textileio/go-threads/db"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -95,15 +94,14 @@ func (c *Client) NewDB(ctx context.Context, id thread.ID) error {
}

// NewDBFromAddr creates a new DB with address and keys.
func (c *Client) NewDBFromAddr(ctx context.Context, addr ma.Multiaddr, followKey, readKey *symmetric.Key, collections ...db.CollectionConfig) error {
func (c *Client) NewDBFromAddr(ctx context.Context, addr ma.Multiaddr, key thread.Key, collections ...db.CollectionConfig) error {
pbcollections := make([]*pb.CollectionConfig, len(collections))
for i, c := range collections {
pbcollections[i] = collectionConfigToPb(c)
}
_, err := c.c.NewDBFromAddr(ctx, &pb.NewDBFromAddrRequest{
DbAddr: addr.String(),
FollowKey: followKey.Bytes(),
ReadKey: readKey.Bytes(),
DbKey: key.Bytes(),
Collections: pbcollections,
})
return err
Expand Down
15 changes: 4 additions & 11 deletions api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
. "github.com/textileio/go-threads/api/client"
pb "github.com/textileio/go-threads/api/pb"
"github.com/textileio/go-threads/core/thread"
sym "github.com/textileio/go-threads/crypto/symmetric"
"github.com/textileio/go-threads/db"
"github.com/textileio/go-threads/util"
"google.golang.org/grpc"
Expand Down Expand Up @@ -54,11 +53,8 @@ func TestNewDBFromAddr(t *testing.T) {
t.Run("test new db from address", func(t *testing.T) {
addr, err := ma.NewMultiaddr(info.Addresses[0])
checkErr(t, err)
fk, err := sym.FromBytes(info.FollowKey)
checkErr(t, err)
rk, err := sym.FromBytes(info.FollowKey)
checkErr(t, err)
if err := client2.NewDBFromAddr(context.Background(), addr, fk, rk); err != nil {
key, err := thread.KeyFromBytes(info.DbKey)
if err := client2.NewDBFromAddr(context.Background(), addr, key); err != nil {
t.Fatalf("failed to create new db from address: %v", err)
}
})
Expand Down Expand Up @@ -113,11 +109,8 @@ func TestGetDBInfo(t *testing.T) {
if err != nil {
t.Fatalf("failed to create collection: %v", err)
}
if info.FollowKey == nil {
t.Fatal("got nil follow key")
}
if info.ReadKey == nil {
t.Fatal("got nil read key")
if info.DbKey == nil {
t.Fatal("got nil db key")
}
if len(info.Addresses) == 0 {
t.Fatal("got empty addresses")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,20 @@ public void NewDB (String dbID, StreamObserver<NewDBReply> responseObserver) {
asyncStub.newDB(request.build(), responseObserver);
}

public void NewDBFromAddrSync (String address, ByteString followKey, ByteString readKey, List<CollectionConfig> collections) {
public void NewDBFromAddrSync (String address, ByteString key, List<CollectionConfig> collections) {
NewDBFromAddrRequest.Builder request = NewDBFromAddrRequest.newBuilder();
request.setDbAddr(address);
request.setFollowKey(followKey);
request.setReadKey(readKey);
request.setDbKey(key);
for (int i = 0; i < collections.size(); i++) {
request.setCollections(i, collections.get(i));
}
blockingStub.newDBFromAddr(request.build());
}

public void NewDBFromAddr (String address, ByteString followKey, ByteString readKey, List<CollectionConfig> collections, StreamObserver<NewDBReply> responseObserver) {
public void NewDBFromAddr (String address, ByteString key, List<CollectionConfig> collections, StreamObserver<NewDBReply> responseObserver) {
NewDBFromAddrRequest.Builder request = NewDBFromAddrRequest.newBuilder();
request.setDbAddr(address);
request.setFollowKey(followKey);
request.setReadKey(readKey);
request.setDbKey(key);
for (int i = 0; i < collections.size(); i++) {
request.setCollections(i, collections.get(i));
}
Expand Down
186 changes: 85 additions & 101 deletions api/pb/api.pb.go

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions api/pb/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ message CollectionConfig {

message NewDBFromAddrRequest {
string dbAddr = 1;
bytes followKey = 2;
bytes readKey = 3;
bytes dbKey = 2;
repeated CollectionConfig collections = 4;
}

Expand All @@ -36,8 +35,7 @@ message GetDBInfoRequest {

message GetDBInfoReply {
repeated string addresses = 1;
bytes followKey = 2;
bytes readKey = 3;
bytes dbKey = 2;
}

message NewCollectionRequest {
Expand Down
14 changes: 4 additions & 10 deletions api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
coredb "github.com/textileio/go-threads/core/db"
"github.com/textileio/go-threads/core/net"
"github.com/textileio/go-threads/core/thread"
sym "github.com/textileio/go-threads/crypto/symmetric"
"github.com/textileio/go-threads/db"
"github.com/textileio/go-threads/util"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -51,7 +50,7 @@ func NewService(network net.Net, conf Config) (*Service, error) {
network,
db.WithJsonMode(true),
db.WithRepoPath(conf.RepoPath),
db.WithDebug(true))
db.WithDebug(conf.Debug))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -85,11 +84,7 @@ func (s *Service) NewDBFromAddr(ctx context.Context, req *pb.NewDBFromAddrReques
if err != nil {
return nil, err
}
rk, err := sym.FromBytes(req.ReadKey)
if err != nil {
return nil, err
}
fk, err := sym.FromBytes(req.FollowKey)
key, err := thread.KeyFromBytes(req.DbKey)
if err != nil {
return nil, err
}
Expand All @@ -98,7 +93,7 @@ func (s *Service) NewDBFromAddr(ctx context.Context, req *pb.NewDBFromAddrReques
for i, c := range req.Collections {
collections[i] = collectionConfigFromPb(c)
}
if _, err = s.manager.NewDBFromAddr(ctx, addr, fk, rk, collections...); err != nil {
if _, err = s.manager.NewDBFromAddr(ctx, addr, key, collections...); err != nil {
return nil, err
}
return &pb.NewDBReply{}, nil
Expand Down Expand Up @@ -139,8 +134,7 @@ func (s *Service) GetDBInfo(ctx context.Context, req *pb.GetDBInfoRequest) (*pb.
}
reply := &pb.GetDBInfoReply{
Addresses: res,
FollowKey: tinfo.FollowKey.Bytes(),
ReadKey: tinfo.ReadKey.Bytes(),
DbKey: tinfo.Key.Bytes(),
}
return reply, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cbor/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func CreateEvent(ctx context.Context, dag format.DAGService, body format.Node, r
if err != nil {
return nil, err
}
keyb, err := key.Marshal()
keyb, err := key.MarshalBinary()
if err != nil {
return nil, err
}
Expand Down
9 changes: 1 addition & 8 deletions cbor/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ type record struct {
}

// CreateRecord returns a new record from the given block and log private key.
func CreateRecord(
ctx context.Context,
dag format.DAGService,
block format.Node,
prev cid.Cid,
sk ic.PrivKey,
key crypto.EncryptionKey,
) (net.Record, error) {
func CreateRecord(ctx context.Context, dag format.DAGService, block format.Node, prev cid.Cid, sk ic.PrivKey, key crypto.EncryptionKey) (net.Record, error) {
payload := block.Cid().Bytes()
if prev.Defined() {
payload = append(payload, prev.Bytes()...)
Expand Down
12 changes: 6 additions & 6 deletions core/logstore/logstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ type KeyBook interface {
// AddPrivKey adds a private key under a log.
AddPrivKey(thread.ID, peer.ID, ic.PrivKey) error

// ReadKey retrieves the read key of a log.
// ReadKey retrieves the read key of a thread.
ReadKey(thread.ID) (*sym.Key, error)

// AddReadKey adds a read key under a log.
// AddReadKey adds a read key under a thread.
AddReadKey(thread.ID, *sym.Key) error

// FollowKey retrieves the follow key of a log.
FollowKey(thread.ID) (*sym.Key, error)
// ServiceKey retrieves the service key of a thread.
ServiceKey(thread.ID) (*sym.Key, error)

// AddFollowKey adds a follow key under a log.
AddFollowKey(thread.ID, *sym.Key) error
// AddServiceKey adds a service key under a thread.
AddServiceKey(thread.ID, *sym.Key) error

// LogsWithKeys returns a list of log IDs for a service.
LogsWithKeys(thread.ID) (peer.IDSlice, error)
Expand Down
7 changes: 5 additions & 2 deletions core/net/service.go → core/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ type API interface {
GetThread(ctx context.Context, id thread.ID) (thread.Info, error)

// PullThread for new records.
// Logs owned by this host are traversed locally.
// Remotely addressed logs are pulled from the network.
// Is thread-safe.
PullThread(ctx context.Context, id thread.ID) error

// DeleteThread with id.
DeleteThread(ctx context.Context, id thread.ID) error

// AddFollower to a thread.
AddFollower(ctx context.Context, id thread.ID, paddr ma.Multiaddr) (peer.ID, error)
// AddReplicator sends the service key and all logs to another peer.
AddReplicator(ctx context.Context, id thread.ID, paddr ma.Multiaddr) (peer.ID, error)

// CreateRecord with body.
CreateRecord(ctx context.Context, id thread.ID, body format.Node) (ThreadRecord, error)
Expand Down
17 changes: 4 additions & 13 deletions core/net/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,21 @@ package net
import (
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/textileio/go-threads/core/thread"
"github.com/textileio/go-threads/crypto/symmetric"
)

// KeyOptions defines options for keys when creating / adding a thread.
type KeyOptions struct {
FollowKey *symmetric.Key
ReadKey *symmetric.Key
ThreadKey thread.Key
LogKey crypto.Key
}

// KeyOption specifies encryption keys.
type KeyOption func(*KeyOptions)

// FollowKey allows thread record traversal.
func FollowKey(key *symmetric.Key) KeyOption {
// ThreadKey handles log encryption.
func ThreadKey(key thread.Key) KeyOption {
return func(args *KeyOptions) {
args.FollowKey = key
}
}

// ReadKey allows for thread record decryption.
func ReadKey(key *symmetric.Key) KeyOption {
return func(args *KeyOptions) {
args.ReadKey = key
args.ThreadKey = key
}
}

Expand Down
8 changes: 3 additions & 5 deletions core/thread/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
mbase "github.com/multiformats/go-multibase"
sym "github.com/textileio/go-threads/crypto/symmetric"
)

var (
Expand Down Expand Up @@ -304,10 +303,9 @@ func (s IDSlice) Less(i, j int) bool { return s[i].str < s[j].str }

// Info holds thread logs and keys.
type Info struct {
ID ID
Logs []LogInfo
FollowKey *sym.Key
ReadKey *sym.Key
ID ID
Key Key
Logs []LogInfo
}

// GetOwnLog returns the first log found with a private key.
Expand Down
Loading

0 comments on commit 4399371

Please sign in to comment.