Skip to content

Add readAnyScriptBytes and readFileAnyScript to the experimental API#1245

Open
Jimbo4350 wants to merge 3 commits into
masterfrom
read-file-any-script
Open

Add readAnyScriptBytes and readFileAnyScript to the experimental API#1245
Jimbo4350 wants to merge 3 commits into
masterfrom
read-file-any-script

Conversation

@Jimbo4350

Copy link
Copy Markdown
Contributor

Context

Adds script reading to the experimental API, so downstream consumers (e.g. cardano-cli) can decode a script file without knowing its language or serialisation format up front:

  • readAnyScriptBytes decodes an AnyScript (LedgerEra era) from bytes: it first tries a TextEnvelope, using the envelope type to select Plutus (decodeAnyPlutusScript) or simple script (deserialiseSimpleScript) CBOR decoding, and falls back to the legacy JSON-only simple script encoding for inputs that are not text envelopes.
  • readFileAnyScript is the file-reading wrapper, returning FileError AnyScriptDecodeError.
  • AnyScriptDecodeError is the new error type covering the JSON and CBOR failure cases, with an Error instance.

The legacy JSON fallback uses the established TODO Dijkstra placeholder for the Dijkstra era, consistent with the rest of the experimental API.

How to trust this PR

cabal build cardano-api -j4

The change is additive: no existing functions are modified, only new exports from Cardano.Api.Experimental.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated
  • Self-reviewed the diff
  • Changelog fragment added in .changes/

Introduce AnyScriptDecodeError and functions to decode an AnyScript
from a text envelope (simple or Plutus script CBOR) with a fallback
to the legacy JSON simple script encoding.
Copilot AI review requested due to automatic review settings July 8, 2026 15:56
Jimbo4350 added a commit that referenced this pull request Jul 8, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the Cardano.Api.Experimental surface with helpers to decode “any script” from bytes or from a file, without the caller needing to know the script language or encoding upfront. It centralizes the logic for detecting text-envelope CBOR (Plutus vs simple scripts) and falling back to the legacy JSON simple-script encoding.

Changes:

  • Added readAnyScriptBytes to decode AnyScript (LedgerEra era) from bytes via text envelope (CBOR) with legacy JSON fallback.
  • Added readFileAnyScript to read bytes from disk and return FileError AnyScriptDecodeError on failure.
  • Introduced AnyScriptDecodeError to represent JSON/CBOR decode failures (and exported it from Cardano.Api.Experimental).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
cardano-api/src/Cardano/Api/Experimental/AnyScript.hs Adds AnyScriptDecodeError, plus byte/file decoding entry points for AnyScript.
cardano-api/src/Cardano/Api/Experimental.hs Re-exports the new decoding API and error type from the experimental umbrella module.
.changes/20260708_add_read_any_script.yml Adds a changelog fragment describing the new experimental API feature.

Comment on lines +167 to +170
case era of
DijkstraEra -> error "TODO Dijkstra: Simple script not supported"
ConwayEra ->
obtainConwayConstraints era $
Comment on lines +131 to +134
data AnyScriptDecodeError
= AnyScriptJsonError JsonDecodeError
| AnyScriptCborError CBOR.DecoderError
deriving Show
Comment on lines +136 to +138
instance Error AnyScriptDecodeError where
prettyError (AnyScriptJsonError e) = prettyError e
prettyError (AnyScriptCborError e) = prettyError e
Comment on lines +140 to +147
-- | Decode an 'AnyScript' from its serialised form: a text envelope wrapping
-- the CBOR encoding of either a simple or a Plutus script, or (as a fallback)
-- the legacy JSON-only encoding of a simple script.
readAnyScriptBytes
:: forall era
. Era era
-> ByteString
-> Either AnyScriptDecodeError (AnyScript (LedgerEra era))
@Jimbo4350 Jimbo4350 force-pushed the read-file-any-script branch from 93cadef to 233ee93 Compare July 8, 2026 16:25
Cover the three decode paths: Plutus script text envelopes written with
serialiseToTextEnvelope, hand-built simple script text envelopes (simple
scripts have no text envelope format of their own), and the legacy JSON
simple script fallback. Also roundtrip readFileAnyScript through a
temporary file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants