aboutsummaryrefslogtreecommitdiffstats
path: root/library
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2016-08-27 02:08:23 +0800
committerkumavis <aaron@kumavis.me>2016-08-27 02:08:23 +0800
commit23a144fb8b0b214b9c352ee523405d5ecd03046e (patch)
tree43e464ffc4eac01ecba092e758aee3e9b03c8b17 /library
parentbbb684309e04030dbf288f3e933222b3d27d9b54 (diff)
downloadtangerine-wallet-browser-23a144fb8b0b214b9c352ee523405d5ecd03046e.tar
tangerine-wallet-browser-23a144fb8b0b214b9c352ee523405d5ecd03046e.tar.gz
tangerine-wallet-browser-23a144fb8b0b214b9c352ee523405d5ecd03046e.tar.bz2
tangerine-wallet-browser-23a144fb8b0b214b9c352ee523405d5ecd03046e.tar.lz
tangerine-wallet-browser-23a144fb8b0b214b9c352ee523405d5ecd03046e.tar.xz
tangerine-wallet-browser-23a144fb8b0b214b9c352ee523405d5ecd03046e.tar.zst
tangerine-wallet-browser-23a144fb8b0b214b9c352ee523405d5ecd03046e.zip
library - popup handler demo
Diffstat (limited to 'library')
-rwxr-xr-xlibrary/example.sh4
-rw-r--r--library/example/README.md5
-rw-r--r--library/example/index.html17
-rw-r--r--library/example/index.js42
-rw-r--r--library/frame.js67
-rw-r--r--library/index.html20
-rw-r--r--library/index.js42
-rw-r--r--library/lib/setup-iframe.js18
-rw-r--r--library/lib/setup-provider.js24
9 files changed, 239 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
+
+}
+