Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Add hash algorithm option for cid #62

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ exports.deserialize = (data, callback) => {
setImmediate(() => callback(null, deserialized))
}

exports.cid = (dagNode, callback) => {
exports.cid = (dagNode, hashAlg, callback) => {
waterfall([
(cb) => exports.serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, 'sha2-256', cb),
(serialized, cb) => multihashing(serialized, hashAlg, cb),
(mh, cb) => cb(null, new CID(1, resolver.multicodec, mh))
], callback)
}
12 changes: 6 additions & 6 deletions test/interop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('dag-cbor interop tests', () => {
// put it back to bytes
node[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])

dagCBOR.util.cid(node, (err, cid) => {
dagCBOR.util.cid(node, 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['array-link']['/'])
Expand All @@ -63,7 +63,7 @@ describe('dag-cbor interop tests', () => {
expect(err).to.not.exist()
expect(node).to.eql(emptyArrayJSON)

dagCBOR.util.cid(node, (err, cid) => {
dagCBOR.util.cid(node, 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-array']['/'])
Expand All @@ -77,7 +77,7 @@ describe('dag-cbor interop tests', () => {
expect(err).to.not.exist()
expect(node).to.eql(emptyObjJSON)

dagCBOR.util.cid(node, (err, cid) => {
dagCBOR.util.cid(node, 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-obj']['/'])
Expand All @@ -91,7 +91,7 @@ describe('dag-cbor interop tests', () => {
expect(err).to.not.exist()
expect(node).to.eql(fooJSON)

dagCBOR.util.cid(node, (err, cid) => {
dagCBOR.util.cid(node, 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['foo']['/'])
Expand All @@ -105,7 +105,7 @@ describe('dag-cbor interop tests', () => {
expect(err).to.not.exist()
expect(node).to.eql(objNoLinkJSON)

dagCBOR.util.cid(node, (err, cid) => {
dagCBOR.util.cid(node, 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-no-link']['/'])
Expand All @@ -120,7 +120,7 @@ describe('dag-cbor interop tests', () => {
dagCBOR.util.deserialize(objWithLinkCBOR, (err, node) => {
expect(err).to.not.exist()

dagCBOR.util.cid(node, (err, cid) => {
dagCBOR.util.cid(node, 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-with-link']['/'])
Expand Down
11 changes: 6 additions & 5 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const multihash = require('multihashes')
const expect = chai.expect
chai.use(dirtyChai)
const garbage = require('garbage')
Expand Down Expand Up @@ -53,21 +54,21 @@ describe('util', () => {
})

it('.cid', (done) => {
dagCBOR.util.cid(obj, (err, cid) => {
dagCBOR.util.cid(obj, 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
expect(multihash.decode(cid.multihash).name).to.equal('sha2-256')
done()
})
})

it('strings', (done) => {
dagCBOR.util.cid('some test string', (err, cid) => {
it.only('strings', (done) => {
dagCBOR.util.cid('some test string', 'sha2-256', (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
expect(multihash.decode(cid.multihash).name).to.equal('sha2-256')
done()
})
})
Expand Down