2023-01-06 12:21:14 +00:00
|
|
|
package pairing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-26 11:14:27 +00:00
|
|
|
"crypto/ecdsa"
|
2023-01-06 12:21:14 +00:00
|
|
|
"fmt"
|
2024-06-26 11:14:27 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2023-01-06 12:21:14 +00:00
|
|
|
|
|
|
|
"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"
|
2024-08-14 20:10:19 +00:00
|
|
|
"github.com/status-im/status-go/protocol/encryption/multidevice"
|
2023-01-06 12:21:14 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2024-08-14 20:10:19 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
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
|
|
|
|
}
|
2024-10-07 14:05:37 +00:00
|
|
|
_, err = messenger.SendPairInstallation(context.TODO(), "", rawMessageCollector.dispatchMessage)
|
2023-02-28 12:32:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-14 20:10:19 +00:00
|
|
|
func (s *SyncRawMessageHandler) HandleRawMessage(
|
|
|
|
accountPayload *AccountPayload,
|
|
|
|
createAccountRequest *requests.CreateAccount,
|
|
|
|
deviceType string,
|
|
|
|
rmp *RawMessagesPayload) (err error) {
|
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
|
2024-08-14 20:10:19 +00:00
|
|
|
err = s.login(accountPayload, createAccountRequest, rmp)
|
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
|
|
|
}
|
2024-08-14 20:10:19 +00:00
|
|
|
|
|
|
|
func (s *SyncRawMessageHandler) login(accountPayload *AccountPayload, createAccountRequest *requests.CreateAccount, rmp *RawMessagesPayload) error {
|
|
|
|
account := accountPayload.multiaccount
|
|
|
|
installationID := multidevice.GenerateInstallationID()
|
|
|
|
nodeConfig, err := api.DefaultNodeConfig(installationID, createAccountRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keystoreRelativePath, err := s.backend.InitKeyStoreDirWithAccount(nodeConfig.RootDataDir, account.KeyUID)
|
|
|
|
nodeConfig.KeyStoreDir = keystoreRelativePath
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chatKey *ecdsa.PrivateKey
|
|
|
|
if accountPayload.chatKey != "" {
|
|
|
|
chatKeyHex := strings.Trim(accountPayload.chatKey, "0x")
|
|
|
|
chatKey, err = ethcrypto.HexToECDSA(chatKeyHex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if accountPayload.exist {
|
|
|
|
return s.backend.StartNodeWithAccount(*account, accountPayload.password, nodeConfig, chatKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override some of received settings
|
|
|
|
rmp.setting.DeviceName = createAccountRequest.DeviceName
|
|
|
|
rmp.setting.InstallationID = installationID
|
|
|
|
rmp.setting.CurrentNetwork = api.DefaultCurrentNetwork
|
|
|
|
|
|
|
|
return s.backend.StartNodeWithAccountAndInitialConfig(
|
|
|
|
*account,
|
|
|
|
accountPayload.password,
|
|
|
|
*rmp.setting,
|
|
|
|
nodeConfig,
|
|
|
|
rmp.profileKeypair.Accounts,
|
|
|
|
chatKey,
|
|
|
|
)
|
|
|
|
}
|