Refactor pairing confirmation modal

This commit is contained in:
Sosthene 2024-12-18 00:00:51 +01:00
parent aff12a4007
commit 5e1b22d302
2 changed files with 74 additions and 54 deletions

View File

@ -10,8 +10,8 @@ import { interpolate } from '~/utils/html.utils';
export default class ModalService { export default class ModalService {
private static instance: ModalService; private static instance: ModalService;
private currentPcdCommitment: string | null = null; private stateId: string | null = null;
private currentOutpoint: string | null = null; private processId: string | null = null;
private constructor() {} private constructor() {}
private paired_addresses: string[] = []; private paired_addresses: string[] = [];
@ -85,7 +85,7 @@ export default class ModalService {
} }
// this is kind of too specific for pairing though // this is kind of too specific for pairing though
public async openPairingConfirmationModal(roleDefinition: Record<string, RoleDefinition>, commitmentTx: string, merkleRoot: string) { public async openPairingConfirmationModal(roleDefinition: Record<string, RoleDefinition>, processId: string, stateId: string) {
// pairing specifics // pairing specifics
let members; let members;
if (roleDefinition['owner']) { if (roleDefinition['owner']) {
@ -110,8 +110,8 @@ export default class ModalService {
} }
} }
} }
this.currentOutpoint = commitmentTx; this.processId = processId;
this.currentPcdCommitment = merkleRoot; this.stateId = stateId;
await this.injectModal(members); await this.injectModal(members);
const modal = document.getElementById('modal'); const modal = document.getElementById('modal');
if (modal) modal.style.display = 'flex'; if (modal) modal.style.display = 'flex';
@ -144,9 +144,9 @@ export default class ModalService {
if (modal) modal.style.display = 'none'; if (modal) modal.style.display = 'none';
// We send the prd update // We send the prd update
if (this.currentPcdCommitment) { if (this.stateId && this.processId) {
try { try {
const createPrdUpdateReturn = service.createPrdUpdate(this.currentPcdCommitment); const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId);
await service.handleApiReturn(createPrdUpdateReturn); await service.handleApiReturn(createPrdUpdateReturn);
} catch (e) { } catch (e) {
throw e; throw e;
@ -157,23 +157,24 @@ export default class ModalService {
// We send confirmation that we validate the change // We send confirmation that we validate the change
try { try {
const approveChangeReturn = service.approveChange(this.currentPcdCommitment!); const approveChangeReturn = service.approveChange(this.processId!, this.stateId!);
await service.handleApiReturn(approveChangeReturn); await service.handleApiReturn(approveChangeReturn);
} catch (e) { } catch (e) {
throw e; throw e;
} }
try { // try {
service.pairDevice(this.paired_addresses); // service.pairDevice(this.paired_addresses);
} catch (e) { // } catch (e) {
throw e; // throw e;
} // }
this.paired_addresses = []; this.paired_addresses = [];
this.currentOutpoint = null; this.processId = null;
this.currentPcdCommitment = null; this.stateId = null;
const newDevice = service.dumpDevice(); const newDevice = service.dumpDeviceFromMemory();
await service.saveDevice(newDevice); console.log(newDevice);
await service.saveDeviceInDatabase(newDevice);
navigate('process'); navigate('process');
} }

View File

@ -351,24 +351,67 @@ export default class Services {
if (apiReturn.updated_process) { if (apiReturn.updated_process) {
const updatedProcess = apiReturn.updated_process; const updatedProcess = apiReturn.updated_process;
const processId: string = updatedProcess.commitment_tx;
// Save process to storage // Save process to storage
try { try {
await this.saveProcess(updatedProcess.commitment_tx, updatedProcess.current_process); await this.saveProcess(processId, updatedProcess.current_process);
} catch (e) { } catch (e) {
throw e; throw e;
} }
const isPaired = this.isPaired();
if (updatedProcess.new_diffs.length != 0) { if (updatedProcess.new_diffs.length != 0) {
// 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<string, string> = {};
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 { try {
this.saveDiffs(updatedProcess.new_diffs); 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) { } catch (e) {
throw e; throw e;
} }
} else {
await this.openPairingConfirmationModal(diffs, updatedProcess.up_to_date_roles);
} }
}
if (updatedProcess.modified_state) { if (updatedProcess.modified_state) {
// For now it can only mean we added a validation token to an existing state // For now it can only mean we added a validation token to an existing state
// Not sure what action to take // Not sure what action to take
console.log('modified state:', updatedProcess.modified_state);
} }
} }
@ -383,43 +426,19 @@ export default class Services {
}, 0); }, 0);
} }
public async evaluatePendingUpdates() { public async openPairingConfirmationModal(diffs: UserDiff[], roles: Record<string, RoleDefinition>) {
if (!this.currentProcess) { const rolesDiff = diffs.find((diff) => diff.field === 'roles');
throw new Error('No current process'); if (!rolesDiff) {
throw new Error('Pairing process must have roles');
} }
const processId = rolesDiff.process_id;
const stateId = rolesDiff.new_state_merkle_root;
try { 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<string, RoleDefinition> = JSON.parse(value.new_value);
const merkleRoot: string = value.new_state_merkle_root;
try {
await this.routingInstance.openPairingConfirmationModal(actualProposal, this.currentProcess!, merkleRoot);
} catch (e) { } catch (e) {
throw new Error(`${e}`); throw new Error(`${e}`);
} }
} }
}
pairDevice(spAddressList: string[]) { pairDevice(spAddressList: string[]) {
if (this.currentProcess) { if (this.currentProcess) {