2

I know that this is possible in javascript using the cardano serialization lib, as follows:

function toHex(bytes){
    return Buffer.from(bytes).toString("hex");
}

function addrToPubKeyHash(bech32Addr) {
  const pkh = Loader.Cardano.BaseAddress.from_address(
    Loader.Cardano.Address.from_bech32(bech32Addr)
  )
    .payment_cred()
    .to_keyhash();

  return toHex(pkh.to_bytes());
}

How would I do the same in python?

1 Answer 1

3

Check out pycardano. Class Address has a class method from_primitive that can read and decode a bech32 address. Then you can directly read field payment_part, which is the public key hash of the payment credential from the address object. You can find the API documentation here.

Example:

>>> from pycardano import Address   
>>> addr = Address.from_primitive("addr_test1vzv89dr7v69enywx0t7sqcyxshcmnlv9q43lhpg7ecfhjjsjcdsza")
>>> addr.payment_part
VerificationKeyHash(hex='9872b47e668b9991c67afd00608685f1b9fd850563fb851ece13794a')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.