mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +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.
|
* handles posting these messages automatically.
|
||||||
*/
|
*/
|
||||||
function listenForProviderRequest () {
|
function listenForProviderRequest () {
|
||||||
window.addEventListener('message', (event) => {
|
window.addEventListener('message', ({ source, data }) => {
|
||||||
if (event.source !== window) { return }
|
if (source !== window || !data || !data.type) { return }
|
||||||
if (!event.data || !event.data.type || event.data.type !== 'ETHEREUM_ENABLE_PROVIDER') { return }
|
switch (data.type) {
|
||||||
extension.runtime.sendMessage({
|
case 'ETHEREUM_ENABLE_PROVIDER':
|
||||||
action: 'init-provider-request',
|
extension.runtime.sendMessage({
|
||||||
origin: event.source.location.hostname,
|
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 }
|
if (!action) { return }
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'approve-provider-request':
|
case 'approve-provider-request':
|
||||||
@ -142,6 +151,9 @@ function listenForProviderRequest () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
|
case 'provider-status':
|
||||||
|
injectScript(`window.dispatchEvent(new CustomEvent('ethereumproviderstatus', { detail: { isEnabled: ${isEnabled}}}))`)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,15 @@ class ProviderApprovalController {
|
|||||||
this.publicConfigStore = publicConfigStore
|
this.publicConfigStore = publicConfigStore
|
||||||
this.approvedOrigins = {}
|
this.approvedOrigins = {}
|
||||||
platform && platform.addMessageListener && platform.addMessageListener(({ action, origin }) => {
|
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()
|
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
|
* 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
|
// 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
|
// `sendAsync` method on the prototype, causing `this` reference issues with drizzle
|
||||||
const proxiedInpageProvider = new Proxy(inpageProvider, {
|
const proxiedInpageProvider = new Proxy(inpageProvider, {
|
||||||
@ -60,6 +73,7 @@ const proxiedInpageProvider = new Proxy(inpageProvider, {
|
|||||||
|
|
||||||
window.ethereum = proxiedInpageProvider
|
window.ethereum = proxiedInpageProvider
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// setup web3
|
// setup web3
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user