1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 00:28:06 +01:00
metamask-extension/mascara/src/background.js

138 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-03-10 03:24:41 +01:00
global.window = global
2017-03-21 23:35:01 +01:00
const SwGlobalListener = require('sw-stream/lib/sw-global-listener.js')
2017-10-05 08:03:47 +02:00
const connectionListener = new SwGlobalListener(global)
2017-04-03 07:01:44 +02:00
const setupMultiplex = require('../../app/scripts/lib/stream-utils.js').setupMultiplex
2017-03-10 03:24:41 +01:00
const DbController = require('idb-global')
2017-03-10 03:24:41 +01:00
const SwPlatform = require('../../app/scripts/platforms/sw')
2017-04-03 07:01:44 +02:00
const MetamaskController = require('../../app/scripts/metamask-controller')
2017-03-30 20:28:22 +02:00
2017-04-03 07:01:44 +02:00
const Migrator = require('../../app/scripts/lib/migrator/')
const migrations = require('../../app/scripts/migrations/')
const firstTimeState = require('../../app/scripts/first-time-state')
2017-03-10 03:24:41 +01:00
const STORAGE_KEY = 'metamask-config'
2017-09-20 01:48:42 +02:00
const METAMASK_DEBUG = process.env.METAMASK_DEBUG
2017-10-05 08:03:47 +02:00
global.metamaskPopupIsOpen = false
2017-03-10 03:24:41 +01:00
2017-03-23 19:06:38 +01:00
const log = require('loglevel')
global.log = log
log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn')
2017-10-05 08:03:47 +02:00
global.addEventListener('install', function (event) {
event.waitUntil(global.skipWaiting())
2017-03-10 03:24:41 +01:00
})
2017-10-05 08:03:47 +02:00
global.addEventListener('activate', function (event) {
event.waitUntil(global.clients.claim())
2017-03-10 03:24:41 +01:00
})
2017-10-05 08:07:40 +02:00
log.debug('inside:open')
2017-03-10 03:24:41 +01:00
// // state persistence
const dbController = new DbController({
key: STORAGE_KEY,
})
2017-04-04 11:08:10 +02:00
loadStateFromPersistence()
.then((initState) => setupController(initState))
2017-10-05 08:07:40 +02:00
.then(() => log.debug('MetaMask initialization complete.'))
2017-04-04 11:08:10 +02:00
.catch((err) => console.error('WHILE SETTING UP:', err))
2017-03-10 03:24:41 +01:00
//
// State and Persistence
//
2017-10-05 08:03:47 +02:00
async function loadStateFromPersistence () {
2017-03-10 03:24:41 +01:00
// migrations
2017-10-05 08:03:47 +02:00
const migrator = new Migrator({ migrations })
2017-03-10 03:24:41 +01:00
const initialState = migrator.generateInitialState(firstTimeState)
dbController.initialState = initialState
2017-10-05 08:03:47 +02:00
const versionedData = await dbController.open()
const migratedData = await migrator.migrateData(versionedData)
await dbController.put(migratedData)
return migratedData.data
2017-03-10 03:24:41 +01:00
}
function setupController (initState, client) {
//
// MetaMask Controller
//
const platform = new SwPlatform()
2017-03-10 03:24:41 +01:00
const controller = new MetamaskController({
// platform specific implementation
platform,
2017-03-10 03:24:41 +01:00
// User confirmation callbacks:
2017-03-30 20:28:22 +02:00
showUnconfirmedMessage: noop,
unlockAccountMessage: noop,
showUnapprovedTx: noop,
2017-03-10 03:24:41 +01:00
// initial state
initState,
})
global.metamaskController = controller
controller.store.subscribe((state) => {
2017-03-24 03:14:08 +01:00
versionifyData(state)
.then((versionedData) => dbController.put(versionedData))
2017-10-05 08:03:47 +02:00
.catch((err) => { console.error(err) })
2017-03-23 19:06:38 +01:00
})
2017-10-05 08:03:47 +02:00
function versionifyData (state) {
2017-03-24 03:14:08 +01:00
return dbController.get()
.then((rawData) => {
return Promise.resolve({
data: state,
meta: rawData.meta,
2017-10-05 08:03:47 +02:00
})
}
2017-03-24 03:14:08 +01:00
)
2017-03-10 03:24:41 +01:00
}
//
// connect to other contexts
//
2017-04-04 14:25:52 +02:00
2017-03-21 23:35:01 +01:00
connectionListener.on('remote', (portStream, messageEvent) => {
2017-10-05 08:07:40 +02:00
log.debug('REMOTE CONECTION FOUND***********')
connectRemote(portStream, messageEvent.data.context)
2017-03-21 23:35:01 +01:00
})
2017-03-10 03:24:41 +01:00
function connectRemote (connectionStream, context) {
var isMetaMaskInternalProcess = (context === 'popup')
2017-03-10 03:24:41 +01:00
if (isMetaMaskInternalProcess) {
// communication with popup
controller.setupTrustedCommunication(connectionStream, 'MetaMask')
2017-10-05 08:03:47 +02:00
global.metamaskPopupIsOpen = true
2017-03-10 03:24:41 +01:00
} else {
// communication with page
2017-03-30 20:28:22 +02:00
setupUntrustedCommunication(connectionStream, context)
2017-03-10 03:24:41 +01:00
}
}
function setupUntrustedCommunication (connectionStream, originDomain) {
// setup multiplexing
var mx = setupMultiplex(connectionStream)
// connect features
controller.setupProviderConnection(mx.createStream('provider'), originDomain)
controller.setupPublicConfig(mx.createStream('publicConfig'))
}
//
// User Interface setup
//
return Promise.resolve()
}
2017-10-05 08:03:47 +02:00
// // this will be useful later but commented out for linting for now (liiiinting)
// function sendMessageToAllClients (message) {
// global.clients.matchAll().then(function (clients) {
// clients.forEach(function (client) {
// client.postMessage(message)
// })
// })
// }
2017-03-30 20:28:22 +02:00
function noop () {}