Skip to content

Commit 6ea04db

Browse files
authored
fix: update multiaddr in webrtc connection to include webRTC (libp2p#121)
1 parent 6cc405c commit 6ea04db

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

examples/browser-to-browser/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<input type="text" id="message" value="hello" />
3535
<button id="send">Send</button>
3636
</div>
37+
<div id="connectionsWrapper">
38+
<h3> Active Connections: </h3>
39+
<ul id="connections"></ul>
40+
</div>
3741
<div id="connected_peer"></div>
3842
<div id="output"></div>
3943
</div>

examples/browser-to-browser/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ await node.handle("/echo/1.0.0", ({ stream }) => {
5858
)
5959
})
6060

61+
function updateConnList() {
62+
// Update connections list
63+
const connListEls = node.getConnections()
64+
.map((connection) => { return connection.remoteAddr.toString() })
65+
.map((addr) => {
66+
const el = document.createElement("li")
67+
el.textContent = addr
68+
return el
69+
})
70+
document.getElementById("connections").replaceChildren(...connListEls)
71+
}
72+
73+
node.addEventListener("peer:connect", (event) => {
74+
updateConnList()
75+
})
76+
node.addEventListener("peer:disconnect", (event) => {
77+
updateConnList()
78+
})
79+
6180
node.peerStore.addEventListener("change:multiaddrs", (event) => {
6281
const { peerId } = event.detail
6382

examples/browser-to-browser/tests/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ play.describe('browser to browser example:', () => {
9292
// Received message '${message}'
9393
expect(connections).toContain(`Sending message '${message}'`)
9494
expect(connections).toContain(`Received message '${message}'`)
95+
96+
const connListFromPage = await page.textContent('#connections')
97+
const connListFromPageTwo = await pageTwo.textContent('#connections')
98+
// Expect to see the webrtc multiaddr in the connections list
99+
expect(connListFromPage).toContain('/webrtc/')
100+
expect(connListFromPageTwo).toContain('/webrtc/')
95101
})
96102
})
97103

src/peer_transport/transport.ts

+27-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
44
import { multiaddr, Multiaddr, protocols } from '@multiformats/multiaddr'
55
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
66
import type { PeerId } from '@libp2p/interface-peer-id'
7+
import { peerIdFromString } from '@libp2p/peer-id'
78
import { WebRTCMultiaddrConnection } from '../maconn.js'
89
import type { Startable } from '@libp2p/interfaces/startable'
910
import { WebRTCPeerListener } from './listener.js'
@@ -75,15 +76,7 @@ export class WebRTCTransport implements Transport, Startable {
7576
})
7677
}
7778

78-
/*
79-
* dial connects to a remote via the circuit relay or any other protocol
80-
* and proceeds to upgrade to a webrtc connection.
81-
* multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
82-
* For a circuit relay, this will be of the form
83-
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
84-
*/
85-
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
86-
log.trace('dialing address: ', ma)
79+
private splitAddr (ma: Multiaddr): { baseAddr: Multiaddr, peerId: PeerId } {
8780
const addrs = ma.toString().split(`${TRANSPORT}/`)
8881
if (addrs.length !== 2) {
8982
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR)
@@ -97,11 +90,6 @@ export class WebRTCTransport implements Transport, Startable {
9790
throw new CodeError('bad destination', codes.ERR_INVALID_MULTIADDR)
9891
}
9992

100-
if (options.signal == null) {
101-
const controller = new AbortController()
102-
options.signal = controller.signal
103-
}
104-
10593
const lastProtoInRemote = remoteAddr.protos().pop()
10694
if (lastProtoInRemote === undefined) {
10795
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR)
@@ -110,7 +98,26 @@ export class WebRTCTransport implements Transport, Startable {
11098
remoteAddr = remoteAddr.encapsulate(`/p2p/${destinationIdString}`)
11199
}
112100

113-
const connection = await this.components.transportManager.dial(remoteAddr)
101+
return { baseAddr: remoteAddr, peerId: peerIdFromString(destinationIdString) }
102+
}
103+
104+
/*
105+
* dial connects to a remote via the circuit relay or any other protocol
106+
* and proceeds to upgrade to a webrtc connection.
107+
* multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
108+
* For a circuit relay, this will be of the form
109+
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
110+
*/
111+
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
112+
log.trace('dialing address: ', ma)
113+
const { baseAddr, peerId } = this.splitAddr(ma)
114+
115+
if (options.signal == null) {
116+
const controller = new AbortController()
117+
options.signal = controller.signal
118+
}
119+
120+
const connection = await this.components.transportManager.dial(baseAddr)
114121

115122
const rawStream = await connection.newStream([SIGNALING_PROTO_ID], options)
116123

@@ -120,11 +127,12 @@ export class WebRTCTransport implements Transport, Startable {
120127
rtcConfiguration: this.init.rtcConfiguration,
121128
signal: options.signal
122129
})
130+
const webrtcMultiaddr = baseAddr.encapsulate(`${TRANSPORT}/p2p/${peerId.toString()}`)
123131
const result = await options.upgrader.upgradeOutbound(
124132
new WebRTCMultiaddrConnection({
125133
peerConnection: pc,
126134
timeline: { open: (new Date()).getTime() },
127-
remoteAddr: connection.remoteAddr
135+
remoteAddr: webrtcMultiaddr
128136
}),
129137
{
130138
skipProtection: true,
@@ -150,10 +158,12 @@ export class WebRTCTransport implements Transport, Startable {
150158
connection,
151159
stream
152160
})
161+
const remotePeerId = connection.remoteAddr.getPeerId()
162+
const webrtcMultiaddr = connection.remoteAddr.encapsulate(`${TRANSPORT}/p2p/${remotePeerId}`)
153163
await this.components.upgrader.upgradeInbound(new WebRTCMultiaddrConnection({
154164
peerConnection: pc,
155165
timeline: { open: (new Date()).getTime() },
156-
remoteAddr: connection.remoteAddr
166+
remoteAddr: webrtcMultiaddr
157167
}), {
158168
skipEncryption: true,
159169
skipProtection: true,

0 commit comments

Comments
 (0)