chore: allow waku config through API

This commit is contained in:
Danish Arora 2025-10-23 19:04:21 +05:30
parent 08c9acb25a
commit 05938b5127
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
6 changed files with 48 additions and 21 deletions

View File

@ -9,7 +9,13 @@ if (!(window as Window & typeof globalThis).Buffer) {
}
createRoot(document.getElementById('root')!).render(
<OpChanProvider config={{ ordiscanApiKey: '6bb07766-d98c-4ddd-93fb-6a0e94d629dd' }}>
<OpChanProvider config={{
ordiscanApiKey: '6bb07766-d98c-4ddd-93fb-6a0e94d629dd',
wakuConfig: {
contentTopic: '/opchan/1/messages/proto',
reliableChannelId: 'opchan-messages'
}
}}>
<App />
</OpChanProvider>
);

View File

@ -7,9 +7,11 @@ import { UserIdentityService } from '../lib/services/UserIdentityService';
import { DelegationManager, delegationManager } from '../lib/delegation';
import WalletManager from '../lib/wallet';
import { MessageService } from '../lib/services/MessageService';
import { WakuConfig } from '../types';
export interface OpChanClientConfig {
ordiscanApiKey: string;
wakuConfig: WakuConfig;
}
export class OpChanClient {
@ -35,6 +37,10 @@ export class OpChanClient {
environment.configure(env);
// Initialize message manager with WakuConfig
this.messageManager.initialize(config.wakuConfig).catch(error => {
console.error('Failed to initialize message manager:', error);
});
this.messageService = new MessageService(this.delegation);
this.userIdentityService = new UserIdentityService(this.messageService);

View File

@ -9,14 +9,15 @@ import {
} from '../../types/waku';
import { CONTENT_TOPIC } from './constants';
import { OpchanMessage } from '../../types/forum';
import { WakuConfig } from '../../types';
export class CodecManager {
private encoder: IEncoder;
private decoder: IDecoder<IDecodedMessage>;
constructor(private node: LightNode) {
this.encoder = this.node.createEncoder({ contentTopic: CONTENT_TOPIC });
this.decoder = this.node.createDecoder({ contentTopic: CONTENT_TOPIC });
constructor(private node: LightNode, config: WakuConfig) {
this.encoder = this.node.createEncoder({ contentTopic: config.contentTopic || CONTENT_TOPIC });
this.decoder = this.node.createDecoder({ contentTopic: config.contentTopic || CONTENT_TOPIC });
}
/**

View File

@ -7,6 +7,7 @@ import {
import { CodecManager } from '../CodecManager';
import { generateStringId } from '../../utils';
import { OpchanMessage } from '../../../types/forum';
import { WakuConfig } from '../../../types';
export interface MessageStatusCallback {
onSent?: (messageId: string) => void;
@ -22,9 +23,9 @@ export class ReliableMessaging {
private incomingMessageCallbacks: Set<IncomingMessageCallback> = new Set();
private codecManager: CodecManager;
constructor(node: LightNode) {
this.codecManager = new CodecManager(node);
this.initializeChannel(node);
constructor(node: LightNode, config: WakuConfig) {
this.codecManager = new CodecManager(node, config);
this.initializeChannel(node, config);
}
// ===== PUBLIC METHODS =====
@ -65,11 +66,11 @@ export class ReliableMessaging {
// ===== PRIVATE METHODS =====
private async initializeChannel(node: LightNode): Promise<void> {
private async initializeChannel(node: LightNode, config: WakuConfig): Promise<void> {
const encoder = this.codecManager.getEncoder();
const decoder = this.codecManager.getDecoder();
const senderId = generateStringId();
const channelId = 'opchan-messages';
const channelId = config.reliableChannelId || 'opchan-messages';
try {
this.channel = await ReliableChannel.create(

View File

@ -6,6 +6,7 @@ import {
MessageStatusCallback,
} from './services/MessageService';
import { ReliableMessaging } from './core/ReliableMessaging';
import { WakuConfig } from '../../types';
export type { HealthChangeCallback, MessageStatusCallback };
@ -13,13 +14,16 @@ class MessageManager {
private nodeManager: WakuNodeManager | null = null;
private messageService: MessageService | null = null;
private reliableMessaging: ReliableMessaging | null = null;
private wakuConfig: WakuConfig;
constructor() {}
constructor(wakuConfig: WakuConfig) {
this.wakuConfig = wakuConfig;
}
// ===== PUBLIC STATIC METHODS =====
public static async create(): Promise<MessageManager> {
const manager = new MessageManager();
public static async create(wakuConfig: WakuConfig): Promise<MessageManager> {
const manager = new MessageManager(wakuConfig);
await manager.initialize();
return manager;
}
@ -101,7 +105,8 @@ class MessageManager {
try {
console.log('Initializing reliable messaging...');
this.reliableMessaging = new ReliableMessaging(
this.nodeManager.getNode()
this.nodeManager.getNode(),
this.wakuConfig
);
this.messageService?.updateReliableMessaging(this.reliableMessaging);
console.log('Reliable messaging initialized successfully');
@ -126,13 +131,18 @@ export class DefaultMessageManager {
private _initPromise: Promise<MessageManager> | null = null;
private _pendingHealthSubscriptions: HealthChangeCallback[] = [];
private _pendingMessageSubscriptions: ((message: any) => void)[] = [];
private _wakuConfig: WakuConfig | null = null;
// ===== PUBLIC METHODS =====
// Initialize the manager asynchronously
async initialize(): Promise<void> {
if (!this._initPromise) {
this._initPromise = MessageManager.create();
async initialize(wakuConfig?: WakuConfig): Promise<void> {
if (wakuConfig) {
this._wakuConfig = wakuConfig;
}
if (!this._initPromise && this._wakuConfig) {
this._initPromise = MessageManager.create(this._wakuConfig);
}
this._instance = await this._initPromise;
@ -160,6 +170,9 @@ export class DefaultMessageManager {
async sendMessage(message: any, callback?: any): Promise<void> {
if (!this._instance) {
if (!this._wakuConfig) {
throw new Error('WakuConfig must be provided before sending messages');
}
await this.initialize();
}
return this._instance!.sendMessage(message, callback);
@ -200,9 +213,4 @@ export class DefaultMessageManager {
const messageManager = new DefaultMessageManager();
// Initialize in the background
messageManager.initialize().catch(error => {
console.error('Failed to initialize default MessageManager:', error);
});
export default messageManager;

View File

@ -1 +1,6 @@
export * as forum from './forum';
export interface WakuConfig {
contentTopic?: string;
reliableChannelId?: string;
}