Skip to content

Commit

Permalink
fix: ensure keyring is always initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
F-OBrien authored and adamdossa committed Jan 20, 2025
1 parent 91a2095 commit 29b86bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/store/accountMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export default class AccountMigrations extends BaseStore<KeyringJson> implements

// Migrates accounts stored without an extension prefix to be stored with the
// EXTENSION_PREFIX configured in webpack.shared.cjs
public async migrateUnPrefixedAccounts (): Promise<void> {
public async migrateUnPrefixedAccounts (): Promise<boolean> {
const accountsStore = new AccountsStore();
const migrationPromises: Promise<void>[] = [];
let needsReload = false;

const migrateAccount = async (key: string, value: KeyringJson): Promise<void> => {
let existingValue: KeyringJson | null = null;
Expand All @@ -31,6 +32,8 @@ export default class AccountMigrations extends BaseStore<KeyringJson> implements

// migrate the account to the new storage key
await accountsStore.set(key, value);
// Only reload if migrations actually happened this ensure the keyring reinitialized from the migrated storage
needsReload = true;

// Verify the migration was successful
await accountsStore.get(key, (value) => {
Expand All @@ -53,5 +56,7 @@ export default class AccountMigrations extends BaseStore<KeyringJson> implements
});

await Promise.all(migrationPromises);

return needsReload;
}
}
7 changes: 4 additions & 3 deletions packages/extension/src/background-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ async function initializeCryptoAndKeyring () {

chrome.runtime.onInstalled.addListener((details) => {
checkForUpdateAndMigrate(details)
.then(() => {
return initializeCryptoAndKeyring();
})
.catch(fatalErrorHandler);
});

// Run initialization immediately
initializeCryptoAndKeyring()
.catch(fatalErrorHandler);
13 changes: 10 additions & 3 deletions packages/extension/src/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ function isVersionEarlierOrEqual (version: string, compareTo: string): boolean {
async function runMigrations () {
const migrate = new AccountMigrations();

await migrate.migrateUnPrefixedAccounts();
const shouldReload = await migrate.migrateUnPrefixedAccounts();

console.log('Migration completed');

// if required, reloading should only be triggered after all migrations are complete
if (shouldReload) {
console.log('Reloading extension');
chrome.runtime.reload();
}
}

// Check for version update and perform migration if needed
export async function checkForUpdateAndMigrate (details: chrome.runtime.InstalledDetails): Promise<void> {
if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
const previousVersion = details.previousVersion;

// Migrate Account Prefixes. Runs when no lastVersion or a previous version of 2.0.2 or earlier
if (!previousVersion || isVersionEarlierOrEqual(previousVersion, '2.2.0')) {
// Migrate Account Prefixes. Runs when no lastVersion or a previous version of 2.3.0 or earlier
if (!previousVersion || isVersionEarlierOrEqual(previousVersion, '2.3.0')) {
await runMigrations();
}
}
Expand Down

0 comments on commit 29b86bc

Please sign in to comment.