[bug] Fix checkConnections logic

This commit is contained in:
Sosthene 2025-09-07 18:26:50 +02:00
parent a27ffd6462
commit 1408b2ddf3

View File

@ -292,12 +292,53 @@ export class Service {
}
}
public async checkConnections(members: Member[]): Promise<void> {
// If we're updating a process, we must call that after update especially if roles are part of it
// We will take the roles from the last state, wheter it's commited or not
public async checkConnections(process: Process): Promise<void> {
const sharedSecret = await this.getAllSecretsFromDB();
console.log('sharedSecret found', sharedSecret);
if (process.states.length < 2) {
throw new Error('Process doesn\'t have any state yet');
}
let roles = process.states[process.states.length - 2].roles;
if (!roles) {
throw new Error('No roles found');
} else {
console.log('roles found', roles);
}
let members: Set<Member> = new Set();
for (const role of Object.values(roles!)) {
console.log('role found', role);
for (const member of role.members) {
console.log('member found', member);
// Check if we know the member that matches this id
const memberAddresses = this.getAddressesForMemberId(member);
console.log('memberAddresses found', memberAddresses);
if (memberAddresses && memberAddresses.length != 0) {
members.add({ sp_addresses: memberAddresses });
}
}
}
if (members.size === 0) {
// This must be a pairing process
// Check if we have a pairedAddresses in the public data
const publicData = process.states[0]?.public_data;
if (!publicData || !publicData['pairedAddresses']) {
throw new Error('Not a pairing process');
}
const decodedAddresses = this.decodeValue(publicData['pairedAddresses']);
if (decodedAddresses.length === 0) {
throw new Error('Not a pairing process');
}
members.add({ sp_addresses: decodedAddresses });
}
// Ensure the amount is available before proceeding
await this.getTokensFromFaucet();
let unconnectedAddresses = new Set<string>();
const myAddress = this.getDeviceAddress();
for (const member of members) {
for (const member of Array.from(members)) {
const sp_addresses = member.sp_addresses;
if (!sp_addresses || sp_addresses.length === 0) continue;
for (const address of sp_addresses) {
@ -508,18 +549,6 @@ export class Service {
...wasm.encode_binary(publicSplitData.binaryData)
};
let members: Set<Member> = new Set();
for (const role of Object.values(roles!)) {
for (const member of role.members) {
// Check if we know the member that matches this id
const memberAddresses = this.getAddressesForMemberId(member);
if (memberAddresses && memberAddresses.length != 0) {
members.add({ sp_addresses: memberAddresses });
}
}
}
await this.checkConnections([...members]);
const result = wasm.create_new_process (
encodedPrivateData,
roles,
@ -529,7 +558,12 @@ export class Service {
this.getAllMembers()
);
return(result);
if (result.updated_process) {
await this.checkConnections(result.updated_process.current_process);
return result;
} else {
throw new Error('Failed to create new process');
}
}
async parseCipher(message: string): Promise<void> {
@ -598,6 +632,7 @@ export class Service {
}
const result = wasm.create_update_message(process, stateId, this.membersList);
await this.checkConnections(process);
return result;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error || 'Unknown error');
@ -616,7 +651,12 @@ export class Service {
}
const result = wasm.validate_state(process, stateId, this.membersList);
return result;
if (result.updated_process) {
await this.checkConnections(result.updated_process.current_process);
return result;
} else {
throw new Error('Failed to validate state');
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error || 'Unknown error');
throw new Error(errorMessage);
@ -649,27 +689,6 @@ export class Service {
console.log('Roles provided:', roles);
}
let members: Set<Member> = new Set();
for (const role of Object.values(roles!)) {
for (const member of role.members) {
members.add(member)
}
}
if (members.size === 0) {
// This must be a pairing process
// Check if we have a pairedAddresses in the public data
const publicData = process.states[0]?.public_data;
if (!publicData || !publicData['pairedAddresses']) {
throw new Error('Not a pairing process');
}
const decodedAddresses = this.decodeValue(publicData['pairedAddresses']);
if (decodedAddresses.length === 0) {
throw new Error('Not a pairing process');
}
members.add({ sp_addresses: decodedAddresses });
}
await this.checkConnections([...members]);
const privateSplitData = this.splitData(privateData);
const publicSplitData = this.splitData(publicData);
const encodedPrivateData = {
@ -685,8 +704,7 @@ export class Service {
const result = wasm.update_process(process, encodedPrivateData, roles, encodedPublicData, this.membersList);
if (result.updated_process) {
await this.handleApiReturn(result);
await this.checkConnections(result.updated_process.current_process);
return result;
} else {
throw new Error('Failed to update process');