2016-01-15 11:03:42 +01:00
|
|
|
const Dnode = require('dnode')
|
2016-02-11 02:44:46 +01:00
|
|
|
const eos = require('end-of-stream')
|
|
|
|
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-01-15 03:26:54 +01:00
|
|
|
const MetaMaskProvider = require('./lib/metamask-provider')
|
2016-02-11 02:44:46 +01:00
|
|
|
// const IdentityManager = require('./lib/idmgmt')
|
|
|
|
const IdentityStore = require('./lib/idStore')
|
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-02-11 02:44:46 +01:00
|
|
|
//
|
|
|
|
// connect to other contexts
|
|
|
|
//
|
2016-01-17 01:22:54 +01:00
|
|
|
|
2015-08-02 08:36:03 +02: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')
|
|
|
|
if (isMetaMaskInternalProcess) {
|
|
|
|
// communication with popup
|
|
|
|
handleInternalCommunication(remotePort)
|
|
|
|
} else {
|
|
|
|
// communication with page
|
|
|
|
handleExternalCommunication(remotePort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 02:44:46 +01:00
|
|
|
function handleExternalCommunication(remotePort){
|
|
|
|
remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort))
|
|
|
|
}
|
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-02-11 02:44:46 +01:00
|
|
|
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)
|
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())
|
|
|
|
return state
|
2015-08-02 08:36:03 +02:00
|
|
|
}
|
|
|
|
|
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
|
2016-02-09 02:12:53 +01:00
|
|
|
// console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
|
2016-02-10 20:46:13 +01:00
|
|
|
try {
|
|
|
|
remotePort.postMessage(response)
|
|
|
|
} catch (_) {
|
|
|
|
// port disconnected
|
|
|
|
}
|
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
|
|
|
|
//
|
|
|
|
|
|
|
|
function handleInternalCommunication(remotePort){
|
|
|
|
var duplex = new PortStream(remotePort)
|
|
|
|
var connection = Dnode({
|
|
|
|
getState: function(cb){ cb(null, getState()) },
|
|
|
|
// forward directly to idStore
|
2016-02-17 09:55:57 +01:00
|
|
|
createNewVault: idStore.createNewVault.bind(idStore),
|
2016-02-11 02:44:46 +01:00
|
|
|
submitPassword: idStore.submitPassword.bind(idStore),
|
|
|
|
setSelectedAddress: idStore.setSelectedAddress.bind(idStore),
|
|
|
|
signTransaction: idStore.signTransaction.bind(idStore),
|
2016-02-12 21:55:20 +01:00
|
|
|
cancelTransaction: idStore.cancelTransaction.bind(idStore),
|
2016-02-13 02:57:10 +01:00
|
|
|
sendTransaction: idStore.sendTransaction.bind(idStore),
|
2016-02-11 02:44:46 +01:00
|
|
|
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)
|
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
|
|
|
|