Merge branch 'fix_missing_human_readable' into dev

This commit is contained in:
Sosthene 2025-08-08 08:25:57 +02:00
commit 8541427b87

View File

@ -82,17 +82,27 @@ export default class Services {
/** /**
* Calls `this.addWebsocketConnection` for each `wsurl` in relayAddresses. * Calls `this.addWebsocketConnection` for each `wsurl` in relayAddresses.
* Waits for at least one handshake message before returning.
*/ */
public async connectAllRelays(): Promise<void> { public async connectAllRelays(): Promise<void> {
const connectedUrls: string[] = [];
// Connect to all relays
for (const wsurl of Object.keys(this.relayAddresses)) { for (const wsurl of Object.keys(this.relayAddresses)) {
try { try {
console.log(`Connecting to: ${wsurl}`); console.log(`Connecting to: ${wsurl}`);
await this.addWebsocketConnection(wsurl); await this.addWebsocketConnection(wsurl);
connectedUrls.push(wsurl);
console.log(`Successfully connected to: ${wsurl}`); console.log(`Successfully connected to: ${wsurl}`);
} catch (error) { } catch (error) {
console.error(`Failed to connect to ${wsurl}:`, error); console.error(`Failed to connect to ${wsurl}:`, error);
} }
} }
// Wait for at least one handshake message if we have connections
if (connectedUrls.length > 0) {
await this.waitForHandshakeMessage();
}
} }
public async addWebsocketConnection(url: string): Promise<void> { public async addWebsocketConnection(url: string): Promise<void> {
@ -331,7 +341,19 @@ export default class Services {
publicData: Record<string, any>, publicData: Record<string, any>,
roles: Record<string, RoleDefinition>, roles: Record<string, RoleDefinition>,
): Promise<ApiReturn> { ): Promise<ApiReturn> {
const relayAddress = this.getAllRelays()[0]['spAddress']; let relayAddress = this.getAllRelays()[0]?.spAddress;
if (!relayAddress || relayAddress === '') {
console.log('No relay address found, connecting to relays...');
await this.connectAllRelays();
// After connectAllRelays completes, relay addresses should be updated
relayAddress = this.getAllRelays()[0]?.spAddress;
if (!relayAddress || relayAddress === '') {
throw new Error('No relay address available after connecting to relays');
}
}
const feeRate = 1; const feeRate = 1;
// We can't encode files as the rest because Uint8Array is not valid json // We can't encode files as the rest because Uint8Array is not valid json
@ -348,6 +370,9 @@ export default class Services {
...this.sdkClient.encode_binary(publicSplitData.binaryData) ...this.sdkClient.encode_binary(publicSplitData.binaryData)
}; };
console.log('encoded data:', encodedPrivateData);
console.log('encoded data:', encodedPublicData);
let members: Set<Member> = new Set(); let members: Set<Member> = new Set();
for (const role of Object.values(roles!)) { for (const role of Object.values(roles!)) {
for (const member of role.members) { for (const member of role.members) {
@ -358,6 +383,7 @@ export default class Services {
} }
} }
} }
console.log('members:', members);
await this.checkConnections([...members]); await this.checkConnections([...members]);
const result = this.sdkClient.create_new_process ( const result = this.sdkClient.create_new_process (
@ -1211,7 +1237,7 @@ export default class Services {
} }
async importJSON(backup: BackUp): Promise<void> { async importJSON(backup: BackUp): Promise<void> {
const device = JSON.stringify(backup.device); const device = backup.device;
// Reset current device // Reset current device
await this.resetDevice(); await this.resetDevice();
@ -1230,14 +1256,12 @@ export default class Services {
public async createBackUp(): Promise<BackUp | null> { public async createBackUp(): Promise<BackUp | null> {
// Get the device from indexedDB // Get the device from indexedDB
const deviceStr = await this.getDeviceFromDatabase(); const device = await this.getDeviceFromDatabase();
if (!deviceStr) { if (!device) {
console.error('No device loaded'); console.error('No device loaded');
return null; return null;
} }
const device: Device = JSON.parse(deviceStr);
// Get the processes // Get the processes
const processes = await this.getProcesses(); const processes = await this.getProcesses();
@ -1367,6 +1391,38 @@ export default class Services {
return false; return false;
} }
/**
* Waits for at least one handshake message to be received from any connected relay.
* This ensures that the relay addresses are fully populated and the member list is updated.
* @returns A promise that resolves when at least one handshake message is received.
*/
private async waitForHandshakeMessage(timeoutMs: number = 10000): Promise<void> {
const startTime = Date.now();
const pollInterval = 100; // Check every 100ms
return new Promise<void>((resolve, reject) => {
const checkForHandshake = () => {
// Check if we have any members (indicating handshake was received)
if (Object.keys(this.membersList).length > 0) {
console.log('Handshake message received, members list populated');
resolve();
return;
}
// Check timeout
if (Date.now() - startTime >= timeoutMs) {
reject(new Error(`No handshake message received after ${timeoutMs}ms timeout`));
return;
}
// Continue polling
setTimeout(checkForHandshake, pollInterval);
};
checkForHandshake();
});
}
/** /**
* Retourne la liste de tous les membres ordonnés par leur process id * Retourne la liste de tous les membres ordonnés par leur process id
* @returns Un tableau contenant tous les membres * @returns Un tableau contenant tous les membres