diff --git a/cmd/hash.js b/cmd/hash.js index 2d3a9f7..ca823f9 100644 --- a/cmd/hash.js +++ b/cmd/hash.js @@ -1,6 +1,8 @@ import fs from 'fs' +import crypto from 'crypto' import { pipeline } from 'stream/promises' import { CID } from 'multiformats/cid' +import * as Digest from 'multiformats/hashes/digest' import { sha256 } from 'multiformats/hashes/sha2' /** CAR CID code */ @@ -8,21 +10,17 @@ const carCode = 0x0202 /** @param {string} carPath */ export default async function hash (carPath) { - let bytes - if (carPath) { - bytes = await fs.promises.readFile(carPath) - } else { - bytes = await pipeline( - process.stdin, - async (source) => { - const chunks = [] - for await (const chunk of source) { - chunks.push(chunk) - } - return Buffer.concat(chunks) + const hasher = crypto.createHash('sha256') + + await pipeline( + carPath ? fs.createReadStream(carPath) : process.stdin, + async (source) => { + for await (const chunk of source) { + hasher.update(chunk) } - ) - } + } + ) - console.log(CID.createV1(carCode, await sha256.digest(bytes)).toString()) + const digest = Digest.create(sha256.code, hasher.digest()) + console.log(CID.createV1(carCode, digest).toString()) } diff --git a/package-lock.json b/package-lock.json index 61e0083..5c8a98e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,11 +13,11 @@ "@ipld/dag-cbor": "^9.0.0", "@ipld/dag-json": "^10.0.1", "@ipld/dag-pb": "^4.0.2", - "@ipld/unixfs": "^2.1.1", + "@ipld/unixfs": "^3.0.0", "@web3-storage/car-block-validator": "^1.0.1", "files-from-path": "^1.0.0", "ipfs-unixfs-exporter": "^13.0.1", - "multiformats": "^11.0.2", + "multiformats": "^13.0.1", "sade": "^1.8.1", "varint": "^6.0.0" }, @@ -497,6 +497,15 @@ "npm": ">=7.0.0" } }, + "node_modules/@ipld/car/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/@ipld/dag-cbor": { "version": "9.0.0", "resolved": "https://registry.npmjs.org/@ipld/dag-cbor/-/dag-cbor-9.0.0.tgz", @@ -510,6 +519,15 @@ "npm": ">=7.0.0" } }, + "node_modules/@ipld/dag-cbor/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/@ipld/dag-json": { "version": "10.0.1", "resolved": "https://registry.npmjs.org/@ipld/dag-json/-/dag-json-10.0.1.tgz", @@ -523,6 +541,15 @@ "npm": ">=7.0.0" } }, + "node_modules/@ipld/dag-json/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/@ipld/dag-pb": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@ipld/dag-pb/-/dag-pb-4.0.2.tgz", @@ -535,17 +562,25 @@ "npm": ">=7.0.0" } }, + "node_modules/@ipld/dag-pb/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/@ipld/unixfs": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/@ipld/unixfs/-/unixfs-2.1.1.tgz", - "integrity": "sha512-g3gr/3XvfQs4x2VFjlICae09ul5fbWCKRInN6Vgeot2+GH0h/krr3PqZCIo4dy4Ou2mQOsIddxUvG8UZ4p9SbQ==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@ipld/unixfs/-/unixfs-3.0.0.tgz", + "integrity": "sha512-Tj3/BPOlnemcZQ2ETIZAO8hqAs9KNzWyX5J9+JCL9jDwvYwjxeYjqJ3v+9DusNvTBmJhZnGVP6ijUHrsuOLp+g==", "dependencies": { "@ipld/dag-pb": "^4.0.0", "@multiformats/murmur3": "^2.1.3", "@perma/map": "^1.0.2", - "@web-std/stream": "1.0.1", "actor": "^2.3.1", - "multiformats": "^11.0.1", + "multiformats": "^13.0.1", "protobufjs": "^7.1.2", "rabin-rs": "^2.1.0" } @@ -611,6 +646,15 @@ "npm": ">=7.0.0" } }, + "node_modules/@multiformats/murmur3/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/@multiformats/sha3": { "version": "2.0.15", "resolved": "https://registry.npmjs.org/@multiformats/sha3/-/sha3-2.0.15.tgz", @@ -745,14 +789,6 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.15.tgz", "integrity": "sha512-VkhBbVo2+2oozlkdHXLrb3zjsRkpdnaU2bXmX8Wgle3PUi569eLRaHGlgETQHR7lLL1w7GiG3h9SnePhxNDecw==" }, - "node_modules/@web-std/stream": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@web-std/stream/-/stream-1.0.1.tgz", - "integrity": "sha512-tsz4Y0WNDgFA5jwLSeV7/UV5rfMIlj0cPsSLVfTihjaVW0OJPd5NxJ3le1B3yLyqqzRpeG5OAfJAADLc4VoGTA==", - "dependencies": { - "web-streams-polyfill": "^3.1.1" - } - }, "node_modules/@web3-storage/car-block-validator": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@web3-storage/car-block-validator/-/car-block-validator-1.0.1.tgz", @@ -3188,6 +3224,15 @@ "npm": ">=7.0.0" } }, + "node_modules/interface-blockstore/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/interface-store": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/interface-store/-/interface-store-4.1.0.tgz", @@ -3253,6 +3298,15 @@ "npm": ">=7.0.0" } }, + "node_modules/ipfs-unixfs-exporter/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/is-arguments": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.1.tgz", @@ -4394,13 +4448,9 @@ "dev": true }, "node_modules/multiformats": { - "version": "11.0.2", - "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", - "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", - "engines": { - "node": ">=16.0.0", - "npm": ">=7.0.0" - } + "version": "13.0.1", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-13.0.1.tgz", + "integrity": "sha512-bt3R5iXe2O8xpp3wkmQhC73b/lC4S2ihU8Dndwcsysqbydqb8N+bpP116qMcClZ17g58iSIwtXUTcg2zT4sniA==" }, "node_modules/murmurhash3js-revisited": { "version": "3.0.0", @@ -5920,6 +5970,15 @@ "npm": ">=7.0.0" } }, + "node_modules/uint8arrays/node_modules/multiformats": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-11.0.2.tgz", + "integrity": "sha512-b5mYMkOkARIuVZCpvijFj9a6m5wMVLC7cf/jIPd5D/ARDOfLC5+IFkbgDXQgcU2goIsTD/O9NY4DI/Mt4OGvlg==", + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", @@ -6006,14 +6065,6 @@ "defaults": "^1.0.3" } }, - "node_modules/web-streams-polyfill": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", - "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==", - "engines": { - "node": ">= 8" - } - }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 4e6fdff..7d91b52 100644 --- a/package.json +++ b/package.json @@ -36,11 +36,11 @@ "@ipld/dag-cbor": "^9.0.0", "@ipld/dag-json": "^10.0.1", "@ipld/dag-pb": "^4.0.2", - "@ipld/unixfs": "^2.1.1", + "@ipld/unixfs": "^3.0.0", "@web3-storage/car-block-validator": "^1.0.1", "files-from-path": "^1.0.0", "ipfs-unixfs-exporter": "^13.0.1", - "multiformats": "^11.0.2", + "multiformats": "^13.0.1", "sade": "^1.8.1", "varint": "^6.0.0" },