1

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?

2 Answers 2

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.

4
  • 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. Commented Jun 9, 2022 at 5:07
  • 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
    Commented Jun 9, 2022 at 9:01
  • Also see npmjs.com/package/@dcspark/cip5-js, this might help.
    – Fermat
    Commented Jun 9, 2022 at 9:12
  • Thank you very much! Commented Jun 9, 2022 at 23:27
2

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.

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.