Skip to content

Commit

Permalink
fix: do not require peer id for webrtc-direct (#2820)
Browse files Browse the repository at this point in the history
We can get their peer id during the noise handshake, we do not need
to have it in the multiaddr.  We need it for regular webrtc as the
SDP handshake happens via a relay, but that's not the case here.
  • Loading branch information
achingbrain authored Nov 14, 2024
1 parent c917314 commit 2feaedd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 30 deletions.
12 changes: 6 additions & 6 deletions packages/transport-webrtc/src/private-to-public/transport.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { noise } from '@chainsafe/libp2p-noise'
import { transportSymbol, serviceCapabilities, InvalidParametersError } from '@libp2p/interface'
import * as p from '@libp2p/peer-id'
import { peerIdFromString } from '@libp2p/peer-id'
import { protocols } from '@multiformats/multiaddr'
import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
import * as Digest from 'multiformats/hashes/digest'
import { concat } from 'uint8arrays/concat'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
import { DataChannelError, InappropriateMultiaddrError, UnimplementedError } from '../error.js'
import { DataChannelError, UnimplementedError } from '../error.js'
import { WebRTCMultiaddrConnection } from '../maconn.js'
import { DataChannelMuxerFactory } from '../muxer.js'
import { createStream } from '../stream.js'
Expand Down Expand Up @@ -121,11 +121,11 @@ export class WebRTCDirectTransport implements Transport {
const controller = new AbortController()
const signal = controller.signal

let remotePeer: PeerId | undefined
const remotePeerString = ma.getPeerId()
if (remotePeerString === null) {
throw new InappropriateMultiaddrError("we need to have the remote's PeerId")
if (remotePeerString != null) {
remotePeer = peerIdFromString(remotePeerString)
}
const theirPeerId = p.peerIdFromString(remotePeerString)

const remoteCerthash = sdp.decodeCerthash(sdp.certhash(ma))

Expand Down Expand Up @@ -261,7 +261,7 @@ export class WebRTCDirectTransport implements Transport {
// Therefore, we need to secure an inbound noise connection from the remote.
await connectionEncrypter.secureInbound(wrappedDuplex, {
signal,
remotePeer: theirPeerId
remotePeer
})

return await options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory })
Expand Down
15 changes: 0 additions & 15 deletions packages/transport-webrtc/test/transport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { expect } from 'aegir/chai'
import { stubInterface } from 'sinon-ts'
import { UnimplementedError } from '../src/error.js'
import { WebRTCDirectTransport, type WebRTCDirectTransportComponents } from '../src/private-to-public/transport.js'
import { expectError } from './util.js'
import type { Upgrader } from '@libp2p/interface'

describe('WebRTCDirect Transport', () => {
Expand Down Expand Up @@ -81,18 +80,4 @@ describe('WebRTCDirect Transport', () => {
...invalid
])).to.deep.equal(valid)
})

it('throws WebRTC transport error when dialing a multiaddr without a PeerId', async () => {
const ma = multiaddr('/ip4/1.2.3.4/udp/1234/webrtc-direct/certhash/uEiAUqV7kzvM1wI5DYDc1RbcekYVmXli_Qprlw3IkiEg6tQ')
const transport = new WebRTCDirectTransport(components)

try {
await transport.dial(ma, {
upgrader: stubInterface<Upgrader>()
})
} catch (error: any) {
const expected = 'WebRTC transport error: There was a problem with the Multiaddr which was passed in: we need to have the remote\'s PeerId'
expectError(error, expected)
}
})
})
9 changes: 0 additions & 9 deletions packages/transport-webrtc/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { expect } from 'aegir/chai'
import * as lengthPrefixed from 'it-length-prefixed'
import { Message } from '../src/pb/message.js'

export const expectError = (error: unknown, message: string): void => {
if (error instanceof Error) {
expect(error.message).to.equal(message)
} else {
expect('Did not throw error:').to.equal(message)
}
}

/**
* simulates receiving a FIN_ACK on the passed datachannel
*/
Expand Down

0 comments on commit 2feaedd

Please sign in to comment.