Skip to content

Commit 8c1ff5c

Browse files
committed
feat: replace hashAlg option with actual hasher
Instead of passing it a number, you now pass in a hasher implementation.
1 parent 95fc373 commit 8c1ff5c

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function * bufferImporter (file, block, options) {
2626
const opts = {
2727
codec: mc.DAG_PB,
2828
cidVersion: options.cidVersion,
29-
hashAlg: options.hashAlg,
29+
hasher: options.hasher,
3030
onlyHash: options.onlyHash
3131
}
3232

packages/ipfs-unixfs-importer/src/dag-builder/file/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,28 @@ const reduce = (file, block, options) => {
9696
const multihash = mh.decode(leaf.cid.multihash.bytes)
9797
buffer = encode(prepare({ Data: leaf.unixfs.marshal() }))
9898

99+
//// TODO vmx 2021-03-26: This is what the original code does, it checks
100+
//// the multihash of the original leaf node and uses then the same
101+
//// hasher. i wonder if that's really needed or if we could just use
102+
//// the hasher from `options.hasher` instead.
103+
//let hasher
104+
//switch multihash {
105+
// case sha256.code {
106+
// hasher = sha256
107+
// break;
108+
// }
109+
// //case identity.code {
110+
// // hasher = identity
111+
// // break;
112+
// //}
113+
// default: {
114+
// throw new Error(`Unsupported hasher "${multihash}"`)
115+
// }
116+
//}
99117
leaf.cid = await persist(buffer, block, {
100118
...options,
101119
codec: mc.DAG_PB,
102-
// @ts-ignore
103-
hashAlg: multihash.code,
120+
hasher: options.hasher,
104121
cidVersion: options.cidVersion
105122
})
106123
leaf.size = buffer.length

packages/ipfs-unixfs-importer/src/options.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })
44
const multihashing = require('multihashing-async')
5-
const mc = require('multicodec')
5+
const { sha256 } = require('multiformats/hashes/sha2')
66

77
/**
88
* @param {Uint8Array} buf
@@ -39,7 +39,7 @@ const defaultOptions = {
3939
rawLeaves: false,
4040
onlyHash: false,
4141
reduceSingleLeafToSelf: true,
42-
hashAlg: mc.SHA2_256,
42+
hasher: sha256,
4343
leafType: 'file', // 'raw'
4444
cidVersion: 0,
4545
progress: () => () => {},

packages/ipfs-unixfs-importer/src/types.d.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { UnixFS, Mtime } from 'ipfs-unixfs'
22
import CID from 'multiformats/cid'
33
import { HashName } from 'multihashes'
44
import { CodecName } from 'multicodec'
5+
import MultihashDigest from 'multiformats/hashes/hasher'
56

67
interface ImportCandidate {
78
path?: string
@@ -52,7 +53,7 @@ interface UserImporterOptions {
5253
rawLeaves?: boolean
5354
onlyHash?: boolean
5455
reduceSingleLeafToSelf?: boolean
55-
hashAlg?: CodecCode
56+
hasher?: MultihashDigest
5657
leafType?: 'file' | 'raw'
5758
cidVersion?: CIDVersion
5859
progress?: ProgressHandler
@@ -87,7 +88,7 @@ interface ImporterOptions {
8788
rawLeaves: boolean
8889
onlyHash: boolean
8990
reduceSingleLeafToSelf: boolean
90-
hashAlg: CodecCode
91+
hasher: MultihashDigest
9192
leafType: 'file' | 'raw'
9293
cidVersion: CIDVersion
9394
progress: ProgressHandler
@@ -133,7 +134,7 @@ export interface PersistOptions {
133134
//codec?: string
134135
codec?: number
135136
cidVersion: CIDVersion
136-
hashAlg: CodecCode
137+
hasher: MultihashDigest
137138
onlyHash: boolean
138139
preload?: boolean
139140
timeout?: number

packages/ipfs-unixfs-importer/src/utils/persist.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const mc = require('multicodec')
4-
const { sha256 } = require('multiformats/hashes/sha2')
54
const CID = require('multiformats/cid')
65

76
/**
@@ -10,30 +9,19 @@ const CID = require('multiformats/cid')
109
* @param {import('../types').PersistOptions} options
1110
*/
1211
const persist = async (buffer, block, options) => {
13-
if (!options.codec) {
14-
options.codec = mc.DAG_PB
15-
}
16-
17-
if (!options.cidVersion) {
18-
options.cidVersion = 0
12+
if (!options.hasher) {
13+
throw new Error(`Hasher must be specified.`)
1914
}
2015

21-
if (!options.hashAlg) {
22-
options.hashAlg = mc.SHA2_256
16+
if (!options.codec) {
17+
options.codec = mc.DAG_PB
2318
}
2419

25-
if (options.hashAlg !== mc.SHA2_256) {
20+
if (options.cidVersion === undefined) {
2621
options.cidVersion = 1
2722
}
2823

29-
let multihash
30-
switch (options.hashAlg) {
31-
case mc.SHA2_256:
32-
multihash = await sha256.digest(buffer)
33-
break
34-
default:
35-
throw(`TODO vmx 2021-02-24: support other hash algorithms. ${options.hashAlg} not found.`)
36-
}
24+
const multihash = await options.hasher.digest(buffer)
3725
// TODO vmx 2021-02-24: no idea why TypeScript fails here, it should work
3826
// @ts-ignore
3927
const cid = CID.create(options.cidVersion, options.codec, multihash)

0 commit comments

Comments
 (0)