From da7d0f499112de65f7ab402901ddba7377e35db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sun, 14 May 2017 23:29:38 +0200 Subject: [PATCH] feat: Use CID for api.get/api.cat (#555) --- src/api/cat.js | 4 ++-- src/api/get.js | 4 ++-- src/clean-cid.js | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 src/clean-cid.js 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 +}