1
1
const DAGNode = require ( './dag-node' ) . DAGNode
2
2
const Block = require ( 'ipfs-blocks' ) . Block
3
- const isIPFS = require ( 'is-ipfs' )
3
+ const isIPFS = require ( '../node_modules/ is-ipfs/src/index ' )
4
4
const base58 = require ( 'bs58' )
5
5
6
6
exports = module . exports = DAGService
@@ -26,38 +26,26 @@ function DAGService (blockService) {
26
26
27
27
// get retrieves a DAGNode, using the Block Service
28
28
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 )
31
31
32
- if ( ! isBuf && ! isString ) {
32
+ if ( ! isMhash && ! isPath ) {
33
33
return callback ( new Error ( 'Invalid Key' ) )
34
34
}
35
35
36
- if ( isBuf ) {
37
- var mhString = base58 . encode ( multihash )
38
- if ( ! isIPFS . multihash ( mhString ) ) { return callback ( new Error ( 'Invalid Key' ) ) }
36
+ if ( isMhash ) {
39
37
this . getWith ( multihash , callback )
40
38
}
41
39
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 )
56
43
}
57
44
}
58
45
59
46
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 ) => {
61
49
if ( err ) { return callback ( err ) }
62
50
var node = new DAGNode ( )
63
51
node . unMarshal ( block . data )
0 commit comments