2

I'm working with Plutus Core smart contracts, and I have the binary encoding of a Plutus Core script. I'm curious to explore the higher-level Plutus code or any human-readable representation of the script. Does anyone have insights on how to decompile or disassemble the binary encoding back into untyped Plutus code or something higher-level than bytecode?

I appreciate any guidance or recommendations on tools, techniques, or resources that can assist in this process.

Thank you!

2 Answers 2

0

I see you used the plu-ts tag, I assume you are working with it.

plu-ts re-exports the @harmoniclabs/uplc module](https://github.com/harmoniclabs/uplc/) as is.

so you have it already with plu-ts

as you can read from the uplc module readme, there are functionalities to parse the bytes of the contract and print a prettified uplc textual form

this is the example reported in the package

import { fromHex } from "@harmoniclabs/uint8array-utils";
import { parseUPLC, prettyUPLC } from "@harmoniclabs/uplc";

const serialized: Uint8Array = fromHex( "0100003233700900219b8248050005200801" );

const program = parseUPLC( serialized, "flat" );

console.log(
    prettyUPLC(
        program.body, // UPLCTerm 
        4 // indentation spaces
    )
);
/*
expected output:

[
    (lam a 
        [
            [
                (builtin addInteger) 
                (con integer 2)
            ] 
            [
                [
                    (builtin multiplyInteger) 
                    (con integer 10)
                ] 
                a
            ]
        ]
    ) 
    (con integer 4)
]
    
*/

you probably don't need to specify "flat" as second argument of parseUPlC as I assume you are working with cbor.

have fun

1

It will depend on the format used. I'll give an example of converting the text envelope file format used by cardano-cli into PlutusIR. The text envelope contains the plutus flat binary format that is double-wrapped in cbor hex format.

This is the example text envelope script json file:

{ "type": "PlutusScriptV2",
  "description": "",
  "cborHex": "59082e59082b010000323232323322323232323232323232332232323232322232325335323232325335001101a133573892011077726f6e6720746f6b656e206e616d650001932333573466e24005200001901a32323230010033200135501f223350014800088d4008894cd4ccd5cd19b8f0020070210201300a0011300600335002223333500123263201f335738921024c680001f200123263201f3357389201024c680001f23263201f3357389201024c680001f323500122222222222200835001220023333573466e1cd55cea80224000466442466002006004646464646464646464646464646666ae68cdc39aab9d500c480008cccccccccccc88888888888848cccccccccccc00403403002c02802402001c01801401000c008cd405805cd5d0a80619a80b00b9aba1500b33501601835742a014666aa034eb94064d5d0a804999aa80d3ae501935742a01066a02c0426ae85401cccd54068089d69aba150063232323333573466e1cd55cea801240004664424660020060046464646666ae68cdc39aab9d5002480008cc8848cc00400c008cd40b1d69aba15002302d357426ae8940088c98c80c4cd5ce01901881789aab9e5001137540026ae854008c8c8c8cccd5cd19b8735573aa004900011991091980080180119a8163ad35742a004605a6ae84d5d1280111931901899ab9c03203102f135573ca00226ea8004d5d09aba2500223263202d33573805c05a05626aae7940044dd50009aba1500533501675c6ae854010ccd540680788004d5d0a801999aa80d3ae200135742a00460406ae84d5d1280111931901499ab9c02a029027135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d55cf280089baa00135742a00860206ae84d5d1280211931900d99ab9c01c01b0193200135501b223350014800088d4008894cd4ccd5cd19b8f00200701d01c100113006003375c0082030264c6403066ae712410350543500018135573ca00226ea800448c88c008dd6000990009aa80b111999aab9f0012500a233500930043574200460066ae880080588c8c8cccd5cd19b8735573aa004900011991091980080180118061aba150023005357426ae8940088c98c8058cd5ce00b80b00a09aab9e5001137540024646464646666ae68cdc39aab9d5004480008cccc888848cccc00401401000c008c8c8c8cccd5cd19b8735573aa0049000119910919800801801180a9aba1500233500f014357426ae8940088c98c806ccd5ce00e00d80c89aab9e5001137540026ae854010ccd54021d728039aba150033232323333573466e1d4005200423212223002004357426aae79400c8cccd5cd19b875002480088c84888c004010dd71aba135573ca00846666ae68cdc3a801a400042444006464c6403a66ae7007807406c0680644d55cea80089baa00135742a00466a016eb8d5d09aba2500223263201733573803002e02a26ae8940044d5d1280089aab9e500113754002266aa002eb9d6889119118011bab00132001355013223233335573e0044a010466a00e66442466002006004600c6aae754008c014d55cf280118021aba200301413574200222440042442446600200800624464646666ae68cdc3a800a40004642446004006600a6ae84d55cf280191999ab9a3370ea0049001109100091931900919ab9c01301201000f135573aa00226ea80048c8c8cccd5cd19b875001480188c848888c010014c01cd5d09aab9e500323333573466e1d400920042321222230020053009357426aae7940108cccd5cd19b875003480088c848888c004014c01cd5d09aab9e500523333573466e1d40112000232122223003005375c6ae84d55cf280311931900919ab9c01301201000f00e00d135573aa00226ea80048c8c8cccd5cd19b8735573aa004900011991091980080180118029aba15002375a6ae84d5d1280111931900719ab9c00f00e00c135573ca00226ea80048c8cccd5cd19b8735573aa002900011bae357426aae7940088c98c8030cd5ce00680600509baa001232323232323333573466e1d4005200c21222222200323333573466e1d4009200a21222222200423333573466e1d400d2008233221222222233001009008375c6ae854014dd69aba135744a00a46666ae68cdc3a8022400c4664424444444660040120106eb8d5d0a8039bae357426ae89401c8cccd5cd19b875005480108cc8848888888cc018024020c030d5d0a8049bae357426ae8940248cccd5cd19b875006480088c848888888c01c020c034d5d09aab9e500b23333573466e1d401d2000232122222223005008300e357426aae7940308c98c8054cd5ce00b00a80980900880800780700689aab9d5004135573ca00626aae7940084d55cf280089baa0012323232323333573466e1d400520022333222122333001005004003375a6ae854010dd69aba15003375a6ae84d5d1280191999ab9a3370ea0049000119091180100198041aba135573ca00c464c6401c66ae7003c03803002c4d55cea80189aba25001135573ca00226ea80048c8c8cccd5cd19b875001480088c8488c00400cdd71aba135573ca00646666ae68cdc3a8012400046424460040066eb8d5d09aab9e500423263200b33573801801601201026aae7540044dd500089119191999ab9a3370ea00290021091100091999ab9a3370ea00490011190911180180218031aba135573ca00846666ae68cdc3a801a400042444004464c6401866ae700340300280240204d55cea80089baa0012323333573466e1d40052002200523333573466e1d40092000200523263200833573801201000c00a26aae74dd5000891001091000a4c240029210350543100112323001001223300330020020011"}
  1. Extract the cborHex value to a new file "cbor-script-file".
  2. Remove the cbor headers (first 6 bytes). The value now starts with 10000 indicating the plutus compiler version used (1.0.0).
  3. Convert this to binary format and write to a new file by running cat cbor-script-file | xxd -r -p > binary-script-file
  4. Run the follow cmd from the plutus repo to convert to PlutusIR format (executable is available in nix shell) cabal run uplc convert -- --if flat -i binary-script-file

The PlutusIR output looks like

(program
  1.0.0
  [
  (lam
    i_0
    [
      (lam
        i_1
        [
          (lam
            i_2
            ...

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.