@@ -3,37 +3,38 @@ import { InvalidParametersError, TypedEventEmitter } from '@libp2p/interface'
3
3
import { getThinWaistAddresses } from '@libp2p/utils/get-thin-waist-addresses'
4
4
import { multiaddr , fromStringTuples } from '@multiformats/multiaddr'
5
5
import { WebRTCDirect } from '@multiformats/multiaddr-matcher'
6
- import { Crypto } from '@peculiar/webcrypto'
7
6
import getPort from 'get-port'
8
7
import pWaitFor from 'p-wait-for'
9
8
import { CODEC_CERTHASH , CODEC_WEBRTC_DIRECT } from '../constants.js'
10
9
import { connect } from './utils/connect.js'
11
- import { generateTransportCertificate } from './utils/generate-certificates.js'
12
10
import { createDialerRTCPeerConnection } from './utils/get-rtcpeerconnection.js'
13
11
import { stunListener } from './utils/stun-listener.js'
14
12
import type { DataChannelOptions , TransportCertificate } from '../index.js'
13
+ import type { WebRTCDirectTransportCertificateEvents } from './transport.js'
15
14
import type { DirectRTCPeerConnection } from './utils/get-rtcpeerconnection.js'
16
15
import type { StunServer } from './utils/stun-listener.js'
17
- import type { PeerId , ListenerEvents , Listener , Upgrader , ComponentLogger , Logger , CounterGroup , Metrics , PrivateKey } from '@libp2p/interface'
16
+ import type { PeerId , ListenerEvents , Listener , Upgrader , ComponentLogger , Logger , CounterGroup , Metrics , PrivateKey , TypedEventTarget } from '@libp2p/interface'
17
+ import type { Keychain } from '@libp2p/keychain'
18
18
import type { Multiaddr } from '@multiformats/multiaddr'
19
-
20
- const crypto = new Crypto ( )
19
+ import type { Datastore } from 'interface-datastore'
21
20
22
21
export interface WebRTCDirectListenerComponents {
23
22
peerId : PeerId
24
23
privateKey : PrivateKey
25
24
logger : ComponentLogger
26
25
upgrader : Upgrader
26
+ keychain ?: Keychain
27
+ datastore : Datastore
27
28
metrics ?: Metrics
28
29
}
29
30
30
31
export interface WebRTCDirectListenerInit {
31
32
upgrader : Upgrader
32
- certificates ? : TransportCertificate [ ]
33
+ certificate : TransportCertificate
33
34
maxInboundStreams ?: number
34
35
dataChannel ?: DataChannelOptions
35
36
rtcConfiguration ?: RTCConfiguration | ( ( ) => RTCConfiguration | Promise < RTCConfiguration > )
36
- useLibjuice ?: boolean
37
+ emitter : TypedEventTarget < WebRTCDirectTransportCertificateEvents >
37
38
}
38
39
39
40
export interface WebRTCListenerMetrics {
@@ -53,7 +54,7 @@ let UDP_MUX_LISTENERS: UDPMuxServer[] = []
53
54
54
55
export class WebRTCDirectListener extends TypedEventEmitter < ListenerEvents > implements Listener {
55
56
private listeningMultiaddr ?: Multiaddr
56
- private certificate ? : TransportCertificate
57
+ private certificate : TransportCertificate
57
58
private stunServer ?: StunServer
58
59
private readonly connections : Map < string , DirectRTCPeerConnection >
59
60
private readonly log : Logger
@@ -69,8 +70,8 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
69
70
this . components = components
70
71
this . connections = new Map ( )
71
72
this . log = components . logger . forComponent ( 'libp2p:webrtc-direct:listener' )
72
- this . certificate = init . certificates ?. [ 0 ]
73
73
this . shutdownController = new AbortController ( )
74
+ this . certificate = init . certificate
74
75
75
76
if ( components . metrics != null ) {
76
77
this . metrics = {
@@ -80,6 +81,12 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
80
81
} )
81
82
}
82
83
}
84
+
85
+ // inform the transport manager our addresses have changed
86
+ init . emitter . addEventListener ( 'certificate:renew' , evt => {
87
+ this . certificate = evt . detail
88
+ this . safeDispatchEvent ( 'listening' )
89
+ } )
83
90
}
84
91
85
92
async listen ( ma : Multiaddr ) : Promise < void > {
@@ -132,23 +139,6 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
132
139
isIPv6 : family === 6 ,
133
140
server : Promise . resolve ( )
134
141
. then ( async ( ) : Promise < StunServer > => {
135
- // ensure we have a certificate
136
- if ( this . certificate == null ) {
137
- this . log . trace ( 'creating TLS certificate' )
138
- const keyPair = await crypto . subtle . generateKey ( {
139
- name : 'ECDSA' ,
140
- namedCurve : 'P-256'
141
- } , true , [ 'sign' , 'verify' ] )
142
-
143
- const certificate = await generateTransportCertificate ( keyPair , {
144
- days : 365 * 10
145
- } )
146
-
147
- if ( this . certificate == null ) {
148
- this . certificate = certificate
149
- }
150
- }
151
-
152
142
if ( port === 0 ) {
153
143
// libjuice doesn't map 0 to a random free port so we have to do it
154
144
// ourselves
0 commit comments