@@ -4,6 +4,7 @@ import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
4
4
import { multiaddr , Multiaddr , protocols } from '@multiformats/multiaddr'
5
5
import type { IncomingStreamData , Registrar } from '@libp2p/interface-registrar'
6
6
import type { PeerId } from '@libp2p/interface-peer-id'
7
+ import { peerIdFromString } from '@libp2p/peer-id'
7
8
import { WebRTCMultiaddrConnection } from '../maconn.js'
8
9
import type { Startable } from '@libp2p/interfaces/startable'
9
10
import { WebRTCPeerListener } from './listener.js'
@@ -75,15 +76,7 @@ export class WebRTCTransport implements Transport, Startable {
75
76
} )
76
77
}
77
78
78
- /*
79
- * dial connects to a remote via the circuit relay or any other protocol
80
- * and proceeds to upgrade to a webrtc connection.
81
- * multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
82
- * For a circuit relay, this will be of the form
83
- * <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
84
- */
85
- async dial ( ma : Multiaddr , options : DialOptions ) : Promise < Connection > {
86
- log . trace ( 'dialing address: ' , ma )
79
+ private splitAddr ( ma : Multiaddr ) : { baseAddr : Multiaddr , peerId : PeerId } {
87
80
const addrs = ma . toString ( ) . split ( `${ TRANSPORT } /` )
88
81
if ( addrs . length !== 2 ) {
89
82
throw new CodeError ( 'invalid multiaddr' , codes . ERR_INVALID_MULTIADDR )
@@ -97,11 +90,6 @@ export class WebRTCTransport implements Transport, Startable {
97
90
throw new CodeError ( 'bad destination' , codes . ERR_INVALID_MULTIADDR )
98
91
}
99
92
100
- if ( options . signal == null ) {
101
- const controller = new AbortController ( )
102
- options . signal = controller . signal
103
- }
104
-
105
93
const lastProtoInRemote = remoteAddr . protos ( ) . pop ( )
106
94
if ( lastProtoInRemote === undefined ) {
107
95
throw new CodeError ( 'invalid multiaddr' , codes . ERR_INVALID_MULTIADDR )
@@ -110,7 +98,26 @@ export class WebRTCTransport implements Transport, Startable {
110
98
remoteAddr = remoteAddr . encapsulate ( `/p2p/${ destinationIdString } ` )
111
99
}
112
100
113
- const connection = await this . components . transportManager . dial ( remoteAddr )
101
+ return { baseAddr : remoteAddr , peerId : peerIdFromString ( destinationIdString ) }
102
+ }
103
+
104
+ /*
105
+ * dial connects to a remote via the circuit relay or any other protocol
106
+ * and proceeds to upgrade to a webrtc connection.
107
+ * multiaddr of the form: <multiaddr>/webrtc/p2p/<destination-peer>
108
+ * For a circuit relay, this will be of the form
109
+ * <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
110
+ */
111
+ async dial ( ma : Multiaddr , options : DialOptions ) : Promise < Connection > {
112
+ log . trace ( 'dialing address: ' , ma )
113
+ const { baseAddr, peerId } = this . splitAddr ( ma )
114
+
115
+ if ( options . signal == null ) {
116
+ const controller = new AbortController ( )
117
+ options . signal = controller . signal
118
+ }
119
+
120
+ const connection = await this . components . transportManager . dial ( baseAddr )
114
121
115
122
const rawStream = await connection . newStream ( [ SIGNALING_PROTO_ID ] , options )
116
123
@@ -120,11 +127,12 @@ export class WebRTCTransport implements Transport, Startable {
120
127
rtcConfiguration : this . init . rtcConfiguration ,
121
128
signal : options . signal
122
129
} )
130
+ const webrtcMultiaddr = baseAddr . encapsulate ( `${ TRANSPORT } /p2p/${ peerId . toString ( ) } ` )
123
131
const result = await options . upgrader . upgradeOutbound (
124
132
new WebRTCMultiaddrConnection ( {
125
133
peerConnection : pc ,
126
134
timeline : { open : ( new Date ( ) ) . getTime ( ) } ,
127
- remoteAddr : connection . remoteAddr
135
+ remoteAddr : webrtcMultiaddr
128
136
} ) ,
129
137
{
130
138
skipProtection : true ,
@@ -150,10 +158,12 @@ export class WebRTCTransport implements Transport, Startable {
150
158
connection,
151
159
stream
152
160
} )
161
+ const remotePeerId = connection . remoteAddr . getPeerId ( )
162
+ const webrtcMultiaddr = connection . remoteAddr . encapsulate ( `${ TRANSPORT } /p2p/${ remotePeerId } ` )
153
163
await this . components . upgrader . upgradeInbound ( new WebRTCMultiaddrConnection ( {
154
164
peerConnection : pc ,
155
165
timeline : { open : ( new Date ( ) ) . getTime ( ) } ,
156
- remoteAddr : connection . remoteAddr
166
+ remoteAddr : webrtcMultiaddr
157
167
} ) , {
158
168
skipEncryption : true ,
159
169
skipProtection : true ,
0 commit comments