From 1f4153619a956a2171a9da536254949b2b6218de Mon Sep 17 00:00:00 2001 From: Sosthene Date: Mon, 14 Oct 2024 13:22:27 +0200 Subject: [PATCH] Minor improvements and bug fixes --- src/services/routing.service.ts | 8 +-- src/services/service.ts | 96 +++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/services/routing.service.ts b/src/services/routing.service.ts index acf50d1..204675e 100644 --- a/src/services/routing.service.ts +++ b/src/services/routing.service.ts @@ -69,7 +69,7 @@ export default class Routing { window.onclick = (event) => { const modal = document.getElementById('modal'); if (event.target === modal) { - this.confirm(this.currentPrd || pcd, outpointCommitment || this.currentOutpoint); + this.confirm(pcd, outpointCommitment); } } } @@ -84,12 +84,12 @@ export default class Routing { if (modal) modal.style.display = 'none'; } - async confirm(prd?: any, outpointCommitment?: string) { + async confirm(prd: any, outpointCommitment: string) { const service = await Services.getInstance() const modal = document.getElementById('modal') - console.log("🚀 ~ Routing ~ confirm ~ prd:", this.currentPrd || prd, outpointCommitment) + console.log("🚀 ~ Routing ~ confirm ~ prd:", prd, outpointCommitment) if (modal) modal.style.display = 'none'; - if(this.currentPrd || prd) service.pairDevice(this.currentPrd || prd, outpointCommitment || this.currentOutpoint as string) + await service.pairDevice(prd, outpointCommitment); } async closeConfirmationModal() { const modal = document.getElementById('modal') diff --git a/src/services/service.ts b/src/services/service.ts index 8c82743..d2ac18e 100644 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -103,10 +103,10 @@ export default class Services { } } - async prepareProcessTx(myAddress: string, recipientAddress: string) { + private prepareProcessTx(myAddress: string, recipientAddress: string) { const initial_session_privkey = new Uint8Array(32); const initial_session_pubkey = new Uint8Array(32); - const paringTemplate = { + const pairingTemplate = { "description": "AliceBob", "roles": { "owner": { @@ -134,27 +134,33 @@ export default class Services { } - const apiReturn = await this.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1) + const apiReturn = this.sdkClient.create_update_transaction(undefined, JSON.stringify(pairingTemplate), 1) return apiReturn } public async sendPairingTx(spAddress: string): Promise { - // const services = await Services.getInstance(); - const amount = await this.getAmount() as any - console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount) + const amount = this.sdkClient.get_available_amount(); + + if (amount === 0n) { + const faucetMsg = this.sdkClient.create_faucet_msg(); + await this.sendFaucetMessage(faucetMsg); + } - const localAddress = await this.getDeviceAddress() as any + const localAddress = this.sdkClient.get_address(); const emptyTxid = '0'.repeat(64) try { let commitmentOutpoint = `${emptyTxid}:${U32_MAX}`; - await this.sdkClient.pair_device(commitmentOutpoint, [spAddress]) + this.sdkClient.pair_device(commitmentOutpoint, [spAddress]) } catch (e) { console.error("Services ~ Error:", e); return } - const apiReturn = await this.prepareProcessTx(localAddress, spAddress) - this.handleApiReturn(apiReturn); + + setTimeout( async () => { + const apiReturn = this.prepareProcessTx(localAddress, spAddress) + await this.handleApiReturn(apiReturn); + }, 100) return } @@ -211,7 +217,7 @@ export default class Services { async parseNewTx(tx: string) { try { // console.log('==============> sending txxxxxxx parser', tx) - const parsedTx = await this.sdkClient.parse_new_tx(tx, 0, 0.01) + const parsedTx = await this.sdkClient.parse_new_tx(tx, 0, 0.0001) if(parsedTx) { console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx) try { @@ -223,40 +229,52 @@ export default class Services { } } } catch(e) { - console.trace('Not our transaction'); + console.trace(e); } } private async handleApiReturn(apiReturn: ApiReturn) { - if(apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length) { + if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length) { await this.sendCipherMessages(apiReturn.ciphers_to_send) } - if(apiReturn.new_tx_to_send) { - await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send)) - } + setTimeout(async () => { + 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); - 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) - } + // Save process to storage + localStorage.setItem(processCommitment, JSON.stringify(process)); + // Check if the newly updated process reveals some new information + try { + const proposals: string[] = this.sdkClient.get_update_proposals(processCommitment); + const actual_proposal = JSON.parse(proposals[0]); // just hacky way to solve redundant info for now + console.info(actual_proposal); + let router = await Routing.getInstance(); + router.openConfirmationModal(actual_proposal, processCommitment); + } catch (e) { + console.error(e); + } + } - 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)); - }); - } + 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)); + }); + } - if (apiReturn.commit_to_send) { - const commit = apiReturn.commit_to_send; - this.sendCommitMessage(JSON.stringify(commit)); - } + if (apiReturn.commit_to_send) { + const commit = apiReturn.commit_to_send; + await this.sendCommitMessage(JSON.stringify(commit)); + } + + if (apiReturn.new_tx_to_send) { + await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send)) + } + }, 0) } async pairDevice(prd: any, outpointCommitment: string) { @@ -327,9 +345,7 @@ export default class Services { } async getDeviceAddress() { - const address = await this.sdkClient.get_address() - console.log("🚀 ~ Services ~ getDeviceAddress ~ address:", address) - return address + return await this.sdkClient.get_address(); } async dumpDevice() { @@ -481,7 +497,7 @@ export default class Services { } } - private cleanSubsciptions(): void { + private cleanSubscriptions(): void { for (const sub of this.subscriptions) { const el = sub.element; const eventHandler = sub.eventHandler; @@ -498,7 +514,7 @@ export default class Services { return; } - this.cleanSubsciptions() + this.cleanSubscriptions() // const user = services.sdkClient.create_user('Test', 'test', 10, 1, 'Messaging') // console.log("🚀 ~ Services ~ injectProcessListPage ~ user:", user)