From f7faf94c4013479b60c197b36d1e9050ef6d1434 Mon Sep 17 00:00:00 2001 From: decanus Date: Tue, 9 Jul 2019 17:21:01 -0400 Subject: [PATCH] updated protobufs, added some helpers --- client/Client.go | 19 ++++++------- protobuf/message.pb.go | 60 ++++++++++++++++++------------------------ protobuf/message.proto | 9 +++---- protobuf/messageid.go | 22 ++++++++++++++++ 4 files changed, 62 insertions(+), 48 deletions(-) create mode 100644 protobuf/messageid.go diff --git a/client/Client.go b/client/Client.go index 2536c44..900ab3b 100644 --- a/client/Client.go +++ b/client/Client.go @@ -2,8 +2,6 @@ package client import ( - "time" - "github.com/golang/protobuf/proto" "github.com/status-im/dasy/protobuf" mvds "github.com/status-im/mvds/node" @@ -16,6 +14,8 @@ type Chat state.GroupID // Client is the actual daisy client. type Client struct { node mvds.Node + + lastMessage protobuf.MessageID } // Invite invites a peer to a chat. @@ -44,23 +44,22 @@ func (c *Client) Ack(chat Chat, messageID []byte) { } // Post sends a message to a chat. -func (c *Client) Post(chat Chat, body []byte) { - c.send(chat, protobuf.Message_POST, body) // @todo +func (c *Client) Post(chat Chat, body []byte) error { + return c.send(chat, protobuf.Message_POST, body) } func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) error { msg := &protobuf.Message{ - Clock: 0, - Timestamp: uint64(time.Now().Unix()), // @todo we may be able to take this from mvds - MessageType: protobuf.Message_MessageType(t), - Body: body, + MessageType: protobuf.Message_MessageType(t), + Body: body, + PreviousMessage: c.lastMessage[:], } // @todo sign buf, err := proto.Marshal(msg) if err != nil { - return err + return err } _, err = c.node.AppendMessage(state.GroupID(chat), buf) @@ -68,5 +67,7 @@ func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) er return err } + c.lastMessage = msg.ID() + return nil } diff --git a/protobuf/message.pb.go b/protobuf/message.pb.go index fe2c373..ac9d695 100644 --- a/protobuf/message.pb.go +++ b/protobuf/message.pb.go @@ -58,11 +58,10 @@ func (Message_MessageType) EnumDescriptor() ([]byte, []int) { } type Message struct { - Clock uint64 `protobuf:"varint,1,opt,name=clock,proto3" json:"clock,omitempty"` - Timestamp uint64 `protobuf:"varint,2,opt,name=timestamp,proto3" json:"timestamp,omitempty"` - MessageType Message_MessageType `protobuf:"varint,3,opt,name=message_type,json=messageType,proto3,enum=mvds.Message_MessageType" json:"message_type,omitempty"` - Body []byte `protobuf:"bytes,4,opt,name=body,proto3" json:"body,omitempty"` - Signature []byte `protobuf:"bytes,5,opt,name=signature,proto3" json:"signature,omitempty"` + MessageType Message_MessageType `protobuf:"varint,1,opt,name=message_type,json=messageType,proto3,enum=mvds.Message_MessageType" json:"message_type,omitempty"` + Body []byte `protobuf:"bytes,2,opt,name=body,proto3" json:"body,omitempty"` + PreviousMessage []byte `protobuf:"bytes,3,opt,name=previous_message,json=previousMessage,proto3" json:"previous_message,omitempty"` + Signature [][]byte `protobuf:"bytes,4,rep,name=signature,proto3" json:"signature,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -93,20 +92,6 @@ func (m *Message) XXX_DiscardUnknown() { var xxx_messageInfo_Message proto.InternalMessageInfo -func (m *Message) GetClock() uint64 { - if m != nil { - return m.Clock - } - return 0 -} - -func (m *Message) GetTimestamp() uint64 { - if m != nil { - return m.Timestamp - } - return 0 -} - func (m *Message) GetMessageType() Message_MessageType { if m != nil { return m.MessageType @@ -121,7 +106,14 @@ func (m *Message) GetBody() []byte { return nil } -func (m *Message) GetSignature() []byte { +func (m *Message) GetPreviousMessage() []byte { + if m != nil { + return m.PreviousMessage + } + return nil +} + +func (m *Message) GetSignature() [][]byte { if m != nil { return m.Signature } @@ -136,20 +128,20 @@ func init() { func init() { proto.RegisterFile("protobuf/message.proto", fileDescriptor_8368f5d77b0b9b7b) } var fileDescriptor_8368f5d77b0b9b7b = []byte{ - // 240 bytes of a gzipped FileDescriptorProto + // 231 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0xcf, 0x4d, 0x2d, 0x2e, 0x4e, 0x4c, 0x4f, 0xd5, 0x03, 0x0b, 0x08, - 0xb1, 0xe4, 0x96, 0xa5, 0x14, 0x2b, 0xfd, 0x62, 0xe4, 0x62, 0xf7, 0x85, 0x88, 0x0b, 0x89, 0x70, - 0xb1, 0x26, 0xe7, 0xe4, 0x27, 0x67, 0x4b, 0x30, 0x2a, 0x30, 0x6a, 0xb0, 0x04, 0x41, 0x38, 0x42, - 0x32, 0x5c, 0x9c, 0x25, 0x99, 0xb9, 0xa9, 0xc5, 0x25, 0x89, 0xb9, 0x05, 0x12, 0x4c, 0x60, 0x19, - 0x84, 0x80, 0x90, 0x0d, 0x17, 0x0f, 0xd4, 0xd8, 0xf8, 0x92, 0xca, 0x82, 0x54, 0x09, 0x66, 0x05, - 0x46, 0x0d, 0x3e, 0x23, 0x49, 0x3d, 0x90, 0xe1, 0x7a, 0x50, 0x83, 0x61, 0x74, 0x48, 0x65, 0x41, - 0x6a, 0x10, 0x77, 0x2e, 0x82, 0x23, 0x24, 0xc4, 0xc5, 0x92, 0x94, 0x9f, 0x52, 0x29, 0xc1, 0xa2, - 0xc0, 0xa8, 0xc1, 0x13, 0x04, 0x66, 0x83, 0xec, 0x2b, 0xce, 0x4c, 0xcf, 0x4b, 0x2c, 0x29, 0x2d, - 0x4a, 0x95, 0x60, 0x05, 0x4b, 0x20, 0x04, 0x94, 0xbc, 0xb9, 0xb8, 0x91, 0x4c, 0x13, 0xe2, 0xe2, - 0x62, 0xf3, 0xf4, 0x0b, 0xf3, 0x0c, 0x71, 0x15, 0x60, 0x10, 0xe2, 0xe0, 0x62, 0xf1, 0xf2, 0xf7, - 0xf4, 0x13, 0x60, 0x14, 0xe2, 0xe4, 0x62, 0xf5, 0x71, 0x75, 0x0c, 0x73, 0x15, 0x60, 0x02, 0x09, - 0x7a, 0x7b, 0x3a, 0x7b, 0x0b, 0x30, 0x0b, 0xb1, 0x73, 0x31, 0x3b, 0x3a, 0x7b, 0x0b, 0xb0, 0x80, - 0x84, 0x02, 0xfc, 0x83, 0x43, 0x04, 0x58, 0x9d, 0xb8, 0xa2, 0x38, 0x60, 0x81, 0x93, 0xc4, 0x06, - 0x66, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xc3, 0x59, 0x25, 0x81, 0x2f, 0x01, 0x00, 0x00, + 0xb1, 0xe4, 0x96, 0xa5, 0x14, 0x2b, 0x7d, 0x64, 0xe4, 0x62, 0xf7, 0x85, 0x88, 0x0b, 0xd9, 0x70, + 0xf1, 0x40, 0x95, 0xc4, 0x97, 0x54, 0x16, 0xa4, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x19, 0x49, + 0xea, 0x81, 0x14, 0xea, 0x41, 0x15, 0xc1, 0xe8, 0x90, 0xca, 0x82, 0xd4, 0x20, 0xee, 0x5c, 0x04, + 0x47, 0x48, 0x88, 0x8b, 0x25, 0x29, 0x3f, 0xa5, 0x52, 0x82, 0x49, 0x81, 0x51, 0x83, 0x27, 0x08, + 0xcc, 0x16, 0xd2, 0xe4, 0x12, 0x28, 0x28, 0x4a, 0x2d, 0xcb, 0xcc, 0x2f, 0x2d, 0x8e, 0x87, 0xaa, + 0x95, 0x60, 0x06, 0xcb, 0xf3, 0xc3, 0xc4, 0x61, 0x96, 0xcb, 0x70, 0x71, 0x16, 0x67, 0xa6, 0xe7, + 0x25, 0x96, 0x94, 0x16, 0xa5, 0x4a, 0xb0, 0x28, 0x30, 0x6b, 0xf0, 0x04, 0x21, 0x04, 0x94, 0xbc, + 0xb9, 0xb8, 0x91, 0x2c, 0x16, 0xe2, 0xe2, 0x62, 0xf3, 0xf4, 0x0b, 0xf3, 0x0c, 0x71, 0x15, 0x60, + 0x10, 0xe2, 0xe0, 0x62, 0xf1, 0xf2, 0xf7, 0xf4, 0x13, 0x60, 0x14, 0xe2, 0xe4, 0x62, 0xf5, 0x71, + 0x75, 0x0c, 0x73, 0x15, 0x60, 0x02, 0x09, 0x7a, 0x7b, 0x3a, 0x7b, 0x0b, 0x30, 0x0b, 0xb1, 0x73, + 0x31, 0x3b, 0x3a, 0x7b, 0x0b, 0xb0, 0x80, 0x84, 0x02, 0xfc, 0x83, 0x43, 0x04, 0x58, 0x9d, 0xb8, + 0xa2, 0x38, 0x60, 0x61, 0x92, 0xc4, 0x06, 0x66, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x5c, + 0xd7, 0xa3, 0xb8, 0x26, 0x01, 0x00, 0x00, } diff --git a/protobuf/message.proto b/protobuf/message.proto index 76b589e..81feb0e 100644 --- a/protobuf/message.proto +++ b/protobuf/message.proto @@ -14,9 +14,8 @@ message Message { POST = 5; } - uint64 clock = 1; - uint64 timestamp = 2; - MessageType message_type = 3; - bytes body = 4; - bytes signature = 5; + MessageType message_type = 1; + bytes body = 2; + bytes previous_message = 3; + repeated bytes signature = 4; } diff --git a/protobuf/messageid.go b/protobuf/messageid.go new file mode 100644 index 0000000..42bd5f4 --- /dev/null +++ b/protobuf/messageid.go @@ -0,0 +1,22 @@ +// Package protobuf contains protocol buffers as well as helper functions for those buffers. +package protobuf + +import ( + "crypto/sha256" + "encoding/binary" +) + +// MessageID is a hash of `message_type`, `body` & `previous_message`. +type MessageID [32]byte + +// ID returns the message ID. +func (m *Message) ID() MessageID { + t := make([]byte, 8) + binary.LittleEndian.PutUint32(t, uint32(m.MessageType)) + + b := append([]byte("MESSAGE_ID"), t...) + b = append(b, m.Body...) + b = append(b, m.PreviousMessage...) + + return sha256.Sum256(b) +}