diff --git a/src/services/modal.service.ts b/src/services/modal.service.ts index 0197416..e7da88c 100755 --- a/src/services/modal.service.ts +++ b/src/services/modal.service.ts @@ -10,8 +10,8 @@ import { interpolate } from '~/utils/html.utils'; export default class ModalService { private static instance: ModalService; - private currentPcdCommitment: string | null = null; - private currentOutpoint: string | null = null; + private stateId: string | null = null; + private processId: string | null = null; private constructor() {} private paired_addresses: string[] = []; @@ -85,7 +85,7 @@ export default class ModalService { } // this is kind of too specific for pairing though - public async openPairingConfirmationModal(roleDefinition: Record, commitmentTx: string, merkleRoot: string) { + public async openPairingConfirmationModal(roleDefinition: Record, processId: string, stateId: string) { // pairing specifics let members; if (roleDefinition['owner']) { @@ -110,8 +110,8 @@ export default class ModalService { } } } - this.currentOutpoint = commitmentTx; - this.currentPcdCommitment = merkleRoot; + this.processId = processId; + this.stateId = stateId; await this.injectModal(members); const modal = document.getElementById('modal'); if (modal) modal.style.display = 'flex'; @@ -144,9 +144,9 @@ export default class ModalService { if (modal) modal.style.display = 'none'; // We send the prd update - if (this.currentPcdCommitment) { + if (this.stateId && this.processId) { try { - const createPrdUpdateReturn = service.createPrdUpdate(this.currentPcdCommitment); + const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId); await service.handleApiReturn(createPrdUpdateReturn); } catch (e) { throw e; @@ -157,23 +157,24 @@ export default class ModalService { // We send confirmation that we validate the change try { - const approveChangeReturn = service.approveChange(this.currentPcdCommitment!); + const approveChangeReturn = service.approveChange(this.processId!, this.stateId!); await service.handleApiReturn(approveChangeReturn); } catch (e) { throw e; } - try { - service.pairDevice(this.paired_addresses); - } catch (e) { - throw e; - } + // try { + // service.pairDevice(this.paired_addresses); + // } catch (e) { + // throw e; + // } this.paired_addresses = []; - this.currentOutpoint = null; - this.currentPcdCommitment = null; - const newDevice = service.dumpDevice(); - await service.saveDevice(newDevice); + this.processId = null; + this.stateId = null; + const newDevice = service.dumpDeviceFromMemory(); + console.log(newDevice); + await service.saveDeviceInDatabase(newDevice); navigate('process'); } diff --git a/src/services/service.ts b/src/services/service.ts index 5077178..8231481 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -351,24 +351,67 @@ export default class Services { if (apiReturn.updated_process) { const updatedProcess = apiReturn.updated_process; + const processId: string = updatedProcess.commitment_tx; + // Save process to storage try { - await this.saveProcess(updatedProcess.commitment_tx, updatedProcess.current_process); + await this.saveProcess(processId, updatedProcess.current_process); } catch (e) { throw e; } + const isPaired = this.isPaired(); + if (updatedProcess.new_diffs.length != 0) { - try { - this.saveDiffs(updatedProcess.new_diffs); - } catch (e) { - throw e; + // We check if we have the value in diffs + const diffs = updatedProcess.new_diffs; + const stateId = diffs[0].new_state_merkle_root; + let retrievedValues: Record = {}; + for (const diff of diffs) { + // Check if `new_value` is missing + if (!diff.new_value) { + const hash = diff.value_commitment; + if (!hash) { + console.error('No commitment for diff'); + } + try { + const res = await this.fetchValueFromStorage(hash); + if (!res) { + console.error('Failed to fetch value for hash', hash); + } else { + diff.new_value = res['value']; + retrievedValues[hash] = res['value']; + } + } catch (error) { + console.error(`Failed to fetch new_value for diff: ${JSON.stringify(diff)}`, error); + } + } + } + + if (Object.entries(retrievedValues).length != 0) { + // We update the process with the value we retrieved + const hashToValues = JSON.stringify(retrievedValues); + console.log(hashToValues); + const apiReturn = this.sdkClient.update_process_state(processId, stateId, hashToValues); + await this.handleApiReturn(apiReturn); + } + + if (isPaired) { + try { + this.saveDiffs(diffs); + } catch (e) { + throw e; + } + } else { + await this.openPairingConfirmationModal(diffs, updatedProcess.up_to_date_roles); } } + if (updatedProcess.modified_state) { // For now it can only mean we added a validation token to an existing state // Not sure what action to take + console.log('modified state:', updatedProcess.modified_state); } } @@ -383,41 +426,17 @@ export default class Services { }, 0); } - public async evaluatePendingUpdates() { - if (!this.currentProcess) { - throw new Error('No current process'); + public async openPairingConfirmationModal(diffs: UserDiff[], roles: Record) { + const rolesDiff = diffs.find((diff) => diff.field === 'roles'); + if (!rolesDiff) { + throw new Error('Pairing process must have roles'); } - + const processId = rolesDiff.process_id; + const stateId = rolesDiff.new_state_merkle_root; try { - await this.openConfirmationModal(); + await this.routingInstance.openPairingConfirmationModal(roles, processId, stateId); } catch (e) { - throw new Error(`Error while evaluating pending updates for process ${this.currentProcess}: ${e}`); - } - } - - private async openConfirmationModal() { - if (!this.pendingUpdates || this.pendingUpdates.modified_values.length === 0) { - console.log('No pending updates to validate'); - } - - 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}`); - } + throw new Error(`${e}`); } }