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

Commit

Permalink
feat: add util.cid options (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored Jun 25, 2018
1 parent 6e28d3e commit eab262f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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()
})
}

0 comments on commit eab262f

Please sign in to comment.