diff --git a/src/util.js b/src/util.js index f039dd3..6b33635 100644 --- a/src/util.js +++ b/src/util.js @@ -1,5 +1,6 @@ 'use strict' +const assert = require('assert') const cbor = require('borc') const multihashing = require('multihashing-async') const CID = require('cids') @@ -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 = {} @@ -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) } diff --git a/test/interop.spec.js b/test/interop.spec.js index 030775e..bef8abf 100644 --- a/test/interop.spec.js +++ b/test/interop.spec.js @@ -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() }) }) @@ -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() }) }) }) diff --git a/test/util.spec.js b/test/util.spec.js index 3e01c45..bff3dfb 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -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() + }) }) })