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

239 lines
7.3 KiB
JavaScript
Raw Normal View History

import FileSaver from 'file-saver'
import JSzip from 'jszip'
import Keys from './components/Keys'
import KeysManager from './utils/keysManager'
import Loading from './components/Loading'
import React, { Component } from 'react'
import addressGenerator from './utils/addressGenerator'
import getWeb3 from './utils/getWeb3'
import networkAddresses from './utils/addresses'
import swal from 'sweetalert'
import { Footer } from './components/Footer'
import { Header } from './components/Header'
import { constants } from './utils/constants'
import { isTestnet } from './utils/utils'
import './assets/stylesheets/index.css'
2018-10-15 07:52:03 -07:00
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 {
2018-10-15 07:52:03 -07:00
constructor(props) {
super(props)
this.onClick = this.onClick.bind(this)
2018-10-15 07:52:03 -07: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,
isDisabledBtn: props.generateKeysIsDisabled,
isTestnet: false
}
this.keysManager = null
2018-10-15 07:52:03 -07:00
getWeb3()
.then(async web3Config => {
return networkAddresses(web3Config)
2018-02-13 03:14:59 -08:00
})
2018-10-15 07:52:03 -07:00
.then(async config => {
const { web3Config, addresses } = config
this.keysManager = new KeysManager()
2018-10-15 07:52:03 -07:00
await this.keysManager.init({
web3: web3Config.web3Instance,
netId: web3Config.netId,
addresses
})
console.log('culo ' + isTestnet(web3Config.netId))
2018-10-15 07:52:03 -07:00
this.setState({
isTestnet: isTestnet(web3Config.netId),
2018-10-15 07:52:03 -07:00
isDisabledBtn: false,
web3Config
2018-10-16 06:09:49 -07:00
})
2018-10-18 04:24:20 -07:00
})
.catch(error => {
if (error.msg) {
this.setState({ isDisabledBtn: true })
swal({
icon: 'warning',
title: 'Warning',
content: error.node
})
2018-10-18 04:24:20 -07:00
}
})
2017-12-06 23:02:38 -08:00
}
2018-10-15 07:52:03 -07:00
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-10-15 07:52:03 -07: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 {
2018-10-15 07:52:03 -07:00
mining,
voting,
payout
}
}
2018-10-15 07:52:03 -07: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)
2018-01-23 22:23:53 -08:00
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)
2018-01-23 22:23:53 -08:00
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`)
})
2018-01-23 22:23:53 -08:00
}
2017-12-06 23:02:38 -08:00
async onClick() {
this.setState({ loading: true })
const initialKey = this.state.web3Config.defaultAccount
let isValid
2018-02-23 05:31:56 -08:00
try {
isValid = await this.keysManager.isInitialKeyValid(initialKey)
2018-10-15 07:52:03 -07:00
} catch (e) {
isValid = false
2018-02-23 05:31:56 -08:00
}
2018-10-15 07:52:03 -07:00
if (Number(isValid) !== 1) {
this.setState({ loading: false })
const invalidKeyMsg = `The key is an invalid Initial key<br/>
or you're connected to the incorrect chain!<br/>
Please make sure you have loaded correct Initial key in MetaMask.<br/><br/>
2018-02-23 05:13:37 -08:00
<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)
})
return
2017-12-06 23:02:38 -08:00
}
2018-10-15 07:52:03 -07:00
if (Number(isValid) === 1) {
const { mining, voting, payout } = await this.generateKeys()
2017-12-06 23:02:38 -08:00
// add loading screen
2018-10-15 07:52:03 -07:00
await this.keysManager
.createKeys({
mining: mining.jsonStore.address,
voting: voting.jsonStore.address,
payout: payout.jsonStore.address,
sender: initialKey
})
.then(async receipt => {
if (receipt.status === true || receipt.status === '0x1') {
this.setState({ loading: false })
swal('Congratulations!', 'Your keys are generated!', 'success')
2018-10-15 07:52:03 -07:00
await this.generateZip({
mining,
voting,
payout,
netIdName: this.state.web3Config.netIdName
})
2018-10-15 07:52:03 -07:00
} else {
this.setState({ loading: false, keysGenerated: false })
let content = document.createElement('div')
let msg = `Transaction failed`
2018-10-15 07:52:03 -07:00
content.innerHTML = `<div>
2018-02-13 05:23:41 -08:00
Something went wrong!<br/><br/>
Please contact Master Of Ceremony<br/><br/>
${msg}
</div>`
2018-10-15 07:52:03 -07:00
swal({
icon: 'error',
title: 'Error',
2018-10-15 07:52:03 -07:00
content: content
})
2018-10-15 07:52:03 -07:00
}
})
.catch(error => {
this.setState({ loading: false, keysGenerated: false })
let content = document.createElement('div')
let msg
2018-10-15 07:52:03 -07:00
if (error.message.includes(constants.userDeniedTransactionPattern))
msg = `Error: ${constants.userDeniedTransactionPattern}`
else msg = error.message
2018-10-15 07:52:03 -07: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}
</div>`
2018-10-15 07:52:03 -07:00
swal({
icon: 'error',
title: 'Error',
2018-10-15 07:52:03 -07:00
content: content
})
})
2017-12-06 23:02:38 -08:00
}
}
2018-10-15 07:52:03 -07:00
render() {
let loader = this.state.loading ? <Loading netId={this.state.web3Config.netId} /> : ''
2018-10-15 07:52:03 -07: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.
2018-10-15 07:52:03 -07:00
</h2>
<div className="create-keys-button-container">
<button className="create-keys-button" onClick={this.onClick} disabled={this.state.isDisabledBtn}>
2018-10-15 07:52:03 -07:00
Generate keys
</button>
</div>
</div>
)
let content
2018-10-15 07:52:03 -07:00
if (this.state.keysGenerated) {
content = <Keys mining={this.state.mining} voting={this.state.voting} payout={this.state.payout} />
2017-12-06 23:02:38 -08:00
} else {
content = createKeyBtn
2017-12-06 23:02:38 -08:00
}
2017-12-06 23:02:38 -08:00
return (
<div className="lo-App">
<Header isTestnet={this.state.isTestnet} />
2017-12-06 23:02:38 -08:00
{loader}
<section className="lo-App_Content">{content}</section>
<Footer isTestnet={this.state.isTestnet} />
2017-12-06 23:02:38 -08:00
</div>
)
2017-12-06 23:02:38 -08:00
}
}
export default App