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

Commit

Permalink
fix: pass serialized blob to util.cid (#75)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the first argument is now the serialized output NOT the dag node.
See ipld/interface-ipld-format#32
  • Loading branch information
richardschneider committed Jun 26, 2018
1 parent 1d89fa7 commit 2ae9542
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
11 changes: 6 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const assert = require('assert')
const CID = require('cids')
const protons = require('protons')
const proto = protons(require('./dag.proto.js'))
Expand All @@ -17,16 +18,17 @@ exports = module.exports
* @param {?CID} cid - CID if call was successful
*/
/**
* Get the CID of the DAG-Node.
* Get the CID of the serialized ProtoBuf node.
*
* @param {Object} dagNode - Internal representation
* @param {Buffer} blob - Serialized ProtoBuf node
* @param {Object} [options] - Options to create the CID
* @param {number} [options.version] - CID version number. Defaults to zero if hashAlg == 'sha2-256'; otherwise, 1.
* @param {string} [options.hashAlg] - Defaults to hashAlg for the resolver
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
function cid (dagNode, options, callback) {
function cid (blob, options, callback) {
assert(Buffer.isBuffer(blob), 'blob must be a Buffer')
if (typeof options === 'function') {
callback = options
options = {}
Expand All @@ -38,8 +40,7 @@ function cid (dagNode, options, callback) {
version = hashAlg === 'sha2-256' ? 0 : 1
}
waterfall([
(cb) => serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, hashAlg, cb),
(cb) => multihashing(blob, hashAlg, cb),
(mh, cb) => cb(null, new CID(version, resolver.multicodec, mh))
], callback)
}
Expand Down
34 changes: 20 additions & 14 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,29 +424,35 @@ module.exports = (repo) => {
it('get node CID', (done) => {
DAGNode.create(Buffer.from('some data'), (err, node) => {
expect(err).to.not.exist()
util.cid(node, (err, cid) => {
util.serialize(node, (err, serialized) => {
expect(err).to.not.exist()
expect(cid.multihash).to.exist()
expect(cid.codec).to.equal('dag-pb')
expect(cid.version).to.equal(0)
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
util.cid(serialized, (err, cid) => {
expect(err).to.not.exist()
expect(cid.multihash).to.exist()
expect(cid.codec).to.equal('dag-pb')
expect(cid.version).to.equal(0)
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})
})
})

it('get node CID with hashAlg', (done) => {
DAGNode.create(Buffer.from('some data'), (err, node) => {
expect(err).to.not.exist()
util.cid(node, { hashAlg: 'sha2-512' }, (err, cid) => {
util.serialize(node, (err, serialized) => {
expect(err).to.not.exist()
expect(cid.multihash).to.exist()
expect(cid.codec).to.equal('dag-pb')
expect(cid.version).to.equal(1)
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
util.cid(serialized, { hashAlg: 'sha2-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.multihash).to.exist()
expect(cid.codec).to.equal('dag-pb')
expect(cid.version).to.equal(1)
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})
})
})
Expand Down

0 comments on commit 2ae9542

Please sign in to comment.