@@ -14,12 +14,9 @@ import { DEFAULT_ACCOUNT_PRIVATE_KEY_BITS, DEFAULT_ACCOUNT_PRIVATE_KEY_NAME, DEF
14
14
import { DomainMapper } from './domain-mapper.js'
15
15
import { createCsr , importFromPem , loadOrCreateKey , supportedAddressesFilter } from './utils.js'
16
16
import type { AutoTLSComponents , AutoTLSInit , AutoTLS as AutoTLSInterface } from './index.js'
17
- import type { PeerId , PrivateKey , Logger , TypedEventTarget , Libp2pEvents , AbortOptions } from '@libp2p/interface'
18
- import type { AddressManager } from '@libp2p/interface-internal'
19
- import type { Keychain } from '@libp2p/keychain'
17
+ import type { Logger , AbortOptions } from '@libp2p/interface'
20
18
import type { DebouncedFunction } from '@libp2p/utils/debounce'
21
19
import type { Multiaddr } from '@multiformats/multiaddr'
22
- import type { Datastore } from 'interface-datastore'
23
20
24
21
const RETRY_DELAY = 5_000
25
22
@@ -33,12 +30,7 @@ interface Certificate {
33
30
34
31
export class AutoTLS implements AutoTLSInterface {
35
32
private readonly log : Logger
36
- private readonly addressManager : AddressManager
37
- private readonly keychain : Keychain
38
- private readonly datastore : Datastore
39
- private readonly privateKey : PrivateKey
40
- private readonly peerId : PeerId
41
- private readonly events : TypedEventTarget < Libp2pEvents >
33
+ private readonly components : AutoTLSComponents
42
34
private readonly forgeEndpoint : URL
43
35
private readonly forgeDomain : string
44
36
private readonly acmeDirectory : URL
@@ -64,12 +56,7 @@ export class AutoTLS implements AutoTLSInterface {
64
56
65
57
constructor ( components : AutoTLSComponents , init : AutoTLSInit = { } ) {
66
58
this . log = components . logger . forComponent ( 'libp2p:auto-tls' )
67
- this . addressManager = components . addressManager
68
- this . privateKey = components . privateKey
69
- this . peerId = components . peerId
70
- this . events = components . events
71
- this . keychain = components . keychain
72
- this . datastore = components . datastore
59
+ this . components = components
73
60
this . forgeEndpoint = new URL ( init . forgeEndpoint ?? DEFAULT_FORGE_ENDPOINT )
74
61
this . forgeDomain = init . forgeDomain ?? DEFAULT_FORGE_DOMAIN
75
62
this . acmeDirectory = new URL ( init . acmeDirectory ?? DEFAULT_ACME_DIRECTORY )
@@ -82,12 +69,12 @@ export class AutoTLS implements AutoTLSInterface {
82
69
this . certificatePrivateKeyBits = init . certificatePrivateKeyBits ?? DEFAULT_CERTIFICATE_PRIVATE_KEY_BITS
83
70
this . certificateDatastoreKey = init . certificateDatastoreKey ?? DEFAULT_CERTIFICATE_DATASTORE_KEY
84
71
this . autoConfirmAddress = init . autoConfirmAddress ?? DEFAULT_AUTO_CONFIRM_ADDRESS
85
- this . clientAuth = new ClientAuth ( this . privateKey )
72
+ this . clientAuth = new ClientAuth ( this . components . privateKey )
86
73
this . started = false
87
74
this . fetching = false
88
75
this . onSelfPeerUpdate = debounce ( this . _onSelfPeerUpdate . bind ( this ) , init . provisionDelay ?? DEFAULT_PROVISION_DELAY )
89
76
90
- const base36EncodedPeer = base36 . encode ( this . peerId . toCID ( ) . bytes )
77
+ const base36EncodedPeer = base36 . encode ( this . components . peerId . toCID ( ) . bytes )
91
78
this . domain = `${ base36EncodedPeer } .${ this . forgeDomain } `
92
79
this . email = `${ base36EncodedPeer } @${ this . forgeDomain } `
93
80
@@ -120,22 +107,22 @@ export class AutoTLS implements AutoTLSInterface {
120
107
}
121
108
122
109
await start ( this . domainMapper )
123
- this . events . addEventListener ( 'self:peer:update' , this . onSelfPeerUpdate )
110
+ this . components . events . addEventListener ( 'self:peer:update' , this . onSelfPeerUpdate )
124
111
this . shutdownController = new AbortController ( )
125
112
setMaxListeners ( Infinity , this . shutdownController . signal )
126
113
this . started = true
127
114
}
128
115
129
116
async stop ( ) : Promise < void > {
130
- this . events . removeEventListener ( 'self:peer:update' , this . onSelfPeerUpdate )
117
+ this . components . events . removeEventListener ( 'self:peer:update' , this . onSelfPeerUpdate )
131
118
this . shutdownController ?. abort ( )
132
119
clearTimeout ( this . renewTimeout )
133
120
await stop ( this . onSelfPeerUpdate , this . domainMapper )
134
121
this . started = false
135
122
}
136
123
137
124
private _onSelfPeerUpdate ( ) : void {
138
- const addresses = this . addressManager . getAddresses ( )
125
+ const addresses = this . components . addressManager . getAddresses ( )
139
126
. filter ( supportedAddressesFilter )
140
127
141
128
if ( addresses . length === 0 ) {
@@ -187,7 +174,7 @@ export class AutoTLS implements AutoTLSInterface {
187
174
private async fetchCertificate ( multiaddrs : Multiaddr [ ] , options ?: AbortOptions ) : Promise < void > {
188
175
this . log ( 'fetching certificate' )
189
176
190
- const certificatePrivateKey = await loadOrCreateKey ( this . keychain , this . certificatePrivateKeyName , this . certificatePrivateKeyBits )
177
+ const certificatePrivateKey = await loadOrCreateKey ( this . components . keychain , this . certificatePrivateKeyName , this . certificatePrivateKeyBits )
191
178
const { pem, cert } = await this . loadOrCreateCertificate ( certificatePrivateKey , multiaddrs , options )
192
179
193
180
let event : CertificateEvent = 'certificate:provision'
@@ -221,7 +208,7 @@ export class AutoTLS implements AutoTLSInterface {
221
208
222
209
// emit a certificate event
223
210
this . log ( 'dispatching %s' , event )
224
- this . events . safeDispatchEvent ( event , {
211
+ this . components . events . safeDispatchEvent ( event , {
225
212
detail : {
226
213
...this . certificate
227
214
}
@@ -247,7 +234,7 @@ export class AutoTLS implements AutoTLSInterface {
247
234
const cert = new X509Certificate ( pem )
248
235
249
236
// cache cert
250
- await this . datastore . put ( new Key ( this . certificateDatastoreKey ) , uint8ArrayFromString ( pem ) )
237
+ await this . components . datastore . put ( new Key ( this . certificateDatastoreKey ) , uint8ArrayFromString ( pem ) )
251
238
252
239
return {
253
240
pem,
@@ -260,7 +247,7 @@ export class AutoTLS implements AutoTLSInterface {
260
247
261
248
try {
262
249
this . log . trace ( 'try to load existing certificate' )
263
- const buf = await this . datastore . get ( key )
250
+ const buf = await this . components . datastore . get ( key )
264
251
const pem = uint8ArrayToString ( buf )
265
252
const cert = new X509Certificate ( pem )
266
253
@@ -297,7 +284,7 @@ export class AutoTLS implements AutoTLSInterface {
297
284
async fetchAcmeCertificate ( csr : string , multiaddrs : Multiaddr [ ] , options ?: AbortOptions ) : Promise < string > {
298
285
const client = new acme . Client ( {
299
286
directoryUrl : this . acmeDirectory . toString ( ) ,
300
- accountKey : await loadOrCreateKey ( this . keychain , this . accountPrivateKeyName , this . accountPrivateKeyBits )
287
+ accountKey : await loadOrCreateKey ( this . components . keychain , this . accountPrivateKeyName , this . accountPrivateKeyBits )
301
288
} )
302
289
303
290
return client . auto ( {
0 commit comments