1- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
21import { EventEmitter , CustomEvent } from '@libp2p/interfaces/events' ;
32import { createLibp2p , Libp2pOptions , Libp2p , Libp2pInit } from 'libp2p' ;
43import { noise } from '@chainsafe/libp2p-noise' ;
@@ -9,18 +8,18 @@ import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
98import { peerIdFromString } from '@libp2p/peer-id' ;
109import { PeerId } from '@libp2p/interface-peer-id' ;
1110import { OPEN } from '@libp2p/interface-connection/status' ;
12- import { Chain , stringify } from 'viem' ;
11+ import { stringify } from 'viem' ;
1312import {
1413 OfferData ,
1514 GenericOfferOptions ,
1615 GenericQuery ,
17- Contracts ,
1816 RequestData ,
1917} from '../shared/types.js' ;
2018import { centerSub , CenterSub } from '../shared/pubsub.js' ;
21- import { ChainsConfigOption , ServerAddressOption } from '../shared/options.js' ;
19+ import { ServerAddressOption } from '../shared/options.js' ;
2220import { encodeText , decodeText } from '../utils/text.js' ;
2321import { createLogger } from '../utils/logger.js' ;
22+ import { yamux } from '@chainsafe/libp2p-yamux' ;
2423
2524const logger = createLogger ( 'Client' ) ;
2625
@@ -168,7 +167,7 @@ export class Client<
168167 get connected ( ) : boolean {
169168 return (
170169 ! ! this . libp2p &&
171- ( this . libp2p . pubsub as CenterSub ) . started &&
170+ ( this . libp2p . services . pubsub as CenterSub ) . started &&
172171 this . libp2p . getPeers ( ) . length > 0 &&
173172 this . libp2p . getConnections ( this . serverPeerId ) [ 0 ] ?. stat . status === OPEN
174173 ) ;
@@ -183,23 +182,45 @@ export class Client<
183182 async start ( ) : Promise < void > {
184183 const config : Libp2pOptions = {
185184 transports : [ webSockets ( { filter : all } ) ] ,
186- streamMuxers : [ mplex ( ) ] ,
185+ streamMuxers : [ yamux ( ) , mplex ( ) ] ,
187186 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+ } ,
198219 ...this . libp2pInit ,
199220 } ;
200221 this . libp2p = await createLibp2p ( config ) ;
201222
202- ( this . libp2p . pubsub as CenterSub ) . addEventListener (
223+ ( this . libp2p . services . pubsub as CenterSub ) . addEventListener (
203224 'gossipsub:heartbeat' ,
204225 ( ) => {
205226 this . dispatchEvent ( new CustomEvent < void > ( 'heartbeat' ) ) ;
@@ -208,7 +229,7 @@ export class Client<
208229
209230 this . libp2p . addEventListener ( 'peer:connect' , ( { detail } ) => {
210231 try {
211- if ( detail . remotePeer . equals ( this . serverPeerId ) ) {
232+ if ( detail . equals ( this . serverPeerId ) ) {
212233 this . dispatchEvent ( new CustomEvent < void > ( 'connected' ) ) ;
213234 logger . trace (
214235 '🔗 Client connected to server at:' ,
@@ -222,7 +243,7 @@ export class Client<
222243
223244 this . libp2p . addEventListener ( 'peer:disconnect' , ( { detail } ) => {
224245 try {
225- if ( detail . remotePeer . equals ( this . serverPeerId ) ) {
246+ if ( detail . equals ( this . serverPeerId ) ) {
226247 this . dispatchEvent ( new CustomEvent < void > ( 'disconnected' ) ) ;
227248 logger . trace (
228249 '🔌 Client disconnected from server at:' ,
@@ -234,34 +255,37 @@ export class Client<
234255 }
235256 } ) ;
236257
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 } ` ) ;
239262
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+ > ;
246269
247- // @todo Validate offer
270+ // @todo Validate offer
248271
249- logger . trace ( 'Offer received:' , offer ) ;
272+ logger . trace ( 'Offer received:' , offer ) ;
250273
251- // @todo Implement offer verification
274+ // @todo Implement offer verification
252275
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+ ) ;
265289
266290 await this . libp2p . start ( ) ;
267291 this . dispatchEvent ( new CustomEvent < void > ( 'start' ) ) ;
@@ -279,7 +303,7 @@ export class Client<
279303 throw new Error ( 'libp2p not initialized yet' ) ;
280304 }
281305
282- this . libp2p . pubsub
306+ ( this . libp2p . services . pubsub as CenterSub )
283307 . publish ( request . topic , encodeText ( stringify ( request ) ) )
284308 . then ( ( ) => {
285309 this . dispatchEvent (
@@ -302,7 +326,7 @@ export class Client<
302326 throw new Error ( 'libp2p not initialized yet' ) ;
303327 }
304328
305- this . libp2p . pubsub . subscribe ( topic ) ;
329+ ( this . libp2p . services . pubsub as CenterSub ) . subscribe ( topic ) ;
306330 }
307331
308332 /**
@@ -316,7 +340,7 @@ export class Client<
316340 throw new Error ( 'libp2p not initialized yet' ) ;
317341 }
318342
319- this . libp2p . pubsub . unsubscribe ( topic ) ;
343+ ( this . libp2p . services . pubsub as CenterSub ) . unsubscribe ( topic ) ;
320344 }
321345
322346 /**
0 commit comments