-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@libp2p/webtransport): handle dialing circuit addresses (#2054)
We should be able to dial WebRTC addresses over Circuit Relay over WebTransport but the `parseMultiaddr` function was rejecting them. Simplifies the parsing function to handle this and future address variants and adds tests.
- Loading branch information
1 parent
0ce318e
commit 20d5f22
Showing
4 changed files
with
117 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/transport-webtransport/test/utils/parse-multiaddr.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { expect } from 'aegir/chai' | ||
import { base64url } from 'multiformats/bases/base64' | ||
import { parseMultiaddr } from '../../src/utils/parse-multiaddr.js' | ||
|
||
describe('parse multiaddr', () => { | ||
describe('valid addresses', () => { | ||
it('parses relay address', () => { | ||
const relayPeer = '12D3KooWKtv8rpaXJkLCoH4C299wFCVBg1eMzZrPfaV37QVVJrhF' | ||
const targetPeer = '12D3KooWCDt87xcGVJWmQpaXGTSaevbRpAoMJqvsVuETDrQJvSC5' | ||
const certHashes = [ | ||
'uEiCvU3clCu16U6Xjh9dzH7yKE2bkGftZw404nYMR6ZXIyg', | ||
'uEiB8ZfHAe_lEBtxio0KQwmE8mFEesh3p_7-Ac5oOU7HhOw' | ||
] | ||
|
||
const ma = multiaddr(`/ip4/154.38.162.255/udp/4001/quic-v1/webtransport/${certHashes.map(c => `certhash/${c}`).join('/')}/p2p/${relayPeer}/p2p-circuit/p2p/${targetPeer}`) | ||
const { url, certhashes, remotePeer } = parseMultiaddr(ma) | ||
|
||
expect(url).to.equal('https://154.38.162.255:4001') | ||
expect(certhashes.map(hash => base64url.encode(hash.bytes))).to.deep.equal(certHashes) | ||
expect(remotePeer?.toString()).to.equal(relayPeer.toString()) | ||
}) | ||
|
||
it('parses WebRTC relay address', () => { | ||
const relayPeer = '12D3KooWKtv8rpaXJkLCoH4C299wFCVBg1eMzZrPfaV37QVVJrhF' | ||
const targetPeer = '12D3KooWCDt87xcGVJWmQpaXGTSaevbRpAoMJqvsVuETDrQJvSC5' | ||
const certHashes = [ | ||
'uEiCvU3clCu16U6Xjh9dzH7yKE2bkGftZw404nYMR6ZXIyg', | ||
'uEiB8ZfHAe_lEBtxio0KQwmE8mFEesh3p_7-Ac5oOU7HhOw' | ||
] | ||
|
||
const ma = multiaddr(`/ip4/154.38.162.255/udp/4001/quic-v1/webtransport/${certHashes.map(c => `certhash/${c}`).join('/')}/p2p/${relayPeer}/p2p-circuit/webrtc/p2p/${targetPeer}`) | ||
const { url, certhashes, remotePeer } = parseMultiaddr(ma) | ||
|
||
expect(url).to.equal('https://154.38.162.255:4001') | ||
expect(certhashes.map(hash => base64url.encode(hash.bytes))).to.deep.equal(certHashes) | ||
expect(remotePeer?.toString()).to.equal(relayPeer) | ||
}) | ||
|
||
it('parses ip6 loopback address', () => { | ||
const targetPeer = '12D3KooWCDt87xcGVJWmQpaXGTSaevbRpAoMJqvsVuETDrQJvSC5' | ||
const certHashes = [ | ||
'uEiCvU3clCu16U6Xjh9dzH7yKE2bkGftZw404nYMR6ZXIyg', | ||
'uEiB8ZfHAe_lEBtxio0KQwmE8mFEesh3p_7-Ac5oOU7HhOw' | ||
] | ||
|
||
const ma = multiaddr(`/ip6/::1/udp/4001/quic-v1/webtransport/${certHashes.map(c => `certhash/${c}`).join('/')}/p2p/${targetPeer}`) | ||
const { url, certhashes, remotePeer } = parseMultiaddr(ma) | ||
|
||
expect(url).to.equal('https://[::1]:4001') | ||
expect(certhashes.map(hash => base64url.encode(hash.bytes))).to.deep.equal(certHashes) | ||
expect(remotePeer?.toString()).to.equal(targetPeer) | ||
}) | ||
}) | ||
|
||
describe('invalid addresses', () => { | ||
it('fails to parse a non-webtransport address', () => { | ||
const targetPeer = '12D3KooWCDt87xcGVJWmQpaXGTSaevbRpAoMJqvsVuETDrQJvSC5' | ||
const ma = multiaddr(`/ip4/123.123.123.123/udp/4001/p2p/${targetPeer}`) | ||
|
||
expect(() => parseMultiaddr(ma)).to.throw() | ||
.with.property('code', 'ERR_INVALID_MULTIADDR') | ||
}) | ||
|
||
it('fails to parse a webtransport address without certhashes', () => { | ||
const targetPeer = '12D3KooWCDt87xcGVJWmQpaXGTSaevbRpAoMJqvsVuETDrQJvSC5' | ||
const ma = multiaddr(`/ip4/123.123.123.123/udp/4001/webtransport/p2p/${targetPeer}`) | ||
|
||
expect(() => parseMultiaddr(ma)).to.throw() | ||
.with.property('code', 'ERR_INVALID_MULTIADDR') | ||
}) | ||
}) | ||
}) |