2018-08-06 17:47:56 +00:00
|
|
|
import React, {Component, Fragment} from 'react';
|
2018-08-07 19:15:00 +00:00
|
|
|
import ApproveAndCallGasRelayed from './approveandcallgasrelayed';
|
2018-08-07 02:30:16 +00:00
|
|
|
import CallGasRelayed from './callgasrelayed';
|
2018-08-06 17:47:56 +00:00
|
|
|
import Divider from '@material-ui/core/Divider';
|
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
import IdentityFactory from 'Embark/contracts/IdentityFactory';
|
|
|
|
import IdentityGasRelay from 'Embark/contracts/IdentityGasRelay';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Status from './status';
|
|
|
|
import Tab from '@material-ui/core/Tab';
|
|
|
|
import Tabs from '@material-ui/core/Tabs';
|
2018-08-07 19:15:00 +00:00
|
|
|
import Web3 from 'web3';
|
2018-08-06 17:47:56 +00:00
|
|
|
import {Typography} from '@material-ui/core';
|
|
|
|
import {withStyles} from '@material-ui/core/styles';
|
2018-08-07 19:15:00 +00:00
|
|
|
import config from '../config';
|
|
|
|
|
2018-08-06 17:47:56 +00:00
|
|
|
|
|
|
|
const styles = {};
|
|
|
|
|
|
|
|
class Body extends Component {
|
|
|
|
|
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
tab: 0,
|
2018-08-07 02:30:16 +00:00
|
|
|
identityAddress: null,
|
2018-08-07 19:15:00 +00:00
|
|
|
nonce: '0',
|
|
|
|
kid: null,
|
|
|
|
skid: null,
|
|
|
|
message: ''
|
2018-08-06 17:47:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
EmbarkJS.onReady(err => {
|
|
|
|
if(err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:00 +00:00
|
|
|
const web3js = new Web3('ws://localhost:8546');
|
|
|
|
|
|
|
|
web3js.shh.newKeyPair()
|
|
|
|
.then((kid) => {
|
|
|
|
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);
|
|
|
|
if(error){
|
|
|
|
console.error(error);
|
|
|
|
} else {
|
|
|
|
this.setState({message: web3js.utils.toAscii(message.payload)});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-06 17:47:56 +00:00
|
|
|
this.setState({
|
2018-08-07 19:15:00 +00:00
|
|
|
web3js,
|
2018-08-06 17:47:56 +00:00
|
|
|
identityAddress: IdentityGasRelay.options.address
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = (event, tab) => {
|
|
|
|
this.setState({tab});
|
|
|
|
};
|
|
|
|
|
2018-08-07 02:30:16 +00:00
|
|
|
updateNonce = (newNonce) => {
|
|
|
|
this.setState({nonce: newNonce});
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:00 +00:00
|
|
|
clearMessages = () => {
|
|
|
|
this.setState({message: ''});
|
|
|
|
}
|
|
|
|
|
2018-08-06 17:47:56 +00:00
|
|
|
newIdentity = (cb) => {
|
|
|
|
let toSend = IdentityFactory.methods['createIdentity()']();
|
|
|
|
toSend.estimateGas()
|
|
|
|
.then(estimatedGas => {
|
2018-08-07 19:15:00 +00:00
|
|
|
console.log("Estimated Gas: " + estimatedGas);
|
2018-08-07 02:30:16 +00:00
|
|
|
return toSend.send({gas: estimatedGas + 1000000});
|
2018-08-06 17:47:56 +00:00
|
|
|
})
|
|
|
|
.then((receipt) => {
|
2018-08-07 02:30:16 +00:00
|
|
|
console.log(receipt);
|
2018-08-06 17:47:56 +00:00
|
|
|
const instance = receipt.events.IdentityCreated.returnValues.instance;
|
|
|
|
this.setState({identityAddress: instance});
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:00 +00:00
|
|
|
randomizeAddress = () => {
|
|
|
|
// TODO:
|
|
|
|
this.setState({identityAddress: "0xC0F1349e154Be9c2eBcc18088AC65d48Fc9ED0FF"});
|
|
|
|
}
|
|
|
|
|
2018-08-06 17:47:56 +00:00
|
|
|
render(){
|
2018-08-07 19:15:00 +00:00
|
|
|
const {tab, identityAddress, nonce, web3js, message, kid, skid} = this.state;
|
2018-08-06 17:47:56 +00:00
|
|
|
|
|
|
|
return <Fragment>
|
|
|
|
<Tabs value={tab} onChange={this.handleChange}>
|
|
|
|
<Tab label="Call" />
|
|
|
|
<Tab label="Approve and Call" />
|
|
|
|
<Tab label="Deploy" />
|
|
|
|
</Tabs>
|
2018-08-07 19:15:00 +00:00
|
|
|
{tab === 0 && <Container><CallGasRelayed clearMessages={this.clearMessages} web3={web3js} kid={kid} skid={skid} nonce={nonce} identityAddress={identityAddress} /></Container>}
|
|
|
|
{tab === 1 && <Container><ApproveAndCallGasRelayed clearMessages={this.clearMessages} web3={web3js} kid={kid} skid={skid} nonce={nonce} identityAddress={identityAddress} /></Container>}
|
2018-08-07 02:30:16 +00:00
|
|
|
{tab === 2 && <Container>Item Three</Container>}
|
2018-08-06 17:47:56 +00:00
|
|
|
<Divider />
|
2018-08-07 02:30:16 +00:00
|
|
|
<Container>
|
2018-08-07 19:15:00 +00:00
|
|
|
<Status message={message} identityCreationFunction={this.newIdentity} randomizeAddress={this.randomizeAddress} nonceUpdateFunction={this.updateNonce} nonce={nonce} identityAddress={identityAddress} />
|
2018-08-07 02:30:16 +00:00
|
|
|
</Container>
|
2018-08-06 17:47:56 +00:00
|
|
|
</Fragment>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 02:30:16 +00:00
|
|
|
function Container(props) {
|
2018-08-06 17:47:56 +00:00
|
|
|
return <Typography component="div" style={{padding: 8 * 3}}>
|
|
|
|
{props.children}
|
|
|
|
</Typography>;
|
|
|
|
}
|
|
|
|
|
2018-08-07 02:30:16 +00:00
|
|
|
Container.propTypes = {
|
2018-08-06 17:47:56 +00:00
|
|
|
children: PropTypes.node.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
Body.propTypes = {
|
|
|
|
classes: PropTypes.object.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withStyles(styles)(Body);
|