From 8c1ff5c9bbac20d84148730b6ffc82cfdf5b9078 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Fri, 26 Mar 2021 15:35:58 +0100 Subject: [PATCH] feat: replace hashAlg option with actual hasher Instead of passing it a number, you now pass in a hasher implementation. --- .../src/dag-builder/file/buffer-importer.js | 2 +- .../src/dag-builder/file/index.js | 21 ++++++++++++++-- packages/ipfs-unixfs-importer/src/options.js | 4 ++-- packages/ipfs-unixfs-importer/src/types.d.ts | 7 +++--- .../ipfs-unixfs-importer/src/utils/persist.js | 24 +++++-------------- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js index 147d97af..e1b86524 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js @@ -26,7 +26,7 @@ async function * bufferImporter (file, block, options) { const opts = { codec: mc.DAG_PB, cidVersion: options.cidVersion, - hashAlg: options.hashAlg, + hasher: options.hasher, onlyHash: options.onlyHash } diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js index 3df0b0ab..6ee30e90 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/index.js @@ -96,11 +96,28 @@ const reduce = (file, block, options) => { const multihash = mh.decode(leaf.cid.multihash.bytes) buffer = encode(prepare({ Data: leaf.unixfs.marshal() })) + //// TODO vmx 2021-03-26: This is what the original code does, it checks + //// the multihash of the original leaf node and uses then the same + //// hasher. i wonder if that's really needed or if we could just use + //// the hasher from `options.hasher` instead. + //let hasher + //switch multihash { + // case sha256.code { + // hasher = sha256 + // break; + // } + // //case identity.code { + // // hasher = identity + // // break; + // //} + // default: { + // throw new Error(`Unsupported hasher "${multihash}"`) + // } + //} leaf.cid = await persist(buffer, block, { ...options, codec: mc.DAG_PB, - // @ts-ignore - hashAlg: multihash.code, + hasher: options.hasher, cidVersion: options.cidVersion }) leaf.size = buffer.length diff --git a/packages/ipfs-unixfs-importer/src/options.js b/packages/ipfs-unixfs-importer/src/options.js index 6f1b0fdc..6dd51a45 100644 --- a/packages/ipfs-unixfs-importer/src/options.js +++ b/packages/ipfs-unixfs-importer/src/options.js @@ -2,7 +2,7 @@ const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) const multihashing = require('multihashing-async') -const mc = require('multicodec') +const { sha256 } = require('multiformats/hashes/sha2') /** * @param {Uint8Array} buf @@ -39,7 +39,7 @@ const defaultOptions = { rawLeaves: false, onlyHash: false, reduceSingleLeafToSelf: true, - hashAlg: mc.SHA2_256, + hasher: sha256, leafType: 'file', // 'raw' cidVersion: 0, progress: () => () => {}, diff --git a/packages/ipfs-unixfs-importer/src/types.d.ts b/packages/ipfs-unixfs-importer/src/types.d.ts index 98464a0b..5b0c44cc 100644 --- a/packages/ipfs-unixfs-importer/src/types.d.ts +++ b/packages/ipfs-unixfs-importer/src/types.d.ts @@ -2,6 +2,7 @@ import { UnixFS, Mtime } from 'ipfs-unixfs' import CID from 'multiformats/cid' import { HashName } from 'multihashes' import { CodecName } from 'multicodec' +import MultihashDigest from 'multiformats/hashes/hasher' interface ImportCandidate { path?: string @@ -52,7 +53,7 @@ interface UserImporterOptions { rawLeaves?: boolean onlyHash?: boolean reduceSingleLeafToSelf?: boolean - hashAlg?: CodecCode + hasher?: MultihashDigest leafType?: 'file' | 'raw' cidVersion?: CIDVersion progress?: ProgressHandler @@ -87,7 +88,7 @@ interface ImporterOptions { rawLeaves: boolean onlyHash: boolean reduceSingleLeafToSelf: boolean - hashAlg: CodecCode + hasher: MultihashDigest leafType: 'file' | 'raw' cidVersion: CIDVersion progress: ProgressHandler @@ -133,7 +134,7 @@ export interface PersistOptions { //codec?: string codec?: number cidVersion: CIDVersion - hashAlg: CodecCode + hasher: MultihashDigest onlyHash: boolean preload?: boolean timeout?: number diff --git a/packages/ipfs-unixfs-importer/src/utils/persist.js b/packages/ipfs-unixfs-importer/src/utils/persist.js index c31b2d63..6be0d8f9 100644 --- a/packages/ipfs-unixfs-importer/src/utils/persist.js +++ b/packages/ipfs-unixfs-importer/src/utils/persist.js @@ -1,7 +1,6 @@ 'use strict' const mc = require('multicodec') -const { sha256 } = require('multiformats/hashes/sha2') const CID = require('multiformats/cid') /** @@ -10,30 +9,19 @@ const CID = require('multiformats/cid') * @param {import('../types').PersistOptions} options */ const persist = async (buffer, block, options) => { - if (!options.codec) { - options.codec = mc.DAG_PB - } - - if (!options.cidVersion) { - options.cidVersion = 0 + if (!options.hasher) { + throw new Error(`Hasher must be specified.`) } - if (!options.hashAlg) { - options.hashAlg = mc.SHA2_256 + if (!options.codec) { + options.codec = mc.DAG_PB } - if (options.hashAlg !== mc.SHA2_256) { + if (options.cidVersion === undefined) { options.cidVersion = 1 } - let multihash - switch (options.hashAlg) { - case mc.SHA2_256: - multihash = await sha256.digest(buffer) - break - default: - throw(`TODO vmx 2021-02-24: support other hash algorithms. ${options.hashAlg} not found.`) - } + const multihash = await options.hasher.digest(buffer) // TODO vmx 2021-02-24: no idea why TypeScript fails here, it should work // @ts-ignore const cid = CID.create(options.cidVersion, options.codec, multihash)