Skip to content

Commit 805aa79

Browse files
committed
Merge pull request ipfs#31 from nginnever/master
Check ipfs path with is-ipfs
2 parents 2648508 + 770b695 commit 805aa79

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"dependencies": {
2626
"ipfs-blocks": "^0.1.0",
27+
"is-ipfs": "^0.2.0",
2728
"multihashing": "^0.2.0",
2829
"protocol-buffers": "^3.1.4",
2930
"stable": "^0.1.5"

src/dag-service.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
var DAGNode = require('./dag-node').DAGNode
2-
var Block = require('ipfs-blocks').Block
1+
const DAGNode = require('./dag-node').DAGNode
2+
const Block = require('ipfs-blocks').Block
3+
const isIPFS = require('is-ipfs')
4+
const base58 = require('bs58')
35

46
exports = module.exports = DAGService
57

@@ -23,10 +25,27 @@ function DAGService (blockService) {
2325
// this.addRecursive
2426

2527
// get retrieves a DAGNode, using the Block Service
26-
this.get = (multihash, callback) => {
27-
if (!multihash) { return callback(new Error('Invalid Key')) }
28+
this.get = function (multihash, callback) {
29+
const isMhash = isIPFS.multihash(multihash)
30+
const isPath = isIPFS.path(multihash)
31+
32+
if (!isMhash && !isPath) {
33+
return callback(new Error('Invalid Key'))
34+
}
35+
36+
if (isMhash) {
37+
this.getWith(multihash, callback)
38+
}
39+
40+
if (isPath) {
41+
var ipfsKey = multihash.replace('/ipfs/', '')
42+
this.getWith(ipfsKey, callback)
43+
}
44+
}
2845

29-
this.bs.getBlock(multihash, (err, block) => {
46+
this.getWith = function (key, callback) {
47+
const formatted = typeof key === 'string' ? new Buffer(base58.decode(key)) : key
48+
this.bs.getBlock(formatted, (err, block) => {
3049
if (err) { return callback(err) }
3150
var node = new DAGNode()
3251
node.unMarshal(block.data)

tests/merkle-dag-tests.js

+55-10
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,62 @@ module.exports = function (repo) {
154154
})
155155
})
156156

157-
it('get a mdag node', (done) => {
158-
const node = new DAGNode(new Buffer('more data data data'))
159-
dagService.add(node, (err) => {
157+
it('get a mdag node from base58 encoded string', (done) => {
158+
var encodedMh = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
159+
dagService.get(encodedMh, (err, fetchedNode) => {
160160
expect(err).to.not.exist
161-
var mh = node.multihash()
162-
dagService.get(mh, (err, fetchedNode) => {
163-
expect(err).to.not.exist
164-
expect(node.data).to.deep.equal(fetchedNode.data)
165-
expect(node.links).to.deep.equal(fetchedNode.links)
166-
done()
167-
})
161+
expect(fetchedNode.data).to.deep.equal(new Buffer(bs58.decode('cL')))
162+
// just picking the second link and comparing mhash buffer to expected
163+
expect(fetchedNode.links[1].hash).to.deep.equal(new Buffer(bs58.decode('QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y')))
164+
done()
165+
})
166+
})
167+
168+
it('get a mdag node from a multihash buffer', (done) => {
169+
var mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'))
170+
dagService.get(mh, (err, fetchedNode) => {
171+
expect(err).to.not.exist
172+
expect(fetchedNode.data).to.deep.equal(new Buffer(bs58.decode('cL')))
173+
expect(fetchedNode.links[1].hash).to.deep.equal(new Buffer(bs58.decode('QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y')))
174+
done()
175+
})
176+
})
177+
178+
it('get a mdag node from a /ipfs/ path', (done) => {
179+
var ipfsPath = '/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
180+
dagService.get(ipfsPath, (err, fetchedNode) => {
181+
expect(err).to.not.exist
182+
expect(fetchedNode.data).to.deep.equal(new Buffer(bs58.decode('cL')))
183+
expect(fetchedNode.links[1].hash).to.deep.equal(new Buffer(bs58.decode('QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y')))
184+
done()
185+
})
186+
})
187+
188+
it('supply an improperly formatted string path', (done) => {
189+
var mh = 'bad path'
190+
var ipfsPath = '/ipfs/' + mh
191+
dagService.get(ipfsPath, (err, fetchedNode) => {
192+
var error = 'Error: Invalid Key'
193+
expect(err.toString()).to.equal(error)
194+
done()
195+
})
196+
})
197+
198+
it('supply improperly formatted multihash buffer', (done) => {
199+
var mh = new Buffer('more data data data')
200+
dagService.get(mh, (err, fetchedNode) => {
201+
var error = 'Error: Invalid Key'
202+
expect(err.toString()).to.equal(error)
203+
done()
204+
})
205+
})
206+
207+
it('supply something weird', (done) => {
208+
var mh = 3
209+
dagService.get(mh, (err, fetchedNode) => {
210+
var error = 'Error: Invalid Key'
211+
expect(err.toString()).to.equal(error)
212+
done()
168213
})
169214
})
170215

0 commit comments

Comments
 (0)