From 3417c421c8c5a73d363a724613145807bdbee354 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 11 Aug 2016 13:23:49 -0700 Subject: [PATCH 1/6] inpage-provider - remove createSyncProvider, just fail --- app/scripts/lib/inpage-provider.js | 43 +++--------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js index e387be895..5e8b240bc 100644 --- a/app/scripts/lib/inpage-provider.js +++ b/app/scripts/lib/inpage-provider.js @@ -1,9 +1,7 @@ -const HttpProvider = require('web3/lib/web3/httpprovider') const Streams = require('mississippi') const ObjectMultiplex = require('./obj-multiplex') const StreamProvider = require('web3-stream-provider') const RemoteStore = require('./remote-store.js').RemoteStore -const MetamaskConfig = require('../config.js') module.exports = MetamaskInpageProvider @@ -27,13 +25,6 @@ function MetamaskInpageProvider (connectionStream) { }) self.publicConfigStore = publicConfigStore - // connect to sync provider - self.syncProvider = createSyncProvider(publicConfigStore.get('provider')) - // subscribe to publicConfig to update the syncProvider on change - publicConfigStore.subscribe(function (state) { - self.syncProvider = createSyncProvider(state.provider) - }) - // connect to async provider var asyncProvider = new StreamProvider() Streams.pipe(asyncProvider, multiStream.createStream('provider'), asyncProvider, function (err) { @@ -65,9 +56,10 @@ MetamaskInpageProvider.prototype.send = function (payload) { result = selectedAddress || '0x0000000000000000000000000000000000000000' break - // fallback to normal rpc + // throw not-supported Error default: - return self.syncProvider.send(payload) + var message = 'The MetaMask Web3 object does not support synchronous methods. See https://github.com/MetaMask/faq#all-async---think-of-metamask-as-a-light-client for details.' + throw new Error(message) } @@ -89,35 +81,6 @@ MetamaskInpageProvider.prototype.isConnected = function () { // util -function createSyncProvider (providerConfig) { - providerConfig = providerConfig || {} - let syncProviderUrl - - if (providerConfig.rpcTarget) { - syncProviderUrl = providerConfig.rpcTarget - } else { - switch (providerConfig.type) { - case 'testnet': - syncProviderUrl = MetamaskConfig.network.testnet - break - case 'mainnet': - syncProviderUrl = MetamaskConfig.network.mainnet - break - default: - syncProviderUrl = MetamaskConfig.network.default - } - } - - const provider = new HttpProvider(syncProviderUrl) - // Stubbing out the send method to throw on sync methods: - provider.send = function() { - var message = 'The MetaMask Web3 object does not support synchronous methods. See https://github.com/MetaMask/faq#all-async---think-of-metamask-as-a-light-client for details.' - throw new Error(message) - } - - return provider -} - function remoteStoreWithLocalStorageCache (storageKey) { // read local cache var initState = JSON.parse(localStorage[storageKey] || '{}') From 53b1334d37c2792e76e2a114acb3e7b47399e8aa Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 11 Aug 2016 13:31:00 -0700 Subject: [PATCH 2/6] clean -formatting --- app/scripts/lib/inpage-provider.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js index 5e8b240bc..e9e0ccf00 100644 --- a/app/scripts/lib/inpage-provider.js +++ b/app/scripts/lib/inpage-provider.js @@ -39,9 +39,9 @@ function MetamaskInpageProvider (connectionStream) { MetamaskInpageProvider.prototype.send = function (payload) { const self = this + let selectedAddress - - var result = null + let result = null switch (payload.method) { case 'eth_accounts': From d7dffedf4825c468d32f389935fcac6e242a2232 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 11 Aug 2016 14:04:20 -0700 Subject: [PATCH 3/6] deps - local-message-stream extracted as module post-message-stream --- app/scripts/contentscript.js | 2 +- app/scripts/inpage.js | 2 +- app/scripts/lib/local-message-stream.js | 56 ------------------------- package.json | 1 + 4 files changed, 3 insertions(+), 58 deletions(-) delete mode 100644 app/scripts/lib/local-message-stream.js diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 1eb04059d..de2cf263b 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -1,4 +1,4 @@ -const LocalMessageDuplexStream = require('./lib/local-message-stream.js') +const LocalMessageDuplexStream = require('post-message-stream') const PortStream = require('./lib/port-stream.js') const ObjectMultiplex = require('./lib/obj-multiplex') const extension = require('./lib/extension') diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 055235671..7c508c66f 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -1,7 +1,7 @@ /*global Web3*/ cleanContextForImports() require('web3/dist/web3.min.js') -const LocalMessageDuplexStream = require('./lib/local-message-stream.js') +const LocalMessageDuplexStream = require('post-message-stream') const setupDappAutoReload = require('./lib/auto-reload.js') const MetamaskInpageProvider = require('./lib/inpage-provider.js') restoreContextAfterImports() diff --git a/app/scripts/lib/local-message-stream.js b/app/scripts/lib/local-message-stream.js deleted file mode 100644 index 821e51046..000000000 --- a/app/scripts/lib/local-message-stream.js +++ /dev/null @@ -1,56 +0,0 @@ -const Duplex = require('readable-stream').Duplex -const inherits = require('util').inherits - -module.exports = LocalMessageDuplexStream - -inherits(LocalMessageDuplexStream, Duplex) - -function LocalMessageDuplexStream (opts) { - Duplex.call(this, { - objectMode: true, - }) - - // this._origin = opts.origin - this._name = opts.name - this._target = opts.target - - // console.log('LocalMessageDuplexStream ('+this._name+') - initialized...') - window.addEventListener('message', this._onMessage.bind(this), false) -} - -// private - -LocalMessageDuplexStream.prototype._onMessage = function (event) { - var msg = event.data - // console.log('LocalMessageDuplexStream ('+this._name+') - heard message...', event) - // validate message - if (event.origin !== location.origin) return // console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (event.origin !== location.origin) ') - if (typeof msg !== 'object') return // console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (typeof msg !== "object") ') - if (msg.target !== this._name) return // console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (msg.target !== this._name) ', msg.target, this._name) - if (!msg.data) return // console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (!msg.data) ') - // console.log('LocalMessageDuplexStream ('+this._name+') - accepted', msg.data) - // forward message - try { - this.push(msg.data) - } catch (err) { - this.emit('error', err) - } -} - -// stream plumbing - -LocalMessageDuplexStream.prototype._read = noop - -LocalMessageDuplexStream.prototype._write = function (data, encoding, cb) { - // console.log('LocalMessageDuplexStream ('+this._name+') - sending message...') - var message = { - target: this._target, - data: data, - } - window.postMessage(message, location.origin) - cb() -} - -// util - -function noop () {} diff --git a/package.json b/package.json index 7389f5e0e..79cfde5c9 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "once": "^1.3.3", "pojo-migrator": "^2.1.0", "polyfill-crypto.getrandomvalues": "^1.0.0", + "post-message-stream": "^1.0.0", "pumpify": "^1.3.4", "react": "^15.0.2", "react-addons-css-transition-group": "^15.0.2", From a26d68297695d31db1b4a1c00e96d40e9022af24 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 11 Aug 2016 14:09:55 -0700 Subject: [PATCH 4/6] deps - bump web3-stream-provider --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 79cfde5c9..5bf4ef475 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "vreme": "^3.0.2", "web3": "ethereum/web3.js#0.16.0", "web3-provider-engine": "^7.8.3", - "web3-stream-provider": "^2.0.5", + "web3-stream-provider": "^2.0.6", "xtend": "^4.0.1" }, "devDependencies": { From c0f7c491055e3ab8d12efd78b9ff496ff292d74b Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 11 Aug 2016 14:35:55 -0700 Subject: [PATCH 5/6] inpage-prov - sync error - fix link --- app/scripts/lib/inpage-provider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js index e9e0ccf00..b3ed3d9e2 100644 --- a/app/scripts/lib/inpage-provider.js +++ b/app/scripts/lib/inpage-provider.js @@ -58,7 +58,7 @@ MetamaskInpageProvider.prototype.send = function (payload) { // throw not-supported Error default: - var message = 'The MetaMask Web3 object does not support synchronous methods. See https://github.com/MetaMask/faq#all-async---think-of-metamask-as-a-light-client for details.' + var message = 'The MetaMask Web3 object does not support synchronous methods. See https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#all-async---think-of-metamask-as-a-light-client for details.' throw new Error(message) } From 7dcb3c64df50342ece5faf0544e521bb8e5cb975 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 11 Aug 2016 14:42:02 -0700 Subject: [PATCH 6/6] changelog - add note about web3-provider-stream fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac0983d2..9afea415c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Current Master - Fix various typos. +- Make dapp-metamask connection more reliable ## 2.7.3 2016-07-29