diff --git a/package.json b/package.json index 860a95e5a..81b0daee0 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.93.0", + "interface-ipfs-core": "~0.94.0", "ipfsd-ctl": "~0.40.0", "nock": "^10.0.2", "pull-stream": "^3.6.9", diff --git a/src/object/addLink.js b/src/object/addLink.js index 3ed3c7bc5..75087578b 100644 --- a/src/object/addLink.js +++ b/src/object/addLink.js @@ -2,10 +2,9 @@ const promisify = require('promisify-es6') const CID = require('cids') -const cleanMultihash = require('../utils/clean-multihash') module.exports = (send) => { - return promisify((multihash, dLink, opts, callback) => { + return promisify((cid, dLink, opts, callback) => { if (typeof opts === 'function') { callback = opts opts = {} @@ -15,7 +14,7 @@ module.exports = (send) => { } try { - multihash = cleanMultihash(multihash, opts) + cid = new CID(cid) } catch (err) { return callback(err) } @@ -23,9 +22,9 @@ module.exports = (send) => { send({ path: 'object/patch/add-link', args: [ - multihash, + cid.toString(), dLink.name, - cleanMultihash(dLink.cid.buffer) + dLink.cid.toString() ] }, (err, result) => { if (err) { diff --git a/src/object/appendData.js b/src/object/appendData.js index 1fe4ce695..6c6cdb722 100644 --- a/src/object/appendData.js +++ b/src/object/appendData.js @@ -3,13 +3,12 @@ const promisify = require('promisify-es6') const once = require('once') const CID = require('cids') -const cleanMultihash = require('../utils/clean-multihash') const SendOneFile = require('../utils/send-one-file') module.exports = (send) => { const sendOneFile = SendOneFile(send, 'object/patch/append-data') - return promisify((multihash, data, opts, _callback) => { + return promisify((cid, data, opts, _callback) => { if (typeof opts === 'function') { _callback = opts opts = {} @@ -20,12 +19,12 @@ module.exports = (send) => { } try { - multihash = cleanMultihash(multihash, opts) + cid = new CID(cid) } catch (err) { return callback(err) } - sendOneFile(data, { args: [multihash] }, (err, result) => { + sendOneFile(data, { args: [cid.toString()] }, (err, result) => { if (err) { return callback(err) } diff --git a/src/object/get.js b/src/object/get.js index 068d92237..a8d44db92 100644 --- a/src/object/get.js +++ b/src/object/get.js @@ -4,7 +4,6 @@ const promisify = require('promisify-es6') const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode const DAGLink = dagPB.DAGLink -const bs58 = require('bs58') const CID = require('cids') const LRU = require('lru-cache') const lruOptions = { @@ -53,7 +52,7 @@ module.exports = (send) => { result.Data = Buffer.from(result.Data, 'base64') const links = result.Links.map((l) => { - return new DAGLink(l.Name, l.Size, Buffer.from(bs58.decode(l.Hash))) + return new DAGLink(l.Name, l.Size, l.Hash) }) DAGNode.create(result.Data, links, (err, node) => { diff --git a/src/object/links.js b/src/object/links.js index 30196c3ec..41527d998 100644 --- a/src/object/links.js +++ b/src/object/links.js @@ -3,7 +3,7 @@ const promisify = require('promisify-es6') const dagPB = require('ipld-dag-pb') const DAGLink = dagPB.DAGLink -const cleanMultihash = require('../utils/clean-multihash') +const CID = require('cids') const LRU = require('lru-cache') const lruOptions = { max: 128 @@ -12,7 +12,7 @@ const lruOptions = { const cache = new LRU(lruOptions) module.exports = (send) => { - return promisify((multihash, options, callback) => { + return promisify((cid, options, callback) => { if (typeof options === 'function') { callback = options options = {} @@ -22,12 +22,12 @@ module.exports = (send) => { } try { - multihash = cleanMultihash(multihash, options) + cid = new CID(cid) } catch (err) { return callback(err) } - const node = cache.get(multihash) + const node = cache.get(cid.toString()) if (node) { return callback(null, node.links) @@ -35,7 +35,7 @@ module.exports = (send) => { send({ path: 'object/links', - args: multihash + args: cid.toString() }, (err, result) => { if (err) { return callback(err) @@ -44,9 +44,7 @@ module.exports = (send) => { let links = [] if (result.Links) { - links = result.Links.map((l) => { - return new DAGLink(l.Name, l.Size, l.Hash) - }) + links = result.Links.map((l) => new DAGLink(l.Name, l.Size, l.Hash)) } callback(null, links) }) diff --git a/src/object/rmLink.js b/src/object/rmLink.js index 3a45529cf..4726bb216 100644 --- a/src/object/rmLink.js +++ b/src/object/rmLink.js @@ -2,10 +2,9 @@ const promisify = require('promisify-es6') const CID = require('cids') -const cleanMultihash = require('../utils/clean-multihash') module.exports = (send) => { - return promisify((multihash, dLink, opts, callback) => { + return promisify((cid, dLink, opts, callback) => { if (typeof opts === 'function') { callback = opts opts = {} @@ -15,7 +14,7 @@ module.exports = (send) => { } try { - multihash = cleanMultihash(multihash, opts) + cid = new CID(cid) } catch (err) { return callback(err) } @@ -23,7 +22,7 @@ module.exports = (send) => { send({ path: 'object/patch/rm-link', args: [ - multihash, + cid.toString(), dLink.name ] }, (err, result) => { diff --git a/src/object/setData.js b/src/object/setData.js index 7a256ca11..5eb014474 100644 --- a/src/object/setData.js +++ b/src/object/setData.js @@ -3,13 +3,12 @@ const promisify = require('promisify-es6') const once = require('once') const CID = require('cids') -const cleanMultihash = require('../utils/clean-multihash') const SendOneFile = require('../utils/send-one-file') module.exports = (send) => { const sendOneFile = SendOneFile(send, 'object/patch/set-data') - return promisify((multihash, data, opts, _callback) => { + return promisify((cid, data, opts, _callback) => { if (typeof opts === 'function') { _callback = opts opts = {} @@ -20,12 +19,12 @@ module.exports = (send) => { } try { - multihash = cleanMultihash(multihash, opts) + cid = new CID(cid) } catch (err) { return callback(err) } - sendOneFile(data, { args: [multihash] }, (err, result) => { + sendOneFile(data, { args: [cid.toString()] }, (err, result) => { if (err) { return callback(err) } diff --git a/src/object/stat.js b/src/object/stat.js index ba2c2b48c..280e9f8e1 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -1,10 +1,10 @@ 'use strict' const promisify = require('promisify-es6') -const cleanMultihash = require('../utils/clean-multihash') +const CID = require('cids') module.exports = (send) => { - return promisify((multihash, opts, callback) => { + return promisify((cid, opts, callback) => { if (typeof opts === 'function') { callback = opts opts = {} @@ -14,14 +14,14 @@ module.exports = (send) => { } try { - multihash = cleanMultihash(multihash, opts) + cid = new CID(cid) } catch (err) { return callback(err) } send({ path: 'object/stat', - args: multihash + args: cid.toString() }, callback) }) } diff --git a/src/resolve.js b/src/resolve.js index 05b62282d..f48a3b02b 100644 --- a/src/resolve.js +++ b/src/resolve.js @@ -1,10 +1,8 @@ 'use strict' const promisify = require('promisify-es6') - -const transform = function (res, callback) { - callback(null, res.Path) -} +const multibase = require('multibase') +const CID = require('cids') module.exports = (send) => { return promisify((args, opts, callback) => { @@ -13,6 +11,40 @@ module.exports = (send) => { opts = {} } + opts = opts || {} + + if (opts.cidBase) { + opts['cid-base'] = opts.cidBase + delete opts.cidBase + } + + const transform = (res, callback) => { + if (!opts['cid-base']) { + return callback(null, res.Path) + } + + // FIXME: remove when go-ipfs supports ?cid-base for /api/v0/resolve + // https://github.com/ipfs/go-ipfs/pull/5777#issuecomment-439838555 + const parts = res.Path.split('/') // ['', 'ipfs', 'QmHash', ...] + + if (multibase.isEncoded(parts[2]) !== opts['cid-base']) { + try { + let cid = new CID(parts[2]) + + if (cid.version === 0 && opts['cid-base'] !== 'base58btc') { + cid = cid.toV1() + } + + parts[2] = cid.toBaseEncodedString(opts['cid-base']) + res.Path = parts.join('/') + } catch (err) { + return callback(err) + } + } + + callback(null, res.Path) + } + send.andTransform({ path: 'resolve', args: args,