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 (#67)
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 authored Jun 26, 2018
1 parent 1aed60e commit 1ec7744
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 80 deletions.
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.
*
* @param {Object} dagNode - Internal representation
* @param {Buffer} blob - Serialized binary data
* @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

0 comments on commit 1ec7744

Please sign in to comment.