Compare commits

..

3 Commits

Author SHA1 Message Date
Sosthene
06234a986b Add handleGetPairingId 2025-09-04 04:46:42 +02:00
Sosthene
2551c9923a Better error management for handleGetMyProcesses 2025-09-04 04:46:27 +02:00
Sosthene
b77dbceaa9 [bug] prevents addresses duplicates in transactions 2025-09-04 04:45:03 +02:00
2 changed files with 58 additions and 24 deletions

View File

@ -294,7 +294,7 @@ export class Service {
public async checkConnections(members: Member[]): Promise<void> { public async checkConnections(members: Member[]): Promise<void> {
// Ensure the amount is available before proceeding // Ensure the amount is available before proceeding
await this.getTokensFromFaucet(); await this.getTokensFromFaucet();
let unconnectedAddresses = []; let unconnectedAddresses = new Set<string>();
const myAddress = this.getDeviceAddress(); const myAddress = this.getDeviceAddress();
for (const member of members) { for (const member of members) {
const sp_addresses = member.sp_addresses; const sp_addresses = member.sp_addresses;
@ -304,23 +304,23 @@ export class Service {
if (address === myAddress) continue; if (address === myAddress) continue;
const sharedSecret = await this.getSecretForAddress(address); const sharedSecret = await this.getSecretForAddress(address);
if (!sharedSecret) { if (!sharedSecret) {
unconnectedAddresses.push(address); unconnectedAddresses.add(address);
} }
} }
} }
if (unconnectedAddresses && unconnectedAddresses.length != 0) { if (unconnectedAddresses && unconnectedAddresses.size != 0) {
const apiResult = await this.connectAddresses(unconnectedAddresses); const apiResult = await this.connectAddresses(unconnectedAddresses);
await this.handleApiReturn(apiResult); await this.handleApiReturn(apiResult);
} }
} }
public async connectAddresses(addresses: string[]): Promise<ApiReturn> { public async connectAddresses(addresses: Set<string>): Promise<ApiReturn> {
if (addresses.length === 0) { if (addresses.size === 0) {
throw new Error('Trying to connect to empty addresses list'); throw new Error('Trying to connect to empty addresses list');
} }
try { try {
return wasm.create_transaction(addresses, 1); return wasm.create_transaction(Array.from(addresses), 1);
} catch (e) { } catch (e) {
console.error('Failed to connect member:', e); console.error('Failed to connect member:', e);
throw e; throw e;

View File

@ -262,31 +262,63 @@ class SimpleProcessHandlers {
throw new Error('Invalid message type'); throw new Error('Invalid message type');
} }
const processes = this.service.getProcesses(); if (!this.service.isPaired()) {
const myProcesses = await this.service.getMyProcesses(); throw new Error('Device not paired');
if (!myProcesses || myProcesses.length === 0) {
throw new Error('No my processes found');
} }
const filteredProcesses: Record<string, Process> = {}; try {
for (const processId of myProcesses) { const processes = this.service.getProcesses();
const process = processes.get(processId); const myProcesses = await this.service.getMyProcesses();
console.log(processId, ':', process);
if (process) { if (!myProcesses || myProcesses.length === 0) {
filteredProcesses[processId] = process; throw new Error('No my processes found');
} }
const filteredProcesses: Record<string, Process> = {};
for (const processId of myProcesses) {
const process = processes.get(processId);
if (process) {
filteredProcesses[processId] = process;
} else {
console.error(`Process ${processId} not found`); // should not happen
}
}
const data = await this.service.getProcessesData(filteredProcesses);
return {
type: MessageType.PROCESSES_RETRIEVED,
processes: filteredProcesses,
data,
messageId: event.data.messageId
};
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e || 'Unknown error');
throw new Error(errorMessage);
}
}
async handleGetPairingId(event: ServerMessageEvent): Promise<ServerResponse> {
if (event.data.type !== MessageType.GET_PAIRING_ID) {
throw new Error('Invalid message type');
} }
const data = await this.service.getProcessesData(filteredProcesses); if (!this.service.isPaired()) {
throw new Error('Device not paired');
}
return { try {
type: MessageType.PROCESSES_RETRIEVED, const pairingId = this.service.getPairingProcessId();
processes: filteredProcesses, return {
data, type: MessageType.GET_PAIRING_ID,
messageId: event.data.messageId pairingId,
}; messageId: event.data.messageId
};
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e || 'Unknown error');
throw new Error(errorMessage);
}
} }
async handleMessage(event: ServerMessageEvent): Promise<ServerResponse> { async handleMessage(event: ServerMessageEvent): Promise<ServerResponse> {
@ -302,6 +334,8 @@ class SimpleProcessHandlers {
return await this.handleUpdateProcess(event); return await this.handleUpdateProcess(event);
case MessageType.GET_MY_PROCESSES: case MessageType.GET_MY_PROCESSES:
return await this.handleGetMyProcesses(event); return await this.handleGetMyProcesses(event);
case MessageType.GET_PAIRING_ID:
return await this.handleGetPairingId(event);
default: default:
throw new Error(`Unhandled message type: ${event.data.type}`); throw new Error(`Unhandled message type: ${event.data.type}`);
} }