From b37931474ec959bc8ce40e4dcb1bd0a30baa2742 Mon Sep 17 00:00:00 2001 From: ffmcgee Date: Fri, 31 Jan 2025 18:10:55 +0100 Subject: [PATCH 1/7] fix: remove handleUnlockStateChanged --- src/BaseProvider.ts | 31 ------------------------------- src/StreamProvider.ts | 2 -- 2 files changed, 33 deletions(-) diff --git a/src/BaseProvider.ts b/src/BaseProvider.ts index 5dfcda5e..e37773da 100644 --- a/src/BaseProvider.ts +++ b/src/BaseProvider.ts @@ -129,7 +129,6 @@ export abstract class BaseProvider extends SafeEventEmitter { this._handleConnect = this._handleConnect.bind(this); this._handleChainChanged = this._handleChainChanged.bind(this); this._handleDisconnect = this._handleDisconnect.bind(this); - this._handleUnlockStateChanged = this._handleUnlockStateChanged.bind(this); this._rpcRequest = this._rpcRequest.bind(this); this.request = this.request.bind(this); @@ -257,7 +256,6 @@ export abstract class BaseProvider extends SafeEventEmitter { // EIP-1193 connect this._handleConnect(chainId); this._handleChainChanged({ chainId, networkVersion }); - this._handleUnlockStateChanged({ accounts, isUnlocked }); this._handleAccountsChanged(accounts); } @@ -455,33 +453,4 @@ export abstract class BaseProvider extends SafeEventEmitter { } } } - - /** - * Upon receipt of a new isUnlocked state, sets relevant public state. - * Calls the accounts changed handler with the received accounts, or an empty - * array. - * - * Does nothing if the received value is equal to the existing value. - * There are no lock/unlock events. - * - * @param opts - Options bag. - * @param opts.accounts - The exposed accounts, if any. - * @param opts.isUnlocked - The latest isUnlocked value. - */ - protected _handleUnlockStateChanged({ - accounts, - isUnlocked, - }: { accounts?: string[]; isUnlocked?: boolean } = {}) { - if (typeof isUnlocked !== 'boolean') { - this._log.error( - 'MetaMask: Received invalid isUnlocked parameter. Please report this bug.', - ); - return; - } - - if (isUnlocked !== this._state.isUnlocked) { - this._state.isUnlocked = isUnlocked; - this._handleAccountsChanged(accounts ?? []); - } - } } diff --git a/src/StreamProvider.ts b/src/StreamProvider.ts index acc4c942..7126411d 100644 --- a/src/StreamProvider.ts +++ b/src/StreamProvider.ts @@ -81,8 +81,6 @@ export abstract class AbstractStreamProvider extends BaseProvider { const { method, params } = payload; if (method === 'metamask_accountsChanged') { this._handleAccountsChanged(params); - } else if (method === 'metamask_unlockStateChanged') { - this._handleUnlockStateChanged(params); } else if (method === 'metamask_chainChanged') { this._handleChainChanged(params); } else if (EMITTED_NOTIFICATIONS.includes(method)) { From 5894170d9beeb136dfae57978dd4fc492510474b Mon Sep 17 00:00:00 2001 From: ffmcgee Date: Mon, 3 Feb 2025 16:13:37 +0100 Subject: [PATCH 2/7] chore: update jest.config.js --- jest.config.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jest.config.js b/jest.config.js index 5a7a5578..bb62ae83 100644 --- a/jest.config.js +++ b/jest.config.js @@ -46,9 +46,9 @@ const baseConfig = { coverageThreshold: { global: { branches: 66.79, - functions: 68.69, - lines: 68.35, - statements: 68.38, + functions: 68.42, + lines: 68.32, + statements: 68.35, }, }, From 4c4aa4b10b04d2be429db5fdda8507dc9738e1db Mon Sep 17 00:00:00 2001 From: ffmcgee Date: Mon, 3 Feb 2025 16:15:36 +0100 Subject: [PATCH 3/7] lint --- src/BaseProvider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseProvider.ts b/src/BaseProvider.ts index e37773da..0ee7f065 100644 --- a/src/BaseProvider.ts +++ b/src/BaseProvider.ts @@ -251,7 +251,7 @@ export abstract class BaseProvider extends SafeEventEmitter { } if (initialState) { - const { accounts, chainId, isUnlocked, networkVersion } = initialState; + const { accounts, chainId, networkVersion } = initialState; // EIP-1193 connect this._handleConnect(chainId); From bb8edc433f5cdd426eb69263cb60177dbf522f2e Mon Sep 17 00:00:00 2001 From: ffmcgee Date: Mon, 3 Feb 2025 16:22:56 +0100 Subject: [PATCH 4/7] test: update jest.config.js --- jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index bb62ae83..f04fc5a0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -45,7 +45,7 @@ const baseConfig = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 66.79, + branches: 67.47, functions: 68.42, lines: 68.32, statements: 68.35, From e109526c10e77f7fa632310c76eef7e2e247b0b4 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 3 Feb 2025 12:30:09 -0600 Subject: [PATCH 5/7] remove more instances of isUnlocked --- src/MetaMaskInpageProvider.test.ts | 3 --- src/MetaMaskInpageProvider.ts | 9 +-------- src/StreamProvider.test.ts | 6 ------ .../createExternalExtensionProvider.test.ts | 2 -- 4 files changed, 1 insertion(+), 19 deletions(-) diff --git a/src/MetaMaskInpageProvider.test.ts b/src/MetaMaskInpageProvider.test.ts index 623ffec4..8fc826a9 100644 --- a/src/MetaMaskInpageProvider.test.ts +++ b/src/MetaMaskInpageProvider.test.ts @@ -50,7 +50,6 @@ async function getInitializedProvider({ initialState: { accounts = [], chainId = '0x0', - isUnlocked = true, networkVersion = '0', isConnected = true, } = {}, @@ -81,7 +80,6 @@ async function getInitializedProvider({ result: { accounts, chainId, - isUnlocked, networkVersion, isConnected, }, @@ -1114,7 +1112,6 @@ describe('MetaMaskInpageProvider: Miscellanea', () => { return { accounts: ['0xabc'], chainId: '0x0', - isUnlocked: true, networkVersion: '0', isConnected: true, }; diff --git a/src/MetaMaskInpageProvider.ts b/src/MetaMaskInpageProvider.ts index e74ddf48..15928294 100644 --- a/src/MetaMaskInpageProvider.ts +++ b/src/MetaMaskInpageProvider.ts @@ -409,14 +409,7 @@ export class MetaMaskInpageProvider extends AbstractStreamProvider { * * @returns Promise resolving to true if MetaMask is currently unlocked. */ - isUnlocked: async () => { - if (!this._state.initialized) { - await new Promise((resolve) => { - this.on('_initialized', () => resolve()); - }); - } - return this._state.isUnlocked; - }, + isUnlocked: async () => !this._state.isPermanentlyDisconnected, /** * Make a batch RPC request. diff --git a/src/StreamProvider.test.ts b/src/StreamProvider.test.ts index 949c0181..122b9a41 100644 --- a/src/StreamProvider.test.ts +++ b/src/StreamProvider.test.ts @@ -38,7 +38,6 @@ describe('StreamProvider', () => { const accounts = ['0xabc']; const chainId = '0x1'; const networkVersion = '1'; - const isUnlocked = true; const isConnected = true; const streamProvider = new StreamProvider(new MockConnectionStream()); @@ -49,7 +48,6 @@ describe('StreamProvider', () => { return { accounts, chainId, - isUnlocked, networkVersion, isConnected, }; @@ -71,7 +69,6 @@ describe('StreamProvider', () => { const accounts = ['0xabc']; const chainId = '0x1'; const networkVersion = '1'; - const isUnlocked = true; const isConnected = true; const streamProvider = new StreamProvider(new MockConnectionStream()); @@ -80,7 +77,6 @@ describe('StreamProvider', () => { return { accounts, chainId, - isUnlocked, networkVersion, isConnected, }; @@ -411,7 +407,6 @@ describe('StreamProvider', () => { return { accounts: [], chainId: '0x0', - isUnlocked: true, networkVersion: '0', isConnected: true, }; @@ -450,7 +445,6 @@ describe('StreamProvider', () => { return { accounts: [], chainId: '0x0', - isUnlocked: true, networkVersion: '0', isConnected: true, }; diff --git a/src/extension-provider/createExternalExtensionProvider.test.ts b/src/extension-provider/createExternalExtensionProvider.test.ts index 267b4fec..fe95b567 100644 --- a/src/extension-provider/createExternalExtensionProvider.test.ts +++ b/src/extension-provider/createExternalExtensionProvider.test.ts @@ -43,7 +43,6 @@ async function getInitializedProvider({ initialState: { accounts = [], chainId = '0x0', - isUnlocked = true, networkVersion = '0', isConnected = true, } = {}, @@ -72,7 +71,6 @@ async function getInitializedProvider({ result: { accounts, chainId, - isUnlocked, networkVersion, isConnected, }, From 78775373a8a7bc81c31cac641798ddbe3535ac44 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 3 Feb 2025 12:39:57 -0600 Subject: [PATCH 6/7] update jest.config --- jest.config.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jest.config.js b/jest.config.js index f04fc5a0..93839c47 100644 --- a/jest.config.js +++ b/jest.config.js @@ -45,10 +45,10 @@ const baseConfig = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 67.47, - functions: 68.42, - lines: 68.32, - statements: 68.35, + branches: 69.23, + functions: 69.64, + lines: 69.23, + statements: 69.38, }, }, From 60bb23959907055a93c47b878d7ef8eac234bf1a Mon Sep 17 00:00:00 2001 From: ffmcgee Date: Mon, 3 Feb 2025 21:29:38 +0100 Subject: [PATCH 7/7] test: add test case for isUnlocked in MetaMaskInpageProvider.test.ts --- jest.config.js | 8 ++++---- src/MetaMaskInpageProvider.test.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/jest.config.js b/jest.config.js index 93839c47..0a20194d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -45,10 +45,10 @@ const baseConfig = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 69.23, - functions: 69.64, - lines: 69.23, - statements: 69.38, + branches: 69.63, + functions: 71.42, + lines: 70.27, + statements: 70.4, }, }, diff --git a/src/MetaMaskInpageProvider.test.ts b/src/MetaMaskInpageProvider.test.ts index 8fc826a9..0de88c22 100644 --- a/src/MetaMaskInpageProvider.test.ts +++ b/src/MetaMaskInpageProvider.test.ts @@ -1218,4 +1218,25 @@ describe('MetaMaskInpageProvider: Miscellanea', () => { expect(provider.selectedAddress).toBe('0xdeadbeef'); }); }); + + describe('_getExperimentalApi', () => { + let provider: any | MetaMaskInpageProvider; + + beforeEach(async () => { + provider = ( + await getInitializedProvider({ + initialState: { + accounts: ['0xdeadbeef'], + }, + }) + ).provider; + }); + + describe('isUnlocked', () => { + it('should return negated value of `state.isPermanentlyDisconnected`', async () => { + const isUnlocked = await provider._getExperimentalApi().isUnlocked(); + expect(isUnlocked).toBe(!provider._state.isPermanentlyDisconnected); + }); + }); + }); });