nifty-wallet/app/scripts/background.js

213 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-01-15 02:03:42 -08:00
const Dnode = require('dnode')
2016-03-09 18:33:30 -08:00
const Multiplex = require('multiplex')
const Through = require('through2')
2016-02-10 17:44:46 -08:00
const eos = require('end-of-stream')
2016-03-10 13:04:45 -08:00
const combineStreams = require('pumpify')
2016-02-10 17:44:46 -08:00
const extend = require('xtend')
const EthStore = require('eth-store')
2016-01-15 02:03:42 -08:00
const PortStream = require('./lib/port-stream.js')
const MetaMaskProvider = require('web3-provider-engine/zero.js')
2016-02-10 17:44:46 -08:00
const IdentityStore = require('./lib/idStore')
const createTxNotification = require('./lib/tx-notification.js')
2015-07-31 18:38:02 -07:00
2016-01-15 02:03:42 -08:00
console.log('ready to roll')
2015-12-18 22:05:16 -08:00
2016-02-10 17:44:46 -08:00
//
// connect to other contexts
//
2016-01-16 16:22:54 -08:00
chrome.runtime.onConnect.addListener(connectRemote)
2015-12-18 22:05:16 -08:00
function connectRemote(remotePort){
2016-01-15 02:03:42 -08:00
var isMetaMaskInternalProcess = (remotePort.name === 'popup')
2016-03-09 18:33:30 -08:00
var portStream = new PortStream(remotePort)
2016-01-15 02:03:42 -08:00
if (isMetaMaskInternalProcess) {
// communication with popup
2016-03-09 18:33:30 -08:00
handleInternalCommunication(portStream)
2016-01-15 02:03:42 -08:00
} else {
// communication with page
2016-03-09 18:33:30 -08:00
handleEthRpcRequestStream(portStream)
2016-01-15 02:03:42 -08:00
}
}
2016-03-09 18:33:30 -08:00
function handleEthRpcRequestStream(stream){
stream.on('data', onRpcRequest.bind(null, stream))
2016-02-10 17:44:46 -08:00
}
2016-01-17 01:27:25 -08:00
2016-02-10 17:44:46 -08:00
//
// state and network
//
2016-01-17 01:27:25 -08:00
var config = getConfig()
2016-02-10 17:44:46 -08:00
var idStore = new IdentityStore()
var zeroClient = MetaMaskProvider({
rpcUrl: config.rpcTarget,
2016-02-10 17:44:46 -08:00
getAccounts: function(cb){
var selectedAddress = idStore.getSelectedAddress()
var result = selectedAddress ? [selectedAddress] : []
cb(null, result)
},
2016-03-10 15:39:31 -08:00
approveTransaction: addUnconfirmedTx,
2016-02-20 12:13:18 -08:00
signTransaction: idStore.signTransaction.bind(idStore),
2016-02-10 17:44:46 -08:00
})
// log new blocks
zeroClient.on('block', function(block){
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
})
2016-02-10 17:44:46 -08:00
var ethStore = new EthStore(zeroClient)
idStore.setStore(ethStore)
2016-01-15 02:03:42 -08:00
2016-02-10 17:44:46 -08:00
function getState(){
var state = extend(
ethStore.getState(),
idStore.getState(),
getConfig()
)
2016-02-10 17:44:46 -08:00
return state
}
2016-01-15 02:03:42 -08:00
// handle rpc requests
2016-03-09 18:33:30 -08:00
function onRpcRequest(remoteStream, payload){
2016-01-15 02:03:42 -08:00
// console.log('MetaMaskPlugin - incoming payload:', payload)
2015-12-18 22:05:16 -08:00
zeroClient.sendAsync(payload, function onPayloadHandled(err, response){
2016-03-08 13:27:38 -08:00
// provider engine errors are included in response objects
2016-03-10 15:39:31 -08:00
if (!payload.isMetamaskInternal) console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
2016-02-10 11:46:13 -08:00
try {
2016-03-10 13:04:45 -08:00
remoteStream.write(response)
2016-03-09 18:33:30 -08:00
} catch (err) {
console.error(err)
2016-02-10 11:46:13 -08:00
}
2015-12-18 22:05:16 -08:00
})
}
2015-08-01 16:33:31 -07:00
2016-02-10 17:44:46 -08:00
//
// popup integration
//
2016-03-09 18:33:30 -08:00
function handleInternalCommunication(portStream){
// setup multiplexing
var mx = Multiplex()
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()
})
var dnodeStream = mx.createSharedStream('dnode')
2016-03-10 13:04:45 -08:00
var providerStream = combineStreams(
jsonStringifyStream(),
mx.createSharedStream('provider'),
jsonParseStream()
)
2016-03-09 18:33:30 -08:00
linkDnode(dnodeStream)
handleEthRpcRequestStream(providerStream)
}
function linkDnode(stream){
2016-02-10 17:44:46 -08:00
var connection = Dnode({
getState: function(cb){ cb(null, getState()) },
setRpcTarget: setRpcTarget,
2016-02-10 17:44:46 -08:00
// forward directly to idStore
2016-02-17 00:55:57 -08:00
createNewVault: idStore.createNewVault.bind(idStore),
2016-03-15 13:39:12 -07:00
recoverFromSeed: idStore.recoverFromSeed.bind(idStore),
2016-02-10 17:44:46 -08:00
submitPassword: idStore.submitPassword.bind(idStore),
setSelectedAddress: idStore.setSelectedAddress.bind(idStore),
2016-03-02 22:58:23 -08:00
approveTransaction: idStore.approveTransaction.bind(idStore),
2016-02-12 12:55:20 -08:00
cancelTransaction: idStore.cancelTransaction.bind(idStore),
2016-02-10 17:44:46 -08:00
setLocked: idStore.setLocked.bind(idStore),
clearSeedWordCache: idStore.clearSeedWordCache.bind(idStore),
2016-02-10 17:44:46 -08:00
})
2016-03-09 18:33:30 -08:00
stream.pipe(connection).pipe(stream)
2016-02-10 17:44:46 -08:00
connection.on('remote', function(remote){
2016-02-10 17:44:46 -08:00
// push updates to popup
ethStore.on('update', sendUpdate)
idStore.on('update', sendUpdate)
// teardown on disconnect
2016-03-09 18:33:30 -08:00
eos(stream, function unsubscribe(){
2016-02-10 17:44:46 -08:00
ethStore.removeListener('update', sendUpdate)
})
function sendUpdate(){
var state = getState()
remote.sendUpdate(state)
}
})
}
//
// plugin badge text
//
idStore.on('update', updateBadge)
2015-12-18 22:05:16 -08:00
2016-01-18 17:05:46 -08:00
function updateBadge(state){
var label = ''
var count = Object.keys(state.unconfTxs).length
if (count) {
label = String(count)
}
chrome.browserAction.setBadgeText({ text: label })
chrome.browserAction.setBadgeBackgroundColor({ color: '#506F8B' })
2016-01-18 17:05:46 -08:00
}
2015-12-18 22:05:16 -08:00
2016-03-10 15:39:31 -08:00
//
// Add unconfirmed Tx
//
function addUnconfirmedTx(txParams, cb){
var txId = idStore.addUnconfirmedTransaction(txParams, cb)
createTxNotification({
title: 'New Unsigned Transaction',
txParams: txParams,
confirm: idStore.approveTransaction.bind(idStore, txId, noop),
cancel: idStore.cancelTransaction.bind(idStore, txId),
2016-03-10 15:39:31 -08:00
})
}
//
// config
//
// called from popup
function setRpcTarget(rpcTarget){
var config = getConfig()
config.rpcTarget = rpcTarget
setConfig(config)
chrome.runtime.reload()
}
function getConfig(){
return extend({
rpcTarget: 'https://rawtestrpc.metamask.io/',
}, JSON.parse(localStorage['config'] || '{}'))
}
function setConfig(state){
localStorage['config'] = JSON.stringify(state)
2016-03-09 18:33:30 -08:00
}
// util
function jsonParseStream(){
return Through.obj(function(serialized){
this.push(JSON.parse(serialized))
cb()
})
}
function jsonStringifyStream(){
return Through.obj(function(obj){
this.push(JSON.stringify(obj))
cb()
})
}
function noop(){}