1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/background.js

195 lines
5.2 KiB
JavaScript
Raw Normal View History

2016-01-15 11:03:42 +01:00
const Dnode = require('dnode')
2016-03-10 03:33:30 +01:00
const Multiplex = require('multiplex')
2016-02-11 02:44:46 +01:00
const eos = require('end-of-stream')
2016-03-10 22:04:45 +01:00
const combineStreams = require('pumpify')
2016-02-11 02:44:46 +01:00
const extend = require('xtend')
const EthStore = require('eth-store')
2016-01-15 11:03:42 +01:00
const PortStream = require('./lib/port-stream.js')
2016-04-01 01:32:35 +02:00
const MetaMaskProvider = require('./lib/zero.js')
2016-02-11 02:44:46 +01:00
const IdentityStore = require('./lib/idStore')
const createTxNotification = require('./lib/tx-notification.js')
const configManager = require('./lib/config-manager-singleton')
2016-04-08 23:24:10 +02:00
const jsonParseStream = require('./lib/stream-utils.js').jsonParseStream
const jsonStringifyStream = require('./lib/stream-utils.js').jsonStringifyStream
2015-12-19 07:05:16 +01:00
2016-02-11 02:44:46 +01:00
//
// connect to other contexts
//
2016-01-17 01:22:54 +01:00
chrome.runtime.onConnect.addListener(connectRemote)
2015-12-19 07:05:16 +01:00
function connectRemote(remotePort){
2016-01-15 11:03:42 +01:00
var isMetaMaskInternalProcess = (remotePort.name === 'popup')
2016-03-10 03:33:30 +01:00
var portStream = new PortStream(remotePort)
2016-01-15 11:03:42 +01:00
if (isMetaMaskInternalProcess) {
// communication with popup
2016-03-10 03:33:30 +01:00
handleInternalCommunication(portStream)
2016-01-15 11:03:42 +01:00
} else {
// communication with page
2016-03-10 03:33:30 +01:00
handleEthRpcRequestStream(portStream)
2016-01-15 11:03:42 +01:00
}
}
2016-03-10 03:33:30 +01:00
function handleEthRpcRequestStream(stream){
stream.on('data', onRpcRequest.bind(null, stream))
2016-02-11 02:44:46 +01:00
}
2016-01-17 10:27:25 +01:00
2016-02-11 02:44:46 +01:00
//
// state and network
//
2016-01-17 10:27:25 +01:00
2016-04-01 01:32:35 +02:00
var providerConfig = configManager.getProvider()
2016-02-11 02:44:46 +01:00
var idStore = new IdentityStore()
2016-04-01 01:32:35 +02:00
var providerOpts = {
rpcUrl: configManager.getCurrentRpcAddress(),
2016-02-11 02:44:46 +01:00
getAccounts: function(cb){
var selectedAddress = idStore.getSelectedAddress()
var result = selectedAddress ? [selectedAddress] : []
cb(null, result)
},
2016-03-11 00:39:31 +01:00
approveTransaction: addUnconfirmedTx,
2016-02-20 21:13:18 +01:00
signTransaction: idStore.signTransaction.bind(idStore),
2016-04-01 01:32:35 +02:00
}
var provider = MetaMaskProvider(providerOpts)
// log new blocks
2016-04-01 01:32:35 +02:00
provider.on('block', function(block){
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
})
2016-04-01 01:32:35 +02:00
var ethStore = new EthStore(provider)
2016-02-11 02:44:46 +01:00
idStore.setStore(ethStore)
2016-01-15 11:03:42 +01:00
2016-02-11 02:44:46 +01:00
function getState(){
var state = extend(
ethStore.getState(),
idStore.getState(),
configManager.getConfig()
)
2016-02-11 02:44:46 +01:00
return state
}
2016-01-15 11:03:42 +01:00
// handle rpc requests
2016-03-10 03:33:30 +01:00
function onRpcRequest(remoteStream, payload){
2016-01-15 11:03:42 +01:00
// console.log('MetaMaskPlugin - incoming payload:', payload)
2016-04-01 01:32:35 +02:00
provider.sendAsync(payload, function onPayloadHandled(err, response){
2016-03-08 22:27:38 +01:00
// provider engine errors are included in response objects
2016-03-11 00:39:31 +01:00
if (!payload.isMetamaskInternal) console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
2016-02-10 20:46:13 +01:00
try {
2016-03-10 22:04:45 +01:00
remoteStream.write(response)
2016-03-10 03:33:30 +01:00
} catch (err) {
console.error(err)
2016-02-10 20:46:13 +01:00
}
2015-12-19 07:05:16 +01:00
})
}
2015-08-02 01:33:31 +02:00
2016-02-11 02:44:46 +01:00
//
// popup integration
//
2016-03-10 03:33:30 +01: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-04-08 23:24:10 +02:00
var providerStream = combineStreams.obj(
2016-03-10 22:04:45 +01:00
jsonStringifyStream(),
mx.createSharedStream('provider'),
jsonParseStream()
)
2016-03-10 03:33:30 +01:00
linkDnode(dnodeStream)
handleEthRpcRequestStream(providerStream)
}
function linkDnode(stream){
2016-02-11 02:44:46 +01:00
var connection = Dnode({
getState: function(cb){ cb(null, getState()) },
setRpcTarget: setRpcTarget,
2016-04-01 01:32:35 +02:00
useEtherscanProvider: useEtherscanProvider,
2016-02-11 02:44:46 +01:00
// forward directly to idStore
2016-02-17 09:55:57 +01:00
createNewVault: idStore.createNewVault.bind(idStore),
2016-03-15 21:39:12 +01:00
recoverFromSeed: idStore.recoverFromSeed.bind(idStore),
2016-02-11 02:44:46 +01:00
submitPassword: idStore.submitPassword.bind(idStore),
setSelectedAddress: idStore.setSelectedAddress.bind(idStore),
2016-03-03 07:58:23 +01:00
approveTransaction: idStore.approveTransaction.bind(idStore),
2016-02-12 21:55:20 +01:00
cancelTransaction: idStore.cancelTransaction.bind(idStore),
2016-02-11 02:44:46 +01:00
setLocked: idStore.setLocked.bind(idStore),
clearSeedWordCache: idStore.clearSeedWordCache.bind(idStore),
2016-04-06 21:01:10 +02:00
exportAccount: idStore.exportAccount.bind(idStore),
2016-02-11 02:44:46 +01:00
})
2016-03-10 03:33:30 +01:00
stream.pipe(connection).pipe(stream)
2016-02-11 02:44:46 +01:00
connection.on('remote', function(remote){
2016-02-11 02:44:46 +01:00
// push updates to popup
ethStore.on('update', sendUpdate)
idStore.on('update', sendUpdate)
// teardown on disconnect
2016-03-10 03:33:30 +01:00
eos(stream, function unsubscribe(){
2016-02-11 02:44:46 +01:00
ethStore.removeListener('update', sendUpdate)
})
function sendUpdate(){
var state = getState()
remote.sendUpdate(state)
}
})
}
//
// plugin badge text
//
idStore.on('update', updateBadge)
2015-12-19 07:05:16 +01:00
2016-01-19 02:05:46 +01: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-19 02:05:46 +01:00
}
2015-12-19 07:05:16 +01:00
2016-03-11 00:39:31 +01: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-11 00:39:31 +01:00
})
}
//
// config
//
// called from popup
function setRpcTarget(rpcTarget){
configManager.setRpcTarget(rpcTarget)
chrome.runtime.reload()
}
2016-04-01 01:32:35 +02:00
function useEtherscanProvider() {
configManager.useEtherscanProvider()
chrome.runtime.reload()
}
2016-03-10 03:33:30 +01:00
// util
function noop(){}