Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
fix: Update splitAddr function to correctly parse multiaddrs (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel-G authored Jun 1, 2023
1 parent bc3ae14 commit 22a7029
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
62 changes: 31 additions & 31 deletions src/private-to-private/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,6 @@ export class WebRTCTransport implements Transport, Startable {
})
}

private splitAddr (ma: Multiaddr): { baseAddr: Multiaddr, peerId: PeerId } {
const addrs = ma.toString().split(WEBRTC_TRANSPORT)
if (addrs.length !== 2) {
throw new CodeError('webrtc protocol was not present in multiaddr', codes.ERR_INVALID_MULTIADDR)
}

if (!addrs[0].includes(CIRCUIT_RELAY_TRANSPORT)) {
throw new CodeError('p2p-circuit protocol was not present in multiaddr', codes.ERR_INVALID_MULTIADDR)
}

// look for remote peerId
let remoteAddr = multiaddr(addrs[0])
const destination = multiaddr(addrs[1])

const destinationIdString = destination.getPeerId()
if (destinationIdString == null) {
throw new CodeError('destination peer id was missing', codes.ERR_INVALID_MULTIADDR)
}

const lastProtoInRemote = remoteAddr.protos().pop()
if (lastProtoInRemote === undefined) {
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR)
}
if (lastProtoInRemote.name !== 'p2p') {
remoteAddr = remoteAddr.encapsulate(`/p2p/${destinationIdString}`)
}

return { baseAddr: remoteAddr, peerId: peerIdFromString(destinationIdString) }
}

/*
* dial connects to a remote via the circuit relay or any other protocol
* and proceeds to upgrade to a webrtc connection.
Expand All @@ -111,7 +81,7 @@ export class WebRTCTransport implements Transport, Startable {
*/
async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
log.trace('dialing address: ', ma)
const { baseAddr, peerId } = this.splitAddr(ma)
const { baseAddr, peerId } = splitAddr(ma)

if (options.signal == null) {
const controller = new AbortController()
Expand Down Expand Up @@ -176,3 +146,33 @@ export class WebRTCTransport implements Transport, Startable {
}
}
}

export function splitAddr (ma: Multiaddr): { baseAddr: Multiaddr, peerId: PeerId } {
const addrs = ma.toString().split(WEBRTC_TRANSPORT + '/')
if (addrs.length !== 2) {
throw new CodeError('webrtc protocol was not present in multiaddr', codes.ERR_INVALID_MULTIADDR)
}

if (!addrs[0].includes(CIRCUIT_RELAY_TRANSPORT)) {
throw new CodeError('p2p-circuit protocol was not present in multiaddr', codes.ERR_INVALID_MULTIADDR)
}

// look for remote peerId
let remoteAddr = multiaddr(addrs[0])
const destination = multiaddr('/' + addrs[1])

const destinationIdString = destination.getPeerId()
if (destinationIdString == null) {
throw new CodeError('destination peer id was missing', codes.ERR_INVALID_MULTIADDR)
}

const lastProtoInRemote = remoteAddr.protos().pop()
if (lastProtoInRemote === undefined) {
throw new CodeError('invalid multiaddr', codes.ERR_INVALID_MULTIADDR)
}
if (lastProtoInRemote.name !== 'p2p') {
remoteAddr = remoteAddr.encapsulate(`/p2p/${destinationIdString}`)
}

return { baseAddr: remoteAddr, peerId: peerIdFromString(destinationIdString) }
}
22 changes: 21 additions & 1 deletion test/peer.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { pbStream } from 'it-pb-stream'
import Sinon from 'sinon'
import { initiateConnection, handleIncomingStream } from '../src/private-to-private/handler'
import { Message } from '../src/private-to-private/pb/message.js'
import { WebRTCTransport } from '../src/private-to-private/transport'
import { WebRTCTransport, splitAddr } from '../src/private-to-private/transport'

const browser = detect()

Expand Down Expand Up @@ -109,4 +109,24 @@ describe('webrtc filter', () => {
})
})

describe('webrtc splitAddr', () => {
it('can split a ws relay addr', async () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/49173/ws/p2p/12D3KooWFqpHsdZaL4NW6eVE3yjhoSDNv7HJehPZqj17kjKntAh2/p2p-circuit/webrtc/p2p/12D3KooWF2P1k8SVRL1cV1Z9aNM8EVRwbrMESyRf58ceQkaht4AF')

const { baseAddr, peerId } = splitAddr(ma)

expect(baseAddr.toString()).to.eq('/ip4/127.0.0.1/tcp/49173/ws/p2p/12D3KooWFqpHsdZaL4NW6eVE3yjhoSDNv7HJehPZqj17kjKntAh2/p2p-circuit/p2p/12D3KooWF2P1k8SVRL1cV1Z9aNM8EVRwbrMESyRf58ceQkaht4AF')
expect(peerId.toString()).to.eq('12D3KooWF2P1k8SVRL1cV1Z9aNM8EVRwbrMESyRf58ceQkaht4AF')
})

it('can split a webrtc-direct relay addr', async () => {
const ma = multiaddr('/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiBUr89tH2P9paTCPn-AcfVZcgvIvkwns96t4h55IpxFtA/p2p/12D3KooWB64sJqc3T3VCaubQCrfCvvfummrAA9z1vEXHJT77ZNJh/p2p-circuit/webrtc/p2p/12D3KooWFNBgv86tcpcYUHQz9FWGTrTmpMgr8feZwQXQySVTo3A7')

const { baseAddr, peerId } = splitAddr(ma)

expect(baseAddr.toString()).to.eq('/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiBUr89tH2P9paTCPn-AcfVZcgvIvkwns96t4h55IpxFtA/p2p/12D3KooWB64sJqc3T3VCaubQCrfCvvfummrAA9z1vEXHJT77ZNJh/p2p-circuit/p2p/12D3KooWFNBgv86tcpcYUHQz9FWGTrTmpMgr8feZwQXQySVTo3A7')
expect(peerId.toString()).to.eq('12D3KooWFNBgv86tcpcYUHQz9FWGTrTmpMgr8feZwQXQySVTo3A7')
})
})

export { }

0 comments on commit 22a7029

Please sign in to comment.