I am trying to get the stash id of a validator from the host of the node. I have a list of candidate validators. How would I achieve this?

I tried the following using Polkadot.js

var validator = "H3DL157HL7DkvV2kXocanmKaGXNyQphUDVW33Fnfk8KNhsv" // dummy key
var hasKey = await api.rpc.author.hasKey(validator, "acco")

But it always returns false even from a known match

when running it via shell:

curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "athor_hasKey", "params":["H3DL157HL7DkvV2kXocanmKaGXNyQphUDVW33Fnfk8KNhsv", "acco"]}' http://localhost:9933

I get the following error

{"jsonrpc":"2.0","error":{"code":-32602,"message":"invalid hex character: H, at 0 at line 1 column 49"},"id":1}

I also tried the keys returned from api.query.session.nextKeys, but I did not get a match. Not sure what I should use.

2 Answers 2


Where are you getting this list of candidate validators from?

The hasKey query is for checking if a node contains a specific session key. That is, you would not pass an account ID or address to it. I see that you are connected to localhost. Is this actually a validator with keys?

If you want to check if your local node is a validator, you can look up the nextKeys for the address you're interested in, and then call api.rpc.author.hasSessionKeys(<keys>) for the concatenation of all the keys. If it returns true, then that node has the session keys for that validator.

Note that this does not guarantee that you are safe from equivocation, i.e. that another node does not have the keys as well. Only that this node does have the keys.

EDIT: Here is a more complete example:

First, I'll check if the stash I'm interested in has signaled intent to be a validator with staking.validators.


It is, so let's look up its session keys with session.nextKeys(1NDRMvN7FH9YtJLVPf9doF5zbuUwn6hdH1b4WmVyZDr5joM).


Now, I SSH into the node that I know is using those keys. Of course, you need RPC access to whatever node you want to check the keys on. To make the storage key, I concatenate all of the keys into one value:


And calling author.hasSessionKeys(<long hex value>), I get true :tada:


If I just want to check one, I can do so with session.hasKey(<key>, <type>). In this case I'll use babe, so the key is 0x3a80d898638dccd4aaa472e7948e7394969cc443335c494159948915c097c036.



working JS code (thank you @joepetrowski)

var validatorAddress = ''; // add your address here
var keys = await api.query.session.nextKeys(validatorAddress);
var keysJson = JSON.parse(JSON.stringify(keys));
// typescript Struct has a .toArray() function
var grandpa = keysJson.grandpa;
var babe = keysJson.babe;
var imOnline = keysJson.imOnline;
var paraValidator = keysJson.paraValidator;
var paraAssignment = keysJson.paraAssignment;
var authorityDiscovery = keysJson.authorityDiscovery;
var keys = [grandpa, babe, imOnline, paraValidator, paraAssignment, authorityDiscovery];
var sessionKeys = "0x";
for (const key of keys) {
  if (key != undefined) {
    sessionKeys += key.replace(/^(0x)/,'');
var result = await api.rpc.author.hasSessionKeys(sessionKeys)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.