Reformat websocket message parsing

This commit is contained in:
Sosthene 2024-05-22 20:43:31 +02:00
parent 29c1db5869
commit 2f4cd4fb9b

View File

@ -1,5 +1,5 @@
import Services from "./services"; import Services from "./services";
import { AnkFlag, AnkNetworkMsg, parseNetworkMsgReturn } from "../dist/pkg/sdk_client"; import { AnkFlag, AnkNetworkMsg, CachedMessage } from "../dist/pkg/sdk_client";
class WebSocketClient { class WebSocketClient {
private ws: WebSocket; private ws: WebSocket;
@ -25,24 +25,38 @@ class WebSocketClient {
(async () => { (async () => {
if (typeof(msgData) === 'string') { if (typeof(msgData) === 'string') {
// console.log("Received text message: "+msgData); console.log("Received text message: "+msgData);
try { try {
const feeRate = 1; const feeRate = 1;
let res = await services.parseNetworkMessage(msgData, feeRate); // By parsing the message, we can link it with existing cached message and return the updated version of the message
if (res.topic === 'new_tx') { let res: CachedMessage = await services.parseNetworkMessage(msgData, feeRate);
// we received a tx console.debug(res);
window.alert(`New tx\n${res.message}`); if (res.status === 'FaucetComplete') {
// we received a faucet tx, there's nothing else to do
window.alert(`New faucet output\n${res.commited_in}`);
await services.removeMessage(res.id);
await services.updateOwnedOutputsForUser(); await services.updateOwnedOutputsForUser();
} else if (res.topic === 'unknown') { } else if (res.status === 'TxWaitingCipher') {
let message = res.message['plaintext']; // we received a tx but we don't have the cipher
let sender = res.message['sender']; console.debug(`received notification in output ${res.commited_in}, waiting for cipher message`);
if (!message || !sender) { await services.updateMessages(res);
throw 'Message missing plaintext and/or sender'; await services.updateOwnedOutputsForUser();
} } else if (res.status === 'CipherWaitingTx') {
window.alert(`new message: ${message}\nAsking sender ${sender} to confirm identity...`); // we received a cipher but we don't have the key
console.debug(`sending confirm message to ${sender}`); console.debug(`received a cipher`);
await services.updateMessages(res.message); await services.updateMessages(res);
await services.confirm_sender_address(sender); } else if (res.status === 'SentWaitingConfirmation') {
// We are sender and we're waiting for the challenge that will confirm recipient got the transaction and the message
await services.updateOwnedOutputsForUser();
} else if (res.status === 'MustSpendConfirmation') {
// we received a challenge for a notification we made
// that means we can stop rebroadcasting the tx and we must spend the challenge to confirm
window.alert(`Spending ${res.confirmed_by} to prove our identity`);
console.debug(`sending confirm message to ${res.recipient}`);
await services.updateMessages(res);
await services.confirm_sender_address(res);
} else {
console.debug('Received an unimplemented valid message');
} }
} catch (error) { } catch (error) {
console.error('Received an invalid message:', error); console.error('Received an invalid message:', error);