How to convert a Wallet Address into a Public Key

import bech32

wallet_address = "rdx1q...."

_hrp, wallet_readdr_5bit = bech32.bech32_decode(wallet_address)
wallet_readdr = bech32.convertbits(wallet_readdr_5bit, 5, 8, pad=False)

# Remove the Radix Engine Address 04 prefix byte
wallet_public_key = wallet_readdr[1:34]

print("Wallet Public Key:", bytes(wallet_public_key).hex())

Run this code on Replit:

Also see: How to convert a Public Key into a Wallet Address

2 Likes

Is this also the correct way to validate an Radix address? Is there any other methods to check to make sure an Radix address is valid or not?

The Bech32 algorithm has 6 character checksum that “gives a chance of less than 1 in a billion that an invalid address is deemed correct”.

So given a Radix wallet address, you can validate it by using the Bech32 decode function and then:

  1. Checking the data part is 34 bytes long
  2. Checking that the first byte is 04 - which defines it as a Radix Engine Address
  3. Checking that the second byte is either 02 or 03 - to indicate that it is a compressed public key

Further Reading:
https://medium.com/@meshcollider/some-of-the-math-behind-bech32-addresses-cf03c7496285

Thanks for your reply, I also add the checking for _hrp (returned by method bech32_decode) to make sure it is “rdx”

here are the hrp’s of addresses olympia-node/radixdlt-java-common/src/main/java/com/radixdlt/networks/Network.java at main · radixdlt/olympia-node · GitHub

1 Like