Skip to content

Commit

Permalink
feat: replace hashAlg option with actual hasher
Browse files Browse the repository at this point in the history
Instead of passing it a number, you now pass in a hasher implementation.
  • Loading branch information
vmx committed Mar 26, 2021
1 parent 95fc373 commit 8c1ff5c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
21 changes: 19 additions & 2 deletions packages/ipfs-unixfs-importer/src/dag-builder/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-unixfs-importer/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,7 +39,7 @@ const defaultOptions = {
rawLeaves: false,
onlyHash: false,
reduceSingleLeafToSelf: true,
hashAlg: mc.SHA2_256,
hasher: sha256,
leafType: 'file', // 'raw'
cidVersion: 0,
progress: () => () => {},
Expand Down
7 changes: 4 additions & 3 deletions packages/ipfs-unixfs-importer/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +53,7 @@ interface UserImporterOptions {
rawLeaves?: boolean
onlyHash?: boolean
reduceSingleLeafToSelf?: boolean
hashAlg?: CodecCode
hasher?: MultihashDigest
leafType?: 'file' | 'raw'
cidVersion?: CIDVersion
progress?: ProgressHandler
Expand Down Expand Up @@ -87,7 +88,7 @@ interface ImporterOptions {
rawLeaves: boolean
onlyHash: boolean
reduceSingleLeafToSelf: boolean
hashAlg: CodecCode
hasher: MultihashDigest
leafType: 'file' | 'raw'
cidVersion: CIDVersion
progress: ProgressHandler
Expand Down Expand Up @@ -133,7 +134,7 @@ export interface PersistOptions {
//codec?: string
codec?: number
cidVersion: CIDVersion
hashAlg: CodecCode
hasher: MultihashDigest
onlyHash: boolean
preload?: boolean
timeout?: number
Expand Down
24 changes: 6 additions & 18 deletions packages/ipfs-unixfs-importer/src/utils/persist.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const mc = require('multicodec')
const { sha256 } = require('multiformats/hashes/sha2')
const CID = require('multiformats/cid')

/**
Expand All @@ -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)
Expand Down

0 comments on commit 8c1ff5c

Please sign in to comment.