From 45b344ca3afe067389b20101b28f54838c0286ef Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Sun, 9 Dec 2018 21:41:27 +0000 Subject: [PATCH 1/4] fix: properly serialize CID instances The CID version agnostic tests https://github.com/ipfs/interface-ipfs-core/pull/413 identified some functions were not properly serializing CID instances. This PR adds `cleanCID` step to several functions and also updates the `cleanCID` function to not assume buffer CIDs are CIDv0 multihashes. depends on https://github.com/ipfs/interface-ipfs-core/pull/413 License: MIT Signed-off-by: Alan Shaw --- src/files-regular/ls-pull-stream.js | 7 +++++++ src/files-regular/ls-readable-stream.js | 7 +++++++ src/files-regular/ls.js | 8 ++++++++ src/utils/clean-cid.js | 7 +++---- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/files-regular/ls-pull-stream.js b/src/files-regular/ls-pull-stream.js index 3430d60c2..a647825fc 100644 --- a/src/files-regular/ls-pull-stream.js +++ b/src/files-regular/ls-pull-stream.js @@ -3,6 +3,7 @@ const moduleConfig = require('../utils/module-config') const pull = require('pull-stream') const deferred = require('pull-defer') +const cleanCID = require('../utils/clean-cid') module.exports = (arg) => { const send = moduleConfig(arg) @@ -13,6 +14,12 @@ module.exports = (arg) => { opts = {} } + try { + args = cleanCID(args) + } catch (err) { + return callback(err) + } + const p = deferred.source() send({ path: 'ls', args: args, qs: opts }, (err, results) => { diff --git a/src/files-regular/ls-readable-stream.js b/src/files-regular/ls-readable-stream.js index 514b8dd31..c249f5b64 100644 --- a/src/files-regular/ls-readable-stream.js +++ b/src/files-regular/ls-readable-stream.js @@ -2,6 +2,7 @@ const moduleConfig = require('../utils/module-config') const Stream = require('readable-stream') +const cleanCID = require('../utils/clean-cid') module.exports = (arg) => { const send = moduleConfig(arg) @@ -12,6 +13,12 @@ module.exports = (arg) => { opts = {} } + try { + args = cleanCID(args) + } catch (err) { + return callback(err) + } + const pt = new Stream.PassThrough({ objectMode: true }) send({ path: 'ls', args: args, qs: opts }, (err, results) => { diff --git a/src/files-regular/ls.js b/src/files-regular/ls.js index 772915e95..67549ecd8 100644 --- a/src/files-regular/ls.js +++ b/src/files-regular/ls.js @@ -2,6 +2,7 @@ const promisify = require('promisify-es6') const moduleConfig = require('../utils/module-config') +const cleanCID = require('../utils/clean-cid') module.exports = (arg) => { const send = moduleConfig(arg) @@ -11,6 +12,13 @@ module.exports = (arg) => { callback = opts opts = {} } + + try { + args = cleanCID(args) + } catch (err) { + return callback(err) + } + send({ path: 'ls', args: args, diff --git a/src/utils/clean-cid.js b/src/utils/clean-cid.js index dbc8386ee..e36901292 100644 --- a/src/utils/clean-cid.js +++ b/src/utils/clean-cid.js @@ -1,18 +1,17 @@ 'use strict' -const bs58 = require('bs58') const CID = require('cids') module.exports = function (cid) { if (Buffer.isBuffer(cid)) { - cid = bs58.encode(cid) + return new CID(cid).toString() } if (CID.isCID(cid)) { - cid = cid.toBaseEncodedString() + return cid.toString() } if (typeof cid !== 'string') { throw new Error('unexpected cid type: ' + typeof cid) } - CID.validateCID(new CID(cid.split('/')[0])) + new CID(cid.split('/')[0]) // eslint-disable-line return cid } From bed26870314166e1b869ecee8fb18eb89e241d33 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 11 Dec 2018 16:41:16 +0000 Subject: [PATCH 2/4] fix: disable just the rule we're breaking License: MIT Signed-off-by: Alan Shaw --- src/utils/clean-cid.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/clean-cid.js b/src/utils/clean-cid.js index e36901292..1767c9ac2 100644 --- a/src/utils/clean-cid.js +++ b/src/utils/clean-cid.js @@ -12,6 +12,6 @@ module.exports = function (cid) { if (typeof cid !== 'string') { throw new Error('unexpected cid type: ' + typeof cid) } - new CID(cid.split('/')[0]) // eslint-disable-line + new CID(cid.split('/')[0]) // eslint-disable-line no-new return cid } From 98bf3d7107606fd9affbc314dab2135a551b23c4 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 12 Dec 2018 09:24:59 +0000 Subject: [PATCH 3/4] chore: update dependencies License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d0e939af3..2cfb6b260 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "eslint-plugin-react": "^7.11.1", "go-ipfs-dep": "~0.4.18", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.91.0", + "interface-ipfs-core": "~0.92.0", "ipfsd-ctl": "~0.40.0", "nock": "^10.0.2", "pull-stream": "^3.6.9", From 0e15761dc4d7b14168d5cd47d688ad627fb0fc2a Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 13 Dec 2018 11:18:07 +0000 Subject: [PATCH 4/4] fix: skip test that go-ipfs cannot pass For some reason go-ipfs does not support a `version` parameter in the `/api/v0/block/put` endpoint but js-ipfs does. This means that go-ipfs cannot pass this skipped CID version agnostic test. When IPFS switches to v1 CIDs by default we'll have to switch this skipped test to the one that tests the other way around i.e. CIDv0 to CIDv1. License: MIT Signed-off-by: Alan Shaw --- test/interface.spec.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/interface.spec.js b/test/interface.spec.js index ba648a932..5d87a2b91 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -34,7 +34,12 @@ describe('interface-ipfs-core tests', () => { ] }) - tests.block(defaultCommonFactory) + tests.block(defaultCommonFactory, { + skip: [{ + name: 'should get a block added as CIDv1 with a CIDv0', + reason: 'go-ipfs does not support the `version` param' + }] + }) tests.bootstrap(defaultCommonFactory)