Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 2d4f8d4

Browse files
author
Alan Shaw
committed
refactor: pass cidBase arg to core
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 76a1153 commit 2d4f8d4

File tree

19 files changed

+322
-111
lines changed

19 files changed

+322
-111
lines changed

src/cli/commands/bitswap/unwant.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

3-
const print = require('../../utils').print
3+
const multibase = require('multibase')
4+
const { print } = require('../../utils')
5+
const { cidToString } = require('../../../utils/cid')
46

57
module.exports = {
68
command: 'unwant <key>',
@@ -12,14 +14,19 @@ module.exports = {
1214
alias: 'k',
1315
describe: 'Key to remove from your wantlist',
1416
type: 'string'
17+
},
18+
'cid-base': {
19+
describe: 'Number base to display CIDs in.',
20+
type: 'string',
21+
choices: multibase.names
1522
}
1623
},
1724
handler (argv) {
1825
argv.ipfs.bitswap.unwant(argv.key, (err) => {
1926
if (err) {
2027
throw err
2128
}
22-
print(`Key ${argv.key} removed from wantlist`)
29+
print(`Key ${cidToString(argv.key, argv.cidBase)} removed from wantlist`)
2330
})
2431
}
2532
}

src/cli/commands/bitswap/wantlist.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ module.exports = {
2828
if (err) {
2929
throw err
3030
}
31-
cids.forEach((cid) => {
32-
print(cid)
33-
})
31+
cids.forEach((cid) => print(cid))
3432
})
3533
}
3634
}

src/cli/commands/files/add.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const waterfall = require('async/waterfall')
1313
const mh = require('multihashes')
1414
const multibase = require('multibase')
1515
const { print, isDaemonOn, createProgressBar } = require('../../utils')
16-
const { cidToString } = require('../../../utils/cid')
1716

1817
function checkPath (inPath, recursive) {
1918
// This function is to check for the following possible inputs
@@ -90,7 +89,7 @@ function addPipeline (index, addStream, list, argv) {
9089
sortBy(added, 'path')
9190
.reverse()
9291
.map((file) => {
93-
const log = [ 'added', cidToString(file.hash, argv.cidBase) ]
92+
const log = [ 'added', file.hash ]
9493

9594
if (!quiet && file.path.length > 0) log.push(file.path)
9695

@@ -199,6 +198,7 @@ module.exports = {
199198
? argv.shardSplitThreshold
200199
: Infinity,
201200
cidVersion: argv.cidVersion,
201+
cidBase: argv.cidBase,
202202
rawLeaves: argv.rawLeaves,
203203
onlyHash: argv.onlyHash,
204204
hashAlg: argv.hash,

src/cli/commands/ls.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const multibase = require('multibase')
44
const { print, rightpad } = require('../utils')
5-
const { cidToString } = require('../../utils/cid')
65

76
module.exports = {
87
command: 'ls <key>',
@@ -35,13 +34,11 @@ module.exports = {
3534
},
3635

3736
handler (argv) {
38-
argv.ipfs.ls(argv.key, { recursive: argv.recursive }, (err, links) => {
37+
argv.ipfs.ls(argv.key, { recursive: argv.recursive, cidBase: argv.cidBase }, (err, links) => {
3938
if (err) {
4039
throw err
4140
}
4241

43-
links = links.map(l => Object.assign(l, { hash: cidToString(l.hash, argv.cidBase) }))
44-
4542
if (argv.headers) {
4643
links = [{hash: 'Hash', size: 'Size', name: 'Name'}].concat(links)
4744
}

src/cli/commands/pin/add.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5-
const { cidToString } = require('../../../utils/cid')
65

76
module.exports = {
87
command: 'add <ipfsPath...>',
@@ -24,12 +23,13 @@ module.exports = {
2423
},
2524

2625
handler (argv) {
27-
const recursive = argv.recursive
26+
const { recursive, cidBase } = argv
2827
const type = recursive ? 'recursive' : 'direct'
29-
argv.ipfs.pin.add(argv.ipfsPath, { recursive: recursive }, (err, results) => {
28+
29+
argv.ipfs.pin.add(argv.ipfsPath, { recursive, cidBase }, (err, results) => {
3030
if (err) { throw err }
3131
results.forEach((res) => {
32-
print(`pinned ${cidToString(res.hash, argv.cidBase)} ${type}ly`)
32+
print(`pinned ${res.hash} ${type}ly`)
3333
})
3434
})
3535
}

src/cli/commands/pin/ls.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5-
const { cidToString } = require('../../../utils/cid')
65

76
module.exports = {
87
// bracket syntax with '...' tells yargs to optionally accept a list
@@ -33,13 +32,12 @@ module.exports = {
3332

3433
handler: (argv) => {
3534
const paths = argv.ipfsPath
36-
const type = argv.type
37-
const quiet = argv.quiet
35+
const { type, quiet, cidBase } = argv
3836

39-
argv.ipfs.pin.ls(paths, { type }, (err, results) => {
37+
argv.ipfs.pin.ls(paths, { type, cidBase }, (err, results) => {
4038
if (err) { throw err }
4139
results.forEach((res) => {
42-
let line = cidToString(res.hash, argv.cidBase)
40+
let line = res.hash
4341
if (!quiet) {
4442
line += ` ${res.type}`
4543
}

src/cli/commands/pin/rm.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ module.exports = {
2424
},
2525

2626
handler: (argv) => {
27-
const recursive = argv.recursive
28-
argv.ipfs.pin.rm(argv.ipfsPath, { recursive: recursive }, (err, results) => {
27+
const { recursive, cidBase } = argv
28+
29+
argv.ipfs.pin.rm(argv.ipfsPath, { recursive, cidBase }, (err, results) => {
2930
if (err) { throw err }
3031
results.forEach((res) => {
3132
print(`unpinned ${cidToString(res.hash, argv.cidBase)}`)

src/cli/commands/resolve.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ module.exports = {
2222
},
2323

2424
handler (argv) {
25-
argv.ipfs.resolve(argv.name, {
26-
recursive: argv.recursive,
27-
cidBase: argv.cidBase
28-
}, (err, res) => {
25+
const { recursive, cidBase } = argv
26+
argv.ipfs.resolve(argv.name, { recursive, cidBase }, (err, res) => {
2927
if (err) throw err
3028
print(res)
3129
})

src/core/components/bitswap.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ const Big = require('big.js')
77
const CID = require('cids')
88
const PeerId = require('peer-id')
99
const errCode = require('err-code')
10+
const multibase = require('multibase')
11+
const { cidToString } = require('../../utils/cid')
1012

11-
function wantlistToCidArray (list) {
12-
return Array.from(list).map((e) => e[1].cid)
13+
function formatWantlist (list, cidBase) {
14+
return Array.from(list).map((e) => cidToString(e[1].cid, cidBase))
1315
}
1416

1517
module.exports = function bitswap (self) {
@@ -24,10 +26,18 @@ module.exports = function bitswap (self) {
2426
options = {}
2527
}
2628

29+
options = options || {}
30+
2731
if (!self.isOnline()) {
2832
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
2933
}
3034

35+
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
36+
return setImmediate(() => {
37+
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
38+
})
39+
}
40+
3141
let list
3242
if (peerId) {
3343
try {
@@ -43,7 +53,7 @@ module.exports = function bitswap (self) {
4353
list = self._bitswap.getWantlist()
4454
}
4555

46-
setImmediate(() => callback(null, wantlistToCidArray(list)))
56+
setImmediate(() => callback(null, formatWantlist(list, options.cidBase)))
4757
}),
4858

4959
stat: promisify((options, callback) => {
@@ -52,17 +62,25 @@ module.exports = function bitswap (self) {
5262
options = {}
5363
}
5464

65+
options = options || {}
66+
5567
if (!self.isOnline()) {
5668
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
5769
}
5870

71+
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
72+
return setImmediate(() => {
73+
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
74+
})
75+
}
76+
5977
const snapshot = self._bitswap.stat().snapshot
6078

6179
setImmediate(() => {
6280
callback(null, {
6381
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
6482
blocksReceived: new Big(snapshot.blocksReceived),
65-
wantlist: wantlistToCidArray(self._bitswap.getWantlist()),
83+
wantlist: formatWantlist(self._bitswap.getWantlist(), options.cidBase),
6684
peers: self._bitswap.peers().map((id) => id.toB58String()),
6785
dupBlksReceived: new Big(snapshot.dupBlksReceived),
6886
dupDataReceived: new Big(snapshot.dupDataReceived),

src/core/components/block.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const waterfall = require('async/waterfall')
77
const setImmediate = require('async/setImmediate')
88
const promisify = require('promisify-es6')
99
const errCode = require('err-code')
10+
const multibase = require('multibase')
11+
const { cidToString } = require('../../utils/cid')
1012

1113
module.exports = function block (self) {
1214
return {
@@ -102,6 +104,12 @@ module.exports = function block (self) {
102104
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
103105
}
104106

107+
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
108+
return setImmediate(() => {
109+
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
110+
})
111+
}
112+
105113
if (options.preload !== false) {
106114
self._preload(cid)
107115
}
@@ -111,7 +119,7 @@ module.exports = function block (self) {
111119
return callback(err)
112120
}
113121
callback(null, {
114-
key: cid,
122+
key: cidToString(cid, options.cidBase),
115123
size: block.data.length
116124
})
117125
})

src/core/components/files.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const OtherBuffer = require('buffer').Buffer
1717
const CID = require('cids')
1818
const toB58String = require('multihashes').toB58String
1919
const errCode = require('err-code')
20+
const multibase = require('multibase')
2021
const parseChunkerString = require('../utils').parseChunkerString
22+
const { cidToString } = require('../../utils/cid')
2123

2224
const WRAPPER = 'wrapper/'
2325

@@ -32,11 +34,13 @@ function prepareFile (file, opts) {
3234
cid = cid.toV1()
3335
}
3436

37+
const cidStr = cidToString(cid, opts.cidBase)
38+
3539
return {
3640
path: opts.wrapWithDirectory
3741
? file.path.substring(WRAPPER.length)
38-
: (file.path || cid.toBaseEncodedString()),
39-
hash: cid,
42+
: (file.path || cidStr),
43+
hash: cidStr,
4044
size: file.size
4145
}
4246
}
@@ -141,19 +145,28 @@ class AddHelper extends Duplex {
141145
}
142146

143147
module.exports = function files (self) {
144-
function _addPullStream (options = {}) {
148+
function _addPullStream (options) {
149+
options = options || {}
150+
145151
let chunkerOptions
146152
try {
147153
chunkerOptions = parseChunkerString(options.chunker)
148154
} catch (err) {
149155
return pull.map(() => { throw err })
150156
}
157+
151158
const opts = Object.assign({}, {
152159
shardSplitThreshold: self._options.EXPERIMENTAL.sharding
153160
? 1000
154161
: Infinity
155162
}, options, chunkerOptions)
156163

164+
if (opts.cidBase && !multibase.names.includes(opts.cidBase)) {
165+
return pull.map(() => {
166+
throw errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE')
167+
})
168+
}
169+
157170
if (opts.hashAlg && opts.cidVersion !== 1) {
158171
opts.cidVersion = 1
159172
}
@@ -228,6 +241,10 @@ module.exports = function files (self) {
228241
const maxDepth = recursive ? global.Infinity : pathDepth
229242
options.maxDepth = options.maxDepth || maxDepth
230243

244+
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
245+
return pull.error(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
246+
}
247+
231248
if (options.preload !== false) {
232249
self._preload(pathComponents[0])
233250
}
@@ -238,10 +255,7 @@ module.exports = function files (self) {
238255
recursive ? node.depth >= pathDepth : node.depth === pathDepth
239256
),
240257
pull.map(node => {
241-
// TODO: is this check required - is node.hash from exporter always a CID now?
242-
if (!CID.isCID(node.hash)) {
243-
node = Object.assign({}, node, { hash: new CID(node.hash) })
244-
}
258+
node = Object.assign({}, node, { hash: cidToString(node.hash, options.cidBase) })
245259
delete node.content
246260
return node
247261
})
@@ -269,11 +283,6 @@ module.exports = function files (self) {
269283
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
270284
}
271285

272-
// CID v0 is for multihashes encoded with sha2-256
273-
if (options.hashAlg && options.cidVersion !== 1) {
274-
options.cidVersion = 1
275-
}
276-
277286
pull(
278287
pull.values([data]),
279288
_addPullStream(options),

src/core/components/object.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const CID = require('cids')
1010
const mh = require('multihashes')
1111
const Unixfs = require('ipfs-unixfs')
1212
const errCode = require('err-code')
13+
const multibase = require('multibase')
14+
const { cidToString } = require('../../utils/cid')
1315

1416
function normalizeMultihash (multihash, enc) {
1517
if (typeof multihash === 'string') {
@@ -292,6 +294,14 @@ module.exports = function object (self) {
292294
options = {}
293295
}
294296

297+
options = options || {}
298+
299+
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
300+
return setImmediate(() => {
301+
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
302+
})
303+
}
304+
295305
self.object.get(multihash, options, (err, node) => {
296306
if (err) {
297307
return callback(err)
@@ -305,10 +315,8 @@ module.exports = function object (self) {
305315
const blockSize = serialized.length
306316
const linkLength = node.links.reduce((a, l) => a + l.size, 0)
307317

308-
const nodeJSON = node.toJSON()
309-
310318
callback(null, {
311-
Hash: nodeJSON.multihash,
319+
Hash: cidToString(node.multihash, options.cidBase),
312320
NumLinks: node.links.length,
313321
BlockSize: blockSize,
314322
LinksSize: blockSize - node.data.length,

0 commit comments

Comments
 (0)