2023-02-21 12:35:26 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-05-16 10:48:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2023-02-21 12:35:26 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
func (m *Messenger) prepareSyncKeycardsMessage(keyUID string) (message []*protobuf.SyncKeycard, err error) {
|
2023-02-21 12:35:26 +00:00
|
|
|
allKeycards, err := m.settings.GetAllKnownKeycards()
|
|
|
|
if err != nil {
|
2023-02-27 10:19:18 +00:00
|
|
|
return message, err
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, kc := range allKeycards {
|
2023-05-16 10:48:00 +00:00
|
|
|
if kc.KeyUID != keyUID {
|
|
|
|
continue
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
2023-05-16 10:48:00 +00:00
|
|
|
syncKeycard := kc.ToSyncKeycard()
|
|
|
|
message = append(message, syncKeycard)
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 10:19:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:35:26 +00:00
|
|
|
func (m *Messenger) dispatchKeycardActivity(ctx context.Context, syncMessage protobuf.SyncKeycardAction) error {
|
|
|
|
if !m.hasPairedDevices() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clock, chat := m.getLastClockWithRelatedChat()
|
|
|
|
|
|
|
|
encodedMessage, err := proto.Marshal(&syncMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage := common.RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_KEYCARD_ACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, rawMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.LastClockValue = clock
|
|
|
|
return m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleSyncKeycardActivity(state *ReceivedMessageState, syncMessage protobuf.SyncKeycardAction) (err error) {
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
var kcAction = &accounts.KeycardAction{
|
2023-02-21 12:35:26 +00:00
|
|
|
Action: protobuf.SyncKeycardAction_Action_name[int32(syncMessage.Action)],
|
|
|
|
OldKeycardUID: syncMessage.OldKeycardUid,
|
2023-05-16 10:48:00 +00:00
|
|
|
Keycard: &accounts.Keycard{},
|
2023-02-21 12:35:26 +00:00
|
|
|
}
|
|
|
|
kcAction.Keycard.FromSyncKeycard(syncMessage.Keycard)
|
|
|
|
|
|
|
|
switch syncMessage.Action {
|
|
|
|
case protobuf.SyncKeycardAction_KEYCARD_ADDED,
|
|
|
|
protobuf.SyncKeycardAction_ACCOUNTS_ADDED:
|
2023-05-09 18:48:56 +00:00
|
|
|
_, _, err = m.settings.AddKeycardOrAddAccountsIfKeycardIsAdded(*kcAction.Keycard)
|
2023-02-21 12:35:26 +00:00
|
|
|
case protobuf.SyncKeycardAction_KEYCARD_DELETED:
|
|
|
|
err = m.settings.DeleteKeycard(kcAction.Keycard.KeycardUID, kcAction.Keycard.LastUpdateClock)
|
|
|
|
case protobuf.SyncKeycardAction_ACCOUNTS_REMOVED:
|
|
|
|
err = m.settings.RemoveMigratedAccountsForKeycard(kcAction.Keycard.KeycardUID, kcAction.Keycard.AccountsAddresses,
|
|
|
|
kcAction.Keycard.LastUpdateClock)
|
|
|
|
case protobuf.SyncKeycardAction_LOCKED:
|
|
|
|
err = m.settings.KeycardLocked(kcAction.Keycard.KeycardUID, kcAction.Keycard.LastUpdateClock)
|
|
|
|
case protobuf.SyncKeycardAction_UNLOCKED:
|
|
|
|
err = m.settings.KeycardUnlocked(kcAction.Keycard.KeycardUID, kcAction.Keycard.LastUpdateClock)
|
|
|
|
case protobuf.SyncKeycardAction_UID_UPDATED:
|
|
|
|
err = m.settings.UpdateKeycardUID(kcAction.OldKeycardUID, kcAction.Keycard.KeycardUID,
|
|
|
|
kcAction.Keycard.LastUpdateClock)
|
|
|
|
case protobuf.SyncKeycardAction_NAME_CHANGED:
|
|
|
|
err = m.settings.SetKeycardName(kcAction.Keycard.KeycardUID, kcAction.Keycard.KeycardName,
|
|
|
|
kcAction.Keycard.LastUpdateClock)
|
|
|
|
default:
|
|
|
|
panic("unknown action for handling keycard activity")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Response.AddKeycardAction(kcAction)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
func (m *Messenger) AddKeycardOrAddAccountsIfKeycardIsAdded(ctx context.Context, kp *accounts.Keycard) (added bool, err error) {
|
2023-05-09 18:48:56 +00:00
|
|
|
addedKc, addedAccs, err := m.settings.AddKeycardOrAddAccountsIfKeycardIsAdded(*kp)
|
2023-02-21 12:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return addedKc || addedAccs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
activityMessage := protobuf.SyncKeycardAction{
|
|
|
|
Keycard: kp.ToSyncKeycard(),
|
|
|
|
}
|
|
|
|
if addedKc {
|
|
|
|
activityMessage.Action = protobuf.SyncKeycardAction_KEYCARD_ADDED
|
|
|
|
} else if addedAccs {
|
|
|
|
activityMessage.Action = protobuf.SyncKeycardAction_ACCOUNTS_ADDED
|
|
|
|
}
|
|
|
|
|
|
|
|
return addedKc || addedAccs, m.dispatchKeycardActivity(ctx, activityMessage)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:43:05 +00:00
|
|
|
func (m *Messenger) RemoveMigratedAccountsForKeycard(ctx context.Context, kcUID string, addresses []types.Address, clock uint64) error {
|
2023-02-21 12:35:26 +00:00
|
|
|
|
|
|
|
err := m.settings.RemoveMigratedAccountsForKeycard(kcUID, addresses, clock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activityMessage := protobuf.SyncKeycardAction{
|
|
|
|
Action: protobuf.SyncKeycardAction_ACCOUNTS_REMOVED,
|
|
|
|
Keycard: &protobuf.SyncKeycard{
|
|
|
|
Uid: kcUID,
|
|
|
|
Clock: clock,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
activityMessage.Keycard.Addresses = append(activityMessage.Keycard.Addresses, addr.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.dispatchKeycardActivity(ctx, activityMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SetKeycardName(ctx context.Context, kcUID string, kpName string, clock uint64) error {
|
|
|
|
err := m.settings.SetKeycardName(kcUID, kpName, clock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activityMessage := protobuf.SyncKeycardAction{
|
|
|
|
Action: protobuf.SyncKeycardAction_NAME_CHANGED,
|
|
|
|
Keycard: &protobuf.SyncKeycard{
|
|
|
|
Uid: kcUID,
|
|
|
|
Name: kpName,
|
|
|
|
Clock: clock,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.dispatchKeycardActivity(ctx, activityMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) KeycardLocked(ctx context.Context, kcUID string, clock uint64) error {
|
|
|
|
err := m.settings.KeycardLocked(kcUID, clock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activityMessage := protobuf.SyncKeycardAction{
|
|
|
|
Action: protobuf.SyncKeycardAction_LOCKED,
|
|
|
|
Keycard: &protobuf.SyncKeycard{
|
|
|
|
Uid: kcUID,
|
|
|
|
Clock: clock,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.dispatchKeycardActivity(ctx, activityMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) KeycardUnlocked(ctx context.Context, kcUID string, clock uint64) error {
|
|
|
|
err := m.settings.KeycardUnlocked(kcUID, clock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activityMessage := protobuf.SyncKeycardAction{
|
|
|
|
Action: protobuf.SyncKeycardAction_UNLOCKED,
|
|
|
|
Keycard: &protobuf.SyncKeycard{
|
|
|
|
Uid: kcUID,
|
|
|
|
Clock: clock,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.dispatchKeycardActivity(ctx, activityMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DeleteKeycard(ctx context.Context, kcUID string, clock uint64) error {
|
|
|
|
err := m.settings.DeleteKeycard(kcUID, clock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activityMessage := protobuf.SyncKeycardAction{
|
|
|
|
Action: protobuf.SyncKeycardAction_KEYCARD_DELETED,
|
|
|
|
Keycard: &protobuf.SyncKeycard{
|
|
|
|
Uid: kcUID,
|
|
|
|
Clock: clock,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.dispatchKeycardActivity(ctx, activityMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) UpdateKeycardUID(ctx context.Context, oldKcUID string, newKcUID string, clock uint64) error {
|
|
|
|
err := m.settings.UpdateKeycardUID(oldKcUID, newKcUID, clock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activityMessage := protobuf.SyncKeycardAction{
|
|
|
|
Action: protobuf.SyncKeycardAction_UID_UPDATED,
|
|
|
|
OldKeycardUid: oldKcUID,
|
|
|
|
Keycard: &protobuf.SyncKeycard{
|
|
|
|
Uid: newKcUID,
|
|
|
|
Clock: clock,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.dispatchKeycardActivity(ctx, activityMessage)
|
|
|
|
}
|