1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Add isUnlocked provider hook

This commit is contained in:
bitpshr 2018-10-17 18:38:31 -04:00 committed by Dan Finlay
parent 573139b935
commit 84874a639d
4 changed files with 29 additions and 3 deletions

View File

@ -131,10 +131,15 @@ function listenForProviderRequest () {
origin: source.location.hostname,
})
break
case 'METAMASK_UNLOCK_STATUS':
extension.runtime.sendMessage({
action: 'init-unlock-request',
})
break
}
})
extension.runtime.onMessage.addListener(({ action, isEnabled }) => {
extension.runtime.onMessage.addListener(({ action, isEnabled, isUnlocked }) => {
if (!action) { return }
switch (action) {
case 'approve-provider-request':
@ -147,6 +152,9 @@ function listenForProviderRequest () {
case 'answer-status-request':
injectScript(`window.dispatchEvent(new CustomEvent('ethereumproviderstatus', { detail: { isEnabled: ${isEnabled}}}))`)
break
case 'answer-unlock-request':
injectScript(`window.dispatchEvent(new CustomEvent('metamaskunlockstatus', { detail: { isUnlocked: ${isUnlocked}}}))`)
break
}
})
}

View File

@ -9,7 +9,7 @@ class ProviderApprovalController {
*
* @param {Object} [config] - Options to configure controller
*/
constructor ({ closePopup, openPopup, platform, preferencesController, publicConfigStore } = {}) {
constructor ({ closePopup, openPopup, keyringController, platform, preferencesController, publicConfigStore } = {}) {
this.store = new ObservableStore()
this.closePopup = closePopup
this.openPopup = openPopup
@ -17,6 +17,7 @@ class ProviderApprovalController {
this.publicConfigStore = publicConfigStore
this.approvedOrigins = {}
this.preferencesController = preferencesController
this.keyringController = keyringController
platform && platform.addMessageListener && platform.addMessageListener(({ action, origin }) => {
if (!action) { return }
switch (action) {
@ -26,6 +27,8 @@ class ProviderApprovalController {
case 'init-status-request':
this.handleProviderStatusRequest(origin)
break
case 'init-unlock-request':
this.handleUnlockRequest()
case 'init-privacy-request':
this.handlePrivacyStatusRequest()
}
@ -56,6 +59,11 @@ class ProviderApprovalController {
this.platform && this.platform.sendMessage({ action: 'answer-status-request', isEnabled }, { active: true })
}
handleUnlockRequest() {
const isUnlocked = this.keyringController.memStore.getState().isUnlocked
this.platform && this.platform.sendMessage({ action: 'answer-unlock-request', isUnlocked }, { active: true })
}
handlePrivacyStatusRequest () {
const privacyMode = this.preferencesController.getFeatureFlags().privacyMode
if (!privacyMode) {

View File

@ -71,7 +71,16 @@ inpageProvider.isApproved = function () {
}
inpageProvider.isUnlocked = function () {
return new Promise((resolve, reject) => {
window.addEventListener('metamaskunlockstatus', ({ detail }) => {
if (typeof detail.error !== 'undefined') {
reject(detail.error)
} else {
resolve(!!detail.isUnlocked)
}
})
window.postMessage({ type: 'METAMASK_UNLOCK_STATUS' }, '*')
})
}
// Work around for web3@1.0 deleting the bound `sendAsync` but not the unbound

View File

@ -226,6 +226,7 @@ module.exports = class MetamaskController extends EventEmitter {
platform: opts.platform,
preferencesController: this.preferencesController,
publicConfigStore: this.publicConfigStore,
keyringController: this.keyringController,
})
this.store.updateStructure({