1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00

Move convenience methods to _metamask namespace

This commit is contained in:
bitpshr 2018-10-22 15:08:26 -04:00 committed by Dan Finlay
parent 504f4a50f1
commit 32630b68df

View File

@ -10,8 +10,8 @@ restoreContextAfterImports()
log.setDefaultLevel(process.env.METAMASK_DEBUG ? 'debug' : 'warn') log.setDefaultLevel(process.env.METAMASK_DEBUG ? 'debug' : 'warn')
console.warn('ATTENTION: In an effort to improve user privacy, MetaMask will ' + console.warn('ATTENTION: In an effort to improve user privacy, MetaMask ' +
'stop exposing user accounts to dapps by default beginning November 2nd, 2018. ' + '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 ' + '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.') '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 // set a high max listener count to avoid unnecesary warnings
inpageProvider.setMaxListeners(100) inpageProvider.setMaxListeners(100)
var isEnabled = false var isEnabled = false
var warned = false
// Augment the provider with its enable method // augment the provider with its enable method
inpageProvider.enable = function () { inpageProvider.enable = function () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.addEventListener('ethereumprovider', ({ detail }) => { window.addEventListener('ethereumprovider', ({ detail }) => {
@ -53,35 +54,61 @@ inpageProvider.enable = function () {
}) })
} }
inpageProvider.isEnabled = function () { // add metamask-specific convenience methods
return isEnabled 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) => { * Determines if this domain has been previously approved
window.addEventListener('ethereumproviderstatus', ({ detail }) => { *
if (typeof detail.error !== 'undefined') { * @returns {Promise<boolean>} - Promise resolving to true if this domain has been previously approved
reject(detail.error) */
} else { isApproved: function() {
resolve(!!detail.isEnabled) 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) => { * Determines if MetaMask is unlocked by the user
window.addEventListener('metamaskunlockstatus', ({ detail }) => { *
if (typeof detail.error !== 'undefined') { * @returns {Promise<boolean>} - Promise resolving to true if MetaMask is currently unlocked
reject(detail.error) */
} else { isUnlocked: function () {
resolve(!!detail.isUnlocked) 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 // 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