aboutsummaryrefslogtreecommitdiffstats
path: root/mascara/src/background.js
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2017-06-13 04:27:04 +0800
committerGitHub <noreply@github.com>2017-06-13 04:27:04 +0800
commit27220b7bcd5ea1ebdae3bc1b494d28d9c828918c (patch)
treec9385fba3d742ca9cbfb9f3b4c6c9ef29c4f4a1b /mascara/src/background.js
parent57d1a1f1860e50837104a10b7b9f86d398c795ec (diff)
parent8af41f1b0539e70cf4c6e1f4a9f4b10ad13656fc (diff)
downloadtangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.gz
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.bz2
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.lz
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.xz
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.zst
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.zip
Merge branch 'master' into i#1203MainNetSwitch
Diffstat (limited to 'mascara/src/background.js')
-rw-r--r--mascara/src/background.js155
1 files changed, 155 insertions, 0 deletions
diff --git a/mascara/src/background.js b/mascara/src/background.js
new file mode 100644
index 000000000..d9dbf593a
--- /dev/null
+++ b/mascara/src/background.js
@@ -0,0 +1,155 @@
+global.window = global
+const self = global
+const pipe = require('pump')
+
+const SwGlobalListener = require('sw-stream/lib/sw-global-listener.js')
+const connectionListener = new SwGlobalListener(self)
+const setupMultiplex = require('../../app/scripts/lib/stream-utils.js').setupMultiplex
+const PortStream = require('../../app/scripts/lib/port-stream.js')
+
+const DbController = require('idb-global')
+
+const SwPlatform = require('../../app/scripts/platforms/sw')
+const MetamaskController = require('../../app/scripts/metamask-controller')
+const extension = {} //require('../../app/scripts/lib/extension')
+
+const storeTransform = require('obs-store/lib/transform')
+const Migrator = require('../../app/scripts/lib/migrator/')
+const migrations = require('../../app/scripts/migrations/')
+const firstTimeState = require('../../app/scripts/first-time-state')
+
+const STORAGE_KEY = 'metamask-config'
+// const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG'
+const METAMASK_DEBUG = true
+let popupIsOpen = false
+let connectedClientCount = 0
+
+const log = require('loglevel')
+global.log = log
+log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn')
+
+self.addEventListener('install', function(event) {
+ event.waitUntil(self.skipWaiting())
+})
+self.addEventListener('activate', function(event) {
+ event.waitUntil(self.clients.claim())
+})
+
+console.log('inside:open')
+
+
+// // state persistence
+let diskStore
+const dbController = new DbController({
+ key: STORAGE_KEY,
+})
+loadStateFromPersistence()
+.then((initState) => setupController(initState))
+.then(() => console.log('MetaMask initialization complete.'))
+.catch((err) => console.error('WHILE SETTING UP:', err))
+
+// initialization flow
+
+//
+// State and Persistence
+//
+function loadStateFromPersistence() {
+ // migrations
+ let migrator = new Migrator({ migrations })
+ const initialState = migrator.generateInitialState(firstTimeState)
+ dbController.initialState = initialState
+ return dbController.open()
+ .then((versionedData) => migrator.migrateData(versionedData))
+ .then((versionedData) => {
+ dbController.put(versionedData)
+ return Promise.resolve(versionedData)
+ })
+ .then((versionedData) => Promise.resolve(versionedData.data))
+}
+
+function setupController (initState, client) {
+
+ //
+ // MetaMask Controller
+ //
+
+ const platform = new SwPlatform()
+
+ const controller = new MetamaskController({
+ // platform specific implementation
+ platform,
+ // User confirmation callbacks:
+ showUnconfirmedMessage: noop,
+ unlockAccountMessage: noop,
+ showUnapprovedTx: noop,
+ // initial state
+ initState,
+ })
+ global.metamaskController = controller
+
+ controller.store.subscribe((state) => {
+ versionifyData(state)
+ .then((versionedData) => dbController.put(versionedData))
+ .catch((err) => {console.error(err)})
+ })
+ function versionifyData(state) {
+ return dbController.get()
+ .then((rawData) => {
+ return Promise.resolve({
+ data: state,
+ meta: rawData.meta,
+ })}
+ )
+ }
+
+ //
+ // connect to other contexts
+ //
+
+ connectionListener.on('remote', (portStream, messageEvent) => {
+ console.log('REMOTE CONECTION FOUND***********')
+ connectedClientCount += 1
+ connectRemote(portStream, messageEvent.data.context)
+ })
+
+ function connectRemote (connectionStream, context) {
+ var isMetaMaskInternalProcess = (context === 'popup')
+ if (isMetaMaskInternalProcess) {
+ // communication with popup
+ controller.setupTrustedCommunication(connectionStream, 'MetaMask')
+ popupIsOpen = true
+ } else {
+ // communication with page
+ setupUntrustedCommunication(connectionStream, context)
+ }
+ }
+
+ function setupUntrustedCommunication (connectionStream, originDomain) {
+ // setup multiplexing
+ var mx = setupMultiplex(connectionStream)
+ // connect features
+ controller.setupProviderConnection(mx.createStream('provider'), originDomain)
+ controller.setupPublicConfig(mx.createStream('publicConfig'))
+ }
+
+ function setupTrustedCommunication (connectionStream, originDomain) {
+ // setup multiplexing
+ var mx = setupMultiplex(connectionStream)
+ // connect features
+ controller.setupProviderConnection(mx.createStream('provider'), originDomain)
+ }
+ //
+ // User Interface setup
+ //
+ return Promise.resolve()
+
+}
+
+function sendMessageToAllClients (message) {
+ self.clients.matchAll().then(function(clients) {
+ clients.forEach(function(client) {
+ client.postMessage(message)
+ })
+ })
+}
+function noop () {}