import React, { Component } from 'react'; import { withTranslation } from 'react-i18next'; import { Button, Tooltip, Grid, Typography, TextField, ButtonGroup, CircularProgress, IconButton, } from '@mui/material'; import { Link } from 'react-router-dom'; import SmoothImage from 'react-smooth-image'; import { InfoDialog } from './Dialogs'; import SmartToyIcon from '@mui/icons-material/SmartToy'; import CasinoIcon from '@mui/icons-material/Casino'; import ContentCopy from '@mui/icons-material/ContentCopy'; import BoltIcon from '@mui/icons-material/Bolt'; import DownloadIcon from '@mui/icons-material/Download'; import { RoboSatsNoTextIcon } from './Icons'; import { sha256 } from 'js-sha256'; import { genBase62Token, tokenStrength } from '../utils/token'; import { genKey } from '../utils/pgp'; import { getCookie, writeCookie, deleteCookie } from '../utils/cookies'; import { saveAsJson } from '../utils/saveFile'; import { copyToClipboard } from '../utils/clipboard'; import { apiClient } from '../services/api/index'; class UserGenPage extends Component { constructor(props) { super(props); this.state = { openInfo: false, tokenHasChanged: false, token: '', }; this.refCode = this.props.match.params.refCode; } componentDidMount() { // Checks in parent HomePage if there is already a nick and token // Displays the existing one if (this.props.nickname != null) { this.setState({ nickname: this.props.nickname, token: this.props.token ? this.props.token : '', avatarUrl: '/static/assets/avatars/' + this.props.nickname + '.png', loadingRobot: false, }); } else { const newToken = genBase62Token(36); this.setState({ token: newToken, }); this.getGeneratedUser(newToken); } } getGeneratedUser = (token) => { const strength = tokenStrength(token); const refCode = this.refCode; const requestBody = genKey(token).then(function (key) { return { token_sha256: sha256(token), public_key: key.publicKeyArmored, encrypted_private_key: key.encryptedPrivateKeyArmored, unique_values: strength.uniqueValues, counts: strength.counts, length: token.length, ref_code: refCode, }; }); requestBody.then((body) => apiClient.post('/api/user/', body).then((data) => { this.setState({ nickname: data.nickname, bit_entropy: data.token_bits_entropy, avatarUrl: '/static/assets/avatars/' + data.nickname + '.png', shannon_entropy: data.token_shannon_entropy, bad_request: data.bad_request, found: data.found, loadingRobot: false, stealthInvoices: data.wants_stealth, }) & // Add nick and token to App state (token only if not a bad request) (data.bad_request ? this.props.setAppState({ nickname: data.nickname, avatarLoaded: false, activeOrderId: data.active_order_id ? data.active_order_id : null, referralCode: data.referral_code, earnedRewards: data.earned_rewards, lastOrderId: data.last_order_id ? data.last_order_id : null, stealthInvoices: data.wants_stealth, tgEnabled: data.tg_enabled, tgBotName: data.tg_bot_name, tgToken: data.tg_token, }) : this.props.setAppState({ nickname: data.nickname, token, avatarLoaded: false, activeOrderId: data.active_order_id ? data.active_order_id : null, lastOrderId: data.last_order_id ? data.last_order_id : null, referralCode: data.referral_code, earnedRewards: data.earned_rewards, stealthInvoices: data.wants_stealth, }) & writeCookie('robot_token', token) & writeCookie('pub_key', data.public_key.split('\n').join('\\')) & writeCookie('enc_priv_key', data.encrypted_private_key.split('\n').join('\\'))) & // If the robot has been found (recovered) we assume the token is backed up (data.found ? this.props.setAppState({ copiedToken: true }) : null); }), ); }; delGeneratedUser() { apiClient.delete('/api/user'); deleteCookie('sessionid'); deleteCookie('robot_token'); deleteCookie('pub_key'); deleteCookie('enc_priv_key'); } handleClickNewRandomToken = () => { const token = genBase62Token(36); this.setState({ token, tokenHasChanged: true, }); this.props.setAppState({ copiedToken: true }); }; handleChangeToken = (e) => { this.setState({ token: e.target.value.split(' ').join(''), tokenHasChanged: true, }); }; handleClickSubmitToken = () => { this.delGeneratedUser(); this.getGeneratedUser(this.state.token); this.setState({ loadingRobot: true, tokenHasChanged: false }); this.props.setAppState({ avatarLoaded: false, nickname: null, token: null, copiedToken: false, lastOrderId: null, activeOrderId: null, }); }; handleClickOpenInfo = () => { this.setState({ openInfo: true }); }; handleCloseInfo = () => { this.setState({ openInfo: false }); }; createJsonFile = () => { return { token: getCookie('robot_token'), token_shannon_entropy: this.state.shannon_entropy, token_bit_entropy: this.state.bit_entropy, public_key: getCookie('pub_key').split('\\').join('\n'), encrypted_private_key: getCookie('enc_priv_key').split('\\').join('\n'), }; }; render() { const { t, i18n } = this.props; const fontSize = this.props.theme.typography.fontSize; const fontSizeFactor = fontSize / 14; // to scale sizes, default fontSize is 14 return (
{this.props.avatarLoaded && this.state.avatarUrl ? (
{this.state.nickname && getCookie('sessionid') ? ( ) : ( '' )}

) : ( )}
{this.state.found ? ( {this.state.found ? t('A robot avatar was found, welcome back!') : null}
) : ( '' )} { if (e.key === 'Enter') { this.handleClickSubmitToken(); } }} InputProps={{ startAdornment: (
saveAsJson(this.state.nickname + '.json', this.createJsonFile()) } > copyToClipboard(getCookie('robot_token')) & this.props.setAppState({ copiedToken: true }) } >
), endAdornment: ( ), }} />
{this.state.tokenHasChanged ? ( ) : (
)}
{t('Simple and Private LN P2P Exchange')}
); } } export default withTranslation()(UserGenPage);