mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Add isUnlocked provider hook
This commit is contained in:
parent
573139b935
commit
84874a639d
@ -131,10 +131,15 @@ function listenForProviderRequest () {
|
|||||||
origin: source.location.hostname,
|
origin: source.location.hostname,
|
||||||
})
|
})
|
||||||
break
|
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 }
|
if (!action) { return }
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'approve-provider-request':
|
case 'approve-provider-request':
|
||||||
@ -147,6 +152,9 @@ function listenForProviderRequest () {
|
|||||||
case 'answer-status-request':
|
case 'answer-status-request':
|
||||||
injectScript(`window.dispatchEvent(new CustomEvent('ethereumproviderstatus', { detail: { isEnabled: ${isEnabled}}}))`)
|
injectScript(`window.dispatchEvent(new CustomEvent('ethereumproviderstatus', { detail: { isEnabled: ${isEnabled}}}))`)
|
||||||
break
|
break
|
||||||
|
case 'answer-unlock-request':
|
||||||
|
injectScript(`window.dispatchEvent(new CustomEvent('metamaskunlockstatus', { detail: { isUnlocked: ${isUnlocked}}}))`)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ class ProviderApprovalController {
|
|||||||
*
|
*
|
||||||
* @param {Object} [config] - Options to configure controller
|
* @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.store = new ObservableStore()
|
||||||
this.closePopup = closePopup
|
this.closePopup = closePopup
|
||||||
this.openPopup = openPopup
|
this.openPopup = openPopup
|
||||||
@ -17,6 +17,7 @@ class ProviderApprovalController {
|
|||||||
this.publicConfigStore = publicConfigStore
|
this.publicConfigStore = publicConfigStore
|
||||||
this.approvedOrigins = {}
|
this.approvedOrigins = {}
|
||||||
this.preferencesController = preferencesController
|
this.preferencesController = preferencesController
|
||||||
|
this.keyringController = keyringController
|
||||||
platform && platform.addMessageListener && platform.addMessageListener(({ action, origin }) => {
|
platform && platform.addMessageListener && platform.addMessageListener(({ action, origin }) => {
|
||||||
if (!action) { return }
|
if (!action) { return }
|
||||||
switch (action) {
|
switch (action) {
|
||||||
@ -26,6 +27,8 @@ class ProviderApprovalController {
|
|||||||
case 'init-status-request':
|
case 'init-status-request':
|
||||||
this.handleProviderStatusRequest(origin)
|
this.handleProviderStatusRequest(origin)
|
||||||
break
|
break
|
||||||
|
case 'init-unlock-request':
|
||||||
|
this.handleUnlockRequest()
|
||||||
case 'init-privacy-request':
|
case 'init-privacy-request':
|
||||||
this.handlePrivacyStatusRequest()
|
this.handlePrivacyStatusRequest()
|
||||||
}
|
}
|
||||||
@ -56,6 +59,11 @@ class ProviderApprovalController {
|
|||||||
this.platform && this.platform.sendMessage({ action: 'answer-status-request', isEnabled }, { active: true })
|
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 () {
|
handlePrivacyStatusRequest () {
|
||||||
const privacyMode = this.preferencesController.getFeatureFlags().privacyMode
|
const privacyMode = this.preferencesController.getFeatureFlags().privacyMode
|
||||||
if (!privacyMode) {
|
if (!privacyMode) {
|
||||||
|
@ -71,7 +71,16 @@ inpageProvider.isApproved = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inpageProvider.isUnlocked = 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
|
// Work around for web3@1.0 deleting the bound `sendAsync` but not the unbound
|
||||||
|
@ -226,6 +226,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
platform: opts.platform,
|
platform: opts.platform,
|
||||||
preferencesController: this.preferencesController,
|
preferencesController: this.preferencesController,
|
||||||
publicConfigStore: this.publicConfigStore,
|
publicConfigStore: this.publicConfigStore,
|
||||||
|
keyringController: this.keyringController,
|
||||||
})
|
})
|
||||||
|
|
||||||
this.store.updateStructure({
|
this.store.updateStructure({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user