diff --git a/package.json b/package.json index 38efd00..cbb8e01 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,8 @@ "homepage": "https://github.com/ipfs/js-ipfs-block#readme", "devDependencies": { "aegir": "^8.0.0", - "chai": "^3.5.0" + "chai": "^3.5.0", + "multihashes": "^0.2.2" }, "dependencies": { "multihashing": "^0.2.0" @@ -47,4 +48,4 @@ "David Dias ", "dignifiedquire " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index c3ce0f6..cbb6035 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,8 @@ 'use strict' -const util = require('./util') -// Immutable block of data -function Block (data, type) { +const multihashing = require('multihashing') + +function Block (data) { if (!data) { throw new Error('Block must be constructed with data') } @@ -17,21 +17,13 @@ function Block (data, type) { this.data = new Buffer(data) } - this.key = util.hash(this.data) - this.type = type || 'protobuf' -} - -Object.defineProperty(Block.prototype, 'extension', { - get () { - switch (this.type) { - case 'protobuf': - return 'data' - case 'ipld': - return 'ipld' - default: - return this.type + this.key = (hashFunc) => { + if (!hashFunc) { + hashFunc = 'sha2-256' } + + return multihashing(this.data, hashFunc) } -}) +} module.exports = Block diff --git a/src/util.js b/src/util.js deleted file mode 100644 index c1c7c74..0000000 --- a/src/util.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict' - -const multihashing = require('multihashing') - -exports = module.exports - -// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits -exports.hash = (data) => multihashing(data, 'sha2-256') diff --git a/test/block.spec.js b/test/block.spec.js index f474e1c..50c548d 100644 --- a/test/block.spec.js +++ b/test/block.spec.js @@ -3,20 +3,21 @@ const expect = require('chai').expect const Block = require('../src') +const multihash = require('multihashes') describe('block', () => { it('create', () => { const b = new Block('random-data') - expect(b.key).to.exist + expect(multihash.toB58String(b.key())).to.equal('QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG') + expect(multihash.toB58String(b.key('sha1'))).to.equal('5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY') expect(b.data).to.exist - expect(b.extension).to.be.eql('data') }) it('create /wo new', () => { const b = Block('random-data') - expect(b.key).to.exist + expect(multihash.toB58String(b.key())).to.equal('QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG') + expect(multihash.toB58String(b.key('sha1'))).to.equal('5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY') expect(b.data).to.exist - expect(b.extension).to.be.eql('data') }) it('fail to create an empty block', () => { @@ -33,28 +34,9 @@ describe('block', () => { // it from the original implementation // It doesn't stricly verify the immutability of the Block object const block = new Block("Can't change this!") - let key = block.key + let key = block.key() key = new Buffer('new key') - expect(key.equals(block.key)).to.equal(false) - }) - - it('has the right extension to type mapping', () => { - const b1 = new Block('hello', 'protobuf') - const b2 = new Block('hello') - const b3 = new Block('hello', 'ipld') - const b4 = new Block('hello', 'woot') - - expect(b1.type).to.be.eql('protobuf') - expect(b1.extension).to.be.eql('data') - - expect(b2.type).to.be.eql('protobuf') - expect(b2.extension).to.be.eql('data') - - expect(b3.type).to.be.eql('ipld') - expect(b3.extension).to.be.eql('ipld') - - expect(b4.type).to.be.eql('woot') - expect(b4.extension).to.be.eql('woot') + expect(key.equals(block.key())).to.equal(false) }) })