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

View File

@ -262,31 +262,63 @@ class SimpleProcessHandlers {
throw new Error('Invalid message type');
}
const processes = this.service.getProcesses();
const myProcesses = await this.service.getMyProcesses();
if (!myProcesses || myProcesses.length === 0) {
throw new Error('No my processes found');
if (!this.service.isPaired()) {
throw new Error('Device not paired');
}
const filteredProcesses: Record<string, Process> = {};
for (const processId of myProcesses) {
const process = processes.get(processId);
console.log(processId, ':', process);
try {
const processes = this.service.getProcesses();
const myProcesses = await this.service.getMyProcesses();
if (process) {
filteredProcesses[processId] = process;
if (!myProcesses || myProcesses.length === 0) {
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 {
type: MessageType.PROCESSES_RETRIEVED,
processes: filteredProcesses,
data,
messageId: event.data.messageId
};
try {
const pairingId = this.service.getPairingProcessId();
return {
type: MessageType.GET_PAIRING_ID,
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> {
@ -302,6 +334,8 @@ class SimpleProcessHandlers {
return await this.handleUpdateProcess(event);
case MessageType.GET_MY_PROCESSES:
return await this.handleGetMyProcesses(event);
case MessageType.GET_PAIRING_ID:
return await this.handleGetPairingId(event);
default:
throw new Error(`Unhandled message type: ${event.data.type}`);
}