Compare commits

...

5 Commits

9 changed files with 754 additions and 282 deletions

View File

@ -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/",

249
patch.patch Normal file
View File

@ -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<void> {
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<string, RoleDefinition> = {
+ 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;
}

203
process.diff Normal file
View File

@ -0,0 +1,203 @@
diff --git a/src/pages/process/process.ts b/src/pages/process/process.ts
index 27c1301..a4f8943 100755
--- a/src/pages/process/process.ts
+++ b/src/pages/process/process.ts
@@ -2,9 +2,15 @@ import { addSubscription } from '../../utils/subscription.utils';
import Services from '../../services/service';
import { getCorrectDOM } from '~/utils/html.utils';
import { Process } from 'pkg/sdk_client';
+import chatStyle from '../../../public/style/chat.css?inline';
+import { Database } from '../../services/database.service';
+
+let myProcesses = new Set();
+let allProcesses = new Set();
// Initialize function, create initial tokens with itens that are already selected by the user
export async function init() {
+
const container = getCorrectDOM('process-list-4nk-component') as HTMLElement;
const element = container.querySelector('select') as HTMLSelectElement;
// Create div that wroaps all the elements inside (select, elements selected, search div) to put select inside
@@ -43,6 +49,23 @@ export async function init() {
wrapper.appendChild(search_div);
addPlaceholder(wrapper);
+
+ await loadAllProcesses();
+
+ const database = await Database.getInstance();
+
+ try {
+ await database.updateMyProcesses({ myProcessesId: Array.from(myProcesses) });
+ const updateProcesses = await database.updateMyProcesses({ myProcessesId: Array.from(myProcesses) });
+ console.log("UPDATE PROCESSES d'INIT: ", updateProcesses);
+ } catch (error) {
+ console.error("Error updating my processes:", error);
+ }
+
+
+
+
+
}
function removePlaceholder(wrapper: HTMLElement) {
@@ -155,62 +178,39 @@ function clearAutocompleteList(select: HTMLSelectElement) {
if (autocomplete_list) autocomplete_list.innerHTML = '';
}
-// Populate the autocomplete list following a given query from the user
-function populateAutocompleteList(select: HTMLSelectElement, query: string, dropdown = false) {
+async function populateAutocompleteList(select: HTMLSelectElement, query: string, dropdown = false) {
const { autocomplete_options } = getOptions(select);
- let options_to_show;
+ let options_to_show = [];
- if (dropdown) {
- let messagingCounter = 1;
- const messagingOptions = select.querySelectorAll('option[value="messaging"]');
-
- options_to_show = autocomplete_options.map(option => {
- if (option === 'messaging') {
- // Récupérer l'élément option correspondant au compteur actuel
- const currentOption = messagingOptions[messagingCounter - 1];
- const processId = currentOption?.getAttribute('data-process-id');
- console.log(`Mapping messaging ${messagingCounter} with processId:`, processId);
-
- const optionText = `messaging ${messagingCounter}`;
- messagingCounter++;
-
- // Stocker le processId dans un attribut data sur le select
- select.setAttribute(`data-messaging-id-${messagingCounter - 1}`, processId || '');
-
- return optionText;
- }
- return option;
- });
- } else {
- options_to_show = autocomplete(query, autocomplete_options);
- }
+ console.log(myProcesses);
+
+ const mineArray = Array.from(myProcesses);
+ const allArray = Array.from(allProcesses).filter(id => !myProcesses.has(id));
const wrapper = select.parentNode;
const input_search = wrapper?.querySelector('.search-container');
const autocomplete_list = wrapper?.querySelector('.autocomplete-list');
if (autocomplete_list) autocomplete_list.innerHTML = '';
- const result_size = options_to_show.length;
- if (result_size == 1) {
+ const addProcessToList = (processId:string, isMine: boolean) => {
const li = document.createElement('li');
- li.innerText = options_to_show[0];
- li.setAttribute('data-value', options_to_show[0]);
+ li.innerText = processId;
+ li.setAttribute("data-value", processId);
+
+ if (isMine) {
+ li.classList.add("my-process");
+ li.style.cssText = `color: var(--accent-color)`;
+ }
+
if (li) addSubscription(li, 'click', selectOption);
autocomplete_list?.appendChild(li);
- if (query.length == options_to_show[0].length) {
- const event = new Event('click');
- li.dispatchEvent(event);
- }
- } else if (result_size > 1) {
- for (let i = 0; i < result_size; i++) {
- const li = document.createElement('li');
- li.innerText = options_to_show[i];
- li.setAttribute('data-value', options_to_show[i]);
- if (li) addSubscription(li, 'click', selectOption);
- autocomplete_list?.appendChild(li);
- }
- } else {
+ };
+
+ mineArray.forEach(processId => addProcessToList(processId, true));
+ allArray.forEach(processId => addProcessToList(processId, false));
+
+ if (myProcesses.size === 0 && allProcesses.size === 0) {
const li = document.createElement('li');
li.classList.add('not-cursor');
li.innerText = 'No options found';
@@ -392,6 +392,20 @@ addSubscription(document, 'click', () => {
}
});
+async function loadAllProcesses() {
+ try {
+ const [allProcessesNew, myProcessesNew] = await Promise.all([
+ getProcesses(),
+ getMyProcesses()
+ ]);
+
+ myProcesses = myProcesses.union(myProcessesNew);
+ allProcesses = allProcesses.union(allProcessesNew);
+ } catch (error) {
+ console.error("Error loading processes:", error);
+ }
+}
+
async function showSelectedProcess(elem: MouseEvent) {
const container = getCorrectDOM('process-list-4nk-component') as HTMLElement;
@@ -539,43 +553,36 @@ async function getDescription(processId: string, process: Process): Promise<stri
return null;
}
-async function getProcesses(): Promise<any[]> {
+async function getProcesses(): Promise<Set<string>> {
const service = await Services.getInstance();
const processes = await service.getProcesses();
+ const processIds = new Set<string>(Object.keys(processes));
- const res = Object.entries(processes).map(([key, value]) => ({
- key,
- value,
- }));
-
- return res;
+ return processIds;
}
-async function getMyProcesses() {
+async function getMyProcesses(): Promise<Set<string>> {
const service = await Services.getInstance();
try {
const processes = await service.getProcesses();
- const userProcessSet = new Set();
-
+ const userProcessSet = new Set<string>();
+
for (const [processId, process] of Object.entries(processes)) {
let roles;
try {
- roles = await this.getRoles(process);
+ roles = await service.getRoles(process);
if (!roles) {
roles = await process.states[0].encrypted_pcd.roles;
}
+ console.log("ROLES: ", roles);
+
+ const hasCurrentUser = service.rolesContainsUs(roles);
- const hasCurrentUser = Object.values(roles).some(role =>
- service.rolesContainsUs(role)
- );
-
if (hasCurrentUser) {
userProcessSet.add(processId);
}
-
} catch (e) {
continue;
- console.error(`Error processing process ${processId}:`, e);
}
}

View File

@ -383,7 +383,7 @@ class ChatElement extends HTMLElement {
const processRoles = this.processRoles;
const selectedMember = this.selectedMember;
for (const child of children) {
const roles = await this.getRoles(JSON.parse(child));
const roles = await service.getRoles(JSON.parse(child));
// Check that we and the other members are in the role
if (!service.isChildRole(processRoles, roles)) {
console.error('Child process roles are not a subset of parent')
@ -489,7 +489,7 @@ class ChatElement extends HTMLElement {
if (description !== "dm") {
continue;
}
const roles = await this.getRoles(process);
const roles = await service.getRoles(process);
if (!service.rolesContainsMember(roles, recipientAddresses)) {
console.error('Member is not part of the process');
continue;
@ -911,44 +911,6 @@ class ChatElement extends HTMLElement {
roleElement.appendChild(memberList);
}
async getRoles(process: Process): Promise<any | null> {
const service = await Services.getInstance();
// Get the `commited_in` value of the last state and remove it from the array
const currentCommitedIn = process.states.pop()?.commited_in;
if (currentCommitedIn === undefined) {
return null; // No states available
}
// Find the last state where `commited_in` is different
let lastDifferentState = process.states.findLast(
state => state.commited_in !== currentCommitedIn
);
if (!lastDifferentState) {
// It means that we only have one state that is not commited yet, that can happen with process we just created
// let's assume that the right description is in the last concurrent state and not handle the (arguably rare) case where we have multiple concurrent states on a creation
lastDifferentState = process.states.pop();
}
if (!lastDifferentState || !lastDifferentState.pcd_commitment) {
return null;
}
// Take the roles out of the state
const roles = lastDifferentState!.pcd_commitment['roles'];
if (roles) {
const userDiff = await service.getDiffByValue(roles);
if (userDiff) {
console.log("Successfully retrieved userDiff:", userDiff);
return userDiff.new_value;
}
}
return null;
}
private async switchTab(tabType: string, tabs: NodeListOf<Element>) {
// Mettre à jour les classes des onglets
tabs.forEach(tab => {
@ -970,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':
@ -1046,7 +1010,7 @@ class ChatElement extends HTMLElement {
const oneProcess = process.states[0].commited_in;
let roles;
try {
roles = await this.getRoles(process);
roles = await service.getRoles(process);
if (!roles) {
roles = await process.states[0].encrypted_pcd.roles;
}
@ -1421,7 +1385,7 @@ class ChatElement extends HTMLElement {
for (const [processId, process] of Object.entries(processes)) {
let roles;
try {
roles = await this.getRoles(process);
roles = await service.getRoles(process);
if (!roles) {
roles = await process.states[0].encrypted_pcd.roles;
}
@ -1483,7 +1447,7 @@ class ChatElement extends HTMLElement {
const service = await Services.getInstance();
const process = await service.getProcess(this.processId);
const roles = await this.getRoles(process);
const roles = await service.getRoles(process);
if (roles === null) {
console.error('no roles in process');
return;

View File

@ -2,9 +2,15 @@ import { addSubscription } from '../../utils/subscription.utils';
import Services from '../../services/service';
import { getCorrectDOM } from '~/utils/html.utils';
import { Process } from 'pkg/sdk_client';
import chatStyle from '../../../public/style/chat.css?inline';
import { Database } from '../../services/database.service';
let myProcesses = new Set();
let allProcesses = new Set();
// Initialize function, create initial tokens with itens that are already selected by the user
export async function init() {
const container = getCorrectDOM('process-list-4nk-component') as HTMLElement;
const element = container.querySelector('select') as HTMLSelectElement;
// Create div that wroaps all the elements inside (select, elements selected, search div) to put select inside
@ -43,6 +49,16 @@ export async function init() {
wrapper.appendChild(search_div);
addPlaceholder(wrapper);
await loadAllProcesses();
const database = await Database.getInstance();
try {
await database.updateMyProcesses({ myProcessesId: Array.from(myProcesses) });
} catch (error) {
console.error("Error updating my processes:", error);
}
}
function removePlaceholder(wrapper: HTMLElement) {
@ -155,62 +171,39 @@ function clearAutocompleteList(select: HTMLSelectElement) {
if (autocomplete_list) autocomplete_list.innerHTML = '';
}
// Populate the autocomplete list following a given query from the user
function populateAutocompleteList(select: HTMLSelectElement, query: string, dropdown = false) {
async function populateAutocompleteList(select: HTMLSelectElement, query: string, dropdown = false) {
const { autocomplete_options } = getOptions(select);
let options_to_show;
let options_to_show = [];
if (dropdown) {
let messagingCounter = 1;
const messagingOptions = select.querySelectorAll('option[value="messaging"]');
options_to_show = autocomplete_options.map(option => {
if (option === 'messaging') {
// Récupérer l'élément option correspondant au compteur actuel
const currentOption = messagingOptions[messagingCounter - 1];
const processId = currentOption?.getAttribute('data-process-id');
console.log(`Mapping messaging ${messagingCounter} with processId:`, processId);
const optionText = `messaging ${messagingCounter}`;
messagingCounter++;
// Stocker le processId dans un attribut data sur le select
select.setAttribute(`data-messaging-id-${messagingCounter - 1}`, processId || '');
return optionText;
}
return option;
});
} else {
options_to_show = autocomplete(query, autocomplete_options);
}
console.log(myProcesses);
const mineArray = Array.from(myProcesses);
const allArray = Array.from(allProcesses).filter(id => !myProcesses.has(id));
const wrapper = select.parentNode;
const input_search = wrapper?.querySelector('.search-container');
const autocomplete_list = wrapper?.querySelector('.autocomplete-list');
if (autocomplete_list) autocomplete_list.innerHTML = '';
const result_size = options_to_show.length;
if (result_size == 1) {
const addProcessToList = (processId:string, isMine: boolean) => {
const li = document.createElement('li');
li.innerText = options_to_show[0];
li.setAttribute('data-value', options_to_show[0]);
li.innerText = processId;
li.setAttribute("data-value", processId);
if (isMine) {
li.classList.add("my-process");
li.style.cssText = `color: var(--accent-color)`;
}
if (li) addSubscription(li, 'click', selectOption);
autocomplete_list?.appendChild(li);
if (query.length == options_to_show[0].length) {
const event = new Event('click');
li.dispatchEvent(event);
}
} else if (result_size > 1) {
for (let i = 0; i < result_size; i++) {
const li = document.createElement('li');
li.innerText = options_to_show[i];
li.setAttribute('data-value', options_to_show[i]);
if (li) addSubscription(li, 'click', selectOption);
autocomplete_list?.appendChild(li);
}
} else {
};
mineArray.forEach(processId => addProcessToList(processId, true));
allArray.forEach(processId => addProcessToList(processId, false));
if (myProcesses.size === 0 && allProcesses.size === 0) {
const li = document.createElement('li');
li.classList.add('not-cursor');
li.innerText = 'No options found';
@ -392,6 +385,20 @@ addSubscription(document, 'click', () => {
}
});
async function loadAllProcesses() {
try {
const [allProcessesNew, myProcessesNew] = await Promise.all([
getProcesses(),
getMyProcesses()
]);
myProcesses = myProcesses.union(myProcessesNew);
allProcesses = allProcesses.union(allProcessesNew);
} catch (error) {
console.error("Error loading processes:", error);
}
}
async function showSelectedProcess(elem: MouseEvent) {
const container = getCorrectDOM('process-list-4nk-component') as HTMLElement;
@ -539,43 +546,33 @@ async function getDescription(processId: string, process: Process): Promise<stri
return null;
}
async function getProcesses(): Promise<any[]> {
async function getProcesses(): Promise<Set<string>> {
const service = await Services.getInstance();
const processes = await service.getProcesses();
const processIds = new Set<string>(Object.keys(processes));
const res = Object.entries(processes).map(([key, value]) => ({
key,
value,
}));
return res;
return processIds;
}
async function getMyProcesses() {
async function getMyProcesses(): Promise<Set<string>> {
const service = await Services.getInstance();
try {
const processes = await service.getProcesses();
const userProcessSet = new Set();
const userProcessSet = new Set<string>();
for (const [processId, process] of Object.entries(processes)) {
let roles;
try {
roles = await this.getRoles(process);
if (!roles) {
roles = await process.states[0].encrypted_pcd.roles;
}
roles = await service.getRoles(process);
// console.log("ROLES: ", roles);
const hasCurrentUser = service.rolesContainsUs(roles);
const hasCurrentUser = Object.values(roles).some(role =>
service.rolesContainsUs(role)
);
if (hasCurrentUser) {
userProcessSet.add(processId);
}
} catch (e) {
continue;
console.error(`Error processing process ${processId}:`, e);
}
}

View File

@ -1,3 +1,6 @@
let processesToScan = new Set();
let toDownload = [];
self.addEventListener('install', (event) => {
event.waitUntil(self.skipWaiting()); // Activate worker immediately
});
@ -23,64 +26,54 @@ self.addEventListener('message', async (event) => {
data: itemsWithFlag,
});
};
const scanMissingData = async () => {
console.log('Scanning for missing data...');
const myProcesses = getProcesses(processesToScan);
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',
data: toDownload,
});
}
fetchNotifications();
setInterval(fetchNotifications, 2 * 60 * 1000);
scanMissingData();
setInterval(scanMissingData, 2 * 1000);
}
if (data.type === 'SCAN_PROCESS') {
if (data.type === 'UPDATE_PROCESSES') {
try {
const { myProcessesId } = data.payload;
const db = await openDatabase();
// Créer un tableau pour stocker toutes les promesses de processus
const processPromises = myProcessesId.map(async (processId) => {
// Récupérer le processus
const process = await new Promise((resolve, reject) => {
const tx = db.transaction('processes', 'readonly');
const store = tx.objectStore('processes');
const request = store.get(processId);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
if (!process || !process.states || process.states.length === 0) {
throw new Error(`Process ${processId} not found or invalid`);
if (myProcessesId && myProcessesId.length != 0) {
for (const processId of myProcessesId) {
if (processesToScan) {
processesToScan.add(processId);
} else {
throw new Error('No processesToScan');
}
}
// Récupérer les diffs pour chaque état
const diffPromises = process.states.map(async (state) => {
return new Promise((resolve, reject) => {
const tx = db.transaction('diffs', 'readonly');
const store = tx.objectStore('diffs');
for (const hash of state.pcd_commitment) {
const request = store.get(hash);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
}
});
});
const diffs = await Promise.all(diffPromises);
process.diffs = diffs.filter(diff => diff != null);
return process;
});
const results = await Promise.all(processPromises);
event.ports[0].postMessage({
status: 'success',
message: 'All processes scanned',
data: results
});
console.log(processesToScan);
} else {
throw new Error('Empty processIds list');
}
} catch (error) {
event.ports[0].postMessage({ status: 'error', message: error.message });
}
setInterval(fetchNotifications, 2 * 1000);
}
if (data.type === 'ADD_OBJECT') {
@ -123,31 +116,55 @@ async function openDatabase() {
// Function to get all processes because it is asynchronous
async function getAllProcesses() {
const db = await openDatabase();
return new Promise((resolve, reject) => {
if (!db) {
reject(new Error('Database is not available'));
return;
}
const tx = db.transaction('processes', 'readonly');
const store = tx.objectStore('processes');
// const request = store.openCursor();
// const processes = [];
const request = store.getAll();
request.onsuccess = (event) => {
// const cursor = event.target.result;
// if (cursor) {
// processes.push({ key: cursor.key, ...cursor.value });
// cursor.continue();
// } else {
// resolve(processes);
// }
const allProcesses = store.getAll();
resolve(allProcesses);
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = (event) => {
reject(event.target.error);
request.onerror = () => {
reject(request.error);
};
});
};
async function getAllItemsWithFlag() {
async function getProcesses(processIds) {
if (!processIds || processIds.length === 0) {
return [];
}
const db = await openDatabase();
if (!db) {
throw new Error('Database is not available');
}
const tx = db.transaction('processes', 'readonly');
const store = tx.objectStore('processes');
const requests = processIds.map((processId) => {
return new Promise((resolve) => {
const request = store.get(processId);
request.onsuccess = () => resolve(request.result);
request.onerror = () => {
console.error(`Error fetching process ${processId}:`, request.error);
resolve(undefined);
};
});
});
const results = await Promise.all(requests);
return results.filter(result => result !== undefined);
}
async function getAllDiffsNeedValidation() {
const db = await openDatabase();
const allProcesses = await getAllProcesses();

View File

@ -1,13 +1,11 @@
import Services from './service';
class Database {
export class Database {
private static instance: Database;
private db: IDBDatabase | null = null;
private dbName: string = '4nk';
private dbVersion: number = 1;
private serviceWorkerRegistration: ServiceWorkerRegistration | null = null;
private messageChannel: MessageChannel = new MessageChannel();
private messageChannelForGet: MessageChannel = new MessageChannel();
private storeDefinitions = {
AnkLabels: {
name: 'labels',
@ -121,14 +119,15 @@ class Database {
this.serviceWorkerRegistration = registration
await this.checkForUpdates();
// Set up the message channels
this.messageChannel.port1.onmessage = this.handleAddObjectResponse;
this.messageChannelForGet.port1.onmessage = this.handleGetObjectResponse;
registration.active?.postMessage(
const messageChannel = new MessageChannel();
messageChannel.port1.onmessage = this.handleAddObjectResponse;
const readyRegistration = await navigator.serviceWorker.ready;
readyRegistration.active?.postMessage(
{
type: 'START',
},
[this.messageChannel.port2],
[messageChannel.port2],
);
// Optionally, initialize service worker with some data
} catch (error) {
@ -162,10 +161,42 @@ 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) {
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
// if the diffs is found, we have to add the diff to the object
// we have to do that for each object
private handleGetObjectResponse = (event: MessageEvent) => {
console.log('Received response from service worker (GET_OBJECT):', event.data);
};
public addObject(payload: { storeName: string; object: any; key: any }): Promise<void> {
return new Promise((resolve, reject) => {
// Check if the service worker is active
@ -201,6 +232,44 @@ class Database {
}
});
}
public updateMyProcesses(payload: { myProcessesId: string[] }): Promise<void> {
return new Promise((resolve, reject) => {
// Check if the service worker is active
if (!this.serviceWorkerRegistration?.active) {
reject(new Error('Service worker is not active'));
return;
}
// Create a message channel for communication
const messageChannel = new MessageChannel();
// Handle the response from the service worker
messageChannel.port1.onmessage = (event) => {
if (event.data.status === 'success') {
resolve();
} else {
const error = event.data.message;
reject(new Error(error || 'Unknown error occurred while scanning our processes'));
}
};
try {
console.log('Sending UPDATE_PROCESSES msg with payload', payload);
this.serviceWorkerRegistration.active.postMessage(
{
type: 'UPDATE_PROCESSES',
payload,
},
[messageChannel.port2],
);
} catch (error) {
reject(new Error(`Failed to send message to service worker: ${error}`));
}
});
}
public async getObject(storeName: string, key: string): Promise<any | null> {
const db = await this.getDb();

View File

@ -95,11 +95,11 @@ export default class ModalService {
public async openPairingConfirmationModal(roleDefinition: Record<string, RoleDefinition>, processId: string, stateId: string) {
let members;
if (roleDefinition['owner']) {
const owner = roleDefinition['owner'];
if (roleDefinition['pairing']) {
const owner = roleDefinition['pairing'];
members = owner.members;
} else {
throw new Error('No "owner" role');
throw new Error('No "pairing" role');
}
if (members.length != 1) {

View File

@ -302,25 +302,31 @@ export default class Services {
}
const myAddress: string = this.sdkClient.get_address();
pairWith.push(myAddress);
const roles: Record<string, RoleDefinition> = {
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}`);
}
@ -524,20 +530,20 @@ export default class Services {
// get the process
try {
const process = await this.getProcess(diff.process_id);
const state = process.states.find(state => state.state_id === diff.state_id);
if (state) {
// Now we return the encrypted value for that field
const cipher = state.encrypted_pcd[diff.field];
if (cipher) {
return cipher;
} else {
console.error('Failed to get encrypted value');
}
}
} catch (e) {
console.error('Failed to get process:', e);
return null;
}
const state = process.states.find(state => state.state_id === diff.state_id);
if (state) {
// Now we return the encrypted value for that field
const cipher = state.encrypted_pcd[diff.field];
if (cipher) {
return cipher;
} else {
console.error('Failed to get encrypted value');
}
}
return null;
}
@ -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;
}
@ -1280,37 +1265,25 @@ export default class Services {
}
public async getRoles(process: Process): Promise<any | null> {
public async getRoles(process: Process): Promise<Record<string, RoleDefinition>> {
const currentCommitedIn = process.states.pop()?.commited_in;
if (currentCommitedIn === undefined) {
return null;
return {};
}
let lastDifferentState = process.states.findLast(
state => state.commited_in !== currentCommitedIn
state => state.commited_in !== currentCommitedIn
);
if (!lastDifferentState) {
lastDifferentState = process.states.pop();
lastDifferentState = process.states.pop();
}
if (!lastDifferentState || !lastDifferentState.pcd_commitment) {
return null;
if (lastDifferentState && lastDifferentState.roles) {
return lastDifferentState!.roles;
} else {
return {};
}
const roles = lastDifferentState!.pcd_commitment['roles'];
if (roles) {
const userDiff = await this.getDiffByValue(roles);
if (userDiff) {
console.log("Successfully retrieved userDiff:", userDiff);
return userDiff.new_value;
}
}
return null;
}
}