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

Commit

Permalink
fix: override hashing algorithm when importing files (#4042)
Browse files Browse the repository at this point in the history
Looks like this got missed during the multiformats migration.

Fixes #3952
  • Loading branch information
achingbrain committed Feb 5, 2022
1 parent 383dc07 commit 709831f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/examples.yml
Expand Up @@ -120,7 +120,7 @@ jobs:
deps: ipfs-core@$PWD/packages/ipfs-core/dist
- name: types with typed js
repo: https://github.com/ipfs-examples/js-ipfs-types-use-ipfs-from-typed-js.git
deps: ipfs-core@$PWD/packages/ipfs-core/dist,ipfs-core-types@$PWD/packages/ipfs-core-types/dist
deps: ipfs-core@$PWD/packages/ipfs-core/dist
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
17 changes: 17 additions & 0 deletions packages/interface-ipfs-core/src/add-all.js
Expand Up @@ -15,6 +15,7 @@ import bufferStream from 'it-buffer-stream'
import * as raw from 'multiformats/codecs/raw'
import * as dagPB from '@ipld/dag-pb'
import resolve from 'aegir/utils/resolve.js'
import { sha256, sha512 } from 'multiformats/hashes/sha2'

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

it('should add all with sha2-256 by default', async function () {
const content = String(Math.random() + Date.now())

const files = await all(ipfs.addAll([content]))

expect(files).to.have.nested.property('[0].cid.multihash.code', sha256.code)
})

it('should add all with a different hashing algorithm', async function () {
const content = String(Math.random() + Date.now())

const files = await all(ipfs.addAll([content], { hashAlg: 'sha2-512' }))

expect(files).to.have.nested.property('[0].cid.multihash.code', sha512.code)
})

it('should respect raw leaves when file is smaller than one block and no metadata is present', async () => {
const files = await all(ipfs.addAll([Uint8Array.from([0, 1, 2])], {
cidVersion: 1,
Expand Down
17 changes: 17 additions & 0 deletions packages/interface-ipfs-core/src/add.js
Expand Up @@ -11,6 +11,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import last from 'it-last'
import * as raw from 'multiformats/codecs/raw'
import * as dagPB from '@ipld/dag-pb'
import { sha256, sha512 } from 'multiformats/hashes/sha2'

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

it('should add with sha2-256 by default', async function () {
const content = String(Math.random() + Date.now())

const file = await ipfs.add(content)

expect(file).to.have.nested.property('cid.multihash.code', sha256.code)
})

it('should add with a different hashing algorithm', async function () {
const content = String(Math.random() + Date.now())

const file = await ipfs.add(content, { hashAlg: 'sha2-512' })

expect(file).to.have.nested.property('cid.multihash.code', sha512.code)
})

it('should add with mode as string', async function () {
// @ts-ignore this is mocha
this.slow(10 * 1000)
Expand Down
13 changes: 12 additions & 1 deletion packages/ipfs-core/src/components/add-all/index.js
Expand Up @@ -9,16 +9,19 @@ const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
/**
* @typedef {import('multiformats/cid').CID} CID
* @typedef {import('ipfs-unixfs-importer').ImportResult} ImportResult
* @typedef {import('multiformats/hashes/interface').MultihashHasher} MultihashHasher
* @typedef {import('ipfs-core-utils/multihashes').Multihashes} Multihashes
*/

/**
* @typedef {Object} Context
* @property {import('ipfs-repo').IPFSRepo} repo
* @property {import('../../types').Preload} preload
* @property {Multihashes} hashers
* @property {import('ipfs-core-types/src/root').ShardingOptions} [options]
* @param {Context} context
*/
export function createAddAll ({ repo, preload, options }) {
export function createAddAll ({ repo, preload, hashers, options }) {
const isShardingEnabled = options && options.sharding

/**
Expand Down Expand Up @@ -81,13 +84,21 @@ export function createAddAll ({ repo, preload, options }) {
}
}

/** @type {MultihashHasher | undefined} */
let hasher

if (opts.hashAlg != null) {
hasher = await hashers.getHasher(opts.hashAlg)
}

const iterator = pipe(
normaliseInput(source),
/**
* @param {AsyncIterable<import('ipfs-unixfs-importer').ImportCandidate>} source
*/
source => importer(source, repo.blocks, {
...opts,
hasher,
pin: false
}),
transformFile(opts),
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-core/src/components/index.js
Expand Up @@ -126,7 +126,8 @@ class IPFS {
const { add, addAll, cat, get, ls } = new RootAPI({
preload,
repo,
options: options.EXPERIMENTAL
options: options.EXPERIMENTAL,
hashers: this.hashers
})

const files = createFiles({
Expand Down
5 changes: 3 additions & 2 deletions packages/ipfs-core/src/components/root.js
Expand Up @@ -15,11 +15,12 @@ export class RootAPI {
/**
* @param {Context} context
*/
constructor ({ preload, repo, options }) {
constructor ({ preload, repo, hashers, options }) {
const addAll = createAddAll({
preload,
repo,
options
options,
hashers
})

this.addAll = addAll
Expand Down

0 comments on commit 709831f

Please sign in to comment.