11import { helpers as messages } from './messages' ;
22
3- import { AccountsChangedCallback } from './types' ;
3+ import { AccountsChangedCallback , ObservableEvents } from './types' ;
44
55// eslint-disable-next-line @typescript-eslint/no-explicit-any
66const anyGlobal : any = global ;
@@ -20,39 +20,37 @@ export const detect = async (): Promise<boolean> => {
2020 * Modern Metamask Version
2121 */
2222 if ( anyGlobal . ethereum ) {
23+ const { ethereum } = anyGlobal ;
2324 /*
24- * @NOTE This is a temporary failsafe check, since Metmask is running an
25- * intermediate version which, while it contains most of the `ethereum`
26- * global object, it doesn't contain this helper method
25+ * Check that the provider is connected to the chain
26+ */
27+ if ( ! ethereum . isConnected ( ) ) {
28+ throw new Error ( messages . noProvider ) ;
29+ }
30+ /*
31+ * Check if the account is unlocked
2732 *
28- * @TODO Remove legacy metmask object availability check
29- * After an adequate amount of time has passed
33+ * @NOTE we just assume the required methods exist on the metamask provider
34+ * otherwise we'll get right back to "support legacy metamask hell"
3035 */
31- if (
32- anyGlobal . ethereum . isUnlocked &&
33- ! ( await anyGlobal . ethereum . isUnlocked ( ) )
34- ) {
36+ // eslint-disable-next-line no-underscore-dangle
37+ if ( ! ( await ethereum . _metamask . isUnlocked ( ) ) ) {
3538 throw new Error ( messages . isLocked ) ;
3639 }
3740 /*
38- * @NOTE This is a temporary failsafe check, since Metmask is running an
39- * intermediate version which, while it contains most of the `ethereum`
40- * global object, it doesn't contain this helper method
41- *
42- * @TODO Remove legacy metmask object availability check
43- * After an adequate amount of time has passed
41+ * If we don't have the `eth_accounts` permissions it means that we don't have
42+ * account access
4443 */
44+ const permissions = await ethereum . request ( {
45+ method : 'wallet_getPermissions' ,
46+ } ) ;
4547 if (
46- anyGlobal . ethereum . isEnabled &&
47- ! ( await anyGlobal . ethereum . isEnabled ( ) )
48+ ! permissions . length ||
49+ ! permissions [ 0 ] ||
50+ permissions [ 0 ] . parentCapability !== 'eth_accounts'
4851 ) {
49- throw new Error ( messages . notEnabled ) ;
52+ throw new Error ( messages . notConnected ) ;
5053 }
51- /*
52- * @NOTE If the `isUnlocked` and the `isEnabled` methods are not available
53- * it means we are running the pre-release version of Metamask, just prior
54- * to the EIP-1102 update, so we just ignore those checks
55- */
5654 return true ;
5755 }
5856 throw new Error ( messages . noExtension ) ;
@@ -103,7 +101,8 @@ export const methodCaller = async <T>(
103101 */
104102export const setStateEventObserver = (
105103 callback : AccountsChangedCallback ,
104+ observableEvent : ObservableEvents = 'accountsChanged' ,
106105) : void => {
107106 const { ethereum } = anyGlobal ;
108- ethereum . on ( 'accountsChanged' , callback ) ;
107+ ethereum . on ( observableEvent , callback ) ;
109108} ;
0 commit comments