diff --git a/src/background/controller/wallet.ts b/src/background/controller/wallet.ts index 3d6a0291..52ee21f9 100644 --- a/src/background/controller/wallet.ts +++ b/src/background/controller/wallet.ts @@ -33,6 +33,7 @@ import { type LoggedInAccount } from '@/shared/types/wallet-types'; import { ensureEvmAddressPrefix, isValidEthereumAddress, withPrefix } from '@/shared/utils/address'; import { getSignAlgo } from '@/shared/utils/algo'; import { convertToIntegerAmount, validateAmount } from '@/shared/utils/number'; +import { retryOperation } from '@/shared/utils/retryOperation'; import { keyringService, preferenceService, @@ -181,6 +182,24 @@ export class WalletController extends BaseController { await this.refreshUserWallets(); // Refresh the child wallets await this.setChildWallet(await this.checkUserChildAccount()); + // Refresh the logged in account + const [keys, pubKTuple] = await Promise.all([this.getAccount(), this.getPubKey()]); + // Check if a child wallet is active (it shouldn't be...) + const anyActiveChild = await this.getActiveWallet(); + // Get the current wallet + const currentWallet = await this.getCurrentWallet(); + if (!currentWallet) { + throw new Error('Current wallet is undefined'); + } + // Refresh the user info + let userInfo = {}; + try { + userInfo = await retryOperation(async () => this.getUserInfo(true), 3, 1000); + } catch (error) { + console.error('Error refreshing user info:', error); + } + // Refresh the user info + await openapiService.freshUserInfo(currentWallet, keys, pubKTuple, userInfo, anyActiveChild); }; retrievePk = async (password: string) => { @@ -835,17 +854,14 @@ export class WalletController extends BaseController { return preferenceService.updateIsFirstOpen(); }; // userinfo - getUserInfo = async (forceRefresh: boolean) => { - const data = await userInfoService.getUserInfo(); - - if (forceRefresh) { - return await this.fetchUserInfo(); - } - - if (data.username.length) { - return data; + getUserInfo = async (forceRefresh: boolean = false) => { + if (!forceRefresh) { + const data = userInfoService.getUserInfo(); + if (data.username.length) { + return data; + } } - + // Either force refresh or the user info is not set return await this.fetchUserInfo(); }; @@ -957,14 +973,6 @@ export class WalletController extends BaseController { return domain; }; - updateUserInfo = (data: UserInfoStore) => { - userInfoService.updateUserInfo(data); - }; - - removeUserInfo = () => { - userInfoService.removeUserInfo(); - }; - getDashIndex = async () => { const dashIndex = await userInfoService.getDashIndex(); return dashIndex; @@ -1594,6 +1602,9 @@ export class WalletController extends BaseController { }; getCurrentWallet = async (): Promise => { + if (!this.isBooted() || userWalletService.isLocked()) { + return; + } const wallet = await userWalletService.getCurrentWallet(); if (!wallet?.address) { const network = await this.getNetwork(); @@ -1641,6 +1652,9 @@ export class WalletController extends BaseController { }; getMainAddress = async () => { + if (!this.isBooted() || userWalletService.isLocked()) { + return ''; + } const network = await this.getNetwork(); const address = await userWalletService.getMainWallet(network); if (!address) { @@ -3071,8 +3085,9 @@ export class WalletController extends BaseController { }; checkNetwork = async () => { - const network = await this.getNetwork(); - await this.switchNetwork(network); + if (!this.isBooted() || userWalletService.isLocked()) { + return; + } }; switchMonitor = async (monitor: string) => { @@ -3084,6 +3099,7 @@ export class WalletController extends BaseController { }; refreshAll = async () => { + console.trace('refreshAll trace'); console.log('refreshAll'); await this.refreshUserWallets(); this.clearNFT(); diff --git a/src/background/index.ts b/src/background/index.ts index 4789aece..ba9a9c29 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -136,8 +136,16 @@ async function restoreAppState() { // Set the loaded flag to true so that the UI knows the app is ready walletController.setLoaded(true); console.log('restoreAppState chrome.runtime.sendMessage->'); - chrome.runtime.sendMessage({ type: 'walletInitialized' }); - + chrome.runtime.sendMessage({ type: 'walletInitialized' }, (response) => { + if (chrome.runtime.lastError) { + console.log( + 'chrome.runtime.sendMessage - Message delivery failed:', + chrome.runtime.lastError.message + ); + } else { + console.log('chrome.runtime.sendMessage - Message delivered successfully:', response); + } + }); console.log('restoreAppState chrome.tabs.query->'); chrome.tabs .query({ @@ -147,9 +155,19 @@ async function restoreAppState() { .then((tabs) => { tabs.forEach((tab) => { const tabId = tab.id; - if (tabId) { + if (tabId && !tab.url?.match(/^chrome*/)) { console.log('restoreAppState chrome.tabs.sendMessage->', tabId); - chrome.tabs.sendMessage(tabId, { type: 'walletInitialized' }); + chrome.tabs.sendMessage(tabId, { type: 'walletInitialized' }, (response) => { + if (chrome.runtime.lastError) { + console.log( + 'chrome.tabs.sendMessage - Message delivery failed:', + chrome.runtime.lastError.message + ); + // You can implement retry logic or alternative actions here + } else { + console.log('chrome.tabs.sendMessage - Message delivered successfully:', response); + } + }); } }); }); diff --git a/src/background/service/user.ts b/src/background/service/user.ts index 302b7476..8d236a95 100644 --- a/src/background/service/user.ts +++ b/src/background/service/user.ts @@ -78,12 +78,6 @@ class UserInfo { this.store = template; }; - updateUserInfo = (data: UserInfoStore) => { - this.store = data; - // identify the user - mixpanelTrack.identify(this.store.user_id); - }; - setDashIndex = (data: number) => { this.store.dashboardIndex = data; }; diff --git a/src/background/service/userWallet.ts b/src/background/service/userWallet.ts index a5646f7a..ec95a4b6 100644 --- a/src/background/service/userWallet.ts +++ b/src/background/service/userWallet.ts @@ -474,7 +474,16 @@ class UserWallet { }; reSign = async () => { + // Try to re-establish the session if the user's wallet is unlocked + if (this.isLocked()) { + // If the wallet is locked, we can't sign in + return; + } const password = keyringService.password; + if (!password) { + // No password means the wallet is not unlocked + return; + } const privateKey = await wallet.getPrivateKeyForCurrentAccount(password); return await this.sigInWithPk(privateKey); }; diff --git a/src/shared/utils/retryOperation.ts b/src/shared/utils/retryOperation.ts index bed77b9c..256ea3ea 100644 --- a/src/shared/utils/retryOperation.ts +++ b/src/shared/utils/retryOperation.ts @@ -1,15 +1,18 @@ -export const retryOperation = async ( - operation: () => Promise, +export const retryOperation = async ( + operation: () => Promise, maxAttempts = 3, delay = 1000 -) => { +): Promise => { for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { return await operation(); } catch (error) { if (attempt === maxAttempts) throw error; + // eslint-disable-next-line no-console console.log(`Attempt ${attempt} failed, retrying in ${delay}ms...`); await new Promise((resolve) => setTimeout(resolve, delay)); } } + // Show never get here... + throw new Error('Operation failed after all attempts'); }; diff --git a/src/ui/FRWComponent/LandingPages/RepeatPhrase.tsx b/src/ui/FRWComponent/LandingPages/RepeatPhrase.tsx index 8792d0d0..e178a582 100644 --- a/src/ui/FRWComponent/LandingPages/RepeatPhrase.tsx +++ b/src/ui/FRWComponent/LandingPages/RepeatPhrase.tsx @@ -121,12 +121,12 @@ const RepeatPhrase = ({ handleSwitchTab, mnemonic }) => { > {repeatArray.map((word, i) => { return ( - + {chrome.i18n.getMessage('Select_the_word_at')} - + {' #' + (chosenIndex[i] + 1) + ' '} - + { backgroundColor: '#333333', transition: 'all .3s linear', }} - key={i} - aria-label={`row${i}`} > {word.map((v, index) => { return ( - +