aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/background.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2016-04-16 03:12:04 +0800
committerkumavis <aaron@kumavis.me>2016-04-16 03:12:04 +0800
commitd840e81a101351bd661668cf0b1f9e5b73683890 (patch)
tree7c24e6afc902774d14f5594727173ed87d73ce17 /app/scripts/background.js
parent907e39e5abc301fa54e144c5c151b79c3affb788 (diff)
downloadtangerine-wallet-browser-d840e81a101351bd661668cf0b1f9e5b73683890.tar
tangerine-wallet-browser-d840e81a101351bd661668cf0b1f9e5b73683890.tar.gz
tangerine-wallet-browser-d840e81a101351bd661668cf0b1f9e5b73683890.tar.bz2
tangerine-wallet-browser-d840e81a101351bd661668cf0b1f9e5b73683890.tar.lz
tangerine-wallet-browser-d840e81a101351bd661668cf0b1f9e5b73683890.tar.xz
tangerine-wallet-browser-d840e81a101351bd661668cf0b1f9e5b73683890.tar.zst
tangerine-wallet-browser-d840e81a101351bd661668cf0b1f9e5b73683890.zip
wiring - trusted-untrusted features + remote-store
Diffstat (limited to 'app/scripts/background.js')
-rw-r--r--app/scripts/background.js62
1 files changed, 37 insertions, 25 deletions
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)