1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/app/scripts/background.js

110 lines
3.0 KiB
JavaScript
Raw Normal View History

2016-01-15 11:03:42 +01:00
const Dnode = require('dnode')
const PortStream = require('./lib/port-stream.js')
2016-01-15 03:26:54 +01:00
const MetaMaskProvider = require('./lib/metamask-provider')
2016-01-17 01:22:54 +01:00
const IdentityManager = require('./lib/idmgmt')
2016-01-17 10:27:25 +01:00
const eos = require('end-of-stream')
2015-08-01 03:38:02 +02:00
2016-01-15 11:03:42 +01:00
console.log('ready to roll')
2015-12-19 07:05:16 +01:00
2016-01-17 10:27:25 +01:00
var wallet = new IdentityManager()
2016-01-17 01:22:54 +01:00
2016-01-15 11:03:42 +01:00
// setup provider
var zeroClient = MetaMaskProvider({
2016-01-17 01:22:54 +01:00
rpcUrl: 'https://rawtestrpc.metamask.io/',
getAccounts: wallet.getAccounts.bind(wallet),
2016-01-18 08:31:49 +01:00
signTransaction: wallet.addUnconfirmedTransaction.bind(wallet),
2016-01-17 01:22:54 +01:00
})
wallet.setProvider(zeroClient)
zeroClient.on('block', function(block){
wallet.newBlock(block)
2016-01-15 11:03:42 +01:00
})
2015-08-02 01:33:31 +02:00
2016-01-17 01:22:54 +01:00
// setup messaging
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')
if (isMetaMaskInternalProcess) {
// communication with popup
handleInternalCommunication(remotePort)
} else {
// communication with page
handleExternalCommunication(remotePort)
}
}
function handleInternalCommunication(remotePort){
var duplex = new PortStream(remotePort)
2016-01-17 10:27:25 +01:00
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)
}
})
// sub to metamask store
2016-01-15 11:03:42 +01:00
}
function handleExternalCommunication(remotePort){
2015-12-19 07:05:16 +01:00
remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort))
}
2016-01-15 11:03:42 +01:00
// handle rpc requests
2015-12-19 07:05:16 +01:00
function onRpcRequest(remotePort, payload){
2016-01-15 11:03:42 +01:00
// console.log('MetaMaskPlugin - incoming payload:', payload)
2015-12-19 07:05:16 +01:00
zeroClient.sendAsync(payload, function onPayloadHandled(err, response){
if (err) throw err
2015-12-21 01:22:18 +01:00
console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
2015-12-19 07:05:16 +01:00
remotePort.postMessage(response)
})
}
2015-08-02 01:33:31 +02:00
// setup badge text
2016-01-19 02:05:46 +01:00
wallet.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'})
}
2015-12-19 07:05:16 +01:00
// function handleMessage(msg){
// console.log('got message!', msg.type)
// switch(msg.type){
2015-12-19 07:05:16 +01:00
// case 'addUnsignedTx':
// addTransaction(msg.payload)
// return
// case 'removeUnsignedTx':
// removeTransaction(msg.payload)
// return
// }
// }