Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support webtransport multiaddrs #148

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const UDP = and(IP, base('udp'))
export const UTP = and(UDP, base('utp'))

export const QUIC = and(UDP, base('quic'))
export const QUICV1 = and(UDP, base('quic-v1'))

export const WebSockets = or(
and(TCP, base('ws')),
Expand Down Expand Up @@ -64,6 +65,12 @@ export const WebRTCDirect = or(
_WebRTCDirect
)

const _WebTransport = and(QUICV1, base('webtransport'), base('certhash'), base('certhash'))
export const WebTransport = or(
and(_WebTransport, base('p2p')),
_WebTransport
)

/**
* @deprecated
*/
Expand Down Expand Up @@ -102,7 +109,8 @@ export const Reliable = or(
UTP,
QUIC,
DNS,
WebRTCDirect
WebRTCDirect,
WebTransport
)

// Unlike ws-star, stardust can run over any transport thus removing the requirement for websockets (but don't even think about running a stardust server over webrtc-star ;) )
Expand All @@ -116,6 +124,7 @@ const _P2P = or(
P2PWebRTCStar,
P2PWebRTCDirect,
WebRTCDirect,
WebTransport,
base('p2p')
)

Expand Down
23 changes: 23 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ describe('multiaddr validation', function () {
'/ip4/0.0.0.0/tcp/12345/udp/2222/wss/webrtc'
]

const goodWebTransport = [
'/ip4/10.5.0.2/udp/4001/quic-v1/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/uEiCoik2HBeT5oc9Jib3SQJzNjn9AnznMDpQWcOeKSuEc9A/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
'/ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/uEiCoik2HBeT5oc9Jib3SQJzNjn9AnznMDpQWcOeKSuEc9A/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
'/ip4/97.126.16.119/udp/4001/quic-v1/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/uEiCoik2HBeT5oc9Jib3SQJzNjn9AnznMDpQWcOeKSuEc9A/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
'/ip6/::1/udp/4001/quic-v1/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/uEiCoik2HBeT5oc9Jib3SQJzNjn9AnznMDpQWcOeKSuEc9A/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]

const badWebTransport = [
// quic instead of quic-v1
'/ip4/10.5.0.2/udp/4001/quic/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/uEiCoik2HBeT5oc9Jib3SQJzNjn9AnznMDpQWcOeKSuEc9A/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
// missing second certhash value
'/ip4/10.5.0.2/udp/4001/quic-v1/webtransport/certhash/uEiDWmsTxXe55Mbwnvd1qrPZAcE5Jtc0tE9WtGXD_NpMERg/certhash/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
// missing webtransport/certhash base
'/ip4/10.5.0.2/udp/4001/quic-v1/webtransport/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v',
// missing value for base 'webtransport/certhash' `${value}/certhash/${value}`
'/ip4/10.5.0.2/udp/4001/quic-v1/webtransport/certhash/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]

function assertMatches (p: Mafmt, ...tests: string[][]): void {
tests.forEach(function (test) {
test.forEach(function (testcase) {
Expand Down Expand Up @@ -355,4 +373,9 @@ describe('multiaddr validation', function () {
assertMatches(mafmt.WebRTC, goodWebRTC)
assertMismatches(mafmt.WebRTC, badWebRTC)
})

it('WebTransport validation', function () {
assertMatches(mafmt.WebTransport, goodWebTransport)
assertMismatches(mafmt.WebTransport, badWebTransport)
})
})