Skip to content

Commit

Permalink
Fix robot recovery (#945)
Browse files Browse the repository at this point in the history
* Fix Robot recovery

* Change testralia onion
  • Loading branch information
KoalaSat authored and Reckless-Satoshi committed Dec 2, 2023
1 parent b973afe commit 8d8e3a5
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 38 deletions.
6 changes: 3 additions & 3 deletions frontend/src/basic/RobotPage/Recovery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ interface RecoveryProps {
inputToken: string;
badToken: string;
setInputToken: (state: string) => void;
getGenerateRobot: (token: string) => void;
getRecoverRobot: (token: string) => void;
}

const Recovery = ({
inputToken,
badToken,
setView,
setInputToken,
getGenerateRobot,
getRecoverRobot,
}: RecoveryProps): JSX.Element => {
const { t } = useTranslation();

const onClickRecover = (): void => {
getGenerateRobot(inputToken);
getRecoverRobot(inputToken);
setView('profile');
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/basic/RobotPage/RobotProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
26 changes: 20 additions & 6 deletions frontend/src/basic/RobotPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand All @@ -67,16 +65,32 @@ const RobotPage = ({ avatarBaseUrl }: RobotPageProps): JSX.Element => {
}
}, [inputToken]);

const getRecoverRobot = (token: string): void => {
setInputToken(token);
genKey(token)
.then((key) => {
garage.updateRobot({
token,
pubKey: key.publicKeyArmored,
encPrivKey: key.encryptedPrivateKeyArmored,
});
void federation.fetchRobot(garage, garage.currentSlot);
})
.catch((error) => {
console.error('Error:', error);
});
};

const getGenerateRobot = (token: string): void => {
setInputToken(token);
genKey(token)
.then((key) => {
const slot = garage.createRobot({
garage.createRobot({
token,
pubKey: key.publicKeyArmored,
encPrivKey: key.encryptedPrivateKeyArmored,
});
void federation.fetchRobot(garage, slot);
void federation.fetchRobot(garage, garage.currentSlot);
})
.catch((error) => {
console.error('Error:', error);
Expand Down Expand Up @@ -180,7 +194,7 @@ const RobotPage = ({ avatarBaseUrl }: RobotPageProps): JSX.Element => {
badToken={badToken}
inputToken={inputToken}
setInputToken={setInputToken}
getGenerateRobot={getGenerateRobot}
getRecoverRobot={getRecoverRobot}
/>
) : null}
</Paper>
Expand Down
25 changes: 15 additions & 10 deletions frontend/src/models/Coordinator.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/models/Federation.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions frontend/src/models/Garage.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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: [],
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -104,12 +104,12 @@ class Garage {
return this.getSlot(slot).robot;
};

createRobot = (attributes: Record<any, any>): number => {
createRobot = (attributes: Record<any, any>): 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
Expand Down
6 changes: 3 additions & 3 deletions frontend/static/federation.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"i2p": ""
},
"testnet": {
"onion": "https://qu6xztmzhlve6nxbb77jldek53pvhkaltz6seni7wq6g6yyj233qp4yd.onion",
"onion": "https://testraliar7xkhos2gipv2k65obykofb4jqzl5l4danfryacifi4t7qd.onion",
"clearnet": "https://test.unsafe.satstralia.com",
"i2p": ""
},
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion nodeapp/robosats-client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 8d8e3a5

Please sign in to comment.