diff --git a/src/util.js b/src/util.js index d536357..eb6850b 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 (typeof options === '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..e2d6194 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -150,6 +150,28 @@ 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) + }) + }) + + 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) => { @@ -168,3 +190,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() + }) +}