@@ -4,18 +4,17 @@ import { ErrorCode } from './types';
44import { delay } from './utils' ;
55
66export enum BrowserSupport {
7- NoMetamask = 'NoMetamask' ,
8- MetamaskLegacy = 'MetamaskLegacy' ,
9- MetamaskModern = 'MetamaskModern' ,
7+ MetamaskDisabled = 'MetamaskDisabled' ,
8+ MetamaskEnabled = 'MetamaskEnabled' ,
109 None = 'None' ,
1110}
1211
1312interface Ethereum extends Provider {
1413 networkVersion : string ;
1514 _metamask ?: {
16- isApproved : ( ) => Promise < boolean > ;
15+ isUnlocked : ( ) => Promise < boolean > ;
1716 } ;
18- enable ( ) : Promise < any > ;
17+ request ( payload : { method : string } ) : Promise < any > ;
1918}
2019
2120interface Web3VersionAPI {
@@ -35,9 +34,6 @@ interface ExtendedWindow extends Window {
3534interface WindowWithEthereum extends ExtendedWindow {
3635 ethereum : Ethereum ;
3736}
38- interface WindowWithWeb3 extends ExtendedWindow {
39- web3 : InjectedWeb3 ;
40- }
4137
4238/**
4339 * Returns the browser support for Ethereum
@@ -52,34 +48,24 @@ export function getBrowserSupport() {
5248 const win = window as ExtendedWindow ;
5349
5450 if ( win . ethereum ) {
55- return BrowserSupport . MetamaskModern ;
56- }
57- if ( win . web3 ) {
58- return BrowserSupport . MetamaskLegacy ;
51+ return BrowserSupport . MetamaskEnabled ;
5952 }
60- return BrowserSupport . NoMetamask ;
53+ return BrowserSupport . MetamaskDisabled ;
6154}
6255
6356/* eslint-disable @typescript-eslint/no-unused-vars */
6457/**
6558 * @hidden
6659 */
67- function isModern ( obj : any ) : obj is WindowWithEthereum {
68- return getBrowserSupport ( ) === BrowserSupport . MetamaskModern ;
69- }
70-
71- /**
72- * @hidden
73- */
74- function isLegacy ( obj : any ) : obj is WindowWithWeb3 {
75- return getBrowserSupport ( ) === BrowserSupport . MetamaskLegacy ;
60+ function isWindowWithEthereum ( obj : any ) : obj is WindowWithEthereum {
61+ return getBrowserSupport ( ) === BrowserSupport . MetamaskEnabled ;
7662}
7763
7864/**
7965 * @hidden
8066 */
81- function isUnsupported ( obj : any ) : obj is ExtendedWindow {
82- return getBrowserSupport ( ) === BrowserSupport . NoMetamask ;
67+ function isExtendedWindow ( obj : any ) : obj is ExtendedWindow {
68+ return getBrowserSupport ( ) === BrowserSupport . MetamaskDisabled ;
8369}
8470/* eslint-enable @typescript-eslint/no-unused-vars */
8571
@@ -95,18 +81,14 @@ export async function getInjectedProvider(): Promise<Provider | undefined> {
9581
9682 const win = ( window as any ) as ExtendedWindow ;
9783
98- if ( isModern ( win ) ) {
84+ if ( isWindowWithEthereum ( win ) ) {
9985 const injectedProvider = win . ethereum ;
10086 try {
101- await injectedProvider . enable ( ) ;
87+ await injectedProvider . request ( { method : 'eth_requestAccounts' } ) ;
10288 return injectedProvider ;
10389 } catch ( err ) {
10490 throw new PolymathError ( { code : ErrorCode . UserDeniedAccess } ) ;
10591 }
106- } else if ( isLegacy ( win ) ) {
107- const injectedWeb3 = win . web3 ;
108- const web3Provider = injectedWeb3 . currentProvider ;
109- return web3Provider ;
11092 } else {
11193 throw new PolymathError ( { code : ErrorCode . MetamaskNotInstalled } ) ;
11294 }
@@ -135,10 +117,8 @@ export async function getNetworkId(): Promise<number | null> {
135117
136118 let rawNetworkId : string | undefined ;
137119
138- if ( isModern ( win ) ) {
120+ if ( isWindowWithEthereum ( win ) ) {
139121 rawNetworkId = win . ethereum . networkVersion ;
140- } else if ( isLegacy ( win ) ) {
141- rawNetworkId = win . web3 . version . network ;
142122 } else {
143123 return null ;
144124 }
@@ -163,16 +143,16 @@ export async function getCurrentAddress() {
163143 throw new PolymathError ( { code : ErrorCode . NonBrowserEnvironment } ) ;
164144 }
165145
166- if ( isModern ( win ) ) {
146+ if ( isWindowWithEthereum ( win ) ) {
167147 // Special check for Metamask to know if it is locked or not
168148 const metamask = win . ethereum . _metamask ;
169149 if ( metamask ) {
170- const isApproved = await metamask . isApproved ( ) ;
171- if ( isApproved && ! accounts . length ) {
150+ const isUnlocked = await metamask . isUnlocked ( ) ;
151+ if ( isUnlocked && ! accounts . length ) {
172152 throw new PolymathError ( { code : ErrorCode . WalletIsLocked } ) ;
173153 }
174154 }
175- } else if ( isUnsupported ( win ) ) {
155+ } else if ( isExtendedWindow ( win ) ) {
176156 throw new PolymathError ( { code : ErrorCode . IncompatibleBrowser } ) ;
177157 }
178158
@@ -187,7 +167,7 @@ export async function getCurrentAddress() {
187167 * @returns an unsubscribe function
188168 */
189169export function onAddressChange ( cb : ( newAddress : string , previousAddress ?: string ) => void ) {
190- if ( isUnsupported ( window as ExtendedWindow ) ) {
170+ if ( isExtendedWindow ( window ) ) {
191171 // eslint-disable-next-line no-console
192172 console . warn ( '"onAddressChange" Was called, but the current browser does not support Ethereum' ) ;
193173 return ( ) => { } ;
@@ -223,7 +203,7 @@ export function onAddressChange(cb: (newAddress: string, previousAddress?: strin
223203 * @returns an unsubscribe function
224204 */
225205export function onNetworkChange ( cb : ( newNetwork : number , previousNetwork ?: number ) => void ) {
226- if ( isUnsupported ( window as ExtendedWindow ) ) {
206+ if ( isExtendedWindow ( window ) ) {
227207 // eslint-disable-next-line no-console
228208 console . warn ( '"onNetworkChange" Was called, but the current browser does not support Ethereum' ) ;
229209 return ( ) => { } ;
0 commit comments