From 0995da1ca0503e171e51fc9ee180947cee65caa5 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sun, 24 Jun 2018 22:56:06 +1200 Subject: [PATCH] feat: add util.cid options See ipld/interface-ipld-format#40 --- package.json | 6 ++++-- src/index.js | 15 +++++++++++++-- test/index.spec.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index d1655af..623427d 100644 --- a/package.json +++ b/package.json @@ -38,11 +38,13 @@ }, "homepage": "https://github.com/ipld/js-ipld-raw#readme", "dependencies": { - "cids": "~0.5.2" + "cids": "~0.5.2", + "multihashing-async": "~0.5.1" }, "devDependencies": { "aegir": "^12.4.0", "chai": "^4.1.2", - "dirty-chai": "^2.0.1" + "dirty-chai": "^2.0.1", + "multihashes": "~0.4.12" } } diff --git a/src/index.js b/src/index.js index b6a4914..3e98bd1 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ 'use strict' const CID = require('cids') +const multihash = require('multihashing-async') // binary resolver module.exports = { @@ -26,8 +27,18 @@ module.exports = { serialize: (data, cb) => { cb(null, data) }, - cid: (data, cb) => { - cb(null, new CID(1, 'raw', data)) + cid: (data, options, cb) => { + if (options instanceof Function) { + cb = options + options = {} + } + options = options || {} + const hashAlg = options.hashAlg || 'sha2-256' + const version = options.version || 1 + multihash(data, hashAlg, (err, mh) => { + if (err) return cb(err) + cb(null, new CID(version, 'raw', mh)) + }) } } } diff --git a/test/index.spec.js b/test/index.spec.js index b56157c..39be805 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -7,6 +7,7 @@ chai.use(dirtyChai) const ipldRaw = require('../src/index') const resolver = ipldRaw.resolver +const multihash = require('multihashes') describe('raw codec', () => { let testData = Buffer.from('test data') @@ -52,3 +53,47 @@ describe('raw codec', () => { }) }) }) + +describe('raw util', () => { + let rawData = Buffer.from('some raw data') + + it('serialize is noop', (done) => { + ipldRaw.util.serialize(rawData, (err, result) => { + expect(err).to.not.exist() + expect(result).to.equal(rawData) + done() + }) + }) + + it('deserialize is noop', (done) => { + ipldRaw.util.deserialize(rawData, (err, result) => { + expect(err).to.not.exist() + expect(result).to.equal(rawData) + done() + }) + }) + + it('create cid', (done) => { + ipldRaw.util.cid(rawData, (err, cid) => { + expect(err).to.not.exist() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('raw') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha2-256') + done() + }) + }) + + it('create cid with hashAlg', (done) => { + ipldRaw.util.cid(rawData, { hashAlg: 'sha2-512' }, (err, cid) => { + expect(err).to.not.exist() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('raw') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha2-512') + done() + }) + }) +})