From f8471434e00f684e2cc4e569d8cea373d6021cec Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 4 Sep 2025 13:29:45 +0300 Subject: [PATCH 1/2] fix: match circuit relay address without target peer id 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. --- src/index.ts | 4 ++-- src/utils.ts | 18 ++++++++++++++++++ test/index.spec.ts | 5 ++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index bf3d1bc..76969d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,7 +33,7 @@ */ 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' -import { and, or, optional, fmt, code, value } from './utils.js' +import { and, or, optional, fmt, code, value, not } from './utils.js' import type { Multiaddr, Component } from '@multiformats/multiaddr' /** @@ -406,7 +406,7 @@ const _P2P = or( */ export const P2P = fmt(_P2P) -const _Circuit = and(_P2P, code(CODE_P2P_CIRCUIT), value(CODE_P2P)) +const _Circuit = and(optional(_P2P), code(CODE_P2P_CIRCUIT), not(code(CODE_WEBRTC)), optional(value(CODE_P2P))) /** * Matches circuit relay addresses diff --git a/src/utils.ts b/src/utils.ts index 8676f3a..dec8d35 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -52,6 +52,24 @@ export const value = (code: number, value?: string): Matcher => { } } +/** + * Matches a multiaddr component with the specified code and value. If the value + * is omitted any non-undefined value is matched. + */ +export const not = (matcher: Matcher): Matcher => { + return { + match: (vals) => { + const result = matcher.match(vals) + + if (result === false) { + return vals + } + + return false + } + } +} + /** * An optional matcher */ diff --git a/test/index.spec.ts b/test/index.spec.ts index c7deed5..12d2983 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -214,6 +214,7 @@ describe('multiaddr matcher', () => { ] const goodCircuit = [ + '/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit', '/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79', '/ip4/0.0.0.0/tcp/12345/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79', '/dns/example.org/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79', @@ -231,7 +232,9 @@ describe('multiaddr matcher', () => { '/ip4/0.0.7.6/udp/1234', '/ip6/::/udp/0/utp', '/dnsaddr/ipfs.io/ws', - '/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-star' + '/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-star', + '/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc', + '/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79', ] const goodIPFS = [ From 72ea7d1e1f16ba7fba294cf939345aa4cb355248 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 4 Sep 2025 13:33:18 +0300 Subject: [PATCH 2/2] chore: linting --- test/index.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.spec.ts b/test/index.spec.ts index 12d2983..2ec8a8c 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -234,7 +234,7 @@ describe('multiaddr matcher', () => { '/dnsaddr/ipfs.io/ws', '/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-star', '/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc', - '/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79', + '/ip4/0.0.0.0/tcp/12345/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/p2p-circuit/webrtc/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79' ] const goodIPFS = [