Skip to content

Commit

Permalink
decline transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
cammellos committed Jan 3, 2020
1 parent 5bb33f4 commit 67d8219
Show file tree
Hide file tree
Showing 25 changed files with 650 additions and 229 deletions.
4 changes: 2 additions & 2 deletions protocol/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type Message struct {

// Replace indicates that this is a replacement of a message
// that has been updated
Replace string `json:"replace"`
Replace string `json:"replace,omitEmpty"`
SigPubKey *ecdsa.PublicKey `json:"-"`
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func (m *Message) MarshalJSON() ([]byte, error) {
ChatId string `json:"chatId"`
LocalChatID string `json:"localChatId"`
Clock uint64 `json:"clock"`
Replace string `json:"replace"`
Replace string `json:"replace,omitEmpty"`
ResponseTo string `json:"responseTo"`
EnsName string `json:"ensName"`
Sticker *protobuf.StickerMessage `json:"sticker"`
Expand Down
34 changes: 34 additions & 0 deletions protocol/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,40 @@ func (m *MessageHandler) HandleDeclineRequestAddressForTransaction(messageState
return m.handleCommandMessage(messageState, oldMessage)
}

func (m *MessageHandler) HandleDeclineRequestTransaction(messageState *ReceivedMessageState, command protobuf.DeclineRequestTransaction) error {
err := ValidateReceivedDeclineRequestTransaction(&command)
if err != nil {
return err
}
oldMessage, err := m.persistence.MessageByID(command.Id)
if err != nil {
return err
}
if oldMessage == nil {
return errors.New("message not found")
}

if oldMessage.LocalChatID != messageState.CurrentMessageState.Contact.ID {
return errors.New("From must match")
}

if oldMessage.OutgoingStatus == "" {
return errors.New("Initial message must originate from us")
}

if oldMessage.CommandParameters.CommandState != CommandStateRequestTransaction {
return errors.New("Wrong state for command")
}

oldMessage.Clock = command.Clock
oldMessage.Timestamp = messageState.CurrentMessageState.WhisperTimestamp
oldMessage.Text = "Transaction request declined"
oldMessage.Replace = command.Id
oldMessage.CommandParameters.CommandState = CommandStateRequestTransactionDeclined

return m.handleCommandMessage(messageState, oldMessage)
}

func (m *MessageHandler) matchMessage(message *Message, chats map[string]*Chat) (*Chat, error) {
if message.SigPubKey == nil {
m.logger.Error("public key can't be empty")
Expand Down
12 changes: 12 additions & 0 deletions protocol/message_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func ValidateReceivedDeclineRequestAddressForTransaction(message *protobuf.Decli
return nil
}

func ValidateReceivedDeclineRequestTransaction(message *protobuf.DeclineRequestTransaction) error {
if message.Clock == 0 {
return errors.New("Clock can't be 0")
}

if len(message.Id) == 0 {
return errors.New("MessageID can't be empty")
}

return nil
}

func ValidateReceivedChatMessage(message *protobuf.ChatMessage) error {
if message.Clock == 0 {
return errors.New("Clock can't be 0")
Expand Down
19 changes: 0 additions & 19 deletions protocol/message_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,6 @@ func (s *MessageValidatorSuite) TestValidatePlainTextMessage() {
ContentType: protobuf.ChatMessage_STICKER,
},
},
{
Name: "Invalid sticker message without Pack",
Valid: false,
Message: protobuf.ChatMessage{
ChatId: "a",
Text: "valid",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
Payload: &protobuf.ChatMessage_Sticker{
Sticker: &protobuf.StickerMessage{
Hash: "some-hash",
},
},
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_STICKER,
},
},
{
Name: "Invalid sticker message without Hash",
Valid: false,
Expand Down
92 changes: 90 additions & 2 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,11 +1123,9 @@ func (m *Messenger) hasPairedDevices() bool {

// sendToPairedDevices will check if we have any paired devices and send to them if necessary
func (m *Messenger) sendToPairedDevices(ctx context.Context, payload []byte, messageType protobuf.ApplicationMetadataMessage_Type) error {
m.logger.Info("PAIRED")
hasPairedDevices := m.hasPairedDevices()
// We send a message to any paired device
if hasPairedDevices {
m.logger.Info("hAH PAIRED")
_, err := m.processor.SendPrivateRaw(ctx, &m.identity.PublicKey, payload, messageType)
if err != nil {
return err
Expand Down Expand Up @@ -1703,6 +1701,15 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
continue
}

case protobuf.DeclineRequestTransaction:
command := msg.ParsedMessage.(protobuf.DeclineRequestTransaction)
logger.Debug("Handling DeclineRequestTransaction")
err = m.handler.HandleDeclineRequestTransaction(messageState, command)
if err != nil {
logger.Warn("failed to handle DeclineRequestTransaction", zap.Error(err))
continue
}

case protobuf.RequestTransaction:
command := msg.ParsedMessage.(protobuf.RequestTransaction)
logger.Debug("Handling RequestTransaction")
Expand Down Expand Up @@ -2169,6 +2176,87 @@ func (m *Messenger) AcceptRequestAddressForTransaction(ctx context.Context, mess
return &response, m.saveChat(chat)
}

func (m *Messenger) DeclineRequestTransaction(ctx context.Context, messageID string) (*MessengerResponse, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

var response MessengerResponse

message, err := m.MessageByID(messageID)
if err != nil {
return nil, err
}

if message == nil {
return nil, errors.New("message not found")
}

chatID := message.LocalChatID

// A valid added chat is required.
chat, ok := m.allChats[chatID]
if !ok {
return nil, errors.New("Chat not found")
}
if chat.ChatType != ChatTypeOneToOne {
return nil, errors.New("Need to be a one-to-one chat")
}

clock, timestamp := chat.NextClockAndTimestamp()
message.Clock = clock
message.Timestamp = timestamp
message.Text = "Transaction request declined"
message.OutgoingStatus = OutgoingStatusSending
message.Replace = messageID

err = m.persistence.HideMessage(messageID)
if err != nil {
return nil, err
}

request := &protobuf.DeclineRequestTransaction{
Clock: message.Clock,
Id: messageID,
}
encodedMessage, err := proto.Marshal(request)
if err != nil {
return nil, err
}

newMessageID, err := m.dispatchMessage(ctx, &RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_TRANSACTION,
ResendAutomatically: true,
})

if err != nil {
return nil, err
}

message.ID = types.EncodeHex(newMessageID)
message.CommandParameters.CommandState = CommandStateRequestTransactionDeclined

err = message.PrepareContent()
if err != nil {
return nil, err
}

err = chat.UpdateFromMessage(message)
if err != nil {
return nil, err
}

err = m.persistence.SaveMessagesLegacy([]*Message{message})
if err != nil {
return nil, err
}

response.Chats = []*Chat{chat}
response.Messages = []*Message{message}
return &response, m.saveChat(chat)
}

func (m *Messenger) DeclineRequestAddressForTransaction(ctx context.Context, messageID string) (*MessengerResponse, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
Expand Down
Loading

0 comments on commit 67d8219

Please sign in to comment.