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

Commit fa6053c

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

File tree

5 files changed

+57
-36
lines changed

5 files changed

+57
-36
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: 37 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,65 @@ 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, cb),
24+
(multihash, cb) => {
25+
if (opts.version) {
26+
cid = new CID(opts.version, opts.format, multihash)
27+
} else {
28+
cid = new CID(multihash)
29+
}
30+
cb(null, cid)
31+
},
32+
(cid, cb) => ipfs.block.put(new Block(data), cb)
33+
], (err) => {
34+
if (err) { throw err }
2635

27-
console.log(mh.toB58String(key))
36+
console.log(cid.toBaseEncodedString())
2837
})
2938
})
3039
}
3140

3241
module.exports = {
33-
command: 'put [data]',
42+
command: 'put [block]',
3443

3544
describe: 'Stores input as an IPFS block',
3645

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

3962
handler (argv) {
40-
if (argv.data) {
41-
return addBlock(fs.readFileSync(argv.data))
63+
if (argv.block) {
64+
return addBlock(fs.readFileSync(argv.block), argv)
4265
}
4366

4467
process.stdin.pipe(bl((err, input) => {
4568
if (err) {
4669
throw err
4770
}
4871

49-
addBlock(input)
72+
addBlock(input, argv)
5073
}))
5174
}
5275
}

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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ 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(
13+
expect(out).to.eql(
1414
'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
1515
)
1616
})
1717
})
1818

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

2531
it('stat', () => {
2632
return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
27-
expect(out).to.be.eql([
33+
expect(out).to.eql([
2834
'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
2935
'Size: 12'
3036
].join('\n'))
@@ -33,7 +39,7 @@ describe('block', () => {
3339

3440
it.skip('rm', () => {
3541
return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
36-
expect(out).to.be.eql(
42+
expect(out).to.eql(
3743
'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
3844
)
3945
})

test/test-data/eth-block

534 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)