diff --git a/package.json b/package.json index f0515ae..4e52ec3 100755 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "dist/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build_wasm": "wasm-pack build --out-dir ../ihm_client/pkg ../sdk_client --target bundler --dev", + "build_wasm": "wasm-pack build --out-dir ../ihm_client_dev2/pkg ../sdk_client --target bundler ", "start": "vite --host 0.0.0.0", "build": "tsc && vite build", "deploy": "sudo cp -r dist/* /var/www/html/", diff --git a/patch.patch b/patch.patch new file mode 100644 index 0000000..e988ff3 --- /dev/null +++ b/patch.patch @@ -0,0 +1,249 @@ +diff --git a/package.json b/package.json +index f0515ae..4e52ec3 100755 +--- a/package.json ++++ b/package.json +@@ -5,7 +5,7 @@ + "main": "dist/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", +- "build_wasm": "wasm-pack build --out-dir ../ihm_client/pkg ../sdk_client --target bundler --dev", ++ "build_wasm": "wasm-pack build --out-dir ../ihm_client_dev2/pkg ../sdk_client --target bundler ", + "start": "vite --host 0.0.0.0", + "build": "tsc && vite build", + "deploy": "sudo cp -r dist/* /var/www/html/", +diff --git a/src/pages/chat/chat.ts b/src/pages/chat/chat.ts +index 80493a0..2bd753a 100755 +--- a/src/pages/chat/chat.ts ++++ b/src/pages/chat/chat.ts +@@ -932,6 +932,8 @@ class ChatElement extends HTMLElement { + switch (tabType) { + case 'processes': + const processSet = await this.getProcessesWhereTheCurrentMemberIs(); ++ const db = await Database.getInstance(); ++ await db.updateMyProcesses(processSet); + await this.loadAllProcesses(processSet); + break; + case 'members': +diff --git a/src/service-workers/database.worker.js b/src/service-workers/database.worker.js +index 5753d1b..5c15bce 100755 +--- a/src/service-workers/database.worker.js ++++ b/src/service-workers/database.worker.js +@@ -27,9 +27,23 @@ self.addEventListener('message', async (event) => { + }); + }; + const scanMissingData = async () => { ++ console.log('Scanning for missing data...'); + const myProcesses = getProcesses(myProcessesId); + +- ++ let toDownload = []; ++ // Iterate on each process ++ for (const process of myProcesses) { ++ console.log(process); ++ // Iterate on states ++ for (const state of process.states) { ++ console.log(state); ++ // iterate on pcd_commitment ++ for (const hash of state.pcd_commitment) { ++ console.log(hash); ++ toDownload.push(hash); ++ } ++ } ++ } + + event.ports[0].postMessage({ + type: 'TO_DOWNLOAD', +diff --git a/src/services/database.service.ts b/src/services/database.service.ts +index 0544ccd..6e0d3c2 100755 +--- a/src/services/database.service.ts ++++ b/src/services/database.service.ts +@@ -220,7 +220,10 @@ export class Database { + }); + } + ++<<<<<<< Updated upstream + ++======= ++>>>>>>> Stashed changes + public updateMyProcesses(payload: { myProcessesId: string[] }): Promise { + return new Promise((resolve, reject) => { + // Check if the service worker is active +diff --git a/src/services/service.ts b/src/services/service.ts +index 7a2549d..01efad6 100755 +--- a/src/services/service.ts ++++ b/src/services/service.ts +@@ -302,25 +302,31 @@ export default class Services { + } + const myAddress: string = this.sdkClient.get_address(); + pairWith.push(myAddress); ++ const roles: Record = { ++ pairing: { ++ members: [{ sp_addresses: pairWith }], ++ validation_rules: [ ++ { ++ quorum: 1.0, ++ fields: ['description', 'counter'], ++ min_sig_member: 1.0, ++ }, ++ ], ++ storages: [storageUrl] ++ }, ++ }; + const pairingTemplate = { + description: 'pairing', + counter: 0, +- roles: { +- pairing: { +- members: [{ sp_addresses: pairWith }], +- validation_rules: [ +- { +- quorum: 1.0, +- fields: ['description', 'roles', 'counter'], +- min_sig_member: 1.0, +- }, +- ], +- storages: [storageUrl] +- }, +- }, + }; + try { +- return this.sdkClient.create_new_process(JSON.stringify(pairingTemplate), null, relayAddress, feeRate); ++ return this.sdkClient.create_new_process( ++ JSON.stringify(pairingTemplate), ++ JSON.stringify(roles), ++ null, ++ relayAddress, ++ feeRate ++ ); + } catch (e) { + throw new Error(`Creating process failed:, ${e}`); + } +@@ -605,6 +611,7 @@ export default class Services { + } + + public async handleApiReturn(apiReturn: ApiReturn) { ++ console.log(apiReturn); + if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.transaction.length != 0) { + await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send)); + await new Promise(r => setTimeout(r, 500)); +@@ -639,71 +646,51 @@ export default class Services { + } + } + +- setTimeout(async () => { +- if (apiReturn.updated_process) { +- const updatedProcess = apiReturn.updated_process; ++ if (apiReturn.updated_process) { ++ const updatedProcess = apiReturn.updated_process; ++ ++ const processId: string = updatedProcess.process_id; + +- const processId: string = updatedProcess.process_id; ++ // Save process to db ++ try { ++ await this.saveProcessToDb(processId, updatedProcess.current_process); ++ } catch (e) { ++ throw e; ++ } + +- // Save process to db ++ const isPaired = this.isPaired(); ++ ++ if (updatedProcess.diffs && updatedProcess.diffs.length != 0) { + try { +- await this.saveProcessToDb(processId, updatedProcess.current_process); ++ await this.saveDiffsToDb(updatedProcess.diffs); + } catch (e) { +- throw e; +- } +- +- const isPaired = this.isPaired(); +- +- if (updatedProcess.diffs && updatedProcess.diffs.length != 0) { +- const [updatedDiffs, retrievedValues] = await this.tryFetchDiffValue(updatedProcess.diffs); +- if (Object.entries(retrievedValues).length != 0) { +- const stateId = updatedDiffs[0].state_id; +- const processId = updatedDiffs[0].process_id; +- // We update the process with the value we retrieved +- const hashToValues = JSON.stringify(retrievedValues); +- const apiReturn = this.sdkClient.update_process_state(processId, stateId, hashToValues); +- await this.handleApiReturn(apiReturn); +- } else { +- try { +- await this.saveDiffsToDb(updatedDiffs); +- } catch (e) { +- throw e; +- } +- if (!isPaired) { +- await this.openPairingConfirmationModal(updatedDiffs); +- } +- } +- } +- +- +- if (updatedProcess.validated_state) { +- const responsePrdReturn = this.sdkClient.create_response_prd(processId, updatedProcess.validated_state); +- await this.handleApiReturn(responsePrdReturn); ++ console.error('Failed to save diffs to db:', e); + } + } +- +- if (apiReturn.commit_to_send) { +- const commit = apiReturn.commit_to_send; +- await this.sendCommitMessage(JSON.stringify(commit)); ++ if (!isPaired) { ++ console.log(updatedProcess); ++ await this.openPairingConfirmationModal(updatedProcess.current_process.states[0]); + } ++ } + +- if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length != 0) { +- await this.sendCipherMessages(apiReturn.ciphers_to_send); +- } +- }, 0); +- } ++ if (apiReturn.commit_to_send) { ++ const commit = apiReturn.commit_to_send; ++ await this.sendCommitMessage(JSON.stringify(commit)); ++ } + +- public async openPairingConfirmationModal(diffs: UserDiff[]) { +- const rolesDiff = diffs.find((diff) => diff.field === 'roles'); +- if (!rolesDiff) { +- throw new Error('Pairing process must have roles'); ++ if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length != 0) { ++ await this.sendCipherMessages(apiReturn.ciphers_to_send); + } +- const processId = rolesDiff.process_id; +- const stateId = rolesDiff.state_id; ++ } ++ ++ public async openPairingConfirmationModal(firstState: ProcessState) { ++ const roles = firstState.roles; ++ const processId = firstState.commited_in; ++ const stateId = firstState.state_id; + try { +- await this.routingInstance.openPairingConfirmationModal(rolesDiff.new_value, processId, stateId); ++ await this.routingInstance.openPairingConfirmationModal(roles, processId, stateId); + } catch (e) { +- throw new Error(`${e}`); ++ console.error(e); + } + } + +@@ -804,7 +791,6 @@ export default class Services { + try { + this.sdkClient.roles_contains_us(JSON.stringify(roles)); + } catch (e) { +- console.error(e); + return false; + } + +@@ -815,7 +801,6 @@ export default class Services { + try { + this.sdkClient.roles_contains_member(JSON.stringify(roles), member); + } catch (e) { +- console.error(e); + return false; + } + diff --git a/src/pages/chat/chat.ts b/src/pages/chat/chat.ts index 80493a0..2bd753a 100755 --- a/src/pages/chat/chat.ts +++ b/src/pages/chat/chat.ts @@ -932,6 +932,8 @@ class ChatElement extends HTMLElement { switch (tabType) { case 'processes': const processSet = await this.getProcessesWhereTheCurrentMemberIs(); + const db = await Database.getInstance(); + await db.updateMyProcesses(processSet); await this.loadAllProcesses(processSet); break; case 'members': diff --git a/src/service-workers/database.worker.js b/src/service-workers/database.worker.js index 5753d1b..5c15bce 100755 --- a/src/service-workers/database.worker.js +++ b/src/service-workers/database.worker.js @@ -27,9 +27,23 @@ self.addEventListener('message', async (event) => { }); }; const scanMissingData = async () => { + console.log('Scanning for missing data...'); const myProcesses = getProcesses(myProcessesId); - + let toDownload = []; + // Iterate on each process + for (const process of myProcesses) { + console.log(process); + // Iterate on states + for (const state of process.states) { + console.log(state); + // iterate on pcd_commitment + for (const hash of state.pcd_commitment) { + console.log(hash); + toDownload.push(hash); + } + } + } event.ports[0].postMessage({ type: 'TO_DOWNLOAD', diff --git a/src/services/database.service.ts b/src/services/database.service.ts index 0544ccd..0582fb2 100755 --- a/src/services/database.service.ts +++ b/src/services/database.service.ts @@ -162,14 +162,27 @@ export class Database { }; private handleUpdateProcessesResponse = async (event: MessageEvent) => { + const data = event.data; console.log('Received response from service worker (UPDATE_PROCESSES):', data); for (const process of data.data) { - - console.log("PROCESS: ", process); - + const hash = process.states.pcd_commitment[0]; + console.log("HASH: ", hash); + const diff = await this.requestStoreByIndex('diffs', 'byStateId', hash); + console.log("DIFF: ", diff); + if (diff && diff.new_value !== null) { + try { + const newState = JSON.parse(diff.new_value); + } catch (error) { + console.error('Error updating process:', error); + } + } else { + console.log("No diff found for process: ", process.id); + } } }; + // TODO : get the message from the service worker to the client + // TODO : get the message from the service worker to the client // we get an object, then we have to loop to look for the diffs // for each hash in INDEXEDB --> request for true or false @@ -220,7 +233,6 @@ export class Database { }); } - public updateMyProcesses(payload: { myProcessesId: string[] }): Promise { return new Promise((resolve, reject) => { // Check if the service worker is active diff --git a/src/services/service.ts b/src/services/service.ts index 7a2549d..01efad6 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -302,25 +302,31 @@ export default class Services { } const myAddress: string = this.sdkClient.get_address(); pairWith.push(myAddress); + const roles: Record = { + pairing: { + members: [{ sp_addresses: pairWith }], + validation_rules: [ + { + quorum: 1.0, + fields: ['description', 'counter'], + min_sig_member: 1.0, + }, + ], + storages: [storageUrl] + }, + }; const pairingTemplate = { description: 'pairing', counter: 0, - roles: { - pairing: { - members: [{ sp_addresses: pairWith }], - validation_rules: [ - { - quorum: 1.0, - fields: ['description', 'roles', 'counter'], - min_sig_member: 1.0, - }, - ], - storages: [storageUrl] - }, - }, }; try { - return this.sdkClient.create_new_process(JSON.stringify(pairingTemplate), null, relayAddress, feeRate); + return this.sdkClient.create_new_process( + JSON.stringify(pairingTemplate), + JSON.stringify(roles), + null, + relayAddress, + feeRate + ); } catch (e) { throw new Error(`Creating process failed:, ${e}`); } @@ -605,6 +611,7 @@ export default class Services { } public async handleApiReturn(apiReturn: ApiReturn) { + console.log(apiReturn); if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.transaction.length != 0) { await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send)); await new Promise(r => setTimeout(r, 500)); @@ -639,71 +646,51 @@ export default class Services { } } - setTimeout(async () => { - if (apiReturn.updated_process) { - const updatedProcess = apiReturn.updated_process; + if (apiReturn.updated_process) { + const updatedProcess = apiReturn.updated_process; - const processId: string = updatedProcess.process_id; + const processId: string = updatedProcess.process_id; - // Save process to db + // Save process to db + try { + await this.saveProcessToDb(processId, updatedProcess.current_process); + } catch (e) { + throw e; + } + + const isPaired = this.isPaired(); + + if (updatedProcess.diffs && updatedProcess.diffs.length != 0) { try { - await this.saveProcessToDb(processId, updatedProcess.current_process); + await this.saveDiffsToDb(updatedProcess.diffs); } catch (e) { - throw e; - } - - const isPaired = this.isPaired(); - - if (updatedProcess.diffs && updatedProcess.diffs.length != 0) { - const [updatedDiffs, retrievedValues] = await this.tryFetchDiffValue(updatedProcess.diffs); - if (Object.entries(retrievedValues).length != 0) { - const stateId = updatedDiffs[0].state_id; - const processId = updatedDiffs[0].process_id; - // We update the process with the value we retrieved - const hashToValues = JSON.stringify(retrievedValues); - const apiReturn = this.sdkClient.update_process_state(processId, stateId, hashToValues); - await this.handleApiReturn(apiReturn); - } else { - try { - await this.saveDiffsToDb(updatedDiffs); - } catch (e) { - throw e; - } - if (!isPaired) { - await this.openPairingConfirmationModal(updatedDiffs); - } - } - } - - - if (updatedProcess.validated_state) { - const responsePrdReturn = this.sdkClient.create_response_prd(processId, updatedProcess.validated_state); - await this.handleApiReturn(responsePrdReturn); + console.error('Failed to save diffs to db:', e); } } - - if (apiReturn.commit_to_send) { - const commit = apiReturn.commit_to_send; - await this.sendCommitMessage(JSON.stringify(commit)); + if (!isPaired) { + console.log(updatedProcess); + await this.openPairingConfirmationModal(updatedProcess.current_process.states[0]); } + } - if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length != 0) { - await this.sendCipherMessages(apiReturn.ciphers_to_send); - } - }, 0); + if (apiReturn.commit_to_send) { + const commit = apiReturn.commit_to_send; + await this.sendCommitMessage(JSON.stringify(commit)); + } + + if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length != 0) { + await this.sendCipherMessages(apiReturn.ciphers_to_send); + } } - public async openPairingConfirmationModal(diffs: UserDiff[]) { - 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.state_id; + public async openPairingConfirmationModal(firstState: ProcessState) { + const roles = firstState.roles; + const processId = firstState.commited_in; + const stateId = firstState.state_id; try { - await this.routingInstance.openPairingConfirmationModal(rolesDiff.new_value, processId, stateId); + await this.routingInstance.openPairingConfirmationModal(roles, processId, stateId); } catch (e) { - throw new Error(`${e}`); + console.error(e); } } @@ -804,7 +791,6 @@ export default class Services { try { this.sdkClient.roles_contains_us(JSON.stringify(roles)); } catch (e) { - console.error(e); return false; } @@ -815,7 +801,6 @@ export default class Services { try { this.sdkClient.roles_contains_member(JSON.stringify(roles), member); } catch (e) { - console.error(e); return false; }