From b822f351fde7980547653b42f6784c24190f7e31 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Sat, 30 Nov 2024 20:07:27 +0100 Subject: [PATCH] Pairing works --- src/services/modal.service.ts | 30 +++++++++--------------- src/services/service.ts | 43 ++++++++++++++++++++++++----------- src/utils/sp-address.utils.ts | 10 ++------ 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/services/modal.service.ts b/src/services/modal.service.ts index ee171ec..98ae36d 100755 --- a/src/services/modal.service.ts +++ b/src/services/modal.service.ts @@ -53,23 +53,11 @@ export default class ModalService { } // this is kind of too specific for pairing though - public async openConfirmationModal(pcd: any, commitmentTx: string, merkleRoot: string) { - let map: Record; - if (pcd['roles']) { - const roles = pcd['roles']; - try { - map = JSON.parse(roles); - } catch (e) { - throw new Error(`Failed to parse roles: ${e}`); - } - } else { - throw new Error('Pcd doesn\'t have a \"roles\" field'); - } - + public async openPairingConfirmationModal(roleDefinition: Record, commitmentTx: string, merkleRoot: string) { // pairing specifics let members; - if (map['owner']) { - const owner = map['owner']; + if (roleDefinition['owner']) { + const owner = roleDefinition['owner']; members = owner.members; } else { throw new Error('No \"owner\" role'); @@ -83,7 +71,6 @@ export default class ModalService { // We take all the addresses except our own const service = await Services.getInstance(); const localAddress = await service.getDeviceAddress(); - console.log('🚀 ~ Routing ~ openConfirmationModal ~ pcd:', pcd); for (const member of members) { for (const address of member['sp_addresses']) { if (address !== localAddress) { @@ -127,7 +114,7 @@ export default class ModalService { // We send the prd update if (this.currentPcdCommitment) { try { - const createPrdUpdateReturn = await service.createPrdUpdate(this.currentPcdCommitment); + const createPrdUpdateReturn = service.createPrdUpdate(this.currentPcdCommitment); await service.handleApiReturn(createPrdUpdateReturn); } catch (e) { throw e @@ -138,13 +125,18 @@ export default class ModalService { // We send confirmation that we validate the change try { - const approveChangeReturn = await service.approveChange(this.currentPcdCommitment!); + const approveChangeReturn = service.approveChange(this.currentPcdCommitment!); await service.handleApiReturn(approveChangeReturn); } catch (e) { throw e } - service.pairDevice(this.paired_addresses); + try { + service.pairDevice(this.paired_addresses); + } catch (e) { + throw e; + } + this.paired_addresses = []; this.currentOutpoint = null; this.currentPcdCommitment = null; diff --git a/src/services/service.ts b/src/services/service.ts index 41d355a..ae6eacc 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -3,7 +3,7 @@ import { INotification } from '~/models/notification.model'; import { IProcess } from '~/models/process.model'; // import Database from './database'; import { initWebsocket, sendMessage } from '../websockets'; -import { ApiReturn, Member, Process } from '../../dist/pkg/sdk_client'; +import { ApiReturn, Member, PcdUpdates, Process, RoleDefinition } from '../../dist/pkg/sdk_client'; import ModalService from './modal.service'; import { navigate } from '../router'; import Database from './database.service'; @@ -16,7 +16,7 @@ export default class Services { private static initializing: Promise | null = null; private static instance: Services; private currentProcess: string | null = null; - private pendingUpdates: Record = {}; + private pendingUpdates: PcdUpdates | null = null; private currentUpdateMerkleRoot: string | null = null; private localAddress: string | null = null; private pairedAddresses: string[] = []; @@ -265,10 +265,10 @@ export default class Services { public getUpdateProposals(commitmentOutpoint: string) { try { - const proposals: ApiReturn = this.sdkClient.get_update_proposals(commitmentOutpoint); + const proposals: PcdUpdates = this.sdkClient.get_update_proposals(commitmentOutpoint); if (proposals.decrypted_pcds && proposals.decrypted_pcds.length != 0) { this.currentProcess = commitmentOutpoint; - this.pendingUpdates = proposals.decrypted_pcds; + this.pendingUpdates = proposals; } else { throw new Error('No pending proposals'); } @@ -315,8 +315,13 @@ export default class Services { throw e; } - // do we need to act? - updatedProcess.user_validation_required + if (updatedProcess.modified_state || updatedProcess.new_state) { + this.currentProcess = updatedProcess.commitment_tx; + + this.getUpdateProposals(this.currentProcess!); + + await this.evaluatePendingUpdates(); + } } if (apiReturn.commit_to_send) { @@ -343,16 +348,28 @@ export default class Services { } private async openConfirmationModal() { - if (this.pendingUpdates.length === 0) { - console.log('No pending updates'); + if (!this.pendingUpdates || this.pendingUpdates.modified_values.length === 0) { + console.log('No pending updates to validate'); } - try { - for (const [merkleRoot, actual_proposal] of Object.entries(this.pendingUpdates)) { - await this.routingInstance.openConfirmationModal(actual_proposal, this.currentProcess!, merkleRoot); + for (const value of this.pendingUpdates!.modified_values) { + if (value.notify_user) { + // TODO notification pop up + } + if (!value.need_validation) { + continue; + } + if (value.proof) { + // It seems we already validated that, check the proof and if valid just notify user + continue; + } + const actualProposal: Record = JSON.parse(value.new_value); + const merkleRoot: string = value.new_state_merkle_root; + try { + await this.routingInstance.openPairingConfirmationModal(actualProposal, this.currentProcess!, merkleRoot); + } catch (e) { + throw new Error(`${e}`); } - } catch (e) { - throw new Error(`${e}`); } } diff --git a/src/utils/sp-address.utils.ts b/src/utils/sp-address.utils.ts index 1c8b535..1b54708 100755 --- a/src/utils/sp-address.utils.ts +++ b/src/utils/sp-address.utils.ts @@ -161,18 +161,12 @@ async function onOkButtonClick() { // Create the process const relayAddress = await service.getRelayAddresses(); // Get one (or more?) relay addresses const createPairingProcessReturn = await service.createPairingProcess([addressInput], relayAddress[0], 1); - await service.handleApiReturn(createPairingProcessReturn); - + if (!createPairingProcessReturn.updated_process) { throw new Error('createPairingProcess returned an empty new process'); // This should never happen } - const commitmentOutpoint = createPairingProcessReturn.updated_process.commitment_tx; - - // We set the service to the process - service.getUpdateProposals(commitmentOutpoint); - - await service.evaluatePendingUpdates(); + await service.handleApiReturn(createPairingProcessReturn); } catch (e) { console.error(`onOkButtonClick error: ${e}`); }