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

Commit 2ae9542

Browse files
fix: pass serialized blob to util.cid (#75)
BREAKING CHANGE: the first argument is now the serialized output NOT the dag node. See ipld/interface-ipld-format#32
1 parent 1d89fa7 commit 2ae9542

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/util.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const assert = require('assert')
34
const CID = require('cids')
45
const protons = require('protons')
56
const proto = protons(require('./dag.proto.js'))
@@ -17,16 +18,17 @@ exports = module.exports
1718
* @param {?CID} cid - CID if call was successful
1819
*/
1920
/**
20-
* Get the CID of the DAG-Node.
21+
* Get the CID of the serialized ProtoBuf node.
2122
*
22-
* @param {Object} dagNode - Internal representation
23+
* @param {Buffer} blob - Serialized ProtoBuf node
2324
* @param {Object} [options] - Options to create the CID
2425
* @param {number} [options.version] - CID version number. Defaults to zero if hashAlg == 'sha2-256'; otherwise, 1.
2526
* @param {string} [options.hashAlg] - Defaults to hashAlg for the resolver
2627
* @param {CidCallback} callback - Callback that handles the return value
2728
* @returns {void}
2829
*/
29-
function cid (dagNode, options, callback) {
30+
function cid (blob, options, callback) {
31+
assert(Buffer.isBuffer(blob), 'blob must be a Buffer')
3032
if (typeof options === 'function') {
3133
callback = options
3234
options = {}
@@ -38,8 +40,7 @@ function cid (dagNode, options, callback) {
3840
version = hashAlg === 'sha2-256' ? 0 : 1
3941
}
4042
waterfall([
41-
(cb) => serialize(dagNode, cb),
42-
(serialized, cb) => multihashing(serialized, hashAlg, cb),
43+
(cb) => multihashing(blob, hashAlg, cb),
4344
(mh, cb) => cb(null, new CID(version, resolver.multicodec, mh))
4445
], callback)
4546
}

test/dag-node-test.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -424,29 +424,35 @@ module.exports = (repo) => {
424424
it('get node CID', (done) => {
425425
DAGNode.create(Buffer.from('some data'), (err, node) => {
426426
expect(err).to.not.exist()
427-
util.cid(node, (err, cid) => {
427+
util.serialize(node, (err, serialized) => {
428428
expect(err).to.not.exist()
429-
expect(cid.multihash).to.exist()
430-
expect(cid.codec).to.equal('dag-pb')
431-
expect(cid.version).to.equal(0)
432-
const mh = multihash.decode(cid.multihash)
433-
expect(mh.name).to.equal('sha2-256')
434-
done()
429+
util.cid(serialized, (err, cid) => {
430+
expect(err).to.not.exist()
431+
expect(cid.multihash).to.exist()
432+
expect(cid.codec).to.equal('dag-pb')
433+
expect(cid.version).to.equal(0)
434+
const mh = multihash.decode(cid.multihash)
435+
expect(mh.name).to.equal('sha2-256')
436+
done()
437+
})
435438
})
436439
})
437440
})
438441

439442
it('get node CID with hashAlg', (done) => {
440443
DAGNode.create(Buffer.from('some data'), (err, node) => {
441444
expect(err).to.not.exist()
442-
util.cid(node, { hashAlg: 'sha2-512' }, (err, cid) => {
445+
util.serialize(node, (err, serialized) => {
443446
expect(err).to.not.exist()
444-
expect(cid.multihash).to.exist()
445-
expect(cid.codec).to.equal('dag-pb')
446-
expect(cid.version).to.equal(1)
447-
const mh = multihash.decode(cid.multihash)
448-
expect(mh.name).to.equal('sha2-512')
449-
done()
447+
util.cid(serialized, { hashAlg: 'sha2-512' }, (err, cid) => {
448+
expect(err).to.not.exist()
449+
expect(cid.multihash).to.exist()
450+
expect(cid.codec).to.equal('dag-pb')
451+
expect(cid.version).to.equal(1)
452+
const mh = multihash.decode(cid.multihash)
453+
expect(mh.name).to.equal('sha2-512')
454+
done()
455+
})
450456
})
451457
})
452458
})

0 commit comments

Comments
 (0)