fix: changes needed to support recovering from waku in case recovering is done using keycard, but stored data refers to a regular profile and vice versa

This commit is contained in:
Sale Djenic 2023-07-27 15:20:40 +02:00 committed by saledjenic
parent 10a42e639d
commit 4088edfa14
4 changed files with 64 additions and 21 deletions

View File

@ -1372,7 +1372,15 @@ func (b *GethStatusBackend) saveAccountsAndSettings(settings settings.Settings,
LastUsedDerivationIndex: 0,
}
// When creating a new account, the chat account should have position -1, cause it doesn't participate
// in the wallet view and default wallet account should be at position 0.
for _, acc := range subaccs {
if acc.Chat {
acc.Position = -1
}
if acc.Wallet {
acc.Position = 0
}
acc.Operable = accounts.AccountFullyOperable
keypair.Accounts = append(keypair.Accounts, acc)
}

View File

@ -435,7 +435,8 @@ func (db *Database) getKeypairs(tx *sql.Tx, keyUID string, includeRemoved bool)
var (
rows *sql.Rows
err error
where string
mainQueryWhere string
subQueryWhere string
)
if tx == nil {
tx, err = db.db.Begin()
@ -452,12 +453,16 @@ func (db *Database) getKeypairs(tx *sql.Tx, keyUID string, includeRemoved bool)
}
if keyUID != "" {
where = "WHERE k.key_uid = ?"
mainQueryWhere = "WHERE k.key_uid = ?"
if !includeRemoved {
where += " AND k.removed = 0"
mainQueryWhere += " AND k.removed = 0"
}
} else if !includeRemoved {
where = "WHERE k.removed = 0"
mainQueryWhere = "WHERE k.removed = 0"
}
if !includeRemoved {
subQueryWhere = "WHERE removed = 0"
}
query := fmt.Sprintf( // nolint: gosec
@ -486,14 +491,13 @@ func (db *Database) getKeypairs(tx *sql.Tx, keyUID string, includeRemoved bool)
SELECT *
FROM
keypairs_accounts
WHERE
removed = 0
%s
) AS ka
ON
k.key_uid = ka.key_uid
%s
ORDER BY
ka.position`, where)
ka.position`, subQueryWhere, mainQueryWhere)
stmt, err := tx.Prepare(query)
if err != nil {

View File

@ -201,6 +201,16 @@ func (m *Messenger) handleKeypair(message *protobuf.SyncKeypair) error {
return nil
}
multiAcc, err := m.multiAccounts.GetAccount(message.KeyUid)
if err != nil {
return err
}
// If user is recovering his account via seed phrase, but the backed up messages indicate that the profile keypair
// is a keycard related profile, then we need to remove related profile keycards (only profile, other keycards should remain).
if multiAcc != nil && multiAcc.KeyUID == message.KeyUid && multiAcc.KeycardPairing == "" && len(message.Keycards) > 0 {
message.Keycards = []*protobuf.SyncKeycard{}
}
keypair, err := m.handleSyncKeypair(message)
if err != nil {
if err == ErrTryingToStoreOldKeypair {
@ -225,7 +235,7 @@ func (m *Messenger) handleWatchOnlyAccount(message *protobuf.SyncAccount) error
return nil
}
acc, err := m.handleSyncWatchOnlyAccount(message)
acc, err := m.handleSyncWatchOnlyAccount(message, true)
if err != nil {
if err == ErrTryingToStoreOldWalletAccount {
return nil

View File

@ -3085,7 +3085,7 @@ func (m *Messenger) resolveAccountOperability(syncAcc *protobuf.SyncAccount, syn
return accountsOperability, nil
}
func (m *Messenger) handleSyncWatchOnlyAccount(message *protobuf.SyncAccount) (*accounts.Account, error) {
func (m *Messenger) handleSyncWatchOnlyAccount(message *protobuf.SyncAccount, fromBackup bool) (*accounts.Account, error) {
if message.KeyUid != "" {
return nil, ErrNotWatchOnlyAccount
}
@ -3108,9 +3108,15 @@ func (m *Messenger) handleSyncWatchOnlyAccount(message *protobuf.SyncAccount) (*
if err != nil {
return nil, err
}
// if keypair is retrieved from backed up data, no need for resolving accounts positions
if !fromBackup {
err = m.settings.ResolveAccountsPositions(message.Clock)
if err != nil {
return nil, err
}
}
dbAccount.Removed = true
return dbAccount, err
return dbAccount, nil
}
}
@ -3193,6 +3199,15 @@ func (m *Messenger) handleSyncKeypair(message *protobuf.SyncKeypair) (*accounts.
for _, sAcc := range message.Accounts {
syncKpMigratedToKeycard := len(message.Keycards) > 0
if message.SyncedFrom == accounts.SyncedFromBackup && kp.Type == accounts.KeypairTypeProfile {
// if a profile keypair is coming from backup, we're handling within this block the case when a recovering
// was inititiated via keycard, while backed up profile keypair data refers to a regular profile
multiAcc, err := m.multiAccounts.GetAccount(kp.KeyUID)
if err != nil {
return nil, err
}
syncKpMigratedToKeycard = multiAcc != nil && multiAcc.KeycardPairing != ""
}
accountOperability, err := m.resolveAccountOperability(sAcc, syncKpMigratedToKeycard, accountReceivedFromLocalPairing)
if err != nil {
return nil, err
@ -3236,10 +3251,13 @@ func (m *Messenger) handleSyncKeypair(message *protobuf.SyncKeypair) (*accounts.
// if entire keypair was removed, there is no point to continue
if kp.Removed {
// if keypair is retrieved from backed up data, no need for resolving accounts positions
if message.SyncedFrom != accounts.SyncedFromBackup {
err = m.settings.ResolveAccountsPositions(message.Clock)
if err != nil {
return nil, err
}
}
return kp, nil
}
@ -3249,11 +3267,14 @@ func (m *Messenger) handleSyncKeypair(message *protobuf.SyncKeypair) (*accounts.
return nil, err
}
// if keypair is retrieved from backed up data, no need for resolving accounts positions
if message.SyncedFrom != accounts.SyncedFromBackup {
// then resolve accounts positions, cause some accounts might be removed
err = m.settings.ResolveAccountsPositions(message.Clock)
if err != nil {
return nil, err
}
}
for _, sKc := range message.Keycards {
kc := accounts.Keycard{}
@ -3292,7 +3313,7 @@ func (m *Messenger) HandleSyncAccountsPositions(state *ReceivedMessageState, mes
}
func (m *Messenger) HandleSyncWatchOnlyAccount(state *ReceivedMessageState, message protobuf.SyncAccount) error {
acc, err := m.handleSyncWatchOnlyAccount(&message)
acc, err := m.handleSyncWatchOnlyAccount(&message, false)
if err != nil {
if err == ErrTryingToStoreOldWalletAccount {
return nil