How to convert a Node Address (rn...) into a Validator Address (rv...)

Both the node address and the validator address are Bech32 encodings of the same compressed public key with different human-readable parts (hrp).

import bech32

node_address = "rn1q22ts4wderfhzham0vnzs93g68d5hal3g8yyvv06re8sn88er9ud7us2dgj"
print("Node Address:", node_address)

# Extract Node HRP (rn) and Public Key
_hrp, public_key5 = bech32.bech32_decode(node_address)

# Re-encode Public Key with Validator HRP (rv)
validator_address = bech32.bech32_encode("rv", public_key5)
print("Validator Adddress:", validator_address)

# Returns:
# Node Address: rn1q22ts4wderfhzham0vnzs93g68d5hal3g8yyvv06re8sn88er9ud7us2dgj
# Validator Adddress: rv1q22ts4wderfhzham0vnzs93g68d5hal3g8yyvv06re8sn88er9ud7yrqayv

Run this code on Replit: