Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 709831f

Browse files
authored
fix: override hashing algorithm when importing files (#4042)
Looks like this got missed during the multiformats migration. Fixes #3952
1 parent 383dc07 commit 709831f

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

.github/workflows/examples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ jobs:
120120
deps: ipfs-core@$PWD/packages/ipfs-core/dist
121121
- name: types with typed js
122122
repo: https://github.com/ipfs-examples/js-ipfs-types-use-ipfs-from-typed-js.git
123-
deps: ipfs-core@$PWD/packages/ipfs-core/dist,ipfs-core-types@$PWD/packages/ipfs-core-types/dist
123+
deps: ipfs-core@$PWD/packages/ipfs-core/dist
124124
steps:
125125
- uses: actions/checkout@v2
126126
- uses: actions/setup-node@v2

packages/interface-ipfs-core/src/add-all.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import bufferStream from 'it-buffer-stream'
1515
import * as raw from 'multiformats/codecs/raw'
1616
import * as dagPB from '@ipld/dag-pb'
1717
import resolve from 'aegir/utils/resolve.js'
18+
import { sha256, sha512 } from 'multiformats/hashes/sha2'
1819

1920
/**
2021
* @typedef {import('ipfsd-ctl').Factory} Factory
@@ -479,6 +480,22 @@ export function testAddAll (factory, options) {
479480
.and.to.have.property('name').that.equals('TimeoutError')
480481
})
481482

483+
it('should add all with sha2-256 by default', async function () {
484+
const content = String(Math.random() + Date.now())
485+
486+
const files = await all(ipfs.addAll([content]))
487+
488+
expect(files).to.have.nested.property('[0].cid.multihash.code', sha256.code)
489+
})
490+
491+
it('should add all with a different hashing algorithm', async function () {
492+
const content = String(Math.random() + Date.now())
493+
494+
const files = await all(ipfs.addAll([content], { hashAlg: 'sha2-512' }))
495+
496+
expect(files).to.have.nested.property('[0].cid.multihash.code', sha512.code)
497+
})
498+
482499
it('should respect raw leaves when file is smaller than one block and no metadata is present', async () => {
483500
const files = await all(ipfs.addAll([Uint8Array.from([0, 1, 2])], {
484501
cidVersion: 1,

packages/interface-ipfs-core/src/add.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1111
import last from 'it-last'
1212
import * as raw from 'multiformats/codecs/raw'
1313
import * as dagPB from '@ipld/dag-pb'
14+
import { sha256, sha512 } from 'multiformats/hashes/sha2'
1415

1516
const echoUrl = (/** @type {string} */ text) => `${process.env.ECHO_SERVER}/download?data=${encodeURIComponent(text)}`
1617
const redirectUrl = (/** @type {string} */ url) => `${process.env.ECHO_SERVER}/redirect?to=${encodeURI(url)}`
@@ -274,6 +275,22 @@ export function testAdd (factory, options) {
274275
.and.to.have.property('name').that.equals('TimeoutError')
275276
})
276277

278+
it('should add with sha2-256 by default', async function () {
279+
const content = String(Math.random() + Date.now())
280+
281+
const file = await ipfs.add(content)
282+
283+
expect(file).to.have.nested.property('cid.multihash.code', sha256.code)
284+
})
285+
286+
it('should add with a different hashing algorithm', async function () {
287+
const content = String(Math.random() + Date.now())
288+
289+
const file = await ipfs.add(content, { hashAlg: 'sha2-512' })
290+
291+
expect(file).to.have.nested.property('cid.multihash.code', sha512.code)
292+
})
293+
277294
it('should add with mode as string', async function () {
278295
// @ts-ignore this is mocha
279296
this.slow(10 * 1000)

packages/ipfs-core/src/components/add-all/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
99
/**
1010
* @typedef {import('multiformats/cid').CID} CID
1111
* @typedef {import('ipfs-unixfs-importer').ImportResult} ImportResult
12+
* @typedef {import('multiformats/hashes/interface').MultihashHasher} MultihashHasher
13+
* @typedef {import('ipfs-core-utils/multihashes').Multihashes} Multihashes
1214
*/
1315

1416
/**
1517
* @typedef {Object} Context
1618
* @property {import('ipfs-repo').IPFSRepo} repo
1719
* @property {import('../../types').Preload} preload
20+
* @property {Multihashes} hashers
1821
* @property {import('ipfs-core-types/src/root').ShardingOptions} [options]
1922
* @param {Context} context
2023
*/
21-
export function createAddAll ({ repo, preload, options }) {
24+
export function createAddAll ({ repo, preload, hashers, options }) {
2225
const isShardingEnabled = options && options.sharding
2326

2427
/**
@@ -81,13 +84,21 @@ export function createAddAll ({ repo, preload, options }) {
8184
}
8285
}
8386

87+
/** @type {MultihashHasher | undefined} */
88+
let hasher
89+
90+
if (opts.hashAlg != null) {
91+
hasher = await hashers.getHasher(opts.hashAlg)
92+
}
93+
8494
const iterator = pipe(
8595
normaliseInput(source),
8696
/**
8797
* @param {AsyncIterable<import('ipfs-unixfs-importer').ImportCandidate>} source
8898
*/
8999
source => importer(source, repo.blocks, {
90100
...opts,
101+
hasher,
91102
pin: false
92103
}),
93104
transformFile(opts),

packages/ipfs-core/src/components/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class IPFS {
126126
const { add, addAll, cat, get, ls } = new RootAPI({
127127
preload,
128128
repo,
129-
options: options.EXPERIMENTAL
129+
options: options.EXPERIMENTAL,
130+
hashers: this.hashers
130131
})
131132

132133
const files = createFiles({

packages/ipfs-core/src/components/root.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ export class RootAPI {
1515
/**
1616
* @param {Context} context
1717
*/
18-
constructor ({ preload, repo, options }) {
18+
constructor ({ preload, repo, hashers, options }) {
1919
const addAll = createAddAll({
2020
preload,
2121
repo,
22-
options
22+
options,
23+
hashers
2324
})
2425

2526
this.addAll = addAll

0 commit comments

Comments
 (0)