Skip to content

Commit

Permalink
Merge branch 'main' into connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks authored Dec 7, 2020
2 parents 1c2f45c + 89ea071 commit 2068cd5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
53 changes: 20 additions & 33 deletions src/MetaMaskInpageProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {

this._initializeState()

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

if (method === 'metamask_accountsChanged') {
this._handleAccountsChanged(result)
this._handleAccountsChanged(params)

} else if (method === 'metamask_unlockStateChanged') {
this._handleUnlockStateChanged(result)
this._handleUnlockStateChanged(params)
} else if (method === 'metamask_chainChanged') {
this._handleChainChanged(result)
this._handleChainChanged(params)
} else if (EMITTED_NOTIFICATIONS.includes(method)) {
this.emit('message', {
type: method,
Expand Down Expand Up @@ -329,7 +329,7 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
this.emit('connect', { chainId })

this._handleChainChanged({ chainId, networkVersion })
this._handleUnlockStateChanged(isUnlocked)
this._handleUnlockStateChanged({ accounts, isUnlocked })
this._handleAccountsChanged(accounts)
} catch (error) {
this._log.error(
Expand All @@ -349,9 +349,8 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
* @private
* @param {Object} payload - The RPC request object.
* @param {Function} callback - The consumer's callback.
* @param {boolean} [isInternal=false] - Whether the request is internal.
*/
_rpcRequest (payload, callback, isInternal = false) {
_rpcRequest (payload, callback) {
let cb = callback

if (!Array.isArray(payload)) {
Expand All @@ -370,7 +369,6 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
this._handleAccountsChanged(
res.result || [],
payload.method === 'eth_accounts',
isInternal,
)
callback(err, res)
}
Expand Down Expand Up @@ -499,10 +497,8 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
* @param {string[]} accounts - The new accounts value.
* @param {boolean} isEthAccounts - Whether the accounts value was returned by
* a call to eth_accounts.
* @param {boolean} isInternal - Whether the accounts value was returned by an
* internally initiated request.
*/
_handleAccountsChanged (accounts, isEthAccounts = false, isInternal = false) {
_handleAccountsChanged (accounts, isEthAccounts = false) {
let _accounts = accounts

if (!Array.isArray(accounts)) {
Expand All @@ -517,8 +513,8 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
if (!dequal(this._state.accounts, _accounts)) {

// we should always have the correct accounts even before eth_accounts
// returns, except in cases where isInternal is true
if (isEthAccounts && this._state.accounts !== null && !isInternal) {
// returns
if (isEthAccounts && this._state.accounts !== null) {
this._log.error(
`MetaMask: 'eth_accounts' unexpectedly updated accounts. Please report this bug.`,
_accounts,
Expand All @@ -538,36 +534,27 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
}

/**
* Upon receipt of a new isUnlocked state, emits the corresponding event
* and sets relevant public state.
* 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.
*
* @private
* @param {boolean} isUnlocked - The latest isUnlocked value.
* @param {Object} opts - Options bag.
* @param {string[]} [opts.accounts] - The exposed accounts, if any.
* @param {boolean} opts.isUnlocked - The latest isUnlocked value.
*/
_handleUnlockStateChanged (isUnlocked) {
_handleUnlockStateChanged ({ accounts, isUnlocked }) {
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

if (isUnlocked) {

// this will get the exposed accounts, if any
try {
this._rpcRequest(
{ method: 'eth_accounts', params: [] },
NOOP,
true, // indicating that eth_accounts _should_ update accounts
)
} catch (_) { /* no-op */ }
} else {
// accounts are never exposed when the extension is locked
this._handleAccountsChanged([])
}
this._handleAccountsChanged(accounts || [])
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/MetaMaskInpageProvider.rpc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('MetaMaskInpageProvider: RPC', () => {
)

expect(provider._handleAccountsChanged)
.toHaveBeenCalledWith(['0x1'], true, false)
.toHaveBeenCalledWith(['0x1'], true)

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

expect(provider._handleAccountsChanged)
.toHaveBeenCalledWith([], true, false)
.toHaveBeenCalledWith([], true)

expect(err).toStrictEqual(new Error('foo'))
expect(res).toStrictEqual({ error: 'foo' })
Expand Down

0 comments on commit 2068cd5

Please sign in to comment.