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: prevent side effect on create #9

Merged
merged 1 commit into from
Sep 24, 2018
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
21 changes: 21 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,26 @@ const create = (privateKey, value, seq, lifetime, callback) => {
// Validity in ISOString with nanoseconds precision and validity type EOL
const isoValidity = nanoDateEol.toISOStringFull()
const validityType = ipnsEntryProto.ValidityType.EOL
_create(privateKey, value, seq, isoValidity, validityType, callback)
}

/**
* Same as create(), but instead of generating a new Date, it receives the intended expiration time
* WARNING: nano precision is not standard, make sure the value in seconds is 9 orders of magnitude lesser than the one provided.
* @param {Object} privateKey private key for signing the record.
* @param {string} value value to be stored in the record.
* @param {number} seq number representing the current version of the record.
* @param {string} expiration expiration time of the record (in nanoseconds).
* @param {function(Error, entry)} [callback]
* @return {Void}
*/
const createWithExpiration = (privateKey, value, seq, expiration, callback) => {
const bnExpiration = new NanoDate(new Big(expiration).toString()).toISOStringFull()
const validityType = ipnsEntryProto.ValidityType.EOL
_create(privateKey, value, seq, bnExpiration, validityType, callback)
}

const _create = (privateKey, value, seq, isoValidity, validityType, callback) => {
sign(privateKey, value, validityType, isoValidity, (error, signature) => {
if (error) {
log.error('record signature creation failed')
Expand Down Expand Up @@ -294,6 +313,8 @@ const validator = {
module.exports = {
// create ipns entry record
create,
// create ipns entry record specifying the expiration time
createWithExpiration,
// validate ipns entry record
validate,
// embed public key in the record
Expand Down
17 changes: 17 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ describe('ipns', function () {
})
})

it('should be able to create a record with a fixed expiration', (done) => {
const sequence = 0
// 2033-05-18T03:33:20.000000000Z
const expiration = 2000000000 * 1000000000

ipns.createWithExpiration(rsa, cid, sequence, expiration, (err, entry) => {
expect(err).to.not.exist()

ipns.validate(rsa.public, entry, (err) => {
expect(err).to.not.exist()
expect(entry).to.have.a.property('validity')
expect(entry.validity).to.equal('2033-05-18T03:33:20.000000000Z')
done()
})
})
})

it('should create an ipns record and validate it correctly', (done) => {
const sequence = 0
const validity = 1000000
Expand Down