Merge pull request #3 from status-im/js-library

Simplifying subscriptions
This commit is contained in:
Richard Ramos 2018-09-12 11:57:54 -04:00 committed by GitHub
commit d4a67355dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 43 deletions

View File

@ -7,7 +7,6 @@ import CardContent from '@material-ui/core/CardContent';
import CardHeader from '@material-ui/core/CardHeader'; import CardHeader from '@material-ui/core/CardHeader';
import EmbarkJS from 'Embark/EmbarkJS'; import EmbarkJS from 'Embark/EmbarkJS';
import Grid from '@material-ui/core/Grid'; import Grid from '@material-ui/core/Grid';
import IdentityGasRelay from 'Embark/contracts/IdentityGasRelay';
import MySnackbarContentWrapper from './snackbar'; import MySnackbarContentWrapper from './snackbar';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import STT from 'Embark/contracts/STT'; import STT from 'Embark/contracts/STT';
@ -71,8 +70,6 @@ class ApproveAndCallGasRelayed extends Component {
msgSent: false, msgSent: false,
transactionError: '' transactionError: ''
}); });
IdentityGasRelay.options.address = this.props.identityAddress;
try { try {
const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount) const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount)

View File

@ -1,4 +1,5 @@
import React, {Component, Fragment} from 'react'; import React, {Component, Fragment} from 'react';
import StatusGasRelayer, {Messages} from '../status-gas-relayer';
import ApproveAndCallGasRelayed from './approveandcallgasrelayed'; import ApproveAndCallGasRelayed from './approveandcallgasrelayed';
import CallGasRelayed from './callgasrelayed'; import CallGasRelayed from './callgasrelayed';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
@ -46,32 +47,25 @@ class Body extends Component {
web3js.shh.addSymKey(config.relaySymKey) web3js.shh.addSymKey(config.relaySymKey)
.then((skid) => { .then((skid) => {
this.setState({kid, 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); StatusGasRelayer.subscribe(web3js, (error, msgObj) => {
const msgObj = JSON.parse(msg); if(error) {
console.error(error);
return;
}
if(msgObj.message == 'Available'){ if(msgObj.message == Messages.available){
// found a relayer // found a relayer
console.log("Relayer available: " + message.sig); console.log("Relayer available: " + msgObj.sig);
let relayers = this.state.relayers; let relayers = this.state.relayers;
relayers.push(message.sig); relayers.push(msgObj.sig);
relayers = relayers.filter((value, index, self) => self.indexOf(value) === index); relayers = relayers.filter((value, index, self) => self.indexOf(value) === index);
this.setState({relayers}); this.setState({relayers});
} }
if(error){ this.setState({message: JSON.stringify(msgObj, null, 2)});
console.error(error); }, {
} else { privateKeyID: kid
this.setState({message: msg});
}
}); });
return true; return true;

View File

@ -1,13 +1,14 @@
import React, {Component, Fragment} from 'react'; import React, {Component, Fragment} from 'react';
import StatusGasRelayer, {Messages} from '../status-gas-relayer';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
import EmbarkJS from 'Embark/EmbarkJS'; import EmbarkJS from 'Embark/EmbarkJS';
import STT from 'Embark/contracts/STT'; import Execute from './execute';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import STT from 'Embark/contracts/STT';
import Status from './status-sntcontroller'; import Status from './status-sntcontroller';
import Tab from '@material-ui/core/Tab'; import Tab from '@material-ui/core/Tab';
import Tabs from '@material-ui/core/Tabs'; import Tabs from '@material-ui/core/Tabs';
import TransferSNT from './transfersnt'; import TransferSNT from './transfersnt';
import Execute from './execute';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import Web3 from 'web3'; import Web3 from 'web3';
import config from '../config'; import config from '../config';
@ -48,32 +49,24 @@ class Body extends Component {
.then((skid) => { .then((skid) => {
this.setState({kid, skid}); this.setState({kid, skid});
web3js.shh.subscribe('messages', { StatusGasRelayer.subscribe(web3js, (error, msgObj) => {
"privateKeyID": kid, if(error) {
"ttl": 1000, console.error(error);
"minPow": 0.1, return;
"powTime": 1000 }
}, (error, message) => {
console.log(message);
const msg = web3js.utils.toAscii(message.payload); if(msgObj.message == Messages.available){
const msgObj = JSON.parse(msg);
if(msgObj.message == 'Available'){
// found a relayer // found a relayer
console.log("Relayer available: " + message.sig); console.log("Relayer available: " + msgObj.sig);
let relayers = this.state.relayers; let relayers = this.state.relayers;
relayers.push(message.sig); relayers.push(msgObj.sig);
relayers = relayers.filter((value, index, self) => self.indexOf(value) === index); relayers = relayers.filter((value, index, self) => self.indexOf(value) === index);
this.setState({relayers}); this.setState({relayers});
} }
if(error){ this.setState({message: JSON.stringify(msgObj, null, 2)});
console.error(error); }, {
} else { privateKeyID: kid
this.setState({message: web3js.utils.toAscii(message.payload)});
}
}); });
return true; return true;

View File

@ -19,6 +19,10 @@ export const Actions = {
'Transaction': 'transaction' 'Transaction': 'transaction'
}; };
export const Messages = {
'available': 'Available'
};
const relayerSymmmetricKeyID = "0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b"; const relayerSymmmetricKeyID = "0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b";
class StatusGasRelayer { 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) => { post = async (options) => {
options = options || {}; options = options || {};