From 5a98fac745c9fc58e6fb613d9b4c471acb0a10ef Mon Sep 17 00:00:00 2001 From: Sosthene Date: Tue, 8 Jul 2025 17:21:29 +0200 Subject: [PATCH] Update our pairing addresses on receiving updates --- src/services/service.ts | 61 +++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/src/services/service.ts b/src/services/service.ts index 92e7943..4174bd8 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -673,6 +673,41 @@ export default class Services { await navigate('account'); } + public async updateDevice(): Promise { + let myPairingProcessId: string; + try { + myPairingProcessId = this.getPairingProcessId(); + } catch (e) { + console.error('Failed to get pairing process id'); + return; + } + + const myPairingProcess = await this.getProcess(myPairingProcessId); + if (!myPairingProcess) { + console.error('Unknown pairing process'); + return; + } + const myPairingState = this.getLastCommitedState(myPairingProcess); + if (myPairingState) { + const encodedSpAddressList = myPairingState.public_data['pairedAddresses']; + const spAddressList = this.decodeValue(encodedSpAddressList); + if (spAddressList.length === 0) { + console.error('Empty pairedAddresses'); + return; + } + // We can check if our address is included and simply unpair if it's not + if (!spAddressList.includes(this.getDeviceAddress())) { + await this.unpairDevice(); + return; + } + // We can update the device with the new addresses + this.sdkClient.unpair_device(); + this.sdkClient.pair_device(myPairingProcessId, spAddressList); + const newDevice = this.dumpDeviceFromMemory(); + await this.saveDeviceInDatabase(newDevice); + } + } + public async pairDevice() { if (!this.processId) { console.error('No processId set'); @@ -1249,14 +1284,6 @@ export default class Services { if (this.rolesContainsUs(state.roles)) { new_states.push(state.state_id); roles.push(state.roles); - } else if (state.public_data && state.public_data['pairedAddresses']) { - // This is a pairing process - const pairedAddresses = this.decodeValue(state.public_data['pairedAddresses']); - // Are we part of it? - if (pairedAddresses.includes(this.getDeviceAddress())) { - // We save the process to db - await this.saveProcessToDb(processId, process as Process); - } } } } @@ -1266,6 +1293,24 @@ export default class Services { await this.requestDataFromPeers(processId, new_states, roles); } + // Just to be sure check if that's a pairing process + const lastCommitedState = this.getLastCommitedState(process); + if (lastCommitedState && lastCommitedState.public_data && lastCommitedState.public_data['pairedAddresses']) { + // This is a pairing process + try { + const pairedAddresses = this.decodeValue(lastCommitedState.public_data['pairedAddresses']); + // Are we part of it? + if (pairedAddresses && pairedAddresses.length > 0 && pairedAddresses.includes(this.getDeviceAddress())) { + // We save the process to db + await this.saveProcessToDb(processId, process as Process); + // We update the device + await this.updateDevice(); + } + } catch (e) { + console.error('Failed to check for pairing process:', e); + } + } + // Otherwise we're probably just in the initial loading at page initialization // We may learn an update for this process