From 29dd81d029fb3cb5e0477758a59917b4cdf1091e Mon Sep 17 00:00:00 2001 From: frankiebee Date: Mon, 18 Sep 2017 19:08:02 -0700 Subject: [PATCH 1/8] require metamascara --- mascara/example/app.js | 67 +++++++++---------------------- mascara/example/app/index.html | 6 +-- mascara/src/lib/setup-iframe.js | 19 --------- mascara/src/lib/setup-provider.js | 22 ---------- mascara/src/mascara.js | 48 +--------------------- package.json | 1 + 6 files changed, 23 insertions(+), 140 deletions(-) delete mode 100644 mascara/src/lib/setup-iframe.js delete mode 100644 mascara/src/lib/setup-provider.js diff --git a/mascara/example/app.js b/mascara/example/app.js index aae7ccd19..d0cb6ba83 100644 --- a/mascara/example/app.js +++ b/mascara/example/app.js @@ -1,57 +1,26 @@ -window.addEventListener('load', web3Detect) +const EthQuery = require('ethjs-query') + +window.addEventListener('load', loadProvider) window.addEventListener('message', console.warn) -function web3Detect() { - if (global.web3) { - logToDom('web3 detected!') - startApp() - } else { - logToDom('no web3 detected!') - } +async function loadProvider() { + const ethereumProvider = window.metamask.createDefaultProvider({ host: 'http://localhost:9001' }) + const ethQuery = new EthQuery(ethereumProvider) + const accounts = await ethQuery.accounts() + logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined') + setupButton(ethQuery) } -function startApp(){ - console.log('app started') - - var primaryAccount - console.log('getting main account...') - web3.eth.getAccounts((err, addresses) => { - if (err) console.error(err) - console.log('set address', addresses[0]) - primaryAccount = addresses[0] - }) - - document.querySelector('.action-button-1').addEventListener('click', function(){ - console.log('saw click') - console.log('sending tx') - primaryAccount - web3.eth.sendTransaction({ - from: primaryAccount, - to: primaryAccount, - value: 0, - }, function(err, txHash){ - if (err) throw err - console.log('sendTransaction result:', err || txHash) - }) - }) - document.querySelector('.action-button-2').addEventListener('click', function(){ - console.log('saw click') - setTimeout(function(){ - console.log('sending tx') - web3.eth.sendTransaction({ - from: primaryAccount, - to: primaryAccount, - value: 0, - }, function(err, txHash){ - if (err) throw err - console.log('sendTransaction result:', err || txHash) - }) - }) - }) - -} function logToDom(message){ - document.body.appendChild(document.createTextNode(message)) + document.getElementById('account').innerText = message console.log(message) } + +function setupButton (ethQuery) { + const button = document.getElementById('action-button-1') + button.addEventListener('click', async () => { + const accounts = await ethQuery.accounts() + logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined') + }) +} \ No newline at end of file diff --git a/mascara/example/app/index.html b/mascara/example/app/index.html index 02323e5f9..f3e38877c 100644 --- a/mascara/example/app/index.html +++ b/mascara/example/app/index.html @@ -3,13 +3,13 @@ - MetaMask ZeroClient Example + MetaMask ZeroClient Example - - + +
\ No newline at end of file diff --git a/mascara/src/lib/setup-iframe.js b/mascara/src/lib/setup-iframe.js deleted file mode 100644 index dcf404574..000000000 --- a/mascara/src/lib/setup-iframe.js +++ /dev/null @@ -1,19 +0,0 @@ -const Iframe = require('iframe') -const createIframeStream = require('iframe-stream').IframeStream - -module.exports = setupIframe - - -function setupIframe(opts) { - opts = opts || {} - var frame = Iframe({ - src: opts.zeroClientProvider || 'https://zero.metamask.io/', - container: opts.container || document.head, - sandboxAttributes: opts.sandboxAttributes || ['allow-scripts', 'allow-popups'], - }) - var iframe = frame.iframe - iframe.style.setProperty('display', 'none') - var iframeStream = createIframeStream(iframe) - - return iframeStream -} diff --git a/mascara/src/lib/setup-provider.js b/mascara/src/lib/setup-provider.js deleted file mode 100644 index 62335b18d..000000000 --- a/mascara/src/lib/setup-provider.js +++ /dev/null @@ -1,22 +0,0 @@ -const setupIframe = require('./setup-iframe.js') -const MetamaskInpageProvider = require('../../../app/scripts/lib/inpage-provider.js') - -module.exports = getProvider - - -function getProvider(opts){ - if (global.web3) { - console.log('MetaMask ZeroClient - using environmental web3 provider') - return global.web3.currentProvider - } - console.log('MetaMask ZeroClient - injecting zero-client iframe!') - var iframeStream = setupIframe({ - zeroClientProvider: opts.mascaraUrl, - sandboxAttributes: ['allow-scripts', 'allow-popups', 'allow-same-origin'], - container: document.body, - }) - - var inpageProvider = new MetamaskInpageProvider(iframeStream) - return inpageProvider - -} diff --git a/mascara/src/mascara.js b/mascara/src/mascara.js index 1655d1f64..0af6f532f 100644 --- a/mascara/src/mascara.js +++ b/mascara/src/mascara.js @@ -1,47 +1 @@ -const Web3 = require('web3') -const setupProvider = require('./lib/setup-provider.js') -const setupDappAutoReload = require('../../app/scripts/lib/auto-reload.js') -const MASCARA_ORIGIN = process.env.MASCARA_ORIGIN || 'http://localhost:9001' -console.log('MASCARA_ORIGIN:', MASCARA_ORIGIN) - -// -// setup web3 -// - -const provider = setupProvider({ - mascaraUrl: MASCARA_ORIGIN + '/proxy/', -}) -instrumentForUserInteractionTriggers(provider) - -const web3 = new Web3(provider) -setupDappAutoReload(web3, provider.publicConfigStore) -// -// ui stuff -// - -let shouldPop = false -window.addEventListener('click', maybeTriggerPopup) - -// -// util -// - -function maybeTriggerPopup(){ - if (!shouldPop) return - shouldPop = false - window.open(MASCARA_ORIGIN, '', 'width=360 height=500') - console.log('opening window...') -} - -function instrumentForUserInteractionTriggers(provider){ - const _super = provider.sendAsync.bind(provider) - provider.sendAsync = function(payload, cb){ - if (payload.method === 'eth_sendTransaction') { - console.log('saw send') - shouldPop = true - } - _super(payload, cb) - } -} - - +global.metamask = require('metamascara') diff --git a/package.json b/package.json index 14e7f100f..4da6d99a7 100644 --- a/package.json +++ b/package.json @@ -99,6 +99,7 @@ "json-rpc-engine": "^3.1.0", "json-rpc-middleware-stream": "^1.0.0", "loglevel": "^1.4.1", + "metamascara": "^1.1.1", "metamask-logo": "^2.1.2", "mississippi": "^1.2.0", "mkdirp": "^0.5.1", From bfd75107f11995e0636def0e1efa6dd14b3ee8a6 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 19 Sep 2017 10:45:32 -0700 Subject: [PATCH 2/8] add context to platform to not have X-Metamask-Origin in mascara --- app/scripts/background.js | 1 + app/scripts/metamask-controller.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 1b96d68b5..a271db263 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -20,6 +20,7 @@ window.log = log log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn') const platform = new ExtensionPlatform() +platform.context = 'extension' const notificationManager = new NotificationManager() global.METAMASK_NOTIFIER = notificationManager diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fef16c3a9..3a2f3878c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -196,7 +196,7 @@ module.exports = class MetamaskController extends EventEmitter { }, // rpc data source rpcUrl: this.networkController.getCurrentRpcAddress(), - originHttpHeaderKey: 'X-Metamask-Origin', + originHttpHeaderKey: this.platform.context === 'extension' ? 'X-Metamask-Origin' : undefined, // account mgmt getAccounts: (cb) => { const isUnlocked = this.keyringController.memStore.getState().isUnlocked From 4e57cdf8b35c0bde571fb108b76d70ca251644f7 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 19 Sep 2017 10:53:56 -0700 Subject: [PATCH 3/8] stub platform --- mock-dev.js | 1 + test/unit/metamask-controller-test.js | 1 + 2 files changed, 2 insertions(+) diff --git a/mock-dev.js b/mock-dev.js index a47f1ed4d..0a3eb12ce 100644 --- a/mock-dev.js +++ b/mock-dev.js @@ -62,6 +62,7 @@ const controller = new MetamaskController({ showUnconfirmedMessage: noop, unlockAccountMessage: noop, showUnapprovedTx: noop, + platform: {}, // initial state initState: firstTimeState, }) diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index 5ee0a6c84..ef6cae758 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -10,6 +10,7 @@ describe('MetaMaskController', function () { showUnconfirmedMessage: noop, unlockAccountMessage: noop, showUnapprovedTx: noop, + platform: {}, // initial state initState: clone(firstTimeState), }) From daeb7f6ad3aefae7c643a753320edf178e9bc9bb Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 19 Sep 2017 12:58:51 -0700 Subject: [PATCH 4/8] update metamascara --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a7f52abee..f23f31d7c 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,7 @@ "json-rpc-engine": "^3.1.0", "json-rpc-middleware-stream": "^1.0.0", "loglevel": "^1.4.1", - "metamascara": "^1.1.1", + "metamascara": "^1.2.1", "metamask-logo": "^2.1.2", "mississippi": "^1.2.0", "mkdirp": "^0.5.1", From 418f01411e3c41505d2011767e571af9401c3a2d Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 19 Sep 2017 16:48:42 -0700 Subject: [PATCH 5/8] mascara: turn off background --- mascara/src/background.js | 3 +-- package.json | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mascara/src/background.js b/mascara/src/background.js index d9dbf593a..5ba865ad8 100644 --- a/mascara/src/background.js +++ b/mascara/src/background.js @@ -19,8 +19,7 @@ const migrations = require('../../app/scripts/migrations/') const firstTimeState = require('../../app/scripts/first-time-state') const STORAGE_KEY = 'metamask-config' -// const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' -const METAMASK_DEBUG = true +const METAMASK_DEBUG = process.env.METAMASK_DEBUG let popupIsOpen = false let connectedClientCount = 0 diff --git a/package.json b/package.json index f23f31d7c..c897af8a5 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "ui": "npm run test:flat:build:states && beefy ui-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./", "mock": "beefy mock-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./", "watch": "mocha watch --recursive \"test/unit/**/*.js\"", - "mascara": "node ./mascara/example/server", + "mascara": "METAMASK_DEBUG=true node ./mascara/example/server", "dist": "npm run dist:clear && npm install && gulp dist", "dist:clear": "rm -rf node_modules/eth-contract-metadata && rm -rf node_modules/eth-phishing-detect", "test": "npm run lint && npm run test:coverage && npm run test:integration", From 14b9d16eced03026da051604091833b5d19ab01e Mon Sep 17 00:00:00 2001 From: frankiebee Date: Thu, 21 Sep 2017 11:12:04 -0700 Subject: [PATCH 6/8] platforms: put context for extension in platform extension class --- app/scripts/background.js | 1 - app/scripts/metamask-controller.js | 2 +- app/scripts/platforms/extension.js | 3 +++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index a271db263..1b96d68b5 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -20,7 +20,6 @@ window.log = log log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn') const platform = new ExtensionPlatform() -platform.context = 'extension' const notificationManager = new NotificationManager() global.METAMASK_NOTIFIER = notificationManager diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3a2f3878c..4d149545a 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -196,7 +196,7 @@ module.exports = class MetamaskController extends EventEmitter { }, // rpc data source rpcUrl: this.networkController.getCurrentRpcAddress(), - originHttpHeaderKey: this.platform.context === 'extension' ? 'X-Metamask-Origin' : undefined, + originHttpHeaderKey: this.platform.isExtension ? 'X-Metamask-Origin' : undefined, // account mgmt getAccounts: (cb) => { const isUnlocked = this.keyringController.memStore.getState().isUnlocked diff --git a/app/scripts/platforms/extension.js b/app/scripts/platforms/extension.js index 00c2aa275..d97138207 100644 --- a/app/scripts/platforms/extension.js +++ b/app/scripts/platforms/extension.js @@ -5,6 +5,9 @@ class ExtensionPlatform { // // Public // + get isExtension () { + return true + } reload () { extension.runtime.reload() From 4979f5902fc15a7bb65e1e7420e699282d40c8b7 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 22 Sep 2017 20:04:58 -0700 Subject: [PATCH 7/8] bump metamascara version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c897af8a5..6c0f8ce67 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,7 @@ "json-rpc-engine": "^3.1.0", "json-rpc-middleware-stream": "^1.0.0", "loglevel": "^1.4.1", - "metamascara": "^1.2.1", + "metamascara": "^1.3.1", "metamask-logo": "^2.1.2", "mississippi": "^1.2.0", "mkdirp": "^0.5.1", From b24e16d346d0e71b140a659815a32be9ce7cd117 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Wed, 27 Sep 2017 16:14:58 -0700 Subject: [PATCH 8/8] re-enabled x-metamask-origin for mascara --- app/scripts/metamask-controller.js | 2 +- app/scripts/platforms/extension.js | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 4d149545a..fef16c3a9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -196,7 +196,7 @@ module.exports = class MetamaskController extends EventEmitter { }, // rpc data source rpcUrl: this.networkController.getCurrentRpcAddress(), - originHttpHeaderKey: this.platform.isExtension ? 'X-Metamask-Origin' : undefined, + originHttpHeaderKey: 'X-Metamask-Origin', // account mgmt getAccounts: (cb) => { const isUnlocked = this.keyringController.memStore.getState().isUnlocked diff --git a/app/scripts/platforms/extension.js b/app/scripts/platforms/extension.js index d97138207..0afe04b74 100644 --- a/app/scripts/platforms/extension.js +++ b/app/scripts/platforms/extension.js @@ -5,10 +5,6 @@ class ExtensionPlatform { // // Public // - get isExtension () { - return true - } - reload () { extension.runtime.reload() }