Add readAnyScriptBytes and readFileAnyScript to the experimental API#1245
Open
Jimbo4350 wants to merge 3 commits into
Open
Add readAnyScriptBytes and readFileAnyScript to the experimental API#1245Jimbo4350 wants to merge 3 commits into
Jimbo4350 wants to merge 3 commits into
Conversation
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.
Jimbo4350
added a commit
that referenced
this pull request
Jul 8, 2026
Contributor
There was a problem hiding this comment.
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
readAnyScriptBytesto decodeAnyScript (LedgerEra era)from bytes via text envelope (CBOR) with legacy JSON fallback. - Added
readFileAnyScriptto read bytes from disk and returnFileError AnyScriptDecodeErroron failure. - Introduced
AnyScriptDecodeErrorto represent JSON/CBOR decode failures (and exported it fromCardano.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)) |
93cadef to
233ee93
Compare
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.
233ee93 to
d0c3a0f
Compare
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:readAnyScriptBytesdecodes anAnyScript (LedgerEra era)from bytes: it first tries aTextEnvelope, 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.readFileAnyScriptis the file-reading wrapper, returningFileError AnyScriptDecodeError.AnyScriptDecodeErroris the new error type covering the JSON and CBOR failure cases, with anErrorinstance.The legacy JSON fallback uses the established
TODO Dijkstraplaceholder for the Dijkstra era, consistent with the rest of the experimental API.How to trust this PR
The change is additive: no existing functions are modified, only new exports from
Cardano.Api.Experimental.Checklist
.changes/