aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/background.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2015-12-19 14:05:16 +0800
committerkumavis <aaron@kumavis.me>2015-12-19 14:05:16 +0800
commit72a747165dda417aa7968e44b404eb90707202a2 (patch)
tree0fd4c52727815665b8851ca80b2b8d9dc462b19a /app/scripts/background.js
parentcc935a1ebaa73d855056d143ef548ca982a5f8e8 (diff)
downloadtangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.gz
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.bz2
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.lz
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.xz
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.zst
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.zip
migrate to ProviderEngine zero-client
Diffstat (limited to 'app/scripts/background.js')
-rw-r--r--app/scripts/background.js238
1 files changed, 127 insertions, 111 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js
index ddb867219..c439558c8 100644
--- a/app/scripts/background.js
+++ b/app/scripts/background.js
@@ -1,7 +1,10 @@
+const ZeroClientProvider = require('web3-provider-skeleton')
+// const PortStream = require('./lib/port-stream.js')
const identitiesUrl = 'https://alpha.metamask.io/identities/'
-const messagingChannelName = 'metamask'
-var unsignedTxs = {}
+// var unsignedTxs = {}
+
+var zeroClient = ZeroClientProvider()
// setup badge click handler
chrome.browserAction.onClicked.addListener(function(activeTab) {
@@ -10,118 +13,131 @@ chrome.browserAction.onClicked.addListener(function(activeTab) {
// setup messaging
chrome.runtime.onConnect.addListener(connectRemote)
-chrome.runtime.onConnectExternal.addListener(connectRemote)
-function connectRemote(remote){
- remote.onMessage.addListener(handleMessage)
- exportUnsignedTxs(remote)
+// chrome.runtime.onConnectExternal.addListener(connectRemote)
+function connectRemote(remotePort){
+ remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort))
}
-// 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()
-})
+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)
+ })
+}
-// 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))
- }
- }
-})
+// // 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 handleMessage(msg){
- console.log('got message!', msg.type)
- switch(msg.type){
+// 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 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 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
-} \ No newline at end of file
+// 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
+// } \ No newline at end of file