Merge pull request #3 from status-im/js-library
Simplifying subscriptions
This commit is contained in:
commit
d4a67355dd
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 || {};
|
||||
|
||||
|
|
Loading…
Reference in New Issue