File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,23 @@ export interface MultiaddrMatcher {
69
69
exactMatch ( ma : Multiaddr ) : boolean
70
70
}
71
71
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
+
72
89
/**
73
90
* DNS matchers
74
91
*/
Original file line number Diff line number Diff line change @@ -354,6 +354,22 @@ describe('multiaddr matcher', () => {
354
354
'/ip4/0.0.0.0/udp/80/http'
355
355
]
356
356
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
+
357
373
function assertMatches ( p : MultiaddrMatcher , ...tests : string [ ] [ ] ) : void {
358
374
tests . forEach ( ( test ) => {
359
375
test . forEach ( ( testcase ) => {
@@ -474,4 +490,10 @@ describe('multiaddr matcher', () => {
474
490
assertExactMatches ( mafmt . Memory , exactMemory )
475
491
assertMismatches ( mafmt . Memory , badMemory )
476
492
} )
493
+
494
+ it ( 'PeerID addresses' , ( ) => {
495
+ assertMatches ( mafmt . PEER_ID , goodPeer )
496
+ assertExactMatches ( mafmt . PEER_ID , exactPeer )
497
+ assertMismatches ( mafmt . PEER_ID , badPeer )
498
+ } )
477
499
} )
You can’t perform that action at this time.
0 commit comments