metamask controller - adopt setup*Communication from background

feature/default_network_editable
kumavis 8 years ago
parent fc3a53ec7b
commit 2113979be7
  1. 52
      app/scripts/background.js
  2. 33
      app/scripts/metamask-controller.js

@ -1,6 +1,5 @@
const urlUtil = require('url') const urlUtil = require('url')
const Dnode = require('dnode') const endOfStream = require('end-of-stream')
const eos = require('end-of-stream')
const asyncQ = require('async-q') const asyncQ = require('async-q')
const pipe = require('pump') const pipe = require('pump')
const LocalStorageStore = require('obs-store/lib/localStorage') const LocalStorageStore = require('obs-store/lib/localStorage')
@ -10,7 +9,6 @@ const migrations = require('./migrations/')
const PortStream = require('./lib/port-stream.js') const PortStream = require('./lib/port-stream.js')
const notification = require('./lib/notifications.js') const notification = require('./lib/notifications.js')
const messageManager = require('./lib/message-manager') const messageManager = require('./lib/message-manager')
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
const MetamaskController = require('./metamask-controller') const MetamaskController = require('./metamask-controller')
const extension = require('./lib/extension') const extension = require('./lib/extension')
const firstTimeState = require('./first-time-state') const firstTimeState = require('./first-time-state')
@ -93,51 +91,21 @@ function setupController (initState) {
var portStream = new PortStream(remotePort) var portStream = new PortStream(remotePort)
if (isMetaMaskInternalProcess) { if (isMetaMaskInternalProcess) {
// communication with popup // communication with popup
popupIsOpen = remotePort.name === 'popup' popupIsOpen = popupIsOpen || (remotePort.name === 'popup')
setupTrustedCommunication(portStream, 'MetaMask', remotePort.name) controller.setupTrustedCommunication(portStream, 'MetaMask', remotePort.name)
// record popup as closed
if (remotePort.name === 'popup') {
endOfStream(portStream, () => {
popupIsOpen = false
})
}
} else { } else {
// communication with page // communication with page
var originDomain = urlUtil.parse(remotePort.sender.url).hostname var originDomain = urlUtil.parse(remotePort.sender.url).hostname
setupUntrustedCommunication(portStream, originDomain) controller.setupUntrustedCommunication(portStream, originDomain)
} }
} }
function setupUntrustedCommunication (connectionStream, originDomain) {
// setup multiplexing
var mx = setupMultiplex(connectionStream)
// connect features
controller.setupProviderConnection(mx.createStream('provider'), originDomain)
controller.setupPublicConfig(mx.createStream('publicConfig'))
}
function setupTrustedCommunication (connectionStream, originDomain) {
// setup multiplexing
var mx = setupMultiplex(connectionStream)
// connect features
setupControllerConnection(mx.createStream('controller'))
controller.setupProviderConnection(mx.createStream('provider'), originDomain)
}
//
// remote features
//
function setupControllerConnection (outStream) {
var api = controller.getApi()
var dnode = Dnode(api)
outStream.pipe(dnode).pipe(outStream)
dnode.on('remote', (remote) => {
// push updates to popup
var sendUpdate = remote.sendUpdate.bind(remote)
controller.on('update', sendUpdate)
// teardown on disconnect
eos(outStream, () => {
controller.removeListener('update', sendUpdate)
popupIsOpen = false
})
})
}
// //
// User Interface setup // User Interface setup
// //

@ -2,11 +2,13 @@ const EventEmitter = require('events')
const extend = require('xtend') const extend = require('xtend')
const promiseToCallback = require('promise-to-callback') const promiseToCallback = require('promise-to-callback')
const pipe = require('pump') const pipe = require('pump')
const Dnode = require('dnode')
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const storeTransform = require('obs-store/lib/transform') const storeTransform = require('obs-store/lib/transform')
const EthStore = require('./lib/eth-store') const EthStore = require('./lib/eth-store')
const EthQuery = require('eth-query') const EthQuery = require('eth-query')
const MetaMaskProvider = require('web3-provider-engine/zero.js') const MetaMaskProvider = require('web3-provider-engine/zero.js')
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
const KeyringController = require('./keyring-controller') const KeyringController = require('./keyring-controller')
const NoticeController = require('./notice-controller') const NoticeController = require('./notice-controller')
const messageManager = require('./lib/message-manager') const messageManager = require('./lib/message-manager')
@ -202,8 +204,35 @@ module.exports = class MetamaskController extends EventEmitter {
} }
} }
setupProviderConnection (stream, originDomain) { setupUntrustedCommunication (connectionStream, originDomain) {
stream.on('data', this.onRpcRequest.bind(this, stream, originDomain)) // setup multiplexing
var mx = setupMultiplex(connectionStream)
// connect features
this.setupProviderConnection(mx.createStream('provider'), originDomain)
this.setupPublicConfig(mx.createStream('publicConfig'))
}
setupTrustedCommunication (connectionStream, originDomain) {
// setup multiplexing
var mx = setupMultiplex(connectionStream)
// connect features
this.setupControllerConnection(mx.createStream('controller'))
this.setupProviderConnection(mx.createStream('provider'), originDomain)
}
setupControllerConnection (outStream) {
const api = this.getApi()
const dnode = Dnode(api)
outStream.pipe(dnode).pipe(outStream)
dnode.on('remote', (remote) => {
// push updates to popup
const sendUpdate = remote.sendUpdate.bind(remote)
this.on('update', sendUpdate)
})
}
setupProviderConnection (outStream, originDomain) {
outStream.on('data', this.onRpcRequest.bind(this, outStream, originDomain))
} }
onRpcRequest (stream, originDomain, request) { onRpcRequest (stream, originDomain, request) {

Loading…
Cancel
Save