2022-09-14 10:46:11 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2023-12-27 14:59:06 +00:00
|
|
|
multiAccCommon "github.com/status-im/status-go/multiaccounts/common"
|
2022-09-14 10:46:11 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2023-08-18 11:39:59 +00:00
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
2022-09-14 10:46:11 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Messenger) UpsertSavedAddress(ctx context.Context, sa wallet.SavedAddress) error {
|
|
|
|
updatedClock, err := m.savedAddressesManager.UpdateMetadataAndUpsertSavedAddress(sa)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-06 12:21:14 +00:00
|
|
|
return m.syncNewSavedAddress(ctx, &sa, updatedClock, m.dispatchMessage)
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 14:59:54 +00:00
|
|
|
func (m *Messenger) DeleteSavedAddress(ctx context.Context, address gethcommon.Address, isTest bool) error {
|
2023-03-06 13:26:46 +00:00
|
|
|
updateClock := uint64(time.Now().Unix())
|
2024-01-09 14:59:54 +00:00
|
|
|
_, err := m.savedAddressesManager.DeleteSavedAddress(address, isTest, updateClock)
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-09 14:59:54 +00:00
|
|
|
return m.syncDeletedSavedAddress(ctx, address, isTest, updateClock, m.dispatchMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) GetSavedAddresses(ctx context.Context) ([]wallet.SavedAddress, error) {
|
|
|
|
return m.savedAddressesManager.GetSavedAddresses()
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) garbageCollectRemovedSavedAddresses() error {
|
|
|
|
return m.savedAddressesManager.DeleteSoftRemovedSavedAddresses(uint64(time.Now().AddDate(0, 0, -30).Unix()))
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func (m *Messenger) dispatchSyncSavedAddress(ctx context.Context, syncMessage *protobuf.SyncSavedAddress, rawMessageHandler RawMessageHandler) error {
|
2022-09-14 10:46:11 +00:00
|
|
|
if !m.hasPairedDevices() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clock, chat := m.getLastClockWithRelatedChat()
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
encodedMessage, err := proto.Marshal(syncMessage)
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
rawMessage := common.RawMessage{
|
2022-09-14 10:46:11 +00:00
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_SAVED_ADDRESS,
|
|
|
|
ResendAutomatically: true,
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = rawMessageHandler(ctx, rawMessage)
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.LastClockValue = clock
|
|
|
|
return m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
func (m *Messenger) syncNewSavedAddress(ctx context.Context, savedAddress *wallet.SavedAddress, updateClock uint64, rawMessageHandler RawMessageHandler) error {
|
2023-08-18 11:39:59 +00:00
|
|
|
return m.dispatchSyncSavedAddress(ctx, &protobuf.SyncSavedAddress{
|
2023-03-06 13:26:46 +00:00
|
|
|
Address: savedAddress.Address.Bytes(),
|
|
|
|
Name: savedAddress.Name,
|
|
|
|
Removed: savedAddress.Removed,
|
|
|
|
UpdateClock: savedAddress.UpdateClock,
|
|
|
|
ChainShortNames: savedAddress.ChainShortNames,
|
|
|
|
Ens: savedAddress.ENSName,
|
|
|
|
IsTest: savedAddress.IsTest,
|
2023-12-27 14:59:06 +00:00
|
|
|
Color: string(savedAddress.ColorID),
|
2023-01-06 12:21:14 +00:00
|
|
|
}, rawMessageHandler)
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 14:59:54 +00:00
|
|
|
func (m *Messenger) syncDeletedSavedAddress(ctx context.Context, address gethcommon.Address, isTest bool, updateClock uint64, rawMessageHandler RawMessageHandler) error {
|
2023-08-18 11:39:59 +00:00
|
|
|
return m.dispatchSyncSavedAddress(ctx, &protobuf.SyncSavedAddress{
|
2022-09-14 10:46:11 +00:00
|
|
|
Address: address.Bytes(),
|
|
|
|
UpdateClock: updateClock,
|
|
|
|
Removed: true,
|
2023-03-06 13:26:46 +00:00
|
|
|
IsTest: isTest,
|
2023-01-06 12:21:14 +00:00
|
|
|
}, rawMessageHandler)
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
func (m *Messenger) syncSavedAddress(ctx context.Context, savedAddress wallet.SavedAddress, rawMessageHandler RawMessageHandler) (err error) {
|
2022-09-14 10:46:11 +00:00
|
|
|
if savedAddress.Removed {
|
2024-01-09 14:59:54 +00:00
|
|
|
if err = m.syncDeletedSavedAddress(ctx, savedAddress.Address, savedAddress.IsTest, savedAddress.UpdateClock, rawMessageHandler); err != nil {
|
2022-09-14 10:46:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-06 12:21:14 +00:00
|
|
|
if err = m.syncNewSavedAddress(ctx, &savedAddress, savedAddress.UpdateClock, rawMessageHandler); err != nil {
|
2022-09-14 10:46:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
func (m *Messenger) HandleSyncSavedAddress(state *ReceivedMessageState, syncMessage *protobuf.SyncSavedAddress, statusMessage *v1protocol.StatusMessage) (err error) {
|
2022-09-14 10:46:11 +00:00
|
|
|
address := gethcommon.BytesToAddress(syncMessage.Address)
|
|
|
|
if syncMessage.Removed {
|
2023-03-06 13:26:46 +00:00
|
|
|
_, err = m.savedAddressesManager.DeleteSavedAddress(
|
2024-01-09 14:59:54 +00:00
|
|
|
address, syncMessage.IsTest, syncMessage.UpdateClock)
|
2022-09-14 10:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-06 13:26:46 +00:00
|
|
|
state.Response.AddSavedAddress(&wallet.SavedAddress{Address: address, ENSName: syncMessage.Ens, IsTest: syncMessage.IsTest})
|
2022-09-14 10:46:11 +00:00
|
|
|
} else {
|
|
|
|
sa := wallet.SavedAddress{
|
2023-03-06 13:26:46 +00:00
|
|
|
Address: address,
|
|
|
|
Name: syncMessage.Name,
|
|
|
|
ChainShortNames: syncMessage.ChainShortNames,
|
|
|
|
ENSName: syncMessage.Ens,
|
|
|
|
IsTest: syncMessage.IsTest,
|
2023-12-27 14:59:06 +00:00
|
|
|
ColorID: multiAccCommon.CustomizationColor(syncMessage.Color),
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.savedAddressesManager.AddSavedAddressIfNewerUpdate(sa, syncMessage.UpdateClock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-13 20:09:20 +00:00
|
|
|
state.Response.AddSavedAddress(&sa)
|
2022-09-14 10:46:11 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|