Skip to content

Commit

Permalink
Avoid crash if no wallets installed for a specific crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Oct 28, 2023
1 parent 82e8a91 commit 4778583
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/paima-sdk/paima-providers/src/algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class AlgorandConnector implements IConnector<AlgorandApi> {
// 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: {
Expand Down
9 changes: 5 additions & 4 deletions packages/paima-sdk/paima-providers/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -125,14 +125,15 @@ export class EvmConnector implements IConnector<EvmApi> {
}

// 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),
},
]);
}
Expand Down Expand Up @@ -166,7 +167,7 @@ export class EvmConnector implements IConnector<EvmApi> {

// 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;
Expand Down
3 changes: 2 additions & 1 deletion packages/paima-sdk/paima-providers/src/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type PolkadotApi = InjectedExtension;

declare global {
interface Window {
injectedWeb3: Record<string, InjectedWindowProvider>;
injectedWeb3?: Record<string, InjectedWindowProvider>;
}
}

Expand All @@ -25,6 +25,7 @@ export class PolkadotConnector implements IConnector<PolkadotApi> {
private static INSTANCE: undefined | PolkadotConnector = undefined;

static async getWalletOptions(gameName: string): Promise<ConnectionOption<PolkadotApi>[]> {
if (window.injectedWeb3 == null) return [];
return Object.keys(window.injectedWeb3).map(wallet => ({
metadata: {
name: wallet,
Expand Down

0 comments on commit 4778583

Please sign in to comment.