Reformat websocket message parsing
This commit is contained in:
parent
29c1db5869
commit
2f4cd4fb9b
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user