2023-01-06 12:21:14 +00:00
|
|
|
package pairing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/api"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2023-02-17 13:02:42 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2023-01-06 12:21:14 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2023-04-26 11:48:49 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/signal"
|
2023-01-06 12:21:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SyncRawMessageHandler struct {
|
|
|
|
backend *api.GethStatusBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSyncRawMessageHandler(backend *api.GethStatusBackend) *SyncRawMessageHandler {
|
|
|
|
return &SyncRawMessageHandler{backend: backend}
|
|
|
|
}
|
|
|
|
|
2023-02-28 12:32:45 +00:00
|
|
|
func (s *SyncRawMessageHandler) CollectInstallationData(rawMessageCollector *RawMessageCollector, deviceType string) error {
|
2023-03-23 11:44:15 +00:00
|
|
|
// TODO Could this function be part of the installation data exchange flow?
|
|
|
|
// https://github.com/status-im/status-go/issues/3304
|
2023-02-28 12:32:45 +00:00
|
|
|
messenger := s.backend.Messenger()
|
|
|
|
if messenger == nil {
|
|
|
|
return fmt.Errorf("messenger is nil when CollectInstallationData")
|
|
|
|
}
|
|
|
|
err := messenger.SetInstallationDeviceType(deviceType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = messenger.SendPairInstallation(context.TODO(), rawMessageCollector.dispatchMessage)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
func (s *SyncRawMessageHandler) PrepareRawMessage(keyUID, deviceType string) (rm []*protobuf.RawMessage, kp *accounts.Keypair, syncSettings *settings.Settings, err error) {
|
2023-04-02 23:08:29 +00:00
|
|
|
syncSettings = new(settings.Settings)
|
2023-01-06 12:21:14 +00:00
|
|
|
messenger := s.backend.Messenger()
|
|
|
|
if messenger == nil {
|
2023-04-02 23:08:29 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("messenger is nil when PrepareRawMessage")
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentAccount, err := s.backend.GetActiveAccount()
|
|
|
|
if err != nil {
|
2023-03-29 17:12:27 +00:00
|
|
|
return
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
if keyUID != currentAccount.KeyUID {
|
2023-04-02 23:08:29 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("keyUID not equal")
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messenger.SetLocalPairing(true)
|
|
|
|
defer func() {
|
|
|
|
messenger.SetLocalPairing(false)
|
|
|
|
}()
|
|
|
|
rawMessageCollector := new(RawMessageCollector)
|
|
|
|
err = messenger.SyncDevices(context.TODO(), currentAccount.Name, currentAccount.Identicon, rawMessageCollector.dispatchMessage)
|
|
|
|
if err != nil {
|
2023-03-29 17:12:27 +00:00
|
|
|
return
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
2023-02-28 12:32:45 +00:00
|
|
|
|
|
|
|
err = s.CollectInstallationData(rawMessageCollector, deviceType)
|
|
|
|
if err != nil {
|
2023-03-29 17:12:27 +00:00
|
|
|
return
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
rsm := rawMessageCollector.convertToSyncRawMessage()
|
|
|
|
rm = rsm.RawMessages
|
2023-02-28 12:32:45 +00:00
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
accountService := s.backend.StatusNode().AccountService()
|
2023-04-02 23:08:29 +00:00
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
kp, err = accountService.GetKeypairByKeyUID(keyUID)
|
2023-01-06 12:21:14 +00:00
|
|
|
if err != nil {
|
2023-03-29 17:12:27 +00:00
|
|
|
return
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
2023-04-02 23:08:29 +00:00
|
|
|
*syncSettings, err = accountService.GetSettings()
|
2023-01-06 12:21:14 +00:00
|
|
|
if err != nil {
|
2023-03-29 17:12:27 +00:00
|
|
|
return
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
2023-02-06 09:55:41 +00:00
|
|
|
|
2023-03-29 17:12:27 +00:00
|
|
|
return
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 23:08:29 +00:00
|
|
|
func (s *SyncRawMessageHandler) HandleRawMessage(accountPayload *AccountPayload, nodeConfig *params.NodeConfig, settingCurrentNetwork, deviceType string, rmp *RawMessagesPayload) (err error) {
|
2023-02-28 12:32:45 +00:00
|
|
|
account := accountPayload.multiaccount
|
2023-01-06 12:21:14 +00:00
|
|
|
|
2023-02-28 12:32:45 +00:00
|
|
|
activeAccount, _ := s.backend.GetActiveAccount()
|
|
|
|
if activeAccount == nil { // not login yet
|
|
|
|
s.backend.UpdateRootDataDir(nodeConfig.RootDataDir)
|
|
|
|
// because client don't know keyUID before received data, we need help client to update keystore dir
|
|
|
|
keystoreDir := filepath.Join(nodeConfig.KeyStoreDir, account.KeyUID)
|
|
|
|
nodeConfig.KeyStoreDir = keystoreDir
|
|
|
|
if accountPayload.exist {
|
|
|
|
err = s.backend.StartNodeWithAccount(*account, accountPayload.password, nodeConfig)
|
|
|
|
} else {
|
|
|
|
accountManager := s.backend.AccountManager()
|
|
|
|
err = accountManager.InitKeystore(filepath.Join(nodeConfig.RootDataDir, keystoreDir))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-02 23:08:29 +00:00
|
|
|
rmp.setting.InstallationID = nodeConfig.ShhextConfig.InstallationID
|
|
|
|
rmp.setting.CurrentNetwork = settingCurrentNetwork
|
2023-02-06 09:55:41 +00:00
|
|
|
|
2023-05-16 10:48:00 +00:00
|
|
|
err = s.backend.StartNodeWithAccountAndInitialConfig(*account, accountPayload.password, *rmp.setting, nodeConfig, rmp.profileKeypair.Accounts)
|
2023-02-28 12:32:45 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messenger := s.backend.Messenger()
|
2023-01-11 11:30:54 +00:00
|
|
|
if messenger == nil {
|
|
|
|
return fmt.Errorf("messenger is nil when HandleRawMessage")
|
|
|
|
}
|
2023-02-28 12:32:45 +00:00
|
|
|
err = messenger.SetInstallationDeviceType(deviceType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-26 11:48:49 +00:00
|
|
|
|
|
|
|
installations := GetMessengerInstallationsMap(messenger)
|
|
|
|
|
|
|
|
err = messenger.HandleSyncRawMessages(rmp.rawMessages)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if newInstallation := FindNewInstallations(messenger, installations); newInstallation != nil {
|
|
|
|
signal.SendLocalPairingEvent(Event{
|
|
|
|
Type: EventReceivedInstallation,
|
|
|
|
Action: ActionPairingInstallation,
|
|
|
|
Data: newInstallation})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|