From 353d3df1f4288c5bbb944380f703c0c7dba012cc Mon Sep 17 00:00:00 2001 From: imin314 Date: Wed, 11 May 2022 15:19:30 +0700 Subject: [PATCH 1/2] fix: fix ethereum provider type --- .../src/@types/extend-window.ts | 4 ++-- packages/clover-connector/src/index.ts | 2 +- .../src/@types/extend-window.ts | 4 ++-- packages/inpage-connector/src/index.ts | 18 ++++++++---------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/clover-connector/src/@types/extend-window.ts b/packages/clover-connector/src/@types/extend-window.ts index 80f4685..6649d8d 100644 --- a/packages/clover-connector/src/@types/extend-window.ts +++ b/packages/clover-connector/src/@types/extend-window.ts @@ -7,7 +7,7 @@ export type CloverProvider = Provider & { declare global { interface Window { - ethereum?: CloverProvider; - clover?: CloverProvider; + ethereum?: unknown; + clover?: unknown; } } diff --git a/packages/clover-connector/src/index.ts b/packages/clover-connector/src/index.ts index 1ef8835..43499c4 100644 --- a/packages/clover-connector/src/index.ts +++ b/packages/clover-connector/src/index.ts @@ -12,7 +12,7 @@ export interface CloverConnectionPayload extends DefaultConnectionPayload { } export class CloverConnector extends AbstractConnector { public async connect(): Promise { - const provider = window.clover || window.ethereum; + const provider: CloverProvider = window.clover || window.ethereum; if (!provider?.isClover) { throw new Error( diff --git a/packages/inpage-connector/src/@types/extend-window.ts b/packages/inpage-connector/src/@types/extend-window.ts index 9e5fa79..5679311 100644 --- a/packages/inpage-connector/src/@types/extend-window.ts +++ b/packages/inpage-connector/src/@types/extend-window.ts @@ -7,8 +7,8 @@ export type InpageProvider = Provider & { declare global { interface Window { web3?: { - currentProvider?: InpageProvider; + currentProvider?: unknown; }; - ethereum?: InpageProvider; + ethereum?: unknown; } } diff --git a/packages/inpage-connector/src/index.ts b/packages/inpage-connector/src/index.ts index a2d6390..70ed2c7 100644 --- a/packages/inpage-connector/src/index.ts +++ b/packages/inpage-connector/src/index.ts @@ -15,16 +15,14 @@ export interface InpageConnectionPayload extends DefaultConnectionPayload { export class InpageConnector extends AbstractConnector { public async connect(): Promise { - let provider: InpageProvider; - - if (window.ethereum) { - // edge case if Metamask and Coinbase extensions are both installed - const { providers } = window.ethereum; - provider = - (Array.isArray(providers) && providers.find((x) => x.isMetaMask)) || window.ethereum; - } else if (window.web3?.currentProvider) { - provider = window.web3.currentProvider; - } else { + let provider: InpageProvider = window.ethereum || window.web3?.currentProvider; + + // edge case if Metamask and Coinbase extensions are both installed + if (Array.isArray(provider.providers)) { + provider = provider.providers.find((x: InpageProvider) => x.isMetaMask) || provider; + } + + if (!provider) { throw new Error( 'Web3 provider not found! Please install the Web3 extension (e.g. Metamask) or use the Web3 browser (e.g. TrustWallet on your mobile device).', ); From 30a6e7001d0e5aee0d64ef9d339546bf22450266 Mon Sep 17 00:00:00 2001 From: imin314 Date: Wed, 11 May 2022 15:32:07 +0700 Subject: [PATCH 2/2] fix: check providers property correctly --- packages/inpage-connector/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/inpage-connector/src/index.ts b/packages/inpage-connector/src/index.ts index 70ed2c7..7721469 100644 --- a/packages/inpage-connector/src/index.ts +++ b/packages/inpage-connector/src/index.ts @@ -18,7 +18,7 @@ export class InpageConnector extends AbstractConnector let provider: InpageProvider = window.ethereum || window.web3?.currentProvider; // edge case if Metamask and Coinbase extensions are both installed - if (Array.isArray(provider.providers)) { + if (Array.isArray(provider?.providers)) { provider = provider.providers.find((x: InpageProvider) => x.isMetaMask) || provider; }