From 1e989fb8a48e61a8dfb9cb3d1f82f46925b1239b Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 14 Apr 2016 21:11:35 -0700 Subject: [PATCH 1/3] inpage - defaultAccount - fix bug --- app/scripts/inpage.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 121246d6a..01f35d0fe 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -26,6 +26,17 @@ remoteProvider.pipe(pluginStream).pipe(remoteProvider) pluginStream.on('error', console.error.bind(console)) remoteProvider.on('error', console.error.bind(console)) +// +// global web3 +// + +var web3 = new Web3(remoteProvider) +window.web3 = web3 +web3.setProvider = function(){ + console.log('MetaMask - overrode web3.setProvider') +} +console.log('MetaMask - injected web3') + // // handle synchronous requests @@ -33,7 +44,7 @@ remoteProvider.on('error', console.error.bind(console)) // handle accounts cache var accountsCache = JSON.parse(localStorage['MetaMask-Accounts'] || '[]') -web3.eth.defaultAccount = accounts[0] +web3.eth.defaultAccount = accountsCache[0] setInterval(populateAccountsCache, 4000) function populateAccountsCache(){ @@ -46,8 +57,8 @@ function populateAccountsCache(){ // update localStorage var accounts = response.result if (accounts.toString() !== accountsCache.toString()) { - web3.eth.defaultAccount = accounts[0] accountsCache = accounts + web3.eth.defaultAccount = accountsCache[0] localStorage['MetaMask-Accounts'] = JSON.stringify(accounts) } }) @@ -83,13 +94,3 @@ remoteProvider.send = function(payload){ } } -// -// global web3 -// - -var web3 = new Web3(remoteProvider) -window.web3 = web3 -web3.setProvider = function(){ - console.log('MetaMask - overrode web3.setProvider') -} -console.log('MetaMask - injected web3') From cef1f2a65f27d594b12afd021dea73fcaf569af5 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 14 Apr 2016 21:19:08 -0700 Subject: [PATCH 2/3] ui - util - fixes#110 formatBalance --- ui/app/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/app/util.js b/ui/app/util.js index 74e2c19aa..67b7c8f67 100644 --- a/ui/app/util.js +++ b/ui/app/util.js @@ -66,11 +66,11 @@ function weiToEth(bn) { var decimalsToKeep = 4 function formatBalance(balance) { - if (!balance) return 'None' + if (!balance || balance === '0x0') return 'None' var wei = numericBalance(balance) var padded = wei.toString(10) var len = padded.length - var nonZeroIndex = padded.match(/[^0]/).index + var nonZeroIndex = padded.match(/[^0]/) && padded.match(/[^0]/).index var beforeDecimal = padded.substr(nonZeroIndex ? nonZeroIndex : 0, len - 18) var afterDecimal = padded.substr(len - 18, decimalsToKeep) return `${beforeDecimal}.${afterDecimal} ETH` From 907e39e5abc301fa54e144c5c151b79c3affb788 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 14 Apr 2016 21:22:04 -0700 Subject: [PATCH 3/3] wiring - move to obj-multiplex --- app/scripts/background.js | 15 ++++-------- app/scripts/lib/obj-multiplex.js | 41 ++++++++++++++++++++++++++++++++ app/scripts/popup.js | 18 ++++---------- 3 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 app/scripts/lib/obj-multiplex.js diff --git a/app/scripts/background.js b/app/scripts/background.js index a4f80d7f6..f3dd8cbb6 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,5 +1,5 @@ const Dnode = require('dnode') -const Multiplex = require('multiplex') +const ObjectMultiplex = require('./lib/obj-multiplex') const eos = require('end-of-stream') const combineStreams = require('pumpify') const extend = require('xtend') @@ -89,7 +89,7 @@ function onRpcRequest(remoteStream, payload){ function handleInternalCommunication(portStream){ // setup multiplexing - var mx = Multiplex() + var mx = ObjectMultiplex() portStream.pipe(mx).pipe(portStream) mx.on('error', function(err) { console.error(err) @@ -99,15 +99,8 @@ function handleInternalCommunication(portStream){ console.error(err) mx.destroy() }) - var dnodeStream = mx.createSharedStream('dnode') - var providerStream = combineStreams.obj( - jsonStringifyStream(), - mx.createSharedStream('provider'), - jsonParseStream() - ) - - linkDnode(dnodeStream) - handleEthRpcRequestStream(providerStream) + linkDnode(mx.createStream('dnode')) + handleEthRpcRequestStream(mx.createStream('provider')) } function linkDnode(stream){ diff --git a/app/scripts/lib/obj-multiplex.js b/app/scripts/lib/obj-multiplex.js new file mode 100644 index 000000000..333b6061f --- /dev/null +++ b/app/scripts/lib/obj-multiplex.js @@ -0,0 +1,41 @@ +const through = require('through2') + +module.exports = ObjectMultiplex + + +function ObjectMultiplex(opts){ + opts = opts || {} + // create multiplexer + var mx = through.obj(function(chunk, enc, cb) { + var name = chunk.name + var data = chunk.data + var substream = mx.streams[name] + if (!substream) { + console.warn("orphaned data for stream " + name) + } else { + substream.push(data) + } + return cb() + }) + mx.streams = {} + // create substreams + mx.createStream = function(name) { + var substream = mx.streams[name] = through.obj(function(chunk, enc, cb) { + mx.push({ + name: name, + data: chunk, + }) + return cb() + }) + mx.on('end', function() { + return substream.emit('end') + }) + if (opts.error) { + mx.on('error', function() { + return substream.emit('error') + }) + } + return substream + } + return mx +} diff --git a/app/scripts/popup.js b/app/scripts/popup.js index 523ecbd8f..3049ff2c3 100644 --- a/app/scripts/popup.js +++ b/app/scripts/popup.js @@ -1,7 +1,7 @@ const url = require('url') const EventEmitter = require('events').EventEmitter const async = require('async') -const Multiplex = require('multiplex') +const ObjectMultiplex = require('./lib/obj-multiplex') const Dnode = require('dnode') const Web3 = require('web3') const MetaMaskUi = require('../../ui') @@ -9,8 +9,6 @@ const MetaMaskUiCss = require('../../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() @@ -26,7 +24,7 @@ function connectToAccountManager(cb){ var pluginPort = chrome.runtime.connect({name: 'popup'}) var portStream = new PortStream(pluginPort) // setup multiplexing - var mx = Multiplex() + var mx = ObjectMultiplex() portStream.pipe(mx).pipe(portStream) mx.on('error', function(err) { console.error(err) @@ -36,19 +34,13 @@ function connectToAccountManager(cb){ console.error(err) mx.destroy() }) - var dnodeStream = mx.createSharedStream('dnode') - var providerStream = mx.createSharedStream('provider') - linkDnode(dnodeStream, cb) - linkWeb3(providerStream) + linkDnode(mx.createStream('dnode'), cb) + linkWeb3(mx.createStream('provider')) } function linkWeb3(stream){ var remoteProvider = new StreamProvider() - remoteProvider - .pipe(jsonStringifyStream()) - .pipe(stream) - .pipe(jsonParseStream()) - .pipe(remoteProvider) + remoteProvider.pipe(stream).pipe(remoteProvider) stream.on('error', console.error.bind(console)) remoteProvider.on('error', console.error.bind(console)) global.web3 = new Web3(remoteProvider)