Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
fix: do not ignore cid.options
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jun 27, 2018
1 parent 77354ac commit 8553b4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ 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 {Object} [options] - Options to create the CID
* @param {number} [options.version=1] - CID version number
* @param {string} [options.hashAlg=dbl-sha2-256] - Hashing algorithm
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
Expand All @@ -77,15 +79,18 @@ const cid = (dagNode, options, callback) => {
options = {}
}
options = options || {}
// avoid deadly embrace between resolver and util
const hashAlg = options.hashAlg || require('./resolver').defaultHashAlg
const version = typeof options.version === 'undefined' ? 1 : options.version
waterfall([
(cb) => {
try {
multihashing(dagNode.toBuffer(true), 'dbl-sha2-256', cb)
multihashing(dagNode.toBuffer(true), hashAlg, cb)
} catch (err) {
cb(err)
}
},
(mh, cb) => cb(null, new CID(1, 'bitcoin-block', mh))
(mh, cb) => cb(null, new CID(version, 'bitcoin-block', mh))
], callback)
}

Expand Down
19 changes: 15 additions & 4 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,18 @@ describe('IPLD format util API cid()', () => {
})
})

it('should encode the CID correctly and ignore all options', (done) => {
it('should encode the CID correctly with option', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
verifyCid1(
dagNode,
{ hashAlg: 'unknown' },
'56203ec2c691d447b2fd0d6a94742345af1f351037dab1ab9e900200000000000000',
{ hashAlg: 'sha3-256' },
'16208fd2802e0304c79c08a1ff2afb706ce64b78f3b94fd1c9142946c2e715589cfb',
done)
})
})

it('should encode the CID correctly and ignore undefined options', (done) => {
it('should encode the CID correctly with undefined options', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
verifyCid1(
Expand All @@ -172,6 +172,17 @@ describe('IPLD format util API cid()', () => {
done)
})
})

it('should encode the CID correctly with default options specified', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
verifyCid1(
dagNode,
{ version: 1, hashAlg: 'dbl-sha2-256' },
'56203ec2c691d447b2fd0d6a94742345af1f351037dab1ab9e900200000000000000',
done)
})
})
})

const verifyBlock = (dagNode, expected) => {
Expand Down

0 comments on commit 8553b4e

Please sign in to comment.