From 78822aa7feb3d3c5ee050c15c503d34d5661627a Mon Sep 17 00:00:00 2001 From: Sosthene Date: Sat, 12 Oct 2024 11:57:58 +0200 Subject: [PATCH] Rename processTx to handleApiReturn() --- src/services/service.ts | 91 ++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/src/services/service.ts b/src/services/service.ts index 371a93c..3abb043 100644 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -168,11 +168,24 @@ export default class Services { await service.sdkClient.reset_device() } - async sendCipherMessages(messages: string[]) { - const service = await Services.getInstance(); - messages.forEach((message) => { - service.websocketConnection?.sendMessage('Cipher', message) - }) + async sendNewTxMessage(message: string) { + const services = await Services.getInstance(); + console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ newTxMessage:", message) + services.websocketConnection?.sendMessage('NewTx', message) + } + + async sendCommitMessage(message: string) { + const services = await Services.getInstance(); + console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ CommitMessage:", message) + services.websocketConnection?.sendMessage('Commit', message) + } + + async sendCipherMessages(ciphers: string[]) { + const services = await Services.getInstance(); + for (let i = 0; i < ciphers.length; i++) { + const cipher = ciphers[i]; + await services.websocketConnection?.sendMessage('Cipher', cipher); + } } async sendFaucetMessage(): Promise { @@ -195,7 +208,7 @@ export default class Services { } const parsedTx = await services.sdkClient.parse_cipher(message, 0.00001) console.log("🚀 ~ Services ~ parseCipher ~ parsedTx:", parsedTx) - await this.processTx(parsedTx) + await this.handleApiReturn(parsedTx) // await this.saveCipherTxToDb(parsedTx) } catch(e) { console.log(e) @@ -209,7 +222,7 @@ export default class Services { const parsedTx = await services.sdkClient.parse_new_tx(tx, 0, 0.01) console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx) if(parsedTx) { - await this.processTx(parsedTx) + await this.handleApiReturn(parsedTx) // await this.saveTxToDb(parsedTx) } } catch(e) { @@ -217,53 +230,37 @@ export default class Services { } } - async processTx(tx: ApiReturn) { - const service = await Services.getInstance() - if(tx.ciphers_to_send && tx.ciphers_to_send.length) { - await this.sendCipherMessages(tx.ciphers_to_send) + private async handleApiReturn(apiReturn: ApiReturn) { + // const service = await Services.getInstance() + if(apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length) { + await this.sendCipherMessages(apiReturn.ciphers_to_send) } - if(tx.new_tx_to_send) { - service.websocketConnection?.sendMessage('NewTx', tx.new_tx_to_send) + if(apiReturn.new_tx_to_send) { + await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send)) } - if(tx.updated_process && tx.updated_process.length) { - // if(tx.ciphers_to_send?.length != 1 || tx.updated_process?.length != 2) return - const impendingRequest = tx.updated_process[1].impending_requests[0] - const pcdCommitment = impendingRequest.payload - const outpointCommitment = tx.updated_process[0] - console.log("🚀 ~ Services ~ processTx ~ pcdCommitment:", pcdCommitment) - if(impendingRequest.prd_type === 'Update' && tx.ciphers_to_send.length === 0) { - const router = await Routing.getInstance(); - // Mocker le fait que c'est une transaction de connection - const services = await Services.getInstance(); - // const faucetMessage = await services.createFaucetMessage() - // console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage) - // services.websocketConnection?.sendNormalMessage(faucetMessage) - router.openConfirmationModal(impendingRequest, outpointCommitment) - } + if(apiReturn.updated_process && apiReturn.updated_process.length) { + const [processCommitment, process] = apiReturn.updated_process; + console.debug('Updated Process Commitment:', processCommitment); + console.debug('Process Details:', process); + // Save process to storage + localStorage.setItem(processCommitment, JSON.stringify(process)); + // router.openConfirmationModal(impendingRequest, outpointCommitment) } - - // if(tx.updated_cached_msg && tx.updated_cached_msg.length) { - // tx.updated_cached_msg.forEach(message => this.saveTxToDb(message)) - // } - // if(tx.prd) { - // const parsedPrd = JSON.parse(tx.prd) - // console.log("🚀 ~ Services ~ processTx ~ Opened:", parsedPrd) - // const router = await Routing.getInstance(); + if(apiReturn.updated_cached_msg && apiReturn.updated_cached_msg.length) { + apiReturn.updated_cached_msg.forEach((msg, index) => { + console.debug(`CachedMessage ${index}:`, msg); + // Save the message to local storage + localStorage.setItem(msg.id.toString(), JSON.stringify(msg)); + }); + } - // // Mocker le fait que c'est une transaction de connection - // if(parsedPrd && parsedPrd.prd_type === 'Init') { - // const services = await Services.getInstance(); - // const faucetMessage = await services.createFaucetMessage() - // console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage) - // services.websocketConnection?.sendNormalMessage(faucetMessage) - // router.openConfirmationModal(parsedPrd) - // } else if(parsedPrd && parsedPrd.prd_type === 'Signed') { - // router.closeLoginModal() - // } - // } + if (apiReturn.commit_to_send) { + const commit = apiReturn.commit_to_send; + this.sendCommitMessage(JSON.stringify(commit)); + } } async pairDevice(prd: any, outpointCommitment: string) {