aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Huang <tmashuang@users.noreply.github.com>2017-10-06 02:06:17 +0800
committerGitHub <noreply@github.com>2017-10-06 02:06:17 +0800
commitc880d81ec3bfcd4d46c5d454cf9828adff15a0f5 (patch)
tree7c359f5fe8ca642f0034ac1ce5417a3db004ceb7
parent35f4148343cf4400f7a38eef07282cae44cd0335 (diff)
parent7ec068d279276a7e0fcc862d18e3fd3e1648728b (diff)
downloadtangerine-wallet-browser-c880d81ec3bfcd4d46c5d454cf9828adff15a0f5.tar.gz
tangerine-wallet-browser-c880d81ec3bfcd4d46c5d454cf9828adff15a0f5.tar.zst
tangerine-wallet-browser-c880d81ec3bfcd4d46c5d454cf9828adff15a0f5.zip
Merge pull request #2289 from MetaMask/mascara-tuneups
Mascara tuneups
-rw-r--r--gulpfile.js2
-rw-r--r--mascara/example/app.js28
-rw-r--r--mascara/example/app/index.html2
-rw-r--r--mascara/server/index.js8
-rw-r--r--mascara/server/util.js10
-rw-r--r--mascara/src/background.js98
-rw-r--r--mascara/src/proxy.js4
-rw-r--r--mascara/src/ui.js10
8 files changed, 76 insertions, 86 deletions
diff --git a/gulpfile.js b/gulpfile.js
index ac36cf983..557b58a68 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -151,7 +151,7 @@ gulp.task('copy:watch', function(){
gulp.task('lint', function () {
// Ignoring node_modules, dist/firefox, and docs folders:
- return gulp.src(['app/**/*.js', 'ui/**/*.js', '!node_modules/**', '!dist/firefox/**', '!docs/**', '!app/scripts/chromereload.js'])
+ return gulp.src(['app/**/*.js', 'ui/**/*.js', 'mascara/src/*.js', 'mascara/server/*.js', '!node_modules/**', '!dist/firefox/**', '!docs/**', '!app/scripts/chromereload.js', '!mascara/test/jquery-3.1.0.min.js'])
.pipe(eslint(fs.readFileSync(path.join(__dirname, '.eslintrc'))))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
diff --git a/mascara/example/app.js b/mascara/example/app.js
index d0cb6ba83..598e2c84c 100644
--- a/mascara/example/app.js
+++ b/mascara/example/app.js
@@ -7,20 +7,32 @@ async function loadProvider() {
const ethereumProvider = window.metamask.createDefaultProvider({ host: 'http://localhost:9001' })
const ethQuery = new EthQuery(ethereumProvider)
const accounts = await ethQuery.accounts()
- logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined')
- setupButton(ethQuery)
+ window.METAMASK_ACCOUNT = accounts[0] || 'locked'
+ logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined', 'account')
+ setupButtons(ethQuery)
}
-function logToDom(message){
- document.getElementById('account').innerText = message
+function logToDom(message, context){
+ document.getElementById(context).innerText = message
console.log(message)
}
-function setupButton (ethQuery) {
- const button = document.getElementById('action-button-1')
- button.addEventListener('click', async () => {
+function setupButtons (ethQuery) {
+ const accountButton = document.getElementById('action-button-1')
+ accountButton.addEventListener('click', async () => {
const accounts = await ethQuery.accounts()
- logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined')
+ window.METAMASK_ACCOUNT = accounts[0] || 'locked'
+ logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined', 'account')
+ })
+ const txButton = document.getElementById('action-button-2')
+ txButton.addEventListener('click', async () => {
+ if (!window.METAMASK_ACCOUNT || window.METAMASK_ACCOUNT === 'locked') return
+ const txHash = await ethQuery.sendTransaction({
+ from: window.METAMASK_ACCOUNT,
+ to: window.METAMASK_ACCOUNT,
+ data: '',
+ })
+ logToDom(txHash, 'cb-value')
})
} \ No newline at end of file
diff --git a/mascara/example/app/index.html b/mascara/example/app/index.html
index f3e38877c..8afb6f3f2 100644
--- a/mascara/example/app/index.html
+++ b/mascara/example/app/index.html
@@ -10,6 +10,8 @@
<body>
<button id="action-button-1">GET ACCOUNT</button>
<div id="account"></div>
+ <button id="action-button-2">SEND TRANSACTION</button>
+ <div id="cb-value" ></div>
<script src="./app.js"></script>
</body>
</html> \ No newline at end of file
diff --git a/mascara/server/index.js b/mascara/server/index.js
index 14e3fa18e..12b527e5d 100644
--- a/mascara/server/index.js
+++ b/mascara/server/index.js
@@ -5,7 +5,7 @@ const serveBundle = require('./util').serveBundle
module.exports = createMetamascaraServer
-function createMetamascaraServer(){
+function createMetamascaraServer () {
// start bundlers
const metamascaraBundle = createBundle(__dirname + '/../src/mascara.js')
@@ -17,13 +17,13 @@ function createMetamascaraServer(){
const server = express()
// ui window
serveBundle(server, '/ui.js', uiBundle)
- server.use(express.static(__dirname+'/../ui/'))
- server.use(express.static(__dirname+'/../../dist/chrome'))
+ server.use(express.static(__dirname + '/../ui/'))
+ server.use(express.static(__dirname + '/../../dist/chrome'))
// metamascara
serveBundle(server, '/metamascara.js', metamascaraBundle)
// proxy
serveBundle(server, '/proxy/proxy.js', proxyBundle)
- server.use('/proxy/', express.static(__dirname+'/../proxy'))
+ server.use('/proxy/', express.static(__dirname + '/../proxy'))
// background
serveBundle(server, '/background.js', backgroundBuild)
diff --git a/mascara/server/util.js b/mascara/server/util.js
index 6e25b35d8..6ab41b729 100644
--- a/mascara/server/util.js
+++ b/mascara/server/util.js
@@ -7,14 +7,14 @@ module.exports = {
}
-function serveBundle(server, path, bundle){
- server.get(path, function(req, res){
+function serveBundle (server, path, bundle) {
+ server.get(path, function (req, res) {
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8')
res.send(bundle.latest)
})
}
-function createBundle(entryPoint){
+function createBundle (entryPoint) {
var bundleContainer = {}
@@ -30,8 +30,8 @@ function createBundle(entryPoint){
return bundleContainer
- function bundle() {
- bundler.bundle(function(err, result){
+ function bundle () {
+ bundler.bundle(function (err, result) {
if (err) {
console.log(`Bundle failed! (${entryPoint})`)
console.error(err)
diff --git a/mascara/src/background.js b/mascara/src/background.js
index 5ba865ad8..8aa1d8fe2 100644
--- a/mascara/src/background.js
+++ b/mascara/src/background.js
@@ -1,72 +1,60 @@
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 connectionListener = new SwGlobalListener(global)
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 = process.env.METAMASK_DEBUG
-let popupIsOpen = false
-let connectedClientCount = 0
+global.metamaskPopupIsOpen = false
const log = require('loglevel')
global.log = log
log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn')
-self.addEventListener('install', function(event) {
- event.waitUntil(self.skipWaiting())
+global.addEventListener('install', function (event) {
+ event.waitUntil(global.skipWaiting())
})
-self.addEventListener('activate', function(event) {
- event.waitUntil(self.clients.claim())
+global.addEventListener('activate', function (event) {
+ event.waitUntil(global.clients.claim())
})
-console.log('inside:open')
+log.debug('inside:open')
// // state persistence
-let diskStore
const dbController = new DbController({
key: STORAGE_KEY,
})
loadStateFromPersistence()
.then((initState) => setupController(initState))
-.then(() => console.log('MetaMask initialization complete.'))
+.then(() => log.debug('MetaMask initialization complete.'))
.catch((err) => console.error('WHILE SETTING UP:', err))
-// initialization flow
-
//
// State and Persistence
//
-function loadStateFromPersistence() {
+async function loadStateFromPersistence () {
// migrations
- let migrator = new Migrator({ migrations })
+ const 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))
+ const versionedData = await dbController.open()
+ const migratedData = await migrator.migrateData(versionedData)
+ await dbController.put(migratedData)
+ return migratedData.data
}
-function setupController (initState, client) {
+async function setupController (initState, client) {
//
// MetaMask Controller
@@ -86,19 +74,19 @@ function setupController (initState, client) {
})
global.metamaskController = controller
- controller.store.subscribe((state) => {
- versionifyData(state)
- .then((versionedData) => dbController.put(versionedData))
- .catch((err) => {console.error(err)})
+ controller.store.subscribe(async (state) => {
+ try {
+ const versionedData = await versionifyData(state)
+ await dbController.put(versionedData)
+ } catch (e) { console.error('METAMASK Error:', e) }
})
- function versionifyData(state) {
- return dbController.get()
- .then((rawData) => {
- return Promise.resolve({
- data: state,
- meta: rawData.meta,
- })}
- )
+
+ async function versionifyData (state) {
+ const rawData = await dbController.get()
+ return {
+ data: state,
+ meta: rawData.meta,
+ }
}
//
@@ -106,8 +94,7 @@ function setupController (initState, client) {
//
connectionListener.on('remote', (portStream, messageEvent) => {
- console.log('REMOTE CONECTION FOUND***********')
- connectedClientCount += 1
+ log.debug('REMOTE CONECTION FOUND***********')
connectRemote(portStream, messageEvent.data.context)
})
@@ -116,7 +103,7 @@ function setupController (initState, client) {
if (isMetaMaskInternalProcess) {
// communication with popup
controller.setupTrustedCommunication(connectionStream, 'MetaMask')
- popupIsOpen = true
+ global.metamaskPopupIsOpen = true
} else {
// communication with page
setupUntrustedCommunication(connectionStream, context)
@@ -130,25 +117,14 @@ function setupController (initState, client) {
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()
-
}
+// // 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)
+// })
+// })
+// }
-function sendMessageToAllClients (message) {
- self.clients.matchAll().then(function(clients) {
- clients.forEach(function(client) {
- client.postMessage(message)
- })
- })
-}
function noop () {}
diff --git a/mascara/src/proxy.js b/mascara/src/proxy.js
index 07c5b0e3c..54c5d5cf4 100644
--- a/mascara/src/proxy.js
+++ b/mascara/src/proxy.js
@@ -2,7 +2,7 @@ const createParentStream = require('iframe-stream').ParentStream
const SWcontroller = require('client-sw-ready-event/lib/sw-client.js')
const SwStream = require('sw-stream/lib/sw-stream.js')
-let intervalDelay = Math.floor(Math.random() * (30000 - 1000)) + 1000
+const intervalDelay = Math.floor(Math.random() * (30000 - 1000)) + 1000
const background = new SWcontroller({
fileName: '/background.js',
letBeIdle: false,
@@ -12,7 +12,7 @@ const background = new SWcontroller({
const pageStream = createParentStream()
background.on('ready', () => {
- let swStream = SwStream({
+ const swStream = SwStream({
serviceWorker: background.controller,
context: 'dapp',
})
diff --git a/mascara/src/ui.js b/mascara/src/ui.js
index 2f940ad1a..b272a2e06 100644
--- a/mascara/src/ui.js
+++ b/mascara/src/ui.js
@@ -17,17 +17,17 @@ var name = 'popup'
window.METAMASK_UI_TYPE = name
window.METAMASK_PLATFORM_TYPE = 'mascara'
-let intervalDelay = Math.floor(Math.random() * (30000 - 1000)) + 1000
+const intervalDelay = Math.floor(Math.random() * (30000 - 1000)) + 1000
const background = new SWcontroller({
fileName: '/background.js',
letBeIdle: false,
intervalDelay,
- wakeUpInterval: 20000
+ wakeUpInterval: 20000,
})
// Setup listener for when the service worker is read
const connectApp = function (readSw) {
- let connectionStream = SwStream({
+ const connectionStream = SwStream({
serviceWorker: background.controller,
context: name,
})
@@ -57,7 +57,7 @@ background.on('updatefound', windowReload)
background.startWorker()
-function windowReload() {
+function windowReload () {
if (window.METAMASK_SKIP_RELOAD) return
window.location.reload()
}
@@ -66,4 +66,4 @@ function timeout (time) {
return new Promise((resolve) => {
setTimeout(resolve, time || 1500)
})
-} \ No newline at end of file
+}