aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbitpshr <mail@bitpshr.net>2018-10-23 03:08:26 +0800
committerDan Finlay <542863+danfinlay@users.noreply.github.com>2018-11-06 07:07:09 +0800
commit32630b68dfae257a59bf1ce985e9a04ca1ca58b4 (patch)
tree9f8c13aaadc78f89998c48405e7ea13bd9c4521d
parent504f4a50f13b12588576f2f06bb2e371bd966dfb (diff)
downloadtangerine-wallet-browser-32630b68dfae257a59bf1ce985e9a04ca1ca58b4.tar.gz
tangerine-wallet-browser-32630b68dfae257a59bf1ce985e9a04ca1ca58b4.tar.zst
tangerine-wallet-browser-32630b68dfae257a59bf1ce985e9a04ca1ca58b4.zip
Move convenience methods to _metamask namespace
-rw-r--r--app/scripts/inpage.js83
1 files 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<boolean>} - 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<boolean>} - 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