Hash of the latest block via the NIS API


#1

Hi,

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.

Thanks


#2

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).


#3

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.


#4

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

https://bob.nem.ninja/docs/#last-block-of-the-block-chain-score


#5

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

{“timeStamp”:76498126,“signature”:“181f2d49d95acad9ef4a68985e2b9971c717eb236389aa2b3502a60e9980f453fa6303d486564f89c70c9dac00e7efeac708c58cb32529e65db6558cbdb91f08”,“prevBlockHash”:{“data”:“9ae0f030204865a0fd60a04193fad9b80d099fd734e87f74a9a17ff963fe0711”},“type”:1,“transactions”:[],“version”:-1744830463,“signer”:“9822cf9571a5551ec19720b87a567a20797b75ec4b6711387643fc352fef704e”,“height”:1096596}

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

{“timeStamp”:76498193,“signature”:“23e266537517fc5d3939c4394efd5f26ccddaf9862bec3033c58d93176af6e0b6fc9d1b3dd58cfea3d5daf200b83c1555efae7a026261a68cbf98e4fba45de01”,“prevBlockHash”:{“data”:“a7568a84d2e50cd029fce5dd58a29d69524f91c045b1bad43954cbb76b1101b8”},“type”:1,“transactions”:[],“version”:-1744830463,“signer”:“b7a0c9cfdb034b5d927f843d911be449c0dfdd50c58982644378dd651f5f3a2e”,“height”:1096597}

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.


#6

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


#7

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?


#8

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]
#9

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


#10

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


#11

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);_
System.out.println(“blockHash:”+blockHash);

The result blockHash is “52908179ad92b86213a3359ebf208639cc4215b8b437f3fc2a327f9025de9da8”;

Descriptions:
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.


#12

the block hash is not the hash of the signature. You have to collect the block data as described in the api docs:
https://nemproject.github.io/#gathering-data-for-the-signature


#13

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?


#14
  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.


#15

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:
https://nemproject.github.io/#explorerBlockViewModel


#16

Thanks for both of you for explaining.
@pawelm
@BloodyRookie

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?


#17

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


#18

@pawelm
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 "?


#19

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.


#20
  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:

http://192.3.61.243:7890/account/transfers/all?address=TCUPVQC77TAMH7QKPFP5OT3TLUV4JYRPV6CEGJXW

{“data”:[{“meta”:{“innerHash”:{},“id”:330885,“hash”:{“data”:“3c5d5b4d953be13ae823ece7a8eb6ec660487f52af6f511125c212084b814ba4”},“height”:1686550},“transaction”:{“timeStamp”:112306547,“amount”:500000000,“signature”:“8076ac247152caa088a593df5ad960b4400aa64094711750f5bcb1ace8307c1245f2ef4c696d38533e75256cbdf2f0ceb8141e687b4d2441db5b5876163c8d0a”,“fee”:100000,“recipient”:“TCKLLG2MDSL5LENA7TEE2J3D357HXG3GWO5MPVVR”,“type”:257,“deadline”:112310147,“message”:{“payload”:“676f6f64206c75636b21”,“type”:1},“version”:-1744830463,“signer”:“cba08dd72505e0c6aa0b7521598c7c63ecef72bd48175355f9dd977664e4fcd1”}},{“meta”:{“innerHash”:{},“id”:329991,“hash”:{“data”:“8d9f3fc0df81a934afdeeed0a6067d7a2147f287b8c7722dfcd68400826f2677”},“height”:1684747},“transaction”:{“timeStamp”:112196725,“amount”:1000000,“signature”:“c3fc923aa241e200ee5b95532e42058a927809dd638baf70d7de780551494705a9e1f2c7b288c1587a946554d246bf7207a8e51f2dda7c1ee83a1d1cb5846b0a”,“fee”:50000,“recipient”:“TBXQJ5TLYF27JZUJXAXY2NLUTO5IS46IS3DKSMNI”,“mosaics”:[{“quantity”:30000000,“mosaicId”:{“namespaceId”:“prx”,“name”:“xpx”}}],“type”:257,“deadline”:112200325,“message”:{},“version”:-1744830462,“signer”:“cba08dd72505e0c6aa0b7521598c7c63ecef72bd48175355f9dd977664e4fcd1”}}…

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}” http://192.3.61.243:7890/block/at/public

{“timeStamp”:112306682,“signature”:“85ff78f4b26308c60ca8bb5b0ae1031629d25f4afcd11e1f90ec48087242db721dd02f0c50fd30d4aee9219b9983423317515ecb720494775ed7d5a688de630f”,“prevBlockHash”:{“data”:“2077d86763f64204ade1fa72b841e75dac6d7ce0c5f99f3ec667582c51b48adc”},“type”:1,“transactions”:[],“version”:-1744830463,“signer”:“76164494236c9aa3e4731dddfa67f28b4cb0be077690abe52a664302e3e57d24”,“height”:1686551}

http://bob.nem.ninja:8765/#/block/1686550

Of course you need to wait to next block generation.

Not sure if this will be helpful