status-go/server/pairing/raw_message_handler.go
Samuel Hawksby-Robinson 465afd0131 Refactored BasePayloadReceiver to handle Receive()
Additionally to allow this process flow I refactored RawMessagePayloadReceiver and InstallationPayloadHandler to use a dedicated Marshaller type. Also added a fix to struct extention functionality, we want to ignore the process if there is no public key because that will key encoding. Seems an unnecessary bug to have to handle when you know there is no key.
2023-04-04 11:56:40 +01:00

122 lines
3.7 KiB
Go

package pairing
import (
"context"
"fmt"
"path/filepath"
"github.com/status-im/status-go/api"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol/protobuf"
)
type SyncRawMessageHandler struct {
backend *api.GethStatusBackend
}
func NewSyncRawMessageHandler(backend *api.GethStatusBackend) *SyncRawMessageHandler {
return &SyncRawMessageHandler{backend: backend}
}
func (s *SyncRawMessageHandler) CollectInstallationData(rawMessageCollector *RawMessageCollector, deviceType string) error {
// TODO Could this function be part of the installation data exchange flow?
// https://github.com/status-im/status-go/issues/3304
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
}
func (s *SyncRawMessageHandler) PrepareRawMessage(keyUID, deviceType string) (rm []*protobuf.RawMessage, as []*accounts.Account, syncSettings *settings.Settings, err error) {
syncSettings = new(settings.Settings)
messenger := s.backend.Messenger()
if messenger == nil {
return nil, nil, nil, fmt.Errorf("messenger is nil when PrepareRawMessage")
}
currentAccount, err := s.backend.GetActiveAccount()
if err != nil {
return
}
if keyUID != currentAccount.KeyUID {
return nil, nil, nil, fmt.Errorf("keyUID not equal")
}
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 {
return
}
err = s.CollectInstallationData(rawMessageCollector, deviceType)
if err != nil {
return
}
rsm := rawMessageCollector.convertToSyncRawMessage()
rm = rsm.RawMessages
accountService := s.backend.StatusNode().AccountService()
as, err = accountService.GetAccountsByKeyUID(keyUID)
if err != nil {
return
}
*syncSettings, err = accountService.GetSettings()
if err != nil {
return
}
return
}
func (s *SyncRawMessageHandler) HandleRawMessage(accountPayload *AccountPayload, nodeConfig *params.NodeConfig, settingCurrentNetwork, deviceType string, rmp *RawMessagesPayload) (err error) {
account := accountPayload.multiaccount
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
}
rmp.setting.InstallationID = nodeConfig.ShhextConfig.InstallationID
rmp.setting.CurrentNetwork = settingCurrentNetwork
err = s.backend.StartNodeWithAccountAndInitialConfig(*account, accountPayload.password, *rmp.setting, nodeConfig, rmp.subAccounts)
}
if err != nil {
return err
}
}
messenger := s.backend.Messenger()
if messenger == nil {
return fmt.Errorf("messenger is nil when HandleRawMessage")
}
err = messenger.SetInstallationDeviceType(deviceType)
if err != nil {
return err
}
return messenger.HandleSyncRawMessages(rmp.rawMessages)
}