Rename processTx to handleApiReturn()

This commit is contained in:
Sosthene 2024-10-12 11:57:58 +02:00
parent 2f2b2a3a75
commit 78822aa7fe

View File

@ -168,11 +168,24 @@ export default class Services {
await service.sdkClient.reset_device() await service.sdkClient.reset_device()
} }
async sendCipherMessages(messages: string[]) { async sendNewTxMessage(message: string) {
const service = await Services.getInstance(); const services = await Services.getInstance();
messages.forEach((message) => { console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ newTxMessage:", message)
service.websocketConnection?.sendMessage('Cipher', 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<void> { async sendFaucetMessage(): Promise<void> {
@ -195,7 +208,7 @@ export default class Services {
} }
const parsedTx = await services.sdkClient.parse_cipher(message, 0.00001) const parsedTx = await services.sdkClient.parse_cipher(message, 0.00001)
console.log("🚀 ~ Services ~ parseCipher ~ parsedTx:", parsedTx) console.log("🚀 ~ Services ~ parseCipher ~ parsedTx:", parsedTx)
await this.processTx(parsedTx) await this.handleApiReturn(parsedTx)
// await this.saveCipherTxToDb(parsedTx) // await this.saveCipherTxToDb(parsedTx)
} catch(e) { } catch(e) {
console.log(e) console.log(e)
@ -209,7 +222,7 @@ export default class Services {
const parsedTx = await services.sdkClient.parse_new_tx(tx, 0, 0.01) const parsedTx = await services.sdkClient.parse_new_tx(tx, 0, 0.01)
console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx) console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx)
if(parsedTx) { if(parsedTx) {
await this.processTx(parsedTx) await this.handleApiReturn(parsedTx)
// await this.saveTxToDb(parsedTx) // await this.saveTxToDb(parsedTx)
} }
} catch(e) { } catch(e) {
@ -217,53 +230,37 @@ export default class Services {
} }
} }
async processTx(tx: ApiReturn) { private async handleApiReturn(apiReturn: ApiReturn) {
const service = await Services.getInstance() // const service = await Services.getInstance()
if(tx.ciphers_to_send && tx.ciphers_to_send.length) { if(apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length) {
await this.sendCipherMessages(tx.ciphers_to_send) await this.sendCipherMessages(apiReturn.ciphers_to_send)
} }
if(tx.new_tx_to_send) { if(apiReturn.new_tx_to_send) {
service.websocketConnection?.sendMessage('NewTx', tx.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 if(apiReturn.updated_process && apiReturn.updated_process.length) {
const services = await Services.getInstance(); const [processCommitment, process] = apiReturn.updated_process;
// const faucetMessage = await services.createFaucetMessage() console.debug('Updated Process Commitment:', processCommitment);
// console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage) console.debug('Process Details:', process);
// services.websocketConnection?.sendNormalMessage(faucetMessage) // Save process to storage
router.openConfirmationModal(impendingRequest, outpointCommitment) 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) { if(apiReturn.updated_cached_msg && apiReturn.updated_cached_msg.length) {
// const parsedPrd = JSON.parse(tx.prd) apiReturn.updated_cached_msg.forEach((msg, index) => {
// console.log("🚀 ~ Services ~ processTx ~ Opened:", parsedPrd) console.debug(`CachedMessage ${index}:`, msg);
// const router = await Routing.getInstance(); // 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 (apiReturn.commit_to_send) {
// if(parsedPrd && parsedPrd.prd_type === 'Init') { const commit = apiReturn.commit_to_send;
// const services = await Services.getInstance(); this.sendCommitMessage(JSON.stringify(commit));
// 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()
// }
// }
} }
async pairDevice(prd: any, outpointCommitment: string) { async pairDevice(prd: any, outpointCommitment: string) {