I'm trying to create an IPFS compatible mutihash but it is not matching. I am asking here because I have not yet found an example that takes this from hashing to the end result.
echo -n multihash > multihash.txt
ipfs add multihash.txt
added QmZLXzjiZU39eN8QirMZ2CGXjMLiuEkQriRu7a7FeSB4fg multihash.txt
sha256sum multihash.txt
9cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47 multihash.txt
node
> var bs58=require('bs58')
bs58.encode(new Buffer('9cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47','hex'))
'BYptxaTgpcBrqZx9tghNCWFfUuYBcGfLydEvDjXqBV7k'
> var mh=require('multihashes')
mh.toB58String(mh.encode(new Buffer('9cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47','hex'), 'sha2-256'))
'QmYtUc4iTCbbfVSDNKvtQqrfyezPPnFvE33wFmutw9PBBk'
The intent is to re-create the IPFS path QmZLXzjiZU39eN8QirMZ2CGXjMLiuEkQriRu7a7FeSB4fg
using the multihashes package.
I'm able to create the same hash QmYtUc...9PBBk
as shown in the example here: https://github.com/multiformats/multihash#example
The simplest way currently is to simply use
ipfs-http-client
. All above previous solutions in this thread don't work for me anymore.Some of the other answers are outdated. This is what worked for me:
A file in IPFS is 'transformed' into a Unixfs file, which is a representation of files in a DAG, in your example, you are hashing directly your multihash.txt with sha2-256, but what happens inside IPFS is:
IPFS uses multihash where the format is the following:
The list of hash function codes can be found in this table.
Here's some pseudocode of the process using SHA2-256 as the hashing function.
Concatenating those three items will produce
Which then you encode it to base58
Here's an example of how to essentially implement multihash in JavaScript:
There's a CLI you can use to generate multihashes:
As @David stated, A file in IPFS is "transformed" into a Unixfs "file", which is a representation of files in a DAG. So when you use
add
to upload a file to IPFS, the data has metadata wrapper which will give you a different result when you multihash it.For example:
Here's an example in Node.js of how to generate the exact same multihash as
ipfs add
:Hope this helps