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 #74

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 30 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,42 @@
const CID = require('cids')
const protons = require('protons')
const proto = protons(require('./dag.proto.js'))
const resolver = require('./resolver')
const DAGLink = require('./dag-link')
const DAGNode = require('./dag-node')
const multihashing = require('multihashing-async')
const waterfall = require('async/waterfall')

exports = module.exports

function cid (node, callback) {
if (node.multihash) {
return callback(null, new CID(node.multihash))
/**
* @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] - CID version number. Defaults to zero if hashAlg == 'sha2-256'; otherwise, 1.
* @param {string} [options.hashAlg] - Defaults to hashAlg for the resolver
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
function 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 = {}
}
callback(new Error('not valid dagPB node'))
options = options || {}
const hashAlg = options.hashAlg || resolver.defaultHashAlg
const version = options.version || hashAlg === 'sha2-256' ? 0 : 1
waterfall([
(cb) => serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, hashAlg, cb),
(mh, cb) => cb(null, new CID(version, resolver.multicodec, mh))
], callback)
}

function serialize (node, callback) {
Expand Down
18 changes: 18 additions & 0 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const util = dagPB.util
const series = require('async/series')
const waterfall = require('async/waterfall')
const isNode = require('detect-node')
const multihash = require('multihashes')

const BlockService = require('ipfs-block-service')
const Block = require('ipfs-block')
Expand Down Expand Up @@ -428,6 +429,23 @@ module.exports = (repo) => {
expect(cid.multihash).to.exist()
expect(cid.codec).to.equal('dag-pb')
expect(cid.version).to.equal(0)
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})
})

it('get node CID with hashAlg', (done) => {
DAGNode.create(Buffer.from('some data'), (err, node) => {
expect(err).to.not.exist()
util.cid(node, { hashAlg: 'sha2-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.multihash).to.exist()
expect(cid.codec).to.equal('dag-pb')
expect(cid.version).to.equal(1)
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})
Expand Down