Skip to content

Commit

Permalink
feat: add webrtc addresses (#130)
Browse files Browse the repository at this point in the history
Adds webrtc address matching
  • Loading branch information
ckousik committed Feb 28, 2023
1 parent 6589adc commit d0ce7d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export const HTTPS = or(
and(DNS, base('https'))
)

const _WebRTC = and(UDP, base('webrtc'), base('certhash'))
export const WebRTC = or(
and(_WebRTC, base('p2p')),
_WebRTC
)

export const WebRTCStar = or(
and(WebSockets, base('p2p-webrtc-star'), base('p2p')),
and(WebSocketsSecure, base('p2p-webrtc-star'), base('p2p')),
Expand Down Expand Up @@ -89,7 +95,8 @@ export const Reliable = or(
TCP,
UTP,
QUIC,
DNS
DNS,
WebRTC
)

// 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 @@ -102,6 +109,7 @@ const _P2P = or(
and(Reliable, base('p2p')),
WebRTCStar,
WebRTCDirect,
WebRTC,
base('p2p')
)

Expand Down
16 changes: 16 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ describe('multiaddr validation', function () {
'/dns6/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
].concat(goodCircuit)

const goodWebRTC = [
'/ip4/0.0.0.0/udp/4004/webrtc/certhash/uEiAeP0OEmBbGVTH5Bhnm3WopwRNSQ0et46xNkn2dIagnGw',
'/ip4/0.0.0.0/udp/4004/webrtc/certhash/uEiAeP0OEmBbGVTH5Bhnm3WopwRNSQ0et46xNkn2dIagnGw/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
]

const badWebRTC = [
'/ip4/0.0.0.0/udp/4004/webrtc',
'/ip4/0.0.0.0/tcp/4004/webrtc',
'/ip4/0.0.0.0/udp/4004/webrtc/uEiAeP0OEmBbGVTH5Bhnm3WopwRNSQ0et46xNkn2dIagnGw/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
]

function assertMatches (p: Mafmt, ...tests: string[][]) {
tests.forEach(function (test) {
test.forEach(function (testcase) {
Expand Down Expand Up @@ -323,4 +334,9 @@ describe('multiaddr validation', function () {
it('IPFS validation', function () {
assertMatches(mafmt.IPFS, goodIPFS)
})

it('WebRTC validation', function () {
assertMatches(mafmt.WebRTC, goodWebRTC)
assertMismatches(mafmt.WebRTC, badWebRTC)
})
})

0 comments on commit d0ce7d7

Please sign in to comment.