Skip to main content
1 of 3
sean
  • 1.5k
  • 5
  • 14

Programmatically creating the policyid / scripthash in Javascript (Browser)

I'm looking for a Javascript implementation of the CLI's cardano-cli transaction policyid function, can anyone point me in the right direction here?

Trying to replicate it with blakejs (https://github.com/dcposch/blakejs) and cbor-web (https://github.com/hildjj/node-cbor/tree/main/packages/cbor-web), but failing to get the hashes to match the output of the CLI.

Eg I want to create a PolicyID / ScriptHash from this policy script:

{
  "type": "all",
  "scripts":
  [
   {
     "type": "before",
     "slot": 42863577
   },
   {
     "type": "sig",
     "keyHash": "46a26617f9b59cc48945ccc6c0aecee732f7698fadbe8b5624b44b9e"
   }
  ]
}

(CLI gives me this policyid: 463e9264962cea64efafa576d44c8d2821d09c0c7495c253d4101a9a)

In JS I'm trying something along these lines:

let policy ='[policy as above]'
let encPolicy = cbor.encode(toHex(policy));

// Prepend 0 byte (?) (https://forum.cardano.org/t/programmatically-generate-policy-id-from-policy-script/62045)
let prepCborEnc = new Uint8Array(cborEnc.length + 1);
prepCborEnc.set(Uint8Array.of(0), 0);
prepCborEnc.set(cborEnc, 1);
encPolicy = prepCborEnc;

let blakeHex = Blake.blake2bHex(cborEnc, null, 28);
console.log(blakeHex);

(gives me this: 5db35cd45775e7d7b0d1bd5491a9baea7ac54b41772a8c96ce587579)

Any guidance is very much appreciated

sean
  • 1.5k
  • 5
  • 14