Skip to content

Commit

Permalink
add ChatID to transaction commands (#2274)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitgamma authored Jul 7, 2021
1 parent 491e2a8 commit 58e39ad
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 31 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.80.6
0.81.0
15 changes: 11 additions & 4 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3215,6 +3215,7 @@ func (m *Messenger) RequestTransaction(ctx context.Context, chatID, value, contr
Address: address,
Value: value,
Contract: contract,
ChatId: chatID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
Expand Down Expand Up @@ -3287,6 +3288,7 @@ func (m *Messenger) RequestAddressForTransaction(ctx context.Context, chatID, fr
Clock: message.Clock,
Value: value,
Contract: contract,
ChatId: chatID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
Expand Down Expand Up @@ -3384,6 +3386,7 @@ func (m *Messenger) AcceptRequestAddressForTransaction(ctx context.Context, mess
Clock: message.Clock,
Id: messageID,
Address: address,
ChatId: chatID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
Expand Down Expand Up @@ -3461,8 +3464,9 @@ func (m *Messenger) DeclineRequestTransaction(ctx context.Context, messageID str
}

request := &protobuf.DeclineRequestTransaction{
Clock: message.Clock,
Id: messageID,
Clock: message.Clock,
Id: messageID,
ChatId: chatID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
Expand Down Expand Up @@ -3539,8 +3543,9 @@ func (m *Messenger) DeclineRequestAddressForTransaction(ctx context.Context, mes
}

request := &protobuf.DeclineRequestAddressForTransaction{
Clock: message.Clock,
Id: messageID,
Clock: message.Clock,
Id: messageID,
ChatId: chatID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
Expand Down Expand Up @@ -3634,6 +3639,7 @@ func (m *Messenger) AcceptRequestTransaction(ctx context.Context, transactionHas
Id: messageID,
TransactionHash: transactionHash,
Signature: signature,
ChatId: chatID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
Expand Down Expand Up @@ -3707,6 +3713,7 @@ func (m *Messenger) SendTransaction(ctx context.Context, chatID, value, contract
Clock: message.Clock,
TransactionHash: transactionHash,
Signature: signature,
ChatId: chatID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
Expand Down
21 changes: 13 additions & 8 deletions protocol/messenger_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,11 @@ func (m *Messenger) HandleRequestAddressForTransaction(messageState *ReceivedMes
}
message := &common.Message{
ChatMessage: protobuf.ChatMessage{
Clock: command.Clock,
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
Text: "Request address for transaction",
ChatId: contactIDFromPublicKey(&m.identity.PublicKey),
Clock: command.Clock,
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
Text: "Request address for transaction",
// ChatId is only used as-is for messages sent to oneself (i.e: mostly sync) so no need to check it here
ChatId: command.GetChatId(),
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TRANSACTION_COMMAND,
},
Expand All @@ -713,10 +714,11 @@ func (m *Messenger) HandleRequestTransaction(messageState *ReceivedMessageState,
}
message := &common.Message{
ChatMessage: protobuf.ChatMessage{
Clock: command.Clock,
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
Text: "Request transaction",
ChatId: contactIDFromPublicKey(&m.identity.PublicKey),
Clock: command.Clock,
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
Text: "Request transaction",
// ChatId is only used for messages sent to oneself (i.e: mostly sync) so no need to check it here
ChatId: command.GetChatId(),
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TRANSACTION_COMMAND,
},
Expand Down Expand Up @@ -762,6 +764,7 @@ func (m *Messenger) HandleAcceptRequestAddressForTransaction(messageState *Recei
initialMessage.CommandParameters.Address = command.Address
initialMessage.Seen = false
initialMessage.CommandParameters.CommandState = common.CommandStateRequestAddressForTransactionAccepted
initialMessage.ChatId = command.GetChatId()

// Hide previous message
previousMessage, err := m.persistence.MessageByCommandID(messageState.CurrentMessageState.Contact.ID, command.Id)
Expand Down Expand Up @@ -831,6 +834,7 @@ func (m *Messenger) HandleDeclineRequestAddressForTransaction(messageState *Rece
oldMessage.Text = requestAddressForTransactionDeclinedMessage
oldMessage.Seen = false
oldMessage.CommandParameters.CommandState = common.CommandStateRequestAddressForTransactionDeclined
oldMessage.ChatId = command.GetChatId()

// Hide previous message
err = m.persistence.HideMessage(command.Id)
Expand Down Expand Up @@ -872,6 +876,7 @@ func (m *Messenger) HandleDeclineRequestTransaction(messageState *ReceivedMessag
oldMessage.Text = transactionRequestDeclinedMessage
oldMessage.Seen = false
oldMessage.CommandParameters.CommandState = common.CommandStateRequestTransactionDeclined
oldMessage.ChatId = command.GetChatId()

// Hide previous message
err = m.persistence.HideMessage(command.Id)
Expand Down
6 changes: 6 additions & 0 deletions protocol/messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,7 @@ func (s *MessengerSuite) TestDeclineRequestAddressForTransaction() {
_, err := theirMessenger.Start()
s.Require().NoError(err)
theirPkString := types.EncodeHex(crypto.FromECDSAPub(&theirMessenger.identity.PublicKey))
myPkString := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))

chat := CreateOneToOneChat(theirPkString, &theirMessenger.identity.PublicKey, s.m.transport)
err = s.m.SaveChat(chat)
Expand Down Expand Up @@ -1655,6 +1656,7 @@ func (s *MessengerSuite) TestDeclineRequestAddressForTransaction() {
s.Require().Equal(value, receiverMessage.CommandParameters.Value)
s.Require().Equal(contract, receiverMessage.CommandParameters.Contract)
s.Require().Equal(initialCommandID, receiverMessage.CommandParameters.ID)
s.Require().Equal(theirPkString, receiverMessage.ChatId)
s.Require().Equal(common.CommandStateRequestAddressForTransaction, receiverMessage.CommandParameters.CommandState)

// We decline the request
Expand Down Expand Up @@ -1692,6 +1694,7 @@ func (s *MessengerSuite) TestDeclineRequestAddressForTransaction() {
s.Require().Equal(contract, receiverMessage.CommandParameters.Contract)
s.Require().Equal(common.CommandStateRequestAddressForTransactionDeclined, receiverMessage.CommandParameters.CommandState)
s.Require().Equal(initialCommandID, receiverMessage.CommandParameters.ID)
s.Require().Equal(myPkString, receiverMessage.ChatId)
s.Require().Equal(initialCommandID, receiverMessage.Replace)
s.Require().NoError(theirMessenger.Shutdown())
}
Expand Down Expand Up @@ -1911,6 +1914,7 @@ func (s *MessengerSuite) TestAcceptRequestAddressForTransaction() {
_, err := theirMessenger.Start()
s.Require().NoError(err)
theirPkString := types.EncodeHex(crypto.FromECDSAPub(&theirMessenger.identity.PublicKey))
myPkString := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))

myAddress := crypto.PubkeyToAddress(s.m.identity.PublicKey)

Expand Down Expand Up @@ -1955,6 +1959,7 @@ func (s *MessengerSuite) TestAcceptRequestAddressForTransaction() {
s.Require().Equal(contract, receiverMessage.CommandParameters.Contract)
s.Require().Equal(initialCommandID, receiverMessage.CommandParameters.ID)
s.Require().Equal(common.CommandStateRequestAddressForTransaction, receiverMessage.CommandParameters.CommandState)
s.Require().Equal(theirPkString, receiverMessage.ChatId)

// We accept the request
response, err = theirMessenger.AcceptRequestAddressForTransaction(context.Background(), receiverMessage.ID, "some-address")
Expand Down Expand Up @@ -1994,6 +1999,7 @@ func (s *MessengerSuite) TestAcceptRequestAddressForTransaction() {
s.Require().Equal(initialCommandID, receiverMessage.CommandParameters.ID)
s.Require().Equal("some-address", receiverMessage.CommandParameters.Address)
s.Require().Equal(initialCommandID, receiverMessage.Replace)
s.Require().Equal(myPkString, receiverMessage.ChatId)
s.Require().NoError(theirMessenger.Shutdown())
}

Expand Down
86 changes: 68 additions & 18 deletions protocol/protobuf/command.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions protocol/protobuf/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,40 @@ message RequestAddressForTransaction {
uint64 clock = 1;
string value = 2;
string contract = 3;
string chat_id = 4;
}

message AcceptRequestAddressForTransaction {
uint64 clock = 1;
string id = 2;
string address = 3;
string chat_id = 4;
}

message DeclineRequestAddressForTransaction {
uint64 clock = 1;
string id = 2;
string chat_id = 3;
}

message DeclineRequestTransaction {
uint64 clock = 1;
string id = 2;
string chat_id = 3;
}

message RequestTransaction {
uint64 clock = 1;
string address = 2;
string value = 3;
string contract = 4;
string chat_id = 5;
}

message SendTransaction {
uint64 clock = 1;
string id = 2;
string transaction_hash = 3;
bytes signature = 4;
string chat_id = 5;
}

0 comments on commit 58e39ad

Please sign in to comment.