From b0c0a7038706c376e2d508a8de4ed19f38cdaa04 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 28 Jun 2018 19:37:35 +1200 Subject: [PATCH] fix: do not ignore cid.options --- src/util.js | 10 +++++++--- test/util.spec.js | 13 ++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/util.js b/src/util.js index dd9e8ec..b153569 100644 --- a/src/util.js +++ b/src/util.js @@ -78,7 +78,9 @@ exports.deserialize = (data, callback) => { * Get the CID of the DAG-Node. * * @param {Object} dagNode - Internal representation - * @param {Object} [options] - Ignored + * @param {Object} [options] - Options to create the CID + * @param {number} [options.version=1] - CID version number + * @param {string} [options.hashAlg='sha1'] - Hashing algorithm * @param {CidCallback} callback - Callback that handles the return value * @returns {void} */ @@ -88,9 +90,11 @@ exports.cid = (dagNode, options, callback) => { options = {} } options = options || {} + const hashAlg = options.hashAlg || resolver.defaultHashAlg + const version = typeof options.version === 'undefined' ? 1 : options.version 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) } diff --git a/test/util.spec.js b/test/util.spec.js index 8f70fd6..92e367f 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -47,14 +47,21 @@ describe('IPLD format util', () => { }) }) - it('.cid ignores options', (done) => { - ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }, (err, cid) => { + it('.cid with options', (done) => { + ipldGit.util.cid(tagNode, { hashAlg: 'sha3-512' }, (err, cid) => { expect(err).to.not.exist() expect(cid.version).to.equal(1) expect(cid.codec).to.equal('git-raw') expect(cid.multihash).to.exist() const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha1') + expect(mh.name).to.equal('sha3-512') + done() + }) + }) + + it('.cid errors unknown hashAlg', (done) => { + ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }, (err, cid) => { + expect(err).to.exist() done() }) })