Integrating policy script generated by CLI in cardano-serialization-lib

I’m trying to find a way to use the policy ID and script generated by CLI with cardano-serialization-lib. To my understanding, cardano-serialization-lib works in a different way than the CLI.

In Cardano CLI, I used to generate policy sKey and vKey using cardano-cli address key-gen and then use the vKey to generate keyHash with cardano-cli address key-hash and finally the policy script which looks something like this:-

{
  "type": "all",
  "scripts":
  [
   {
     "type": "before",
     "slot": 66013091
   },
   {
     "type": "sig",
     "keyHash": "0e2ddcb532c75431d714f792209c6b800aeed728f6f52912362c3132"
   }
  ]
}

However, In cardano-serialization-lib, the policy id and script look a bit different:-

{
  policyId: 'af0c7904625b7032b2de82b580c9d6cb3653e825949db41e669f3a00',
  policyScript: '8201828200581cac024adc0c65702a7f2912888034f5d4eb8dd48adcba1549db09543b82051a0086af8c',
  paymentKeyHash: 'ac024adc0c65702a7f2912888034f5d4eb8dd48adcba1549db09543b',
}

Is there a way to transform and use the policy script generated by CLI in the Cardano-serialization-lib library?

I have noticed that someone asked the same question in Github but there is no useful answer to that thread.

The answers seem quite useful to me. The last one even contains the complete example code.

The policy ID is a hash of the policy – actually not of the JSON file, but of the CBOR generated from the JSON file (the thing you see at policyScript in your CSL example).

Since both of your example policies contain different key hashes, the policy IDs cannot be the same. You have to import the exact same key (or its hash) to Javascript to get the same policy ID or – the other way round – export a key generated by CSL or its hash to the command line to get the same result there.

1 Like

For what it’s worth:

policy-script.json:

{
    "type": "all",
    "scripts":
    [
        {
            "type": "before",
            "slot": 66013091
        },
        {
            "type": "sig",
            "keyHash": "0e2ddcb532c75431d714f792209c6b800aeed728f6f52912362c3132"
        }
    ]
}
$ cardano-cli transaction policyid --script-file policy-script.json
e66e5a0ce00d69fb6ee9ac8ae62c34858a55a1976a0647a7efed81be

policy-script.js:

"use strict"

const CSL = require("@emurgo/cardano-serialization-lib-nodejs/cardano_serialization_lib")

const keyBytes = Buffer.from('0e2ddcb532c75431d714f792209c6b800aeed728f6f52912362c3132', 'hex')
const keyHash = CSL.Ed25519KeyHash.from_bytes(keyBytes)

const nativeScripts = CSL.NativeScripts.new()

const lockScript = CSL.NativeScript.new_timelock_expiry(
    CSL.TimelockExpiry.new(66013091))
nativeScripts.add(lockScript)

const sigScript = CSL.NativeScript.new_script_pubkey(
    CSL.ScriptPubkey.new(keyHash))
nativeScripts.add(sigScript)

const allScript = CSL.NativeScript.new_script_all(
    CSL.ScriptAll.new(nativeScripts))

console.log(Buffer.from(allScript.hash().to_bytes()).toString('hex'))
$ node policy-script.js
e66e5a0ce00d69fb6ee9ac8ae62c34858a55a1976a0647a7efed81be
1 Like

@HeptaSean You are the man. It worked like a silver bullet.

Actually, I checked the GitHub thread yesterday and while writing this topic today, I just copied the link from my bookmark, didn’t even check that there was a new response at the bottom. Silly me.

Thanks for the sample code and your time. You have been very good to the Cardano community.

1 Like

And I didn’t check that the response with the example code is so new. …

Modified it again a bit. It still contained some unneccessary conversions. (The thing you get from allScript.hash() already is a CSL.ScriptHash object. No need to go another round.)

According to https://github.com/Emurgo/cardano-serialization-lib/blob/master/rust/pkg/cardano_serialization_lib.js.flow, Ed25519KeyHash should have a from_hex method and ScriptHash should have a to_hex method, which would save a few additional conversions, but they are unfortunately not there, when I install the latest version with npm.