From b2f3647b0b1892512f7af80097bb9f197c2299e0 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Mon, 25 Jun 2018 21:36:10 +1200 Subject: [PATCH 1/2] fix: pass serialized blob to util.cid BREAKING CHANGE: the first argument is now the serialized output NOT the dag node. See https://github.com/ipld/interface-ipld-format/issues/32 --- src/util.js | 9 +++++---- test/interop.spec.js | 10 +++++----- test/util.spec.js | 44 ++++++++++++++++++++------------------------ 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/util.js b/src/util.js index f039dd3..867751c 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 - Output from serialize() * @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..6c6939c 100644 --- a/test/interop.spec.js +++ b/test/interop.spec.js @@ -49,7 +49,7 @@ describe('dag-cbor interop tests', () => { // put it back to bytes node[0]['/'] = bs58.decode(arrayLinkJSON[0]['/']) - dagCBOR.util.cid(node, (err, cid) => { + dagCBOR.util.cid(arrayLinkCBOR, (err, cid) => { expect(err).to.not.exist() const cidStr = cid.toBaseEncodedString() expect(cidStr).to.eql(expectedCIDs['array-link']['/']) @@ -63,7 +63,7 @@ describe('dag-cbor interop tests', () => { expect(err).to.not.exist() expect(node).to.eql(emptyArrayJSON) - dagCBOR.util.cid(node, (err, cid) => { + dagCBOR.util.cid(emptyArrayCBOR, (err, cid) => { expect(err).to.not.exist() const cidStr = cid.toBaseEncodedString() expect(cidStr).to.eql(expectedCIDs['empty-array']['/']) @@ -77,7 +77,7 @@ describe('dag-cbor interop tests', () => { expect(err).to.not.exist() expect(node).to.eql(emptyObjJSON) - dagCBOR.util.cid(node, (err, cid) => { + dagCBOR.util.cid(emptyObjCBOR, (err, cid) => { expect(err).to.not.exist() const cidStr = cid.toBaseEncodedString() expect(cidStr).to.eql(expectedCIDs['empty-obj']['/']) @@ -105,7 +105,7 @@ describe('dag-cbor interop tests', () => { expect(err).to.not.exist() expect(node).to.eql(objNoLinkJSON) - dagCBOR.util.cid(node, (err, cid) => { + dagCBOR.util.cid(objNoLinkCBOR, (err, cid) => { expect(err).to.not.exist() const cidStr = cid.toBaseEncodedString() expect(cidStr).to.eql(expectedCIDs['obj-no-link']['/']) @@ -120,7 +120,7 @@ describe('dag-cbor interop tests', () => { dagCBOR.util.deserialize(objWithLinkCBOR, (err, node) => { expect(err).to.not.exist() - dagCBOR.util.cid(node, (err, cid) => { + dagCBOR.util.cid(objWithLinkCBOR, (err, cid) => { expect(err).to.not.exist() const cidStr = cid.toBaseEncodedString() expect(cidStr).to.eql(expectedCIDs['obj-with-link']['/']) 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() + }) }) }) From 8d4f0eb94b804e9f430db8548d9b7d22f037fa3a Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 26 Jun 2018 11:34:36 +1200 Subject: [PATCH 2/2] fix: review changes --- src/util.js | 2 +- test/interop.spec.js | 73 +++++++++++++------------------------------- 2 files changed, 22 insertions(+), 53 deletions(-) diff --git a/src/util.js b/src/util.js index 867751c..6b33635 100644 --- a/src/util.js +++ b/src/util.js @@ -117,7 +117,7 @@ exports.deserialize = (data, callback) => { /** * Get the CID of the DAG-Node. * - * @param {Buffer} blob - Output from serialize() + * @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 diff --git a/test/interop.spec.js b/test/interop.spec.js index 6c6939c..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(arrayLinkCBOR, (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(emptyArrayCBOR, (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(emptyObjCBOR, (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(objNoLinkCBOR, (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(objWithLinkCBOR, (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() }) }) })