<p>If anyone is trying to do this using cardano-serialization-lib and Nami:</p>
<pre><code>const PUBKEY_HASH = async () =&gt; {
  const cardano = window.cardano;

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

  return pkh;
};
</code></pre>
<p>Assuming you are using a loader like the one from <a href="https://github.com/Berry-Pool/spacebudz/blob/main/src/cardano/market/loader.js" rel="noreferrer">spacebudz</a> and you are importing Buffer <code>import { Buffer } from &quot;buffer&quot;;</code></p>
<p><strong>UPDATE</strong></p>
<p>Made a more abstract version that takes a string (the bech32 address) and returns another string (the public key hash)</p>
<pre><code>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());
}
</code></pre>

CC BY-SA 4.0
2021-12-23T12:47:57.407Z
Sorry `fromHex` is defined as `const fromHex = (hex) => Buffer.from(hex, "hex")`
Mateus
CC BY-SA 4.0
2021-12-23T12:48:26.113Z
And `toHex` `const toHex = (bytes) => Buffer.from(bytes).toString("hex")`
Mateus
CC BY-SA 4.0
2021-12-20T16:53:00.620Z
2021-12-23T12:47:04.293Z
2021-12-23T12:47:04.293Z
Mateus
<p>I think you're looking for this:</p>
<ul>
<li><code>cardano-cli address key-hash --payment-verification-key STRING</code></li>
<li><code>cardano-cli address key-hash --payment-verification-key-file FILE</code></li>
</ul>
<p>You should use your <code>payment.vkey</code></p>

CC BY-SA 4.0
2022-02-04T14:53:45.307Z
This is the `cborHex` value in the `.vkey` file yes?
Hud
CC BY-SA 4.0
2021-10-25T17:54:53.587Z
2021-10-25T17:54:53.587Z
georgeos
<p>I am working on this as well... I am not sure but this seems quite promising</p>
<p><a href="https://input-output-hk.github.io/cardano-addresses/demo/" rel="nofollow noreferrer">https://input-output-hk.github.io/cardano-addresses/demo/</a></p>
<p>Maybe this is usefull as well</p>
<p><a href="https://cips.cardano.org/cips/cip19/#shelleyaddresses" rel="nofollow noreferrer">https://cips.cardano.org/cips/cip19/#shelleyaddresses</a></p>

CC BY-SA 4.0
2021-12-20T16:54:39.330Z
If you are using the serialization lib, my answer may help
Mateus
CC BY-SA 4.0
2021-12-17T15:02:37.873Z
2021-12-17T15:31:04.430Z
2021-12-17T15:31:04.430Z
Alicia Basilio
<p>You can use the &quot;cardano-address address inspect&quot; 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: <a href="https://github.com/input-output-hk/cardano-addresses" rel="nofollow noreferrer">https://github.com/input-output-hk/cardano-addresses</a></p>

CC BY-SA 4.0
2022-02-02T05:18:46.213Z
2022-02-02T05:18:46.213Z
Genty SAMVURA
<p>Couldn't you just pass the address to Plutus and then convert it there to a public key hash using the function in <code>Ledger.Address</code>:</p>
<pre><code>{-# INLINABLE toPubKeyHash #-}
-- | The PubKeyHash of the address, if any
toPubKeyHash :: Address -&gt; Maybe PubKeyHash
</code></pre>
<p>The address is serializable so you can pass it easily and get a proper instance of <code>Address</code> that you can use at your will in your Plutus code.</p>

CC BY-SA 4.0
2021-12-20T22:12:31.657Z
Are you sure this `Address` is a bech32 string? I don't think it is https://staging.plutus.iohkdev.io/doc/haddock/plutus-ledger-api/html/Plutus-V1-Ledger-Address.html#t:Address
Mateus
CC BY-SA 4.0
2021-12-20T23:05:06.483Z
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
CC BY-SA 4.0
2021-12-21T10:55:45.880Z
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
CC BY-SA 4.0
2021-12-20T21:21:15.343Z
2021-12-20T21:21:15.343Z
Jey
<p>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 &quot;payment.addr&quot;, &quot;payment.skey&quot;, &quot;payment.vkey&quot;, etc. But now, the validator script that I created requires as one of it's parameters a Plutus <code>AssocMap</code> associating public key hashes to integers.</p>
<p>My question is, based on the keys I created, how can I find out their <code>PubKeyHash</code>? I believe this is related to &quot;payment.addr&quot;, but I'm not sure.</p>

CC BY-SA 4.0
2021-12-08T10:34:12.023Z
How to get this value when using e.g. Nami Wallet?
Adam Machera
CC BY-SA 4.0
2021-12-15T13:41:19.047Z
Is there a way to get key-hash from payment.address only?
Mantas Andriuška
CC BY-SA 4.0
2021-12-20T16:54:15.823Z
@AdamMachera See my new answer
Mateus
<plutus><transaction><testnet><public-key>
How to convert a cardano address into a public key hash?
# How to convert a cardano address into a public key hash?

(Asked by: username_0 on 2021-10-25)

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.

**Tags:** plutus, transaction, testnet, public-key
> **Comments:**
> **username_1**: How to get this value when using e.g. Nami Wallet?  
> **username_2**: Is there a way to get key-hash from payment.address only?  
> **username_0**: @AdamMachera See my new answer  

---

## 5 answers

### Answer by username_3 on 2021-10-25. Score: 12 | Accepted answer

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`

> **Comments:**
> **username_4**: This is the `cborHex` value in the `.vkey` file yes?  

---

### Answer by username_0 on 2021-12-20. Score: 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());
}

> **Comments:**
> **username_0**: Sorry `fromHex` is defined as `const fromHex = (hex) => Buffer.from(hex, "hex")`  
> **username_0**: And `toHex` `const toHex = (bytes) => Buffer.from(bytes).toString("hex")`  

---

### Answer by username_5 on 2021-12-17. Score: 2

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>

> **Comments:**
> **username_0**: If you are using the serialization lib, my answer may help  

---

### Answer by username_6 on 2022-02-02. Score: 2

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>


---

### Answer by username_7 on 2021-12-20. Score: 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.

> **Comments:**
> **username_0**: Are you sure this `Address` is a bech32 string? I don't think it is https://staging.plutus.iohkdev.io/doc/haddock/plutus-ledger-api/html/Plutus-V1-Ledger-Address.html#t:Address  
> **username_7**: 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.  
> **username_7**: This is getting strange. Why is it so difficult to find a function in Plutus that parses the address representation to the address class ?  

---

