<p>To convert from <code>cborHex</code> to <code>VRFKeyHash</code> I did the following, assuming there's a file with this format...</p>
<pre><code>{
    &quot;type&quot;: &quot;VrfVerificationKey_PraosVRF&quot;,
    &quot;description&quot;: &quot;VRF Verification Key&quot;,
    &quot;cborHex&quot;: &quot;5820d3e6e69edf63e50ce0d12bc8fd891d6d00a09cb484a55f99caf0767bd9d75488&quot;
}
</code></pre>
<p>...which can be generated using <code>cardano-cli</code> or <code>cardanocli-js</code>:</p>
<pre><code>const vrfKeyFile = readFileSync(vrfKeyAccount.vkey, 'utf-8');
CardanoWasm.VRFKeyHash.from_bytes(Buffer.from(JSON.parse(vrfKeyFile).cborHex.substring(4), &quot;hex&quot;));
</code></pre>
<p>You need to get rid of the first 2 bytes (&quot;<code>5820</code>&quot; prefix) in order for it to work.</p>

CC BY-SA 4.0
2022-05-31T06:36:51.407Z
2022-06-10T01:55:10.770Z
2022-06-10T01:55:10.770Z
Falcon Stakepool
<p>Verification keys can be represented in different ways. The one that you want is a representation where the key hash is converted with a prefix <code>vrf_</code> to the bech32 format. This can be done via the following repo <a href="https://github.com/input-output-hk/bech32" rel="nofollow noreferrer">(1)</a>.
Given your specific given vrf verification key file you first have to convert it to a vrf key hash. This can be done via the cardano-cli with</p>
<pre><code>cardano-cli node key-hash-VRF --verification-key-file fileName.vkey 
</code></pre>
<p>Which returns <code>80bdf88001bcfb0723f93aff337b299d1502f1d1884afe65342b7b8dfd18c2e6</code> for your vkey file. Then one can convert this hash to bech32 format with prefix <code>vrf_</code> as follows</p>
<pre><code>cabal run exe:bech32 -- vrf_ &lt;&lt;&lt; 80bdf88001bcfb0723f93aff337b299d1502f1d1884afe65342b7b8dfd18c2e6
</code></pre>
<p>which returns <code>vrf_1sz7l3qqphnaswgle8tlnx7efn52s9uw33p90uef59dacmlgcctnqut3ndw</code>.
To reverse the bech32 transformation you can run the following</p>
<pre><code>cabal run exe:bech32 -- &lt;&lt;&lt; vrf_vk13hg4gg5fg67399nuz2ldk89kqw9h379cfgtkpvd83ra89f908kcqv2cw3j
</code></pre>
<p>which return <code>8dd154228946bd12967c12bedb1cb6038b78f8b84a1760b1a788fa72a4af3db0</code>, the vrf key hash of the example bech32 address you gave.</p>

CC BY-SA 4.0
2022-06-09T05:07:30.383Z
Do you know what conversion should I apply to cborHex in order to work with Cardano Serialization Lib?

const vrfKeyHash = CardanoWasm.VRFKeyHash.from_bytes(X)

Imagine that X is the cborHex. If I provide it directly that way, I get the following:

"JsValue("Deserialization failed in VRFKeyHash because: Invalid cbor: expected tuple 'hash length' of length 32 but got length Len(34).")"

I have also tried converting it to bytes, but same error.
Falcon Stakepool
CC BY-SA 4.0
2022-06-09T09:01:02.410Z
Note that in the bech32 is something similar to JSON, to indicate if something is a bytestring or a list etc some bytes indicate those objects. If you have a look at cbor.me and put in the full you will see the key is `D3E6E69EDF63E50CE0D12BC8FD891D6D00A09CB484A55F99CAF0767BD9D75488`. Which has length 32 not 34. I am no expert on the serialization lib so I cannot help with that unfortunately.
Fermat
CC BY-SA 4.0
2022-06-09T09:12:54.860Z
Also see https://www.npmjs.com/package/@dcspark/cip5-js, this might help.
Fermat
CC BY-SA 4.0
2022-06-09T23:27:18.240Z
Thank you very much!
Falcon Stakepool
CC BY-SA 4.0
2022-05-05T09:17:26.460Z
2022-05-05T09:17:26.460Z
Fermat
<p>I need to generate a VRF Key Hash in this format: <code>vrf_vk13hg4gg5fg67399nuz2ldk89kqw9h379cfgtkpvd83ra89f908kcqv2cw3j</code></p>
<p>When I generate it using <code>cardano-cli</code>, I get this file:</p>
<pre><code>{
    &quot;type&quot;: &quot;VrfVerificationKey_PraosVRF&quot;,
    &quot;description&quot;: &quot;VRF Verification Key&quot;,
    &quot;cborHex&quot;: &quot;5820d3e6e69edf63e50ce0d12bc8fd891d6d00a09cb484a55f99caf0767bd9d75488&quot;
}
</code></pre>
<p>What I'm trying to do is to convert this to <code>VRFKeyHash</code>. I'm using this function (I'm reading the value directly from the file, that's why I'm parsing it):</p>
<pre><code>const fromHex=(hex)=&gt;Buffer.from(hex,&quot;hex&quot;);
const vrfKeyHash = CardanoWasm.VRFKeyHash.from_bytes(fromHex(JSON.parse(vrfKeyFile).cborHex));
</code></pre>
<p>But when I do this, I get the following error:</p>
<pre><code>Deserialization failed in VRFKeyHash because: Invalid cbor: expected tuple 'hash length' of length 32 but got length Len(34).
</code></pre>
<p>What is it that I'm doing wrong?</p>

<cardano-cli><cardano-serialization-lib><hash><signing-key-file>
How can I convert from cborHex to VRFKeyHash type from the Cardano Serialization Lib?
# How can I convert from cborHex to VRFKeyHash type from the Cardano Serialization Lib?

(Asked by: username_0 on 2022-05-05)

I need to generate a VRF Key Hash in this format: vrf_vk13hg4gg5fg67399nuz2ldk89kqw9h379cfgtkpvd83ra89f908kcqv2cw3j
When I generate it using cardano-cli, I get this file:
{
    "type": "VrfVerificationKey_PraosVRF",
    "description": "VRF Verification Key",
    "cborHex": "5820d3e6e69edf63e50ce0d12bc8fd891d6d00a09cb484a55f99caf0767bd9d75488"
}

What I'm trying to do is to convert this to VRFKeyHash. I'm using this function (I'm reading the value directly from the file, that's why I'm parsing it):
const fromHex=(hex)=>Buffer.from(hex,"hex");
const vrfKeyHash = CardanoWasm.VRFKeyHash.from_bytes(fromHex(JSON.parse(vrfKeyFile).cborHex));

But when I do this, I get the following error:
Deserialization failed in VRFKeyHash because: Invalid cbor: expected tuple 'hash length' of length 32 but got length Len(34).

What is it that I'm doing wrong?

**Tags:** cardano-cli, cardano-serialization-lib, hash, signing-key-file

---

## 2 answers

### Answer by username_0 on 2022-05-31. Score: 2 | Accepted answer

To convert from cborHex to VRFKeyHash I did the following, assuming there's a file with this format...
{
    "type": "VrfVerificationKey_PraosVRF",
    "description": "VRF Verification Key",
    "cborHex": "5820d3e6e69edf63e50ce0d12bc8fd891d6d00a09cb484a55f99caf0767bd9d75488"
}

...which can be generated using cardano-cli or cardanocli-js:
const vrfKeyFile = readFileSync(vrfKeyAccount.vkey, 'utf-8');
CardanoWasm.VRFKeyHash.from_bytes(Buffer.from(JSON.parse(vrfKeyFile).cborHex.substring(4), "hex"));

You need to get rid of the first 2 bytes ("5820" prefix) in order for it to work.


---

### Answer by username_1 on 2022-05-05. Score: 3

Verification keys can be represented in different ways. The one that you want is a representation where the key hash is converted with a prefix vrf_ to the bech32 format. This can be done via the following repo (1).
Given your specific given vrf verification key file you first have to convert it to a vrf key hash. This can be done via the cardano-cli with
cardano-cli node key-hash-VRF --verification-key-file fileName.vkey 

Which returns 80bdf88001bcfb0723f93aff337b299d1502f1d1884afe65342b7b8dfd18c2e6 for your vkey file. Then one can convert this hash to bech32 format with prefix vrf_ as follows
cabal run exe:bech32 -- vrf_ <<< 80bdf88001bcfb0723f93aff337b299d1502f1d1884afe65342b7b8dfd18c2e6

which returns vrf_1sz7l3qqphnaswgle8tlnx7efn52s9uw33p90uef59dacmlgcctnqut3ndw.
To reverse the bech32 transformation you can run the following
cabal run exe:bech32 -- <<< vrf_vk13hg4gg5fg67399nuz2ldk89kqw9h379cfgtkpvd83ra89f908kcqv2cw3j

which return 8dd154228946bd12967c12bedb1cb6038b78f8b84a1760b1a788fa72a4af3db0, the vrf key hash of the example bech32 address you gave.

> **Comments:**
> **username_0**: Do you know what conversion should I apply to cborHex in order to work with Cardano Serialization Lib?  const vrfKeyHash = CardanoWasm.VRFKeyHash.from_bytes(X)  Imagine that X is the cborHex. If I provide it directly that way, I get the following:  "JsValue("Deserialization failed in VRFKeyHash because: Invalid cbor: expected tuple 'hash length' of length 32 but got length Len(34).")"  I have also tried converting it to bytes, but same error.  
> **username_1**: Note that in the bech32 is something similar to JSON, to indicate if something is a bytestring or a list etc some bytes indicate those objects. If you have a look at cbor.me and put in the full you will see the key is `D3E6E69EDF63E50CE0D12BC8FD891D6D00A09CB484A55F99CAF0767BD9D75488`. Which has length 32 not 34. I am no expert on the serialization lib so I cannot help with that unfortunately.  
> **username_1**: Also see https://www.npmjs.com/package/@dcspark/cip5-js, this might help.  
> **username_0**: Thank you very much!  

---

