1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Differentiate isEnabled and isApproved hook

This commit is contained in:
bitpshr 2018-10-17 18:29:40 -04:00 committed by Dan Finlay
parent 3a2ade4e84
commit 573139b935

View File

@ -27,9 +27,10 @@ var metamaskStream = new LocalMessageDuplexStream({
// compose the inpage provider
var inpageProvider = new MetamaskInpageProvider(metamaskStream)
// set a high max listener count to avoid unnecesary warnings
inpageProvider.setMaxListeners(100)
var originApproved = false
var isEnabled = false
// Augment the provider with its enable method
inpageProvider.enable = function () {
@ -38,11 +39,11 @@ inpageProvider.enable = function () {
if (typeof detail.error !== 'undefined') {
reject(detail.error)
} else {
originApproved = true
inpageProvider.sendAsync({ method: 'eth_accounts', params: [] }, (error, response) => {
if (error) {
reject(error)
} else {
isEnabled = true
resolve(response.result)
}
})
@ -53,18 +54,26 @@ inpageProvider.enable = function () {
}
inpageProvider.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(originApproved && !!detail.isEnabled)
resolve(!!detail.isEnabled)
}
})
window.postMessage({ type: 'ETHEREUM_QUERY_STATUS' }, '*')
})
}
inpageProvider.isUnlocked = function () {
}
// 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, {