@@ -4,18 +4,17 @@ import { ErrorCode } from './types';
4
4
import { delay } from './utils' ;
5
5
6
6
export enum BrowserSupport {
7
- NoMetamask = 'NoMetamask' ,
8
- MetamaskLegacy = 'MetamaskLegacy' ,
9
- MetamaskModern = 'MetamaskModern' ,
7
+ MetamaskDisabled = 'MetamaskDisabled' ,
8
+ MetamaskEnabled = 'MetamaskEnabled' ,
10
9
None = 'None' ,
11
10
}
12
11
13
12
interface Ethereum extends Provider {
14
13
networkVersion : string ;
15
14
_metamask ?: {
16
- isApproved : ( ) => Promise < boolean > ;
15
+ isUnlocked : ( ) => Promise < boolean > ;
17
16
} ;
18
- enable ( ) : Promise < any > ;
17
+ request ( payload : { method : string } ) : Promise < any > ;
19
18
}
20
19
21
20
interface Web3VersionAPI {
@@ -35,9 +34,6 @@ interface ExtendedWindow extends Window {
35
34
interface WindowWithEthereum extends ExtendedWindow {
36
35
ethereum : Ethereum ;
37
36
}
38
- interface WindowWithWeb3 extends ExtendedWindow {
39
- web3 : InjectedWeb3 ;
40
- }
41
37
42
38
/**
43
39
* Returns the browser support for Ethereum
@@ -52,34 +48,24 @@ export function getBrowserSupport() {
52
48
const win = window as ExtendedWindow ;
53
49
54
50
if ( win . ethereum ) {
55
- return BrowserSupport . MetamaskModern ;
56
- }
57
- if ( win . web3 ) {
58
- return BrowserSupport . MetamaskLegacy ;
51
+ return BrowserSupport . MetamaskEnabled ;
59
52
}
60
- return BrowserSupport . NoMetamask ;
53
+ return BrowserSupport . MetamaskDisabled ;
61
54
}
62
55
63
56
/* eslint-disable @typescript-eslint/no-unused-vars */
64
57
/**
65
58
* @hidden
66
59
*/
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 ;
76
62
}
77
63
78
64
/**
79
65
* @hidden
80
66
*/
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 ;
83
69
}
84
70
/* eslint-enable @typescript-eslint/no-unused-vars */
85
71
@@ -95,18 +81,14 @@ export async function getInjectedProvider(): Promise<Provider | undefined> {
95
81
96
82
const win = ( window as any ) as ExtendedWindow ;
97
83
98
- if ( isModern ( win ) ) {
84
+ if ( isWindowWithEthereum ( win ) ) {
99
85
const injectedProvider = win . ethereum ;
100
86
try {
101
- await injectedProvider . enable ( ) ;
87
+ await injectedProvider . request ( { method : 'eth_requestAccounts' } ) ;
102
88
return injectedProvider ;
103
89
} catch ( err ) {
104
90
throw new PolymathError ( { code : ErrorCode . UserDeniedAccess } ) ;
105
91
}
106
- } else if ( isLegacy ( win ) ) {
107
- const injectedWeb3 = win . web3 ;
108
- const web3Provider = injectedWeb3 . currentProvider ;
109
- return web3Provider ;
110
92
} else {
111
93
throw new PolymathError ( { code : ErrorCode . MetamaskNotInstalled } ) ;
112
94
}
@@ -135,10 +117,8 @@ export async function getNetworkId(): Promise<number | null> {
135
117
136
118
let rawNetworkId : string | undefined ;
137
119
138
- if ( isModern ( win ) ) {
120
+ if ( isWindowWithEthereum ( win ) ) {
139
121
rawNetworkId = win . ethereum . networkVersion ;
140
- } else if ( isLegacy ( win ) ) {
141
- rawNetworkId = win . web3 . version . network ;
142
122
} else {
143
123
return null ;
144
124
}
@@ -163,16 +143,16 @@ export async function getCurrentAddress() {
163
143
throw new PolymathError ( { code : ErrorCode . NonBrowserEnvironment } ) ;
164
144
}
165
145
166
- if ( isModern ( win ) ) {
146
+ if ( isWindowWithEthereum ( win ) ) {
167
147
// Special check for Metamask to know if it is locked or not
168
148
const metamask = win . ethereum . _metamask ;
169
149
if ( metamask ) {
170
- const isApproved = await metamask . isApproved ( ) ;
171
- if ( isApproved && ! accounts . length ) {
150
+ const isUnlocked = await metamask . isUnlocked ( ) ;
151
+ if ( isUnlocked && ! accounts . length ) {
172
152
throw new PolymathError ( { code : ErrorCode . WalletIsLocked } ) ;
173
153
}
174
154
}
175
- } else if ( isUnsupported ( win ) ) {
155
+ } else if ( isExtendedWindow ( win ) ) {
176
156
throw new PolymathError ( { code : ErrorCode . IncompatibleBrowser } ) ;
177
157
}
178
158
@@ -187,7 +167,7 @@ export async function getCurrentAddress() {
187
167
* @returns an unsubscribe function
188
168
*/
189
169
export function onAddressChange ( cb : ( newAddress : string , previousAddress ?: string ) => void ) {
190
- if ( isUnsupported ( window as ExtendedWindow ) ) {
170
+ if ( isExtendedWindow ( window ) ) {
191
171
// eslint-disable-next-line no-console
192
172
console . warn ( '"onAddressChange" Was called, but the current browser does not support Ethereum' ) ;
193
173
return ( ) => { } ;
@@ -223,7 +203,7 @@ export function onAddressChange(cb: (newAddress: string, previousAddress?: strin
223
203
* @returns an unsubscribe function
224
204
*/
225
205
export function onNetworkChange ( cb : ( newNetwork : number , previousNetwork ?: number ) => void ) {
226
- if ( isUnsupported ( window as ExtendedWindow ) ) {
206
+ if ( isExtendedWindow ( window ) ) {
227
207
// eslint-disable-next-line no-console
228
208
console . warn ( '"onNetworkChange" Was called, but the current browser does not support Ethereum' ) ;
229
209
return ( ) => { } ;
0 commit comments