nifty-wallet/app/scripts/background.js

139 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-01-14 18:26:54 -08:00
const MetaMaskProvider = require('./lib/metamask-provider')
2015-12-18 22:05:16 -08:00
// const PortStream = require('./lib/port-stream.js')
2015-08-01 16:33:31 -07:00
const identitiesUrl = 'https://alpha.metamask.io/identities/'
2015-07-31 18:38:02 -07:00
2015-12-18 22:05:16 -08:00
// var unsignedTxs = {}
2016-01-14 18:26:54 -08:00
var zeroClient = MetaMaskProvider()
2015-08-01 16:33:31 -07:00
// setup messaging
chrome.runtime.onConnect.addListener(connectRemote)
2015-12-18 22:05:16 -08:00
// chrome.runtime.onConnectExternal.addListener(connectRemote)
function connectRemote(remotePort){
remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort))
}
2015-12-18 22:05:16 -08:00
function onRpcRequest(remotePort, payload){
zeroClient.sendAsync(payload, function onPayloadHandled(err, response){
if (err) throw err
2015-12-20 16:22:18 -08:00
console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
2015-12-22 12:32:50 -08:00
// if (response.result === true) debugger
2015-12-18 22:05:16 -08:00
// if (typeof response !== 'object') {
// if (!response) {
// console.warn('-------------------------------')
// console.warn(payload, '->', response)
// console.warn('-------------------------------')
// }
remotePort.postMessage(response)
})
}
2015-08-01 16:33:31 -07:00
2015-12-18 22:05:16 -08:00
// // load from storage
// chrome.storage.sync.get(function(data){
// for (var key in data) {
// var serialized = data[key]
// var tx = deserializeTx(serialized)
// var hash = simpleHash(serialized)
// unsignedTxs[hash] = tx
// }
// updateBadge()
// })
// // listen to storage changes
// chrome.storage.onChanged.addListener(function(changes, namespace) {
// for (key in changes) {
// var storageChange = changes[key]
// if (storageChange.oldValue && !storageChange.newValue) {
// // was removed
// removeTransaction(storageChange.oldValue)
// } else if (!storageChange.oldValue && storageChange.newValue) {
// // was added
// addTransaction(deserializeTx(storageChange.newValue))
// }
// }
// })
// setup badge text
2015-12-18 22:05:16 -08:00
// updateBadge()
// function updateBadge(){
// var label = ''
// var count = Object.keys(unsignedTxs).length
// if (count) {
// label = String(count)
// }
// chrome.browserAction.setBadgeText({text: label})
// chrome.browserAction.setBadgeBackgroundColor({color: '#506F8B'})
// }
// 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
// }
// }
// function addTransaction(tx){
// var serialized = serializeTx(tx)
// var hash = simpleHash(serialized)
// unsignedTxs[hash] = tx
// var data = {}
// data[hash] = serialized
// chrome.storage.sync.set(data)
// // trigger ui changes
// updateBadge()
// }
// function removeTransaction(serialized){
// var hash = simpleHash(serialized)
// delete unsignedTxs[hash]
// var data = {}
// data[hash] = undefined
// chrome.storage.sync.set(data)
// // trigger ui changes
// updateBadge()
// }
// function exportUnsignedTxs(remote){
// console.log('exporting txs!', unsignedTxs)
// var data = {
// type: 'importUnsignedTxs',
// payload: getValues(unsignedTxs),
// }
// remote.postMessage(data)
// }
// function simpleHash(input) {
// var hash = 0, i, chr, len
// if (input.length == 0) return hash
// for (i = 0, len = input.length; i < len; i++) {
// chr = input.charCodeAt(i)
// hash = ((hash << 5) - hash) + chr
// hash |= 0 // Convert to 32bit integer
// }
// return hash
// }
// function serializeTx(tx){
// return JSON.stringify(tx)
// }
// function deserializeTx(tx){
// return JSON.parse(tx)
// }
// function getValues(obj){
// var output = []
// for (var key in obj) {
// output.push(obj[key])
// }
// return output
// }