diff --git a/implementations/Libp2pComponent.ts b/implementations/Libp2pComponent.ts index 23657e3..6a5fe63 100644 --- a/implementations/Libp2pComponent.ts +++ b/implementations/Libp2pComponent.ts @@ -242,12 +242,16 @@ export class Libp2pComponent implements ILibp2pComponent { } async start(): Promise { + logger.info('[TypeScript] start() called, _started:', this._started, '_starting:', this._starting); + if (this._started || this._starting) { + logger.info('[TypeScript] Already started or starting, returning early'); return; // Already started or starting } this._starting = true; this._error = null; + logger.info('[TypeScript] Setting _starting to true, proceeding with start'); try { // Register protocols if any @@ -281,7 +285,18 @@ export class Libp2pComponent implements ILibp2pComponent { config.privateKey = Buffer.from(privateKeyBytes).toString('base64'); } - const result = await this.nativeModule.startLibp2p(config); + logger.debug('[Libp2pComponent] About to call nativeModule.startLibp2p with config:', config); + logger.debug('[Libp2pComponent] nativeModule exists:', !!this.nativeModule); + logger.debug('[Libp2pComponent] nativeModule.startLibp2p exists:', !!this.nativeModule?.startLibp2p); + + let result; + try { + result = await this.nativeModule.startLibp2p(config); + logger.info('[Libp2pComponent] startLibp2p returned:', result); + } catch (error) { + logger.error('[Libp2pComponent] startLibp2p failed:', error); + throw error; + } // Update internal state from result if (result.peerId) { @@ -348,22 +363,46 @@ export class Libp2pComponent implements ILibp2pComponent { } async dial(multiaddr: string): Promise { - await this.nativeModule.connectToPeer(multiaddr); + console.log(`[Libp2pComponent] dial() called with multiaddr: ${multiaddr}`); - // Create connection object - const peerId = multiaddr.match(/\/p2p\/([^/]+)/)?.[1] || ''; - return { - id: `${peerId}-${Date.now()}`, - remotePeer: new SimplePeerId(peerId), - remoteAddr: new SimpleMultiaddr(multiaddr), - stat: { - direction: 'outbound', - status: 'open', - timeline: { - open: Date.now(), + try { + const result = await this.nativeModule.connectToPeer(multiaddr); + console.log('[Libp2pComponent] connectToPeer result:', result); + + // Create connection object + // Try to extract peer ID from result or multiaddr + let peerId = result?.peer_id || result?.peerId || ''; + if (!peerId) { + // Try to extract from multiaddr if not in result + peerId = multiaddr.match(/\/p2p\/([^/]+)/)?.[1] || ''; + } + + // If still no peer ID, this might be a connection without known peer ID + // The real peer ID will be determined during handshake + if (!peerId || peerId === 'pending') { + console.log('[Libp2pComponent] Dialing without known peer ID, will be determined during handshake'); + peerId = 'pending'; + } + + const connection = { + id: `${peerId}-${Date.now()}`, + remotePeer: new SimplePeerId(peerId), + remoteAddr: new SimpleMultiaddr(multiaddr), + stat: { + direction: 'outbound' as const, + status: 'open' as const, + timeline: { + open: Date.now(), + }, }, - }, - }; + }; + + console.log('[Libp2pComponent] Returning connection:', connection); + return connection; + } catch (error) { + console.error('[Libp2pComponent] dial error:', error); + throw error; + } } async hangUp(peerId: string): Promise {