From 4778583a335371c712ecd1ca4a5e2fead07e1ffa Mon Sep 17 00:00:00 2001 From: Sebastien Guillemot Date: Sat, 28 Oct 2023 12:24:50 +0900 Subject: [PATCH] Avoid crash if no wallets installed for a specific crypto --- packages/paima-sdk/paima-providers/src/algorand.ts | 4 ++++ packages/paima-sdk/paima-providers/src/evm.ts | 9 +++++---- packages/paima-sdk/paima-providers/src/polkadot.ts | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/paima-sdk/paima-providers/src/algorand.ts b/packages/paima-sdk/paima-providers/src/algorand.ts index 3aaa75ba3..0464bbe22 100644 --- a/packages/paima-sdk/paima-providers/src/algorand.ts +++ b/packages/paima-sdk/paima-providers/src/algorand.ts @@ -30,6 +30,10 @@ export class AlgorandConnector implements IConnector { // but it doesn't give any information about which wallet is injected // and, similar to window.ethereum, has wallets overriding each other // and Pera wallet doesn't even use this standard + // instead, the best we can do is check if Pera injected its UI component in the window + if (window.customElements.get('pera-wallet-connect-modal') == null) { + return []; + } return [ { metadata: { diff --git a/packages/paima-sdk/paima-providers/src/evm.ts b/packages/paima-sdk/paima-providers/src/evm.ts index 6c5716e82..8563c4a81 100644 --- a/packages/paima-sdk/paima-providers/src/evm.ts +++ b/packages/paima-sdk/paima-providers/src/evm.ts @@ -52,7 +52,7 @@ window?.addEventListener('eip6963:announceProvider', (event: EIP6963AnnounceProv window?.dispatchEvent(new Event('eip6963:requestProvider')); declare global { interface Window { - ethereum: EIP1193Provider; + ethereum?: EIP1193Provider; evmproviders?: EIP5749EVMProviders; } interface WindowEventMap { @@ -125,14 +125,15 @@ export class EvmConnector implements IConnector { } // Metamask doesn't support EIP6963 yet, but it plans to. In the meantime, we add it manually - if (window.ethereum.isMetaMask) { + if (window.ethereum != null && window.ethereum.isMetaMask) { + const ethereum = window.ethereum; addOptions([ { metadata: { name: 'metamask', displayName: 'Metamask', }, - api: () => Promise.resolve(window.ethereum), + api: () => Promise.resolve(ethereum), }, ]); } @@ -166,7 +167,7 @@ export class EvmConnector implements IConnector { // Update the selected Eth address if the user changes after logging in. // warning: not supported by all wallets (ex: Flint) - window.ethereum.on('accountsChanged', newAccounts => { + window.ethereum?.on('accountsChanged', newAccounts => { const accounts = newAccounts as string[]; if (!accounts || !accounts[0] || accounts[0] !== this.provider?.address) { this.provider = undefined; diff --git a/packages/paima-sdk/paima-providers/src/polkadot.ts b/packages/paima-sdk/paima-providers/src/polkadot.ts index 9a6f5bee4..5f1f0c082 100644 --- a/packages/paima-sdk/paima-providers/src/polkadot.ts +++ b/packages/paima-sdk/paima-providers/src/polkadot.ts @@ -16,7 +16,7 @@ export type PolkadotApi = InjectedExtension; declare global { interface Window { - injectedWeb3: Record; + injectedWeb3?: Record; } } @@ -25,6 +25,7 @@ export class PolkadotConnector implements IConnector { private static INSTANCE: undefined | PolkadotConnector = undefined; static async getWalletOptions(gameName: string): Promise[]> { + if (window.injectedWeb3 == null) return []; return Object.keys(window.injectedWeb3).map(wallet => ({ metadata: { name: wallet,