From 32630b68dfae257a59bf1ce985e9a04ca1ca58b4 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Mon, 22 Oct 2018 15:08:26 -0400 Subject: [PATCH] Move convenience methods to _metamask namespace --- app/scripts/inpage.js | 83 ++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 72f7033f9..9d50a695f 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -10,8 +10,8 @@ restoreContextAfterImports() log.setDefaultLevel(process.env.METAMASK_DEBUG ? 'debug' : 'warn') -console.warn('ATTENTION: In an effort to improve user privacy, MetaMask will ' + -'stop exposing user accounts to dapps by default beginning November 2nd, 2018. ' + +console.warn('ATTENTION: In an effort to improve user privacy, MetaMask ' + +'stopped exposing user accounts to dapps by default beginning November 2nd, 2018. ' + 'Dapps should call provider.enable() in order to view and use accounts. Please see ' + 'https://bit.ly/2QQHXvF for complete information and up-to-date example code.') @@ -31,8 +31,9 @@ var inpageProvider = new MetamaskInpageProvider(metamaskStream) // set a high max listener count to avoid unnecesary warnings inpageProvider.setMaxListeners(100) var isEnabled = false +var warned = false -// Augment the provider with its enable method +// augment the provider with its enable method inpageProvider.enable = function () { return new Promise((resolve, reject) => { window.addEventListener('ethereumprovider', ({ detail }) => { @@ -53,35 +54,61 @@ inpageProvider.enable = function () { }) } -inpageProvider.isEnabled = function () { - return isEnabled -} +// add metamask-specific convenience methods +inpageProvider._metamask = new Proxy({ + /** + * Determines if this domain is currently enabled + * + * @returns {boolean} - true if this domain is currently enabled + */ + isEnabled: function () { + return isEnabled + }, -inpageProvider.isApproved = function () { - return new Promise((resolve, reject) => { - window.addEventListener('ethereumproviderstatus', ({ detail }) => { - if (typeof detail.error !== 'undefined') { - reject(detail.error) - } else { - resolve(!!detail.isEnabled) - } + /** + * Determines if this domain has been previously approved + * + * @returns {Promise} - Promise resolving to true if this domain has been previously approved + */ + isApproved: 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_QUERY_STATUS' }, '*') }) - window.postMessage({ type: 'ETHEREUM_QUERY_STATUS' }, '*') - }) -} + }, -inpageProvider.isUnlocked = function () { - return new Promise((resolve, reject) => { - window.addEventListener('metamaskunlockstatus', ({ detail }) => { - if (typeof detail.error !== 'undefined') { - reject(detail.error) - } else { - resolve(!!detail.isUnlocked) - } + /** + * Determines if MetaMask is unlocked by the user + * + * @returns {Promise} - Promise resolving to true if MetaMask is currently unlocked + */ + 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' }, '*') }) - window.postMessage({ type: 'METAMASK_UNLOCK_STATUS' }, '*') - }) -} + }, +}, { + get: function(obj, prop) { + !warned && console.warn('Heads up! ethereum._metamask exposes convenience methods that have ' + + 'not been standardized yet. This means that these methods may not be implemented ' + + 'in other dapp browsers.') + warned = true + return obj[prop] + }, +}) // 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