From 2f4cd4fb9b320e83a845101dc57ddf162ba162c7 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Wed, 22 May 2024 20:43:31 +0200 Subject: [PATCH] Reformat websocket message parsing --- src/websockets.ts | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/websockets.ts b/src/websockets.ts index 5067254..67d8434 100644 --- a/src/websockets.ts +++ b/src/websockets.ts @@ -1,5 +1,5 @@ import Services from "./services"; -import { AnkFlag, AnkNetworkMsg, parseNetworkMsgReturn } from "../dist/pkg/sdk_client"; +import { AnkFlag, AnkNetworkMsg, CachedMessage } from "../dist/pkg/sdk_client"; class WebSocketClient { private ws: WebSocket; @@ -25,24 +25,38 @@ class WebSocketClient { (async () => { if (typeof(msgData) === 'string') { - // console.log("Received text message: "+msgData); + console.log("Received text message: "+msgData); try { const feeRate = 1; - let res = await services.parseNetworkMessage(msgData, feeRate); - if (res.topic === 'new_tx') { - // we received a tx - window.alert(`New tx\n${res.message}`); + // By parsing the message, we can link it with existing cached message and return the updated version of the message + let res: CachedMessage = await services.parseNetworkMessage(msgData, feeRate); + console.debug(res); + 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(); - } else if (res.topic === 'unknown') { - let message = res.message['plaintext']; - let sender = res.message['sender']; - if (!message || !sender) { - throw 'Message missing plaintext and/or sender'; - } - window.alert(`new message: ${message}\nAsking sender ${sender} to confirm identity...`); - console.debug(`sending confirm message to ${sender}`); - await services.updateMessages(res.message); - await services.confirm_sender_address(sender); + } else if (res.status === 'TxWaitingCipher') { + // we received a tx but we don't have the cipher + console.debug(`received notification in output ${res.commited_in}, waiting for cipher message`); + await services.updateMessages(res); + await services.updateOwnedOutputsForUser(); + } else if (res.status === 'CipherWaitingTx') { + // we received a cipher but we don't have the key + console.debug(`received a cipher`); + await services.updateMessages(res); + } 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) { console.error('Received an invalid message:', error);