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

fix(put): pass CID options to resolver #133

Merged
merged 5 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ const Resolver = new Resolver(<ipfs-block-service instance>)

`options` is an object that must contain one of the following combinations:
- `cid` - the CID of the node
- `hashAlg` and `format` - the hashAlg and the format that should be used to create the CID of the node
- `[hashAlg]`, `[version]` and `format` - the hashAlg, version and the format that should be used to create the CID of the node. The
`hashAlg` and `version` defaults to the default values for the `format`.

`callback` is a function that should have the signature as following: `function (err, cid) {}`, where `err` is an Error object in case of error and `cid` is the cid of the stored object.

Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,36 @@
"homepage": "https://github.com/ipld/js-ipld#readme",
"license": "MIT",
"devDependencies": {
"aegir": "^13.1.0",
"aegir": "^14.0.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"eth-hash-to-cid": "~0.1.1",
"ethereumjs-block": "^1.7.1",
"ethereumjs-block": "^2.0.0",
"lodash": "^4.17.10",
"ncp": "^2.0.0",
"rimraf": "^2.6.2",
"rlp": "^2.0.0"
"rlp": "^2.1.0"
},
"dependencies": {
"async": "^2.6.1",
"cids": "~0.5.3",
"interface-datastore": "~0.4.2",
"ipfs-block": "~0.7.1",
"ipfs-block-service": "~0.14.0",
"ipfs-repo": "~0.22.0",
"ipld-bitcoin": "~0.1.5",
"ipld-dag-cbor": "~0.12.0",
"ipld-dag-pb": "~0.14.4",
"ipld-ethereum": "^2.0.0",
"ipld-git": "~0.2.0",
"ipld-raw": "^2.0.0",
"ipld-zcash": "~0.1.3",
"ipfs-repo": "~0.22.1",
"ipld-bitcoin": "~0.1.6",
"ipld-dag-cbor": "~0.12.1",
"ipld-dag-pb": "~0.14.5",
"ipld-ethereum": "^2.0.1",
"ipld-git": "~0.2.1",
"ipld-raw": "^2.0.1",
"ipld-zcash": "~0.1.4",
"is-ipfs": "~0.3.2",
"lodash.flatten": "^4.4.0",
"lodash.includes": "^4.3.0",
"memdown": "^3.0.0",
"multihashes": "~0.4.13",
"pull-defer": "^0.2.2",
"pull-defer": "~0.2.2",
"pull-sort": "^1.0.1",
"pull-stream": "^3.6.8",
"pull-traverse": "^1.0.3"
Expand Down
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,11 @@ class IPLDResolver {
return this._put(options.cid, node, callback)
}

options.hashAlg = options.hashAlg || 'sha2-256'
const r = this.resolvers[options.format]
if (!r) {
return callback(new Error('No resolver found for codec "' + options.format + '"'))
}
// TODO add support for different hash funcs in the utils of
// each format (just really needed for CBOR for now, really
// r.util.cid(node1, hashAlg, (err, cid) => {
r.util.cid(node, (err, cid) => {
r.util.cid(node, options, (err, cid) => {
if (err) {
return callback(err)
}
Expand Down
26 changes: 26 additions & 0 deletions test/ipld-bitcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ module.exports = (repo) => {
resolver.put(node1, { cid: cid1 }, done)
})

it('resolver.put with format', (done) => {
resolver.put(node1, { format: 'bitcoin-block' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('bitcoin-block')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('dbl-sha2-256')
done()
})
})

it('resolver.put with format + hashAlg', (done) => {
resolver.put(node1, { format: 'bitcoin-block', hashAlg: 'sha3-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('bitcoin-block')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha3-512')
done()
})
})

it('root path (same as get)', (done) => {
resolver.get(cid1, '/', (err, result) => {
expect(err).to.not.exist()
Expand Down
30 changes: 25 additions & 5 deletions test/ipld-dag-cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const dagCBOR = require('ipld-dag-cbor')
const series = require('async/series')
const each = require('async/each')
const pull = require('pull-stream')
const multihash = require('multihashes')

const IPLDResolver = require('../src')

Expand Down Expand Up @@ -107,11 +108,30 @@ module.exports = (repo) => {
resolver.put(node1, { cid: cid1 }, done)
})

it('resolver.put with hashAlg + format', (done) => {
resolver.put(node1, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
}, done)
it('resolver.put with format', (done) => {
resolver.put(node1, { format: 'dag-cbor' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.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('resolver.put with format + hashAlg', (done) => {
resolver.put(node1, { format: 'dag-cbor', hashAlg: 'sha3-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.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('sha3-512')
done()
})
})

it('resolver.get just CID', (done) => {
Expand Down
31 changes: 25 additions & 6 deletions test/ipld-dag-pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const dagPB = require('ipld-dag-pb')
const series = require('async/series')
const each = require('async/each')
const pull = require('pull-stream')

const multihash = require('multihashes')
const IPLDResolver = require('../src')

module.exports = (repo) => {
Expand Down Expand Up @@ -151,11 +151,30 @@ module.exports = (repo) => {
resolver.put(node1, { cid: cid1 }, done)
})

it('resolver.put with hashAlg + format', (done) => {
resolver.put(node1, {
format: 'dag-pb',
hashAlg: 'sha2-256'
}, done)
it('resolver.put with format', (done) => {
resolver.put(node1, { format: 'dag-pb' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(0)
expect(cid.codec).to.equal('dag-pb')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})

it('resolver.put with format + hashAlg', (done) => {
resolver.put(node1, { format: 'dag-pb', hashAlg: 'sha3-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-pb')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha3-512')
done()
})
})

it('resolver.get just CID', (done) => {
Expand Down
27 changes: 27 additions & 0 deletions test/ipld-eth-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ chai.use(dirtyChai)
const BlockService = require('ipfs-block-service')
const ipldEthBlock = require('ipld-ethereum').ethBlock
const EthBlockHeader = require('ethereumjs-block/header')
const multihash = require('multihashes')
const series = require('async/series')
const each = require('async/each')
const pull = require('pull-stream')
Expand Down Expand Up @@ -110,6 +111,32 @@ module.exports = (repo) => {
resolver.put(node1, { cid: cid1 }, done)
})

it('resolver.put with format', (done) => {
resolver.put(node1, { format: 'eth-block' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.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('resolver.put with format + hashAlg', (done) => {
resolver.put(node1, { format: 'eth-block', hashAlg: 'keccak-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.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()
})
})

it('root path (same as get)', (done) => {
resolver.get(cid1, '/', (err, result) => {
expect(err).to.not.exist()
Expand Down
27 changes: 27 additions & 0 deletions test/ipld-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const expect = chai.expect
chai.use(dirtyChai)
const BlockService = require('ipfs-block-service')
const ipldGit = require('ipld-git')
const multihash = require('multihashes')
const series = require('async/series')
const each = require('async/each')
const pull = require('pull-stream')
Expand Down Expand Up @@ -175,6 +176,32 @@ module.exports = (repo) => {
resolver.put(blobNode, { cid: blobCid }, done)
})

it('resolver.put with format', (done) => {
resolver.put(blobNode, { format: 'git-raw' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('git-raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha1')
done()
})
})

it('resolver.put with format + hashAlg', (done) => {
resolver.put(blobNode, { format: 'git-raw', hashAlg: 'sha3-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('git-raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha3-512')
done()
})
})

it('resolver.get root path', (done) => {
resolver.get(blobCid, '/', (err, result) => {
expect(err).to.not.exist()
Expand Down
26 changes: 26 additions & 0 deletions test/ipld-zcash.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ module.exports = (repo) => {
resolver.put(node1, { cid: cid1 }, done)
})

it('resolver.put with format', (done) => {
resolver.put(node1, { format: 'zcash-block' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('zcash-block')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('dbl-sha2-256')
done()
})
})

it('resolver.put with format + hashAlg', (done) => {
resolver.put(node1, { format: 'zcash-block', hashAlg: 'sha3-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('zcash-block')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha3-512')
done()
})
})

it('root path (same as get)', (done) => {
resolver.get(cid1, '/', (err, result) => {
expect(err).to.not.exist()
Expand Down