|
|
@ -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' }, '*') |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}, |
|
|
|