Skip to content

Commit 9477624

Browse files
authored
feat: add matcher for /p2p/Qmfoo addresses (#46)
Allows matching multiaddrs that just contain a peer id without and routing information.
1 parent f2d5f25 commit 9477624

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ export interface MultiaddrMatcher {
6969
exactMatch(ma: Multiaddr): boolean
7070
}
7171

72+
/**
73+
* Matches PeerId addresses
74+
*
75+
* @example
76+
*
77+
* ```ts
78+
* import { multiaddr } from '@multiformats/multiaddr'
79+
* import { PEER_ID } from '@multiformats/multiaddr-matcher'
80+
*
81+
* PEER_ID.matches(multiaddr('/p2p/Qmfoo')) // true
82+
* PEER_ID.matches(multiaddr('/ipfs/Qmfoo')) // true
83+
* ```
84+
*/
85+
const _PEER_ID = peerId()
86+
87+
export const PEER_ID = fmt(_PEER_ID)
88+
7289
/**
7390
* DNS matchers
7491
*/

test/index.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,22 @@ describe('multiaddr matcher', () => {
354354
'/ip4/0.0.0.0/udp/80/http'
355355
]
356356

357+
const exactPeer = [
358+
'/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
359+
'/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
360+
'/ipfs/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
361+
'/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
362+
]
363+
364+
const goodPeer = [
365+
...exactPeer
366+
]
367+
368+
const badPeer = [
369+
'/ip4/0.0.0.0/udp/80/http',
370+
'/memory/0xDEADBEEF'
371+
]
372+
357373
function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
358374
tests.forEach((test) => {
359375
test.forEach((testcase) => {
@@ -474,4 +490,10 @@ describe('multiaddr matcher', () => {
474490
assertExactMatches(mafmt.Memory, exactMemory)
475491
assertMismatches(mafmt.Memory, badMemory)
476492
})
493+
494+
it('PeerID addresses', () => {
495+
assertMatches(mafmt.PEER_ID, goodPeer)
496+
assertExactMatches(mafmt.PEER_ID, exactPeer)
497+
assertMismatches(mafmt.PEER_ID, badPeer)
498+
})
477499
})

0 commit comments

Comments
 (0)