aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/background.js
blob: c439558c80c86743ddf204edecc1e4b96602b129 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const ZeroClientProvider = require('web3-provider-skeleton')
// const PortStream = require('./lib/port-stream.js')
const identitiesUrl = 'https://alpha.metamask.io/identities/'

// var unsignedTxs = {}

var zeroClient = ZeroClientProvider()

// setup badge click handler
chrome.browserAction.onClicked.addListener(function(activeTab) {
  chrome.tabs.create({ url: identitiesUrl })
})

// setup messaging
chrome.runtime.onConnect.addListener(connectRemote)
// chrome.runtime.onConnectExternal.addListener(connectRemote)
function connectRemote(remotePort){
  remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort))
}

function onRpcRequest(remotePort, payload){
  zeroClient.sendAsync(payload, function onPayloadHandled(err, response){
    if (err) throw err
    // console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
    // if (typeof response !== 'object') {
    // if (!response) {
    //   console.warn('-------------------------------')
    //   console.warn(payload, '->', response)
    //   console.warn('-------------------------------')
    // }
    remotePort.postMessage(response)
  })
}

// // 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
// 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){
    
//     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
// }