diff --git a/package.json b/package.json index a95a1b3..c005d1d 100644 --- a/package.json +++ b/package.json @@ -137,15 +137,15 @@ "dependencies": { "@chainsafe/libp2p-noise": "^12.0.0", "@libp2p/interface-connection": "^5.0.2", - "@libp2p/interface-libp2p": "^3.1.0", "@libp2p/interface-metrics": "^4.0.8", "@libp2p/interface-peer-id": "^2.0.2", "@libp2p/interface-registrar": "^2.0.12", "@libp2p/interface-stream-muxer": "^4.0.1", - "@libp2p/interface-transport": "^4.0.2", + "@libp2p/interface-transport": "^4.0.3", "@libp2p/interfaces": "^3.3.2", "@libp2p/logger": "^2.0.7", "@libp2p/peer-id": "^2.0.3", + "@multiformats/mafmt": "^12.1.2", "@multiformats/multiaddr": "^12.1.2", "abortable-iterator": "^5.0.1", "detect-browser": "^5.3.0", diff --git a/src/private-to-private/listener.ts b/src/private-to-private/listener.ts index 099f500..018e569 100644 --- a/src/private-to-private/listener.ts +++ b/src/private-to-private/listener.ts @@ -1,47 +1,35 @@ import { EventEmitter } from '@libp2p/interfaces/events' -import type { Libp2pEvents } from '@libp2p/interface-libp2p' +import { Circuit } from '@multiformats/mafmt' import type { PeerId } from '@libp2p/interface-peer-id' -import type { ListenerEvents, Listener } from '@libp2p/interface-transport' +import type { ListenerEvents, Listener, TransportManager } from '@libp2p/interface-transport' import type { Multiaddr } from '@multiformats/multiaddr' export interface ListenerOptions { peerId: PeerId - events: EventEmitter + transportManager: TransportManager } export class WebRTCPeerListener extends EventEmitter implements Listener { private readonly peerId: PeerId - private listeners: Listener[] = [] + private readonly transportManager: TransportManager constructor (opts: ListenerOptions) { super() this.peerId = opts.peerId - - opts.events.addEventListener('transport:listening', (event) => { - const listener = event.detail - - if (listener === this || this.listeners.includes(listener)) { - return - } - - this.listeners.push(listener) - }) - - opts.events.addEventListener('transport:close', (event) => { - const listener = event.detail - - this.listeners = this.listeners.filter(l => l !== listener) - }) + this.transportManager = opts.transportManager } - async listen (ma: Multiaddr): Promise { + async listen (): Promise { this.safeDispatchEvent('listening', {}) } getAddrs (): Multiaddr[] { - return this.listeners + return this.transportManager + .getListeners() + .filter(l => l !== this) .map(l => l.getAddrs() + .filter(ma => Circuit.matches(ma)) .map(ma => { return ma.encapsulate(`/webrtc/p2p/${this.peerId}`) }) diff --git a/src/private-to-private/transport.ts b/src/private-to-private/transport.ts index 92c64ab..fb2e204 100644 --- a/src/private-to-private/transport.ts +++ b/src/private-to-private/transport.ts @@ -8,10 +8,8 @@ import { WebRTCMultiaddrConnection } from '../maconn.js' import { initiateConnection, handleIncomingStream } from './handler.js' import { WebRTCPeerListener } from './listener.js' import type { Connection } from '@libp2p/interface-connection' -import type { Libp2pEvents } from '@libp2p/interface-libp2p' import type { PeerId } from '@libp2p/interface-peer-id' import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar' -import type { EventEmitter } from '@libp2p/interfaces/events' import type { Startable } from '@libp2p/interfaces/startable' const log = logger('libp2p:webrtc:peer') @@ -30,7 +28,6 @@ export interface WebRTCTransportComponents { registrar: Registrar upgrader: Upgrader transportManager: TransportManager - events: EventEmitter } export class WebRTCTransport implements Transport, Startable { diff --git a/test/listener.spec.ts b/test/listener.spec.ts new file mode 100644 index 0000000..4928ca6 --- /dev/null +++ b/test/listener.spec.ts @@ -0,0 +1,40 @@ +import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import { multiaddr } from '@multiformats/multiaddr' +import { expect } from 'aegir/chai' +import { stubInterface } from 'sinon-ts' +import { WebRTCPeerListener } from '../src/private-to-private/listener' +import type { Listener, TransportManager } from '@libp2p/interface-transport' + +describe('webrtc private-to-private listener', () => { + it('should only return relay addresses as webrtc listen addresses', async () => { + const relayedAddress = '/ip4/127.0.0.1/tcp/4034/ws/p2p-circuit' + const otherListenAddress = '/ip4/127.0.0.1/tcp/4001' + const peerId = await createEd25519PeerId() + const transportManager = stubInterface() + + const listener = new WebRTCPeerListener({ + peerId, + transportManager + }) + + const otherListener = stubInterface({ + getAddrs: [multiaddr(otherListenAddress)] + }) + + const relayListener = stubInterface({ + getAddrs: [multiaddr(relayedAddress)] + }) + + transportManager.getListeners.returns([ + listener, + otherListener, + relayListener + ]) + + const addresses = listener.getAddrs() + + expect(addresses.map(ma => ma.toString())).to.deep.equal([ + `${relayedAddress}/webrtc/p2p/${peerId}` + ]) + }) +}) diff --git a/test/peer.browser.spec.ts b/test/peer.browser.spec.ts index d123e0a..9b6e5f8 100644 --- a/test/peer.browser.spec.ts +++ b/test/peer.browser.spec.ts @@ -1,5 +1,4 @@ import { mockConnection, mockMultiaddrConnection, mockRegistrar, mockStream, mockUpgrader } from '@libp2p/interface-mocks' -import { EventEmitter } from '@libp2p/interfaces/events' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -99,8 +98,7 @@ describe('webrtc filter', () => { transportManager: Sinon.stub() as any, peerId: Sinon.stub() as any, registrar: mockRegistrar(), - upgrader: mockUpgrader({}), - events: new EventEmitter() + upgrader: mockUpgrader({}) }, {}) const valid = [