nifty-wallet/app/scripts/background.js

114 lines
3.1 KiB
JavaScript
Raw Normal View History

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