How to get the hash of Block and Transaction

I am using the Ledger canister api to get back the blocks, but i can not get the hash of both the block and transaction. I found there are fun inside the ledger-core project, but all of these fun are not pub. Am I missing something or i am going in the wrong way.

Hi @nickcen!

but I can not get the hash of both the block and transaction

The ICP ledger specification defines the block hash as SHA-256 of the block’s serialized (protobuf) representation. Since Protobuf representation is not unique and depends on the library you use, we specified our encoding.

The easiest way to get a block hash from a canister is to fetch the next block from the ledger and look at the parent hash of that block.

Transaction hashes are even more extreme: there is no specification for transaction hashes. It’s hard to reproduce the exact encoding the ICP ledger canister uses (unless you use the same Rust code).

Could you elaborate a bit on your use case? What is the application that you’re trying to build?

Thanks, @roman-kashitsyn .
Right now, I can pull back all the blocks , and wanna to persist the block into the database, then doing some analysis or an app something similar to the ICP Explorer.
For the blocks part, I think i can do as you suggest, and sounds that can work.

Do you think if I use something likes rosetta api to fetch the datas, then i can get back the day i want?

Do you pull the blocks into a canister or some service not hosted on the IC?
If it’s the latter, then setting up a rosetta node and using its JSON API is probably the easiest path. That’s also how the IC dashboard works.
Internally, the Rosetta node fetches raw blocks using poorly documented Protobuf API. The node also shares code with the ledger to compute transaction hashes.

I am pulling from service not hosted on the IC. So I will deploy a Rosetta node. Thanks.