/** * Copyright (c) 2018 - present Zilliqa Research Pte. Ltd. * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import React, { useState, useEffect } from 'react'; import { Card, Label, Input, FormGroup, Form, Row, Col, FormFeedback } from 'reactstrap'; import Steps, { Step } from 'rc-steps'; import Button from '../button'; import Spinner from '../spinner'; import { getInputValidationState, downloadObjectAsJson } from '../../utils'; import { requestStatus } from '../../constants'; import Disclaimer from '../disclaimer'; const FIRST_STEP = 0; const SECOND_STEP = 1; const FINAL_STEP = 2; const GenerateForm: React.FunctionComponent = () => { const [currentStep, setCurrentStep] = useState(FIRST_STEP); const [privateKey, setPrivateKey] = useState(); const [passphrase, setPassphrase] = useState(''); return (
{currentStep === FIRST_STEP ? ( ) : currentStep === SECOND_STEP ? ( ) : currentStep === FINAL_STEP ? ( ) : null}
); }; const PassphraseStep = ({ passphrase, setPassphrase, setCurrentStep }) => { const [passphraseValid, setPassphraseValid] = useState(false); const [passphraseInvalid, setPassphraseInvalid] = useState(false); const [isDisclaimerChecked, setIsDisclaimerChecked] = useState(false); const handleCheck = () => { setIsDisclaimerChecked(!isDisclaimerChecked); }; const changePassphrase = (e: React.ChangeEvent): void => { e.preventDefault(); const value = e.target.value; const key = 'passphrase'; const validationResult: any = getInputValidationState(key, value, /^.{8,}$/); setPassphrase(value); setPassphraseValid(validationResult.passphraseValid); setPassphraseInvalid(validationResult.passphraseInvalid); }; const isDisabled = !passphraseValid || !isDisclaimerChecked; return (

{'Set Passphrase for your Keystore File'}

{`Please set the password for the keystore file for your new wallet.`}

e.preventDefault()}> {'invalid passphrase'} {'valid passphrase'}
); }; const KeystoreStep = ({ setCurrentStep, setPrivateKey, privateKey, passphrase }) => { const [worker, setWorker] = useState(); const [encryptStatus, setEncryptStatus] = useState(requestStatus.INITIAL); const [keystoreJSON, setKeystoreJSON] = useState(''); const downloadKeystore = () => { if (keystoreJSON === '') { return; } const keystoreObject = JSON.parse(keystoreJSON); const filename = `zilliqa_keystore_${new Date().toISOString()}`; setCurrentStep(FINAL_STEP); setEncryptStatus(requestStatus.SUCCEEDED); downloadObjectAsJson(keystoreObject, filename); }; useEffect(() => { if (privateKey && keystoreJSON) { downloadKeystore(); } }, [privateKey, keystoreJSON]); useEffect(() => { if (worker === undefined) { const myWorker = new Worker('./encrypt.worker', { type: 'module' }); myWorker.onmessage = (event) => { const { data } = event; if (data.keystoreJSON === undefined || data.privateKey === undefined) { return setEncryptStatus(requestStatus.FAILED); } setKeystoreJSON(data.keystoreJSON); setPrivateKey(data.privateKey); }; setWorker(myWorker as any); } }); const generateKeystore = () => { setEncryptStatus(requestStatus.PENDING); //@ts-ignore worker.postMessage({ passphrase }); }; const isPending = encryptStatus === requestStatus.PENDING; const buttonText = isPending ? 'Generating Keystore File' : 'Generate Keystore File'; return (

{'Generate Keystore File'}

{`The password for your keystore file for a new wallet has been set. Please click the tab below to generate your keystore to setup your wallet and move on to the last step.`}

); }; const FinalStep = ({ privateKey }) => { return (

{'Please Save Your Private Key'}

{`Your new wallet has been created.`}
{`Make sure to copy the private key below and save it.`}

Private Key

{privateKey}

); }; export default GenerateForm;