-
Notifications
You must be signed in to change notification settings - Fork 393
CIP-0116? | Universal JSON Encoding for Domain Types #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rphair
merged 64 commits into
cardano-foundation:master
from
klntsky:klntsky/json-spec-cip
May 14, 2024
Merged
Changes from 27 commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
cd585c9
JSON-spec: add specs for numeric types
klntsky 88fc4c9
Add Address types and PlutusData
klntsky 345d3cf
Re-implement PlutusData via oneOf
klntsky b2be44f
Switch to `tag`/`value`-based encoding of PlutusData for ease of prog…
klntsky b4e91ab
Add schema for TransactionOutput subtypes
klntsky 55a6644
Add TransactionMetadatum
klntsky 9c48c2e
More types
klntsky 55348c6
Rename cardano.json to cardano-babbage.json
klntsky 6da3763
Add NativeScript, Update, TransactionBody
klntsky a72e822
- Fix: requiredProperties -> required
klntsky 088b881
Use `format` instead of `contentEncoding`
klntsky 88ce339
Fix `pattern` for ByteString - it can be empty
klntsky 13ec4cf
Add a README, rename the schema file
klntsky 746debe
Apply suggestions from code review by Ryun1
klntsky 06cf021
Fix header layout
klntsky f01e7e7
Remove a dead link
klntsky b259b4d
Expand "Limitations", add info on encoding of binary types
klntsky 96acbaa
Fix RewardAddress prefix
klntsky b1bc4a1
Add note on uniqueness of encoding & move scope of the schema section
klntsky 07db939
Add TransactionMetadata, AuxiliaryData and Transaction types
klntsky cfd5c58
Typo: requiredProperties -> required
klntsky 6fc9917
Use uniform names for numeric types
klntsky 02ec737
Complete the definitions + fixes
klntsky 4062971
Fix PlutusScript, add titles everywhere
klntsky b6aaea7
Add a note on AuxiliaryData
klntsky ea31486
fix bracket mismatch in code sample
rphair f0d2f63
setting candidate CIP number to 116
rphair d90f1fb
Fix suggested by Evgeny: Use BigInt in PlutusData
klntsky ed7c56d
Fix suggested by Evgeny
klntsky c8b5301
Apply suggestions + fixes
klntsky 1498dd1
Assign a number
klntsky 4ab4608
Fix Mint type. Update docs for `Map` type schema.
klntsky e22fbb4
Add a link to the repo with tests
klntsky ec684bc
small tidy
Ryun1 08e8fee
fix header link
Ryun1 280fd5f
added changelog template
Ryun1 f127bf9
add notes to rationale
Ryun1 b72870c
editorial adjustments
Ryun1 26e7a36
flesh out rationale
Ryun1 ff1a9d6
Merge branch 'klntsky/json-spec-cip' into ryun1/json-spec-cip-worksho…
Ryun1 bfdc224
fix typo
Ryun1 0c18016
Update CIP-0116/README.md
Ryun1 1c93732
Update CIP-0116/README.md
Ryun1 e2ddb40
Update CIP-0116/README.md
Ryun1 865f4ef
Update CIP-0116/README.md
Ryun1 3960d0c
adjust scope rationale
Ryun1 7ce52f4
Update CIP-0116/README.md
Ryun1 08e9ebb
Update cardano-babbage.json
klntsky 4b042df
Format
klntsky caadc7c
Fix incorrectly specified required field
klntsky 987aa28
Fix incorrectly specified required Mint properties
klntsky 144e049
Add missing Array wrapper for Mint assets
klntsky eab98df
Add new formats
klntsky cdc6b6b
Specify length for BootstrapWitness chain_code
klntsky 16c9e4d
Move PoolMetadataHash to a separate type
klntsky c081411
Specify VRFCert proof length
klntsky 89736cf
Remove legacy HeaderLeaderCert
klntsky cc391f2
Inline AuxiliaryDataSet type
klntsky 820350b
Update 'Path to active'
klntsky 86d9692
Merge branch 'klntsky/json-spec-cip' into ryun1/json-spec-cip-worksho…
Ryun1 71593aa
Update CIP-0116/README.md
Ryun1 d11978a
Add myself to implementors
klntsky 15fc23a
Use PoolPubKeyHash instead of Ed25519KeyHash in stake delegation cert
klntsky ac0ad54
Merge pull request #3 from Ryun1/ryun1/json-spec-cip-workshop-feedback
klntsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| --- | ||
| CIP: 116 | ||
| Title: Standard JSON encoding for Domain Types | ||
| Category: Tools | ||
| Status: Proposed | ||
| Authors: | ||
| - Vladimir Kalnitsky <klntsky@gmail.com> | ||
| Implementors: [] | ||
| Discussions: | ||
| - https://github.com/cardano-foundation/cips/pulls/766 | ||
| Created: 2024-02-22 | ||
| License: CC-BY-4.0 | ||
| --- | ||
|
|
||
| ## Abstract | ||
|
|
||
| Canonical JSON encoding for Cardano domain types lets the ecosystem converge on a single way of serializing data to JSON, thus freeing the developers from repeating roughly the same, but slightly different encoding/decoding logic over and over. | ||
|
|
||
| ## Motivation: why is this CIP necessary? | ||
|
|
||
| Cardano domain types have canonical CDDL definitions (for every era), but when it comes to use in web apps, where JSON is the universally accepted format, there is no definite standard. This CIP aims to change that. | ||
|
|
||
| The full motivation text is provided in [CPS-11 | Universal JSON Encoding for Domain Types](https://github.com/cardano-foundation/CIPs/pull/742). | ||
|
|
||
| ## Specification | ||
|
|
||
| This CIP is expected to contain multiple schema definitions for Cardano Eras starting from Babbage. | ||
|
klntsky marked this conversation as resolved.
Outdated
|
||
|
|
||
| - [Babbage](./cardano-babbage.json) | ||
|
|
||
| ### Scope of the Schema | ||
|
|
||
| The schemas should cover `Block` type and all of its structural components, which corresponds to the scope of CDDL files located in [the ledger repo](https://github.com/IntersectMBO/cardano-ledger/). | ||
|
|
||
| ### Schema Design Principles | ||
|
|
||
| Below you can find some principles outlining the process of schema creation / modification. They are intended to be applied when there is a need to create a schema for a new Cardano era. | ||
|
|
||
| #### Uniqueness of encoding | ||
|
|
||
| Every transaction (i.e. CBOR-encoded binary) must have exactly one valid JSON encoding, up to entry ordering in mappings (that are represented as key-value pairs). | ||
|
|
||
| For a single JSON fixture, however, there are multiple variants of encoding it as CBOR. | ||
|
|
||
| #### Consistency with the previous versions | ||
|
|
||
| To simplify transitions of dApps between eras, the scope of changes introduced to the schemas SHOULD be limited to the scope of CDDL changes. | ||
|
|
||
|
|
||
| #### Absence of extensibility | ||
|
|
||
| The schemas MUST NOT be extensible with additional properties. This may sound counter-intuitive and against the spirit of json-schema, but there are some motivations behind that: | ||
|
|
||
| - More safety from typos: object fields that are optional may be specified with slightly incorrect names in dApps' code, leading to inability of the decoders to pick up the values, which may go unnoticed. | ||
| - Clear delineation between Cardano domain types and user dApp domain types: forcing the developers to store their dApp domain data separately from Cardano data, or close to it (as opposed to mixing these together in a single object) will indirectly motivate better structured dApp code. | ||
|
|
||
| ### Schema Conventions | ||
|
|
||
| These conventions help to keep the schema uniform in style. | ||
|
|
||
| #### Encoding of binary types | ||
|
|
||
| Binary data MUST be encoded as lower-case hexademical strings. Restricting the character set to lower-case letters (`a-f`) allows for comparisons and equality checks without the need to normalize the values to a uniform case. | ||
|
|
||
| #### Encoding of mapping types | ||
|
|
||
| `Map`-like container types MUST be encoded as arrays of key-value pairs. Uniqueness of `"key"` objects in a map MUST be preserved (but this property is not expressible via a schema). | ||
|
|
||
| ```json | ||
| "Map": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "object", | ||
| "properties": { | ||
| "key": ..., | ||
| "value": ... | ||
| }, | ||
| "additionalProperties": false | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Implementations MUST consider mappings with conflicting keys invalid. | ||
|
|
||
| #### Encoding of variant types | ||
|
|
||
| Encoding types with variable payloads MUST be done with the use of `oneOf` and an explicit discriminator property: `tag`: | ||
|
|
||
| ```json | ||
| "Credential": { | ||
| "type": "object", | ||
| "discriminator": { | ||
| "propertyName": "tag" | ||
| }, | ||
| "oneOf": [ | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "tag": { | ||
| "enum": [ | ||
| "pubkey_hash" | ||
| ] | ||
| }, | ||
| "value": { | ||
| "$ref": "cardano-babbage.json#/definitions/Ed25519KeyHash" | ||
| } | ||
| }, | ||
| "required": ["tag", "value"], | ||
| "additionalProperties": false | ||
| }, | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "tag": { | ||
| "enum": [ | ||
| "script_hash" | ||
| ] | ||
| }, | ||
| "value": { | ||
| "$ref": "cardano-babbage.json#/definitions/ScriptHash" | ||
| } | ||
| }, | ||
| "required": ["tag", "value"], | ||
| "additionalProperties": false | ||
| } | ||
| ] | ||
| }, | ||
| ``` | ||
|
|
||
| Other properties of a tagged object MUST be specified in lower-case snake-case. | ||
|
|
||
| #### Encoding of enum types | ||
|
|
||
| Enums are a special kind of variant types that carry no payloads. These MUST be encoded as string `enum`s. | ||
|
|
||
| Lowercase snake case identifiers MUST be used for the options, e.g.: | ||
|
|
||
| ```json | ||
| "Language": { | ||
| "title": "Language", | ||
| "type": "string", | ||
| "enum": [ | ||
| "plutus_v1", | ||
| "plutus_v2" | ||
| ] | ||
| }, | ||
| ``` | ||
|
|
||
| #### Encoding of record types | ||
|
|
||
| All record types MUST be encoded as objects with explicit list of `required` properties, and `additionalProperties` set to `false` (see "absence of extensibility" chapter for the motivation behind this suggestion). | ||
|
|
||
| #### Encoding of nominal type synonyms | ||
|
|
||
| Some of the types have identical representations, differing only by nominal name. For example, `Slot` domain type is expressed as `uint` in CDDL. | ||
|
|
||
| For these types, their nominal name SHOULD NOT have a separate definition in the json-schema, and the "representation type" should be used via a `$ref` instead. The domain type name SHOULD be included as `title` string at the point of usage. | ||
|
|
||
| ### Additional format types | ||
|
|
||
| Some non-standard `format` types are used: | ||
|
|
||
| - `hex` | ||
| - `bech32` | ||
| - `base58` | ||
|
|
||
| TODO: describe the formats | ||
|
|
||
| ### Limitations | ||
|
|
||
| JSON-schema does not allow to express certain properties of some of the types. | ||
|
|
||
| #### Uniqueness of mapping keys | ||
|
|
||
| See the chapter on encoding of mapping types. | ||
|
|
||
| #### Bech32 and Base58 formats | ||
|
|
||
| Validity of values of these types can't be expressed as a regular expression, so the implementations MAY validate them separately. | ||
|
|
||
| #### Address types | ||
|
|
||
| Bech32 strings are not always valid addresses: even if the prefixes are correct, the [binary layout of the payload](https://github.com/IntersectMBO/cardano-ledger/blob/f754084675a1decceed4f309814b09605f443dd5/libs/cardano-ledger-core/src/Cardano/Ledger/Address.hs#L603) must also be valid. | ||
|
|
||
| The implementations MAY validate it separately. | ||
|
|
||
| #### Byte length limits for strings | ||
|
|
||
| In CDDL, the length of a `tstr` value gives the number of bytes, but in `json-schema` there is no way to specify restrictions on byte lengths. So, `maxLength` is not the correct way of specifying the limits, but it is still useful, because no string longer than 64 *characters* satisfies the 64-byte limit. | ||
|
|
||
| #### Auxiliary Data encoding | ||
|
|
||
| `auxiliary_data` CDDL type is handled specially. | ||
|
|
||
| ```cddl | ||
| auxiliary_data = | ||
| metadata ; Shelley | ||
| / [ transaction_metadata: metadata ; Shelley-ma | ||
| , auxiliary_scripts: [ * native_script ] | ||
| ] | ||
| / #6.259({ ? 0 => metadata ; Alonzo and beyond | ||
| , ? 1 => [ * native_script ] | ||
| , ? 2 => [ * plutus_v1_script ] | ||
| , ? 3 => [ * plutus_v2_script ] | ||
| }) | ||
| ``` | ||
|
|
||
| Instead of providing all three variants of encoding, we base the schema on the one that is the most general (the last one): | ||
|
|
||
| ```json | ||
| "AuxiliaryData": { | ||
| "properties": { | ||
| "metadata": { | ||
| "$ref": "cardano-babbage.json#/definitions/TransactionMetadata" | ||
| }, | ||
| "native_scripts": { | ||
| "type": "array", | ||
| "items": { | ||
| "$ref": "cardano-babbage.json#/definitions/NativeScript" | ||
| } | ||
| }, | ||
| "plutus_scripts": { | ||
| "type": "array", | ||
| "items": { | ||
| "$ref": "cardano-babbage.json#/definitions/PlutusScript" | ||
| } | ||
| } | ||
| }, | ||
|
rphair marked this conversation as resolved.
|
||
| }, | ||
| ``` | ||
|
|
||
| It is up to implementors to decide how to serialize the values into CBOR. The property we want to maintain is preserved regardless of the choice: for every block binary there is exactly one JSON encoding. | ||
|
|
||
| ## Rationale: how does this CIP achieve its goals? | ||
|
|
||
| ## Path to Active | ||
|
|
||
| - [ ] Complete the specification | ||
| - [ ] Provide an implementation of validating functions that uses this json-schema | ||
| - [ ] Collect a list of cardano domain types implementations and negotiate transition to the specified formats with maintainers (if it makes sense and is possible) | ||
|
|
||
| ### Acceptance Criteria | ||
|
|
||
|
Ryun1 marked this conversation as resolved.
Outdated
|
||
| ### Implementation Plan | ||
| <!-- A plan to meet those criteria. Or `N/A` if not applicable. --> | ||
|
Ryun1 marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Copyright | ||
|
|
||
| This CIP is licensed under [CC-BY-4.0](https://creativecommons.org/licenses/by/4.0/legalcode). | ||
|
|
||
| [CC-BY-4.0]: https://creativecommons.org/licenses/by/4.0/legalcode | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.