From d472c99b94b1804f7c1db8766390a4f744ab43d8 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 23 Jun 2018 16:32:57 +1200 Subject: [PATCH 1/3] feat: add util.cid options See https://github.com/ipld/interface-ipld-format/issues/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 d536357..d3fbb9d 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 {BitcoinBlock} dagNode - Internal representation of a Bitcoin 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 6426f59..4d0234c 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -150,6 +150,17 @@ describe('IPLD format util API cid()', () => { done() }) }) + + it('should encode the CID correctly and ignore all options', (done) => { + IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => { + expect(err).to.not.exist() + verifyCid1( + dagNode, + { hashAlg: 'unknown' }, + '56203ec2c691d447b2fd0d6a94742345af1f351037dab1ab9e900200000000000000', + done) + }) + }) }) const verifyBlock = (dagNode, expected) => { @@ -168,3 +179,11 @@ const verifyCid = (dagNode, expectedCid, doneCb) => { doneCb() }) } + +const verifyCid1 = (dagNode, options, expectedCid, doneCb) => { + IpldBitcoin.util.cid(dagNode, options, (err, cid) => { + expect(err).to.not.exist() + expect(cid.multihash.toString('hex')).to.equal(expectedCid) + doneCb() + }) +} From d56d7cbb4e088d8edf318aaee5d3a3dc2d717ed2 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Mon, 25 Jun 2018 09:15:20 +1200 Subject: [PATCH 2/3] fix: changes from the review --- src/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index d3fbb9d..eb6850b 100644 --- a/src/util.js +++ b/src/util.js @@ -71,7 +71,7 @@ const deserialize = (binaryBlob, callback) => { * @returns {void} */ const cid = (dagNode, options, callback) => { - if (options instanceof Function) { + if (typeof options === 'function') { callback = options options = {} } From 081daf5faa4f37ed3eb86cd0efd9a0a4057bc658 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Mon, 25 Jun 2018 09:55:32 +1200 Subject: [PATCH 3/3] test: improve code coverage --- test/util.spec.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/util.spec.js b/test/util.spec.js index 4d0234c..e2d6194 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -161,6 +161,17 @@ describe('IPLD format util API cid()', () => { done) }) }) + + it('should encode the CID correctly and ignore undefined options', (done) => { + IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => { + expect(err).to.not.exist() + verifyCid1( + dagNode, + undefined, + '56203ec2c691d447b2fd0d6a94742345af1f351037dab1ab9e900200000000000000', + done) + }) + }) }) const verifyBlock = (dagNode, expected) => {