Skip to content

Commit

Permalink
fix: connecting wallet using firefox (#386)
Browse files Browse the repository at this point in the history
* fix: connecting wallet using firefox

* fix: getInjectedExtensions function

* refactor: clean up
  • Loading branch information
impelcrypto committed May 6, 2022
1 parent 422432d commit 163b18e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/hooks/helper/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ export const capitalize = (str: string): string => {
export const getRandomFromArray = (list: any[]) => {
return list[Math.floor(Math.random() * list.length)];
};

//Ref: https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
export const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
18 changes: 17 additions & 1 deletion src/hooks/helper/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { wait } from 'src/hooks/helper/common';
import { EthereumProvider } from './../types/CustomSignature';
import { supportEvmWalletObj, SupportWallet } from 'src/config/wallets';
import { web3Enable } from '@polkadot/extension-dapp';
Expand All @@ -6,9 +7,24 @@ import { SubstrateAccount } from './../../store/general/state';
import { deepLink } from 'src/links';

export const getInjectedExtensions = async (): Promise<any[]> => {
const extensions = await web3Enable('AstarNetwork/astar-apps');
// Memo: Firefox takes some time to load the wallet extensions at the boot time.
let extensions = await web3Enable('AstarNetwork/astar-apps');
// Memo: obtain the extension name
// console.log('extensions', extensions);

const injectedWeb3 = window.injectedWeb3;
const numWalletExtensions = injectedWeb3 ? Object.values(window.injectedWeb3).length : 0;
const maxRetry = 20;
let numRetry = 0;
while (extensions.length !== numWalletExtensions) {
wait(400);
extensions = await web3Enable('AstarNetwork/astar-apps');
numRetry++;
if (numRetry > maxRetry) {
break;
}
}

return extensions;
};

Expand Down

0 comments on commit 163b18e

Please sign in to comment.