From edef1428c3a1a21dd12cd118d9bc33d7e0aca71f Mon Sep 17 00:00:00 2001 From: Sosthene Date: Sat, 12 Oct 2024 11:59:59 +0200 Subject: [PATCH] Refactor initialisation of pairing process --- src/index.ts | 26 ++++++------- src/services/service.ts | 84 +++++++++++++++++++---------------------- 2 files changed, 49 insertions(+), 61 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6bc33b7..3688414 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,23 +4,20 @@ import Services from './services/service'; // const wsurl = `ws://74.234.68.175:8091`; const wsurl = `wss://localhost/ws/`; document.addEventListener('DOMContentLoaded', async () => { - try { +try { - const services = await Services.getInstance(); - let user = await services.getWallet() + const services = await Services.getInstance(); + let device = await services.getDevice() - setTimeout(async () => { - if(!user) { - const sp_adress = await services.createNewDevice(); - user = await services.getWallet() - - } else { - const address = await services.getAdresses() - const device = await services.getDevice() - if(device) await services.restoreDevice(device) - } + if(!device) { + device = await services.createNewDevice(); + } else { + await services.restoreDevice(device) + } await services.addWebsocketConnection(wsurl); - await services.recoverInjectHtml() + await services.recoverInjectHtml(); + await services.restoreProcesses(); + await services.restoreMessages(); const queryString = window.location.search; const urlParams = new URLSearchParams(queryString) @@ -28,7 +25,6 @@ document.addEventListener('DOMContentLoaded', async () => { if(pairingAddress) { await services.sendPairingTx(pairingAddress) } - }) } catch (error) { console.error(error); } diff --git a/src/services/service.ts b/src/services/service.ts index 09e38d1..e137496 100644 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -16,12 +16,13 @@ type ProcessesCache = { [key: string]: any; }; +const U32_MAX = 4294967295; + export default class Services { private static instance: Services; private current_process: string | null = null; private sdkClient: any; private websocketConnection: WebSocketClient | null = null; - private sp_address: string | null = null; private processes: IProcess[] | null = null; private notifications: INotification[] | null = null; private subscriptions: {element: Element; event: string; eventHandler: string;}[] = [] ; @@ -52,6 +53,7 @@ export default class Services { services.websocketConnection = newClient; } } + public async recoverInjectHtml(): Promise { const container = document.getElementById('containerId'); @@ -71,9 +73,12 @@ export default class Services { if(btn) { this.addSubscription(btn, 'click', 'injectProcessListPage') } - const url = location.href + // const url = location.href - this.generateQRCode(this.sp_address || '') + const services = await Services.getInstance(); + const localAddress = await this.getDeviceAddress(); + + this.generateQRCode(localAddress || '') } private generateQRCode = async (text: string) => { @@ -90,77 +95,64 @@ export default class Services { } async prepareProcessTx(myAddress: string, recipientAddress: string) { - - const txid = '0'.repeat(64) - var vout = Number.MAX_SAFE_INTEGER; + const initial_session_privkey = new Uint8Array(32); + const initial_session_pubkey = new Uint8Array(32); const paringTemplate = { - "html": "", - "style": "", - "script": "", "description": "AliceBob", "roles": { "owner": { - "members": [{sp_addresses: [myAddress]}, {sp_addresses:[recipientAddress]}], + "members": + [{sp_addresses: [myAddress]}, {sp_addresses: [recipientAddress]}], "validation_rules": [ { "quorum": 1.0, "fields": [ + "description", "roles", - "pairing_tx" + "session_privkey", + "session_pubkey", + "key_parity" ], "min_sig_member": 1.0 } ] } }, - "pairing_tx": `${txid}:4294967295`, + "session_privkey": initial_session_privkey, + "session_pubkey": initial_session_pubkey, + "key_parity": true, } const service = await Services.getInstance(); - const process = await service.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1) - return process + const apiReturn = await service.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1) + return apiReturn } - async sendPairingTx(sp_address: string): Promise { + public async sendPairingTx(spAddress: string): Promise { const services = await Services.getInstance(); const amount = await this.getAmount() as any console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount) - // if(amount === 0n) { - // const faucetMessage = await services.createFaucetMessage() - // console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage) - // services.websocketConnection?.sendNormalMessage(faucetMessage) - // } + if(amount === 0n) { + const faucetMessage = await services.createFaucetMessage() + console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage) + services.websocketConnection?.sendNormalMessage(faucetMessage) + } - const spAddress = await this.getDeviceAddress() as any - let txid = '0'.repeat(64) + const localAddress = await this.getDeviceAddress() as any + const emptyTxid = '0'.repeat(64) - setTimeout(async () => { - let pairing = await services.sdkClient.pair_device(`${txid}:4294967295`, [sp_address]) - const process = await this.prepareProcessTx(spAddress, sp_address) - const tx = process.new_tx_to_send - const parsedTx = JSON.parse(tx) - const transaction = parsedTx.transaction - txid = await services.sdkClient.get_txid(transaction) - const root_commitment = process.updated_process[0]; - const init_process = process.updated_process[1]; - console.log("🚀 ~ Services ~ setTimeout ~ init_process:", init_process, init_process.payload) - console.log("🚀 ~ Services ~ setTimeout ~ txToSend:", tx) - const prd = JSON.stringify(init_process.impending_requests[0]); - services.websocketConnection?.sendMessage('NewTx', tx) - pairing = await services.sdkClient.pair_device(`${txid}:0`, [sp_address]) + try { + await services.sdkClient.pair_device(`${emptyTxid}:${U32_MAX}`, [spAddress]) + } catch (e) { + console.error("Services ~ Error:", e); + return + } + const apiReturn = await this.prepareProcessTx(localAddress, spAddress) + this.handleApiReturn(apiReturn); - const dump = await services.sdkClient.dump_device() - - const prd_response = await services.sdkClient.response_prd(root_commitment, prd, true) - console.log("🚀 ~ Services ~ setTimeout ~ prd_response:", prd_response, prd, root_commitment) - if(process?.ciphers_to_send && process.ciphers_to_send.length) { - await this.sendCipherMessages(process.ciphers_to_send) - } - const router = await Routing.getInstance(); - router.openLoginModal(spAddress, sp_address) - }, 2000) + return } async resetDevice() {