2022-07-05 19:49:44 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2022-08-31 14:41:58 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2022-10-28 11:37:46 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2023-08-18 11:39:59 +00:00
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
2022-07-05 19:49:44 +00:00
|
|
|
"github.com/status-im/status-go/protocol/verification"
|
|
|
|
)
|
|
|
|
|
|
|
|
const minContactVerificationMessageLen = 1
|
|
|
|
const maxContactVerificationMessageLen = 280
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
func (m *Messenger) SendContactVerificationRequest(ctx context.Context, contactID string, challenge string) (*MessengerResponse, error) {
|
2022-07-05 19:49:44 +00:00
|
|
|
if len(challenge) < minContactVerificationMessageLen || len(challenge) > maxContactVerificationMessageLen {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, errors.New("invalid verification request challenge length")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contact, ok := m.allContacts.Load(contactID)
|
2023-01-20 14:28:30 +00:00
|
|
|
if !ok || !contact.mutual() {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, errors.New("must be a mutual contact")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
verifRequest := &verification.Request{
|
|
|
|
From: common.PubkeyToHex(&m.identity.PublicKey),
|
|
|
|
To: contact.ID,
|
|
|
|
Challenge: challenge,
|
|
|
|
RequestStatus: verification.RequestStatusPENDING,
|
|
|
|
RepliedAt: 0,
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chat, ok := m.allChats.Load(contactID)
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
|
|
|
|
request := &protobuf.RequestContactVerification{
|
|
|
|
Clock: clock,
|
|
|
|
Challenge: challenge,
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
rawMessage, err := m.dispatchMessage(ctx, common.RawMessage{
|
2022-07-05 19:49:44 +00:00
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_REQUEST_CONTACT_VERIFICATION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contact.VerificationStatus = VerificationStatusVERIFYING
|
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
|
|
|
|
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We sync the contact with the other devices
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.syncContact(context.Background(), contact, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.allContacts.Store(contact.ID, contact)
|
|
|
|
|
|
|
|
verifRequest.RequestedAt = clock
|
2022-08-31 14:41:58 +00:00
|
|
|
verifRequest.ID = rawMessage.ID
|
2022-07-05 19:49:44 +00:00
|
|
|
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(verifRequest)
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), verifRequest, m.dispatchMessage)
|
2022-08-31 14:41:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
chatMessage, err := m.createLocalContactVerificationMessage(request.Challenge, chat, rawMessage.ID, common.ContactVerificationStatePending)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
err = m.persistence.SaveMessages([]*common.Message{chatMessage})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-13 20:09:20 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
response.AddVerificationRequest(verifRequest)
|
2022-08-31 14:41:58 +00:00
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
err = m.createOrUpdateOutgoingContactVerificationNotification(contact, response, verifRequest, chatMessage, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
response.AddMessage(chatMessage)
|
|
|
|
|
2024-01-08 18:18:57 +00:00
|
|
|
err = m.prepareMessages(response.messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-31 14:41:58 +00:00
|
|
|
|
|
|
|
return response, nil
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
func (m *Messenger) GetVerificationRequestSentTo(ctx context.Context, contactID string) (*verification.Request, error) {
|
2022-07-05 19:49:44 +00:00
|
|
|
_, ok := m.allContacts.Load(contactID)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("contact not found")
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:27:02 +00:00
|
|
|
return m.verificationDatabase.GetLatestVerificationRequestSentTo(contactID)
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) GetReceivedVerificationRequests(ctx context.Context) ([]*verification.Request, error) {
|
|
|
|
myPubKey := hexutil.Encode(crypto.FromECDSAPub(&m.identity.PublicKey))
|
|
|
|
return m.verificationDatabase.GetReceivedVerificationRequests(myPubKey)
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:27:02 +00:00
|
|
|
func (m *Messenger) CancelVerificationRequest(ctx context.Context, id string) (*MessengerResponse, error) {
|
|
|
|
verifRequest, err := m.verificationDatabase.GetVerificationRequest(id)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-12-14 08:27:02 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if verifRequest == nil {
|
2022-12-14 08:27:02 +00:00
|
|
|
m.logger.Error("could not find verification request with id", zap.String("id", id))
|
|
|
|
return nil, verification.ErrVerificationRequestNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if verifRequest.From != common.PubkeyToHex(&m.identity.PublicKey) {
|
|
|
|
return nil, errors.New("Can cancel only outgoing contact request")
|
|
|
|
}
|
|
|
|
|
|
|
|
contactID := verifRequest.To
|
|
|
|
contact, ok := m.allContacts.Load(contactID)
|
2023-01-20 14:28:30 +00:00
|
|
|
if !ok || !contact.mutual() {
|
2022-12-14 08:27:02 +00:00
|
|
|
return nil, errors.New("Can't find contact for canceling verification request")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if verifRequest.RequestStatus != verification.RequestStatusPENDING {
|
2022-12-14 08:27:02 +00:00
|
|
|
return nil, errors.New("can cancel only pending verification request")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
verifRequest.RequestStatus = verification.RequestStatusCANCELED
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(verifRequest)
|
|
|
|
if err != nil {
|
2022-12-14 08:27:02 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
contact.VerificationStatus = VerificationStatusUNVERIFIED
|
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
|
|
|
|
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
2022-12-14 08:27:02 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We sync the contact with the other devices
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.syncContact(context.Background(), contact, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-12-14 08:27:02 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 08:27:02 +00:00
|
|
|
m.allContacts.Store(contact.ID, contact)
|
|
|
|
|
|
|
|
// NOTE: does we need it?
|
|
|
|
chat, ok := m.allChats.Load(verifRequest.To)
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
response.AddVerificationRequest(verifRequest)
|
|
|
|
|
2024-02-13 19:34:56 +00:00
|
|
|
response.AddContact(contact)
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), verifRequest, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-12-14 08:27:02 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 08:27:02 +00:00
|
|
|
request := &protobuf.CancelContactVerification{
|
|
|
|
Id: id,
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2022-12-14 08:27:02 +00:00
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, common.RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_CANCEL_CONTACT_VERIFICATION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
notification, err := m.persistence.GetActivityCenterNotificationByID(types.FromHex(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notification != nil {
|
|
|
|
notification.ContactVerificationStatus = verification.RequestStatusCANCELED
|
|
|
|
message := notification.Message
|
|
|
|
message.ContactVerificationState = common.ContactVerificationStateCanceled
|
2023-01-28 09:52:53 +00:00
|
|
|
notification.Read = true
|
2023-10-22 09:41:20 +00:00
|
|
|
notification.UpdatedAt = m.GetCurrentTimeInMillis()
|
2023-01-28 09:52:53 +00:00
|
|
|
|
2023-10-22 09:41:20 +00:00
|
|
|
err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterReadByIDs)
|
2022-12-26 04:35:46 +00:00
|
|
|
if err != nil {
|
2023-01-28 09:52:53 +00:00
|
|
|
m.logger.Error("failed to save notification", zap.Error(err))
|
2022-12-26 04:35:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:27:02 +00:00
|
|
|
return response, nil
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
func (m *Messenger) AcceptContactVerificationRequest(ctx context.Context, id string, response string) (*MessengerResponse, error) {
|
|
|
|
verifRequest, err := m.verificationDatabase.GetVerificationRequest(id)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if verifRequest == nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
m.logger.Error("could not find verification request with id", zap.String("id", id))
|
|
|
|
return nil, verification.ErrVerificationRequestNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
contactID := verifRequest.From
|
|
|
|
|
|
|
|
contact, ok := m.allContacts.Load(contactID)
|
2023-01-20 14:28:30 +00:00
|
|
|
if !ok || !contact.mutual() {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, errors.New("must be a mutual contact")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chat, ok := m.allChats.Load(contactID)
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
err = m.verificationDatabase.AcceptContactVerificationRequest(id, response)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
verifRequest, err = m.verificationDatabase.GetVerificationRequest(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), verifRequest, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request := &protobuf.AcceptContactVerification{
|
|
|
|
Clock: clock,
|
2022-08-31 14:41:58 +00:00
|
|
|
Id: verifRequest.ID,
|
2022-07-05 19:49:44 +00:00
|
|
|
Response: response,
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
rawMessage, err := m.dispatchMessage(ctx, common.RawMessage{
|
2022-07-05 19:49:44 +00:00
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_ACCEPT_CONTACT_VERIFICATION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
// Pull one from the db if there
|
|
|
|
notification, err := m.persistence.GetActivityCenterNotificationByID(types.FromHex(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-13 20:09:20 +00:00
|
|
|
resp := &MessengerResponse{}
|
|
|
|
|
|
|
|
resp.AddVerificationRequest(verifRequest)
|
2022-08-31 14:41:58 +00:00
|
|
|
|
2022-10-28 09:51:54 +00:00
|
|
|
replyMessage, err := m.createLocalContactVerificationMessage(response, chat, rawMessage.ID, common.ContactVerificationStateAccepted)
|
2022-08-31 14:41:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:51:54 +00:00
|
|
|
err = m.persistence.SaveMessages([]*common.Message{replyMessage})
|
2022-08-31 14:41:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:51:54 +00:00
|
|
|
resp.AddMessage(replyMessage)
|
2022-08-31 14:41:58 +00:00
|
|
|
|
|
|
|
if notification != nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
// TODO: Should we update only the message or only the notification or both?
|
2022-08-31 14:41:58 +00:00
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
notification.ContactVerificationStatus = verification.RequestStatusACCEPTED
|
2022-08-31 14:41:58 +00:00
|
|
|
message := notification.Message
|
|
|
|
message.ContactVerificationState = common.ContactVerificationStateAccepted
|
2022-10-28 11:37:46 +00:00
|
|
|
notification.ReplyMessage = replyMessage
|
2023-01-28 09:52:53 +00:00
|
|
|
notification.Read = true
|
|
|
|
notification.Accepted = true
|
2023-10-22 09:41:20 +00:00
|
|
|
notification.UpdatedAt = m.GetCurrentTimeInMillis()
|
|
|
|
err = m.addActivityCenterNotification(resp, notification, m.syncActivityCenterAcceptedByIDs)
|
2022-08-31 14:41:58 +00:00
|
|
|
if err != nil {
|
2023-01-28 09:52:53 +00:00
|
|
|
m.logger.Error("failed to save notification", zap.Error(err))
|
2022-08-31 14:41:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-17 10:08:08 +00:00
|
|
|
resp.AddMessage(message) // <=== wasn't typo?
|
2022-08-31 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 11:37:46 +00:00
|
|
|
func (m *Messenger) VerifiedTrusted(ctx context.Context, request *requests.VerifiedTrusted) (*MessengerResponse, error) {
|
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Pull one from the db if there
|
|
|
|
notification, err := m.persistence.GetActivityCenterNotificationByID(request.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notification == nil || notification.ReplyMessage == nil {
|
|
|
|
return nil, errors.New("could not find notification")
|
|
|
|
}
|
|
|
|
|
|
|
|
contactID := notification.ReplyMessage.From
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
contact, ok := m.allContacts.Load(contactID)
|
2023-01-20 14:28:30 +00:00
|
|
|
if !ok || !contact.mutual() {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, errors.New("must be a mutual contact")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 11:37:46 +00:00
|
|
|
err = m.verificationDatabase.SetTrustStatus(contactID, verification.TrustStatusTRUSTED, m.getTimesource().GetCurrentTime())
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncTrustedUser(context.Background(), contactID, verification.TrustStatusTRUSTED, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contact.VerificationStatus = VerificationStatusVERIFIED
|
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chat, ok := m.allChats.Load(contactID)
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:27:02 +00:00
|
|
|
verifRequest, err := m.verificationDatabase.GetLatestVerificationRequestSentTo(contactID)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if verifRequest == nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, errors.New("no contact verification found")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
verifRequest.RequestStatus = verification.RequestStatusTRUSTED
|
|
|
|
verifRequest.RepliedAt = clock
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(verifRequest)
|
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), verifRequest, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We sync the contact with the other devices
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.syncContact(context.Background(), contact, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 09:36:54 +00:00
|
|
|
// Dispatch profile message to save a contact to the encrypted profile part
|
|
|
|
err = m.DispatchProfileShowcase()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-28 11:37:46 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
notification.ContactVerificationStatus = verification.RequestStatusTRUSTED
|
|
|
|
notification.Message.ContactVerificationState = common.ContactVerificationStateTrusted
|
2023-01-28 09:52:53 +00:00
|
|
|
notification.Read = true
|
|
|
|
notification.Accepted = true
|
2023-10-22 09:41:20 +00:00
|
|
|
notification.UpdatedAt = m.GetCurrentTimeInMillis()
|
2022-10-28 11:37:46 +00:00
|
|
|
|
2023-10-22 09:41:20 +00:00
|
|
|
err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterAcceptedByIDs)
|
2022-10-28 11:37:46 +00:00
|
|
|
if err != nil {
|
2023-01-28 09:52:53 +00:00
|
|
|
m.logger.Error("failed to save notification", zap.Error(err))
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := m.persistence.MessageByID(notification.ReplyMessage.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
msg.ContactVerificationState = common.ContactVerificationStateTrusted
|
|
|
|
|
|
|
|
err = m.persistence.SaveMessages([]*common.Message{msg})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddMessage(msg)
|
|
|
|
|
2024-02-13 19:34:56 +00:00
|
|
|
response.AddContact(contact)
|
|
|
|
|
2022-10-28 11:37:46 +00:00
|
|
|
return response, nil
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 11:37:46 +00:00
|
|
|
func (m *Messenger) VerifiedUntrustworthy(ctx context.Context, request *requests.VerifiedUntrustworthy) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull one from the db if there
|
|
|
|
notification, err := m.persistence.GetActivityCenterNotificationByID(request.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notification == nil || notification.ReplyMessage == nil {
|
|
|
|
return nil, errors.New("could not find notification")
|
|
|
|
}
|
|
|
|
|
|
|
|
contactID := notification.ReplyMessage.From
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
contact, ok := m.allContacts.Load(contactID)
|
2023-01-20 14:28:30 +00:00
|
|
|
if !ok || !contact.mutual() {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, errors.New("must be a mutual contact")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 11:37:46 +00:00
|
|
|
err = m.verificationDatabase.SetTrustStatus(contactID, verification.TrustStatusUNTRUSTWORTHY, m.getTimesource().GetCurrentTime())
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncTrustedUser(context.Background(), contactID, verification.TrustStatusUNTRUSTWORTHY, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contact.VerificationStatus = VerificationStatusVERIFIED
|
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
chat, ok := m.allChats.Load(contactID)
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
verifRequest, err := m.verificationDatabase.GetLatestVerificationRequestSentTo(contactID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if verifRequest == nil {
|
|
|
|
return nil, errors.New("no contact verification found")
|
|
|
|
}
|
|
|
|
|
|
|
|
verifRequest.RequestStatus = verification.RequestStatusUNTRUSTWORTHY
|
|
|
|
verifRequest.RepliedAt = clock
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(verifRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), verifRequest, m.dispatchMessage)
|
2022-12-26 04:35:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
// We sync the contact with the other devices
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.syncContact(context.Background(), contact, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 09:36:54 +00:00
|
|
|
// Dispatch profile message to remove a contact from the encrypted profile part
|
|
|
|
err = m.DispatchProfileShowcase()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-28 11:37:46 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
notification.ContactVerificationStatus = verification.RequestStatusUNTRUSTWORTHY
|
|
|
|
notification.Message.ContactVerificationState = common.ContactVerificationStateUntrustworthy
|
2023-01-28 09:52:53 +00:00
|
|
|
notification.Read = true
|
|
|
|
notification.Accepted = true
|
2023-10-22 09:41:20 +00:00
|
|
|
notification.UpdatedAt = m.GetCurrentTimeInMillis()
|
2022-10-28 11:37:46 +00:00
|
|
|
|
2023-10-22 09:41:20 +00:00
|
|
|
err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterAcceptedByIDs)
|
2022-10-28 11:37:46 +00:00
|
|
|
if err != nil {
|
2023-01-28 09:52:53 +00:00
|
|
|
m.logger.Error("failed to save notification", zap.Error(err))
|
2022-10-28 11:37:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := m.persistence.MessageByID(notification.ReplyMessage.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
msg.ContactVerificationState = common.ContactVerificationStateUntrustworthy
|
|
|
|
|
|
|
|
err = m.persistence.SaveMessages([]*common.Message{msg})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddMessage(msg)
|
|
|
|
|
|
|
|
return response, nil
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
func (m *Messenger) DeclineContactVerificationRequest(ctx context.Context, id string) (*MessengerResponse, error) {
|
2022-08-31 14:41:58 +00:00
|
|
|
verifRequest, err := m.verificationDatabase.GetVerificationRequest(id)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
if verifRequest == nil {
|
|
|
|
m.logger.Error("could not find verification request with id", zap.String("id", id))
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, verification.ErrVerificationRequestNotFound
|
2022-08-31 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contact, ok := m.allContacts.Load(verifRequest.From)
|
2023-01-20 14:28:30 +00:00
|
|
|
if !ok || !contact.mutual() {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, errors.New("must be a mutual contact")
|
2022-08-31 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
if verifRequest == nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, errors.New("no contact verification found")
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
chat, ok := m.allChats.Load(verifRequest.From)
|
2022-07-05 19:49:44 +00:00
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
|
|
|
|
verifRequest.RequestStatus = verification.RequestStatusDECLINED
|
|
|
|
verifRequest.RepliedAt = clock
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(verifRequest)
|
|
|
|
if err != nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
response.AddVerificationRequest(verifRequest)
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), verifRequest, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request := &protobuf.DeclineContactVerification{
|
2022-08-31 14:41:58 +00:00
|
|
|
Id: id,
|
2022-07-05 19:49:44 +00:00
|
|
|
Clock: clock,
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, common.RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_DECLINE_CONTACT_VERIFICATION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.verificationDatabase.DeclineContactVerificationRequest(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
notification, err := m.persistence.GetActivityCenterNotificationByID(types.FromHex(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notification != nil {
|
|
|
|
notification.ContactVerificationStatus = verification.RequestStatusDECLINED
|
2023-01-28 09:52:53 +00:00
|
|
|
notification.Read = true
|
|
|
|
notification.Dismissed = true
|
2023-10-22 09:41:20 +00:00
|
|
|
notification.UpdatedAt = m.GetCurrentTimeInMillis()
|
2023-01-28 09:52:53 +00:00
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
message := notification.Message
|
|
|
|
message.ContactVerificationState = common.ContactVerificationStateDeclined
|
2023-01-28 09:52:53 +00:00
|
|
|
|
2023-10-22 09:41:20 +00:00
|
|
|
err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterDismissedByIDs)
|
2022-10-24 11:33:47 +00:00
|
|
|
if err != nil {
|
2023-01-28 09:52:53 +00:00
|
|
|
m.logger.Error("failed to save notification", zap.Error(err))
|
2022-10-24 11:33:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddMessage(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) MarkAsTrusted(ctx context.Context, contactID string) error {
|
|
|
|
err := m.verificationDatabase.SetTrustStatus(contactID, verification.TrustStatusTRUSTED, m.getTimesource().GetCurrentTime())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
return m.SyncTrustedUser(ctx, contactID, verification.TrustStatusTRUSTED, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) MarkAsUntrustworthy(ctx context.Context, contactID string) error {
|
|
|
|
err := m.verificationDatabase.SetTrustStatus(contactID, verification.TrustStatusUNTRUSTWORTHY, m.getTimesource().GetCurrentTime())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
return m.SyncTrustedUser(ctx, contactID, verification.TrustStatusUNTRUSTWORTHY, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) RemoveTrustStatus(ctx context.Context, contactID string) error {
|
|
|
|
err := m.verificationDatabase.SetTrustStatus(contactID, verification.TrustStatusUNKNOWN, m.getTimesource().GetCurrentTime())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-09 09:36:54 +00:00
|
|
|
// Dispatch profile message to remove a contact from the encrypted profile part
|
|
|
|
err = m.DispatchProfileShowcase()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
return m.SyncTrustedUser(ctx, contactID, verification.TrustStatusUNKNOWN, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) GetTrustStatus(contactID string) (verification.TrustStatus, error) {
|
|
|
|
return m.verificationDatabase.GetTrustStatus(contactID)
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func ValidateContactVerificationRequest(request *protobuf.RequestContactVerification) error {
|
2022-07-05 19:49:44 +00:00
|
|
|
challengeLen := len(strings.TrimSpace(request.Challenge))
|
|
|
|
if challengeLen < minContactVerificationMessageLen || challengeLen > maxContactVerificationMessageLen {
|
|
|
|
return errors.New("invalid verification request challenge length")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func (m *Messenger) HandleRequestContactVerification(state *ReceivedMessageState, request *protobuf.RequestContactVerification, statusMessage *v1protocol.StatusMessage) error {
|
2022-07-05 19:49:44 +00:00
|
|
|
if err := ValidateContactVerificationRequest(request); err != nil {
|
|
|
|
m.logger.Debug("Invalid verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
id := state.CurrentMessageState.MessageID
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
if common.IsPubKeyEqual(state.CurrentMessageState.PublicKey, &m.identity.PublicKey) {
|
|
|
|
return nil // Is ours, do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
myPubKey := hexutil.Encode(crypto.FromECDSAPub(&m.identity.PublicKey))
|
|
|
|
contactID := hexutil.Encode(crypto.FromECDSAPub(state.CurrentMessageState.PublicKey))
|
|
|
|
|
|
|
|
contact := state.CurrentMessageState.Contact
|
2023-01-20 14:28:30 +00:00
|
|
|
if !contact.mutual() {
|
2022-07-05 19:49:44 +00:00
|
|
|
m.logger.Debug("Received a verification request for a non added mutual contact", zap.String("contactID", contactID))
|
|
|
|
return errors.New("must be a mutual contact")
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
persistedVR, err := m.verificationDatabase.GetVerificationRequest(id)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error obtaining verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR != nil && persistedVR.RequestedAt > request.Clock {
|
|
|
|
return nil // older message, ignore it
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR == nil {
|
|
|
|
// This is a new verification request, and we have not received its acceptance/decline before
|
|
|
|
persistedVR = &verification.Request{}
|
2022-08-31 14:41:58 +00:00
|
|
|
persistedVR.ID = id
|
2022-07-05 19:49:44 +00:00
|
|
|
persistedVR.From = contactID
|
|
|
|
persistedVR.To = myPubKey
|
|
|
|
persistedVR.RequestStatus = verification.RequestStatusPENDING
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
if persistedVR.From != contactID {
|
|
|
|
return errors.New("mismatch contactID and ID")
|
|
|
|
}
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
persistedVR.Challenge = request.Challenge
|
|
|
|
persistedVR.RequestedAt = request.Clock
|
|
|
|
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(persistedVR)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error storing verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
2022-08-31 14:41:58 +00:00
|
|
|
m.logger.Info("SAVED", zap.String("id", persistedVR.ID))
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), persistedVR, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
chat, ok := m.allChats.Load(contactID)
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
m.allChats.Store(chat.ID, chat)
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
chatMessage, err := m.createContactVerificationMessage(request.Challenge, chat, state, common.ContactVerificationStatePending)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Response.AddMessage(chatMessage)
|
|
|
|
|
|
|
|
state.AllVerificationRequests = append(state.AllVerificationRequests, persistedVR)
|
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
return m.createOrUpdateIncomingContactVerificationNotification(contact, state, persistedVR, chatMessage, nil)
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func ValidateAcceptContactVerification(request *protobuf.AcceptContactVerification) error {
|
2022-07-05 19:49:44 +00:00
|
|
|
responseLen := len(strings.TrimSpace(request.Response))
|
|
|
|
if responseLen < minContactVerificationMessageLen || responseLen > maxContactVerificationMessageLen {
|
|
|
|
return errors.New("invalid verification request response length")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func (m *Messenger) HandleAcceptContactVerification(state *ReceivedMessageState, request *protobuf.AcceptContactVerification, statusMessage *v1protocol.StatusMessage) error {
|
2022-07-05 19:49:44 +00:00
|
|
|
if err := ValidateAcceptContactVerification(request); err != nil {
|
|
|
|
m.logger.Debug("Invalid AcceptContactVerification", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if common.IsPubKeyEqual(state.CurrentMessageState.PublicKey, &m.identity.PublicKey) {
|
|
|
|
return nil // Is ours, do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
myPubKey := hexutil.Encode(crypto.FromECDSAPub(&m.identity.PublicKey))
|
|
|
|
contactID := hexutil.Encode(crypto.FromECDSAPub(state.CurrentMessageState.PublicKey))
|
|
|
|
|
|
|
|
contact := state.CurrentMessageState.Contact
|
2023-01-20 14:28:30 +00:00
|
|
|
if !contact.mutual() {
|
2022-07-05 19:49:44 +00:00
|
|
|
m.logger.Debug("Received a verification response for a non mutual contact", zap.String("contactID", contactID))
|
|
|
|
return errors.New("must be a mutual contact")
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
persistedVR, err := m.verificationDatabase.GetVerificationRequest(request.Id)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error obtaining verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR != nil && persistedVR.RepliedAt > request.Clock {
|
|
|
|
return nil // older message, ignore it
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR.RequestStatus == verification.RequestStatusCANCELED {
|
|
|
|
return nil // Do nothing, We have already cancelled the verification request
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR == nil {
|
|
|
|
// This is a response for which we have not received its request before
|
|
|
|
persistedVR = &verification.Request{}
|
2022-08-31 14:41:58 +00:00
|
|
|
persistedVR.ID = request.Id
|
2022-07-05 19:49:44 +00:00
|
|
|
persistedVR.From = contactID
|
|
|
|
persistedVR.To = myPubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
persistedVR.RequestStatus = verification.RequestStatusACCEPTED
|
|
|
|
persistedVR.Response = request.Response
|
|
|
|
persistedVR.RepliedAt = request.Clock
|
|
|
|
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(persistedVR)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error storing verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), persistedVR, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
chat, ok := m.allChats.Load(contactID)
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
|
|
|
|
chatMessage, err := m.createContactVerificationMessage(request.Response, chat, state, common.ContactVerificationStateAccepted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Response.AddMessage(chatMessage)
|
|
|
|
|
2022-10-28 09:51:54 +00:00
|
|
|
msg, err := m.persistence.MessageByID(request.Id)
|
2022-08-31 14:41:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-28 11:37:46 +00:00
|
|
|
msg.ContactVerificationState = common.ContactVerificationStateAccepted
|
2022-08-31 14:41:58 +00:00
|
|
|
|
2022-10-28 09:51:54 +00:00
|
|
|
state.Response.AddMessage(msg)
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
err = m.createOrUpdateOutgoingContactVerificationNotification(contact, state.Response, persistedVR, msg, chatMessage)
|
2022-10-28 09:51:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.AllVerificationRequests = append(state.AllVerificationRequests, persistedVR)
|
2022-07-05 19:49:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func (m *Messenger) HandleDeclineContactVerification(state *ReceivedMessageState, request *protobuf.DeclineContactVerification, statusMessage *v1protocol.StatusMessage) error {
|
2022-07-05 19:49:44 +00:00
|
|
|
if common.IsPubKeyEqual(state.CurrentMessageState.PublicKey, &m.identity.PublicKey) {
|
|
|
|
return nil // Is ours, do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
myPubKey := hexutil.Encode(crypto.FromECDSAPub(&m.identity.PublicKey))
|
|
|
|
contactID := hexutil.Encode(crypto.FromECDSAPub(state.CurrentMessageState.PublicKey))
|
|
|
|
|
|
|
|
contact := state.CurrentMessageState.Contact
|
2023-01-20 14:28:30 +00:00
|
|
|
if !contact.mutual() {
|
2022-07-05 19:49:44 +00:00
|
|
|
m.logger.Debug("Received a verification decline for a non mutual contact", zap.String("contactID", contactID))
|
|
|
|
return errors.New("must be a mutual contact")
|
|
|
|
}
|
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
persistedVR, err := m.verificationDatabase.GetVerificationRequest(request.Id)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error obtaining verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR != nil && persistedVR.RepliedAt > request.Clock {
|
|
|
|
return nil // older message, ignore it
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR.RequestStatus == verification.RequestStatusCANCELED {
|
|
|
|
return nil // Do nothing, We have already cancelled the verification request
|
|
|
|
}
|
|
|
|
|
2024-02-13 19:34:56 +00:00
|
|
|
contact.VerificationStatus = VerificationStatusUNVERIFIED
|
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
|
|
|
|
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We sync the contact with the other devices
|
|
|
|
err = m.syncContact(context.Background(), contact, m.dispatchMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allContacts.Store(contact.ID, contact)
|
|
|
|
|
|
|
|
state.Response.AddContact(contact)
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
if persistedVR == nil {
|
|
|
|
// This is a response for which we have not received its request before
|
|
|
|
persistedVR = &verification.Request{}
|
|
|
|
persistedVR.From = contactID
|
|
|
|
persistedVR.To = myPubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
persistedVR.RequestStatus = verification.RequestStatusDECLINED
|
|
|
|
persistedVR.RepliedAt = request.Clock
|
|
|
|
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(persistedVR)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error storing verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), persistedVR, m.dispatchMessage)
|
2022-07-05 19:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.AllVerificationRequests = append(state.AllVerificationRequests, persistedVR)
|
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
msg, err := m.persistence.MessageByID(request.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2022-10-24 11:33:47 +00:00
|
|
|
if msg != nil {
|
|
|
|
msg.ContactVerificationState = common.ContactVerificationStateDeclined
|
|
|
|
state.Response.AddMessage(msg)
|
|
|
|
}
|
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
return m.createOrUpdateOutgoingContactVerificationNotification(contact, state.Response, persistedVR, msg, nil)
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func (m *Messenger) HandleCancelContactVerification(state *ReceivedMessageState, request *protobuf.CancelContactVerification, statusMessage *v1protocol.StatusMessage) error {
|
2022-12-14 08:27:02 +00:00
|
|
|
myPubKey := hexutil.Encode(crypto.FromECDSAPub(&m.identity.PublicKey))
|
|
|
|
contactID := hexutil.Encode(crypto.FromECDSAPub(state.CurrentMessageState.PublicKey))
|
|
|
|
|
|
|
|
contact := state.CurrentMessageState.Contact
|
|
|
|
|
|
|
|
persistedVR, err := m.verificationDatabase.GetVerificationRequest(request.Id)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error obtaining verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR != nil && persistedVR.RequestStatus != verification.RequestStatusPENDING {
|
|
|
|
m.logger.Debug("Only pending verification request can be canceled", zap.String("contactID", contactID))
|
|
|
|
return errors.New("must be a pending verification request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if persistedVR == nil {
|
|
|
|
// This is a response for which we have not received its request before
|
|
|
|
persistedVR = &verification.Request{}
|
|
|
|
persistedVR.From = contactID
|
|
|
|
persistedVR.To = myPubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
persistedVR.RequestStatus = verification.RequestStatusCANCELED
|
|
|
|
persistedVR.RepliedAt = request.Clock
|
|
|
|
|
|
|
|
err = m.verificationDatabase.SaveVerificationRequest(persistedVR)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Debug("Error storing verification request", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.SyncVerificationRequest(context.Background(), persistedVR, m.dispatchMessage)
|
2022-12-14 08:27:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.AllVerificationRequests = append(state.AllVerificationRequests, persistedVR)
|
|
|
|
|
|
|
|
msg, err := m.persistence.MessageByID(request.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg != nil {
|
|
|
|
msg.ContactVerificationState = common.ContactVerificationStateCanceled
|
|
|
|
state.Response.AddMessage(msg)
|
|
|
|
}
|
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
return m.createOrUpdateIncomingContactVerificationNotification(contact, state, persistedVR, msg, nil)
|
2022-12-14 08:27:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
func (m *Messenger) GetLatestVerificationRequestFrom(contactID string) (*verification.Request, error) {
|
|
|
|
return m.verificationDatabase.GetLatestVerificationRequestFrom(contactID)
|
|
|
|
}
|
2022-07-05 19:49:44 +00:00
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
func (m *Messenger) createOrUpdateOutgoingContactVerificationNotification(contact *Contact, response *MessengerResponse, vr *verification.Request, chatMessage *common.Message, replyMessage *common.Message) error {
|
|
|
|
notification := &ActivityCenterNotification{
|
|
|
|
ID: types.FromHex(vr.ID),
|
2023-02-22 16:57:33 +00:00
|
|
|
Name: contact.PrimaryName(),
|
2022-12-26 04:35:46 +00:00
|
|
|
Type: ActivityCenterNotificationTypeContactVerification,
|
|
|
|
Author: chatMessage.From,
|
|
|
|
Message: chatMessage,
|
|
|
|
ReplyMessage: replyMessage,
|
|
|
|
Timestamp: chatMessage.WhisperTimestamp,
|
|
|
|
ChatID: contact.ID,
|
|
|
|
ContactVerificationStatus: vr.RequestStatus,
|
2023-01-28 09:52:53 +00:00
|
|
|
Read: vr.RequestStatus != verification.RequestStatusACCEPTED, // Mark as Unread Accepted notification because we are waiting for the asnwer
|
|
|
|
Accepted: vr.RequestStatus == verification.RequestStatusTRUSTED || vr.RequestStatus == verification.RequestStatusUNTRUSTWORTHY,
|
|
|
|
Dismissed: vr.RequestStatus == verification.RequestStatusDECLINED,
|
2023-10-22 09:41:20 +00:00
|
|
|
UpdatedAt: m.GetCurrentTimeInMillis(),
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 09:41:20 +00:00
|
|
|
return m.addActivityCenterNotification(response, notification, nil)
|
2022-07-05 19:49:44 +00:00
|
|
|
}
|
2022-08-31 14:41:58 +00:00
|
|
|
|
2022-12-26 04:35:46 +00:00
|
|
|
func (m *Messenger) createOrUpdateIncomingContactVerificationNotification(contact *Contact, messageState *ReceivedMessageState, vr *verification.Request, chatMessage *common.Message, replyMessage *common.Message) error {
|
2022-08-31 14:41:58 +00:00
|
|
|
notification := &ActivityCenterNotification{
|
|
|
|
ID: types.FromHex(vr.ID),
|
2023-02-22 16:57:33 +00:00
|
|
|
Name: contact.PrimaryName(),
|
2022-08-31 14:41:58 +00:00
|
|
|
Type: ActivityCenterNotificationTypeContactVerification,
|
|
|
|
Author: messageState.CurrentMessageState.Contact.ID,
|
|
|
|
Message: chatMessage,
|
2022-10-28 11:37:46 +00:00
|
|
|
ReplyMessage: replyMessage,
|
2022-08-31 14:41:58 +00:00
|
|
|
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
|
|
|
|
ChatID: contact.ID,
|
|
|
|
ContactVerificationStatus: vr.RequestStatus,
|
2023-01-28 09:52:53 +00:00
|
|
|
Read: vr.RequestStatus != verification.RequestStatusPENDING, // Unread only for pending incomming
|
|
|
|
Accepted: vr.RequestStatus == verification.RequestStatusACCEPTED || vr.RequestStatus == verification.RequestStatusTRUSTED || vr.RequestStatus == verification.RequestStatusUNTRUSTWORTHY,
|
|
|
|
Dismissed: vr.RequestStatus == verification.RequestStatusDECLINED,
|
2023-10-22 09:41:20 +00:00
|
|
|
UpdatedAt: m.GetCurrentTimeInMillis(),
|
2022-08-31 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 09:41:20 +00:00
|
|
|
return m.addActivityCenterNotification(messageState.Response, notification, nil)
|
2022-08-31 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) createContactVerificationMessage(challenge string, chat *Chat, state *ReceivedMessageState, verificationStatus common.ContactVerificationState) (*common.Message, error) {
|
2023-08-18 11:39:59 +00:00
|
|
|
chatMessage := common.NewMessage()
|
2022-08-31 14:41:58 +00:00
|
|
|
chatMessage.ID = state.CurrentMessageState.MessageID
|
|
|
|
chatMessage.From = state.CurrentMessageState.Contact.ID
|
|
|
|
chatMessage.Alias = state.CurrentMessageState.Contact.Alias
|
|
|
|
chatMessage.SigPubKey = state.CurrentMessageState.PublicKey
|
|
|
|
chatMessage.Identicon = state.CurrentMessageState.Contact.Identicon
|
|
|
|
chatMessage.WhisperTimestamp = state.CurrentMessageState.WhisperTimestamp
|
|
|
|
|
|
|
|
chatMessage.ChatId = chat.ID
|
|
|
|
chatMessage.Text = challenge
|
|
|
|
chatMessage.ContentType = protobuf.ChatMessage_IDENTITY_VERIFICATION
|
|
|
|
chatMessage.ContactVerificationState = verificationStatus
|
|
|
|
|
|
|
|
err := chatMessage.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return chatMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) createLocalContactVerificationMessage(challenge string, chat *Chat, id string, status common.ContactVerificationState) (*common.Message, error) {
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
chatMessage := common.NewMessage()
|
2022-08-31 14:41:58 +00:00
|
|
|
chatMessage.ID = id
|
|
|
|
err := extendMessageFromChat(chatMessage, chat, &m.identity.PublicKey, m.getTimesource())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chatMessage.ChatId = chat.ID
|
|
|
|
chatMessage.Text = challenge
|
|
|
|
chatMessage.ContentType = protobuf.ChatMessage_IDENTITY_VERIFICATION
|
|
|
|
chatMessage.ContactVerificationState = status
|
|
|
|
err = extendMessageFromChat(chatMessage, chat, &m.identity.PublicKey, m.getTimesource())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = chatMessage.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return chatMessage, nil
|
|
|
|
}
|