From 0ff5e7f3e23736e27c1b44f4aae30bcf937de653 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 3 Sep 2018 08:53:53 -0400 Subject: [PATCH] Simplifying subscriptions --- .../components/approveandcallgasrelayed.js | 3 -- test-dapp/app/components/body-identity.js | 30 ++++++--------- .../app/components/body-sntcontroller.js | 37 ++++++++----------- test-dapp/app/status-gas-relayer.js | 34 +++++++++++++++++ 4 files changed, 61 insertions(+), 43 deletions(-) diff --git a/test-dapp/app/components/approveandcallgasrelayed.js b/test-dapp/app/components/approveandcallgasrelayed.js index 63a0b5c..ad1a50a 100644 --- a/test-dapp/app/components/approveandcallgasrelayed.js +++ b/test-dapp/app/components/approveandcallgasrelayed.js @@ -7,7 +7,6 @@ import CardContent from '@material-ui/core/CardContent'; import CardHeader from '@material-ui/core/CardHeader'; import EmbarkJS from 'Embark/EmbarkJS'; import Grid from '@material-ui/core/Grid'; -import IdentityGasRelay from 'Embark/contracts/IdentityGasRelay'; import MySnackbarContentWrapper from './snackbar'; import PropTypes from 'prop-types'; import STT from 'Embark/contracts/STT'; @@ -71,8 +70,6 @@ class ApproveAndCallGasRelayed extends Component { msgSent: false, transactionError: '' }); - - IdentityGasRelay.options.address = this.props.identityAddress; try { const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount) diff --git a/test-dapp/app/components/body-identity.js b/test-dapp/app/components/body-identity.js index 710ba1c..972cd35 100644 --- a/test-dapp/app/components/body-identity.js +++ b/test-dapp/app/components/body-identity.js @@ -1,4 +1,5 @@ import React, {Component, Fragment} from 'react'; +import StatusGasRelayer, {Messages} from '../status-gas-relayer'; import ApproveAndCallGasRelayed from './approveandcallgasrelayed'; import CallGasRelayed from './callgasrelayed'; import Divider from '@material-ui/core/Divider'; @@ -46,32 +47,25 @@ class Body extends Component { web3js.shh.addSymKey(config.relaySymKey) .then((skid) => { this.setState({kid, skid}); - web3js.shh.subscribe('messages', { - "privateKeyID": kid, - "ttl": 1000, - "minPow": 0.1, - "powTime": 1000 - }, (error, message) => { - console.log(message); - const msg = web3js.utils.toAscii(message.payload); - const msgObj = JSON.parse(msg); + StatusGasRelayer.subscribe(web3js, (error, msgObj) => { + if(error) { + console.error(error); + return; + } - if(msgObj.message == 'Available'){ + if(msgObj.message == Messages.available){ // found a relayer - console.log("Relayer available: " + message.sig); - + console.log("Relayer available: " + msgObj.sig); let relayers = this.state.relayers; - relayers.push(message.sig); + relayers.push(msgObj.sig); relayers = relayers.filter((value, index, self) => self.indexOf(value) === index); this.setState({relayers}); } - if(error){ - console.error(error); - } else { - this.setState({message: msg}); - } + this.setState({message: JSON.stringify(msgObj, null, 2)}); + }, { + privateKeyID: kid }); return true; diff --git a/test-dapp/app/components/body-sntcontroller.js b/test-dapp/app/components/body-sntcontroller.js index 28c1220..6eba1f2 100644 --- a/test-dapp/app/components/body-sntcontroller.js +++ b/test-dapp/app/components/body-sntcontroller.js @@ -1,13 +1,14 @@ import React, {Component, Fragment} from 'react'; +import StatusGasRelayer, {Messages} from '../status-gas-relayer'; import Divider from '@material-ui/core/Divider'; import EmbarkJS from 'Embark/EmbarkJS'; -import STT from 'Embark/contracts/STT'; +import Execute from './execute'; import PropTypes from 'prop-types'; +import STT from 'Embark/contracts/STT'; import Status from './status-sntcontroller'; import Tab from '@material-ui/core/Tab'; import Tabs from '@material-ui/core/Tabs'; import TransferSNT from './transfersnt'; -import Execute from './execute'; import Typography from '@material-ui/core/Typography'; import Web3 from 'web3'; import config from '../config'; @@ -48,32 +49,24 @@ class Body extends Component { .then((skid) => { this.setState({kid, skid}); - web3js.shh.subscribe('messages', { - "privateKeyID": kid, - "ttl": 1000, - "minPow": 0.1, - "powTime": 1000 - }, (error, message) => { - console.log(message); + StatusGasRelayer.subscribe(web3js, (error, msgObj) => { + if(error) { + console.error(error); + return; + } - const msg = web3js.utils.toAscii(message.payload); - const msgObj = JSON.parse(msg); - - if(msgObj.message == 'Available'){ + if(msgObj.message == Messages.available){ // found a relayer - console.log("Relayer available: " + message.sig); - + console.log("Relayer available: " + msgObj.sig); let relayers = this.state.relayers; - relayers.push(message.sig); + relayers.push(msgObj.sig); relayers = relayers.filter((value, index, self) => self.indexOf(value) === index); this.setState({relayers}); } - - if(error){ - console.error(error); - } else { - this.setState({message: web3js.utils.toAscii(message.payload)}); - } + + this.setState({message: JSON.stringify(msgObj, null, 2)}); + }, { + privateKeyID: kid }); return true; diff --git a/test-dapp/app/status-gas-relayer.js b/test-dapp/app/status-gas-relayer.js index 934ebbd..7b56667 100644 --- a/test-dapp/app/status-gas-relayer.js +++ b/test-dapp/app/status-gas-relayer.js @@ -19,6 +19,10 @@ export const Actions = { 'Transaction': 'transaction' }; +export const Messages = { + 'available': 'Available' +}; + const relayerSymmmetricKeyID = "0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b"; class StatusGasRelayer { @@ -59,6 +63,36 @@ class StatusGasRelayer { } } + static async subscribe(web3, cb, options) { + options = options || {}; + + if(!options.privateKeyID){ + options.privateKeyID = await web3.shh.newKeyPair(); + // TODO: keypair should be shared between actions and this class. + } + + web3.shh.subscribe('messages', { + "privateKeyID": options.privateKeyID, + "ttl": options.ttl || 1000, + "minPow": options.minPow || 0.1, + "powTime": options.powTime || 1000 + }, (error, message) => { + if(error){ + cb(error); + return; + } + + try { + const msg = web3.utils.toAscii(message.payload); + const msgObj = JSON.parse(msg); + msgObj.sig = message.sig; + cb(false, msgObj); + } catch (err) { + cb(err); + } + }); + } + post = async (options) => { options = options || {};