Skip to content

Commit 20801ba

Browse files
committed
feat: added updated webRTC transport (#11)
1 parent 63a3944 commit 20801ba

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/frontend/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@libp2p/delegated-peer-routing": "^4.0.0",
1818
"@libp2p/kad-dht": "^7.0.0",
1919
"@libp2p/peer-id": "^2.0.1",
20-
"@libp2p/webrtc": "^1.0.4",
20+
"@libp2p/webrtc": "github:libp2p/js-libp2p-webrtc#feat/browser-to-browser",
2121
"@libp2p/websockets": "^5.0.3",
2222
"@libp2p/webtransport": "^1.0.7",
2323
"@types/node": "18.14.6",

packages/frontend/src/lib/libp2p.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { webSockets } from '@libp2p/websockets'
2323
import { webTransport } from '@libp2p/webtransport'
2424
import { webRTC } from '@libp2p/webrtc'
2525
import { PeerId } from 'kubo-rpc-client/dist/src/types'
26-
import { CHAT_TOPIC } from './constants'
26+
import { CHAT_TOPIC, CIRCUIT_RELAY_CODE, WEBRTC_CODE } from './constants'
27+
import { circuitRelayTransport } from 'libp2p/circuit'
2728

2829
export async function startLibp2p(options: {} = {}) {
2930
// localStorage.debug = 'libp2p*,-*:trace'
@@ -43,8 +44,9 @@ export async function startLibp2p(options: {} = {}) {
4344
const libp2p = await createLibp2p({
4445
// dht: kadDHT(),
4546
datastore,
46-
transports: [webTransport(), webSockets(), webRTC()],
47-
// transports: [webRTC()],
47+
transports: [webTransport(), webSockets(), webRTC({}), circuitRelayTransport({
48+
discoverRelays: 1,
49+
})],
4850
connectionEncryption: [noise()],
4951
streamMuxers: [yamux()],
5052
// connectionGater: {
@@ -244,3 +246,19 @@ export class Libp2pDialError extends Error {
244246
this.error = error
245247
}
246248
}
249+
250+
export const getCircuitRelayAddress = (libp2p: Libp2p) => (ma: Multiaddr): Multiaddr => {
251+
if (ma.protoCodes().includes(CIRCUIT_RELAY_CODE)) {
252+
if (ma.protos().pop()?.name === 'p2p') {
253+
ma = ma.decapsulateCode(protocols('p2p').code)
254+
}
255+
return multiaddr(ma.toString() + '/webrtc/p2p/' + libp2p.peerId)
256+
}
257+
258+
throw new Error('Not a circuit relay address')
259+
260+
}
261+
262+
export const isWebrtc = (ma: Multiaddr): boolean => {
263+
return ma.protoCodes().includes(WEBRTC_CODE)
264+
}

0 commit comments

Comments
 (0)