From ba40fcbcb43c5adcb3a961afd4050cdb2025b7a6 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Mon, 29 Oct 2018 21:55:13 +0100 Subject: Handle logout gracefully --- app/scripts/contentscript.js | 21 ++++++++++++--------- app/scripts/controllers/provider-approval.js | 25 +++++++++++++++---------- app/scripts/inpage.js | 14 +++++++++----- app/scripts/metamask-controller.js | 7 ++++++- 4 files changed, 42 insertions(+), 25 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index fdc04ba1b..0244f6fa0 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -129,21 +129,21 @@ function listenForProviderRequest () { origin: source.location.hostname, }) break - case 'ETHEREUM_QUERY_STATUS': + case 'ETHEREUM_IS_APPROVED': extension.runtime.sendMessage({ - action: 'init-status-request', + action: 'init-is-approved', origin: source.location.hostname, }) break - case 'METAMASK_UNLOCK_STATUS': + case 'METAMASK_IS_UNLOCKED': extension.runtime.sendMessage({ - action: 'init-unlock-request', + action: 'init-is-unlocked', }) break } }) - extension.runtime.onMessage.addListener(({ action, isEnabled, isUnlocked }) => { + extension.runtime.onMessage.addListener(({ action, isEnabled, isApproved, isUnlocked }) => { if (!action) { return } switch (action) { case 'approve-provider-request': @@ -153,11 +153,14 @@ function listenForProviderRequest () { case 'reject-provider-request': injectScript(`window.dispatchEvent(new CustomEvent('ethereumprovider', { detail: { error: 'User rejected provider access' }}))`) break - case 'answer-status-request': - injectScript(`window.dispatchEvent(new CustomEvent('ethereumproviderstatus', { detail: { isEnabled: ${isEnabled}}}))`) + case 'answer-is-approved': + injectScript(`window.dispatchEvent(new CustomEvent('ethereumisapproved', { detail: { isApproved: ${isApproved}}}))`) break - case 'answer-unlock-request': - injectScript(`window.dispatchEvent(new CustomEvent('metamaskunlockstatus', { detail: { isUnlocked: ${isUnlocked}}}))`) + case 'answer-is-unlocked': + injectScript(`window.dispatchEvent(new CustomEvent('metamaskisunlocked', { detail: { isUnlocked: ${isUnlocked}}}))`) + break + case 'metamask-set-locked': + injectScript(`window.dispatchEvent(new CustomEvent('metamasksetlocked', { detail: {}}))`) break } }) diff --git a/app/scripts/controllers/provider-approval.js b/app/scripts/controllers/provider-approval.js index fa2fb2cf8..3af165438 100644 --- a/app/scripts/controllers/provider-approval.js +++ b/app/scripts/controllers/provider-approval.js @@ -24,11 +24,11 @@ class ProviderApprovalController { case 'init-provider-request': this.handleProviderRequest(origin) break - case 'init-status-request': - this.handleProviderStatusRequest(origin) + case 'init-is-approved': + this.handleIsApproved(origin) break - case 'init-unlock-request': - this.handleUnlockRequest() + case 'init-is-unlocked': + this.handleIsUnlocked() break case 'init-privacy-request': this.handlePrivacyStatusRequest() @@ -44,7 +44,8 @@ class ProviderApprovalController { */ handleProviderRequest (origin) { this.store.updateState({ providerRequests: [{ origin }] }) - if (this.isApproved(origin)) { + const isUnlocked = this.keyringController.memStore.getState().isUnlocked + if (isUnlocked && this.isApproved(origin)) { this.approveProviderRequest(origin) return } @@ -56,14 +57,14 @@ class ProviderApprovalController { * * @param {string} origin - Origin of the window requesting provider status */ - async handleProviderStatusRequest (origin) { - const isEnabled = this.isApproved(origin) - this.platform && this.platform.sendMessage({ action: 'answer-status-request', isEnabled }, { active: true }) + async handleIsApproved (origin) { + const isApproved = this.isApproved(origin) + this.platform && this.platform.sendMessage({ action: 'answer-is-approved', isApproved }, { active: true }) } - handleUnlockRequest () { + handleIsUnlocked () { const isUnlocked = this.keyringController.memStore.getState().isUnlocked - this.platform && this.platform.sendMessage({ action: 'answer-unlock-request', isUnlocked }, { active: true }) + this.platform && this.platform.sendMessage({ action: 'answer-is-unlocked', isUnlocked }, { active: true }) } handlePrivacyStatusRequest () { @@ -119,6 +120,10 @@ class ProviderApprovalController { const privacyMode = this.preferencesController.getFeatureFlags().privacyMode return !privacyMode || this.approvedOrigins[origin] } + + setLocked () { + this.platform.sendMessage({ action: 'metamask-set-locked' }) + } } module.exports = ProviderApprovalController diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index cadc61727..49a18c5e9 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -33,6 +33,10 @@ inpageProvider.setMaxListeners(100) var isEnabled = false var warned = false +window.addEventListener('metamasksetlocked', () => { + isEnabled = false +}) + // augment the provider with its enable method inpageProvider.enable = function () { return new Promise((resolve, reject) => { @@ -91,14 +95,14 @@ inpageProvider._metamask = new Proxy({ */ isApproved: function() { return new Promise((resolve, reject) => { - window.addEventListener('ethereumproviderstatus', ({ detail }) => { + window.addEventListener('ethereumisapproved', ({ detail }) => { if (typeof detail.error !== 'undefined') { reject(detail.error) } else { - resolve(!!detail.isEnabled) + resolve(!!detail.isApproved) } }) - window.postMessage({ type: 'ETHEREUM_QUERY_STATUS' }, '*') + window.postMessage({ type: 'ETHEREUM_IS_APPROVED' }, '*') }) }, @@ -109,14 +113,14 @@ inpageProvider._metamask = new Proxy({ */ isUnlocked: function () { return new Promise((resolve, reject) => { - window.addEventListener('metamaskunlockstatus', ({ detail }) => { + window.addEventListener('metamaskisunlocked', ({ detail }) => { if (typeof detail.error !== 'undefined') { reject(detail.error) } else { resolve(!!detail.isUnlocked) } }) - window.postMessage({ type: 'METAMASK_UNLOCK_STATUS' }, '*') + window.postMessage({ type: 'METAMASK_IS_UNLOCKED' }, '*') }) }, }, { diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 8930a3f2d..bf1df7ff5 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -422,7 +422,7 @@ module.exports = class MetamaskController extends EventEmitter { setAddressBook: nodeify(addressBookController.setAddressBook, addressBookController), // KeyringController - setLocked: nodeify(keyringController.setLocked, keyringController), + setLocked: nodeify(this.setLocked, this), createNewVaultAndKeychain: nodeify(this.createNewVaultAndKeychain, this), createNewVaultAndRestore: nodeify(this.createNewVaultAndRestore, this), addNewKeyring: nodeify(keyringController.addNewKeyring, keyringController), @@ -1576,4 +1576,9 @@ module.exports = class MetamaskController extends EventEmitter { whitelistPhishingDomain (hostname) { return this.blacklistController.whitelistDomain(hostname) } + + setLocked() { + this.providerApprovalController.setLocked() + return this.keyringController.setLocked() + } } -- cgit v1.2.3