From 8d8e3a56887a9265882da8af7fbc25fc304b1efb Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Tue, 14 Nov 2023 13:24:45 +0000 Subject: [PATCH] Fix robot recovery (#945) * Fix Robot recovery * Change testralia onion --- frontend/src/basic/RobotPage/Recovery.tsx | 6 ++-- frontend/src/basic/RobotPage/RobotProfile.tsx | 2 +- frontend/src/basic/RobotPage/index.tsx | 28 ++++++++++++++----- frontend/src/models/Coordinator.model.ts | 25 ++++++++++------- frontend/src/models/Federation.model.ts | 11 ++++---- frontend/src/models/Garage.model.ts | 18 ++++++------ frontend/static/federation.json | 6 ++-- nodeapp/robosats-client.sh | 2 +- 8 files changed, 59 insertions(+), 39 deletions(-) diff --git a/frontend/src/basic/RobotPage/Recovery.tsx b/frontend/src/basic/RobotPage/Recovery.tsx index 4503ddf7..8fde204e 100644 --- a/frontend/src/basic/RobotPage/Recovery.tsx +++ b/frontend/src/basic/RobotPage/Recovery.tsx @@ -9,7 +9,7 @@ interface RecoveryProps { inputToken: string; badToken: string; setInputToken: (state: string) => void; - getGenerateRobot: (token: string) => void; + getRecoverRobot: (token: string) => void; } const Recovery = ({ @@ -17,12 +17,12 @@ const Recovery = ({ badToken, setView, setInputToken, - getGenerateRobot, + getRecoverRobot, }: RecoveryProps): JSX.Element => { const { t } = useTranslation(); const onClickRecover = (): void => { - getGenerateRobot(inputToken); + getRecoverRobot(inputToken); setView('profile'); }; diff --git a/frontend/src/basic/RobotPage/RobotProfile.tsx b/frontend/src/basic/RobotPage/RobotProfile.tsx index 68166169..e14344b9 100644 --- a/frontend/src/basic/RobotPage/RobotProfile.tsx +++ b/frontend/src/basic/RobotPage/RobotProfile.tsx @@ -58,7 +58,7 @@ const RobotProfile = ({ if (garage.getRobot().nickname != null && garage.getRobot().avatarLoaded) { setLoading(false); } - }, [robotUpdatedAt]); + }, [robotUpdatedAt, loading]); const handleAddRobot = (): void => { getGenerateRobot(genBase62Token(36)); diff --git a/frontend/src/basic/RobotPage/index.tsx b/frontend/src/basic/RobotPage/index.tsx index 1b69e26a..913cfdcd 100644 --- a/frontend/src/basic/RobotPage/index.tsx +++ b/frontend/src/basic/RobotPage/index.tsx @@ -48,10 +48,8 @@ const RobotPage = ({ avatarBaseUrl }: RobotPageProps): JSX.Element => { const token = urlToken ?? garage.getRobot().token; if (token !== undefined) { setInputToken(token); - } - if (garage.getRobot().nickname !== undefined && token !== undefined) { if (window.NativeRobosats === undefined || torStatus === '"Done"') { - getGenerateRobot(token); + getRecoverRobot(token); setView('profile'); } } @@ -67,16 +65,32 @@ const RobotPage = ({ avatarBaseUrl }: RobotPageProps): JSX.Element => { } }, [inputToken]); - const getGenerateRobot = (token: string): void => { + const getRecoverRobot = (token: string): void => { setInputToken(token); genKey(token) .then((key) => { - const slot = garage.createRobot({ + garage.updateRobot({ token, pubKey: key.publicKeyArmored, encPrivKey: key.encryptedPrivateKeyArmored, }); - void federation.fetchRobot(garage, slot); + void federation.fetchRobot(garage, garage.currentSlot); + }) + .catch((error) => { + console.error('Error:', error); + }); + }; + + const getGenerateRobot = (token: string): void => { + setInputToken(token); + genKey(token) + .then((key) => { + garage.createRobot({ + token, + pubKey: key.publicKeyArmored, + encPrivKey: key.encryptedPrivateKeyArmored, + }); + void federation.fetchRobot(garage, garage.currentSlot); }) .catch((error) => { console.error('Error:', error); @@ -180,7 +194,7 @@ const RobotPage = ({ avatarBaseUrl }: RobotPageProps): JSX.Element => { badToken={badToken} inputToken={inputToken} setInputToken={setInputToken} - getGenerateRobot={getGenerateRobot} + getRecoverRobot={getRecoverRobot} /> ) : null} diff --git a/frontend/src/models/Coordinator.model.ts b/frontend/src/models/Coordinator.model.ts index c02bb561..69300382 100644 --- a/frontend/src/models/Coordinator.model.ts +++ b/frontend/src/models/Coordinator.model.ts @@ -297,16 +297,21 @@ export class Coordinator { console.log(e); }); - garage.updateRobot( - { - ...newAttributes, - tokenSHA256: authHeaders.tokenSHA256, - loading: false, - bitsEntropy, - shannonEntropy, - }, - index, - ); + if ( + newAttributes?.activeOrderId !== null || + (garage.getRobot(index).activeOrderId === null && newAttributes?.lastOrderId !== null) + ) { + garage.updateRobot( + { + ...newAttributes, + tokenSHA256: authHeaders.tokenSHA256, + loading: false, + bitsEntropy, + shannonEntropy, + }, + index, + ); + } return garage.getRobot(index); }; diff --git a/frontend/src/models/Federation.model.ts b/frontend/src/models/Federation.model.ts index cf05305e..1aa5381e 100644 --- a/frontend/src/models/Federation.model.ts +++ b/frontend/src/models/Federation.model.ts @@ -102,11 +102,12 @@ export class Federation { const coordinator = this.coordinators[currentOrder.shortAlias]; if (coordinator != null && currentOrder.id !== null) { const newOrder = await coordinator.fetchOrder(currentOrder.id, robot); - - return { - ...currentOrder, - order: newOrder, - }; + if (newOrder) { + return { + ...currentOrder, + order: newOrder, + }; + } } } return currentOrder; diff --git a/frontend/src/models/Garage.model.ts b/frontend/src/models/Garage.model.ts index 09f77501..8891da68 100644 --- a/frontend/src/models/Garage.model.ts +++ b/frontend/src/models/Garage.model.ts @@ -17,10 +17,9 @@ class Garage { this.slots = rawSlots .filter((raw: any) => raw !== null) .map((raw: any) => { - const newSlot: Slot = { robot: new Robot(), order: null }; - newSlot.order = raw.order as Order; - newSlot.robot.update(raw.robot); - return newSlot; + const robot = new Robot(raw.robot); + robot.update(raw.robot); + return { robot, order: raw.order as Order }; }); console.log('Robot Garage was loaded from local storage'); } @@ -29,7 +28,8 @@ class Garage { this.slots = [{ robot: new Robot(), order: null }]; } - this.currentSlot = this.slots.length - 1; + this.currentSlot = 0; + this.hooks = { onRobotUpdate: [], onOrderUpdate: [], @@ -73,7 +73,7 @@ class Garage { deleteSlot: (index?: number) => void = (index) => { const targetSlot = index ?? this.slots.length - 1; this.slots.splice(targetSlot, 1); - this.currentSlot = this.slots.length - 1; + this.currentSlot = 0; this.triggerHook('onRobotUpdate'); this.triggerHook('onOrderUpdate'); this.save(); @@ -104,12 +104,12 @@ class Garage { return this.getSlot(slot).robot; }; - createRobot = (attributes: Record): number => { + createRobot = (attributes: Record): void => { const newSlot = { robot: new Robot(), order: null }; newSlot.robot.update(attributes); this.slots.push(newSlot); - - return this.slots.length - 1; + this.currentSlot = this.slots.length - 1; + this.save(); }; // Orders diff --git a/frontend/static/federation.json b/frontend/static/federation.json index 0d13d70f..56ef5521 100644 --- a/frontend/static/federation.json +++ b/frontend/static/federation.json @@ -119,7 +119,7 @@ "i2p": "" }, "testnet": { - "onion": "https://qu6xztmzhlve6nxbb77jldek53pvhkaltz6seni7wq6g6yyj233qp4yd.onion", + "onion": "https://testraliar7xkhos2gipv2k65obykofb4jqzl5l4danfryacifi4t7qd.onion", "clearnet": "https://test.unsafe.satstralia.com", "i2p": "" }, @@ -134,14 +134,14 @@ "color": "#000000", "testnet": { "onion": "http://none.onion", - "clearnet": "http://127.0.0.1:12596" + "clearnet": "http://localhost:12596" }, "badges": { "donatesToDevFund": 0 }, "mainnet": { "onion": "http://none.onion", - "clearnet": "http://127.0.0.1:12596" + "clearnet": "http://localhost:12596" }, "policies": { "Development Policy": "Don't look around, just buidl" diff --git a/nodeapp/robosats-client.sh b/nodeapp/robosats-client.sh index a925c487..27cbddeb 100644 --- a/nodeapp/robosats-client.sh +++ b/nodeapp/robosats-client.sh @@ -39,7 +39,7 @@ testnet_temple_socat="socat tcp4-LISTEN:${testnet_temple_port},reuseaddr,fork,ke mainnet_satstralia_onion=satstraoq35jffvkgpfoqld32nzw2siuvowanruindbfojowpwsjdgad.onion mainnet_satstralia_port=103 # Testnet -testnet_satstralia_onion=qu6xztmzhlve6nxbb77jldek53pvhkaltz6seni7wq6g6yyj233qp4yd.onion +testnet_satstralia_onion=testraliar7xkhos2gipv2k65obykofb4jqzl5l4danfryacifi4t7qd.onion testnet_satstralia_port=1003 # socat cmd mainnet_satstralia_socat="socat tcp4-LISTEN:${mainnet_satstralia_port},reuseaddr,fork,keepalive,bind=127.0.0.1 SOCKS4A:${TOR_PROXY_IP:-127.0.0.1}:${mainnet_satstralia_onion}:80,socksport=${TOR_PROXY_PORT:-9050}"