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 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/inpage-provider.js b/app/scripts/lib/inpage-provider.js index e387be895..b3ed3d9e2 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) { @@ -48,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': @@ -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/blob/master/DEVELOPERS.md#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] || '{}') 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..5bf4ef475 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", @@ -72,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": {