2018-08-06 17:47:56 +00:00
|
|
|
import React, {Component, Fragment} from 'react';
|
|
|
|
import Divider from '@material-ui/core/Divider';
|
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
2018-08-10 18:25:39 +00:00
|
|
|
import STT from 'Embark/contracts/STT';
|
2018-08-06 17:47:56 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-08-09 21:23:32 +00:00
|
|
|
import Status from './status-sntcontroller';
|
2018-08-06 17:47:56 +00:00
|
|
|
import Tab from '@material-ui/core/Tab';
|
|
|
|
import Tabs from '@material-ui/core/Tabs';
|
2018-08-09 21:23:32 +00:00
|
|
|
import TransferSNT from './transfersnt';
|
|
|
|
import Typography from '@material-ui/core/Typography';
|
2018-08-07 19:15:00 +00:00
|
|
|
import Web3 from 'web3';
|
|
|
|
import config from '../config';
|
2018-08-09 21:23:32 +00:00
|
|
|
import web3 from 'Embark/web3';
|
|
|
|
import {withStyles} from '@material-ui/core/styles';
|
2018-08-07 19:15:00 +00:00
|
|
|
|
2018-08-10 18:25:39 +00:00
|
|
|
window.STT = STT;
|
2018-08-06 17:47:56 +00:00
|
|
|
|
|
|
|
const styles = {};
|
|
|
|
|
|
|
|
class Body extends Component {
|
|
|
|
|
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
tab: 0,
|
2018-08-09 21:23:32 +00:00
|
|
|
walletAddress: 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-09 21:23:32 +00:00
|
|
|
web3js
|
2018-08-06 17:47:56 +00:00
|
|
|
});
|
2018-08-09 21:23:32 +00:00
|
|
|
|
|
|
|
web3.eth.getAccounts()
|
|
|
|
.then(accounts => {
|
|
|
|
this.setState({walletAddress: accounts[2]});
|
|
|
|
});
|
|
|
|
|
2018-08-06 17:47:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
render(){
|
2018-08-09 21:23:32 +00:00
|
|
|
const {tab, walletAddress, nonce, web3js, message, kid, skid} = this.state;
|
2018-08-06 17:47:56 +00:00
|
|
|
|
|
|
|
return <Fragment>
|
|
|
|
<Tabs value={tab} onChange={this.handleChange}>
|
2018-08-09 21:23:32 +00:00
|
|
|
<Tab label="Transfer SNT" />
|
|
|
|
<Tab label="????" />
|
2018-08-06 17:47:56 +00:00
|
|
|
</Tabs>
|
2018-08-09 21:23:32 +00:00
|
|
|
{tab === 0 && <Container><TransferSNT clearMessages={this.clearMessages} web3={web3js} kid={kid} skid={skid} nonce={nonce} /></Container>}
|
|
|
|
{tab === 1 && <Container>TODO</Container>}
|
2018-08-06 17:47:56 +00:00
|
|
|
<Divider />
|
2018-08-07 02:30:16 +00:00
|
|
|
<Container>
|
2018-08-09 21:23:32 +00:00
|
|
|
<Status message={message} nonceUpdateFunction={this.updateNonce} nonce={nonce} walletAddress={walletAddress} />
|
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);
|