question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It would be great to be able to use this module to do the equivalent of ipfs add -r --only-hash.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

2reactions
rrthomascommented, Oct 4, 2021

For now, I am using the following code, and specifically the ofDir method:

// A slightly modified version of ipfs-only-hash
// See https://github.com/alanshaw/ipfs-only-hash/issues/18
const { globSource } = require('ipfs-http-client');
const { importer } = require('ipfs-unixfs-importer');

const block = {
  get: async (cid) => { throw new Error(`unexpected block API get for ${cid}`); },
  put: async () => { throw new Error('unexpected block API put'); },
};

async function hash(content_, options_) {
  const options = options_ || {};
  options.onlyHash = true;

  let content = content_;
  if (typeof content === 'string') {
    content = [{ content: new TextEncoder().encode(content) }];
  } else if (content instanceof Object.getPrototypeOf(Uint8Array)) {
    content = [{ content }];
  }

  let lastCID;
  for await (const { cid } of importer(content, block, options)) {
    lastCID = cid;
  }
  return lastCID;
}

module.exports = {
  cidToHex(cid) {
    return `0x${Buffer.from(cid.bytes.slice(2)).toString('hex')}`;
  },

  of: hash,

  async ofDir(directory) {
    const options = {
      cidVersion: 0, // Lines up with the smart contract code
    };

    const files = globSource(directory, { recursive: true });
    const rootCID = await hash(files, options);
    return rootCID;
  },
};
0reactions
rrthomascommented, Nov 15, 2022

@wjagodfrey Thanks for this fix! I will use it myself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Linux: compute a single hash for a given folder & contents?
One possible way would be: sha1sum path/to/folder/* | sha1sum. If there is a whole directory tree, you're probably better off using find and ......
Read more >
How can I generate an MD5 sum for a folder on Windows?
This will generate a .md5 file which contains a listing of the hashes of each file in that directory, including all files in...
Read more >
DirHash - Hash directories and files on Windows - GitHub Pages
DirHash is a Windows console program that computes the hash of a given directory content or a single file. It also supports creating...
Read more >
folder-hash - npm
Create a hash checksum over a folder or a file. The hashes are propagated upwards, the hash that is returned for a folder...
Read more >
hash value on directory - Ask Ubuntu
The folder has so many files and folders. How to calculate hash value of directory as a whole on Ubuntu. md5sum calculate at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found