mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
EIP-1102: add isEnabled convenience method to provider
This commit is contained in:
parent
89b4aa5d62
commit
bfcb73ad53
@ -116,16 +116,25 @@ function setupStreams () {
|
||||
* handles posting these messages automatically.
|
||||
*/
|
||||
function listenForProviderRequest () {
|
||||
window.addEventListener('message', (event) => {
|
||||
if (event.source !== window) { return }
|
||||
if (!event.data || !event.data.type || event.data.type !== 'ETHEREUM_ENABLE_PROVIDER') { return }
|
||||
extension.runtime.sendMessage({
|
||||
action: 'init-provider-request',
|
||||
origin: event.source.location.hostname,
|
||||
})
|
||||
window.addEventListener('message', ({ source, data }) => {
|
||||
if (source !== window || !data || !data.type) { return }
|
||||
switch (data.type) {
|
||||
case 'ETHEREUM_ENABLE_PROVIDER':
|
||||
extension.runtime.sendMessage({
|
||||
action: 'init-provider-request',
|
||||
origin: source.location.hostname,
|
||||
})
|
||||
break
|
||||
case 'ETHEREUM_PROVIDER_STATUS':
|
||||
extension.runtime.sendMessage({
|
||||
action: 'provider-status-request',
|
||||
origin: source.location.hostname,
|
||||
})
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
extension.runtime.onMessage.addListener(({ action }) => {
|
||||
extension.runtime.onMessage.addListener(({ action, isEnabled }) => {
|
||||
if (!action) { return }
|
||||
switch (action) {
|
||||
case 'approve-provider-request':
|
||||
@ -142,6 +151,9 @@ function listenForProviderRequest () {
|
||||
})
|
||||
})
|
||||
break
|
||||
case 'provider-status':
|
||||
injectScript(`window.dispatchEvent(new CustomEvent('ethereumproviderstatus', { detail: { isEnabled: ${isEnabled}}}))`)
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -18,7 +18,15 @@ class ProviderApprovalController {
|
||||
this.publicConfigStore = publicConfigStore
|
||||
this.approvedOrigins = {}
|
||||
platform && platform.addMessageListener && platform.addMessageListener(({ action, origin }) => {
|
||||
action && action === 'init-provider-request' && this.handleProviderRequest(origin)
|
||||
if (!action) { return }
|
||||
switch (action) {
|
||||
case 'init-provider-request':
|
||||
this.handleProviderRequest(origin)
|
||||
break
|
||||
case 'provider-status-request':
|
||||
this.handleProviderStatusRequest(origin)
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -36,6 +44,16 @@ class ProviderApprovalController {
|
||||
this.openPopup && this.openPopup()
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by a tab to detemrine if a full Ethereum provider API is exposed
|
||||
*
|
||||
* @param {string} origin - Origin of the window requesting provider status
|
||||
*/
|
||||
async handleProviderStatusRequest (origin) {
|
||||
const isEnabled = await this.isApproved(origin)
|
||||
this.platform && this.platform.sendMessage({ action: 'provider-status', isEnabled }, { active: true })
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a user approves access to a full Ethereum provider API
|
||||
*
|
||||
|
@ -50,6 +50,19 @@ inpageProvider.enable = function () {
|
||||
})
|
||||
}
|
||||
|
||||
inpageProvider.isEnabled = function () {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.addEventListener('ethereumproviderstatus', ({ detail }) => {
|
||||
if (typeof detail.error !== 'undefined') {
|
||||
reject(detail.error)
|
||||
} else {
|
||||
resolve(detail.isEnabled)
|
||||
}
|
||||
})
|
||||
window.postMessage({ type: 'ETHEREUM_PROVIDER_STATUS' }, '*')
|
||||
})
|
||||
}
|
||||
|
||||
// Work around for web3@1.0 deleting the bound `sendAsync` but not the unbound
|
||||
// `sendAsync` method on the prototype, causing `this` reference issues with drizzle
|
||||
const proxiedInpageProvider = new Proxy(inpageProvider, {
|
||||
@ -60,6 +73,7 @@ const proxiedInpageProvider = new Proxy(inpageProvider, {
|
||||
|
||||
window.ethereum = proxiedInpageProvider
|
||||
|
||||
|
||||
//
|
||||
// setup web3
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user