From fd6e1bf0b66de78ea0ca80f6ad787e0f94652341 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sun, 24 Jun 2018 21:28:25 +1200 Subject: [PATCH] feat: add util.cid options See ipld/interface-ipld-format#40 --- src/util.js | 8 +++++++- test/util.spec.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index 1635e13..f965ea7 100644 --- a/src/util.js +++ b/src/util.js @@ -66,10 +66,16 @@ const deserialize = (binaryBlob, callback) => { * Get the CID of the DAG-Node. * * @param {ZcashBlock} dagNode - Internal representation of a Zcash block + * @param {Object} [options] - Ignored * @param {CidCallback} callback - Callback that handles the return value * @returns {void} */ -const cid = (dagNode, callback) => { +const cid = (dagNode, options, callback) => { + if (options instanceof Function) { + callback = options + options = {} + } + options = options || {} let err = null let cid try { diff --git a/test/util.spec.js b/test/util.spec.js index 3e5dd12..89d1ab5 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -63,6 +63,17 @@ describe('IPLD format util API cid()', () => { }) }) + it('should encode the CID correctly and ignore options', (done) => { + IpldZcash.util.deserialize(fixtureBlock, (err, dagNode) => { + expect(err).to.not.exist() + verifyCid1( + dagNode, + { hashAlg: 'unknown' }, + '5620e1451fd8fecefdd9d443f294bc5ae918301922088ba51d35a2a4672c00000000', + done) + }) + }) + it('should error on an invalid internal representation', (done) => { IpldZcash.util.cid(invalidDagNode, (err, cid) => { expect(cid).to.not.exist() @@ -90,3 +101,11 @@ const verifyCid = (dagNode, expectedCid, doneCb) => { doneCb() }) } + +const verifyCid1 = (dagNode, options, expectedCid, doneCb) => { + IpldZcash.util.cid(dagNode, options, (err, cid) => { + expect(err).to.not.exist() + expect(cid.multihash.toString('hex')).to.equal(expectedCid) + doneCb() + }) +}