Is there a way to determine which IPFS hash corresponds to which block?

I’d like to know if the block in blocks/ can easily be retraced back to its original IPFS address.

For example:

I have pinned a website (www.bzarg.com/p/how-a-kalman-filter-works-in-pictures) which I previously added to IPFS:

$ ipfs pin add QmdLSyPnQLiVk1wy91tZvhVfqbFbD7zSCuRtC51Z9yeY8E

Now I search for its blocks using grep on a specific key phrase in the website:

$ grep -r 'What can we do with a Kalman filter?' $IPFS_PATH
/media/c/ipfs/blocks/WN/CIQFFZSHMIPQ7E32O3JAFHRWBBGANG5FQ33R3ACS72ZMJEM5PZBSWNI.data:<h1>What can we do with a Kalman filter?</h1>

I get a hit, some IPLD object with the name CIQFFZSHMIPQ7E32O3JAFHRWBBGANG5FQ33R3ACS72ZMJEM5PZBSWNI.data.

Is there a simple way to get the content that sharded into this object? Specifically, I’d like to get the original IPFS address, QmdLSyPnQLiVk1wy91tZvhVfqbFbD7zSCuRtC51Z9yeY8E which corresponds to the content.

Please note that if you adding a file, the data is encoded and chunked into smaller pieces.

Though if you know to know the Multihash of a certain block that it stored on disk, you can do that. The blocks on disk are the base32 representation of the Multihash (which is currently base58 encoded by default). So you can just do the reverse:

const base32 = require('base32.js')
const Base58 = require('base-58')

const b32encoded = 'CIQPZXRLF3N2K27UBBQB7NZB72NVYM4NCDXEFHVAJ6XFKENWR67Y7OI'
console.log('b32encoded:', b32encoded)

const decoder = new base32.Decoder()
const decoded = decoder.write(b32encoded).finalize();

const b58encoded = Base58.encode(decoded)
console.log('b58encoded:', b58encoded)

The output of the script looks like that:

b32encoded: CIQPZXRLF3N2K27UBBQB7NZB72NVYM4NCDXEFHVAJ6XFKENWR67Y7OI
b58encoded: QmfMjwGasyzX74517w3gL2Be3sozKMGDRwuGJHgs9m6gfS
1 Like

Thanks @vmx, that’s really helpful.

A second-part to this question. Once I have the Multihash of a block that is a chunk, how can I determine the parent Multihash that it was chunked from? Or more practically, how can I find the other chunks that comprise the rest of this particular content?

Nodes don’t reference their parents, so I believe you’d have to brute-force it and search all the nodes you have for those that reference the unencoded multihash.

Though, if you had pinned the original file, you could have used ipfs pin ls --type=recursive to list all top-level objects you have pinned.

1 Like

Thanks @TUSF, that is what I was I thinking. I appreciate your help!