1
+ import type { PeerId } from "@libp2p/interface-peer-id" ;
1
2
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory" ;
2
3
import { multiaddr } from "@multiformats/multiaddr" ;
3
4
import * as secp from "@noble/secp256k1" ;
@@ -10,7 +11,11 @@ import { ERR_INVALID_ID } from "./constants.js";
10
11
import { EnrCreator } from "./creator.js" ;
11
12
import { EnrDecoder } from "./decoder.js" ;
12
13
import { EnrEncoder } from "./encoder.js" ;
13
- import { ENR } from "./enr.js" ;
14
+ import {
15
+ ENR ,
16
+ TransportProtocol ,
17
+ TransportProtocolPerIpVersion ,
18
+ } from "./enr.js" ;
14
19
import { getPrivateKeyFromPeerId } from "./peer_id.js" ;
15
20
16
21
describe ( "ENR" , function ( ) {
@@ -42,7 +47,7 @@ describe("ENR", function () {
42
47
if ( ! enr2 . signature ) throw "enr.signature is undefined" ;
43
48
44
49
expect ( bytesToHex ( enr2 . signature ) ) . to . be . equal ( bytesToHex ( enr . signature ) ) ;
45
- const ma = enr2 . getLocationMultiaddr ( "udp" ) ! ;
50
+ const ma = enr2 . getLocationMultiaddr ( TransportProtocol . UDP ) ! ;
46
51
expect ( ma . toString ( ) ) . to . be . equal ( "/ip4/18.223.219.100/udp/9000" ) ;
47
52
expect ( enr2 . multiaddrs ) . to . not . be . undefined ;
48
53
expect ( enr2 . multiaddrs ! . length ) . to . be . equal ( 3 ) ;
@@ -256,16 +261,16 @@ describe("ENR", function () {
256
261
record . set ( "ip" , tuples0 [ 0 ] [ 1 ] ) ;
257
262
record . set ( "udp" , tuples0 [ 1 ] [ 1 ] ) ;
258
263
// and get the multiaddr
259
- expect ( record . getLocationMultiaddr ( "udp" ) ! . toString ( ) ) . to . equal (
260
- multi0 . toString ( )
261
- ) ;
264
+ expect (
265
+ record . getLocationMultiaddr ( TransportProtocol . UDP ) ! . toString ( )
266
+ ) . to . equal ( multi0 . toString ( ) ) ;
262
267
// set the multiaddr
263
268
const multi1 = multiaddr ( "/ip4/0.0.0.0/udp/30300" ) ;
264
269
record . setLocationMultiaddr ( multi1 ) ;
265
270
// and get the multiaddr
266
- expect ( record . getLocationMultiaddr ( "udp" ) ! . toString ( ) ) . to . equal (
267
- multi1 . toString ( )
268
- ) ;
271
+ expect (
272
+ record . getLocationMultiaddr ( TransportProtocol . UDP ) ! . toString ( )
273
+ ) . to . equal ( multi1 . toString ( ) ) ;
269
274
// and get the underlying records
270
275
const tuples1 = multi1 . tuples ( ) ;
271
276
expect ( record . get ( "ip" ) ) . to . deep . equal ( tuples1 [ 0 ] [ 1 ] ) ;
@@ -284,16 +289,16 @@ describe("ENR", function () {
284
289
record . set ( "ip" , tuples0 [ 0 ] [ 1 ] ) ;
285
290
record . set ( "tcp" , tuples0 [ 1 ] [ 1 ] ) ;
286
291
// and get the multiaddr
287
- expect ( record . getLocationMultiaddr ( "tcp" ) ! . toString ( ) ) . to . equal (
288
- multi0 . toString ( )
289
- ) ;
292
+ expect (
293
+ record . getLocationMultiaddr ( TransportProtocol . TCP ) ! . toString ( )
294
+ ) . to . equal ( multi0 . toString ( ) ) ;
290
295
// set the multiaddr
291
296
const multi1 = multiaddr ( "/ip4/0.0.0.0/tcp/30300" ) ;
292
297
record . setLocationMultiaddr ( multi1 ) ;
293
298
// and get the multiaddr
294
- expect ( record . getLocationMultiaddr ( "tcp" ) ! . toString ( ) ) . to . equal (
295
- multi1 . toString ( )
296
- ) ;
299
+ expect (
300
+ record . getLocationMultiaddr ( TransportProtocol . TCP ) ! . toString ( )
301
+ ) . to . equal ( multi1 . toString ( ) ) ;
297
302
// and get the underlying records
298
303
const tuples1 = multi1 . tuples ( ) ;
299
304
expect ( record . get ( "ip" ) ) . to . deep . equal ( tuples1 [ 0 ] [ 1 ] ) ;
@@ -306,7 +311,7 @@ describe("ENR", function () {
306
311
const ip6 = "::1" ;
307
312
const tcp = 8080 ;
308
313
const udp = 8080 ;
309
- let peerId ;
314
+ let peerId : PeerId ;
310
315
let enr : ENR ;
311
316
312
317
before ( async function ( ) {
@@ -321,66 +326,85 @@ describe("ENR", function () {
321
326
} ) ;
322
327
323
328
it ( "should properly create location multiaddrs - udp4" , ( ) => {
324
- expect ( enr . getLocationMultiaddr ( "udp4" ) ) . to . deep . equal (
325
- multiaddr ( `/ip4/ ${ ip4 } /udp/ ${ udp } ` )
326
- ) ;
329
+ expect (
330
+ enr . getLocationMultiaddr ( TransportProtocolPerIpVersion . UDP4 )
331
+ ) . to . deep . equal ( multiaddr ( `/ip4/ ${ ip4 } /udp/ ${ udp } ` ) ) ;
327
332
} ) ;
328
333
329
334
it ( "should properly create location multiaddrs - tcp4" , ( ) => {
330
- expect ( enr . getLocationMultiaddr ( "tcp4" ) ) . to . deep . equal (
331
- multiaddr ( `/ip4/ ${ ip4 } /tcp/ ${ tcp } ` )
332
- ) ;
335
+ expect (
336
+ enr . getLocationMultiaddr ( TransportProtocolPerIpVersion . TCP4 )
337
+ ) . to . deep . equal ( multiaddr ( `/ip4/ ${ ip4 } /tcp/ ${ tcp } ` ) ) ;
333
338
} ) ;
334
339
335
340
it ( "should properly create location multiaddrs - udp6" , ( ) => {
336
- expect ( enr . getLocationMultiaddr ( "udp6" ) ) . to . deep . equal (
337
- multiaddr ( `/ip6/ ${ ip6 } /udp/ ${ udp } ` )
338
- ) ;
341
+ expect (
342
+ enr . getLocationMultiaddr ( TransportProtocolPerIpVersion . UDP6 )
343
+ ) . to . deep . equal ( multiaddr ( `/ip6/ ${ ip6 } /udp/ ${ udp } ` ) ) ;
339
344
} ) ;
340
345
341
346
it ( "should properly create location multiaddrs - tcp6" , ( ) => {
342
- expect ( enr . getLocationMultiaddr ( "tcp6" ) ) . to . deep . equal (
343
- multiaddr ( `/ip6/ ${ ip6 } /tcp/ ${ tcp } ` )
344
- ) ;
347
+ expect (
348
+ enr . getLocationMultiaddr ( TransportProtocolPerIpVersion . TCP6 )
349
+ ) . to . deep . equal ( multiaddr ( `/ip6/ ${ ip6 } /tcp/ ${ tcp } ` ) ) ;
345
350
} ) ;
346
351
347
352
it ( "should properly create location multiaddrs - udp" , ( ) => {
348
353
// default to ip4
349
- expect ( enr . getLocationMultiaddr ( "udp" ) ) . to . deep . equal (
354
+ expect ( enr . getLocationMultiaddr ( TransportProtocol . UDP ) ) . to . deep . equal (
350
355
multiaddr ( `/ip4/${ ip4 } /udp/${ udp } ` )
351
356
) ;
352
357
// if ip6 is set, use it
353
358
enr . ip = undefined ;
354
- expect ( enr . getLocationMultiaddr ( "udp" ) ) . to . deep . equal (
359
+ expect ( enr . getLocationMultiaddr ( TransportProtocol . UDP ) ) . to . deep . equal (
355
360
multiaddr ( `/ip6/${ ip6 } /udp/${ udp } ` )
356
361
) ;
357
362
// if ip6 does not exist, use ip4
358
363
enr . ip6 = undefined ;
359
364
enr . ip = ip4 ;
360
- expect ( enr . getLocationMultiaddr ( "udp" ) ) . to . deep . equal (
365
+ expect ( enr . getLocationMultiaddr ( TransportProtocol . UDP ) ) . to . deep . equal (
361
366
multiaddr ( `/ip4/${ ip4 } /udp/${ udp } ` )
362
367
) ;
363
368
enr . ip6 = ip6 ;
364
369
} ) ;
365
370
366
371
it ( "should properly create location multiaddrs - tcp" , ( ) => {
367
372
// default to ip4
368
- expect ( enr . getLocationMultiaddr ( "tcp" ) ) . to . deep . equal (
373
+ expect ( enr . getLocationMultiaddr ( TransportProtocol . TCP ) ) . to . deep . equal (
369
374
multiaddr ( `/ip4/${ ip4 } /tcp/${ tcp } ` )
370
375
) ;
371
376
// if ip6 is set, use it
372
377
enr . ip = undefined ;
373
- expect ( enr . getLocationMultiaddr ( "tcp" ) ) . to . deep . equal (
378
+ expect ( enr . getLocationMultiaddr ( TransportProtocol . TCP ) ) . to . deep . equal (
374
379
multiaddr ( `/ip6/${ ip6 } /tcp/${ tcp } ` )
375
380
) ;
376
381
// if ip6 does not exist, use ip4
377
382
enr . ip6 = undefined ;
378
383
enr . ip = ip4 ;
379
- expect ( enr . getLocationMultiaddr ( "tcp" ) ) . to . deep . equal (
384
+ expect ( enr . getLocationMultiaddr ( TransportProtocol . TCP ) ) . to . deep . equal (
380
385
multiaddr ( `/ip4/${ ip4 } /tcp/${ tcp } ` )
381
386
) ;
382
387
enr . ip6 = ip6 ;
383
388
} ) ;
389
+
390
+ it ( "should properly create peer info with all multiaddrs" , ( ) => {
391
+ const peerInfo = enr . peerInfo ! ;
392
+ console . log ( peerInfo ) ;
393
+ expect ( peerInfo . id . toString ( ) ) . to . equal ( peerId . toString ( ) ) ;
394
+ expect ( peerInfo . multiaddrs . length ) . to . equal ( 4 ) ;
395
+ expect ( peerInfo . multiaddrs . map ( ( ma ) => ma . toString ( ) ) ) . to . contain (
396
+ multiaddr ( `/ip4/${ ip4 } /tcp/${ tcp } ` ) . toString ( )
397
+ ) ;
398
+ expect ( peerInfo . multiaddrs . map ( ( ma ) => ma . toString ( ) ) ) . to . contain (
399
+ multiaddr ( `/ip6/${ ip6 } /tcp/${ tcp } ` ) . toString ( )
400
+ ) ;
401
+ expect ( peerInfo . multiaddrs . map ( ( ma ) => ma . toString ( ) ) ) . to . contain (
402
+ multiaddr ( `/ip4/${ ip4 } /udp/${ udp } ` ) . toString ( )
403
+ ) ;
404
+ expect ( peerInfo . multiaddrs . map ( ( ma ) => ma . toString ( ) ) ) . to . contain (
405
+ multiaddr ( `/ip6/${ ip6 } /udp/${ udp } ` ) . toString ( )
406
+ ) ;
407
+ } ) ;
384
408
} ) ;
385
409
386
410
describe ( "waku2 key round trip" , async ( ) => {
0 commit comments