Hash of the latest block via the NIS API



I am playing around with the NIS API and am trying to get the hash of a block. I am using the testnet and have set up a local node. When I use ‘/block/at/public’ and post the blockheight, I get the prevBlockHash - that is the previous block. Obviously, I could just set the blockheight one higher BUT I want to know the blockhash before the next block is harvested. I have looked at ‘/local/chain/blocks-after’ to try to get the blockhash but that does not appear to be working (I am using the same post method as works with ‘/block/at/public’).

So, what is the best approach to getting the latest block Hash, is there an API call that will work or am i better off scraping the Nem Explorer by going to http://bob.nem.ninja:8765/#/block/ and just live with the pitfalls of scraping.



Best approach is to pull the last block and calculate the hash.
There are functions to calculate the hash in java (and javascript too i guess).


Thanks for the answer - I am not sure exactly how to pull the block and hence the format of what I should be hashing. However, I presume that it will be in the docs somewhere so will investigate.

Any pointers will be appreciated.


Here are the api docs. Might be a bit outdated in a few places though:



Thanks - I have been checking this out and am sure I am just missing something simple

On testnet I got the last block and it responded as follows


I ran that through a sha3-256 hash function and got a hash of dd29eb240b9ac35b218ef9b38115176c62a8f95fd39b2dddd357c86bdcdc7b0c

Then I got the next block and it came back as


Obviously this is a different hash so I have experimented with different parts of the data but could not replicate the hash.

On top of that I tried using the block/get functionality and put in the hash /block/get?BlockHash= and tried these hashes and others on blocks shown on Tembex and always get a error not found response.

Sorry to be a pain but I really would like to start projects on NEM and therefore would like to be able to use the API properly.


how did you gather the data for the hash calculation? and did you use keccak hash function or official sha3?


I just got the data from a browser call and then used online calculators

https://emn178.github.io/online-tools/sha3_256.html and https://www.browserling.com/tools/sha3-hash with the output hash size set to 256 - same result each time.

I think it maybe something to do with the format of the data and what I am trying to hash - am I suppose to hash the whole block info string or just part of it?


Well you can’t just paste the output of the browser (the json object) into a sha3 tool and expect the correct hash.
You need to build a byte array from the json object as shown in the link i gave you above and use a keccak hash calculator.

NIS /local/chain/blocks-after unauthorized [SOLVED]

Thanks - that makes sense. I will build the byte array (using the assumption that it is formated it a similar style to the documented transactions byte arrays) and use keccak-256


Can’t you just code it in java or javascript?


Hi, I just met the similar problem. I use the code in java as followings:
String signature = “1897735366e39abab99edb435dc9a140d5f84f104dbdae6f1441c09a124c8db5a78a88d20066a9e9775db6c53f48a337e56a8b4e821e250ebcd6c4fd38827603”;
byte[] sha3256 = Hashes.sha3_256(Hex.decode(signature));
_ String blockHash = Hex.toHexString(sha3256);_

The result blockHash is “52908179ad92b86213a3359ebf208639cc4215b8b437f3fc2a327f9025de9da8”;

signature is from height:1734714

When using api to get the preBlockHash on height 1734715. I get the result is:
“timeStamp”: 105016412,
“signature”: “0f6ae459573d49898c8370813315c202b51bf15899ff5fa23ede8e726a39097470a3dc17b0b34aa9c45b89c41105c8c6943b63d003fe008f2faa1242f02fe102”,
“prevBlockHash”: {
“data”: “0059209f391e0fe139c081acf05cdb9f4c506649f6456905fea2d9474d204cba

It’s not the same.


the block hash is not the hash of the signature. You have to collect the block data as described in the api docs:


Hi, I know it can generate the signature by signing an array of bytes extracted from the params of transaction.

Now we want to get from the signature to block hash or transaction hash, do you have any api to do this?

  1. The block hash is the hash of the gathered block data.
  2. The signature is the signed block hash.

You cannot derive the block hash from the signature.


Hello @spring
If you running your local NIS I guess you can use API local call to fetch block data with hash.

/local/chain/blocks-after (https://nemproject.github.io/#getting-part-of-a-chain)

As a result you have ExplorerBlockViewModel with contains block hash:


Thanks for both of you for explaining.

We are developing wallet with many coins including XEM and our node server is on AWS, and service server is not the same server, so it can not use the local call.

We used the old api " /block/at/public", but couldn’t get the block hash and tx hash.
Do you have other apis for getting block info and transaction info via specified height? Or other calculate method?


As I understand service server can have other IP over time so changing config.properties to add additional local IP is not a option?


OK, thanks a lot for your patient description.
From config.properties I see there’s an option to set trusted IP addresses to let the requests from these addresses are treated as local requests.

If there are no other apis or methods to get the block hash or transaction hash except this api "/local/chain/blocks-after "?


Local api seems the only way to get calculated hash for block and transaction from api.
Other way (as you already mentioned) could be calculate it manually.

  1. If you want search transaction with hashes from wallet context you can use:

/account/transfers/all (https://nemproject.github.io/#requesting-transaction-data-for-an-account)
For example:


So you have in this way valid transaction hash (http://bob.nem.ninja:8765/#/search/3c5d5b4d953be13ae823ece7a8eb6ec660487f52af6f511125c212084b814ba4)

Response for this call is https://nemproject.github.io/#transactionMetaDataPair

  1. If you want fetch block hash using public api… Maybe you can call /block/at/public with height+1. In such situation in previousBLockHash you have hash.

So to fetch hash for block 1686550 do:

curl -i -H “Content-Type: application/json” -X POST -d “{height”:“1686551}”



Of course you need to wait to next block generation.

Not sure if this will be helpful