How to convert a Node Id (02... or 03...) from the Radix log file into a Validator Address (rv...)?

The Node Id reported in the Radix Node logs is a compressed public key - it starts with either 02 or 03 and is 33 bytes long. To convert it into a Validator Address then just bech32 encode it with a human readable part of rv.

import bech32

hrp = "rv"
node_id = "03d781270fcebe921a9517ead7f79ec4761a4dc80e2b8b705bc1f47938e1421305"

# Convert hex string into bytes
public_key = bytearray.fromhex(node_id)

# Convert from array of 8 bit unsigned integers to array of 5 bit unsigned integers
public_key5 = bech32.convertbits(public_key, 8, 5)

validator_address = bech32.bech32_encode(hrp, public_key5)
print("Validator Adddress:", validator_address)

# Validator Adddress: rv1q0tczfc0e6lfyx54zl4d0au7c3mp5nwgpc4ckuzmc868jw8pggfs2prskrz

The python bech32 library we are using is a reference implementation for creating bitcoin segwit addresses so we have to do a bit of ceremony to convert the public key bytes from 8 bits to 5 bits and use a lower level encode function to work with Radix addresses.

Run this code on Replit: