From e78b5e9c8bd884061209eee89eff88bf21a5d43e Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Mon, 10 Oct 2022 12:40:22 +0000 Subject: [PATCH] Persist Data on Android (#274) * Android Cookies * Android Fix POST headers * Format * App & Cookies Working * Fix token on UserGen --- frontend/src/components/App.js | 11 +++- frontend/src/components/BottomBar.js | 9 ++- frontend/src/components/Dialogs/Profile.tsx | 7 +-- .../src/components/Dialogs/StoreToken.tsx | 4 +- frontend/src/components/EncryptedChat.js | 7 +-- frontend/src/components/MakerPage.js | 5 +- frontend/src/components/OrderPage.js | 5 +- frontend/src/components/TradeBox.js | 1 - frontend/src/components/UserGenPage.js | 58 ++++++++++--------- frontend/src/services/Native/index.d.ts | 5 +- frontend/src/services/Native/index.ts | 19 ++++-- .../System/SystemNativeClient/index.ts | 36 +++++++++++- .../services/System/SystemWebClient/index.ts | 27 +++++++++ frontend/src/services/System/index.ts | 4 ++ .../src/services/api/ApiNativeClient/index.ts | 46 +++++++++++---- .../src/services/api/ApiWebClient/index.ts | 7 ++- frontend/src/utils/cookies.js | 23 -------- mobile/App.tsx | 55 ++++++++++++++++-- mobile/package-lock.json | 16 +++++ mobile/package.json | 1 + mobile/services/Tor/index.ts | 20 ++----- 21 files changed, 247 insertions(+), 119 deletions(-) delete mode 100644 frontend/src/utils/cookies.js diff --git a/frontend/src/components/App.js b/frontend/src/components/App.js index 853cbbc9..bb524e7e 100644 --- a/frontend/src/components/App.js +++ b/frontend/src/components/App.js @@ -134,6 +134,13 @@ export default class App extends Component { } } -const root = ReactDOM.createRoot(document.getElementById('app')); +const loadApp = () => { + if (systemClient.loading) { + setTimeout(loadApp, 200); + } else { + const root = ReactDOM.createRoot(document.getElementById('app')); + root.render(); + } +}; -root.render(); +loadApp(); diff --git a/frontend/src/components/BottomBar.js b/frontend/src/components/BottomBar.js index 6f12b3b7..c1dbb2b0 100644 --- a/frontend/src/components/BottomBar.js +++ b/frontend/src/components/BottomBar.js @@ -19,6 +19,7 @@ import MediaQuery from 'react-responsive'; import Flags from 'country-flag-icons/react/3x2'; import { Link as LinkRouter } from 'react-router-dom'; import { apiClient } from '../services/api'; +import { systemClient } from '../services/System'; import RobotAvatar from './Robots/RobotAvatar'; // Icons @@ -41,7 +42,6 @@ import { UpdateClientDialog, } from './Dialogs'; -import { getCookie } from '../utils/cookies'; import checkVer from '../utils/checkVer'; class BottomBar extends Component { @@ -172,9 +172,8 @@ class BottomBar extends Component { showProfileButton = () => { return ( this.props.avatarLoaded && - (window.NativeRobosats || - ((this.props.token ? getCookie('robot_token') === this.props.token : true) && - getCookie('sessionid'))) + (this.props.token ? systemClient.getCookie('robot_token') === this.props.token : true) && + systemClient.getCookie('sessionid') ); }; @@ -467,7 +466,7 @@ class BottomBar extends Component { handleClickOpenExchangeSummary = () => { // avoid calling getInfo while sessionid not yet set. Temporary fix. - if (getCookie('sessionid')) { + if (systemClient.getCookie('sessionid')) { this.getInfo(); } this.setState({ openExchangeSummary: true }); diff --git a/frontend/src/components/Dialogs/Profile.tsx b/frontend/src/components/Dialogs/Profile.tsx index 95a57aa3..0495303f 100644 --- a/frontend/src/components/Dialogs/Profile.tsx +++ b/frontend/src/components/Dialogs/Profile.tsx @@ -36,7 +36,6 @@ import PersonAddAltIcon from '@mui/icons-material/PersonAddAlt'; import EmojiEventsIcon from '@mui/icons-material/EmojiEvents'; import { UserNinjaIcon, BitcoinIcon } from '../Icons'; -import { getCookie } from '../../utils/cookies'; import { systemClient } from '../../services/System'; import { getWebln } from '../../utils/webln'; import RobotAvatar from '../Robots/RobotAvatar'; @@ -98,7 +97,7 @@ const ProfileDialog = ({ }, [showRewards]); const copyTokenHandler = () => { - const robotToken = getCookie('robot_token'); + const robotToken = systemClient.getCookie('robot_token'); if (robotToken) { systemClient.copyToClipboard(robotToken); @@ -226,12 +225,12 @@ const ProfileDialog = ({ - {getCookie('robot_token') ? ( + {systemClient.getCookie('robot_token') ? ( - {getCookie('robot_token') ? ( + {systemClient.getCookie('robot_token') ? ( this.setState({ openStoreToken: false })} onClickCopy={() => - systemClient.copyToClipboard(getCookie('robot_token')) & + systemClient.copyToClipboard(systemClient.getCookie('robot_token')) & this.props.setAppState({ copiedToken: true }) } copyIconColor={this.props.copiedToken ? 'inherit' : 'primary'} diff --git a/frontend/src/components/OrderPage.js b/frontend/src/components/OrderPage.js index f7a73147..7b1eb407 100644 --- a/frontend/src/components/OrderPage.js +++ b/frontend/src/components/OrderPage.js @@ -50,7 +50,6 @@ import HourglassTopIcon from '@mui/icons-material/HourglassTop'; import CheckIcon from '@mui/icons-material/Check'; import { SendReceiveIcon } from './Icons'; -import { getCookie } from '../utils/cookies'; import { pn } from '../utils/prettyNumbers'; import { systemClient } from '../services/System'; import { getWebln } from '../utils/webln'; @@ -522,12 +521,12 @@ class OrderPage extends Component { }; tokenDialog = () => { - return getCookie('robot_token') ? ( + return systemClient.getCookie('robot_token') ? ( this.setState({ openStoreToken: false })} onClickCopy={() => - systemClient.copyToClipboard(getCookie('robot_token')) & + systemClient.copyToClipboard(systemClient.getCookie('robot_token')) & this.props.setAppState({ copiedToken: true }) } copyIconColor={this.props.copiedToken ? 'inherit' : 'primary'} diff --git a/frontend/src/components/TradeBox.js b/frontend/src/components/TradeBox.js index de378a43..ad3f3f0c 100644 --- a/frontend/src/components/TradeBox.js +++ b/frontend/src/components/TradeBox.js @@ -52,7 +52,6 @@ import RocketLaunchIcon from '@mui/icons-material/RocketLaunch'; import RefreshIcon from '@mui/icons-material/Refresh'; import { NewTabIcon } from './Icons'; -import { getCookie } from '../utils/cookies'; import { pn } from '../utils/prettyNumbers'; class TradeBox extends Component { diff --git a/frontend/src/components/UserGenPage.js b/frontend/src/components/UserGenPage.js index 9cc6fcdf..187f0876 100644 --- a/frontend/src/components/UserGenPage.js +++ b/frontend/src/components/UserGenPage.js @@ -24,7 +24,6 @@ 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 { systemClient } from '../services/System'; import { apiClient } from '../services/api/index'; @@ -47,10 +46,13 @@ class UserGenPage extends Component { // Displays the existing one if (this.props.nickname != null) { this.setState({ - nickname: this.props.nickname, token: this.props.token ? this.props.token : '', loadingRobot: false, }); + } else if (window.NativeRobosats && systemClient.getCookie('robot_token')) { + const token = systemClient.getCookie('robot_token'); + this.props.setAppState({ token }); + this.setState({ token, loadingRobot: false }); } else { const newToken = genBase62Token(36); this.setState({ @@ -78,7 +80,6 @@ class UserGenPage extends Component { requestBody.then((body) => apiClient.post('/api/user/', body).then((data) => { this.setState({ - nickname: data.nickname, bit_entropy: data.token_bits_entropy, shannon_entropy: data.token_shannon_entropy, bad_request: data.bad_request, @@ -110,9 +111,12 @@ class UserGenPage extends Component { tgBotName: data.tg_bot_name, tgToken: data.tg_token, }) & - writeCookie('robot_token', token) & - writeCookie('pub_key', data.public_key.split('\n').join('\\')) & - writeCookie('enc_priv_key', data.encrypted_private_key.split('\n').join('\\'))) & + systemClient.setCookie('robot_token', token) & + systemClient.setCookie('pub_key', data.public_key.split('\n').join('\\')) & + systemClient.setCookie( + '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); }), @@ -122,10 +126,10 @@ class UserGenPage extends Component { delGeneratedUser() { apiClient.delete('/api/user'); - deleteCookie('sessionid'); - deleteCookie('robot_token'); - deleteCookie('pub_key'); - deleteCookie('enc_priv_key'); + systemClient.deleteCookie('sessionid'); + systemClient.deleteCookie('robot_token'); + systemClient.deleteCookie('pub_key'); + systemClient.deleteCookie('enc_priv_key'); } handleClickNewRandomToken = () => { @@ -168,11 +172,11 @@ class UserGenPage extends Component { createJsonFile = () => { return { - token: getCookie('robot_token'), + token: systemClient.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'), + public_key: systemClient.getCookie('pub_key').split('\\').join('\n'), + encrypted_private_key: systemClient.getCookie('enc_priv_key').split('\\').join('\n'), }; }; @@ -191,12 +195,12 @@ class UserGenPage extends Component { align='center' sx={{ width: 370 * fontSizeFactor, height: 260 * fontSizeFactor }} > - {this.props.avatarLoaded && this.state.nickname ? ( + {this.props.avatarLoaded && this.props.nickname ? (
- {this.state.nickname && getCookie('sessionid') ? ( + {this.props.nickname && systemClient.getCookie('sessionid') ? (
- {this.state.nickname} + {this.props.nickname} - saveAsJson(this.state.nickname + '.json', this.createJsonFile()) + saveAsJson(this.props.nickname + '.json', this.createJsonFile()) } > - systemClient.copyToClipboard(getCookie('robot_token')) & + systemClient.copyToClipboard(systemClient.getCookie('robot_token')) & this.props.setAppState({ copiedToken: true }) } > @@ -374,8 +376,9 @@ class UserGenPage extends Component {