Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

feat: add util.cid options #66

Merged
merged 3 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 25 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,32 @@ exports.deserialize = (data, callback) => {
setImmediate(() => callback(null, deserialized))
}

exports.cid = (dagNode, callback) => {
/**
* @callback CidCallback
* @param {?Error} error - Error if getting the CID failed
* @param {?CID} cid - CID if call was successful
*/
/**
* Get the CID of the DAG-Node.
*
* @param {Object} dagNode - Internal representation
* @param {Object} [options] - Options to create the CID
* @param {number} [options.version=1] - CID version number
* @param {string} [options.hashAlg] - Defaults to hashAlg for the resolver
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
exports.cid = (dagNode, options, callback) => {
if (options instanceof Function) {
Copy link
Member

Choose a reason for hiding this comment

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

Searching through the existing IPFS/IPLD code base it seems that the preferred way for checking if something is a function is: if (typeof options !== 'function') {. My guess is that it is related to code minifiers. Please change it.

callback = options
options = {}
}
options = options || {}
const hashAlg = options.hashAlg || resolver.defaultHashAlg
const version = options.version || 1
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't work correctly. If options.version is set to 0, then version would become 1 as 0 evaluates to false.

waterfall([
(cb) => exports.serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, resolver.defaultHashAlg, cb),
(mh, cb) => cb(null, new CID(1, resolver.multicodec, mh))
(serialized, cb) => multihashing(serialized, hashAlg, cb),
(mh, cb) => cb(null, new CID(version, resolver.multicodec, mh))
], callback)
}
15 changes: 15 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ chai.use(dirtyChai)
const garbage = require('garbage')
const map = require('async/map')
const dagCBOR = require('../src')
const multihash = require('multihashes')

describe('util', () => {
const obj = {
Expand Down Expand Up @@ -58,6 +59,20 @@ describe('util', () => {
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})

it('.cid with hashAlg', (done) => {
dagCBOR.util.cid(obj, { hashAlg: 'sha2-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})
Expand Down