From 273079fa3876f7fd17593cc0791bd0fcfef43c77 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 8 Dec 2023 06:51:38 +0000 Subject: [PATCH] feat(cli): check block hash consistency when listing blocks (#157) --- bin.js | 1 + cmd/blocks.js | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/bin.js b/bin.js index ed29ea0..bca2547 100755 --- a/bin.js +++ b/bin.js @@ -47,6 +47,7 @@ cli cli .command('blocks [car]') .describe('List block CIDs from a CAR.') + .option('--verify', 'Verify block hash consistency.', true) .action(createAction('./cmd/blocks.js')) cli diff --git a/cmd/blocks.js b/cmd/blocks.js index 83240a5..422fe71 100644 --- a/cmd/blocks.js +++ b/cmd/blocks.js @@ -1,10 +1,20 @@ import fs from 'fs' -import { CarCIDIterator } from '@ipld/car/iterator' +import { validateBlock } from '@web3-storage/car-block-validator' +import { CarBlockIterator } from '@ipld/car/iterator' -/** @param {string} carPath */ -export default async function blocksList (carPath) { - const cids = await CarCIDIterator.fromIterable(carPath ? fs.createReadStream(carPath) : process.stdin) - for await (const cid of cids) { - console.log(cid.toString()) +/** + * @param {string} carPath + * @param {object} [opts] + * @param {boolean} [opts.verify] + */ +export default async function blocksList (carPath, opts) { + const blocks = await CarBlockIterator.fromIterable(carPath ? fs.createReadStream(carPath) : process.stdin) + for await (const block of blocks) { + /* c8 ignore next */ + if (opts?.verify || opts?.verify == null) { + // @ts-expect-error + await validateBlock(block) + } + console.log(block.cid.toString()) } }