Skip to content

Commit

Permalink
added checks for correct multihash key
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed Mar 22, 2016
1 parent f7cfd7f commit b177708
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"url": "https://github.com/vijayee/js-ipfs-merkle-dag.git"
},
"dependencies": {
"bl": "^1.0.0",
"ipfs-blocks": "^0.1.0",
"is-ipfs": "^0.1.0",
"multihashing": "^0.2.0",
"protocol-buffers": "^3.1.4",
"stable": "^0.1.5"
Expand Down
32 changes: 27 additions & 5 deletions src/dag-service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var DAGNode = require('./dag-node').DAGNode
var Block = require('ipfs-blocks').Block
const DAGNode = require('./dag-node').DAGNode
const Block = require('ipfs-blocks').Block
const isIPFS = require('is-ipfs')
const base58 = require('bs58')

exports = module.exports = DAGService

Expand All @@ -23,10 +25,30 @@ function DAGService (blockService) {
// this.addRecursive

// get retrieves a DAGNode, using the Block Service
this.get = (multihash, callback) => {
if (!multihash) { return callback(new Error('Invalid Key')) }
this.get = function (multihash, callback) {
if (Buffer.isBuffer(multihash)) {
var mhString = base58.encode(multihash)
if (!isIPFS.multihash(mhString)) { return callback(new Error('Invalid Key')) }
this.getWith(multihash, callback)
}

if (typeof multihash === 'string') {
if (!isIPFS.multihash(multihash) && !isIPFS.path(multihash)) {
return callback(new Error('Invalid Key'))
}
if (isIPFS.multihash(multihash)) {
var mhBuffer = new Buffer(base58.decode(multihash))
this.getWith(mhBuffer, callback)
}
if (isIPFS.path(multihash)) {
var ipfsKey = new Buffer(base58.decode(multihash.replace('/ipfs/', '')))
this.getWith(ipfsKey, callback)
}
}
}

this.bs.getBlock(multihash, (err, block) => {
this.getWith = function (key, callback) {
this.bs.getBlock(key, (err, block) => {
if (err) { return callback(err) }
var node = new DAGNode()
node.unMarshal(block.data)
Expand Down
52 changes: 51 additions & 1 deletion tests/merkle-dag-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,22 @@ module.exports = function (repo) {
})
})

it('get a mdag node', (done) => {
it('get a mdag node from base58 encoded string', (done) => {
const node = new DAGNode(new Buffer('more data data data'))
dagService.add(node, (err) => {
expect(err).to.not.exist
var mh = node.multihash()
var encodedMh = bs58.encode(mh)
dagService.get(encodedMh, (err, fetchedNode) => {
expect(err).to.not.exist
expect(node.data).to.deep.equal(fetchedNode.data)
expect(node.links).to.deep.equal(fetchedNode.links)
done()
})
})
})

it('get a mdag node from a multihash buffer', (done) => {
const node = new DAGNode(new Buffer('more data data data'))
dagService.add(node, (err) => {
expect(err).to.not.exist
Expand All @@ -168,6 +183,41 @@ module.exports = function (repo) {
})
})

it('get a mdag node from a /ipfs/ path', (done) => {
const node = new DAGNode(new Buffer('more data data data'))
dagService.add(node, (err) => {
expect(err).to.not.exist
var mh = node.multihash()
var encodedMh = bs58.encode(mh)
var ipfsPath = '/ipfs/' + encodedMh
dagService.get(ipfsPath, (err, fetchedNode) => {
expect(err).to.not.exist
expect(node.data).to.deep.equal(fetchedNode.data)
expect(node.links).to.deep.equal(fetchedNode.links)
done()
})
})
})

it('supply an improperly formatted string path', (done) => {
var mh = 'bad path'
var ipfsPath = '/ipfs/' + mh
dagService.get(ipfsPath, (err, fetchedNode) => {
var error = 'Error: Invalid Key'
expect(err.toString()).to.eql(error)
done()
})
})

it('supply improperly formatted multihash buffer', (done) => {
var mh = new Buffer('more data data data')
dagService.get(mh, (err, fetchedNode) => {
var error = 'Error: Invalid Key'
expect(err.toString()).to.eql(error)
done()
})
})

it('get a dag recursively', (done) => {
// 1 -> 2 -> 3
const node1 = new DAGNode(new Buffer('1'))
Expand Down

0 comments on commit b177708

Please sign in to comment.