@helia/ipns 8.2.4 → 9.0.0-4d51f16d
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/README.md +31 -135
- package/dist/index.min.js +11 -11
- package/dist/index.min.js.map +4 -4
- package/dist/src/constants.d.ts +17 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +19 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/errors.d.ts +0 -4
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +0 -7
- package/dist/src/errors.js.map +1 -1
- package/dist/src/index.d.ts +131 -207
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +49 -416
- package/dist/src/index.js.map +1 -1
- package/dist/src/ipns/publisher.d.ts +29 -0
- package/dist/src/ipns/publisher.d.ts.map +1 -0
- package/dist/src/ipns/publisher.js +73 -0
- package/dist/src/ipns/publisher.js.map +1 -0
- package/dist/src/ipns/republisher.d.ts +30 -0
- package/dist/src/ipns/republisher.d.ts.map +1 -0
- package/dist/src/ipns/republisher.js +112 -0
- package/dist/src/ipns/republisher.js.map +1 -0
- package/dist/src/ipns/resolver.d.ts +26 -0
- package/dist/src/ipns/resolver.d.ts.map +1 -0
- package/dist/src/ipns/resolver.js +165 -0
- package/dist/src/ipns/resolver.js.map +1 -0
- package/dist/src/ipns.d.ts +20 -0
- package/dist/src/ipns.d.ts.map +1 -0
- package/dist/src/ipns.js +70 -0
- package/dist/src/ipns.js.map +1 -0
- package/dist/src/local-store.d.ts +42 -0
- package/dist/src/local-store.d.ts.map +1 -0
- package/dist/src/local-store.js +119 -0
- package/dist/src/local-store.js.map +1 -0
- package/dist/src/pb/metadata.d.ts +12 -0
- package/dist/src/pb/metadata.d.ts.map +1 -0
- package/dist/src/pb/metadata.js +57 -0
- package/dist/src/pb/metadata.js.map +1 -0
- package/dist/src/routing/index.d.ts +5 -3
- package/dist/src/routing/index.d.ts.map +1 -1
- package/dist/src/routing/index.js.map +1 -1
- package/dist/src/routing/local-store.d.ts +4 -19
- package/dist/src/routing/local-store.d.ts.map +1 -1
- package/dist/src/routing/local-store.js +7 -62
- package/dist/src/routing/local-store.js.map +1 -1
- package/dist/src/routing/pubsub.d.ts +21 -1
- package/dist/src/routing/pubsub.d.ts.map +1 -1
- package/dist/src/routing/pubsub.js +2 -2
- package/dist/src/routing/pubsub.js.map +1 -1
- package/dist/src/utils.d.ts +24 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +56 -0
- package/dist/src/utils.js.map +1 -1
- package/package.json +21 -23
- package/src/constants.ts +24 -0
- package/src/errors.ts +0 -9
- package/src/index.ts +154 -548
- package/src/ipns/publisher.ts +97 -0
- package/src/ipns/republisher.ts +144 -0
- package/src/ipns/resolver.ts +217 -0
- package/src/ipns.ts +87 -0
- package/src/local-store.ts +162 -0
- package/src/pb/metadata.proto +9 -0
- package/src/pb/metadata.ts +74 -0
- package/src/routing/index.ts +5 -4
- package/src/routing/local-store.ts +9 -87
- package/src/routing/pubsub.ts +28 -4
- package/src/utils.ts +70 -0
- package/dist/src/dnslink.d.ts +0 -9
- package/dist/src/dnslink.d.ts.map +0 -1
- package/dist/src/dnslink.js +0 -138
- package/dist/src/dnslink.js.map +0 -1
- package/dist/typedoc-urls.json +0 -48
- package/src/dnslink.ts +0 -163
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { generateKeyPair } from '@libp2p/crypto/keys'
|
|
2
|
+
import { isPeerId } from '@libp2p/interface'
|
|
3
|
+
import { createIPNSRecord, marshalIPNSRecord, multihashToIPNSRoutingKey, unmarshalIPNSRecord } from 'ipns'
|
|
4
|
+
import { CID } from 'multiformats/cid'
|
|
5
|
+
import { CustomProgressEvent } from 'progress-events'
|
|
6
|
+
import { DEFAULT_LIFETIME_MS, DEFAULT_TTL_NS } from '../constants.ts'
|
|
7
|
+
import type { IPNSPublishResult, PublishOptions } from '../index.js'
|
|
8
|
+
import type { LocalStore } from '../local-store.js'
|
|
9
|
+
import type { IPNSRouting } from '../routing/index.js'
|
|
10
|
+
import type { AbortOptions, ComponentLogger, Libp2p, PeerId, PrivateKey, PublicKey } from '@libp2p/interface'
|
|
11
|
+
import type { Keychain } from '@libp2p/keychain'
|
|
12
|
+
import type { Datastore } from 'interface-datastore'
|
|
13
|
+
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
14
|
+
|
|
15
|
+
export interface IPNSPublisherComponents {
|
|
16
|
+
datastore: Datastore
|
|
17
|
+
logger: ComponentLogger
|
|
18
|
+
libp2p: Libp2p<{ keychain: Keychain }>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IPNSPublisherInit {
|
|
22
|
+
localStore: LocalStore
|
|
23
|
+
routers: IPNSRouting[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class IPNSPublisher {
|
|
27
|
+
public readonly routers: IPNSRouting[]
|
|
28
|
+
private readonly localStore: LocalStore
|
|
29
|
+
private readonly keychain: Keychain
|
|
30
|
+
|
|
31
|
+
constructor (components: IPNSPublisherComponents, init: IPNSPublisherInit) {
|
|
32
|
+
this.keychain = components.libp2p.services.keychain
|
|
33
|
+
this.localStore = init.localStore
|
|
34
|
+
this.routers = init.routers
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async publish (keyName: string, value: CID | PublicKey | MultihashDigest<0x00 | 0x12> | PeerId | string, options: PublishOptions = {}): Promise<IPNSPublishResult> {
|
|
38
|
+
try {
|
|
39
|
+
const privKey = await this.#loadOrCreateKey(keyName)
|
|
40
|
+
let sequenceNumber = 1n
|
|
41
|
+
const routingKey = multihashToIPNSRoutingKey(privKey.publicKey.toMultihash())
|
|
42
|
+
|
|
43
|
+
if (await this.localStore.has(routingKey, options)) {
|
|
44
|
+
// if we have published under this key before, increment the sequence number
|
|
45
|
+
const { record } = await this.localStore.get(routingKey, options)
|
|
46
|
+
const existingRecord = unmarshalIPNSRecord(record)
|
|
47
|
+
sequenceNumber = existingRecord.sequence + 1n
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isPeerId(value)) {
|
|
51
|
+
value = value.toCID()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// convert ttl from milliseconds to nanoseconds as createIPNSRecord expects
|
|
55
|
+
const ttlNs = options.ttl != null ? BigInt(options.ttl) * 1_000_000n : DEFAULT_TTL_NS
|
|
56
|
+
const lifetime = options.lifetime ?? DEFAULT_LIFETIME_MS
|
|
57
|
+
const record = await createIPNSRecord(privKey, value, sequenceNumber, lifetime, { ...options, ttlNs })
|
|
58
|
+
const marshaledRecord = marshalIPNSRecord(record)
|
|
59
|
+
|
|
60
|
+
if (options.offline === true) {
|
|
61
|
+
// only store record locally
|
|
62
|
+
await this.localStore.put(routingKey, marshaledRecord, { ...options, metadata: { keyName, lifetime } })
|
|
63
|
+
} else {
|
|
64
|
+
// publish record to routing (including the local store)
|
|
65
|
+
await Promise.all(this.routers.map(async r => {
|
|
66
|
+
await r.put(routingKey, marshaledRecord, { ...options, metadata: { keyName, lifetime } })
|
|
67
|
+
}))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
record,
|
|
72
|
+
publicKey: privKey.publicKey
|
|
73
|
+
}
|
|
74
|
+
} catch (err: any) {
|
|
75
|
+
options.onProgress?.(new CustomProgressEvent<Error>('ipns:publish:error', err))
|
|
76
|
+
throw err
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async #loadOrCreateKey (keyName: string): Promise<PrivateKey> {
|
|
81
|
+
try {
|
|
82
|
+
return await this.keychain.exportKey(keyName)
|
|
83
|
+
} catch (err: any) {
|
|
84
|
+
// If no named key found in keychain, generate and import
|
|
85
|
+
const privKey = await generateKeyPair('Ed25519')
|
|
86
|
+
await this.keychain.importKey(keyName, privKey)
|
|
87
|
+
return privKey
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async unpublish (keyName: string, options?: AbortOptions): Promise<void> {
|
|
92
|
+
const { publicKey } = await this.keychain.exportKey(keyName)
|
|
93
|
+
const digest = publicKey.toMultihash()
|
|
94
|
+
const routingKey = multihashToIPNSRoutingKey(digest)
|
|
95
|
+
await this.localStore.delete(routingKey, options)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { Queue, repeatingTask } from '@libp2p/utils'
|
|
2
|
+
import { createIPNSRecord, marshalIPNSRecord, unmarshalIPNSRecord } from 'ipns'
|
|
3
|
+
import { DEFAULT_REPUBLISH_CONCURRENCY, DEFAULT_REPUBLISH_INTERVAL_MS, DEFAULT_TTL_NS } from '../constants.ts'
|
|
4
|
+
import { shouldRepublish } from '../utils.js'
|
|
5
|
+
import type { LocalStore } from '../local-store.js'
|
|
6
|
+
import type { IPNSRouting } from '../routing/index.js'
|
|
7
|
+
import type { AbortOptions, ComponentLogger, Libp2p, Logger, PrivateKey } from '@libp2p/interface'
|
|
8
|
+
import type { Keychain } from '@libp2p/keychain'
|
|
9
|
+
import type { RepeatingTask } from '@libp2p/utils'
|
|
10
|
+
import type { IPNSRecord } from 'ipns'
|
|
11
|
+
|
|
12
|
+
export interface IPNSRepublisherComponents {
|
|
13
|
+
logger: ComponentLogger
|
|
14
|
+
libp2p: Libp2p<{ keychain: Keychain }>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface IPNSRepublisherInit {
|
|
18
|
+
republishConcurrency?: number
|
|
19
|
+
republishInterval?: number
|
|
20
|
+
routers: IPNSRouting[]
|
|
21
|
+
localStore: LocalStore
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class IPNSRepublisher {
|
|
25
|
+
public readonly routers: IPNSRouting[]
|
|
26
|
+
private readonly localStore: LocalStore
|
|
27
|
+
private readonly republishTask: RepeatingTask
|
|
28
|
+
private readonly log: Logger
|
|
29
|
+
private readonly keychain: Keychain
|
|
30
|
+
private started: boolean = false
|
|
31
|
+
private readonly republishConcurrency: number
|
|
32
|
+
|
|
33
|
+
constructor (components: IPNSRepublisherComponents, init: IPNSRepublisherInit) {
|
|
34
|
+
this.log = components.logger.forComponent('helia:ipns')
|
|
35
|
+
this.localStore = init.localStore
|
|
36
|
+
this.keychain = components.libp2p.services.keychain
|
|
37
|
+
this.republishConcurrency = init.republishConcurrency || DEFAULT_REPUBLISH_CONCURRENCY
|
|
38
|
+
this.started = components.libp2p.status === 'started'
|
|
39
|
+
this.routers = init.routers ?? []
|
|
40
|
+
|
|
41
|
+
this.republishTask = repeatingTask(this.#republish.bind(this), init.republishInterval ?? DEFAULT_REPUBLISH_INTERVAL_MS, {
|
|
42
|
+
runImmediately: true
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
if (this.started) {
|
|
46
|
+
this.republishTask.start()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
start (): void {
|
|
51
|
+
if (this.started) {
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.started = true
|
|
56
|
+
this.republishTask.start()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
stop (): void {
|
|
60
|
+
if (!this.started) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.started = false
|
|
65
|
+
this.republishTask.stop()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async #republish (options: AbortOptions = {}): Promise<void> {
|
|
69
|
+
if (!this.started) {
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
this.log('starting ipns republish records loop')
|
|
74
|
+
|
|
75
|
+
const queue = new Queue({
|
|
76
|
+
concurrency: this.republishConcurrency
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const recordsToRepublish: Array<{ routingKey: Uint8Array, record: IPNSRecord }> = []
|
|
81
|
+
|
|
82
|
+
// Find all records using the localStore.list method
|
|
83
|
+
for await (const { routingKey, record, metadata, created } of this.localStore.list(options)) {
|
|
84
|
+
if (metadata == null) {
|
|
85
|
+
// Skip if no metadata is found from before we started
|
|
86
|
+
// storing metadata or for records republished without a key
|
|
87
|
+
this.log(`no metadata found for record ${routingKey.toString()}, skipping`)
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
let ipnsRecord: IPNSRecord
|
|
91
|
+
try {
|
|
92
|
+
ipnsRecord = unmarshalIPNSRecord(record)
|
|
93
|
+
} catch (err: any) {
|
|
94
|
+
this.log.error('error unmarshaling record - %e', err)
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Only republish records that are within the DHT or record expiry threshold
|
|
99
|
+
if (!shouldRepublish(ipnsRecord, created)) {
|
|
100
|
+
this.log.trace(`skipping record ${routingKey.toString()}within republish threshold`)
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
const sequenceNumber = ipnsRecord.sequence + 1n
|
|
104
|
+
const ttlNs = ipnsRecord.ttl ?? DEFAULT_TTL_NS
|
|
105
|
+
let privKey: PrivateKey
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
privKey = await this.keychain.exportKey(metadata.keyName)
|
|
109
|
+
} catch (err: any) {
|
|
110
|
+
this.log.error(`missing key ${metadata.keyName}, skipping republishing record`, err)
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const updatedRecord = await createIPNSRecord(privKey, ipnsRecord.value, sequenceNumber, metadata.lifetime, { ...options, ttlNs })
|
|
115
|
+
recordsToRepublish.push({ routingKey, record: updatedRecord })
|
|
116
|
+
} catch (err: any) {
|
|
117
|
+
this.log.error(`error creating updated IPNS record for ${routingKey.toString()}`, err)
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
this.log(`found ${recordsToRepublish.length} records to republish`)
|
|
123
|
+
|
|
124
|
+
// Republish each record
|
|
125
|
+
for (const { routingKey, record } of recordsToRepublish) {
|
|
126
|
+
// Add job to queue to republish the record to all routers
|
|
127
|
+
queue.add(async () => {
|
|
128
|
+
try {
|
|
129
|
+
const marshaledRecord = marshalIPNSRecord(record)
|
|
130
|
+
await Promise.all(
|
|
131
|
+
this.routers.map(r => r.put(routingKey, marshaledRecord, options))
|
|
132
|
+
)
|
|
133
|
+
} catch (err: any) {
|
|
134
|
+
this.log.error('error republishing record - %e', err)
|
|
135
|
+
}
|
|
136
|
+
}, options)
|
|
137
|
+
}
|
|
138
|
+
} catch (err: any) {
|
|
139
|
+
this.log.error('error during republish - %e', err)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
await queue.onIdle(options) // Wait for all jobs to complete
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { NotFoundError, isPeerId, isPublicKey } from '@libp2p/interface'
|
|
2
|
+
import { multihashToIPNSRoutingKey, unmarshalIPNSRecord } from 'ipns'
|
|
3
|
+
import { ipnsSelector } from 'ipns/selector'
|
|
4
|
+
import { ipnsValidator } from 'ipns/validator'
|
|
5
|
+
import { base36 } from 'multiformats/bases/base36'
|
|
6
|
+
import { base58btc } from 'multiformats/bases/base58'
|
|
7
|
+
import { CID } from 'multiformats/cid'
|
|
8
|
+
import * as Digest from 'multiformats/hashes/digest'
|
|
9
|
+
import { DEFAULT_TTL_NS } from '../constants.ts'
|
|
10
|
+
import { InvalidValueError, RecordsFailedValidationError, UnsupportedMultibasePrefixError, UnsupportedMultihashCodecError } from '../errors.js'
|
|
11
|
+
import { isCodec, IDENTITY_CODEC, SHA2_256_CODEC, isLibp2pCID } from '../utils.js'
|
|
12
|
+
import type { IPNSResolveResult, ResolveOptions, ResolveResult } from '../index.js'
|
|
13
|
+
import type { LocalStore } from '../local-store.js'
|
|
14
|
+
import type { IPNSRouting } from '../routing/index.js'
|
|
15
|
+
import type { Routing } from '@helia/interface'
|
|
16
|
+
import type { ComponentLogger, Logger, PeerId, PublicKey } from '@libp2p/interface'
|
|
17
|
+
import type { Datastore } from 'interface-datastore'
|
|
18
|
+
import type { IPNSRecord } from 'ipns'
|
|
19
|
+
import type { MultibaseDecoder } from 'multiformats/bases/interface'
|
|
20
|
+
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
21
|
+
|
|
22
|
+
const bases: Record<string, MultibaseDecoder<string>> = {
|
|
23
|
+
[base36.prefix]: base36,
|
|
24
|
+
[base58btc.prefix]: base58btc
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface IPNSResolverComponents {
|
|
28
|
+
datastore: Datastore
|
|
29
|
+
routing: Routing
|
|
30
|
+
logger: ComponentLogger
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface IPNResolverInit {
|
|
34
|
+
localStore: LocalStore
|
|
35
|
+
routers: IPNSRouting[]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class IPNSResolver {
|
|
39
|
+
public readonly routers: IPNSRouting[]
|
|
40
|
+
private readonly localStore: LocalStore
|
|
41
|
+
private readonly log: Logger
|
|
42
|
+
|
|
43
|
+
constructor (components: IPNSResolverComponents, init: IPNResolverInit) {
|
|
44
|
+
this.log = components.logger.forComponent('helia:ipns')
|
|
45
|
+
this.localStore = init.localStore
|
|
46
|
+
this.routers = init.routers
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async resolve (key: CID<unknown, 0x72, 0x00 | 0x12, 1> | PublicKey | MultihashDigest<0x00 | 0x12> | PeerId, options: ResolveOptions = {}): Promise<IPNSResolveResult> {
|
|
50
|
+
const digest = isPublicKey(key) || isPeerId(key) ? key.toMultihash() : isLibp2pCID(key) ? key.multihash : key
|
|
51
|
+
const routingKey = multihashToIPNSRoutingKey(digest)
|
|
52
|
+
const record = await this.#findIpnsRecord(routingKey, options)
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
...(await this.#resolve(record.value, options)),
|
|
56
|
+
record
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async #resolve (ipfsPath: string, options: ResolveOptions = {}): Promise<ResolveResult> {
|
|
61
|
+
const parts = ipfsPath.split('/')
|
|
62
|
+
try {
|
|
63
|
+
const scheme = parts[1]
|
|
64
|
+
|
|
65
|
+
if (scheme === 'ipns') {
|
|
66
|
+
const str = parts[2]
|
|
67
|
+
const prefix = str.substring(0, 1)
|
|
68
|
+
let buf: Uint8Array | undefined
|
|
69
|
+
|
|
70
|
+
if (prefix === '1' || prefix === 'Q') {
|
|
71
|
+
buf = base58btc.decode(`z${str}`)
|
|
72
|
+
} else if (bases[prefix] != null) {
|
|
73
|
+
buf = bases[prefix].decode(str)
|
|
74
|
+
} else {
|
|
75
|
+
throw new UnsupportedMultibasePrefixError(`Unsupported multibase prefix "${prefix}"`)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let digest: MultihashDigest<number>
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
digest = Digest.decode(buf)
|
|
82
|
+
} catch {
|
|
83
|
+
digest = CID.decode(buf).multihash
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!isCodec(digest, IDENTITY_CODEC) && !isCodec(digest, SHA2_256_CODEC)) {
|
|
87
|
+
throw new UnsupportedMultihashCodecError(`Unsupported multihash codec "${digest.code}"`)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const { cid } = await this.resolve(digest, options)
|
|
91
|
+
const path = parts.slice(3).join('/')
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
cid,
|
|
95
|
+
path: path === '' ? undefined : path
|
|
96
|
+
}
|
|
97
|
+
} else if (scheme === 'ipfs') {
|
|
98
|
+
const cid = CID.parse(parts[2])
|
|
99
|
+
const path = parts.slice(3).join('/')
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
cid,
|
|
103
|
+
path: path === '' ? undefined : path
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} catch (err) {
|
|
107
|
+
this.log.error('error parsing ipfs path - %e', err)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this.log.error('invalid ipfs path %s', ipfsPath)
|
|
111
|
+
throw new InvalidValueError('Invalid value')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async #findIpnsRecord (routingKey: Uint8Array, options: ResolveOptions = {}): Promise<IPNSRecord> {
|
|
115
|
+
const records: Uint8Array[] = []
|
|
116
|
+
const cached = await this.localStore.has(routingKey, options)
|
|
117
|
+
|
|
118
|
+
if (cached) {
|
|
119
|
+
this.log('record is present in the cache')
|
|
120
|
+
|
|
121
|
+
if (options.nocache !== true) {
|
|
122
|
+
try {
|
|
123
|
+
// check the local cache first
|
|
124
|
+
const { record, created } = await this.localStore.get(routingKey, options)
|
|
125
|
+
|
|
126
|
+
this.log('record retrieved from cache')
|
|
127
|
+
|
|
128
|
+
// validate the record
|
|
129
|
+
await ipnsValidator(routingKey, record)
|
|
130
|
+
|
|
131
|
+
this.log('record was valid')
|
|
132
|
+
|
|
133
|
+
// check the TTL
|
|
134
|
+
const ipnsRecord = unmarshalIPNSRecord(record)
|
|
135
|
+
|
|
136
|
+
// IPNS TTL is in nanoseconds, convert to milliseconds, default to one
|
|
137
|
+
// hour
|
|
138
|
+
const ttlMs = Number((ipnsRecord.ttl ?? DEFAULT_TTL_NS) / 1_000_000n)
|
|
139
|
+
const ttlExpires = created.getTime() + ttlMs
|
|
140
|
+
|
|
141
|
+
if (ttlExpires > Date.now()) {
|
|
142
|
+
// the TTL has not yet expired, return the cached record
|
|
143
|
+
this.log('record TTL was valid')
|
|
144
|
+
return ipnsRecord
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (options.offline === true) {
|
|
148
|
+
// the TTL has expired but we are skipping the routing search
|
|
149
|
+
this.log('record TTL has been reached but we are resolving offline-only, returning record')
|
|
150
|
+
return ipnsRecord
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this.log('record TTL has been reached, searching routing for updates')
|
|
154
|
+
|
|
155
|
+
// add the local record to our list of resolved record, and also
|
|
156
|
+
// search the routing for updates - the most up to date record will be
|
|
157
|
+
// returned
|
|
158
|
+
records.push(record)
|
|
159
|
+
} catch (err) {
|
|
160
|
+
this.log('cached record was invalid - %e', err)
|
|
161
|
+
await this.localStore.delete(routingKey, options)
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
this.log('ignoring local cache due to nocache=true option')
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (options.offline === true) {
|
|
169
|
+
throw new NotFoundError('Record was not present in the cache or has expired')
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.log('did not have record locally')
|
|
173
|
+
|
|
174
|
+
let foundInvalid = 0
|
|
175
|
+
|
|
176
|
+
await Promise.all(
|
|
177
|
+
this.routers.map(async (router) => {
|
|
178
|
+
let record: Uint8Array
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
record = await router.get(routingKey, {
|
|
182
|
+
...options,
|
|
183
|
+
validate: false
|
|
184
|
+
})
|
|
185
|
+
} catch (err: any) {
|
|
186
|
+
this.log.error('error finding IPNS record - %e', err)
|
|
187
|
+
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
await ipnsValidator(routingKey, record)
|
|
193
|
+
|
|
194
|
+
records.push(record)
|
|
195
|
+
} catch (err) {
|
|
196
|
+
// we found a record, but the validator rejected it
|
|
197
|
+
foundInvalid++
|
|
198
|
+
this.log.error('error finding IPNS record - %e', err)
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
if (records.length === 0) {
|
|
204
|
+
if (foundInvalid > 0) {
|
|
205
|
+
throw new RecordsFailedValidationError(`${foundInvalid > 1 ? `${foundInvalid} records` : 'Record'} found for routing key ${foundInvalid > 1 ? 'were' : 'was'} invalid`)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
throw new NotFoundError('Could not find record for routing key')
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const record = records[ipnsSelector(routingKey, records)]
|
|
212
|
+
|
|
213
|
+
await this.localStore.put(routingKey, record, options)
|
|
214
|
+
|
|
215
|
+
return unmarshalIPNSRecord(record)
|
|
216
|
+
}
|
|
217
|
+
}
|
package/src/ipns.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { CID } from 'multiformats/cid'
|
|
2
|
+
import { IPNSPublisher } from './ipns/publisher.ts'
|
|
3
|
+
import { IPNSRepublisher } from './ipns/republisher.ts'
|
|
4
|
+
import { IPNSResolver } from './ipns/resolver.ts'
|
|
5
|
+
import { localStore } from './local-store.js'
|
|
6
|
+
import { helia } from './routing/helia.js'
|
|
7
|
+
import { localStoreRouting } from './routing/local-store.ts'
|
|
8
|
+
import type { IPNSComponents, IPNS as IPNSInterface, IPNSOptions, IPNSPublishResult, IPNSResolveResult, PublishOptions, ResolveOptions } from './index.js'
|
|
9
|
+
import type { LocalStore } from './local-store.js'
|
|
10
|
+
import type { IPNSRouting } from './routing/index.js'
|
|
11
|
+
import type { AbortOptions, PeerId, PublicKey, Startable } from '@libp2p/interface'
|
|
12
|
+
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
13
|
+
|
|
14
|
+
export class IPNS implements IPNSInterface, Startable {
|
|
15
|
+
public readonly routers: IPNSRouting[]
|
|
16
|
+
private readonly publisher: IPNSPublisher
|
|
17
|
+
private readonly republisher: IPNSRepublisher
|
|
18
|
+
private readonly resolver: IPNSResolver
|
|
19
|
+
private readonly localStore: LocalStore
|
|
20
|
+
private started: boolean
|
|
21
|
+
|
|
22
|
+
constructor (components: IPNSComponents, init: IPNSOptions = {}) {
|
|
23
|
+
this.localStore = localStore(components.datastore, components.logger.forComponent('helia:ipns:local-store'))
|
|
24
|
+
this.started = components.libp2p.status === 'started'
|
|
25
|
+
|
|
26
|
+
this.routers = [
|
|
27
|
+
localStoreRouting(this.localStore),
|
|
28
|
+
helia(components.routing),
|
|
29
|
+
...(init.routers ?? [])
|
|
30
|
+
]
|
|
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
|
+
|
|
48
|
+
// start republishing on Helia start
|
|
49
|
+
components.events.addEventListener('start', this.start.bind(this))
|
|
50
|
+
// stop republishing on Helia stop
|
|
51
|
+
components.events.addEventListener('stop', this.stop.bind(this))
|
|
52
|
+
|
|
53
|
+
if (this.started) {
|
|
54
|
+
this.republisher.start()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
start (): void {
|
|
59
|
+
if (this.started) {
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.started = true
|
|
64
|
+
this.republisher.start()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
stop (): void {
|
|
68
|
+
if (!this.started) {
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.started = false
|
|
73
|
+
this.republisher.stop()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async publish (keyName: string, value: CID | PublicKey | MultihashDigest<0x00 | 0x12> | PeerId | string, options: PublishOptions = {}): Promise<IPNSPublishResult> {
|
|
77
|
+
return this.publisher.publish(keyName, value, options)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async resolve (key: CID<unknown, 0x72, 0x00 | 0x12, 1> | PublicKey | MultihashDigest<0x00 | 0x12> | PeerId, options: ResolveOptions = {}): Promise<IPNSResolveResult> {
|
|
81
|
+
return this.resolver.resolve(key, options)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async unpublish (keyName: string, options?: AbortOptions): Promise<void> {
|
|
85
|
+
return this.publisher.unpublish(keyName, options)
|
|
86
|
+
}
|
|
87
|
+
}
|