Skip to content

Commit

Permalink
Merge pull request #3746 from Emurgo/refactor/selected-wallet-chrome-…
Browse files Browse the repository at this point in the history
…storage

use chrome.storage instead of window.localStorage to record selected wallet
  • Loading branch information
vsubhuman authored Nov 21, 2024
2 parents 0553505 + 5978c01 commit 9a48074
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
19 changes: 14 additions & 5 deletions packages/yoroi-extension/app/api/localStorage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,24 @@ export default class LocalStorageApi {

// ========== Select Wallet ========== //

getSelectedWalletId: void => number | null = () => {
const id = localStorage.getItem(storageKeys.SELECTED_WALLET);
if (!id) return null;
getSelectedWalletId: void => Promise<number | null> = async () => {
let id = await getLocalItem(storageKeys.SELECTED_WALLET);
// previously it was stored in window.localStorage, which is not accessible in the mv3 service worker
if (!id) {
id = window?.localStorage.getItem(storageKeys.SELECTED_WALLET);
if (/^\d+$/.test(id)) {
await this.setSelectedWalletId(Number(id));
}
}
if (!id) {
return null;
}
if (isNaN(Number(id))) throw new Error(`Invalid wallet Id: ${id}`);
return Number(id);
};

setSelectedWalletId: number => void = id => {
localStorage.setItem(storageKeys.SELECTED_WALLET, id.toString());
setSelectedWalletId: number => Promise<void> = async (id) => {
await setLocalItem(storageKeys.SELECTED_WALLET, id.toString());
};

// ========== Legacy Theme ========== //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class ProfileStore extends BaseProfileStore<StoresMap, ActionsMap
}
const isRevamp = this.stores.profile.isRevampTheme;
if (isRevamp) {
const lastSelectedWallet = this.stores.wallets.getLastSelectedWallet();
const lastSelectedWallet = await this.stores.wallets.getLastSelectedWallet();
this.actions.router.goToRoute.trigger({
route: ROUTES.WALLETS.ROOT,
publicDeriverId: lastSelectedWallet?.publicDeriverId ?? firstWallet.publicDeriverId,
Expand Down
5 changes: 2 additions & 3 deletions packages/yoroi-extension/app/stores/toplevel/WalletStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,12 @@ export default class WalletStore extends Store<StoresMap, ActionsMap> {
);
this.selectedIndex = walletIndex;
this.selectedWalletName = this.wallets[walletIndex].name;
// Cache select wallet
this.api.localStorage.setSelectedWalletId(publicDeriverId);
subscribe(publicDeriverId);
};

getLastSelectedWallet: void => ?WalletState = () => {
const walletId = this.api.localStorage.getSelectedWalletId();
getLastSelectedWallet: void => Promise<?WalletState> = async () => {
const walletId: ?number = await this.api.localStorage.getSelectedWalletId();
return this.wallets.find(wallet => wallet.publicDeriverId === walletId);
};

Expand Down

0 comments on commit 9a48074

Please sign in to comment.