Compare commits

..

No commits in common. "3ef15fd9c6349f28a9fbaf65f943387ee6f15b5e" and "1664b4aa694a67a8768b26c06a77be7769f24420" have entirely different histories.

4 changed files with 40 additions and 114 deletions

View File

@ -310,13 +310,7 @@ export class RelayManager {
// Relay Message Handling
private handleRelayMessage(relayId: string, message: any): void {
console.log(`📨 Received message from relay ${relayId}:`);
if (message.flag === 'Handshake') {
console.log('🔑 Handshake message');
} else {
console.log(`🔑 ${message.flag} message: ${message.content}`);
}
console.log(`📨 Received message from relay ${relayId}:`, message);
// Handle different types of relay responses
if (message.flag) {

View File

@ -264,6 +264,8 @@ export class Service {
}));
}
/**
* Get relay statistics from RelayManager.
* @returns Statistics about connected relays
@ -701,48 +703,6 @@ export class Service {
}
}
public async getProcessesData(filteredProcesses: Record<string, Process>): Promise<Record<string, any>> {
const data: Record<string, any> = {};
// Now we decrypt all we can in the processes
for (const [processId, process] of Object.entries(filteredProcesses)) {
console.log('process roles:', this.getRoles(process));
// We also take the public data
const lastState = this.getLastCommitedState(process);
if (!lastState) {
console.error(`❌ Process ${processId} doesn't have a commited state`);
continue;
}
const processData: Record<string, any> = {};
for (const attribute of Object.keys(lastState.public_data)) {
try {
const value = this.decodeValue(lastState.public_data[attribute]);
if (value) {
processData[attribute] = value;
}
} catch (e) {
console.error(`❌ Error decoding public data ${attribute} for process ${processId}:`, e);
}
}
for (let i = process.states.length - 2; i >= 0; i--) {
const state = process.states[i];
for (const attribute of Object.keys(state.keys)) {
if (processData[attribute] !== undefined && processData[attribute] !== null) continue;
try {
const value = await this.decryptAttribute(processId, state, attribute);
if (value) {
processData[attribute] = value;
}
} catch (e) {
console.error(`❌ Error decrypting attribute ${attribute} for process ${processId}:`, e);
}
}
}
data[processId] = processData;
}
return data;
}
// Utility method: Get Process
async getProcess(processId: string): Promise<any | null> {
// First check in-memory cache

View File

@ -3,7 +3,7 @@ import { MessageType } from './models';
import { config } from './config';
import { Service } from './service';
import { ApiReturn, Process } from '../pkg/sdk_client';
import { EMPTY32BYTES, splitPrivateData } from './utils';
import { EMPTY32BYTES } from './utils';
interface ServerMessageEvent {
data: {
@ -42,52 +42,6 @@ class SimpleProcessHandlers {
return apiKey === this.apiKey;
}
async handleCreateProcess(event: ServerMessageEvent): Promise<ServerResponse> {
if (event.data.type !== MessageType.CREATE_PROCESS) {
throw new Error('Invalid message type');
}
if (!this.service.isPaired()) {
throw new Error('Device not paired');
}
try {
const { processData, privateFields, roles, exclusionRules, apiKey } = event.data;
if (!apiKey || !this.validateApiKey(apiKey)) {
throw new Error('Invalid API key');
}
const { privateData, publicData } = splitPrivateData(processData, privateFields);
const createProcessReturn = await this.service.createProcess(privateData, publicData, roles);
if (!createProcessReturn.updated_process) {
throw new Error('Empty updated_process in createProcessReturn');
}
console.log('🚀 ~ handleCreateProcess ~ createProcessReturn:', createProcessReturn);
const processId = createProcessReturn.updated_process.process_id;
const process = createProcessReturn.updated_process.current_process;
await this.service.handleApiReturn(createProcessReturn);
const processCreated = {
processId,
process,
processData,
}
return {
type: MessageType.PROCESS_CREATED,
processCreated,
messageId: event.data.messageId
};
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e || 'Unknown error');
// Remove redundant "Error:" prefix and simplify the message
const cleanMessage = errorMessage.replace(/^Error:\s*/, '');
throw new Error(cleanMessage);
}
}
async handleNotifyUpdate(event: ServerMessageEvent): Promise<ServerResponse> {
if (event.data.type !== MessageType.NOTIFY_UPDATE) {
throw new Error('Invalid message type');
@ -279,7 +233,42 @@ class SimpleProcessHandlers {
}
}
const data = await this.service.getProcessesData(filteredProcesses);
const data: Record<string, any> = {};
// Now we decrypt all we can in the processes
for (const [processId, process] of Object.entries(filteredProcesses)) {
// We also take the public data
const lastState = this.service.getLastCommitedState(process);
if (!lastState) {
console.error(`❌ Process ${processId} doesn't have a commited state`);
continue;
}
const processData: Record<string, any> = {};
for (const attribute of Object.keys(lastState.public_data)) {
try {
const value = this.service.decodeValue(lastState.public_data[attribute]);
if (value) {
processData[attribute] = value;
}
} catch (e) {
console.error(`❌ Error decoding public data ${attribute} for process ${processId}:`, e);
}
}
for (let i = process.states.length - 2; i >= 0; i--) {
const state = process.states[i];
for (const attribute of Object.keys(state.keys)) {
if (processData[attribute] !== undefined && processData[attribute] !== null) continue;
try {
const value = await this.service.decryptAttribute(processId, state, attribute);
if (value) {
processData[attribute] = value;
}
} catch (e) {
console.error(`❌ Error decrypting attribute ${attribute} for process ${processId}:`, e);
}
}
}
data[processId] = processData;
}
return {
type: MessageType.PROCESSES_RETRIEVED,
@ -292,8 +281,6 @@ class SimpleProcessHandlers {
async handleMessage(event: ServerMessageEvent): Promise<ServerResponse> {
try {
switch (event.data.type) {
case MessageType.CREATE_PROCESS:
return await this.handleCreateProcess(event);
case MessageType.NOTIFY_UPDATE:
return await this.handleNotifyUpdate(event);
case MessageType.VALIDATE_STATE:

View File

@ -1,21 +1,6 @@
// Server-specific utility functions
export const EMPTY32BYTES = String('').padStart(64, '0');
export function splitPrivateData(data: Record<string, any>, privateFields: string[]): { privateData: Record<string, any>, publicData: Record<string, any> } {
const privateData: Record<string, any> = {};
const publicData: Record<string, any> = {};
for (const [key, value] of Object.entries(data)) {
if (privateFields.includes(key)) {
privateData[key] = value;
} else {
publicData[key] = value;
}
}
return { privateData, publicData };
}
export function isValid32ByteHex(value: string): boolean {
// Check if the value is a valid 32-byte hex string (64 characters)
const hexRegex = /^[0-9a-fA-F]{64}$/;