Skip to content

Expose ByteString-based text envelope deserialization#1158

Merged
Jimbo4350 merged 2 commits into
masterfrom
expose-bytestring-key-reading
Mar 27, 2026
Merged

Expose ByteString-based text envelope deserialization#1158
Jimbo4350 merged 2 commits into
masterfrom
expose-bytestring-key-reading

Conversation

@Jimbo4350
Copy link
Copy Markdown
Contributor

@Jimbo4350 Jimbo4350 commented Mar 26, 2026

Closes #556

Changelog

- description: |
    Expose deserialiseFromTextEnvelopeJSON and deserialiseFromTextEnvelopeJSONAnyOf
    which accept a raw ByteString, enabling text envelope deserialization without
    file I/O.
# uncomment types applicable to the change:
  type:
   - compatible     # the API has changed but is non-breaking
  # - feature        # introduces a new feature
  # - breaking       # the API has changed in a breaking way
  # - optimisation   # measurable performance improvements
  # - refactoring    # QoL changes
  # - bugfix         # fixes a defect
  # - test           # fixes/modifies tests
  # - maintenance    # not directly related to the code
  # - release        # related to a new release preparation
  # - documentation  # change in code docs, haddocks...
# uncomment at least one main project this PR is associated with
  projects:
   - cardano-api
  # - cardano-api-gen
  # - cardano-rpc
  # - cardano-wasm

Context

Addresses #556 — exposes key/text-envelope reading functions that work from a ByteString rather than requiring file I/O.

Adds two new public functions to Cardano.Api.Serialise.TextEnvelope:

  • deserialiseFromTextEnvelopeJSON :: HasTextEnvelope a => ByteString -> Either TextEnvelopeError a
  • deserialiseFromTextEnvelopeJSONAnyOf :: [FromSomeType HasTextEnvelope b] -> ByteString -> Either TextEnvelopeError b

These allow callers to deserialize keys (or any text-enveloped value) directly from a ByteString without going through file I/O. The existing readFileTextEnvelope and readFileTextEnvelopeAnyOf are refactored to use the new functions internally.

How to trust this PR

  • The new functions are straightforward compositions of existing Aeson.eitherDecodeStrict' and deserialiseFromTextEnvelope/deserialiseFromTextEnvelopeAnyOf.
  • The refactored readFile* functions are semantically identical — they just delegate to the new helpers.
  • The build passes with no warnings.

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

Add deserialiseFromTextEnvelopeJSON and deserialiseFromTextEnvelopeJSONAnyOf
which accept a raw ByteString, enabling text envelope deserialization without
file I/O. Refactor readFileTextEnvelope and readFileTextEnvelopeAnyOf to use
the new functions.
@Jimbo4350 Jimbo4350 marked this pull request as ready for review March 26, 2026 17:03
@Jimbo4350 Jimbo4350 force-pushed the expose-bytestring-key-reading branch from e03455c to 6ebdec2 Compare March 26, 2026 17:03
Copilot AI review requested due to automatic review settings March 26, 2026 17:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Exposes ByteString-based helpers for deserialising text envelopes from JSON, and refactors existing file-based readers to delegate to these helpers.

Changes:

  • Adds deserialiseFromTextEnvelopeJSON and deserialiseFromTextEnvelopeJSONAnyOf (ByteString → Either TextEnvelopeError …).
  • Refactors readFileTextEnvelope / readFileTextEnvelopeAnyOf to reuse the new helpers.
  • Re-exports the new functions from Cardano.Api.Serialise.TextEnvelope.

Reviewed changes

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

File Description
cardano-api/src/Cardano/Api/Serialise/TextEnvelope/Internal.hs Introduces new JSON-from-ByteString deserialisers and updates file readers to call them.
cardano-api/src/Cardano/Api/Serialise/TextEnvelope.hs Re-exports the new deserialisation helpers from the public API module.

Comment on lines +255 to +268
deserialiseFromTextEnvelopeJSON
:: HasTextEnvelope a
=> ByteString -> Either TextEnvelopeError a
deserialiseFromTextEnvelopeJSON bs = do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelope te

deserialiseFromTextEnvelopeJSONAnyOf
:: [FromSomeType HasTextEnvelope b]
-> ByteString
-> Either TextEnvelopeError b
deserialiseFromTextEnvelopeJSONAnyOf types bs = do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelopeAnyOf types te
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

These are new public(-reexported) API functions but they don’t have Haddock. Please add brief Haddock that explains: the input must be a JSON-encoded text envelope (UTF-8), this performs no file I/O, and the error behavior (TextEnvelopeAesonDecodeError for JSON parse failures vs. downstream deserialiseFromTextEnvelope* errors). This helps API consumers choose between readFile* and deserialiseFromTextEnvelopeJSON* correctly.

Copilot uses AI. Check for mistakes.
Comment on lines +258 to +260
deserialiseFromTextEnvelopeJSON bs = do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelope te
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The JSON decode step is duplicated in both functions. Consider factoring out a small helper (e.g., decodeTextEnvelopeJSON :: ByteString -> Either TextEnvelopeError TextEnvelope) and then calling deserialiseFromTextEnvelope* on the result. This reduces duplication and keeps future changes to decoding/error-wrapping consistent.

Copilot uses AI. Check for mistakes.
Comment on lines +255 to +268
deserialiseFromTextEnvelopeJSON
:: HasTextEnvelope a
=> ByteString -> Either TextEnvelopeError a
deserialiseFromTextEnvelopeJSON bs = do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelope te

deserialiseFromTextEnvelopeJSONAnyOf
:: [FromSomeType HasTextEnvelope b]
-> ByteString
-> Either TextEnvelopeError b
deserialiseFromTextEnvelopeJSONAnyOf types bs = do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelopeAnyOf types te
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The JSON decode step is duplicated in both functions. Consider factoring out a small helper (e.g., decodeTextEnvelopeJSON :: ByteString -> Either TextEnvelopeError TextEnvelope) and then calling deserialiseFromTextEnvelope* on the result. This reduces duplication and keeps future changes to decoding/error-wrapping consistent.

Suggested change
deserialiseFromTextEnvelopeJSON
:: HasTextEnvelope a
=> ByteString -> Either TextEnvelopeError a
deserialiseFromTextEnvelopeJSON bs = do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelope te
deserialiseFromTextEnvelopeJSONAnyOf
:: [FromSomeType HasTextEnvelope b]
-> ByteString
-> Either TextEnvelopeError b
deserialiseFromTextEnvelopeJSONAnyOf types bs = do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelopeAnyOf types te
decodeTextEnvelopeJSON :: ByteString -> Either TextEnvelopeError TextEnvelope
decodeTextEnvelopeJSON bs =
first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
deserialiseFromTextEnvelopeJSON
:: HasTextEnvelope a
=> ByteString -> Either TextEnvelopeError a
deserialiseFromTextEnvelopeJSON bs =
decodeTextEnvelopeJSON bs >>= deserialiseFromTextEnvelope
deserialiseFromTextEnvelopeJSONAnyOf
:: [FromSomeType HasTextEnvelope b]
-> ByteString
-> Either TextEnvelopeError b
deserialiseFromTextEnvelopeJSONAnyOf types bs =
decodeTextEnvelopeJSON bs >>= deserialiseFromTextEnvelopeAnyOf types

Copilot uses AI. Check for mistakes.
Extract shared JSON decoding into decodeTextEnvelopeJSON and add Haddock
documentation to the new public functions.
Copy link
Copy Markdown
Contributor

@palas palas left a comment

Choose a reason for hiding this comment

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

👍

@Jimbo4350 Jimbo4350 added this pull request to the merge queue Mar 27, 2026
Merged via the queue into master with commit 846306e Mar 27, 2026
34 checks passed
@Jimbo4350 Jimbo4350 deleted the expose-bytestring-key-reading branch March 27, 2026 11:58
palas pushed a commit that referenced this pull request Mar 27, 2026
Expose ByteString-based text envelope deserialization
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.

[FR] - Expose key reading functions, which work not only on files

4 participants