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

Commit da51dc6

Browse files
alanshawAlan Shaw
authored and
Alan Shaw
committed
feat: add cid-base option
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent c73bd2f commit da51dc6

40 files changed

+786
-218
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"mkdirp": "~0.5.1",
148148
"multiaddr": "^5.0.0",
149149
"multiaddr-to-uri": "^4.0.0",
150-
"multibase": "~0.4.0",
150+
"multibase": "~0.5.0",
151151
"multihashes": "~0.4.13",
152152
"once": "^1.4.0",
153153
"path-exists": "^3.0.0",

src/cli/commands/bitswap/stat.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
'use strict'
22

3-
const print = require('../../utils').print
3+
const multibase = require('multibase')
4+
const { print } = require('../../utils')
45

56
module.exports = {
67
command: 'stat',
78

89
describe: 'Show some diagnostic information on the bitswap agent.',
910

10-
builder: {},
11+
builder: {
12+
'cid-base': {
13+
describe: 'Number base to display CIDs in.',
14+
type: 'string',
15+
choices: multibase.names
16+
}
17+
},
1118

1219
handler (argv) {
13-
argv.ipfs.bitswap.stat((err, stats) => {
20+
const { cidBase } = argv
21+
22+
argv.ipfs.bitswap.stat({ cidBase }, (err, stats) => {
1423
if (err) {
1524
throw err
1625
}
1726

1827
stats.wantlist = stats.wantlist || []
19-
stats.wantlist = stats.wantlist.map(entry => entry['/'])
2028
stats.peers = stats.peers || []
2129

2230
print(`bitswap status

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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const print = require('../../utils').print
3+
const multibase = require('multibase')
4+
const { print } = require('../../utils')
45

56
module.exports = {
67
command: 'wantlist [peer]',
@@ -12,17 +13,22 @@ module.exports = {
1213
alias: 'p',
1314
describe: 'Specify which peer to show wantlist for.',
1415
type: 'string'
16+
},
17+
'cid-base': {
18+
describe: 'Number base to display CIDs in.',
19+
type: 'string',
20+
choices: multibase.names
1521
}
1622
},
1723

1824
handler (argv) {
19-
argv.ipfs.bitswap.wantlist(argv.peer, (err, res) => {
25+
const { peer, cidBase } = argv
26+
27+
argv.ipfs.bitswap.wantlist(peer, { cidBase }, (err, cids) => {
2028
if (err) {
2129
throw err
2230
}
23-
res.Keys.forEach((cid) => {
24-
print(cid['/'])
25-
})
31+
cids.forEach((cid) => print(cid))
2632
})
2733
}
2834
}

src/cli/commands/block/put.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const bl = require('bl')
66
const fs = require('fs')
77
const Block = require('ipfs-block')
88
const waterfall = require('async/waterfall')
9-
const print = require('../../utils').print
9+
const multibase = require('multibase')
10+
const { print } = require('../../utils')
11+
const { cidToString } = require('../../../utils/cid')
1012

1113
function addBlock (data, opts) {
1214
const ipfs = opts.ipfs
@@ -27,7 +29,7 @@ function addBlock (data, opts) {
2729
if (err) {
2830
throw err
2931
}
30-
print(cid.toBaseEncodedString())
32+
print(cidToString(cid, opts.cidBase))
3133
})
3234
}
3335

@@ -54,6 +56,11 @@ module.exports = {
5456
describe: 'cid version',
5557
type: 'number',
5658
default: 0
59+
},
60+
'cid-base': {
61+
describe: 'Number base to display CIDs in.',
62+
type: 'string',
63+
choices: multibase.names
5764
}
5865
},
5966

src/cli/commands/block/stat.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
'use strict'
22

3-
const CID = require('cids')
4-
const print = require('../../utils').print
3+
const multibase = require('multibase')
4+
const { print } = require('../../utils')
55

66
module.exports = {
77
command: 'stat <key>',
88

99
describe: 'Print information of a raw IPFS block',
1010

11-
builder: {},
11+
builder: {
12+
'cid-base': {
13+
describe: 'Number base to display CIDs in.',
14+
type: 'string',
15+
choices: multibase.names
16+
}
17+
},
1218

1319
handler (argv) {
14-
argv.ipfs.block.stat(new CID(argv.key), (err, stats) => {
20+
const { key, cidBase } = argv
21+
22+
argv.ipfs.block.stat(key, { cidBase }, (err, stats) => {
1523
if (err) {
1624
throw err
1725
}

src/cli/commands/files/add.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ const getFolderSize = require('get-folder-size')
1111
const byteman = require('byteman')
1212
const waterfall = require('async/waterfall')
1313
const mh = require('multihashes')
14-
const utils = require('../../utils')
15-
const print = require('../../utils').print
16-
const createProgressBar = require('../../utils').createProgressBar
14+
const multibase = require('multibase')
15+
const { print, isDaemonOn, createProgressBar } = require('../../utils')
1716

1817
function checkPath (inPath, recursive) {
1918
// This function is to check for the following possible inputs
@@ -156,10 +155,15 @@ module.exports = {
156155
describe: 'CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)',
157156
default: 0
158157
},
158+
'cid-base': {
159+
describe: 'Number base to display CIDs in.',
160+
type: 'string',
161+
choices: multibase.names
162+
},
159163
hash: {
160164
type: 'string',
161165
choices: Object.keys(mh.names),
162-
describe: 'Hash function to use. Will set Cid version to 1 if used. (experimental)'
166+
describe: 'Hash function to use. Will set CID version to 1 if used. (experimental)'
163167
},
164168
quiet: {
165169
alias: 'q',
@@ -194,6 +198,7 @@ module.exports = {
194198
? argv.shardSplitThreshold
195199
: Infinity,
196200
cidVersion: argv.cidVersion,
201+
cidBase: argv.cidBase,
197202
rawLeaves: argv.rawLeaves,
198203
onlyHash: argv.onlyHash,
199204
hashAlg: argv.hash,
@@ -202,7 +207,7 @@ module.exports = {
202207
chunker: argv.chunker
203208
}
204209

205-
if (options.enableShardingExperiment && utils.isDaemonOn()) {
210+
if (options.enableShardingExperiment && isDaemonOn()) {
206211
throw new Error('Error: Enabling the sharding experiment should be done on the daemon')
207212
}
208213
const ipfs = argv.ipfs

src/cli/commands/ls.js

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

3-
const utils = require('../utils')
3+
const multibase = require('multibase')
4+
const { print, rightpad } = require('../utils')
45

56
module.exports = {
67
command: 'ls <key>',
@@ -24,16 +25,16 @@ module.exports = {
2425
desc: 'Resolve linked objects to find out their types. (not implemented yet)',
2526
type: 'boolean',
2627
default: false // should be true when implemented
28+
},
29+
'cid-base': {
30+
describe: 'Number base to display CIDs in.',
31+
type: 'string',
32+
choices: multibase.names
2733
}
2834
},
2935

3036
handler (argv) {
31-
let path = argv.key
32-
if (path.startsWith('/ipfs/')) {
33-
path = path.replace('/ipfs/', '')
34-
}
35-
36-
argv.ipfs.ls(path, { recursive: argv.recursive }, (err, links) => {
37+
argv.ipfs.ls(argv.key, { recursive: argv.recursive, cidBase: argv.cidBase }, (err, links) => {
3738
if (err) {
3839
throw err
3940
}
@@ -45,12 +46,18 @@ module.exports = {
4546
const multihashWidth = Math.max.apply(null, links.map((file) => file.hash.length))
4647
const sizeWidth = Math.max.apply(null, links.map((file) => String(file.size).length))
4748

49+
let pathParts = argv.key.split('/')
50+
51+
if (argv.key.startsWith('/ipfs/')) {
52+
pathParts = pathParts.slice(2)
53+
}
54+
4855
links.forEach(link => {
4956
const fileName = link.type === 'dir' ? `${link.name || ''}/` : link.name
50-
const padding = link.depth - path.split('/').length
51-
utils.print(
52-
utils.rightpad(link.hash, multihashWidth + 1) +
53-
utils.rightpad(link.size || '', sizeWidth + 1) +
57+
const padding = link.depth - pathParts.length
58+
print(
59+
rightpad(link.hash, multihashWidth + 1) +
60+
rightpad(link.size || '', sizeWidth + 1) +
5461
' '.repeat(padding) + fileName
5562
)
5663
})

src/cli/commands/object/get.js

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

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

58
module.exports = {
69
command: 'get <key>',
@@ -11,6 +14,11 @@ module.exports = {
1114
'data-encoding': {
1215
type: 'string',
1316
default: 'base64'
17+
},
18+
'cid-base': {
19+
describe: 'Number base to display CIDs in.',
20+
type: 'string',
21+
choices: multibase.names
1422
}
1523
},
1624

@@ -27,13 +35,13 @@ module.exports = {
2735

2836
const answer = {
2937
Data: nodeJSON.data,
30-
Hash: nodeJSON.multihash,
38+
Hash: cidToString(new CID(node.multihash), argv.cidBase),
3139
Size: nodeJSON.size,
3240
Links: nodeJSON.links.map((l) => {
3341
return {
3442
Name: l.name,
3543
Size: l.size,
36-
Hash: l.multihash
44+
Hash: cidToString(new CID(l.multihash), argv.cidBase)
3745
}
3846
})
3947
}

src/cli/commands/object/links.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
'use strict'
22

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

58
module.exports = {
69
command: 'links <key>',
710

811
describe: 'Outputs the links pointed to by the specified object',
912

10-
builder: {},
13+
builder: {
14+
'cid-base': {
15+
describe: 'Number base to display CIDs in.',
16+
type: 'string',
17+
choices: multibase.names
18+
}
19+
},
1120

1221
handler (argv) {
1322
argv.ipfs.object.links(argv.key, {
@@ -18,9 +27,10 @@ module.exports = {
1827
}
1928

2029
links.forEach((link) => {
30+
const cidStr = cidToString(new CID(link.multihash), argv.cidBase)
2131
link = link.toJSON()
2232

23-
print(`${link.multihash} ${link.size} ${link.name}`)
33+
print(`${cidStr} ${link.size} ${link.name}`)
2434
})
2535
})
2636
}

src/cli/commands/object/new.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,31 @@
33
const debug = require('debug')
44
const log = debug('cli:object')
55
log.error = debug('cli:object:error')
6-
const print = require('../../utils').print
6+
const CID = require('cids')
7+
const multibase = require('multibase')
8+
const { print } = require('../../utils')
9+
const { cidToString } = require('../../../utils/cid')
710

811
module.exports = {
912
command: 'new [<template>]',
1013

1114
describe: 'Create new ipfs objects',
1215

13-
builder: {},
16+
builder: {
17+
'cid-base': {
18+
describe: 'Number base to display CIDs in.',
19+
type: 'string',
20+
choices: multibase.names
21+
}
22+
},
1423

1524
handler (argv) {
1625
argv.ipfs.object.new(argv.template, (err, node) => {
1726
if (err) {
1827
throw err
1928
}
2029

21-
const nodeJSON = node.toJSON()
22-
23-
print(nodeJSON.multihash)
30+
print(cidToString(new CID(node.multihash), argv.cidBase))
2431
})
2532
}
2633
}

0 commit comments

Comments
 (0)