@@ -16,6 +16,7 @@ import type { CID } from 'multiformats/cid'
16
16
17
17
const log = logger ( 'ipns' )
18
18
const ID_MULTIHASH_CODE = identity . code
19
+ const DEFAULT_TTL = BigInt ( 3.6e+12 ) // 1 Hour or 3600 Seconds
19
20
20
21
export const namespace = '/ipns/'
21
22
export const namespaceLength = namespace . length
@@ -127,6 +128,7 @@ export interface IDKeys {
127
128
}
128
129
129
130
export interface CreateOptions {
131
+ lifetimeNs ?: bigint
130
132
v1Compatible ?: boolean
131
133
}
132
134
@@ -139,7 +141,8 @@ export interface CreateV2Options {
139
141
}
140
142
141
143
const defaultCreateOptions : CreateOptions = {
142
- v1Compatible : true
144
+ v1Compatible : true ,
145
+ lifetimeNs : DEFAULT_TTL
143
146
}
144
147
145
148
/**
@@ -166,8 +169,7 @@ export async function create (peerId: PeerId, value: CID | PeerId | string, seq:
166
169
// Validity in ISOString with nanoseconds precision and validity type EOL
167
170
const expirationDate = new NanoDate ( Date . now ( ) + Number ( lifetime ) )
168
171
const validityType = IpnsEntry . ValidityType . EOL
169
- const [ ms , ns ] = lifetime . toString ( ) . split ( '.' )
170
- const lifetimeNs = ( BigInt ( ms ) * BigInt ( 100000 ) ) + BigInt ( ns ?? '0' )
172
+ const lifetimeNs = typeof options . lifetimeNs === "bigint" ? options . lifetimeNs : DEFAULT_TTL
171
173
172
174
return _create ( peerId , value , seq , validityType , expirationDate , lifetimeNs , options )
173
175
}
@@ -194,11 +196,9 @@ export async function createWithExpiration (peerId: PeerId, value: CID | PeerId
194
196
export async function createWithExpiration ( peerId : PeerId , value : CID | PeerId | string , seq : number | bigint , expiration : string , options : CreateOptions = defaultCreateOptions ) : Promise < IPNSRecord > {
195
197
const expirationDate = NanoDate . fromString ( expiration )
196
198
const validityType = IpnsEntry . ValidityType . EOL
199
+ const lifetimeNs = typeof options . lifetimeNs === "bigint" ? options . lifetimeNs : DEFAULT_TTL
197
200
198
- const ttlMs = expirationDate . toDate ( ) . getTime ( ) - Date . now ( )
199
- const ttlNs = ( BigInt ( ttlMs ) * BigInt ( 100000 ) ) + BigInt ( expirationDate . getNano ( ) )
200
-
201
- return _create ( peerId , value , seq , validityType , expirationDate , ttlNs , options )
201
+ return _create ( peerId , value , seq , validityType , expirationDate , lifetimeNs , options )
202
202
}
203
203
204
204
const _create = async ( peerId : PeerId , value : CID | PeerId | string , seq : number | bigint , validityType : IpnsEntry . ValidityType , expirationDate : NanoDate , ttl : bigint , options : CreateOptions = defaultCreateOptions ) : Promise < IPNSRecord > => {
0 commit comments