diff options
idmgmt - refactor
Diffstat (limited to 'app/scripts/background.js')
-rw-r--r-- | app/scripts/background.js | 132 |
1 files changed, 68 insertions, 64 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js index 0276124cc..4dc92cea5 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,27 +1,18 @@ const Dnode = require('dnode') +const eos = require('end-of-stream') +const extend = require('xtend') +const EthStore = require('eth-store') const PortStream = require('./lib/port-stream.js') const MetaMaskProvider = require('./lib/metamask-provider') -const IdentityManager = require('./lib/idmgmt') -const eos = require('end-of-stream') +// const IdentityManager = require('./lib/idmgmt') +const IdentityStore = require('./lib/idStore') console.log('ready to roll') -var wallet = new IdentityManager() - -// setup provider -var zeroClient = MetaMaskProvider({ - rpcUrl: 'https://rawtestrpc.metamask.io/', - getAccounts: wallet.getAccounts.bind(wallet), - signTransaction: wallet.addUnconfirmedTransaction.bind(wallet), -}) - -wallet.setProvider(zeroClient) -zeroClient.on('block', function(block){ - wallet.newBlock(block) -}) - +// +// connect to other contexts +// -// setup messaging chrome.runtime.onConnect.addListener(connectRemote) function connectRemote(remotePort){ var isMetaMaskInternalProcess = (remotePort.name === 'popup') @@ -34,40 +25,31 @@ function connectRemote(remotePort){ } } -function handleInternalCommunication(remotePort){ - var duplex = new PortStream(remotePort) - var connection = Dnode({ - // this is annoying, have to decompose wallet - getState: wallet.getState.bind(wallet), - submitPassword: wallet.submitPassword.bind(wallet), - setSelectedAddress: wallet.setSelectedAddress.bind(wallet), - signTransaction: wallet.signTransaction.bind(wallet), - setLocked: wallet.setLocked.bind(wallet), - getAccounts: wallet.getAccounts.bind(wallet), - newBlock: wallet.newBlock.bind(wallet), - setProvider: wallet.setProvider.bind(wallet), - }) - duplex.pipe(connection).pipe(duplex) - connection.on('remote', function(remote){ - - // push updates to popup - wallet.on('update', sendUpdate) - eos(duplex, function unsubscribe(){ - wallet.removeListener('update', sendUpdate) - }) - function sendUpdate(state){ - remote.sendUpdate(state) - } +function handleExternalCommunication(remotePort){ + remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort)) +} - }) +// +// state and network +// - - - // sub to metamask store -} +var idStore = new IdentityStore() +var zeroClient = MetaMaskProvider({ + rpcUrl: 'https://rawtestrpc.metamask.io/', + getAccounts: function(cb){ + var selectedAddress = idStore.getSelectedAddress() + var result = selectedAddress ? [selectedAddress] : [] + cb(null, result) + }, + signTransaction: idStore.addUnconfirmedTransaction.bind(idStore), +}) +var ethStore = new EthStore(zeroClient) +idStore.setStore(ethStore) -function handleExternalCommunication(remotePort){ - remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort)) +function getState(){ + var state = extend(ethStore.getState(), idStore.getState()) + console.log(state) + return state } // handle rpc requests @@ -84,8 +66,44 @@ function onRpcRequest(remotePort, payload){ }) } -// setup badge text -wallet.on('update', updateBadge) + +// +// popup integration +// + +function handleInternalCommunication(remotePort){ + var duplex = new PortStream(remotePort) + var connection = Dnode({ + getState: function(cb){ cb(null, getState()) }, + // forward directly to idStore + submitPassword: idStore.submitPassword.bind(idStore), + setSelectedAddress: idStore.setSelectedAddress.bind(idStore), + signTransaction: idStore.signTransaction.bind(idStore), + setLocked: idStore.setLocked.bind(idStore), + }) + duplex.pipe(connection).pipe(duplex) + connection.on('remote', function(remote){ + + // push updates to popup + ethStore.on('update', sendUpdate) + idStore.on('update', sendUpdate) + // teardown on disconnect + eos(duplex, function unsubscribe(){ + ethStore.removeListener('update', sendUpdate) + }) + function sendUpdate(){ + var state = getState() + remote.sendUpdate(state) + } + + }) +} + +// +// plugin badge text +// + +idStore.on('update', updateBadge) function updateBadge(state){ var label = '' @@ -97,17 +115,3 @@ function updateBadge(state){ chrome.browserAction.setBadgeBackgroundColor({color: '#506F8B'}) } -// function handleMessage(msg){ -// console.log('got message!', msg.type) -// switch(msg.type){ - -// case 'addUnsignedTx': -// addTransaction(msg.payload) -// return - -// case 'removeUnsignedTx': -// removeTransaction(msg.payload) -// return - -// } -// } |