@helia/ipns 9.0.0 → 9.1.0-3eb6f3a7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ipns.ts CHANGED
@@ -1,49 +1,26 @@
1
- import { generateKeyPair } from '@libp2p/crypto/keys'
2
- import { NotFoundError, NotStartedError, isPeerId, isPublicKey } from '@libp2p/interface'
3
- import { Queue, repeatingTask } from '@libp2p/utils'
4
- import { createIPNSRecord, marshalIPNSRecord, multihashToIPNSRoutingKey, unmarshalIPNSRecord } from 'ipns'
5
- import { ipnsSelector } from 'ipns/selector'
6
- import { ipnsValidator } from 'ipns/validator'
7
- import { base36 } from 'multiformats/bases/base36'
8
- import { base58btc } from 'multiformats/bases/base58'
9
1
  import { CID } from 'multiformats/cid'
10
- import * as Digest from 'multiformats/hashes/digest'
11
- import { CustomProgressEvent } from 'progress-events'
12
- import { DEFAULT_LIFETIME_MS, DEFAULT_REPUBLISH_CONCURRENCY, DEFAULT_REPUBLISH_INTERVAL_MS, DEFAULT_TTL_NS } from './constants.ts'
13
- import { InvalidValueError, RecordsFailedValidationError, UnsupportedMultibasePrefixError, UnsupportedMultihashCodecError } from './errors.js'
2
+ import { IPNSPublisher } from './ipns/publisher.ts'
3
+ import { IPNSRepublisher } from './ipns/republisher.ts'
4
+ import { IPNSResolver } from './ipns/resolver.ts'
14
5
  import { localStore } from './local-store.js'
15
6
  import { helia } from './routing/helia.js'
16
7
  import { localStoreRouting } from './routing/local-store.ts'
17
- import { isCodec, IDENTITY_CODEC, SHA2_256_CODEC, shouldRepublish, isLibp2pCID } from './utils.js'
18
8
  import type { IPNSComponents, IPNS as IPNSInterface, IPNSOptions, IPNSPublishResult, IPNSResolveResult, PublishOptions, ResolveOptions } from './index.js'
19
9
  import type { LocalStore } from './local-store.js'
20
10
  import type { IPNSRouting } from './routing/index.js'
21
- import type { AbortOptions, Logger, PeerId, PrivateKey, PublicKey, Startable } from '@libp2p/interface'
22
- import type { Keychain } from '@libp2p/keychain'
23
- import type { RepeatingTask } from '@libp2p/utils'
24
- import type { IPNSRecord } from 'ipns'
25
- import type { MultibaseDecoder } from 'multiformats/bases/interface'
11
+ import type { AbortOptions, PeerId, PublicKey, Startable } from '@libp2p/interface'
26
12
  import type { MultihashDigest } from 'multiformats/hashes/interface'
27
13
 
28
- const bases: Record<string, MultibaseDecoder<string>> = {
29
- [base36.prefix]: base36,
30
- [base58btc.prefix]: base58btc
31
- }
32
-
33
14
  export class IPNS implements IPNSInterface, Startable {
34
15
  public readonly routers: IPNSRouting[]
16
+ private readonly publisher: IPNSPublisher
17
+ private readonly republisher: IPNSRepublisher
18
+ private readonly resolver: IPNSResolver
35
19
  private readonly localStore: LocalStore
36
- private readonly republishTask: RepeatingTask
37
- private readonly log: Logger
38
- private readonly keychain: Keychain
39
- private started: boolean = false
40
- private readonly republishConcurrency: number
20
+ private started: boolean
41
21
 
42
22
  constructor (components: IPNSComponents, init: IPNSOptions = {}) {
43
- this.log = components.logger.forComponent('helia:ipns')
44
23
  this.localStore = localStore(components.datastore, components.logger.forComponent('helia:ipns:local-store'))
45
- this.keychain = components.libp2p.services.keychain
46
- this.republishConcurrency = init.republishConcurrency || DEFAULT_REPUBLISH_CONCURRENCY
47
24
  this.started = components.libp2p.status === 'started'
48
25
 
49
26
  this.routers = [
@@ -52,17 +29,29 @@ export class IPNS implements IPNSInterface, Startable {
52
29
  ...(init.routers ?? [])
53
30
  ]
54
31
 
32
+ this.publisher = new IPNSPublisher(components, {
33
+ ...init,
34
+ routers: this.routers,
35
+ localStore: this.localStore
36
+ })
37
+ this.republisher = new IPNSRepublisher(components, {
38
+ ...init,
39
+ routers: this.routers,
40
+ localStore: this.localStore
41
+ })
42
+ this.resolver = new IPNSResolver(components, {
43
+ ...init,
44
+ routers: this.routers,
45
+ localStore: this.localStore
46
+ })
47
+
55
48
  // start republishing on Helia start
56
49
  components.events.addEventListener('start', this.start.bind(this))
57
50
  // stop republishing on Helia stop
58
51
  components.events.addEventListener('stop', this.stop.bind(this))
59
52
 
60
- this.republishTask = repeatingTask(this.#republish.bind(this), init.republishInterval ?? DEFAULT_REPUBLISH_INTERVAL_MS, {
61
- runImmediately: true
62
- })
63
-
64
53
  if (this.started) {
65
- this.republishTask.start()
54
+ this.republisher.start()
66
55
  }
67
56
  }
68
57
 
@@ -72,7 +61,7 @@ export class IPNS implements IPNSInterface, Startable {
72
61
  }
73
62
 
74
63
  this.started = true
75
- this.republishTask.start()
64
+ this.republisher.start()
76
65
  }
77
66
 
78
67
  stop (): void {
@@ -81,320 +70,18 @@ export class IPNS implements IPNSInterface, Startable {
81
70
  }
82
71
 
83
72
  this.started = false
84
- this.republishTask.stop()
85
- }
86
-
87
- #throwIfStopped (): void {
88
- if (!this.started) {
89
- throw new NotStartedError('Helia is stopped, cannot perform IPNS operations')
90
- }
73
+ this.republisher.stop()
91
74
  }
92
75
 
93
76
  async publish (keyName: string, value: CID | PublicKey | MultihashDigest<0x00 | 0x12> | PeerId | string, options: PublishOptions = {}): Promise<IPNSPublishResult> {
94
- this.#throwIfStopped()
95
-
96
- try {
97
- const privKey = await this.#loadOrCreateKey(keyName)
98
- let sequenceNumber = 1n
99
- const routingKey = multihashToIPNSRoutingKey(privKey.publicKey.toMultihash())
100
-
101
- if (await this.localStore.has(routingKey, options)) {
102
- // if we have published under this key before, increment the sequence number
103
- const { record } = await this.localStore.get(routingKey, options)
104
- const existingRecord = unmarshalIPNSRecord(record)
105
- sequenceNumber = existingRecord.sequence + 1n
106
- }
107
-
108
- if (isPeerId(value)) {
109
- value = value.toCID()
110
- }
111
-
112
- // convert ttl from milliseconds to nanoseconds as createIPNSRecord expects
113
- const ttlNs = options.ttl != null ? BigInt(options.ttl) * 1_000_000n : DEFAULT_TTL_NS
114
- const lifetime = options.lifetime ?? DEFAULT_LIFETIME_MS
115
- const record = await createIPNSRecord(privKey, value, sequenceNumber, lifetime, { ...options, ttlNs })
116
- const marshaledRecord = marshalIPNSRecord(record)
117
-
118
- if (options.offline === true) {
119
- // only store record locally
120
- await this.localStore.put(routingKey, marshaledRecord, { ...options, metadata: { keyName, lifetime } })
121
- } else {
122
- // publish record to routing (including the local store)
123
- await Promise.all(this.routers.map(async r => {
124
- await r.put(routingKey, marshaledRecord, { ...options, metadata: { keyName, lifetime } })
125
- }))
126
- }
127
-
128
- return {
129
- record,
130
- publicKey: privKey.publicKey
131
- }
132
- } catch (err: any) {
133
- options.onProgress?.(new CustomProgressEvent<Error>('ipns:publish:error', err))
134
- throw err
135
- }
77
+ return this.publisher.publish(keyName, value, options)
136
78
  }
137
79
 
138
80
  async resolve (key: CID<unknown, 0x72, 0x00 | 0x12, 1> | PublicKey | MultihashDigest<0x00 | 0x12> | PeerId, options: ResolveOptions = {}): Promise<IPNSResolveResult> {
139
- this.#throwIfStopped()
140
- const digest = isPublicKey(key) || isPeerId(key) ? key.toMultihash() : isLibp2pCID(key) ? key.multihash : key
141
- const routingKey = multihashToIPNSRoutingKey(digest)
142
- const record = await this.#findIpnsRecord(routingKey, options)
143
-
144
- return {
145
- ...(await this.#resolve(record.value, options)),
146
- record
147
- }
148
- }
149
-
150
- async #loadOrCreateKey (keyName: string): Promise<PrivateKey> {
151
- try {
152
- return await this.keychain.exportKey(keyName)
153
- } catch (err: any) {
154
- // If no named key found in keychain, generate and import
155
- const privKey = await generateKeyPair('Ed25519')
156
- await this.keychain.importKey(keyName, privKey)
157
- return privKey
158
- }
159
- }
160
-
161
- async #republish (options: AbortOptions = {}): Promise<void> {
162
- if (!this.started) {
163
- return
164
- }
165
-
166
- this.log('starting ipns republish records loop')
167
-
168
- const queue = new Queue({
169
- concurrency: this.republishConcurrency
170
- })
171
-
172
- try {
173
- const recordsToRepublish: Array<{ routingKey: Uint8Array, record: IPNSRecord }> = []
174
-
175
- // Find all records using the localStore.list method
176
- for await (const { routingKey, record, metadata, created } of this.localStore.list(options)) {
177
- if (metadata == null) {
178
- // Skip if no metadata is found from before we started
179
- // storing metadata or for records republished without a key
180
- this.log(`no metadata found for record ${routingKey.toString()}, skipping`)
181
- continue
182
- }
183
- let ipnsRecord: IPNSRecord
184
- try {
185
- ipnsRecord = unmarshalIPNSRecord(record)
186
- } catch (err: any) {
187
- this.log.error('error unmarshaling record - %e', err)
188
- continue
189
- }
190
-
191
- // Only republish records that are within the DHT or record expiry threshold
192
- if (!shouldRepublish(ipnsRecord, created)) {
193
- this.log.trace(`skipping record ${routingKey.toString()}within republish threshold`)
194
- continue
195
- }
196
- const sequenceNumber = ipnsRecord.sequence + 1n
197
- const ttlNs = ipnsRecord.ttl ?? DEFAULT_TTL_NS
198
- let privKey: PrivateKey
199
-
200
- try {
201
- privKey = await this.keychain.exportKey(metadata.keyName)
202
- } catch (err: any) {
203
- this.log.error(`missing key ${metadata.keyName}, skipping republishing record`, err)
204
- continue
205
- }
206
- try {
207
- const updatedRecord = await createIPNSRecord(privKey, ipnsRecord.value, sequenceNumber, metadata.lifetime, { ...options, ttlNs })
208
- recordsToRepublish.push({ routingKey, record: updatedRecord })
209
- } catch (err: any) {
210
- this.log.error(`error creating updated IPNS record for ${routingKey.toString()}`, err)
211
- continue
212
- }
213
- }
214
-
215
- this.log(`found ${recordsToRepublish.length} records to republish`)
216
-
217
- // Republish each record
218
- for (const { routingKey, record } of recordsToRepublish) {
219
- // Add job to queue to republish the record to all routers
220
- queue.add(async () => {
221
- try {
222
- const marshaledRecord = marshalIPNSRecord(record)
223
- await Promise.all(
224
- this.routers.map(r => r.put(routingKey, marshaledRecord, options))
225
- )
226
- } catch (err: any) {
227
- this.log.error('error republishing record - %e', err)
228
- }
229
- }, options)
230
- }
231
- } catch (err: any) {
232
- this.log.error('error during republish - %e', err)
233
- }
234
-
235
- await queue.onIdle(options) // Wait for all jobs to complete
81
+ return this.resolver.resolve(key, options)
236
82
  }
237
83
 
238
84
  async unpublish (keyName: string, options?: AbortOptions): Promise<void> {
239
- const { publicKey } = await this.keychain.exportKey(keyName)
240
- const digest = publicKey.toMultihash()
241
- const routingKey = multihashToIPNSRoutingKey(digest)
242
- await this.localStore.delete(routingKey, options)
243
- }
244
-
245
- async #resolve (ipfsPath: string, options: ResolveOptions = {}): Promise<{ cid: CID, path: string }> {
246
- const parts = ipfsPath.split('/')
247
- try {
248
- const scheme = parts[1]
249
-
250
- if (scheme === 'ipns') {
251
- const str = parts[2]
252
- const prefix = str.substring(0, 1)
253
- let buf: Uint8Array | undefined
254
-
255
- if (prefix === '1' || prefix === 'Q') {
256
- buf = base58btc.decode(`z${str}`)
257
- } else if (bases[prefix] != null) {
258
- buf = bases[prefix].decode(str)
259
- } else {
260
- throw new UnsupportedMultibasePrefixError(`Unsupported multibase prefix "${prefix}"`)
261
- }
262
-
263
- let digest: MultihashDigest<number>
264
-
265
- try {
266
- digest = Digest.decode(buf)
267
- } catch {
268
- digest = CID.decode(buf).multihash
269
- }
270
-
271
- if (!isCodec(digest, IDENTITY_CODEC) && !isCodec(digest, SHA2_256_CODEC)) {
272
- throw new UnsupportedMultihashCodecError(`Unsupported multihash codec "${digest.code}"`)
273
- }
274
-
275
- const { cid } = await this.resolve(digest, options)
276
- const path = parts.slice(3).join('/')
277
- return {
278
- cid,
279
- path
280
- }
281
- } else if (scheme === 'ipfs') {
282
- const cid = CID.parse(parts[2])
283
- const path = parts.slice(3).join('/')
284
- return {
285
- cid,
286
- path
287
- }
288
- }
289
- } catch (err) {
290
- this.log.error('error parsing ipfs path - %e', err)
291
- }
292
-
293
- this.log.error('invalid ipfs path %s', ipfsPath)
294
- throw new InvalidValueError('Invalid value')
295
- }
296
-
297
- async #findIpnsRecord (routingKey: Uint8Array, options: ResolveOptions = {}): Promise<IPNSRecord> {
298
- const records: Uint8Array[] = []
299
- const cached = await this.localStore.has(routingKey, options)
300
-
301
- if (cached) {
302
- this.log('record is present in the cache')
303
-
304
- if (options.nocache !== true) {
305
- try {
306
- // check the local cache first
307
- const { record, created } = await this.localStore.get(routingKey, options)
308
-
309
- this.log('record retrieved from cache')
310
-
311
- // validate the record
312
- await ipnsValidator(routingKey, record)
313
-
314
- this.log('record was valid')
315
-
316
- // check the TTL
317
- const ipnsRecord = unmarshalIPNSRecord(record)
318
-
319
- // IPNS TTL is in nanoseconds, convert to milliseconds, default to one
320
- // hour
321
- const ttlMs = Number((ipnsRecord.ttl ?? DEFAULT_TTL_NS) / 1_000_000n)
322
- const ttlExpires = created.getTime() + ttlMs
323
-
324
- if (ttlExpires > Date.now()) {
325
- // the TTL has not yet expired, return the cached record
326
- this.log('record TTL was valid')
327
- return ipnsRecord
328
- }
329
-
330
- if (options.offline === true) {
331
- // the TTL has expired but we are skipping the routing search
332
- this.log('record TTL has been reached but we are resolving offline-only, returning record')
333
- return ipnsRecord
334
- }
335
-
336
- this.log('record TTL has been reached, searching routing for updates')
337
-
338
- // add the local record to our list of resolved record, and also
339
- // search the routing for updates - the most up to date record will be
340
- // returned
341
- records.push(record)
342
- } catch (err) {
343
- this.log('cached record was invalid - %e', err)
344
- await this.localStore.delete(routingKey, options)
345
- }
346
- } else {
347
- this.log('ignoring local cache due to nocache=true option')
348
- }
349
- }
350
-
351
- if (options.offline === true) {
352
- throw new NotFoundError('Record was not present in the cache or has expired')
353
- }
354
-
355
- this.log('did not have record locally')
356
-
357
- let foundInvalid = 0
358
-
359
- await Promise.all(
360
- this.routers.map(async (router) => {
361
- let record: Uint8Array
362
-
363
- try {
364
- record = await router.get(routingKey, {
365
- ...options,
366
- validate: false
367
- })
368
- } catch (err: any) {
369
- this.log.error('error finding IPNS record - %e', err)
370
-
371
- return
372
- }
373
-
374
- try {
375
- await ipnsValidator(routingKey, record)
376
-
377
- records.push(record)
378
- } catch (err) {
379
- // we found a record, but the validator rejected it
380
- foundInvalid++
381
- this.log.error('error finding IPNS record - %e', err)
382
- }
383
- })
384
- )
385
-
386
- if (records.length === 0) {
387
- if (foundInvalid > 0) {
388
- throw new RecordsFailedValidationError(`${foundInvalid > 1 ? `${foundInvalid} records` : 'Record'} found for routing key ${foundInvalid > 1 ? 'were' : 'was'} invalid`)
389
- }
390
-
391
- throw new NotFoundError('Could not find record for routing key')
392
- }
393
-
394
- const record = records[ipnsSelector(routingKey, records)]
395
-
396
- await this.localStore.put(routingKey, record, options)
397
-
398
- return unmarshalIPNSRecord(record)
85
+ return this.publisher.unpublish(keyName, options)
399
86
  }
400
87
  }
@@ -27,7 +27,7 @@ export type { DatastoreProgressEvents }
27
27
  export type { HeliaRoutingProgressEvents }
28
28
  export type { PubSubProgressEvents }
29
29
 
30
- export type IPNSRoutingEvents =
30
+ export type IPNSRoutingProgressEvents =
31
31
  DatastoreProgressEvents |
32
32
  HeliaRoutingProgressEvents |
33
33
  PubSubProgressEvents
@@ -1,45 +0,0 @@
1
- {
2
- "IPNS": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNS.html",
3
- ".:IPNS": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNS.html",
4
- "IPNSComponents": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSComponents.html",
5
- ".:IPNSComponents": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSComponents.html",
6
- "IPNSOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSOptions.html",
7
- ".:IPNSOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSOptions.html",
8
- "IPNSPublishResult": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSPublishResult.html",
9
- ".:IPNSPublishResult": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSPublishResult.html",
10
- "IPNSRecordMetadata": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSRecordMetadata.html",
11
- ".:IPNSRecordMetadata": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSRecordMetadata.html",
12
- "IPNSResolveResult": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSResolveResult.html",
13
- ".:IPNSResolveResult": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.IPNSResolveResult.html",
14
- "PublishOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.PublishOptions.html",
15
- ".:PublishOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.PublishOptions.html",
16
- "ResolveOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.ResolveOptions.html",
17
- ".:ResolveOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.ResolveOptions.html",
18
- "ResolveResult": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.ResolveResult.html",
19
- ".:ResolveResult": "https://ipfs.github.io/helia/interfaces/_helia_ipns.index.ResolveResult.html",
20
- "DatastoreProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.index.DatastoreProgressEvents.html",
21
- ".:DatastoreProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.index.DatastoreProgressEvents.html",
22
- "PublishProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.index.PublishProgressEvents.html",
23
- ".:PublishProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.index.PublishProgressEvents.html",
24
- "ResolveProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.index.ResolveProgressEvents.html",
25
- ".:ResolveProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.index.ResolveProgressEvents.html",
26
- "ipns": "https://ipfs.github.io/helia/functions/_helia_ipns.index.ipns.html",
27
- ".:ipns": "https://ipfs.github.io/helia/functions/_helia_ipns.index.ipns.html",
28
- "GetOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.GetOptions.html",
29
- "./routing:GetOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.GetOptions.html",
30
- "IPNSRouting": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.IPNSRouting.html",
31
- "./routing:IPNSRouting": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.IPNSRouting.html",
32
- "Message": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.Message.html",
33
- "PublishResult": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.PublishResult.html",
34
- "PubSub": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.PubSub.html",
35
- "PubSubEvents": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.PubSubEvents.html",
36
- "PubsubRoutingComponents": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.PubsubRoutingComponents.html",
37
- "PutOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.PutOptions.html",
38
- "./routing:PutOptions": "https://ipfs.github.io/helia/interfaces/_helia_ipns.routing.PutOptions.html",
39
- "HeliaRoutingProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.routing.HeliaRoutingProgressEvents.html",
40
- "IPNSRoutingEvents": "https://ipfs.github.io/helia/types/_helia_ipns.routing.IPNSRoutingEvents.html",
41
- "./routing:IPNSRoutingEvents": "https://ipfs.github.io/helia/types/_helia_ipns.routing.IPNSRoutingEvents.html",
42
- "PubSubProgressEvents": "https://ipfs.github.io/helia/types/_helia_ipns.routing.PubSubProgressEvents.html",
43
- "helia": "https://ipfs.github.io/helia/functions/_helia_ipns.routing.helia.html",
44
- "pubsub": "https://ipfs.github.io/helia/functions/_helia_ipns.routing.pubsub.html"
45
- }