Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check ipfs path with is-ipfs #31

Merged
merged 7 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
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
41 changes: 36 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,39 @@ 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) {
const isBuf = Buffer.isBuffer(multihash)
const isString = typeof multihash === 'string'

if (!isBuf && !isString) {
return callback(new Error('Invalid Key'))
}

if (isBuf) {
var mhString = base58.encode(multihash)
if (!isIPFS.multihash(mhString)) { return callback(new Error('Invalid Key')) }
this.getWith(multihash, callback)
}

if (isString) {
var isMhash = isIPFS.multihash(multihash)
var isPath = isIPFS.path(multihash)
if (!isMhash && !isPath) {
return callback(new Error('Invalid Key'))
}
if (isMhash) {
var mhBuffer = new Buffer(base58.decode(multihash))
this.getWith(mhBuffer, callback)
}
if (isPath) {
var ipfsKey = new Buffer(base58.decode(multihash.replace('/ipfs/', '')))
this.getWith(ipfsKey, callback)
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 @nginnever :D

Can we get all of this part of 'is-ipfs' logic? I'm sure @xicombd would be happy to include it there and it will be useful for a ton of other places.

btw, it would be stellar if mafmt from @whyrusleeping used this to check multiaddrs that are also ipfs multiaddrs with valid multihashes :)


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
65 changes: 55 additions & 10 deletions tests/merkle-dag-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,62 @@ module.exports = function (repo) {
})
})

it('get a mdag node', (done) => {
const node = new DAGNode(new Buffer('more data data data'))
dagService.add(node, (err) => {
it('get a mdag node from base58 encoded string', (done) => {
var encodedMh = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
dagService.get(encodedMh, (err, fetchedNode) => {
expect(err).to.not.exist
var mh = node.multihash()
dagService.get(mh, (err, fetchedNode) => {
expect(err).to.not.exist
expect(node.data).to.deep.equal(fetchedNode.data)
expect(node.links).to.deep.equal(fetchedNode.links)
done()
})
expect(fetchedNode.data).to.deep.equal(new Buffer(bs58.decode('cL')))
// just picking the second link and comparing mhash buffer to expected
expect(fetchedNode.links[1].hash).to.deep.equal(new Buffer(bs58.decode('QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y')))
done()
})
})

it('get a mdag node from a multihash buffer', (done) => {
var mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'))
dagService.get(mh, (err, fetchedNode) => {
expect(err).to.not.exist
expect(fetchedNode.data).to.deep.equal(new Buffer(bs58.decode('cL')))
expect(fetchedNode.links[1].hash).to.deep.equal(new Buffer(bs58.decode('QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y')))
done()
})
})

it('get a mdag node from a /ipfs/ path', (done) => {
var ipfsPath = '/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
dagService.get(ipfsPath, (err, fetchedNode) => {
expect(err).to.not.exist
expect(fetchedNode.data).to.deep.equal(new Buffer(bs58.decode('cL')))
expect(fetchedNode.links[1].hash).to.deep.equal(new Buffer(bs58.decode('QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y')))
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.equal(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.equal(error)
done()
})
})

it('supply something weird', (done) => {
var mh = 3
dagService.get(mh, (err, fetchedNode) => {
var error = 'Error: Invalid Key'
expect(err.toString()).to.equal(error)
done()
})
})

Expand Down