From a6c522f209fdb86e2a8cdb4517e42e7c29b0c5b5 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 31 Jan 2017 19:44:06 +0000 Subject: [PATCH] feat: add CLI support for different hash func and type (#748) --- src/cli/commands/block/get.js | 16 ++++------ src/cli/commands/block/put.js | 52 ++++++++++++++++++++++++--------- src/cli/commands/block/stat.js | 12 ++++---- test/cli/test-block.js | 38 +++++++++++++----------- test/test-data/eth-block | Bin 0 -> 534 bytes 5 files changed, 69 insertions(+), 49 deletions(-) create mode 100644 test/test-data/eth-block diff --git a/src/cli/commands/block/get.js b/src/cli/commands/block/get.js index f042eb0632..e8a653be0f 100644 --- a/src/cli/commands/block/get.js +++ b/src/cli/commands/block/get.js @@ -1,7 +1,7 @@ 'use strict' const utils = require('../../utils') -const mh = require('multihashes') +const CID = require('cids') const debug = require('debug') const log = debug('cli:block') log.error = debug('cli:block:error') @@ -19,21 +19,15 @@ module.exports = { throw err } - const hash = utils.isDaemonOn() - ? argv.key - : mh.fromB58String(argv.key) + const cid = new CID(argv.key) - ipfs.block.get(hash, (err, block) => { + ipfs.block.get(cid, (err, block) => { if (err) { throw err } - if (block.data) { - console.log(block.data.toString()) - return - } - - console.log(block.toString()) + process.stdout.write(block.data) + process.stdout.write('\n') }) }) } diff --git a/src/cli/commands/block/put.js b/src/cli/commands/block/put.js index cdd47a7776..14895e96d7 100644 --- a/src/cli/commands/block/put.js +++ b/src/cli/commands/block/put.js @@ -1,7 +1,8 @@ 'use strict' const utils = require('../../utils') -const mh = require('multihashes') +const CID = require('cids') +const multihashing = require('multihashing-async') const bl = require('bl') const fs = require('fs') const Block = require('ipfs-block') @@ -10,35 +11,58 @@ const debug = require('debug') const log = debug('cli:block') log.error = debug('cli:block:error') -function addBlock (buf) { +function addBlock (data, opts) { utils.getIPFS((err, ipfs) => { if (err) { throw err } + let cid + waterfall([ - (cb) => ipfs.block.put(new Block(buf), cb), - (block, cb) => block.key(cb) - ], (err, key) => { - if (err) { - throw err - } + (cb) => multihashing(data, opts.mhtype || 'sha2-256', cb), + (multihash, cb) => { + if (!opts.version || opts.version !== 0) { + cid = new CID(1, opts.format || 'dag-pb', multihash) + } else { + cid = new CID(0, 'dag-pb', multihash) + } + cb(null, cid) + }, + (cid, cb) => ipfs.block.put(new Block(data), cid, cb) + ], (err) => { + if (err) { throw err } - console.log(mh.toB58String(key)) + // console.log(cid) + console.log(cid.toBaseEncodedString()) }) }) } module.exports = { - command: 'put [data]', + command: 'put [block]', describe: 'Stores input as an IPFS block', - builder: {}, + builder: { + format: { + alias: 'f', + describe: 'cid format for blocks to be created with.', + default: 'dag-pb' + }, + mhtype: { + describe: 'multihash hash function', + default: 'sha2-256' + }, + mhlen: { + describe: 'multihash hash length', + default: undefined + } + }, handler (argv) { - if (argv.data) { - return addBlock(fs.readFileSync(argv.data)) + if (argv.block) { + return addBlock(fs.readFileSync(argv.block), argv) } process.stdin.pipe(bl((err, input) => { @@ -46,7 +70,7 @@ module.exports = { throw err } - addBlock(input) + addBlock(input, argv) })) } } diff --git a/src/cli/commands/block/stat.js b/src/cli/commands/block/stat.js index ab7887c75b..73c82aff9e 100644 --- a/src/cli/commands/block/stat.js +++ b/src/cli/commands/block/stat.js @@ -2,6 +2,7 @@ const utils = require('../../utils') const debug = require('debug') +const CID = require('cids') const log = debug('cli:block') log.error = debug('cli:block:error') @@ -14,14 +15,11 @@ module.exports = { handler (argv) { utils.getIPFS((err, ipfs) => { - if (err) { - throw err - } + if (err) { throw err } + const cid = new CID(argv.key) - ipfs.block.stat(argv.key, (err, stats) => { - if (err) { - throw err - } + ipfs.block.stat(cid, (err, stats) => { + if (err) { throw err } console.log('Key:', stats.key) console.log('Size:', stats.size) diff --git a/test/cli/test-block.js b/test/cli/test-block.js index 6beb8b3076..4e9db386cc 100644 --- a/test/cli/test-block.js +++ b/test/cli/test-block.js @@ -10,33 +10,37 @@ describe('block', () => { describeOnlineAndOffline(repoPath, () => { it('put', () => { return ipfs('block put test/test-data/hello').then((out) => { - expect(out).to.be.eql( - 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' - ) + expect(out).to.eql('zdj7Wgpi9yzsvjJerghrdhPFpe1p1jZFyB5GKLyXEzFQyaxVk') }) }) + it('put with flags, format and mhtype', () => { + return ipfs('block put --format eth-block --mhtype keccak-256 test/test-data/eth-block') + .then((out) => expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS')) + }) + it('get', () => { - return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => { - expect(out).to.be.eql('hello world\n') - }) + return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') + .then((out) => expect(out).to.eql('hello world\n')) }) it('stat', () => { - return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => { - expect(out).to.be.eql([ - 'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', - 'Size: 12' - ].join('\n')) - }) + return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') + .then((out) => { + expect(out).to.eql([ + 'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', + 'Size: 12' + ].join('\n')) + }) }) it.skip('rm', () => { - return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => { - expect(out).to.be.eql( - 'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' - ) - }) + return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') + .then((out) => { + expect(out).to.eql( + 'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' + ) + }) }) }) }) diff --git a/test/test-data/eth-block b/test/test-data/eth-block new file mode 100644 index 0000000000000000000000000000000000000000..f2b366dc93835883562d5cf2d82c70956abaffb5 GIT binary patch literal 534 zcmey#B)s6nfrZ7aYhTUY?LA|uuFHpm?)@zoO33eH@lkG$zUU>CA2Ua^S;(b2}$~2@hQGZ}NgyT?W2; z^Jcb37Ow?U|lQGf$LV=|nl~px@ IMl5X30Df$PU;qFB literal 0 HcmV?d00001