diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/contentscript.js | 28 | ||||
-rw-r--r-- | app/scripts/controllers/provider-approval.js | 20 | ||||
-rw-r--r-- | app/scripts/inpage.js | 14 |
3 files changed, 53 insertions, 9 deletions
diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 1788cfc36..b1c1e9a0d 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -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 } }) } diff --git a/app/scripts/controllers/provider-approval.js b/app/scripts/controllers/provider-approval.js index 8c7520d59..918fc8ad0 100644 --- a/app/scripts/controllers/provider-approval.js +++ b/app/scripts/controllers/provider-approval.js @@ -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 + } }) } @@ -37,6 +45,16 @@ class ProviderApprovalController { } /** + * 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 * * @param {string} origin - Origin of the target window to approve provider access diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 5b5c02c26..c5f4ee4c9 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -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 // |