From d840e81a101351bd661668cf0b1f9e5b73683890 Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 15 Apr 2016 12:12:04 -0700 Subject: wiring - trusted-untrusted features + remote-store --- app/scripts/background.js | 62 ++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 25 deletions(-) (limited to 'app/scripts/background.js') diff --git a/app/scripts/background.js b/app/scripts/background.js index f3dd8cbb6..0bd7031d8 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -9,8 +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 +const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex +const HostStore = require('./lib/remote-store.js').HostStore // // connect to other contexts @@ -22,15 +22,27 @@ function connectRemote(remotePort){ var portStream = new PortStream(remotePort) if (isMetaMaskInternalProcess) { // communication with popup - handleInternalCommunication(portStream) + setupTrustedCommunication(portStream) } else { // communication with page - handleEthRpcRequestStream(portStream) + setupUntrustedCommunication(portStream) } } -function handleEthRpcRequestStream(stream){ - stream.on('data', onRpcRequest.bind(null, stream)) +function setupUntrustedCommunication(connectionStream){ + // setup multiplexing + var mx = setupMultiplex(connectionStream) + // connect features + setupProviderConnection(mx.createStream('provider')) + setupPublicConfig(mx.createStream('publicConfig')) +} + +function setupTrustedCommunication(connectionStream){ + // setup multiplexing + var mx = setupMultiplex(connectionStream) + // connect features + setupControllerConnection(mx.createStream('controller')) + setupProviderConnection(mx.createStream('provider')) } // @@ -59,6 +71,13 @@ provider.on('block', function(block){ var ethStore = new EthStore(provider) idStore.setStore(ethStore) +// copy idStore substate into public store +var publicConfigStore = new HostStore() +idStore.on('update', function(state){ + publicConfigStore.set('selectedAddress', state.selectedAddress) +}) + + function getState(){ var state = extend( ethStore.getState(), @@ -84,27 +103,20 @@ function onRpcRequest(remoteStream, payload){ // -// popup integration +// remote features // -function handleInternalCommunication(portStream){ - // setup multiplexing - var mx = ObjectMultiplex() - portStream.pipe(mx).pipe(portStream) - mx.on('error', function(err) { - console.error(err) - // portStream.destroy() - }) - portStream.on('error', function(err) { - console.error(err) - mx.destroy() - }) - linkDnode(mx.createStream('dnode')) - handleEthRpcRequestStream(mx.createStream('provider')) +function setupPublicConfig(stream){ + var storeStream = publicConfigStore.createStream() + stream.pipe(storeStream).pipe(stream) +} + +function setupProviderConnection(stream){ + stream.on('data', onRpcRequest.bind(null, stream)) } -function linkDnode(stream){ - var connection = Dnode({ +function setupControllerConnection(stream){ + var dnode = Dnode({ getState: function(cb){ cb(null, getState()) }, setRpcTarget: setRpcTarget, useEtherscanProvider: useEtherscanProvider, @@ -119,8 +131,8 @@ function linkDnode(stream){ clearSeedWordCache: idStore.clearSeedWordCache.bind(idStore), exportAccount: idStore.exportAccount.bind(idStore), }) - stream.pipe(connection).pipe(stream) - connection.on('remote', function(remote){ + stream.pipe(dnode).pipe(stream) + dnode.on('remote', function(remote){ // push updates to popup ethStore.on('update', sendUpdate) -- cgit