From 266e3c7ddb920c933355d559144bab6c633a65fb Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 8 Apr 2016 14:24:10 -0700 Subject: [PATCH] popup - fix web3 plumbing --- app/scripts/background.js | 19 +++---------------- app/scripts/lib/stream-utils.js | 21 +++++++++++++++++++++ app/scripts/popup.js | 8 +++++++- 3 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 app/scripts/lib/stream-utils.js diff --git a/app/scripts/background.js b/app/scripts/background.js index 850a7bc6e..dfcdcc06c 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,6 +1,5 @@ const Dnode = require('dnode') const Multiplex = require('multiplex') -const Through = require('through2') const eos = require('end-of-stream') const combineStreams = require('pumpify') const extend = require('xtend') @@ -10,6 +9,8 @@ const MetaMaskProvider = require('./lib/zero.js') const IdentityStore = require('./lib/idStore') const createTxNotification = require('./lib/tx-notification.js') const configManager = require('./lib/config-manager-singleton') +const jsonParseStream = require('./lib/stream-utils.js').jsonParseStream +const jsonStringifyStream = require('./lib/stream-utils.js').jsonStringifyStream // // connect to other contexts @@ -100,7 +101,7 @@ function handleInternalCommunication(portStream){ mx.destroy() }) var dnodeStream = mx.createSharedStream('dnode') - var providerStream = combineStreams( + var providerStream = combineStreams.obj( jsonStringifyStream(), mx.createSharedStream('provider'), jsonParseStream() @@ -191,18 +192,4 @@ function useEtherscanProvider() { // util -function jsonParseStream(){ - return Through.obj(function(serialized){ - this.push(JSON.parse(serialized)) - cb() - }) -} - -function jsonStringifyStream(){ - return Through.obj(function(obj){ - this.push(JSON.stringify(obj)) - cb() - }) -} - function noop(){} diff --git a/app/scripts/lib/stream-utils.js b/app/scripts/lib/stream-utils.js new file mode 100644 index 000000000..12560ffd8 --- /dev/null +++ b/app/scripts/lib/stream-utils.js @@ -0,0 +1,21 @@ +const Through = require('through2') + + +module.exports = { + jsonParseStream: jsonParseStream, + jsonStringifyStream: jsonStringifyStream, +} + +function jsonParseStream(){ + return Through.obj(function(serialized, encoding, cb){ + this.push(JSON.parse(serialized)) + cb() + }) +} + +function jsonStringifyStream(){ + return Through.obj(function(obj, encoding, cb){ + this.push(JSON.stringify(obj)) + cb() + }) +} diff --git a/app/scripts/popup.js b/app/scripts/popup.js index cfb3c95e0..e6dae0d81 100644 --- a/app/scripts/popup.js +++ b/app/scripts/popup.js @@ -9,6 +9,8 @@ const MetaMaskUiCss = require('metamask-ui/css') const injectCss = require('inject-css') const PortStream = require('./lib/port-stream.js') const StreamProvider = require('./lib/stream-provider.js') +const jsonParseStream = require('./lib/stream-utils.js').jsonParseStream +const jsonStringifyStream = require('./lib/stream-utils.js').jsonStringifyStream // setup app var css = MetaMaskUiCss() @@ -42,7 +44,11 @@ function connectToAccountManager(cb){ function linkWeb3(stream){ var remoteProvider = new StreamProvider() - remoteProvider.pipe(stream).pipe(remoteProvider) + remoteProvider + .pipe(jsonStringifyStream()) + .pipe(stream) + .pipe(jsonParseStream()) + .pipe(remoteProvider) stream.on('error', console.error.bind(console)) remoteProvider.on('error', console.error.bind(console)) global.web3 = new Web3(remoteProvider)