Skip to content

Commit 89fec70

Browse files
committed
refactored dagservice with new is-ipfs
1 parent 7af1081 commit 89fec70

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"dependencies": {
2626
"ipfs-blocks": "^0.1.0",
27-
"is-ipfs": "^0.1.0",
27+
"is-ipfs": "github:nginnever/is-ipfs.git#0ca3ad3c10e4dfb549f0ca70824a9bb07ea6777b",
2828
"multihashing": "^0.2.0",
2929
"protocol-buffers": "^3.1.4",
3030
"stable": "^0.1.5"

src/dag-service.js

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const DAGNode = require('./dag-node').DAGNode
22
const Block = require('ipfs-blocks').Block
3-
const isIPFS = require('is-ipfs')
3+
const isIPFS = require('../node_modules/is-ipfs/src/index')
44
const base58 = require('bs58')
55

66
exports = module.exports = DAGService
@@ -26,38 +26,26 @@ function DAGService (blockService) {
2626

2727
// get retrieves a DAGNode, using the Block Service
2828
this.get = function (multihash, callback) {
29-
const isBuf = Buffer.isBuffer(multihash)
30-
const isString = typeof multihash === 'string'
29+
const isMhash = isIPFS.multihash(multihash)
30+
const isPath = isIPFS.path(multihash)
3131

32-
if (!isBuf && !isString) {
32+
if (!isMhash && !isPath) {
3333
return callback(new Error('Invalid Key'))
3434
}
3535

36-
if (isBuf) {
37-
var mhString = base58.encode(multihash)
38-
if (!isIPFS.multihash(mhString)) { return callback(new Error('Invalid Key')) }
36+
if (isMhash) {
3937
this.getWith(multihash, callback)
4038
}
4139

42-
if (isString) {
43-
var isMhash = isIPFS.multihash(multihash)
44-
var isPath = isIPFS.path(multihash)
45-
if (!isMhash && !isPath) {
46-
return callback(new Error('Invalid Key'))
47-
}
48-
if (isMhash) {
49-
var mhBuffer = new Buffer(base58.decode(multihash))
50-
this.getWith(mhBuffer, callback)
51-
}
52-
if (isPath) {
53-
var ipfsKey = new Buffer(base58.decode(multihash.replace('/ipfs/', '')))
54-
this.getWith(ipfsKey, callback)
55-
}
40+
if (isPath) {
41+
var ipfsKey = new Buffer(base58.decode(multihash.replace('/ipfs/', '')))
42+
this.getWith(ipfsKey, callback)
5643
}
5744
}
5845

5946
this.getWith = function (key, callback) {
60-
this.bs.getBlock(key, (err, block) => {
47+
const formatted = typeof key === 'string' ? new Buffer(base58.decode(key)) : key
48+
this.bs.getBlock(formatted, (err, block) => {
6149
if (err) { return callback(err) }
6250
var node = new DAGNode()
6351
node.unMarshal(block.data)

0 commit comments

Comments
 (0)