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

Commit

Permalink
feat: add util.cid options
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jun 23, 2018
1 parent 424d2a1 commit 16ae34b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
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) {
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

0 comments on commit 16ae34b

Please sign in to comment.