diff options
-rwxr-xr-x | library/example.sh | 4 | ||||
-rw-r--r-- | library/example/README.md | 5 | ||||
-rw-r--r-- | library/example/index.html | 17 | ||||
-rw-r--r-- | library/example/index.js | 42 | ||||
-rw-r--r-- | library/frame.js | 67 | ||||
-rw-r--r-- | library/index.html | 20 | ||||
-rw-r--r-- | library/index.js | 42 | ||||
-rw-r--r-- | library/lib/setup-iframe.js | 18 | ||||
-rw-r--r-- | library/lib/setup-provider.js | 24 | ||||
-rw-r--r-- | package.json | 2 |
10 files changed, 241 insertions, 0 deletions
diff --git a/library/example.sh b/library/example.sh new file mode 100755 index 000000000..73f9cd20c --- /dev/null +++ b/library/example.sh @@ -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
\ No newline at end of file diff --git a/library/example/README.md b/library/example/README.md new file mode 100644 index 000000000..2a5a1cce0 --- /dev/null +++ b/library/example/README.md @@ -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 +```
\ No newline at end of file diff --git a/library/example/index.html b/library/example/index.html new file mode 100644 index 000000000..aa15a4523 --- /dev/null +++ b/library/example/index.html @@ -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>
\ No newline at end of file diff --git a/library/example/index.js b/library/example/index.js new file mode 100644 index 000000000..d24c26f87 --- /dev/null +++ b/library/example/index.js @@ -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) + }) + }) + }) + +} diff --git a/library/frame.js b/library/frame.js new file mode 100644 index 000000000..db5baf36a --- /dev/null +++ b/library/frame.js @@ -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) + } + } + +}
\ No newline at end of file diff --git a/library/index.html b/library/index.html new file mode 100644 index 000000000..5a4c66544 --- /dev/null +++ b/library/index.html @@ -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>
\ No newline at end of file diff --git a/library/index.js b/library/index.js new file mode 100644 index 000000000..9991462ab --- /dev/null +++ b/library/index.js @@ -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) + } +}
\ No newline at end of file diff --git a/library/lib/setup-iframe.js b/library/lib/setup-iframe.js new file mode 100644 index 000000000..dfb51c676 --- /dev/null +++ b/library/lib/setup-iframe.js @@ -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 +} diff --git a/library/lib/setup-provider.js b/library/lib/setup-provider.js new file mode 100644 index 000000000..ced07e0f0 --- /dev/null +++ b/library/lib/setup-provider.js @@ -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 + +} + diff --git a/package.json b/package.json index fdf6c822e..4c30db1a1 100644 --- a/package.json +++ b/package.json @@ -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", |