5

After spending some time learning Plutus with emulators, I decided to start learning how to actually deploy and use the scripts in the testnet. In this process, I installed cardano-cli and created multiple key files, such as "payment.addr", "payment.skey", "payment.vkey", etc. But now, the validator script that I created requires as one of it's parameters a Plutus AssocMap associating public key hashes to integers.

My question is, based on the keys I created, how can I find out their PubKeyHash? I believe this is related to "payment.addr", but I'm not sure.

3
  • 1
    How to get this value when using e.g. Nami Wallet? Dec 8, 2021 at 10:34
  • Is there a way to get key-hash from payment.address only? Dec 15, 2021 at 13:41
  • @AdamMachera See my new answer
    – Mateus
    Dec 20, 2021 at 16:54

5 Answers 5

10

I think you're looking for this:

  • cardano-cli address key-hash --payment-verification-key STRING
  • cardano-cli address key-hash --payment-verification-key-file FILE

You should use your payment.vkey

1
  • This is the cborHex value in the .vkey file yes?
    – Hud
    Feb 4 at 14:53
6

If anyone is trying to do this using cardano-serialization-lib and Nami:

const PUBKEY_HASH = async () => {
  const cardano = window.cardano;

  const pkh = Loader.Cardano.BaseAddress.from_address(
    Loader.Cardano.Address.from_bytes(
      Buffer.from(await cardano.getChangeAddress(), "hex")
    )
  )
    .payment_cred()
    .to_keyhash();

  return pkh;
};

Assuming you are using a loader like the one from spacebudz and you are importing Buffer import { Buffer } from "buffer";

UPDATE

Made a more abstract version that takes a string (the bech32 address) and returns another string (the public key hash)

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());
}
2
  • 1
    Sorry fromHex is defined as const fromHex = (hex) => Buffer.from(hex, "hex")
    – Mateus
    Dec 23, 2021 at 12:47
  • 1
    And toHex const toHex = (bytes) => Buffer.from(bytes).toString("hex")
    – Mateus
    Dec 23, 2021 at 12:48
1

I am working on this as well... I am not sure but this seems quite promising

https://input-output-hk.github.io/cardano-addresses/demo/

Maybe this is usefull as well

https://cips.cardano.org/cips/cip19/#shelleyaddresses

1
  • If you are using the serialization lib, my answer may help
    – Mateus
    Dec 20, 2021 at 16:54
1

You can use the "cardano-address address inspect" syntax to get the public key hash from an address without requiring the verification key. Requiring the verification key assumes having access to the file while addresses can be shared without having to know the owner. This link can help: https://github.com/input-output-hk/cardano-addresses

0

Couldn't you just pass the address to Plutus and then convert it there to a public key hash using the function in Ledger.Address:

{-# INLINABLE toPubKeyHash #-}
-- | The PubKeyHash of the address, if any
toPubKeyHash :: Address -> Maybe PubKeyHash

The address is serializable so you can pass it easily and get a proper instance of Address that you can use at your will in your Plutus code.

3
  • 1
    Are you sure this Address is a bech32 string? I don't think it is staging.plutus.iohkdev.io/doc/haddock/plutus-ledger-api/html/…
    – Mateus
    Dec 20, 2021 at 22:12
  • I don't know if this deserialisation is this easy, actually. I haven't tried yet. But, there must be a function in Plutus to deserialise the string to the Address class.
    – Jey
    Dec 20, 2021 at 23:05
  • This is getting strange. Why is it so difficult to find a function in Plutus that parses the address representation to the address class ?
    – Jey
    Dec 21, 2021 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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