Compare commits
3 Commits
6625771830
...
06234a986b
Author | SHA1 | Date | |
---|---|---|---|
![]() |
06234a986b | ||
![]() |
2551c9923a | ||
![]() |
b77dbceaa9 |
@ -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;
|
||||||
|
@ -262,6 +262,11 @@ class SimpleProcessHandlers {
|
|||||||
throw new Error('Invalid message type');
|
throw new Error('Invalid message type');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!this.service.isPaired()) {
|
||||||
|
throw new Error('Device not paired');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const processes = this.service.getProcesses();
|
const processes = this.service.getProcesses();
|
||||||
const myProcesses = await this.service.getMyProcesses();
|
const myProcesses = await this.service.getMyProcesses();
|
||||||
|
|
||||||
@ -272,10 +277,11 @@ class SimpleProcessHandlers {
|
|||||||
const filteredProcesses: Record<string, Process> = {};
|
const filteredProcesses: Record<string, Process> = {};
|
||||||
for (const processId of myProcesses) {
|
for (const processId of myProcesses) {
|
||||||
const process = processes.get(processId);
|
const process = processes.get(processId);
|
||||||
console.log(processId, ':', process);
|
|
||||||
|
|
||||||
if (process) {
|
if (process) {
|
||||||
filteredProcesses[processId] = process;
|
filteredProcesses[processId] = process;
|
||||||
|
} else {
|
||||||
|
console.error(`Process ${processId} not found`); // should not happen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,6 +293,32 @@ class SimpleProcessHandlers {
|
|||||||
data,
|
data,
|
||||||
messageId: event.data.messageId
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.service.isPaired()) {
|
||||||
|
throw new Error('Device not paired');
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
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}`);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user