1
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
1
import { EventEmitter , CustomEvent } from '@libp2p/interfaces/events' ;
3
2
import { createLibp2p , Libp2pOptions , Libp2p , Libp2pInit } from 'libp2p' ;
4
3
import { noise } from '@chainsafe/libp2p-noise' ;
@@ -9,18 +8,18 @@ import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
9
8
import { peerIdFromString } from '@libp2p/peer-id' ;
10
9
import { PeerId } from '@libp2p/interface-peer-id' ;
11
10
import { OPEN } from '@libp2p/interface-connection/status' ;
12
- import { Chain , stringify } from 'viem' ;
11
+ import { stringify } from 'viem' ;
13
12
import {
14
13
OfferData ,
15
14
GenericOfferOptions ,
16
15
GenericQuery ,
17
- Contracts ,
18
16
RequestData ,
19
17
} from '../shared/types.js' ;
20
18
import { centerSub , CenterSub } from '../shared/pubsub.js' ;
21
- import { ChainsConfigOption , ServerAddressOption } from '../shared/options.js' ;
19
+ import { ServerAddressOption } from '../shared/options.js' ;
22
20
import { encodeText , decodeText } from '../utils/text.js' ;
23
21
import { createLogger } from '../utils/logger.js' ;
22
+ import { yamux } from '@chainsafe/libp2p-yamux' ;
24
23
25
24
const logger = createLogger ( 'Client' ) ;
26
25
@@ -168,7 +167,7 @@ export class Client<
168
167
get connected ( ) : boolean {
169
168
return (
170
169
! ! this . libp2p &&
171
- ( this . libp2p . pubsub as CenterSub ) . started &&
170
+ ( this . libp2p . services . pubsub as CenterSub ) . started &&
172
171
this . libp2p . getPeers ( ) . length > 0 &&
173
172
this . libp2p . getConnections ( this . serverPeerId ) [ 0 ] ?. stat . status === OPEN
174
173
) ;
@@ -183,23 +182,45 @@ export class Client<
183
182
async start ( ) : Promise < void > {
184
183
const config : Libp2pOptions = {
185
184
transports : [ webSockets ( { filter : all } ) ] ,
186
- streamMuxers : [ mplex ( ) ] ,
185
+ streamMuxers : [ yamux ( ) , mplex ( ) ] ,
187
186
connectionEncryption : [ noise ( ) ] ,
188
- pubsub : centerSub ( {
189
- isClient : true ,
190
- /** Client must be connected to the coordination server */
191
- directPeers : [
192
- {
193
- id : this . serverPeerId ,
194
- addrs : [ this . serverMultiaddr ] ,
195
- } ,
196
- ] ,
197
- } ) ,
187
+ services : {
188
+ pubsub : centerSub ( {
189
+ isClient : true ,
190
+ /** Client must be connected to the coordination server */
191
+ directPeers : [
192
+ {
193
+ id : this . serverPeerId ,
194
+ addrs : [ this . serverMultiaddr ] ,
195
+ } ,
196
+ ] ,
197
+ } ) ,
198
+ } ,
199
+ connectionManager : {
200
+ maxPeerAddrsToDial : 10 ,
201
+ minConnections : 0 ,
202
+ maxConnections : 100 ,
203
+ maxParallelDials : 20 ,
204
+ } ,
205
+ connectionGater : {
206
+ //todo check all settings
207
+ denyDialPeer : async ( ) => Promise . resolve ( false ) ,
208
+ denyDialMultiaddr : async ( ) => Promise . resolve ( false ) ,
209
+ denyInboundConnection : async ( ) => Promise . resolve ( false ) ,
210
+ denyOutboundConnection : async ( ) => Promise . resolve ( false ) ,
211
+ denyInboundEncryptedConnection : async ( ) => Promise . resolve ( false ) ,
212
+ denyOutboundEncryptedConnection : async ( ) => Promise . resolve ( false ) ,
213
+ denyInboundUpgradedConnection : async ( ) => Promise . resolve ( false ) ,
214
+ denyOutboundUpgradedConnection : async ( ) => Promise . resolve ( false ) ,
215
+ denyInboundRelayReservation : async ( ) => Promise . resolve ( false ) ,
216
+ denyOutboundRelayedConnection : async ( ) => Promise . resolve ( false ) ,
217
+ denyInboundRelayedConnection : async ( ) => Promise . resolve ( false ) ,
218
+ } ,
198
219
...this . libp2pInit ,
199
220
} ;
200
221
this . libp2p = await createLibp2p ( config ) ;
201
222
202
- ( this . libp2p . pubsub as CenterSub ) . addEventListener (
223
+ ( this . libp2p . services . pubsub as CenterSub ) . addEventListener (
203
224
'gossipsub:heartbeat' ,
204
225
( ) => {
205
226
this . dispatchEvent ( new CustomEvent < void > ( 'heartbeat' ) ) ;
@@ -208,7 +229,7 @@ export class Client<
208
229
209
230
this . libp2p . addEventListener ( 'peer:connect' , ( { detail } ) => {
210
231
try {
211
- if ( detail . remotePeer . equals ( this . serverPeerId ) ) {
232
+ if ( detail . equals ( this . serverPeerId ) ) {
212
233
this . dispatchEvent ( new CustomEvent < void > ( 'connected' ) ) ;
213
234
logger . trace (
214
235
'🔗 Client connected to server at:' ,
@@ -222,7 +243,7 @@ export class Client<
222
243
223
244
this . libp2p . addEventListener ( 'peer:disconnect' , ( { detail } ) => {
224
245
try {
225
- if ( detail . remotePeer . equals ( this . serverPeerId ) ) {
246
+ if ( detail . equals ( this . serverPeerId ) ) {
226
247
this . dispatchEvent ( new CustomEvent < void > ( 'disconnected' ) ) ;
227
248
logger . trace (
228
249
'🔌 Client disconnected from server at:' ,
@@ -234,34 +255,37 @@ export class Client<
234
255
}
235
256
} ) ;
236
257
237
- this . libp2p . pubsub . addEventListener ( 'message' , ( { detail } ) => {
238
- logger . trace ( `Message on topic ${ detail . topic } ` ) ;
258
+ ( this . libp2p . services . pubsub as CenterSub ) . addEventListener (
259
+ 'message' ,
260
+ ( { detail } ) => {
261
+ logger . trace ( `Message on topic ${ detail . topic } ` ) ;
239
262
240
- try {
241
- /** Check is the message is an offer */
242
- const offer = JSON . parse ( decodeText ( detail . data ) ) as OfferData <
243
- CustomRequestQuery ,
244
- CustomOfferOptions
245
- > ;
263
+ try {
264
+ /** Check is the message is an offer */
265
+ const offer = JSON . parse ( decodeText ( detail . data ) ) as OfferData <
266
+ CustomRequestQuery ,
267
+ CustomOfferOptions
268
+ > ;
246
269
247
- // @todo Validate offer
270
+ // @todo Validate offer
248
271
249
- logger . trace ( 'Offer received:' , offer ) ;
272
+ logger . trace ( 'Offer received:' , offer ) ;
250
273
251
- // @todo Implement offer verification
274
+ // @todo Implement offer verification
252
275
253
- this . dispatchEvent (
254
- new CustomEvent < OfferData < CustomRequestQuery , CustomOfferOptions > > (
255
- 'offer' ,
256
- {
257
- detail : offer ,
258
- } ,
259
- ) ,
260
- ) ;
261
- } catch ( error ) {
262
- logger . error ( error ) ;
263
- }
264
- } ) ;
276
+ this . dispatchEvent (
277
+ new CustomEvent < OfferData < CustomRequestQuery , CustomOfferOptions > > (
278
+ 'offer' ,
279
+ {
280
+ detail : offer ,
281
+ } ,
282
+ ) ,
283
+ ) ;
284
+ } catch ( error ) {
285
+ logger . error ( error ) ;
286
+ }
287
+ } ,
288
+ ) ;
265
289
266
290
await this . libp2p . start ( ) ;
267
291
this . dispatchEvent ( new CustomEvent < void > ( 'start' ) ) ;
@@ -279,7 +303,7 @@ export class Client<
279
303
throw new Error ( 'libp2p not initialized yet' ) ;
280
304
}
281
305
282
- this . libp2p . pubsub
306
+ ( this . libp2p . services . pubsub as CenterSub )
283
307
. publish ( request . topic , encodeText ( stringify ( request ) ) )
284
308
. then ( ( ) => {
285
309
this . dispatchEvent (
@@ -302,7 +326,7 @@ export class Client<
302
326
throw new Error ( 'libp2p not initialized yet' ) ;
303
327
}
304
328
305
- this . libp2p . pubsub . subscribe ( topic ) ;
329
+ ( this . libp2p . services . pubsub as CenterSub ) . subscribe ( topic ) ;
306
330
}
307
331
308
332
/**
@@ -316,7 +340,7 @@ export class Client<
316
340
throw new Error ( 'libp2p not initialized yet' ) ;
317
341
}
318
342
319
- this . libp2p . pubsub . unsubscribe ( topic ) ;
343
+ ( this . libp2p . services . pubsub as CenterSub ) . unsubscribe ( topic ) ;
320
344
}
321
345
322
346
/**
0 commit comments