diff --git a/packages/transport-webrtc/src/private-to-public/transport.ts b/packages/transport-webrtc/src/private-to-public/transport.ts index c3430683d3..1e6dd936eb 100644 --- a/packages/transport-webrtc/src/private-to-public/transport.ts +++ b/packages/transport-webrtc/src/private-to-public/transport.ts @@ -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' @@ -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)) @@ -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 }) diff --git a/packages/transport-webrtc/test/transport.spec.ts b/packages/transport-webrtc/test/transport.spec.ts index bcde79bf60..b323ec9dd6 100644 --- a/packages/transport-webrtc/test/transport.spec.ts +++ b/packages/transport-webrtc/test/transport.spec.ts @@ -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', () => { @@ -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() - }) - } 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) - } - }) }) diff --git a/packages/transport-webrtc/test/util.ts b/packages/transport-webrtc/test/util.ts index fe3b3b42d2..d1af0108b6 100644 --- a/packages/transport-webrtc/test/util.ts +++ b/packages/transport-webrtc/test/util.ts @@ -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 */