status-go/protocol/messenger_sync_wallets_test.go

622 lines
24 KiB
Go
Raw Permalink Normal View History

2022-05-18 10:42:51 +00:00
package protocol
import (
"context"
"errors"
"testing"
"github.com/status-im/status-go/eth-node/types"
2022-05-18 10:42:51 +00:00
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/tt"
"github.com/stretchr/testify/suite"
)
func TestMessengerSyncWalletSuite(t *testing.T) {
suite.Run(t, new(MessengerSyncWalletSuite))
}
type MessengerSyncWalletSuite struct {
2023-07-13 10:28:34 +00:00
MessengerBaseTestSuite
2022-05-18 10:42:51 +00:00
}
// user should not be able to change a keypair name directly, it follows display name
func (s *MessengerSyncWalletSuite) TestProfileKeypairNameChange() {
profileKp := accounts.GetProfileKeypairForTest(true, false, false)
profileKp.KeyUID = s.m.account.KeyUID
profileKp.Name = s.m.account.Name
profileKp.Accounts[0].KeyUID = s.m.account.KeyUID
// Create a main account on alice
err := s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err, "profile keypair alice.settings.SaveOrUpdateKeypair")
// Check account is present in the db
dbProfileKp, err := s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp))
// Try to change profile keypair name using `SaveOrUpdateKeypair` function
profileKp1 := accounts.GetProfileKeypairForTest(true, false, false)
profileKp1.Name = profileKp1.Name + "updated"
profileKp1.KeyUID = s.m.account.KeyUID
profileKp1.Accounts[0].KeyUID = s.m.account.KeyUID
err = s.m.SaveOrUpdateKeypair(profileKp1)
s.Require().Error(err)
s.Require().True(err == ErrCannotChangeKeypairName)
// Check the db
dbProfileKp, err = s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp))
// Try to change profile keypair name using `UpdateKeypairName` function
err = s.m.UpdateKeypairName(profileKp1.KeyUID, profileKp1.Name)
s.Require().Error(err)
s.Require().True(err == ErrCannotChangeKeypairName)
// Check the db
dbProfileKp, err = s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp))
}
func (s *MessengerSyncWalletSuite) TestSyncWallets() {
profileKp := accounts.GetProfileKeypairForTest(true, true, true)
// set clocks for accounts
profileKp.Clock = uint64(len(profileKp.Accounts) - 1)
for i, acc := range profileKp.Accounts {
acc.Clock = uint64(i)
}
2022-05-18 10:42:51 +00:00
// Create a main account on alice
err := s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err, "profile keypair alice.settings.SaveOrUpdateKeypair")
2022-05-18 10:42:51 +00:00
// Check account is present in the db
dbProfileKp1, err := s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp1))
2022-05-18 10:42:51 +00:00
// Create new device and add main account to
alicesOtherDevice, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, nil)
s.Require().NoError(err)
// Store only chat and default wallet account on other device
profileKpOtherDevice := accounts.GetProfileKeypairForTest(true, true, false)
err = alicesOtherDevice.settings.SaveOrUpdateKeypair(profileKpOtherDevice)
s.Require().NoError(err, "profile keypair alicesOtherDevice.settings.SaveOrUpdateKeypair")
2022-05-18 10:42:51 +00:00
// Check account is present in the db
dbProfileKp2, err := alicesOtherDevice.settings.GetKeypairByKeyUID(profileKpOtherDevice.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(profileKpOtherDevice, dbProfileKp2))
2022-05-18 10:42:51 +00:00
// Pair devices
im1 := &multidevice.InstallationMetadata{
Name: "alice's-other-device",
DeviceType: "alice's-other-device-type",
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), nil)
2022-05-18 10:42:51 +00:00
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
s.Require().False(response.Chats()[0].Active)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
func(r *MessengerResponse) bool { return len(r.Installations()) > 0 },
2022-05-18 10:42:51 +00:00
"installation not received",
)
s.Require().NoError(err)
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
actualInstallation := response.Installations()[0]
2022-05-18 10:42:51 +00:00
s.Require().Equal(alicesOtherDevice.installationID, actualInstallation.ID)
s.Require().NotNil(actualInstallation.InstallationMetadata)
s.Require().Equal("alice's-other-device", actualInstallation.InstallationMetadata.Name)
s.Require().Equal("alice's-other-device-type", actualInstallation.InstallationMetadata.DeviceType)
err = s.m.EnableInstallation(alicesOtherDevice.installationID)
s.Require().NoError(err)
// Store seed phrase keypair with accounts on alice's device
seedPhraseKp := accounts.GetSeedImportedKeypair1ForTest()
err = s.m.settings.SaveOrUpdateKeypair(seedPhraseKp)
s.Require().NoError(err, "seed phrase keypair alice.settings.SaveOrUpdateKeypair")
dbSeedPhraseKp1, err := s.m.settings.GetKeypairByKeyUID(seedPhraseKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(seedPhraseKp, dbSeedPhraseKp1))
// Store private key keypair with accounts on alice's device
privKeyKp := accounts.GetPrivKeyImportedKeypairForTest()
err = s.m.settings.SaveOrUpdateKeypair(privKeyKp)
s.Require().NoError(err, "private key keypair alice.settings.SaveOrUpdateKeypair")
dbPrivKeyKp1, err := s.m.settings.GetKeypairByKeyUID(privKeyKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairs(privKeyKp, dbPrivKeyKp1))
// Store watch only accounts on alice's device
woAccounts := accounts.GetWatchOnlyAccountsForTest()
err = s.m.settings.SaveOrUpdateAccounts(woAccounts, false)
s.Require().NoError(err)
dbWoAccounts1, err := s.m.settings.GetActiveWatchOnlyAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbWoAccounts1))
s.Require().True(haveSameElements(woAccounts, dbWoAccounts1, accounts.SameAccounts))
dbAccounts1, err := s.m.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(profileKp.Accounts)+len(seedPhraseKp.Accounts)+len(privKeyKp.Accounts)+len(woAccounts), len(dbAccounts1))
2022-05-18 10:42:51 +00:00
// Trigger's a sync between devices
err = s.m.SyncDevices(context.Background(), "ens-name", "profile-image", nil)
2022-05-18 10:42:51 +00:00
s.Require().NoError(err)
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
2022-05-18 10:42:51 +00:00
if err != nil {
return err
}
if len(response.Keypairs) != 3 || // 3 keypairs (profile, seed, priv key)
len(response.WatchOnlyAccounts) != len(woAccounts) {
2022-05-18 10:42:51 +00:00
return errors.New("no sync wallet account received")
}
return nil
})
s.Require().NoError(err)
dbProfileKp2, err = alicesOtherDevice.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().True(profileKp.KeyUID == dbProfileKp2.KeyUID &&
profileKp.Name == dbProfileKp2.Name &&
profileKp.Type == dbProfileKp2.Type &&
profileKp.DerivedFrom == dbProfileKp2.DerivedFrom &&
profileKp.LastUsedDerivationIndex == dbProfileKp2.LastUsedDerivationIndex &&
profileKp.Clock == dbProfileKp2.Clock &&
len(profileKp.Accounts) == len(dbProfileKp2.Accounts))
// chat and default wallet account should be fully operable, other accounts partially operable
for i := range profileKp.Accounts {
match := false
expectedOperableValue := accounts.AccountPartiallyOperable
if profileKp.Accounts[i].Chat || profileKp.Accounts[i].Wallet {
expectedOperableValue = accounts.AccountFullyOperable
}
for j := range dbProfileKp2.Accounts {
if accounts.SameAccountsWithDifferentOperable(profileKp.Accounts[i], dbProfileKp2.Accounts[j], expectedOperableValue) {
match = true
break
}
}
s.Require().True(match)
}
dbSeedPhraseKp2, err := alicesOtherDevice.settings.GetKeypairByKeyUID(seedPhraseKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairsWithDifferentSyncedFrom(seedPhraseKp, dbSeedPhraseKp2, true, "", accounts.AccountNonOperable))
2022-05-18 10:42:51 +00:00
dbPrivKeyKp2, err := alicesOtherDevice.settings.GetKeypairByKeyUID(privKeyKp.KeyUID)
s.Require().NoError(err)
s.Require().True(accounts.SameKeypairsWithDifferentSyncedFrom(privKeyKp, dbPrivKeyKp2, true, "", accounts.AccountNonOperable))
dbWoAccounts2, err := alicesOtherDevice.settings.GetActiveWatchOnlyAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbWoAccounts2))
s.Require().True(haveSameElements(woAccounts, dbWoAccounts2, accounts.SameAccounts))
dbAccounts2, err := alicesOtherDevice.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(profileKp.Accounts)+len(seedPhraseKp.Accounts)+len(privKeyKp.Accounts)+len(woAccounts), len(dbAccounts2))
s.Require().True(haveSameElements(dbAccounts1, dbAccounts2, accounts.SameAccounts))
// Update keypair name on alice's primary device
profileKpUpdated := accounts.GetProfileKeypairForTest(true, true, false)
profileKpUpdated.Name = profileKp.Name + "Updated"
profileKpUpdated.Accounts = profileKp.Accounts[:0]
err = s.m.SaveOrUpdateKeypair(profileKpUpdated)
s.Require().NoError(err, "updated keypair name on alice primary device")
2022-05-18 10:42:51 +00:00
// Sync between devices is triggered automatically
// via watch account changes subscription
// Retrieve community link & community
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
2022-05-18 10:42:51 +00:00
if err != nil {
return err
}
if len(response.Keypairs) != 1 {
return errors.New("no sync keypairs received")
2022-05-18 10:42:51 +00:00
}
return nil
})
s.Require().NoError(err)
// check on alice's other device
dbProfileKp2, err = alicesOtherDevice.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
s.Require().Equal(profileKpUpdated.Name, dbProfileKp2.Name)
// Update accounts on alice's primary device
profileKpUpdated = accounts.GetProfileKeypairForTest(true, true, true)
accountsToUpdate := profileKpUpdated.Accounts[2:]
for _, acc := range accountsToUpdate {
acc.Name = acc.Name + "Updated"
acc.ColorID = acc.ColorID + "Updated"
acc.Emoji = acc.Emoji + "Updated"
err = s.m.SaveOrUpdateAccount(acc)
s.Require().NoError(err, "updated account on alice primary device")
}
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
if err != nil {
return err
}
if len(response.Keypairs) != 2 {
return errors.New("no sync keypairs received")
2022-05-18 10:42:51 +00:00
}
return nil
})
s.Require().NoError(err)
// check on alice's other device
dbProfileKp2, err = alicesOtherDevice.settings.GetKeypairByKeyUID(profileKp.KeyUID)
s.Require().NoError(err)
for _, acc := range accountsToUpdate {
s.Require().True(contains(dbProfileKp2.Accounts, acc, accounts.SameAccounts))
2022-05-18 10:42:51 +00:00
}
}
func (s *MessengerSyncWalletSuite) TestSyncWalletAccountsReorder() {
profileKp := accounts.GetProfileKeypairForTest(true, false, false)
profileKp.Accounts[0].Position = -1 // Chat account must be at position -1 always
woAccounts := []*accounts.Account{
{Address: types.Address{0x11}, Type: accounts.AccountTypeWatch, Position: 0},
{Address: types.Address{0x12}, Type: accounts.AccountTypeWatch, Position: 1},
{Address: types.Address{0x13}, Type: accounts.AccountTypeWatch, Position: 2},
{Address: types.Address{0x14}, Type: accounts.AccountTypeWatch, Position: 3},
{Address: types.Address{0x15}, Type: accounts.AccountTypeWatch, Position: 4},
{Address: types.Address{0x16}, Type: accounts.AccountTypeWatch, Position: 5},
}
// Create a main account on alice
err := s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err, "profile keypair alice.settings.SaveOrUpdateKeypair")
// Store watch only accounts on alice's device
err = s.m.settings.SaveOrUpdateAccounts(woAccounts, false)
s.Require().NoError(err, "wo accounts alice.settings.SaveOrUpdateKeypair")
dbAccounts, err := s.m.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbAccounts)-1)
// Create a main account on alice's other device
alicesOtherDevice, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, nil)
s.Require().NoError(err)
err = alicesOtherDevice.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err, "profile keypair alice.settings.SaveOrUpdateKeypair")
// Store watch only accounts on alice's other device
err = alicesOtherDevice.settings.SaveOrUpdateAccounts(woAccounts, false)
s.Require().NoError(err, "wo accounts alice.settings.SaveOrUpdateKeypair")
dbAccounts, err = alicesOtherDevice.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbAccounts)-1)
// Pair devices
im1 := &multidevice.InstallationMetadata{
Name: "alice's-other-device",
DeviceType: "alice's-other-device-type",
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
s.Require().False(response.Chats()[0].Active)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
func(r *MessengerResponse) bool { return len(r.Installations()) > 0 },
"installation not received",
)
s.Require().NoError(err)
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
actualInstallation := response.Installations()[0]
s.Require().Equal(alicesOtherDevice.installationID, actualInstallation.ID)
s.Require().NotNil(actualInstallation.InstallationMetadata)
s.Require().Equal("alice's-other-device", actualInstallation.InstallationMetadata.Name)
s.Require().Equal("alice's-other-device-type", actualInstallation.InstallationMetadata.DeviceType)
err = s.m.EnableInstallation(alicesOtherDevice.installationID)
s.Require().NoError(err)
// Move down account from position 1 to position 4
err = s.m.MoveWalletAccount(1, 4)
s.Require().NoError(err)
// Expected after moving down
woAccounts = []*accounts.Account{
{Address: types.Address{0x11}, Type: accounts.AccountTypeWatch, Position: 0},
{Address: types.Address{0x13}, Type: accounts.AccountTypeWatch, Position: 1},
{Address: types.Address{0x14}, Type: accounts.AccountTypeWatch, Position: 2},
{Address: types.Address{0x15}, Type: accounts.AccountTypeWatch, Position: 3},
{Address: types.Address{0x12}, Type: accounts.AccountTypeWatch, Position: 4}, // acc with addr 0x12 is at position 4 (moved from position 1)
{Address: types.Address{0x16}, Type: accounts.AccountTypeWatch, Position: 5},
}
dbAccounts, err = s.m.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbAccounts)-1)
for i := 0; i < len(woAccounts); i++ {
s.Require().True(accounts.SameAccountsIncludingPosition(woAccounts[i], dbAccounts[i+1]))
}
// Sync between devices is triggered automatically
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
if err != nil {
return err
}
if len(response.AccountsPositions) != len(woAccounts) {
return errors.New("no sync message received for accounts reordering")
}
return nil
})
s.Require().NoError(err)
// check on alice's other device
dbAccounts, err = alicesOtherDevice.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbAccounts)-1)
for i := 0; i < len(woAccounts); i++ {
s.Require().True(accounts.SameAccountsIncludingPosition(woAccounts[i], dbAccounts[i+1]))
}
// compare times
dbClock, err := s.m.settings.GetClockOfLastAccountsPositionChange()
s.Require().NoError(err)
dbClockOtherDevice, err := s.m.settings.GetClockOfLastAccountsPositionChange()
s.Require().NoError(err)
s.Require().Equal(dbClock, dbClockOtherDevice)
// Move up account from position 5 to position 0
err = s.m.MoveWalletAccount(5, 0)
s.Require().NoError(err)
// Expected after moving down
woAccounts = []*accounts.Account{
{Address: types.Address{0x16}, Type: accounts.AccountTypeWatch, Position: 0}, // acc with addr 0x16 is at position 0 (moved from position 5)
{Address: types.Address{0x11}, Type: accounts.AccountTypeWatch, Position: 1},
{Address: types.Address{0x13}, Type: accounts.AccountTypeWatch, Position: 2},
{Address: types.Address{0x14}, Type: accounts.AccountTypeWatch, Position: 3},
{Address: types.Address{0x15}, Type: accounts.AccountTypeWatch, Position: 4},
{Address: types.Address{0x12}, Type: accounts.AccountTypeWatch, Position: 5},
}
dbAccounts, err = s.m.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbAccounts)-1)
for i := 0; i < len(woAccounts); i++ {
s.Require().True(accounts.SameAccountsIncludingPosition(woAccounts[i], dbAccounts[i+1]))
}
// Sync between devices is triggered automatically
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
if err != nil {
return err
}
if len(response.AccountsPositions) != len(woAccounts) {
return errors.New("no sync message received for accounts reordering")
}
return nil
})
s.Require().NoError(err)
// check on alice's other device
dbAccounts, err = alicesOtherDevice.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(len(woAccounts), len(dbAccounts)-1)
for i := 0; i < len(woAccounts); i++ {
s.Require().True(accounts.SameAccountsIncludingPosition(woAccounts[i], dbAccounts[i+1]))
}
// compare times
dbClock, err = s.m.settings.GetClockOfLastAccountsPositionChange()
s.Require().NoError(err)
dbClockOtherDevice, err = s.m.settings.GetClockOfLastAccountsPositionChange()
s.Require().NoError(err)
s.Require().Equal(dbClock, dbClockOtherDevice)
}
func (s *MessengerSyncWalletSuite) TestSyncWalletAccountOrderAfterDeletion() {
profileKp := accounts.GetProfileKeypairForTest(true, true, true)
// set clocks for accounts
profileKp.Clock = uint64(len(profileKp.Accounts) - 1)
i := -1
for _, acc := range profileKp.Accounts {
acc.Clock = uint64(i + 1)
acc.Position = int64(i)
acc.Operable = accounts.AccountNonOperable
i++
}
// Create a main account on alice
err := s.m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err, "profile keypair alice.settings.SaveOrUpdateKeypair")
// Store seed phrase keypair with accounts on alice's device
seedPhraseKp := accounts.GetSeedImportedKeypair1ForTest()
for _, acc := range seedPhraseKp.Accounts {
acc.Clock = uint64(i + 1)
acc.Position = int64(i)
acc.Operable = accounts.AccountNonOperable
i++
}
err = s.m.settings.SaveOrUpdateKeypair(seedPhraseKp)
s.Require().NoError(err, "seed phrase keypair alice.settings.SaveOrUpdateKeypair")
// Store private key keypair with accounts on alice's device
privKeyKp := accounts.GetPrivKeyImportedKeypairForTest()
for _, acc := range privKeyKp.Accounts {
acc.Clock = uint64(i + 1)
acc.Position = int64(i)
acc.Operable = accounts.AccountNonOperable
i++
}
err = s.m.settings.SaveOrUpdateKeypair(privKeyKp)
s.Require().NoError(err, "private key keypair alice.settings.SaveOrUpdateKeypair")
// Store watch only accounts on alice's device
woAccounts := accounts.GetWatchOnlyAccountsForTest()
for _, acc := range woAccounts {
acc.Clock = uint64(i + 1)
acc.Position = int64(i)
acc.Operable = accounts.AccountFullyOperable
i++
}
err = s.m.settings.SaveOrUpdateAccounts(woAccounts, false)
s.Require().NoError(err)
// Check accounts
dbAccounts1, err := s.m.settings.GetActiveAccounts()
s.Require().NoError(err)
totalNumOfAccounts := len(profileKp.Accounts) + len(seedPhraseKp.Accounts) + len(privKeyKp.Accounts) + len(woAccounts)
s.Require().Equal(totalNumOfAccounts, len(dbAccounts1))
// Create new device and add main account to
alicesOtherDevice, err := newMessengerWithKey(s.shh, s.m.identity, s.logger, nil)
s.Require().NoError(err)
// Store only chat and default wallet account on other device
profileKpOtherDevice := accounts.GetProfileKeypairForTest(true, true, false)
err = alicesOtherDevice.settings.SaveOrUpdateKeypair(profileKpOtherDevice)
s.Require().NoError(err, "profile keypair alicesOtherDevice.settings.SaveOrUpdateKeypair")
// Pair devices
im1 := &multidevice.InstallationMetadata{
Name: "alice's-other-device",
DeviceType: "alice's-other-device-type",
}
err = alicesOtherDevice.SetInstallationMetadata(alicesOtherDevice.installationID, im1)
s.Require().NoError(err)
response, err := alicesOtherDevice.SendPairInstallation(context.Background(), nil)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Chats(), 1)
s.Require().False(response.Chats()[0].Active)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
func(r *MessengerResponse) bool { return len(r.Installations()) > 0 },
"installation not received",
)
s.Require().NoError(err)
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
actualInstallation := response.Installations()[0]
s.Require().Equal(alicesOtherDevice.installationID, actualInstallation.ID)
s.Require().NotNil(actualInstallation.InstallationMetadata)
s.Require().Equal("alice's-other-device", actualInstallation.InstallationMetadata.Name)
s.Require().Equal("alice's-other-device-type", actualInstallation.InstallationMetadata.DeviceType)
err = s.m.EnableInstallation(alicesOtherDevice.installationID)
s.Require().NoError(err)
// Trigger's a sync between devices
err = s.m.SyncDevices(context.Background(), "ens-name", "profile-image", nil)
s.Require().NoError(err)
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
if err != nil {
return err
}
if len(response.Keypairs) != 3 || // 3 keypairs (profile, seed, priv key)
len(response.WatchOnlyAccounts) != len(woAccounts) ||
len(response.AccountsPositions) != totalNumOfAccounts-1 /* we don't include chat account in position ordering*/ {
return errors.New("no sync wallet account received")
}
return nil
})
s.Require().NoError(err)
dbAccounts2, err := alicesOtherDevice.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(totalNumOfAccounts, len(dbAccounts2))
s.Require().True(haveSameElements(dbAccounts1, dbAccounts2, accounts.SameAccountsIncludingPosition))
// Delete keypair related account on alice's primary device
accToDelete := seedPhraseKp.Accounts[1]
err = s.m.DeleteAccount(accToDelete.Address)
s.Require().NoError(err, "delete account on alice primary device")
totalNumOfAccounts-- //one acc less
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
if err != nil {
return err
}
if len(response.Keypairs) != 1 {
return errors.New("no sync keypairs received")
}
return nil
})
s.Require().NoError(err)
dbAccounts1, err = s.m.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(totalNumOfAccounts, len(dbAccounts1))
dbAccounts2, err = alicesOtherDevice.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(totalNumOfAccounts, len(dbAccounts2))
s.Require().True(haveSameElements(dbAccounts1, dbAccounts2, accounts.SameAccountsIncludingPosition))
// Delete watch only account on alice's primary device
accToDelete = woAccounts[1]
err = s.m.DeleteAccount(accToDelete.Address)
s.Require().NoError(err, "delete account on alice primary device")
totalNumOfAccounts-- //one acc less
err = tt.RetryWithBackOff(func() error {
response, err := alicesOtherDevice.RetrieveAll()
if err != nil {
return err
}
if len(response.WatchOnlyAccounts) != 1 {
return errors.New("no sync keypairs received")
}
return nil
})
s.Require().NoError(err)
dbAccounts1, err = s.m.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(totalNumOfAccounts, len(dbAccounts1))
dbAccounts2, err = alicesOtherDevice.settings.GetActiveAccounts()
s.Require().NoError(err)
s.Require().Equal(totalNumOfAccounts, len(dbAccounts2))
s.Require().True(haveSameElements(dbAccounts1, dbAccounts2, accounts.SameAccountsIncludingPosition))
}