Keep a list of relay addresses with url and sp address
This commit is contained in:
parent
309b32910c
commit
9c90cb97e0
@ -11,8 +11,7 @@ import { storeData, retrieveData } from './storage.service';
|
|||||||
export const U32_MAX = 4294967295;
|
export const U32_MAX = 4294967295;
|
||||||
|
|
||||||
const storageUrl = `http://localhost:8080`;
|
const storageUrl = `http://localhost:8080`;
|
||||||
const RELAY_ADDRESS = 'sprt1qqdg4x69xdyhxpz4weuel0985qyswa0x9ycl4q6xc0fngf78jtj27gqj5vff4fvlt3fydx4g7vv0mh7vqv8jncgusp6n2zv860nufdzkyy59pqrdr';
|
const BOOTSTRAPURL = [`http://localhost:8090`];
|
||||||
const wsurl = `https://demo.4nkweb.com/ws/`;
|
|
||||||
|
|
||||||
export default class Services {
|
export default class Services {
|
||||||
private static initializing: Promise<Services> | null = null;
|
private static initializing: Promise<Services> | null = null;
|
||||||
@ -28,6 +27,7 @@ export default class Services {
|
|||||||
private subscriptions: { element: Element; event: string; eventHandler: string }[] = [];
|
private subscriptions: { element: Element; event: string; eventHandler: string }[] = [];
|
||||||
private database: any;
|
private database: any;
|
||||||
private routingInstance!: ModalService;
|
private routingInstance!: ModalService;
|
||||||
|
private relayAddresses: { [wsurl: string]: string } = {};
|
||||||
// Private constructor to prevent direct instantiation from outside
|
// Private constructor to prevent direct instantiation from outside
|
||||||
private constructor() {}
|
private constructor() {}
|
||||||
|
|
||||||
@ -56,7 +56,25 @@ export default class Services {
|
|||||||
this.notifications = this.getNotifications();
|
this.notifications = this.getNotifications();
|
||||||
this.sdkClient = await import('../../pkg/sdk_client');
|
this.sdkClient = await import('../../pkg/sdk_client');
|
||||||
this.sdkClient.setup();
|
this.sdkClient.setup();
|
||||||
|
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<void> {
|
||||||
|
for (const wsurl of Object.keys(this.relayAddresses)) {
|
||||||
|
try {
|
||||||
|
console.log(`Connecting to: ${wsurl}`);
|
||||||
await this.addWebsocketConnection(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<void> {
|
public async addWebsocketConnection(url: string): Promise<void> {
|
||||||
@ -64,9 +82,44 @@ export default class Services {
|
|||||||
await initWebsocket(url);
|
await initWebsocket(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getRelayAddresses(): Promise<string[]> {
|
/**
|
||||||
// We just return one hardcoded address for now
|
* Add or update a key/value pair in relayAddresses.
|
||||||
return [RELAY_ADDRESS];
|
* @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 {
|
public isPaired(): boolean {
|
||||||
|
@ -171,8 +171,8 @@ export async function prepareAndSendPairingTx(secondDeviceAddress: string) {
|
|||||||
}
|
}
|
||||||
// Create the process
|
// Create the process
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
const relayAddress = await service.getRelayAddresses(); // Get one (or more?) relay addresses
|
const relayAddress = service.getAllRelays();
|
||||||
const createPairingProcessReturn = await service.createPairingProcess([secondDeviceAddress], relayAddress[0], 1);
|
const createPairingProcessReturn = await service.createPairingProcess([secondDeviceAddress], relayAddress[0].spAddress, 1);
|
||||||
|
|
||||||
if (!createPairingProcessReturn.updated_process) {
|
if (!createPairingProcessReturn.updated_process) {
|
||||||
throw new Error('createPairingProcess returned an empty new process'); // This should never happen
|
throw new Error('createPairingProcess returned an empty new process'); // This should never happen
|
||||||
|
@ -29,6 +29,9 @@ export async function initWebsocket(url: string) {
|
|||||||
const parsedMessage = JSON.parse(msgData);
|
const parsedMessage = JSON.parse(msgData);
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
switch (parsedMessage.flag) {
|
switch (parsedMessage.flag) {
|
||||||
|
case 'Init':
|
||||||
|
services.updateRelay(url, parsedMessage.content);
|
||||||
|
break;
|
||||||
case 'NewTx':
|
case 'NewTx':
|
||||||
await services.parseNewTx(parsedMessage.content);
|
await services.parseNewTx(parsedMessage.content);
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user