From 9c90cb97e06706df353cd179e9d000b7a3566c88 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Tue, 17 Dec 2024 13:45:54 +0100 Subject: [PATCH] Keep a list of relay addresses with url and sp address --- src/services/service.ts | 65 +++++++++++++++++++++++++++++++---- src/utils/sp-address.utils.ts | 4 +-- src/websockets.ts | 3 ++ 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/services/service.ts b/src/services/service.ts index 71ac46b..d773496 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -11,8 +11,7 @@ import { storeData, retrieveData } from './storage.service'; export const U32_MAX = 4294967295; const storageUrl = `http://localhost:8080`; -const RELAY_ADDRESS = 'sprt1qqdg4x69xdyhxpz4weuel0985qyswa0x9ycl4q6xc0fngf78jtj27gqj5vff4fvlt3fydx4g7vv0mh7vqv8jncgusp6n2zv860nufdzkyy59pqrdr'; -const wsurl = `https://demo.4nkweb.com/ws/`; +const BOOTSTRAPURL = [`http://localhost:8090`]; export default class Services { private static initializing: Promise | null = null; @@ -28,6 +27,7 @@ export default class Services { private subscriptions: { element: Element; event: string; eventHandler: string }[] = []; private database: any; private routingInstance!: ModalService; + private relayAddresses: { [wsurl: string]: string } = {}; // Private constructor to prevent direct instantiation from outside private constructor() {} @@ -56,7 +56,25 @@ export default class Services { this.notifications = this.getNotifications(); this.sdkClient = await import('../../pkg/sdk_client'); this.sdkClient.setup(); - await this.addWebsocketConnection(wsurl); + for (const wsurl of Object.values(BOOTSTRAPURL)) { + this.updateRelay(wsurl, ''); + } + await this.connectAllRelays(); + } + + /** + * Calls `this.addWebsocketConnection` for each `wsurl` in relayAddresses. + */ + public async connectAllRelays(): Promise { + for (const wsurl of Object.keys(this.relayAddresses)) { + try { + console.log(`Connecting to: ${wsurl}`); + await this.addWebsocketConnection(wsurl); + console.log(`Successfully connected to: ${wsurl}`); + } catch (error) { + console.error(`Failed to connect to ${wsurl}:`, error); + } + } } public async addWebsocketConnection(url: string): Promise { @@ -64,9 +82,44 @@ export default class Services { await initWebsocket(url); } - public async getRelayAddresses(): Promise { - // We just return one hardcoded address for now - return [RELAY_ADDRESS]; + /** + * Add or update a key/value pair in relayAddresses. + * @param wsurl - The WebSocket URL (key). + * @param spAddress - The SP Address (value). + */ + public updateRelay(wsurl: string, spAddress: string): void { + this.relayAddresses[wsurl] = spAddress; + console.log(`Updated: ${wsurl} -> ${spAddress}`); + } + + /** + * Retrieve the spAddress for a given wsurl. + * @param wsurl - The WebSocket URL to look up. + * @returns The SP Address if found, or undefined if not. + */ + public getSpAddress(wsurl: string): string | undefined { + return this.relayAddresses[wsurl]; + } + + /** + * Get all key/value pairs from relayAddresses. + * @returns An array of objects containing wsurl and spAddress. + */ + public getAllRelays(): { wsurl: string; spAddress: string }[] { + return Object.entries(this.relayAddresses).map(([wsurl, spAddress]) => ({ + wsurl, + spAddress, + })); + } + + /** + * Print all key/value pairs for debugging. + */ + public printAllRelays(): void { + console.log("Current relay addresses:"); + for (const [wsurl, spAddress] of Object.entries(this.relayAddresses)) { + console.log(`${wsurl} -> ${spAddress}`); + } } public isPaired(): boolean { diff --git a/src/utils/sp-address.utils.ts b/src/utils/sp-address.utils.ts index 05edba9..b0555fd 100755 --- a/src/utils/sp-address.utils.ts +++ b/src/utils/sp-address.utils.ts @@ -171,8 +171,8 @@ export async function prepareAndSendPairingTx(secondDeviceAddress: string) { } // Create the process setTimeout(async () => { - const relayAddress = await service.getRelayAddresses(); // Get one (or more?) relay addresses - const createPairingProcessReturn = await service.createPairingProcess([secondDeviceAddress], relayAddress[0], 1); + const relayAddress = service.getAllRelays(); + const createPairingProcessReturn = await service.createPairingProcess([secondDeviceAddress], relayAddress[0].spAddress, 1); if (!createPairingProcessReturn.updated_process) { throw new Error('createPairingProcess returned an empty new process'); // This should never happen diff --git a/src/websockets.ts b/src/websockets.ts index 174de26..ef172ac 100755 --- a/src/websockets.ts +++ b/src/websockets.ts @@ -29,6 +29,9 @@ export async function initWebsocket(url: string) { const parsedMessage = JSON.parse(msgData); const services = await Services.getInstance(); switch (parsedMessage.flag) { + case 'Init': + services.updateRelay(url, parsedMessage.content); + break; case 'NewTx': await services.parseNewTx(parsedMessage.content); break;