diff --git a/src/api/cat.js b/src/api/cat.js index 762469a14..a7fca4ec9 100644 --- a/src/api/cat.js +++ b/src/api/cat.js @@ -1,7 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const cleanMultihash = require('../clean-multihash') +const cleanCID = require('../clean-cid') module.exports = (send) => { return promisify((hash, opts, callback) => { @@ -11,7 +11,7 @@ module.exports = (send) => { } try { - hash = cleanMultihash(hash) + hash = cleanCID(hash) } catch (err) { return callback(err) } diff --git a/src/api/get.js b/src/api/get.js index 3b4c0c983..1da398ca4 100644 --- a/src/api/get.js +++ b/src/api/get.js @@ -1,7 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const cleanMultihash = require('../clean-multihash') +const cleanCID = require('../clean-cid') const TarStreamToObjects = require('../tar-stream-to-objects') module.exports = (send) => { @@ -21,7 +21,7 @@ module.exports = (send) => { } try { - path = cleanMultihash(path) + path = cleanCID(path) } catch (err) { return callback(err) } diff --git a/src/clean-cid.js b/src/clean-cid.js new file mode 100644 index 000000000..f73a1e004 --- /dev/null +++ b/src/clean-cid.js @@ -0,0 +1,15 @@ +'use strict' + +const bs58 = require('bs58') +const CID = require('cids') + +module.exports = function (cid) { + if (Buffer.isBuffer(cid)) { + cid = bs58.encode(cid) + } + if (typeof cid !== 'string') { + throw new Error('unexpected cid type: ' + typeof cid) + } + CID.validateCID(new CID(cid.split('/')[0])) + return cid +}