diff --git a/src/index.ts b/src/index.ts index 090a147..e761b5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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')), @@ -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 */ @@ -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 ;) ) @@ -116,6 +124,7 @@ const _P2P = or( P2PWebRTCStar, P2PWebRTCDirect, WebRTCDirect, + WebTransport, base('p2p') ) diff --git a/test/index.spec.ts b/test/index.spec.ts index c55b2e8..ccda9d4 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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) { @@ -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) + }) })