Skip to content

Commit 576bc1a

Browse files
eordanovasco-santos
authored andcommitted
feat: add creatWithExpiration function (#9)
1 parent 53779b1 commit 576bc1a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,26 @@ const create = (privateKey, value, seq, lifetime, callback) => {
3939
// Validity in ISOString with nanoseconds precision and validity type EOL
4040
const isoValidity = nanoDateEol.toISOStringFull()
4141
const validityType = ipnsEntryProto.ValidityType.EOL
42+
_create(privateKey, value, seq, isoValidity, validityType, callback)
43+
}
44+
45+
/**
46+
* Same as create(), but instead of generating a new Date, it receives the intended expiration time
47+
* WARNING: nano precision is not standard, make sure the value in seconds is 9 orders of magnitude lesser than the one provided.
48+
* @param {Object} privateKey private key for signing the record.
49+
* @param {string} value value to be stored in the record.
50+
* @param {number} seq number representing the current version of the record.
51+
* @param {string} expiration expiration time of the record (in nanoseconds).
52+
* @param {function(Error, entry)} [callback]
53+
* @return {Void}
54+
*/
55+
const createWithExpiration = (privateKey, value, seq, expiration, callback) => {
56+
const bnExpiration = new NanoDate(new Big(expiration).toString()).toISOStringFull()
57+
const validityType = ipnsEntryProto.ValidityType.EOL
58+
_create(privateKey, value, seq, bnExpiration, validityType, callback)
59+
}
4260

61+
const _create = (privateKey, value, seq, isoValidity, validityType, callback) => {
4362
sign(privateKey, value, validityType, isoValidity, (error, signature) => {
4463
if (error) {
4564
log.error('record signature creation failed')
@@ -294,6 +313,8 @@ const validator = {
294313
module.exports = {
295314
// create ipns entry record
296315
create,
316+
// create ipns entry record specifying the expiration time
317+
createWithExpiration,
297318
// validate ipns entry record
298319
validate,
299320
// embed public key in the record

test/index.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ describe('ipns', function () {
8282
})
8383
})
8484

85+
it('should be able to create a record with a fixed expiration', (done) => {
86+
const sequence = 0
87+
// 2033-05-18T03:33:20.000000000Z
88+
const expiration = 2000000000 * 1000000000
89+
90+
ipns.createWithExpiration(rsa, cid, sequence, expiration, (err, entry) => {
91+
expect(err).to.not.exist()
92+
93+
ipns.validate(rsa.public, entry, (err) => {
94+
expect(err).to.not.exist()
95+
expect(entry).to.have.a.property('validity')
96+
expect(entry.validity).to.equal('2033-05-18T03:33:20.000000000Z')
97+
done()
98+
})
99+
})
100+
})
101+
85102
it('should create an ipns record and validate it correctly', (done) => {
86103
const sequence = 0
87104
const validity = 1000000

0 commit comments

Comments
 (0)