user2897: 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?

user2335: 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.

user2897: 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.

2897: <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>
 2335: <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>
 2897: <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>
