feature/default_network_editable
bitpshr 6 years ago committed by Dan Finlay
parent 0ab5c1c6e2
commit 1ce3591c8e
  1. 10
      app/scripts/contentscript.js
  2. 53
      app/scripts/inpage.js

@ -151,20 +151,20 @@ function listenForProviderRequest () {
switch (action) { switch (action) {
case 'approve-provider-request': case 'approve-provider-request':
isEnabled = true isEnabled = true
injectScript(`window.dispatchEvent(new CustomEvent('ethereumprovider', { detail: {}}))`) window.postMessage({ type: 'ethereumprovider' }, '*')
break break
case 'reject-provider-request': case 'reject-provider-request':
injectScript(`window.dispatchEvent(new CustomEvent('ethereumprovider', { detail: { error: 'User rejected provider access' }}))`) window.postMessage({ type: 'ethereumprovider', error: 'User rejected provider access' }, '*')
break break
case 'answer-is-approved': case 'answer-is-approved':
injectScript(`window.dispatchEvent(new CustomEvent('ethereumisapproved', { detail: { isApproved: ${isApproved}, caching: ${caching}}}))`) window.postMessage({ type: 'ethereumisapproved', isApproved, caching }, '*')
break break
case 'answer-is-unlocked': case 'answer-is-unlocked':
injectScript(`window.dispatchEvent(new CustomEvent('metamaskisunlocked', { detail: { isUnlocked: ${isUnlocked}}}))`) window.postMessage({ type: 'metamaskisunlocked', isUnlocked }, '*')
break break
case 'metamask-set-locked': case 'metamask-set-locked':
isEnabled = false isEnabled = false
injectScript(`window.dispatchEvent(new CustomEvent('metamasksetlocked', { detail: {}}))`) window.postMessage({ type: 'metamasksetlocked' }, '*')
break break
} }
}) })

@ -22,6 +22,14 @@ console.warn('ATTENTION: In an effort to improve user privacy, MetaMask ' +
'accounts. Please see https://bit.ly/2QQHXvF for complete information and up-to-date ' + 'accounts. Please see https://bit.ly/2QQHXvF for complete information and up-to-date ' +
'example code.') 'example code.')
function once(messageType, handler) {
window.addEventListener('message', function ({ data: { type } }) {
if (type !== messageType) { return }
window.removeEventListener('message', handler)
handler.apply(window, arguments)
})
}
// //
// setup plugin communication // setup plugin communication
// //
@ -39,18 +47,21 @@ var inpageProvider = new MetamaskInpageProvider(metamaskStream)
inpageProvider.setMaxListeners(100) inpageProvider.setMaxListeners(100)
// set up a listener for when MetaMask is locked // set up a listener for when MetaMask is locked
window.addEventListener('metamasksetlocked', () => { window.addEventListener('message', ({ data: { type } }) => {
if (type !== 'metamasksetlocked') { return }
isEnabled = false isEnabled = false
}) })
// augment the provider with its enable method // augment the provider with its enable method
inpageProvider.enable = function ({ force } = {}) { inpageProvider.enable = function ({ force } = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.removeEventListener('ethereumprovider', providerHandle) window.removeEventListener('message', providerHandle)
providerHandle = ({ detail }) => { providerHandle = ({ data: { type, error } }) => {
if (typeof detail.error !== 'undefined') { if (type !== 'ethereumprovider') { return }
reject(detail.error) if (typeof error !== 'undefined') {
reject(error)
} else { } else {
window.removeEventListener('message', providerHandle)
// wait for the publicConfig store to populate with an account // wait for the publicConfig store to populate with an account
const publicConfig = new Promise((resolve) => { const publicConfig = new Promise((resolve) => {
const { selectedAddress } = inpageProvider.publicConfigStore.getState() const { selectedAddress } = inpageProvider.publicConfigStore.getState()
@ -84,7 +95,7 @@ inpageProvider.enable = function ({ force } = {}) {
.catch(reject) .catch(reject)
} }
} }
window.addEventListener('ethereumprovider', providerHandle) window.addEventListener('message', providerHandle)
window.postMessage({ type: 'ETHEREUM_ENABLE_PROVIDER', force }, '*') window.postMessage({ type: 'ETHEREUM_ENABLE_PROVIDER', force }, '*')
}) })
} }
@ -107,19 +118,21 @@ inpageProvider._metamask = new Proxy({
*/ */
isApproved: function() { isApproved: function() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.removeEventListener('ethereumisapproved', isApprovedHandle) window.removeEventListener('message', isApprovedHandle)
isApprovedHandle = ({ detail }) => { isApprovedHandle = ({ data: { caching, isApproved, error, type } }) => {
if (typeof detail.error !== 'undefined') { if (type !== 'ethereumisapproved') { return }
reject(detail.error) window.removeEventListener('message', isApprovedHandle)
if (typeof error !== 'undefined') {
reject(error)
} else { } else {
if (detail.caching) { if (caching) {
resolve(!!detail.isApproved) resolve(!!isApproved)
} else { } else {
resolve(false) resolve(false)
} }
} }
} }
window.addEventListener('ethereumisapproved', isApprovedHandle) window.addEventListener('message', isApprovedHandle)
window.postMessage({ type: 'ETHEREUM_IS_APPROVED' }, '*') window.postMessage({ type: 'ETHEREUM_IS_APPROVED' }, '*')
}) })
}, },
@ -131,15 +144,17 @@ inpageProvider._metamask = new Proxy({
*/ */
isUnlocked: function () { isUnlocked: function () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.removeEventListener('metamaskisunlocked', isUnlockedHandle) window.removeEventListener('message', isUnlockedHandle)
isUnlockedHandle = ({ detail }) => { isUnlockedHandle = ({ data: { isUnlocked, error, type } }) => {
if (typeof detail.error !== 'undefined') { if (type !== 'metamaskisunlocked') { return }
reject(detail.error) window.removeEventListener('message', isUnlockedHandle)
if (typeof error !== 'undefined') {
reject(error)
} else { } else {
resolve(!!detail.isUnlocked) resolve(!!isUnlocked)
} }
} }
window.addEventListener('metamaskisunlocked', isUnlockedHandle) window.addEventListener('message', isUnlockedHandle)
window.postMessage({ type: 'METAMASK_IS_UNLOCKED' }, '*') window.postMessage({ type: 'METAMASK_IS_UNLOCKED' }, '*')
}) })
}, },

Loading…
Cancel
Save