Skip to content

fix: back up logic to clear vault before reapplying #14743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions app/core/BackupVault/backupVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ interface KeyringBackupResponse {
error?: string;
}

/**
* removes the vault backup from react-native-keychain
*/
export const resetVaultBackup = async (): Promise<void> => {
await resetInternetCredentials(VAULT_BACKUP_KEY);
};

/**
* places the vault in react-native-keychain for backup
* @returns Promise<KeyringBackupResponse>
Expand All @@ -38,24 +45,34 @@ export async function backupVault(
): Promise<KeyringBackupResponse> {
const keyringVault = keyringState.vault as string;

// Backup vault
const backupResult = await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
keyringVault,
options,
);
try {
// Clear any existing vault backup first to prevent "item already exists" errors
await resetVaultBackup();
Copy link
Member

@gantunesr gantunesr Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While talking to @ccharly, he pointed out that this solution would be more robust if we save two copies of the vault. In the edge case that we execute resetVaultBackup and setInternetCredentials fails, there will be an extra copy that can still be retrieved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented a temporary secondary backup while reseting the primary for now based on our convo - https://consensys.slack.com/archives/C08PN631R0X/p1745884192737909


// Vault backup failed, throw error
if (!backupResult) {
throw new Error(VAULT_BACKUP_FAILED);
}
// Backup vault
const backupResult = await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
keyringVault,
options,
);

// Vault backup failed, throw error
if (!backupResult) {
throw new Error(VAULT_BACKUP_FAILED);
}

// Vault backup successful, return response
return {
success: true,
vault: keyringState.vault,
};
return {
success: true,
vault: keyringState.vault,
};
} catch (error) {
Logger.error(error as Error, 'Vault backup failed');
return {
success: false,
error: error instanceof Error ? error.message : VAULT_BACKUP_FAILED,
};
}
}

/**
Expand All @@ -76,10 +93,3 @@ export async function getVaultFromBackup(): Promise<KeyringBackupResponse> {
Logger.error(vaultFetchError, VAULT_FAILED_TO_GET_VAULT_FROM_BACKUP);
return { success: false, error: VAULT_FAILED_TO_GET_VAULT_FROM_BACKUP };
}

/**
* removes the vault backup from react-native-keychain
*/
export const resetVaultBackup = async (): Promise<void> => {
await resetInternetCredentials(VAULT_BACKUP_KEY);
};
Loading