poa-dapps-keys-generation/src/App.js

209 lines
7.0 KiB
JavaScript
Raw Normal View History

2017-12-06 23:02:38 -08:00
import React, { Component } from 'react';
import getWeb3 from './getWeb3'
import KeysManager from './keysManager';
import Keys from './Keys';
import swal from 'sweetalert';
import './index/index.css';
import addressGenerator from './addressGenerator'
import JSzip from 'jszip';
import FileSaver from 'file-saver';
import { constants } from './constants';
2018-01-30 18:06:34 -08:00
import networkAddresses from './addresses';
2018-02-23 07:45:12 -08:00
import Header from './Header';
2018-02-23 04:20:02 -08:00
import Footer from './Footer';
2018-02-13 03:14:59 -08:00
import Loading from './Loading';
function generateElement(msg){
let errorNode = document.createElement("div");
2018-02-23 05:13:37 -08:00
errorNode.innerHTML = `<div style="line-height: 1.6;">
${msg}
</div>`;
return errorNode;
}
2017-12-06 23:02:38 -08:00
class App extends Component {
constructor(props){
super(props);
this.onClick = this.onClick.bind(this);
2018-01-23 22:23:53 -08:00
this.saveFile = (blob) => {
FileSaver.saveAs(blob, `poa_network_validator_keys.zip`);
};
2017-12-06 23:02:38 -08:00
this.state = {
web3Config: {},
2017-12-06 23:11:01 -08:00
mining: null,
2018-02-13 03:42:17 -08:00
isDisabledBtn: props.generateKeysIsDisabled
2017-12-06 23:02:38 -08:00
}
this.keysManager = null;
2018-01-29 11:39:28 -08:00
getWeb3().then(async (web3Config) => {
2018-01-30 18:06:34 -08:00
return networkAddresses(web3Config)
}).then(async (config) => {
const {web3Config, addresses} = config;
this.keysManager = new KeysManager();
2018-01-29 11:39:28 -08:00
await this.keysManager.init({
web3: web3Config.web3Instance,
2018-01-30 18:06:34 -08:00
netId: web3Config.netId,
addresses,
2017-12-06 23:02:38 -08:00
});
2018-02-13 03:14:59 -08:00
this.setState({
isDisabledBtn: false,
web3Config
})
2017-12-06 23:02:38 -08:00
}).catch((error) => {
if(error.msg){
2017-12-06 23:11:01 -08:00
this.setState({isDisabledBtn: true});
2017-12-06 23:02:38 -08:00
swal({
icon: 'warning',
title: 'Warning',
content: error.node
});
}
})
}
componentDidMount(){
if(window.location.hash.indexOf('just-generate-keys') !== -1) {
this.setState({loading:true});
2018-01-23 22:23:53 -08:00
setTimeout(async () => {
const {mining, voting, payout} = await this.generateKeys();
this.setState({loading:false});
2018-01-23 22:23:53 -08:00
await this.generateZip({mining, voting, payout, netIdName: "manualCreation"});
}, 150)
}
}
2018-01-23 22:23:53 -08:00
async generateKeys(cb) {
const mining = await addressGenerator();
const voting = await addressGenerator();
const payout = await addressGenerator();
this.setState({
mining,
voting,
payout,
keysGenerated: true
})
return {
mining, voting, payout
}
}
2018-01-23 22:23:53 -08:00
async generateZip({mining, voting, payout, netIdName}){
const zip = new JSzip();
zip.file(`${netIdName}_keys/mining_key_${mining.jsonStore.address}.json`, JSON.stringify(mining.jsonStore));
zip.file(`${netIdName}_keys/mining_password_${mining.jsonStore.address}.txt`, mining.password);
zip.file(`${netIdName}_keys/voting_key_${voting.jsonStore.address}.json`, JSON.stringify(voting.jsonStore));
zip.file(`${netIdName}_keys/voting_password_${voting.jsonStore.address}.txt`, voting.password);
zip.file(`${netIdName}_keys/payout_key_${payout.jsonStore.address}.json`, JSON.stringify(payout.jsonStore));
zip.file(`${netIdName}_keys/payout_password_${payout.jsonStore.address}.txt`, payout.password);
zip.generateAsync({type:"blob"}).then((blob) => {
FileSaver.saveAs(blob, `poa_network_validator_keys.zip`);
});
}
2017-12-06 23:02:38 -08:00
async onClick() {
this.setState({loading:true});
const initialKey = window.web3.eth.defaultAccount;
2018-02-23 05:31:56 -08:00
let isValid
try {
isValid = await this.keysManager.isInitialKeyValid(initialKey);
} catch(e) {
isValid = false;
}
2017-12-06 23:02:38 -08:00
console.log(isValid);
if(Number(isValid) !== 1){
this.setState({loading:false});
2018-02-23 05:13:37 -08:00
const invalidKeyMsg = `The key is invalid initial Key<br/>
or you're connect to incorrect chain!<br/>
Please make sure you have loaded correct initial key in Metamask.<br/><br/>
<b>Your current selected key is</b> <i>${initialKey}</i><br/>
<b>Current Network ID</b> is <i>${this.state.web3Config.netId}</i>`
swal({
icon: 'error',
title: 'Error',
content: generateElement(invalidKeyMsg)
})
2017-12-06 23:02:38 -08:00
return;
}
if(Number(isValid) === 1){
const {mining, voting, payout} = await this.generateKeys()
2017-12-06 23:02:38 -08:00
// add loading screen
await this.keysManager.createKeys({
mining: mining.jsonStore.address,
voting: voting.jsonStore.address,
payout: payout.jsonStore.address,
sender: initialKey
2018-01-23 22:23:53 -08:00
}).then(async (receipt) => {
2017-12-06 23:02:38 -08:00
console.log(receipt);
2018-07-05 05:22:32 -07:00
if (receipt.status === true || receipt.status === "0x1") {
2018-02-13 05:23:41 -08:00
this.setState({loading: false})
swal("Congratulations!", "Your keys are generated!", "success");
await this.generateZip({mining, voting, payout, netIdName: this.state.web3Config.netIdName});
} else {
this.setState({loading: false, keysGenerated: false})
2018-02-13 05:25:50 -08:00
let content = document.createElement("div");
2018-02-13 05:23:41 -08:00
let msg = `Transaction failed`;
content.innerHTML = `<div>
Something went wrong!<br/><br/>
Please contact Master Of Ceremony<br/><br/>
${msg}
</div>`;
swal({
icon: 'error',
title: 'Error',
content: content
});
}
2017-12-06 23:02:38 -08:00
}).catch((error) => {
console.error(error.message);
this.setState({loading: false, keysGenerated: false})
2018-02-13 05:25:50 -08:00
let content = document.createElement("div");
let msg;
if (error.message.includes(constants.userDeniedTransactionPattern))
msg = `Error: User ${constants.userDeniedTransactionPattern}`
else
msg = error.message
2017-12-06 23:02:38 -08:00
content.innerHTML = `<div>
2018-01-04 16:58:06 -08:00
Something went wrong!<br/><br/>
Please contact Master Of Ceremony<br/><br/>
${msg}
2017-12-06 23:02:38 -08:00
</div>`;
swal({
icon: 'error',
title: 'Error',
content: content
});
})
}
}
render() {
2018-02-23 07:45:12 -08:00
let loader = this.state.loading ? <Loading netId={this.state.web3Config.netId}/> : '';
2017-12-06 23:02:38 -08:00
let createKeyBtn = (<div className="create-keys">
<h1>Create keys from initial key</h1>
<h2>
In this application, you will create mining, payout and voting keys.
The app will make your initial key unusable after the process.
Please proceed with care, don't lose your keys and follow instructions.
</h2>
<div className="create-keys-button-container">
2017-12-06 23:11:01 -08:00
<button className="create-keys-button" onClick={this.onClick} disabled={this.state.isDisabledBtn}>Generate keys</button>
2017-12-06 23:02:38 -08:00
</div>
</div>)
let content;
if(this.state.keysGenerated){
content = <Keys mining={this.state.mining} voting={this.state.voting} payout={this.state.payout}/>
} else {
content = createKeyBtn
}
return (
<div className="App">
2018-02-23 07:45:12 -08:00
<Header netId={this.state.web3Config.netId}/>
2017-12-06 23:02:38 -08:00
{loader}
2018-02-23 04:20:02 -08:00
<section className="content">
2017-12-06 23:02:38 -08:00
{content}
2018-02-23 04:20:02 -08:00
</section>
2018-02-23 07:45:12 -08:00
<Footer netId={this.state.web3Config.netId}/>
2017-12-06 23:02:38 -08:00
</div>
);
}
}
export default App;