@@ -23,7 +23,8 @@ import { webSockets } from '@libp2p/websockets'
23
23
import { webTransport } from '@libp2p/webtransport'
24
24
import { webRTC } from '@libp2p/webrtc'
25
25
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'
27
28
28
29
export async function startLibp2p ( options : { } = { } ) {
29
30
// localStorage.debug = 'libp2p*,-*:trace'
@@ -43,8 +44,9 @@ export async function startLibp2p(options: {} = {}) {
43
44
const libp2p = await createLibp2p ( {
44
45
// dht: kadDHT(),
45
46
datastore,
46
- transports : [ webTransport ( ) , webSockets ( ) , webRTC ( ) ] ,
47
- // transports: [webRTC()],
47
+ transports : [ webTransport ( ) , webSockets ( ) , webRTC ( { } ) , circuitRelayTransport ( {
48
+ discoverRelays : 1 ,
49
+ } ) ] ,
48
50
connectionEncryption : [ noise ( ) ] ,
49
51
streamMuxers : [ yamux ( ) ] ,
50
52
// connectionGater: {
@@ -244,3 +246,19 @@ export class Libp2pDialError extends Error {
244
246
this . error = error
245
247
}
246
248
}
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