Way to check String is a valid account

Hi devs, I am looking for the best way to assert that a message content is an ardor account RS.

int version = attachment.getInt("version.PrunablePlainMessage");
if (version == 1){
    JO messageObj = attachment.getJo("message");
    String specialAccount = messageObj.getString("specialAccount");
    // here, I want to make sure that specialAccount is a proper accountRs
}

Hi,

Maybe use the nxt.util.ReedSolomon.decode method and check if the result is encoded to the same string - see Convert.rsDecode. You need to remove the account prefix from your string and convert it to UPPERCASE.

An Ardor RS account is just an encoding of a 64 bit integer using Reed Solomon to add some checksum data. Using the tools above will check that encoding to make sure it's well formed. But that only means the encoding is correct and you get a 64 bit integer.

If you want to check, in addition, if that 64 bit unsigned integer account id is announced on the blockchain you can use the GetAccount API call and pass the RS or integer to the account parameter. You will get an error for unannounced accounts and the public key for announced accounts. As a bonus, if you send the RS string there it will also report a malformed Reed Solomon encoding. So you get both checks in a single call.

1 Like

Hi both, thanks for your responses! I did use the RsConvertCall as in the following and it seems to work. If the message contained an invalid account, the RsConvert call would return an errorcode instead.

int version = attachment.getInt("version.PrunablePlainMessage");
if (version == 1){
 JO messageObj = attachment.getJo("message");
 String specialAccount = messageObj.getString("specialAccount");
 JO response = RsConvertCall.create().account(specialAccount).call();

 if(response.getString("accountRS").equals(specialAccount)){
     // String is a valid account.
     // do something...
 }
1 Like