diff --git a/src/services.ts b/src/services.ts index b5ca897..16b5a93 100644 --- a/src/services.ts +++ b/src/services.ts @@ -1,4 +1,4 @@ -import { createUserReturn, User, Process, createNotificationTransactionReturn, parse_network_msg, outputs_list, parseNetworkMsgReturn, FaucetMessage, AnkFlag, NewTxMessage, encryptWithNewKeyResult, AnkSharedSecret, NetworkMessage } from '../dist/pkg/sdk_client'; +import { createUserReturn, User, Process, createTransactionReturn, parse_network_msg, outputs_list, FaucetMessage, AnkFlag, NewTxMessage, encryptWithNewKeyResult, AnkSharedSecret, CachedMessage } from '../dist/pkg/sdk_client'; import IndexedDB from './database' import { WebSocketClient } from './websockets'; @@ -110,38 +110,31 @@ class Services { let notificationInfo = await services.notify_address_for_message(recipientSpAddress, msg_payload); if (notificationInfo) { let networkMsg = notificationInfo.new_network_msg; - const msgId = notificationInfo.new_network_msg.id; let shared_secret = ''; if (networkMsg.shared_secret) { shared_secret = networkMsg.shared_secret; } else { throw 'no shared_secret'; } - let ciphers: string[] = []; console.info('Successfully sent notification transaction'); - // Save the secret to db - const indexedDb = await IndexedDB.getInstance(); - const db = await indexedDb.getDb(); - + + const connection = await services.pickWebsocketConnectionRandom(); + const flag: AnkFlag = "Unknown"; // encrypt the message(s) + // TODO we'd rather do that in the wasm as part of notify_address_for_message try { const cipher = await services.encryptData(msg_payload, shared_secret); let updated_msg = notificationInfo.new_network_msg; updated_msg.plaintext = msg_payload; updated_msg.ciphertext = cipher; - await indexedDb.writeObject(db, indexedDb.getStoreList().AnkMessages, updated_msg, null); - ciphers.push(cipher); + await services.updateMessages(updated_msg); + connection?.sendMessage(flag, cipher); } catch (error) { throw error; } - const connection = await services.pickWebsocketConnectionRandom(); - const flag: AnkFlag = "Unknown"; // add peers list // add processes list // send message (transaction in envelope) - for (const payload of ciphers) { - connection?.sendMessage(flag, payload); - } } } @@ -322,10 +315,10 @@ class Services { services.attachSubmitListener("form4nk", services.updateAnId); } - public async parseNetworkMessage(raw: string, feeRate: number): Promise { + public async parseNetworkMessage(raw: string, feeRate: number): Promise { const services = await Services.getInstance(); try { - const msg: parseNetworkMsgReturn = services.sdkClient.parse_network_msg(raw, feeRate); + const msg: CachedMessage = services.sdkClient.parse_network_msg(raw, feeRate); return msg; } catch (error) { throw error; @@ -443,7 +436,7 @@ class Services { return process; } - public async updateMessages(message: NetworkMessage): Promise { + public async updateMessages(message: CachedMessage): Promise { const indexedDb = await IndexedDB.getInstance(); const db = await indexedDb.getDb(); @@ -454,6 +447,17 @@ class Services { } } + public async removeMessage(id: number): Promise { + const indexedDb = await IndexedDB.getInstance(); + const db = await indexedDb.getDb(); + + try { + await indexedDb.rmObject(db, indexedDb.getStoreList().AnkMessages, id); + } catch (error) { + throw error; + } + } + public async updateProcesses(): Promise { const services = await Services.getInstance(); const processList: Process[] = services.sdkClient.get_processes(); @@ -768,6 +772,7 @@ class Services { const flag: AnkFlag = 'Faucet'; const faucetMsg = services.sdkClient.create_faucet_msg(); connection.sendMessage(flag, JSON.stringify(faucetMsg)); + await services.updateMessages(faucetMsg); } catch (error) { console.error("Failed to obtain tokens with relay ", connection.getUrl()); return null; @@ -806,7 +811,7 @@ class Services { } } - public async confirm_sender_address(sp_address: string): Promise { + public async confirm_sender_address(msg: CachedMessage): Promise { const services = await Services.getInstance(); const connection = await services.pickWebsocketConnectionRandom(); if (!connection) { @@ -824,10 +829,10 @@ class Services { throw error; } - let notificationInfo: createNotificationTransactionReturn; + let notificationInfo: createTransactionReturn; try { const feeRate = 1; - notificationInfo = services.sdkClient.create_notification_transaction(sp_address, null, feeRate); + notificationInfo = services.sdkClient.answer_confirmation_transaction(msg, feeRate); } catch (error) { throw new Error(`Failed to create confirmation transaction: ${error}`); } @@ -840,7 +845,7 @@ class Services { return; } - public async notify_address_for_message(sp_address: string, message: string): Promise { + public async notify_address_for_message(sp_address: string, message: string): Promise { const services = await Services.getInstance(); const connection = await services.pickWebsocketConnectionRandom(); if (!connection) { @@ -850,7 +855,7 @@ class Services { try { const feeRate = 1; const commitment = services.sdkClient.create_commitment(message); - let notificationInfo: createNotificationTransactionReturn = services.sdkClient.create_notification_transaction(sp_address, commitment, feeRate); + let notificationInfo: createTransactionReturn = services.sdkClient.create_notification_transaction(sp_address, commitment, feeRate); const flag: AnkFlag = "NewTx"; const newTxMsg: NewTxMessage = { 'transaction': notificationInfo.transaction,