library - popup handler demo

This commit is contained in:
kumavis 2016-08-26 11:08:23 -07:00
parent bbb684309e
commit 23a144fb8b
10 changed files with 241 additions and 0 deletions

4
library/example.sh Executable file
View File

@ -0,0 +1,4 @@
# run 2 servers and make sure they close together
beefy frame.js:bundle.js 9001 --live &
beefy example/index.js:bundle.js index.js:zero.js --cwd example/ 9002 --live --open

View File

@ -0,0 +1,5 @@
```
trap 'kill %1' SIGINT
beefy frame.js:bundle.js 9001 --live & \
beefy example/index.js:bundle.js index.js:zero.js --cwd example/ 9002 --live --open
```

View File

@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MetaMask ZeroClient Example</title>
</head>
<body>
<button class="action-button-1">SYNC TX</button>
<button class="action-button-2">ASYNC TX</button>
<script src="./zero.js"></script>
<script src="./bundle.js"></script>
</body>
</html>

42
library/example/index.js Normal file
View File

@ -0,0 +1,42 @@
window.addEventListener('load', web3Detect)
function web3Detect() {
if (global.web3) {
document.body.innerHTML += 'web3 detected!'
} else {
document.body.innerHTML += 'no web3 detected!'
}
startApp()
}
var primaryAccount = null
web3.eth.getAccounts(function(err, addresses){
if (err) throw err
primaryAccount = addresses[0]
})
function startApp(){
document.querySelector('.action-button-1').addEventListener('click', function(){
web3.eth.sendTransaction({
from: primaryAccount,
value: 0,
}, function(err, txHash){
if (err) throw err
console.log('sendTransaction result:', err || txHash)
})
})
document.querySelector('.action-button-2').addEventListener('click', function(){
setTimeout(function(){
web3.eth.sendTransaction({
from: primaryAccount,
value: 0,
}, function(err, txHash){
if (err) throw err
console.log('sendTransaction result:', err || txHash)
})
})
})
}

67
library/frame.js Normal file
View File

@ -0,0 +1,67 @@
const ZeroClientProvider = require('web3-provider-engine/zero')
const ParentStream = require('iframe-stream').ParentStream
const handleRequestsFromStream = require('web3-stream-provider/handler')
const Streams = require('mississippi')
const ObjectMultiplex = require('../app/scripts/lib/obj-multiplex')
console.log('yes, this is iframe')
initializeZeroClient()
function initializeZeroClient() {
var provider = ZeroClientProvider({
// rpcUrl: configManager.getCurrentRpcAddress(),
rpcUrl: 'https://morden.infura.io/',
// account mgmt
// getAccounts: function(cb){
// var selectedAddress = idStore.getSelectedAddress()
// var result = selectedAddress ? [selectedAddress] : []
// cb(null, result)
// },
getAccounts: function(cb){
cb(null, ['0x8F331A98aC5C9431d04A5d6Bf8Fa84ed7Ed439f3'.toLowerCase()])
},
// tx signing
// approveTransaction: addUnconfirmedTx,
// signTransaction: idStore.signTransaction.bind(idStore),
signTransaction: function(txParams, cb){
var privKey = new Buffer('7ef33e339ba5a5af0e57fa900ad0ae53deaa978c21ef30a0947532135eb639a8', 'hex')
var Transaction = require('ethereumjs-tx')
console.log('signing tx:', txParams)
txParams.gasLimit = txParams.gas
var tx = new Transaction(txParams)
tx.sign(privKey)
var serialiedTx = '0x'+tx.serialize().toString('hex')
cb(null, serialiedTx)
},
// msg signing
// approveMessage: addUnconfirmedMsg,
// signMessage: idStore.signMessage.bind(idStore),
})
provider.on('block', function(block){
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
})
var connectionStream = new ParentStream()
// setup connectionStream multiplexing
var multiStream = ObjectMultiplex()
Streams.pipe(connectionStream, multiStream, connectionStream, function(err){
console.warn('MetamaskIframe - lost connection to Dapp')
if (err) throw err
})
var providerStream = multiStream.createStream('provider')
handleRequestsFromStream(providerStream, provider, logger)
function logger(err, request, response){
if (err) return console.error(err.stack)
if (!request.isMetamaskInternal) {
console.log('MetaMaskIframe - RPC complete:', request, '->', response)
if (response.error) console.error('Error in RPC response:\n'+response.error.message)
}
}
}

20
library/index.html Normal file
View File

@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MetaMask ZeroClient Iframe</title>
<meta name="description" content="MetaMask ZeroClient">
<meta name="author" content="MetaMask">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
Hello! I am the MetaMask iframe.
<script src="/bundle.js"></script>
</body>
</html>

42
library/index.js Normal file
View File

@ -0,0 +1,42 @@
const Web3 = require('web3')
const setupProvider = require('./lib/setup-provider.js')
//
// setup web3
//
var provider = setupProvider()
hijackProvider(provider)
var web3 = new Web3(provider)
web3.setProvider = function(){
console.log('MetaMask - overrode web3.setProvider')
}
//
// export web3
//
global.web3 = web3
//
// ui stuff
//
var shouldPop = false
window.addEventListener('click', function(){
if (!shouldPop) return
shouldPop = false
window.open('popup.html', '', 'width=1000')
console.log('opening window...')
})
function hijackProvider(provider){
var _super = provider.sendAsync.bind(provider)
provider.sendAsync = function(payload, cb){
if (payload.method === 'eth_sendTransaction') {
shouldPop = true
}
_super(payload, cb)
}
}

View File

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

View File

@ -0,0 +1,24 @@
const setupIframe = require('./setup-iframe.js')
const MetamaskInpageProvider = require('../../app/scripts/lib/inpage-provider.js')
module.exports = getProvider
function getProvider(){
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: 'http://localhost:9001',
sandboxAttributes: ['allow-scripts', 'allow-popups', 'allow-same-origin'],
})
var inpageProvider = new MetamaskInpageProvider(iframeStream)
return inpageProvider
}

View File

@ -48,6 +48,8 @@
"gulp-eslint": "^2.0.0",
"hat": "0.0.3",
"identicon.js": "^1.2.1",
"iframe": "^1.0.0",
"iframe-stream": "^1.0.2",
"inject-css": "^0.1.1",
"jazzicon": "^1.1.3",
"menu-droppo": "^1.1.0",