[SOLVED] Reproduce IPFS hash

Hi! Im trying to reproduce IPFS hash using js-multihash (https://github.com/multiformats/js-multihash). From my understanding IPFS hash basically take the input of file that has been hashed with sha2-256 (or other algorithm), add some identifier and base58 the result.

I try to do that using js-multihash but the result was different than when I use ipfs add -n.
So is there any steps that I miss?

Here’s my code:

import multihashes from 'multihashes'
import hashFiles from 'hash-files'

const hashed = hashFiles.sync({
	files: './src/index.js',
	algorithm: 'sha256'

const buf = new Buffer(hashed, 'hex')
const encode = multihashes.encode(buf, 'sha2-256')
const b58 = multihashes.toB58String(encode)
console.log(b58) // output hash QmRMjXNBfg2EjAVUjejeCe6FC9PhCaKnh8EqC38goFDpBp

// ipfs add -n output hash QmYw4WVtnsfQvkbY8pbGx85ETVATjuWSNYYtFjhU7v1z4x

If you’re trying to reverse-engineer what ipfs add -n does, I’d suggest taking a look at what ipfs.files.add does in js-ipfs when using the onlyHash option.

Curious, is there a reason you can’t use the existing js-ipfs ipfs.files.add API?

At the very least, this is omitting the step where files are chunked and split into a merkledag structure.


SOLVED - https://github.com/ipfs/js-ipfs/issues/1611#issuecomment-426212963


Im not using js-ipfs because the program that I develop is used by client, so i think when using js-ipfs the client needed to download the IPFS which increase the app size quite big.

So I found out js dag generation for IPFS (https://github.com/ipld/js-ipld-dag-pb) but still the result of DAGNode.create doesnt match the result of ipfs add -n. Any suggestions?