Skip to content

Commit 89ea071

Browse files
authored
Receive accounts with isUnlocked notification (#121)
This refactors some methods on the main class to account for the fact that accounts will be received with with the `isUnlocked` notification.
1 parent 87ef029 commit 89ea071

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

src/MetaMaskInpageProvider.js

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,17 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
147147

148148
this._initializeState()
149149

150-
// handle JSON RPC notifications
150+
// handle JSON-RPC notifications
151151
jsonRpcConnection.events.on('notification', (payload) => {
152-
const { method, params, result } = payload
152+
const { method, params } = payload
153153

154154
if (method === 'metamask_accountsChanged') {
155-
this._handleAccountsChanged(result)
155+
this._handleAccountsChanged(params)
156156

157157
} else if (method === 'metamask_unlockStateChanged') {
158-
this._handleUnlockStateChanged(result)
158+
this._handleUnlockStateChanged(params)
159159
} else if (method === 'metamask_chainChanged') {
160-
this._handleChainChanged(result)
160+
this._handleChainChanged(params)
161161
} else if (EMITTED_NOTIFICATIONS.includes(method)) {
162162
this.emit('notification', payload) // deprecated
163163
this.emit('message', {
@@ -323,7 +323,7 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
323323
this.emit('connect', { chainId })
324324

325325
this._handleChainChanged({ chainId, networkVersion })
326-
this._handleUnlockStateChanged(isUnlocked)
326+
this._handleUnlockStateChanged({ accounts, isUnlocked })
327327
this._handleAccountsChanged(accounts)
328328
} catch (error) {
329329
this._log.error(
@@ -343,9 +343,8 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
343343
* @private
344344
* @param {Object} payload - The RPC request object.
345345
* @param {Function} callback - The consumer's callback.
346-
* @param {boolean} [isInternal=false] - Whether the request is internal.
347346
*/
348-
_rpcRequest (payload, callback, isInternal = false) {
347+
_rpcRequest (payload, callback) {
349348
let cb = callback
350349

351350
if (!Array.isArray(payload)) {
@@ -364,7 +363,6 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
364363
this._handleAccountsChanged(
365364
res.result || [],
366365
payload.method === 'eth_accounts',
367-
isInternal,
368366
)
369367
callback(err, res)
370368
}
@@ -403,10 +401,8 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
403401
* @param {string[]} accounts - The new accounts value.
404402
* @param {boolean} isEthAccounts - Whether the accounts value was returned by
405403
* a call to eth_accounts.
406-
* @param {boolean} isInternal - Whether the accounts value was returned by an
407-
* internally initiated request.
408404
*/
409-
_handleAccountsChanged (accounts, isEthAccounts = false, isInternal = false) {
405+
_handleAccountsChanged (accounts, isEthAccounts = false) {
410406
let _accounts = accounts
411407

412408
if (!Array.isArray(accounts)) {
@@ -421,8 +417,8 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
421417
if (!dequal(this._state.accounts, _accounts)) {
422418

423419
// we should always have the correct accounts even before eth_accounts
424-
// returns, except in cases where isInternal is true
425-
if (isEthAccounts && this._state.accounts !== null && !isInternal) {
420+
// returns
421+
if (isEthAccounts && this._state.accounts !== null) {
426422
this._log.error(
427423
`MetaMask: 'eth_accounts' unexpectedly updated accounts. Please report this bug.`,
428424
_accounts,
@@ -477,36 +473,27 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
477473
}
478474

479475
/**
480-
* Upon receipt of a new isUnlocked state, emits the corresponding event
481-
* and sets relevant public state.
476+
* Upon receipt of a new isUnlocked state, sets relevant public state.
477+
* Calls the accounts changed handler with the received accounts, or an empty
478+
* array.
479+
*
482480
* Does nothing if the received value is equal to the existing value.
481+
* There are no lock/unlock events.
483482
*
484483
* @private
485-
* @param {boolean} isUnlocked - The latest isUnlocked value.
484+
* @param {Object} opts - Options bag.
485+
* @param {string[]} [opts.accounts] - The exposed accounts, if any.
486+
* @param {boolean} opts.isUnlocked - The latest isUnlocked value.
486487
*/
487-
_handleUnlockStateChanged (isUnlocked) {
488+
_handleUnlockStateChanged ({ accounts, isUnlocked }) {
488489
if (typeof isUnlocked !== 'boolean') {
489490
this._log.error('MetaMask: Received invalid isUnlocked parameter. Please report this bug.')
490491
return
491492
}
492493

493494
if (isUnlocked !== this._state.isUnlocked) {
494495
this._state.isUnlocked = isUnlocked
495-
496-
if (isUnlocked) {
497-
498-
// this will get the exposed accounts, if any
499-
try {
500-
this._rpcRequest(
501-
{ method: 'eth_accounts', params: [] },
502-
NOOP,
503-
true, // indicating that eth_accounts _should_ update accounts
504-
)
505-
} catch (_) { /* no-op */ }
506-
} else {
507-
// accounts are never exposed when the extension is locked
508-
this._handleAccountsChanged([])
509-
}
496+
this._handleAccountsChanged(accounts || [])
510497
}
511498
}
512499

test/MetaMaskInpageProvider.rpc.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ describe('MetaMaskInpageProvider: RPC', () => {
420420
)
421421

422422
expect(provider._handleAccountsChanged)
423-
.toHaveBeenCalledWith(['0x1'], true, false)
423+
.toHaveBeenCalledWith(['0x1'], true)
424424

425425
expect(err).toBeNull()
426426
expect(res).toStrictEqual({ result: ['0x1'] })
@@ -445,7 +445,7 @@ describe('MetaMaskInpageProvider: RPC', () => {
445445
)
446446

447447
expect(provider._handleAccountsChanged)
448-
.toHaveBeenCalledWith([], true, false)
448+
.toHaveBeenCalledWith([], true)
449449

450450
expect(err).toStrictEqual(new Error('foo'))
451451
expect(res).toStrictEqual({ error: 'foo' })

0 commit comments

Comments
 (0)