Skip to content

Commit 26f2ff6

Browse files
authored
fix: match peer ids in tcp and dns addresses (#35)
Allows matching peer ids in tcp and dns addresses.
1 parent 1111bc3 commit 26f2ff6

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/index.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const _DNS = and(literal('dns'), string())
286286
* DNS4.matches(multiaddr('/dns4/example.org')) // true
287287
* ```
288288
*/
289-
export const DNS4 = fmt(_DNS4)
289+
export const DNS4 = fmt(_DNS4, optional(peerId()))
290290

291291
/**
292292
* Matches dns6 addresses.
@@ -302,7 +302,7 @@ export const DNS4 = fmt(_DNS4)
302302
* DNS6.matches(multiaddr('/dns6/example.org')) // true
303303
* ```
304304
*/
305-
export const DNS6 = fmt(_DNS6)
305+
export const DNS6 = fmt(_DNS6, optional(peerId()))
306306

307307
/**
308308
* Matches dnsaddr addresses.
@@ -316,9 +316,10 @@ export const DNS6 = fmt(_DNS6)
316316
* import { DNSADDR } from '@multiformats/multiaddr-matcher'
317317
*
318318
* DNSADDR.matches(multiaddr('/dnsaddr/example.org')) // true
319+
* DNSADDR.matches(multiaddr('/dnsaddr/example.org/p2p/Qmfoo')) // true
319320
* ```
320321
*/
321-
export const DNSADDR = fmt(_DNSADDR)
322+
export const DNSADDR = fmt(_DNSADDR, optional(peerId()))
322323

323324
/**
324325
* Matches any dns address.
@@ -332,21 +333,14 @@ export const DNSADDR = fmt(_DNSADDR)
332333
* DNS.matches(multiaddr('/dnsaddr/example.org')) // true
333334
* DNS.matches(multiaddr('/dns4/example.org')) // true
334335
* DNS.matches(multiaddr('/dns6/example.org')) // true
336+
* DNS.matches(multiaddr('/dns6/example.org/p2p/Qmfoo')) // true
335337
* ```
336338
*/
337-
export const DNS = fmt(or(
338-
_DNS,
339-
_DNSADDR,
340-
_DNS4,
341-
_DNS6
342-
))
339+
export const DNS = fmt(or(_DNS, _DNSADDR, _DNS4, _DNS6), optional(peerId()))
343340

344341
const _IP4 = and(literal('ip4'), func(isIPv4))
345342
const _IP6 = and(literal('ip6'), func(isIPv6))
346-
const _IP = or(
347-
_IP4,
348-
_IP6
349-
)
343+
const _IP = or(_IP4, _IP6)
350344

351345
const _IP_OR_DOMAIN = or(_IP, _DNS, _DNS4, _DNS6, _DNSADDR)
352346

@@ -359,12 +353,13 @@ const _IP_OR_DOMAIN = or(_IP, _DNS, _DNS4, _DNS6, _DNSADDR)
359353
* import { multiaddr } from '@multiformats/multiaddr'
360354
* import { IP_OR_DOMAIN } from '@multiformats/multiaddr-matcher'
361355
*
356+
* IP_OR_DOMAIN.matches(multiaddr('/ip4/123.123.123.123')) // true
362357
* IP_OR_DOMAIN.matches(multiaddr('/ip4/123.123.123.123/p2p/QmFoo')) // true
363358
* IP_OR_DOMAIN.matches(multiaddr('/dns/example.com/p2p/QmFoo')) // true
364359
* IP_OR_DOMAIN.matches(multiaddr('/p2p/QmFoo')) // false
365360
* ```
366361
*/
367-
export const IP_OR_DOMAIN = fmt(_IP_OR_DOMAIN)
362+
export const IP_OR_DOMAIN = fmt(or(_IP, and(or(_DNS, _DNSADDR, _DNS4, _DNS6), optional(peerId()))))
368363

369364
/**
370365
* Matches ip4 addresses.
@@ -432,7 +427,7 @@ const _UDP = and(_IP_OR_DOMAIN, literal('udp'), number())
432427
* TCP.matches(multiaddr('/ip4/123.123.123.123/tcp/1234')) // true
433428
* ```
434429
*/
435-
export const TCP = fmt(_TCP)
430+
export const TCP = fmt(and(_TCP, optional(peerId())))
436431

437432
/**
438433
* Matches UDP addresses.

test/index.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ describe('multiaddr matcher', () => {
1010
'/dnsaddr/ipfs.io',
1111
'/dns4/ipfs.io',
1212
'/dns4/libp2p.io',
13-
'/dns6/protocol.ai'
13+
'/dns6/protocol.ai',
14+
'/dns6/protocol.ai/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79'
1415
]
1516

1617
const goodDNS = [
@@ -46,6 +47,7 @@ describe('multiaddr matcher', () => {
4647

4748
const exactTCP = [
4849
'/ip4/0.0.7.6/tcp/1234',
50+
'/ip4/0.0.7.6/tcp/1234/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
4951
'/ip6/::/tcp/0',
5052
'/dns4/protocol.ai/tcp/80',
5153
'/dns6/protocol.ai/tcp/80',

0 commit comments

Comments
 (0)