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

Commit 28a3250

Browse files
authored
Merge pull request #671 from ipfs/feat/block-put-flags
CLI - add `block put` flags and `block get` CID support
2 parents 16ad10c + 0128488 commit 28a3250

File tree

9 files changed

+93
-41
lines changed

9 files changed

+93
-41
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"async": "^2.1.4",
7575
"bl": "^1.1.2",
7676
"boom": "^4.2.0",
77+
"cids": "^0.3.4",
7778
"debug": "^2.3.3",
7879
"fs-pull-blob-store": "^0.3.0",
7980
"glob": "^7.1.1",
@@ -102,6 +103,7 @@
102103
"mafmt": "^2.1.2",
103104
"multiaddr": "^2.1.1",
104105
"multihashes": "^0.3.0",
106+
"multihashing-async": "^0.3.0",
105107
"path-exists": "^3.0.0",
106108
"peer-book": "^0.3.0",
107109
"peer-id": "^0.8.0",

src/cli/commands/block/get.js

Lines changed: 6 additions & 6 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,17 +19,17 @@ 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

3129
if (block.data) {
32-
console.log(block.data.toString())
30+
// writing the buffer to stdout seems to be the only way
31+
// to send out binary data correctly
32+
process.stdout.write(block.data)
3333
return
3434
}
3535

src/cli/commands/block/put.js

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

33
const utils = require('../../utils')
4-
const mh = require('multihashes')
54
const bl = require('bl')
65
const fs = require('fs')
76
const Block = require('ipfs-block')
7+
const CID = require('cids')
8+
const multihashing = require('multihashing-async')
89
const waterfall = require('async/waterfall')
910
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 (buf, opts) {
15+
let block = new Block(buf)
16+
let cid
17+
1418
utils.getIPFS((err, ipfs) => {
1519
if (err) {
1620
throw err
1721
}
1822

1923
waterfall([
20-
(cb) => ipfs.block.put(new Block(buf), cb),
21-
(block, cb) => block.key(cb)
22-
], (err, key) => {
24+
(cb) => generateHash(block, opts, cb),
25+
(mhash, cb) => generateCid(mhash, block, opts, cb),
26+
(cb) => ipfs.block.put(block, cid, cb)
27+
], (err) => {
2328
if (err) {
2429
throw err
2530
}
2631

27-
console.log(mh.toB58String(key))
32+
console.log(cid.toBaseEncodedString())
2833
})
2934
})
35+
36+
function generateHash (block, opts, cb) {
37+
if (opts.mhlen === undefined) {
38+
multihashing(buf, opts.mhtype, cb)
39+
} else {
40+
multihashing(buf, opts.mhtype, opts.mhlen, cb)
41+
}
42+
}
43+
44+
function generateCid (mhash, block, opts, cb) {
45+
cid = new CID(opts.verison, opts.format, mhash)
46+
cb()
47+
}
3048
}
3149

3250
module.exports = {
3351
command: 'put [data]',
3452

3553
describe: 'Stores input as an IPFS block',
3654

37-
builder: {},
55+
builder: {
56+
format: {
57+
alias: 'f',
58+
describe: 'cid format for blocks to be created with.',
59+
default: 'v0'
60+
},
61+
mhtype: {
62+
describe: 'multihash hash function',
63+
default: 'sha2-256'
64+
},
65+
mhlen: {
66+
describe: 'multihash hash length',
67+
default: undefined
68+
}
69+
},
3870

3971
handler (argv) {
72+
// parse options
73+
if (argv.format === 'v0') {
74+
argv.verison = 0
75+
argv.format = 'dag-pb'
76+
argv.mhtype = 'sha2-256'
77+
} else {
78+
argv.verison = 1
79+
}
80+
4081
if (argv.data) {
41-
return addBlock(fs.readFileSync(argv.data))
82+
return addBlock(fs.readFileSync(argv.data), argv)
4283
}
4384

4485
process.stdin.pipe(bl((err, input) => {
4586
if (err) {
4687
throw err
4788
}
4889

49-
addBlock(input)
90+
addBlock(input, argv)
5091
}))
5192
}
5293
}

src/cli/commands/block/stat.js

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

33
const utils = require('../../utils')
4+
const CID = require('cids')
45
const debug = require('debug')
56
const log = debug('cli:block')
67
log.error = debug('cli:block:error')
@@ -13,12 +14,14 @@ module.exports = {
1314
builder: {},
1415

1516
handler (argv) {
17+
const cid = new CID(argv.key)
18+
1619
utils.getIPFS((err, ipfs) => {
1720
if (err) {
1821
throw err
1922
}
2023

21-
ipfs.block.stat(argv.key, (err, stats) => {
24+
ipfs.block.stat(cid, (err, stats) => {
2225
if (err) {
2326
throw err
2427
}

src/core/components/dag.js

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

33
const promisify = require('promisify-es6')
4-
// const dagPB = require('ipld-dag-pb')
5-
// const dagCBOR = require('ipld-dag-cbor')
64

7-
// const CID = require('cids')
8-
// const mh = require('multihashes')
5+
const dagPB = require('ipld-dag-pb')
6+
const dagCBOR = require('ipld-dag-cbor')
97

108
module.exports = function dag (self) {
119
return {
1210
put: promisify((dagNode, multicodec, hashAlg, callback) => {
13-
// TODO
14-
// serialize
15-
// get right hash
16-
// generate cid
17-
// put in IPLD Resolver
18-
19-
/*
20-
self._ipldResolver.put({
21-
node: node,
22-
cid: new CID(node.multihash)
23-
}
24-
*/
2511
switch (multicodec) {
26-
case 'dag-pb': {} break
27-
case 'dag-cbor': {} break
28-
default:
29-
callback(new Error('IPLD Format not supported'))
12+
case 'dag-pb': dagPB.util.cid(dagNode, gotCid); break
13+
case 'dag-cbor': dagCBOR.util.cid(dagNode, gotCid); break
14+
default: callback(new Error('IPLD Format not supported'))
15+
}
16+
17+
function gotCid (err, cid) {
18+
if (err) {
19+
return callback(err)
20+
}
21+
self._ipldResolver.put({
22+
node: dagNode,
23+
cid: cid
24+
}, callback)
3025
}
3126
}),
3227
get: promisify((cid, callback) => {
33-
self.ipldResolver.get(cid, callback)
28+
self._ipldResolver.get(cid, callback)
3429
}),
3530
rm: promisify((cid, callback) => {
3631
// TODO once pinning is complete, this remove operation has to first
3732
// verify that some pinning chain is not broken with the operation
38-
self.ipldResolver.remove(cid, callback)
33+
self._ipldResolver.remove(cid, callback)
3934
}),
4035
resolve: promisify((cid, path, callback) => {
41-
self.ipldResolver.resolve(cid, path, callback)
36+
self._ipldResolver.resolve(cid, path, callback)
4237
})
4338
}
4439
}

src/core/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const bootstrap = require('./components/bootstrap')
1818
const config = require('./components/config')
1919
const block = require('./components/block')
2020
const object = require('./components/object')
21+
const dag = require('./components/dag')
2122
const libp2p = require('./components/libp2p')
2223
const swarm = require('./components/swarm')
2324
const ping = require('./components/ping')
@@ -62,6 +63,7 @@ function IPFS (repoInstance) {
6263
this.config = config(this)
6364
this.block = block(this)
6465
this.object = object(this)
66+
this.dag = dag(this)
6567
this.libp2p = libp2p(this)
6668
this.swarm = swarm(this)
6769
this.files = files(this)

test/cli/test-block.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ describe('block', () => {
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.be.eql(
22+
'z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS'
23+
)
24+
})
25+
})
26+
1927
it('get', () => {
2028
return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
21-
expect(out).to.be.eql('hello world\n')
29+
expect(out).to.be.eql('hello world')
2230
})
2331
})
2432

test/core/both/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
'use strict'
33

44
describe('--both', () => {
5-
require('./test-bitswap')
65
require('./test-block')
76
require('./test-bootstrap')
87
require('./test-config')
98
require('./test-files')
109
require('./test-generic')
1110
require('./test-init')
1211
require('./test-object')
12+
require('./test-dag')
13+
require('./test-bitswap')
1314
})

test/test-data/eth-block

534 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)