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

fix: pass serialized blob to util.cid #67

Merged
merged 2 commits into from
Jun 26, 2018
Merged
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
9 changes: 5 additions & 4 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 cbor = require('borc')
const multihashing = require('multihashing-async')
const CID = require('cids')
Expand Down Expand Up @@ -116,14 +117,15 @@ exports.deserialize = (data, callback) => {
/**
* Get the CID of the DAG-Node.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it should say Get the CID of the CBOR node?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about Get the CID of the serialised CBOR node?

*
* @param {Object} dagNode - Internal representation
* @param {Buffer} blob - Serialized binary data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Github UI isn't good with changes. So I repost it (slightly different though :) I would say something like Encoded CBOR blob or Serialized as CBOR.

* @param {Object} [options] - Options to create the CID
* @param {number} [options.version=1] - CID version number
* @param {string} [options.hashAlg] - Defaults to hashAlg for the resolver
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
exports.cid = (dagNode, options, callback) => {
exports.cid = (blob, options, callback) => {
assert(Buffer.isBuffer(blob), 'blob must be a Buffer')
if (typeof options === 'function') {
callback = options
options = {}
Expand All @@ -132,8 +134,7 @@ exports.cid = (dagNode, options, callback) => {
const hashAlg = options.hashAlg || resolver.defaultHashAlg
const version = typeof options.version === 'undefined' ? 1 : options.version
waterfall([
(cb) => exports.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)
}
73 changes: 21 additions & 52 deletions test/interop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,53 +36,31 @@ describe('dag-cbor interop tests', () => {
// the fixtures feature needs to be fixed
if (!isNode) { return }

describe('deserialize and compare', () => {
describe('CID creation', () => {
it('array-link', (done) => {
dagCBOR.util.deserialize(arrayLinkCBOR, (err, node) => {
dagCBOR.util.cid(arrayLinkCBOR, (err, cid) => {
expect(err).to.not.exist()
// the JSON version that gets out of go-ipfs stringifies the CID
const bs58Str = bs58.encode(node[0]['/'])

node[0]['/'] = bs58Str
expect(node).to.eql(arrayLinkJSON)

// put it back to bytes
node[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['array-link']['/'])
done()
})
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['array-link']['/'])
done()
})
})

it('empty-array', (done) => {
dagCBOR.util.deserialize(emptyArrayCBOR, (err, node) => {
dagCBOR.util.cid(emptyArrayCBOR, (err, cid) => {
expect(err).to.not.exist()
expect(node).to.eql(emptyArrayJSON)

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-array']['/'])
done()
})
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-array']['/'])
done()
})
})

it('empty-obj', (done) => {
dagCBOR.util.deserialize(emptyObjCBOR, (err, node) => {
dagCBOR.util.cid(emptyObjCBOR, (err, cid) => {
expect(err).to.not.exist()
expect(node).to.eql(emptyObjJSON)

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-obj']['/'])
done()
})
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-obj']['/'])
done()
})
})

Expand All @@ -101,31 +79,22 @@ describe('dag-cbor interop tests', () => {
})

it('obj-no-link', (done) => {
dagCBOR.util.deserialize(objNoLinkCBOR, (err, node) => {
dagCBOR.util.cid(objNoLinkCBOR, (err, cid) => {
expect(err).to.not.exist()
expect(node).to.eql(objNoLinkJSON)

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-no-link']['/'])
done()
})
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-no-link']['/'])
done()
})
})

it('obj-with-link', (done) => {
if (!isNode) { done() }

dagCBOR.util.deserialize(objWithLinkCBOR, (err, node) => {
dagCBOR.util.cid(objWithLinkCBOR, (err, cid) => {
expect(err).to.not.exist()

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-with-link']['/'])
done()
})
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-with-link']['/'])
done()
})
})
})
Expand Down
44 changes: 20 additions & 24 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,32 @@ describe('util', () => {
})

it('.cid', (done) => {
dagCBOR.util.cid(obj, (err, cid) => {
dagCBOR.util.serialize(obj, (err, serialized) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
dagCBOR.util.cid(serialized, (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()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})
})

it('.cid with hashAlg', (done) => {
dagCBOR.util.cid(obj, { hashAlg: 'sha2-512' }, (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()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})

it('strings', (done) => {
dagCBOR.util.cid('some test string', (err, cid) => {
dagCBOR.util.serialize(obj, (err, serialized) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
done()
dagCBOR.util.cid(serialized, { hashAlg: 'sha2-512' }, (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()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})
})

Expand Down