Generate File Hash Locally on Browser

Hi everyone! I’m glad to be part of this community!

Hopefully someone can help me with my following issue!

What I’m trying to do is to generate the IPFS hash of a file while the user is using a PWA offline (just a web app). If I’m connected, I can just ipfs.add to add a file and get the file hash, no sweat. Now, if I’m offline I need to hash it locally and want the same output as if I added to an IPFS node.

This is the code that I got so far:

import { importer } from "ipfs-unixfs-importer";
import IPLD from "ipld";
import ipldInMemory from "ipld-in-memory";

async function* asAsyncIterable(arr: Uint8Array) {
    yield* [arr];

const ipfsQhashFile = async (file: File) => {
    const fileName =;
    const bytes = new Uint8Array(await file.arrayBuffer());

    const ipld = await ipldInMemory(IPLD, {});
    const source = {
        path: fileName,
        content: asAsyncIterable(bytes), //AsyncIterable<Uint8Array>

    const cids = [];
    for await (const entry of importer(source, ipld)) {">>> entry", entry);

And when I run the function ipfsQhashFile with a file that the user selected on a fileinput field I receive the following error:

Error: `format` parameter must be number (multicodec)
    at IPLDResolver.put (index.js:197)
    at persist (persist.js:33)
    at :3000/async http:/localhost:3000/static/js/vendors~main.chunk.js:32539
    at async parallelBatch (index.js:36)
    at async buildFileBatch (index.js:47)
    at async batch (index.js:20)
    at async reduceToParents (balanced.js:22)
    at async parallelBatch (index.js:36)
    at async treeBuilder (tree-builder.js:95)
    at async importer (index.js:59)
    at async ipfsQhashFile (ipfs.ts:21)

Does anyone know what I’m missing?

Thank you!

Does this help? GitHub - alanshaw/ipfs-only-hash: #️⃣ Just enough code to calculate the IPFS hash for some data

1 Like