Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: implement CID options (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored Jul 13, 2018
1 parent 156e8cf commit da25e17
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 12 deletions.
14 changes: 11 additions & 3 deletions eth-block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ util.deserialize = asyncify((serialized) => {
const rawOmmers = RLP.decode(serialized)
return rawOmmers.map((rawBlock) => new EthBlockHead(rawBlock))
})
util.cid = (blockList, callback) => {
util.cid = (blockList, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
const hashAlg = options.hashAlg || 'keccak-256'
const version = typeof options.version === 'undefined' ? 1 : options.version

waterfall([
(cb) => util.serialize(blockList, cb),
(data, cb) => multihash.digest(data, 'keccak-256', cb),
asyncify((mhash) => cidFromHash('eth-block-list', mhash))
(data, cb) => multihash.digest(data, hashAlg, cb),
asyncify((mhash) => cidFromHash('eth-block-list', mhash, options))
], callback)
}

Expand Down
37 changes: 37 additions & 0 deletions eth-block-list/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,42 @@ describe('IPLD format resolver (local)', () => {
done()
})
})

it('should create CID, no options', (done) => {
dagEthBlockList.util.cid(ethBlock.uncleHeaders, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('eth-block-list')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('keccak-256')
done()
})
})

it('should create CID, empty options', (done) => {
dagEthBlockList.util.cid(ethBlock.uncleHeaders, {}, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('eth-block-list')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('keccak-256')
done()
})
})

it('should create CID, hashAlg', (done) => {
dagEthBlockList.util.cid(ethBlock.uncleHeaders, { hashAlg: 'keccak-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('eth-block-list')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('keccak-512')
done()
})
})

})
})
43 changes: 41 additions & 2 deletions eth-block/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const expect = chai.expect
const CID = require('cids')
const EthBlockHeader = require('ethereumjs-block/header')
const multihashing = require('multihashing-async')
const multihash = require('multihashes')
const waterfall = require('async/waterfall')
const asyncify = require('async/asyncify')

Expand All @@ -16,6 +17,7 @@ const resolver = ipldEthBlock.resolver

describe('IPLD format resolver (local)', () => {
let testBlob
let testEthBlock
let testData = {
// 12345678901234567890123456789012
parentHash: new Buffer('0100000000000000000000000000000000000000000000000000000000000000', 'hex'),
Expand All @@ -36,7 +38,7 @@ describe('IPLD format resolver (local)', () => {
}

before((done) => {
const testEthBlock = new EthBlockHeader(testData)
testEthBlock = new EthBlockHeader(testData)

waterfall([
(cb) => ipldEthBlock.util.serialize(testEthBlock, cb),
Expand All @@ -59,7 +61,6 @@ describe('IPLD format resolver (local)', () => {
})

it('can parse the cid', (done) => {
const testEthBlock = new EthBlockHeader(testData)
ipldEthBlock.util.cid(testEthBlock, (err, cid) => {
expect(err).not.to.exist()
let encodedCid = cid.toBaseEncodedString()
Expand Down Expand Up @@ -90,6 +91,44 @@ describe('IPLD format resolver (local)', () => {
})
})
})

describe('util', () => {
it('should create CID, no options', (done) => {
ipldEthBlock.util.cid(testEthBlock, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('eth-block')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('keccak-256')
done()
})
})

it('should create CID, empty options', (done) => {
ipldEthBlock.util.cid(testEthBlock, {}, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('eth-block')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('keccak-256')
done()
})
})

it('should create CID, hashAlg', (done) => {
ipldEthBlock.util.cid(testEthBlock, { hashAlg: 'keccak-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('eth-block')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('keccak-512')
done()
})
})
})
})

describe('manual ancestor walking', () => {
Expand Down
13 changes: 13 additions & 0 deletions eth-tx/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,18 @@ describe('IPLD format resolver (local)', () => {
done()
})
})

it('create CID, hashAlg', (done) => {
const testTx = new Transaction(testData)
util.cid(testTx, { hashAlg: 'keccak-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('eth-tx')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('keccak-512')
done()
})
})
})
})
4 changes: 2 additions & 2 deletions util/cidFromEthObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const cidFromHash = require('./cidFromHash')

module.exports = cidFromEthObj

function cidFromEthObj (multicodec, ethObj) {
function cidFromEthObj (multicodec, ethObj, options) {
const hashBuffer = ethObj.hash()
const cid = cidFromHash(multicodec, hashBuffer)
const cid = cidFromHash(multicodec, hashBuffer, options)
return cid
}
10 changes: 6 additions & 4 deletions util/cidFromHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const multihashes = require('multihashes')

module.exports = cidFromHash

function cidFromHash (codec, rawhash) {
const multihash = multihashes.encode(rawhash, 'keccak-256')
const cidVersion = 1
return new CID(cidVersion, codec, multihash)
function cidFromHash (codec, rawhash, options) {
options = options || {}
const hashAlg = options.hashAlg || 'keccak-256'
const version = typeof options.version === 'undefined' ? 1 : options.version
const multihash = multihashes.encode(rawhash, hashAlg)
return new CID(version, codec, multihash)
}
2 changes: 1 addition & 1 deletion util/createUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ function createUtil (multicodec, EthObjClass) {
return {
deserialize: asyncify((serialized) => new EthObjClass(serialized)),
serialize: asyncify((ethObj) => ethObj.serialize()),
cid: asyncify((ethObj, options) => cidFromEthObj(multicodec, ethObj))
cid: asyncify((ethObj, options) => cidFromEthObj(multicodec, ethObj, options))
}
}

0 comments on commit da25e17

Please sign in to comment.