Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing custom ttl when publishing ipns #723

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/ipns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ export interface PublishOptions extends AbortOptions, ProgressOptions<PublishPro
* false here to only add a V2 signature. (default: true)
*/
v1Compatible?: boolean

/**
* The TTL of the record in ms (default: 1 hour)
*/
ttl?: number
}

export interface ResolveOptions extends AbortOptions, ProgressOptions<ResolveProgressEvents | IPNSRoutingEvents> {
Expand Down Expand Up @@ -429,6 +434,8 @@ export interface IPNS {

export type { IPNSRouting } from './routing/index.js'

export type { IPNSRecord } from 'ipns'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we want to export this or have folks import it from 'ipns' directly. If it's just a type, it shouldn't cause bundle-bloat issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah to be honest, this one was really needed and the only thing I ended up needing to import from ipns so I decided it's used enough to be reexported.


export interface IPNSComponents {
datastore: Datastore
routing: Routing
Expand Down Expand Up @@ -470,8 +477,9 @@ class DefaultIPNS implements IPNS {
sequenceNumber = existingRecord.sequence + 1n
}

// create record
const record = await createIPNSRecord(key, value, sequenceNumber, options.lifetime ?? DEFAULT_LIFETIME_MS, options)
// convert ttl from milliseconds to nanoseconds as createIPNSRecord expects
const ttlNs = options.ttl != null ? BigInt(options.ttl) * 1_000_000n : DEFAULT_TTL_NS
const record = await createIPNSRecord(key, value, sequenceNumber, options.lifetime ?? DEFAULT_LIFETIME_MS, { ...options, ttlNs })
const marshaledRecord = marshalIPNSRecord(record)

await this.localStore.put(routingKey, marshaledRecord, options)
Expand Down
24 changes: 22 additions & 2 deletions packages/ipns/test/publish.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,35 @@ describe('publish', () => {
expect(ipnsEntry).to.have.property('ttl', 3_600_000_000_000n) // 1 hour
})

it('should publish an IPNS record with a custom ttl params', async function () {
it('should publish an IPNS record with a custom lifetime params', async function () {
const key = await generateKeyPair('Ed25519')
const lifetime = 123000
// lifetime is used to calculate the validity timestamp
const ipnsEntry = await name.publish(key, cid, {
lifetime
})

expect(ipnsEntry).to.have.property('sequence', 1n)
expect(ipnsEntry).to.have.property('ttl', 3_600_000_000_000n)

// Ignore the milliseconds after the dot 2025-01-22T12:07:33.650000000Z
const expectedValidity = new Date(Date.now() + lifetime).toISOString().split('.')[0]

expect(ipnsEntry.validity.split('.')[0]).to.equal(expectedValidity)

expect(heliaRouting.put.called).to.be.true()
expect(customRouting.put.called).to.be.true()
})

it('should publish an IPNS record with a custom ttl params', async function () {
const key = await generateKeyPair('Ed25519')
const ttl = 1000 // override the default ttl

const ipnsEntry = await name.publish(key, cid, {
ttl
})

expect(ipnsEntry).to.have.property('sequence', 1n)
expect(ipnsEntry).to.have.property('ttl', BigInt(ttl * 1e+6))

expect(heliaRouting.put.called).to.be.true()
expect(customRouting.put.called).to.be.true()
Expand Down
Loading