How to export scripts, datums and redemmers

Note

This guide uses the scripts from the basic validators tutorial.

While most Plutus Application s use scripts by directly submitting them as part of transactions from the application itself, it can be useful to be able to export a serialized script. For example, you might want to submit it as part of a manually created transaction with the Cardano node CLI, send it to another party for them to use, or maybe just back it up.

Fortunately, it is quite simple to do this. You can either translate directly into CBOR using the Serialise typeclass from the serialise package, or you can create an envelope of the sort used by the Cardano node CLI.

The same can also be done with Datum and Redeemer datatypes.

serializedDateValidator :: BSL.ByteString
serializedDateValidator = serialise dateValidator

-- The module 'Ledger.Scripts' includes instances related to typeclass
-- 'Cardano.Api.HasTextEnvelope'

-- Envelope of the PLC 'Script'.
envelopeDateValidator :: TextEnvelope
envelopeDateValidator = serialiseToTextEnvelope Nothing (getValidator dateValidator)

-- Envelope of the 'Datum' representing the 'Date' datatype.
envelopeDate :: Date -> TextEnvelope
envelopeDate d = serialiseToTextEnvelope Nothing (Datum $ toBuiltinData d)

-- Envelope of the 'Redeemer' representing the 'EndDate' datatype.
envelopeEndDate :: EndDate -> TextEnvelope
envelopeEndDate d = serialiseToTextEnvelope Nothing (Redeemer $ toBuiltinData d)

main :: IO ()
main = do
  print serializedDateValidator
  print envelopeDateValidator
  print $ envelopeDate (Date 0)
  print $ envelopeEndDate Never