I need a bech32
wallet address and all that I have is my wallet PKH
and a wallet Id
. Is there a way to derive that bech32
address in python?
Needed to perform queries on blockfrost and find how much amount of ADA I have in my wallet.
You can use pycardano to derive the address from a public key file.
Example:
from pycardano import Key, Address, Network
pk = Key.load("/path/to/payment.vkey")
pkh = pk.hash()
addr = Address(payment_part=pkh, Network.MAINNET)
print(addr)
If you already have the public key hash, use the following code to get pkh:
from pycardano import VerificationKeyHash
pkh = VerificationKeyHash(bytes.fromhex("1234abcd..."))
You can learn more about key and address manipulations in this page: https://pycardano.readthedocs.io/en/latest/guides/address.html
The PKH is for verifying the authenticity of a signed transaction. We couldn't convert it to the bech32 wallet address. Hashing is a one-way function. It means that if you hash a piece of data, it is impossible to revert the hash to its original form.
For instance, we consider a simple function used to get the modulus of 3 and a list of the input value
1 `mod` 3 = 1
2 `mod` 3 = 2
3 `mod` 3 = 3
4 `mod` 3 = 1
In this example, the list of the input value is [1,2,3,4] and the list of hash results is [1,2,3,1]. So the 1 can be from 1, 4, 7, and so on. This is for the demonstration only, In practice hash functions shouldn't result in a small range of values like this.