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

Commit d9833f8

Browse files
committed
feat: add CLI support for different hash func and type
1 parent d3dc3d3 commit d9833f8

File tree

5 files changed

+69
-49
lines changed

5 files changed

+69
-49
lines changed

src/cli/commands/block/get.js

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

33
const utils = require('../../utils')
4-
const mh = require('multihashes')
4+
const CID = require('cids')
55
const debug = require('debug')
66
const log = debug('cli:block')
77
log.error = debug('cli:block:error')
@@ -19,21 +19,15 @@ module.exports = {
1919
throw err
2020
}
2121

22-
const hash = utils.isDaemonOn()
23-
? argv.key
24-
: mh.fromB58String(argv.key)
22+
const cid = new CID(argv.key)
2523

26-
ipfs.block.get(hash, (err, block) => {
24+
ipfs.block.get(cid, (err, block) => {
2725
if (err) {
2826
throw err
2927
}
3028

31-
if (block.data) {
32-
console.log(block.data.toString())
33-
return
34-
}
35-
36-
console.log(block.toString())
29+
process.stdout.write(block.data)
30+
process.stdout.write('\n')
3731
})
3832
})
3933
}

src/cli/commands/block/put.js

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

33
const utils = require('../../utils')
4-
const mh = require('multihashes')
4+
const CID = require('cids')
5+
const multihashing = require('multihashing-async')
56
const bl = require('bl')
67
const fs = require('fs')
78
const Block = require('ipfs-block')
@@ -10,43 +11,66 @@ const debug = require('debug')
1011
const log = debug('cli:block')
1112
log.error = debug('cli:block:error')
1213

13-
function addBlock (buf) {
14+
function addBlock (data, opts) {
1415
utils.getIPFS((err, ipfs) => {
1516
if (err) {
1617
throw err
1718
}
1819

20+
let cid
21+
1922
waterfall([
20-
(cb) => ipfs.block.put(new Block(buf), cb),
21-
(block, cb) => block.key(cb)
22-
], (err, key) => {
23-
if (err) {
24-
throw err
25-
}
23+
(cb) => multihashing(data, opts.mhtype || 'sha2-256', cb),
24+
(multihash, cb) => {
25+
if (!opts.version || opts.version !== 0) {
26+
cid = new CID(1, opts.format || 'dag-pb', multihash)
27+
} else {
28+
cid = new CID(0, 'dag-pb', multihash)
29+
}
30+
cb(null, cid)
31+
},
32+
(cid, cb) => ipfs.block.put(new Block(data), cid, cb)
33+
], (err) => {
34+
if (err) { throw err }
2635

27-
console.log(mh.toB58String(key))
36+
// console.log(cid)
37+
console.log(cid.toBaseEncodedString())
2838
})
2939
})
3040
}
3141

3242
module.exports = {
33-
command: 'put [data]',
43+
command: 'put [block]',
3444

3545
describe: 'Stores input as an IPFS block',
3646

37-
builder: {},
47+
builder: {
48+
format: {
49+
alias: 'f',
50+
describe: 'cid format for blocks to be created with.',
51+
default: 'dag-pb'
52+
},
53+
mhtype: {
54+
describe: 'multihash hash function',
55+
default: 'sha2-256'
56+
},
57+
mhlen: {
58+
describe: 'multihash hash length',
59+
default: undefined
60+
}
61+
},
3862

3963
handler (argv) {
40-
if (argv.data) {
41-
return addBlock(fs.readFileSync(argv.data))
64+
if (argv.block) {
65+
return addBlock(fs.readFileSync(argv.block), argv)
4266
}
4367

4468
process.stdin.pipe(bl((err, input) => {
4569
if (err) {
4670
throw err
4771
}
4872

49-
addBlock(input)
73+
addBlock(input, argv)
5074
}))
5175
}
5276
}

src/cli/commands/block/stat.js

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

33
const utils = require('../../utils')
44
const debug = require('debug')
5+
const CID = require('cids')
56
const log = debug('cli:block')
67
log.error = debug('cli:block:error')
78

@@ -14,14 +15,11 @@ module.exports = {
1415

1516
handler (argv) {
1617
utils.getIPFS((err, ipfs) => {
17-
if (err) {
18-
throw err
19-
}
18+
if (err) { throw err }
19+
const cid = new CID(argv.key)
2020

21-
ipfs.block.stat(argv.key, (err, stats) => {
22-
if (err) {
23-
throw err
24-
}
21+
ipfs.block.stat(cid, (err, stats) => {
22+
if (err) { throw err }
2523

2624
console.log('Key:', stats.key)
2725
console.log('Size:', stats.size)

test/cli/test-block.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,37 @@ describe('block', () => {
1010
describeOnlineAndOffline(repoPath, () => {
1111
it('put', () => {
1212
return ipfs('block put test/test-data/hello').then((out) => {
13-
expect(out).to.be.eql(
14-
'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
15-
)
13+
expect(out).to.eql('zdj7Wgpi9yzsvjJerghrdhPFpe1p1jZFyB5GKLyXEzFQyaxVk')
1614
})
1715
})
1816

17+
it('put with flags, format and mhtype', () => {
18+
return ipfs('block put --format eth-block --mhtype keccak-256 test/test-data/eth-block')
19+
.then((out) => expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS'))
20+
})
21+
1922
it('get', () => {
20-
return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
21-
expect(out).to.be.eql('hello world\n')
22-
})
23+
return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
24+
.then((out) => expect(out).to.eql('hello world\n'))
2325
})
2426

2527
it('stat', () => {
26-
return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
27-
expect(out).to.be.eql([
28-
'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
29-
'Size: 12'
30-
].join('\n'))
31-
})
28+
return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
29+
.then((out) => {
30+
expect(out).to.eql([
31+
'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
32+
'Size: 12'
33+
].join('\n'))
34+
})
3235
})
3336

3437
it.skip('rm', () => {
35-
return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
36-
expect(out).to.be.eql(
37-
'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
38-
)
39-
})
38+
return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
39+
.then((out) => {
40+
expect(out).to.eql(
41+
'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
42+
)
43+
})
4044
})
4145
})
4246
})

test/test-data/eth-block

534 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)