Merge pull request #2124 from MetaMask/breakout-mascara

require metamascara
feature/default_network_editable
kumavis 7 years ago committed by GitHub
commit cdf41c2857
  1. 1
      app/scripts/platforms/extension.js
  2. 67
      mascara/example/app.js
  3. 6
      mascara/example/app/index.html
  4. 3
      mascara/src/background.js
  5. 19
      mascara/src/lib/setup-iframe.js
  6. 22
      mascara/src/lib/setup-provider.js
  7. 48
      mascara/src/mascara.js
  8. 1
      mock-dev.js
  9. 3
      package.json
  10. 1
      test/unit/metamask-controller-test.js

@ -5,7 +5,6 @@ class ExtensionPlatform {
//
// Public
//
reload () {
extension.runtime.reload()
}

@ -1,57 +1,26 @@
window.addEventListener('load', web3Detect)
const EthQuery = require('ethjs-query')
window.addEventListener('load', loadProvider)
window.addEventListener('message', console.warn)
function web3Detect() {
if (global.web3) {
logToDom('web3 detected!')
startApp()
} else {
logToDom('no web3 detected!')
}
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)
}
function startApp(){
console.log('app started')
var primaryAccount
console.log('getting main account...')
web3.eth.getAccounts((err, addresses) => {
if (err) console.error(err)
console.log('set address', addresses[0])
primaryAccount = addresses[0]
})
document.querySelector('.action-button-1').addEventListener('click', function(){
console.log('saw click')
console.log('sending tx')
primaryAccount
web3.eth.sendTransaction({
from: primaryAccount,
to: primaryAccount,
value: 0,
}, function(err, txHash){
if (err) throw err
console.log('sendTransaction result:', err || txHash)
})
})
document.querySelector('.action-button-2').addEventListener('click', function(){
console.log('saw click')
setTimeout(function(){
console.log('sending tx')
web3.eth.sendTransaction({
from: primaryAccount,
to: primaryAccount,
value: 0,
}, function(err, txHash){
if (err) throw err
console.log('sendTransaction result:', err || txHash)
})
})
})
}
function logToDom(message){
document.body.appendChild(document.createTextNode(message))
document.getElementById('account').innerText = message
console.log(message)
}
function setupButton (ethQuery) {
const button = document.getElementById('action-button-1')
button.addEventListener('click', async () => {
const accounts = await ethQuery.accounts()
logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined')
})
}

@ -3,13 +3,13 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>MetaMask ZeroClient Example</title>
<script src="http://localhost:9001/metamascara.js"></script>
<title>MetaMask ZeroClient Example</title>
</head>
<body>
<button class="action-button-1">SYNC TX</button>
<button class="action-button-2">ASYNC TX</button>
<button id="action-button-1">GET ACCOUNT</button>
<div id="account"></div>
<script src="./app.js"></script>
</body>
</html>

@ -19,8 +19,7 @@ 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
const METAMASK_DEBUG = process.env.METAMASK_DEBUG
let popupIsOpen = false
let connectedClientCount = 0

@ -1,19 +0,0 @@
const Iframe = require('iframe')
const createIframeStream = require('iframe-stream').IframeStream
module.exports = setupIframe
function setupIframe(opts) {
opts = opts || {}
var frame = Iframe({
src: opts.zeroClientProvider || 'https://zero.metamask.io/',
container: opts.container || document.head,
sandboxAttributes: opts.sandboxAttributes || ['allow-scripts', 'allow-popups'],
})
var iframe = frame.iframe
iframe.style.setProperty('display', 'none')
var iframeStream = createIframeStream(iframe)
return iframeStream
}

@ -1,22 +0,0 @@
const setupIframe = require('./setup-iframe.js')
const MetamaskInpageProvider = require('../../../app/scripts/lib/inpage-provider.js')
module.exports = getProvider
function getProvider(opts){
if (global.web3) {
console.log('MetaMask ZeroClient - using environmental web3 provider')
return global.web3.currentProvider
}
console.log('MetaMask ZeroClient - injecting zero-client iframe!')
var iframeStream = setupIframe({
zeroClientProvider: opts.mascaraUrl,
sandboxAttributes: ['allow-scripts', 'allow-popups', 'allow-same-origin'],
container: document.body,
})
var inpageProvider = new MetamaskInpageProvider(iframeStream)
return inpageProvider
}

@ -1,47 +1 @@
const Web3 = require('web3')
const setupProvider = require('./lib/setup-provider.js')
const setupDappAutoReload = require('../../app/scripts/lib/auto-reload.js')
const MASCARA_ORIGIN = process.env.MASCARA_ORIGIN || 'http://localhost:9001'
console.log('MASCARA_ORIGIN:', MASCARA_ORIGIN)
//
// setup web3
//
const provider = setupProvider({
mascaraUrl: MASCARA_ORIGIN + '/proxy/',
})
instrumentForUserInteractionTriggers(provider)
const web3 = new Web3(provider)
setupDappAutoReload(web3, provider.publicConfigStore)
//
// ui stuff
//
let shouldPop = false
window.addEventListener('click', maybeTriggerPopup)
//
// util
//
function maybeTriggerPopup(){
if (!shouldPop) return
shouldPop = false
window.open(MASCARA_ORIGIN, '', 'width=360 height=500')
console.log('opening window...')
}
function instrumentForUserInteractionTriggers(provider){
const _super = provider.sendAsync.bind(provider)
provider.sendAsync = function(payload, cb){
if (payload.method === 'eth_sendTransaction') {
console.log('saw send')
shouldPop = true
}
_super(payload, cb)
}
}
global.metamask = require('metamascara')

@ -62,6 +62,7 @@ const controller = new MetamaskController({
showUnconfirmedMessage: noop,
unlockAccountMessage: noop,
showUnapprovedTx: noop,
platform: {},
// initial state
initState: firstTimeState,
})

@ -9,7 +9,7 @@
"ui": "npm run test:flat:build:states && beefy ui-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./",
"mock": "beefy mock-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./",
"watch": "mocha watch --recursive \"test/unit/**/*.js\"",
"mascara": "node ./mascara/example/server",
"mascara": "METAMASK_DEBUG=true node ./mascara/example/server",
"dist": "npm run dist:clear && npm install && gulp dist",
"dist:clear": "rm -rf node_modules/eth-contract-metadata && rm -rf node_modules/eth-phishing-detect",
"test": "npm run lint && npm run test:coverage && npm run test:integration",
@ -101,6 +101,7 @@
"json-rpc-engine": "^3.2.0",
"json-rpc-middleware-stream": "^1.0.1",
"loglevel": "^1.4.1",
"metamascara": "^1.3.1",
"metamask-logo": "^2.1.2",
"mississippi": "^1.2.0",
"mkdirp": "^0.5.1",

@ -10,6 +10,7 @@ describe('MetaMaskController', function () {
showUnconfirmedMessage: noop,
unlockAccountMessage: noop,
showUnapprovedTx: noop,
platform: {},
// initial state
initState: clone(firstTimeState),
})

Loading…
Cancel
Save