From 16ae34b5bc7354256f90e8eac99203b831625341 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sun, 24 Jun 2018 11:48:19 +1200 Subject: [PATCH 1/2] feat: add util.cid options See https://github.com/ipld/interface-ipld-format/issues/40 --- src/util.js | 34 ++++++++++++++++++++++++++++++---- test/dag-node-test.js | 18 ++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/util.js b/src/util.js index 1f422a7..a57e64b 100644 --- a/src/util.js +++ b/src/util.js @@ -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) { diff --git a/test/dag-node-test.js b/test/dag-node-test.js index ab364a4..3c99cb9 100644 --- a/test/dag-node-test.js +++ b/test/dag-node-test.js @@ -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') @@ -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() }) }) From 43305eab948de86f85970ff0fb25d14eae32fcdb Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Mon, 25 Jun 2018 09:29:07 +1200 Subject: [PATCH 2/2] fix: changes from the review --- src/util.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/util.js b/src/util.js index a57e64b..baf2ccb 100644 --- a/src/util.js +++ b/src/util.js @@ -27,13 +27,16 @@ exports = module.exports * @returns {void} */ function cid (dagNode, options, callback) { - if (options instanceof Function) { + if (typeof options === 'function') { callback = options options = {} } options = options || {} const hashAlg = options.hashAlg || resolver.defaultHashAlg - const version = options.version || hashAlg === 'sha2-256' ? 0 : 1 + let version = options.version + if (typeof version === 'undefined') { + version = hashAlg === 'sha2-256' ? 0 : 1 + } waterfall([ (cb) => serialize(dagNode, cb), (serialized, cb) => multihashing(serialized, hashAlg, cb),