Skip to content

Commit d4a4c66

Browse files
authored
fix: match circuit relay address without target peer id (#65)
Sometimes the target peer id is in the context of the invocation, for example a PeerInfo object doesn't have the peer id of the peer appended to every multiaddr as it is a waste of space.
1 parent f5d987c commit d4a4c66

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434

3535
import { CODE_P2P, CODE_DNS4, CODE_DNS6, CODE_DNSADDR, CODE_DNS, CODE_IP4, CODE_IP6, CODE_TCP, CODE_UDP, CODE_QUIC, CODE_QUIC_V1, CODE_WS, CODE_WSS, CODE_TLS, CODE_SNI, CODE_WEBRTC_DIRECT, CODE_CERTHASH, CODE_WEBTRANSPORT, CODE_P2P_CIRCUIT, CODE_WEBRTC, CODE_HTTP, CODE_UNIX, CODE_HTTPS, CODE_MEMORY, CODE_IP6ZONE, CODE_IPCIDR } from '@multiformats/multiaddr'
36-
import { and, or, optional, fmt, code, value } from './utils.js'
36+
import { and, or, optional, fmt, code, value, not } from './utils.js'
3737
import type { Multiaddr, Component } from '@multiformats/multiaddr'
3838

3939
/**
@@ -406,7 +406,7 @@ const _P2P = or(
406406
*/
407407
export const P2P = fmt(_P2P)
408408

409-
const _Circuit = and(_P2P, code(CODE_P2P_CIRCUIT), value(CODE_P2P))
409+
const _Circuit = and(optional(_P2P), code(CODE_P2P_CIRCUIT), not(code(CODE_WEBRTC)), optional(value(CODE_P2P)))
410410

411411
/**
412412
* Matches circuit relay addresses

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ export const value = (code: number, value?: string): Matcher => {
5252
}
5353
}
5454

55+
/**
56+
* Matches a multiaddr component with the specified code and value. If the value
57+
* is omitted any non-undefined value is matched.
58+
*/
59+
export const not = (matcher: Matcher): Matcher => {
60+
return {
61+
match: (vals) => {
62+
const result = matcher.match(vals)
63+
64+
if (result === false) {
65+
return vals
66+
}
67+
68+
return false
69+
}
70+
}
71+
}
72+
5573
/**
5674
* An optional matcher
5775
*/

test/index.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ describe('multiaddr matcher', () => {
214214
]
215215

216216
const goodCircuit = [
217+
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit',
217218
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
218219
'/ip4/0.0.0.0/tcp/12345/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
219220
'/dns/example.org/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
@@ -231,7 +232,9 @@ describe('multiaddr matcher', () => {
231232
'/ip4/0.0.7.6/udp/1234',
232233
'/ip6/::/udp/0/utp',
233234
'/dnsaddr/ipfs.io/ws',
234-
'/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-star'
235+
'/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-star',
236+
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc',
237+
'/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79'
235238
]
236239

237240
const goodIPFS = [

0 commit comments

Comments
 (0)