mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Handle logout gracefully
This commit is contained in:
parent
f02e18dd80
commit
ba40fcbcb4
@ -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
|
||||
}
|
||||
})
|
||||
|
@ -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
|
||||
|
@ -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' }, '*')
|
||||
})
|
||||
},
|
||||
}, {
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user