Skip to content

Commit c90984e

Browse files
authored
fix: access components via property (#2912)
To allow declaring services in any order, access components via a components property after all services are constructed.
1 parent fe8af37 commit c90984e

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

packages/auto-tls/src/auto-tls.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ import { DEFAULT_ACCOUNT_PRIVATE_KEY_BITS, DEFAULT_ACCOUNT_PRIVATE_KEY_NAME, DEF
1414
import { DomainMapper } from './domain-mapper.js'
1515
import { createCsr, importFromPem, loadOrCreateKey, supportedAddressesFilter } from './utils.js'
1616
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'
2018
import type { DebouncedFunction } from '@libp2p/utils/debounce'
2119
import type { Multiaddr } from '@multiformats/multiaddr'
22-
import type { Datastore } from 'interface-datastore'
2320

2421
const RETRY_DELAY = 5_000
2522

@@ -33,12 +30,7 @@ interface Certificate {
3330

3431
export class AutoTLS implements AutoTLSInterface {
3532
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
4234
private readonly forgeEndpoint: URL
4335
private readonly forgeDomain: string
4436
private readonly acmeDirectory: URL
@@ -64,12 +56,7 @@ export class AutoTLS implements AutoTLSInterface {
6456

6557
constructor (components: AutoTLSComponents, init: AutoTLSInit = {}) {
6658
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
7360
this.forgeEndpoint = new URL(init.forgeEndpoint ?? DEFAULT_FORGE_ENDPOINT)
7461
this.forgeDomain = init.forgeDomain ?? DEFAULT_FORGE_DOMAIN
7562
this.acmeDirectory = new URL(init.acmeDirectory ?? DEFAULT_ACME_DIRECTORY)
@@ -82,12 +69,12 @@ export class AutoTLS implements AutoTLSInterface {
8269
this.certificatePrivateKeyBits = init.certificatePrivateKeyBits ?? DEFAULT_CERTIFICATE_PRIVATE_KEY_BITS
8370
this.certificateDatastoreKey = init.certificateDatastoreKey ?? DEFAULT_CERTIFICATE_DATASTORE_KEY
8471
this.autoConfirmAddress = init.autoConfirmAddress ?? DEFAULT_AUTO_CONFIRM_ADDRESS
85-
this.clientAuth = new ClientAuth(this.privateKey)
72+
this.clientAuth = new ClientAuth(this.components.privateKey)
8673
this.started = false
8774
this.fetching = false
8875
this.onSelfPeerUpdate = debounce(this._onSelfPeerUpdate.bind(this), init.provisionDelay ?? DEFAULT_PROVISION_DELAY)
8976

90-
const base36EncodedPeer = base36.encode(this.peerId.toCID().bytes)
77+
const base36EncodedPeer = base36.encode(this.components.peerId.toCID().bytes)
9178
this.domain = `${base36EncodedPeer}.${this.forgeDomain}`
9279
this.email = `${base36EncodedPeer}@${this.forgeDomain}`
9380

@@ -120,22 +107,22 @@ export class AutoTLS implements AutoTLSInterface {
120107
}
121108

122109
await start(this.domainMapper)
123-
this.events.addEventListener('self:peer:update', this.onSelfPeerUpdate)
110+
this.components.events.addEventListener('self:peer:update', this.onSelfPeerUpdate)
124111
this.shutdownController = new AbortController()
125112
setMaxListeners(Infinity, this.shutdownController.signal)
126113
this.started = true
127114
}
128115

129116
async stop (): Promise<void> {
130-
this.events.removeEventListener('self:peer:update', this.onSelfPeerUpdate)
117+
this.components.events.removeEventListener('self:peer:update', this.onSelfPeerUpdate)
131118
this.shutdownController?.abort()
132119
clearTimeout(this.renewTimeout)
133120
await stop(this.onSelfPeerUpdate, this.domainMapper)
134121
this.started = false
135122
}
136123

137124
private _onSelfPeerUpdate (): void {
138-
const addresses = this.addressManager.getAddresses()
125+
const addresses = this.components.addressManager.getAddresses()
139126
.filter(supportedAddressesFilter)
140127

141128
if (addresses.length === 0) {
@@ -187,7 +174,7 @@ export class AutoTLS implements AutoTLSInterface {
187174
private async fetchCertificate (multiaddrs: Multiaddr[], options?: AbortOptions): Promise<void> {
188175
this.log('fetching certificate')
189176

190-
const certificatePrivateKey = await loadOrCreateKey(this.keychain, this.certificatePrivateKeyName, this.certificatePrivateKeyBits)
177+
const certificatePrivateKey = await loadOrCreateKey(this.components.keychain, this.certificatePrivateKeyName, this.certificatePrivateKeyBits)
191178
const { pem, cert } = await this.loadOrCreateCertificate(certificatePrivateKey, multiaddrs, options)
192179

193180
let event: CertificateEvent = 'certificate:provision'
@@ -221,7 +208,7 @@ export class AutoTLS implements AutoTLSInterface {
221208

222209
// emit a certificate event
223210
this.log('dispatching %s', event)
224-
this.events.safeDispatchEvent(event, {
211+
this.components.events.safeDispatchEvent(event, {
225212
detail: {
226213
...this.certificate
227214
}
@@ -247,7 +234,7 @@ export class AutoTLS implements AutoTLSInterface {
247234
const cert = new X509Certificate(pem)
248235

249236
// 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))
251238

252239
return {
253240
pem,
@@ -260,7 +247,7 @@ export class AutoTLS implements AutoTLSInterface {
260247

261248
try {
262249
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)
264251
const pem = uint8ArrayToString(buf)
265252
const cert = new X509Certificate(pem)
266253

@@ -297,7 +284,7 @@ export class AutoTLS implements AutoTLSInterface {
297284
async fetchAcmeCertificate (csr: string, multiaddrs: Multiaddr[], options?: AbortOptions): Promise<string> {
298285
const client = new acme.Client({
299286
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)
301288
})
302289

303290
return client.auto({

packages/auto-tls/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @packageDocumentation
33
*
44
* When a publicly dialable address is detected, use the p2p-forge service at
5-
* https://registration.libp2p.direct to acquire a valid Let's Encrypted-backed
5+
* https://registration.libp2p.direct to acquire a valid Let's Encrypt-backed
66
* TLS certificate, which the node can then use with the relevant transports.
77
*
88
* The node must be configured with a listener for at least one of the following

0 commit comments

Comments
 (0)