Skip to content

Commit 2a961be

Browse files
authored
Merge pull request #783 from IntersectMBO/mgalazyn/doc/add-clarifications-scriptdatafromjsondetailedschema
Add warning for deserialisation issues for HashableScriptData
2 parents 526f4d5 + 2cb0532 commit 2a961be

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cardano-api/src/Cardano/Api/Internal/ScriptData.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ instance Error ScriptDataRangeError where
320320
-- can round-trip the JSON.
321321
--
322322
-- The subset of JSON supported is all JSON except:
323+
--
323324
-- * No null or bool values
324325
-- * No floating point, only integers in the range of a 64bit signed integer
325326
-- * A limitation on string lengths
@@ -345,6 +346,20 @@ instance Error ScriptDataRangeError where
345346
-- precisely. It also means any script data can be converted into the JSON and
346347
-- back without loss. That is we can round-trip the script data via the JSON and
347348
-- also round-trip schema-compliant JSON via script data.
349+
--
350+
-- *Warning*: While the JSON representation does round-trip, the CBOR through
351+
-- JSON does not. When serialising and deserialising 'HashableScriptData'
352+
-- through JSON e.g:
353+
-- @
354+
-- CBOR -> HashableScriptData -> JSON -> HashableScriptData -> CBOR
355+
-- @
356+
-- the original CBOR representation is lost and the resulting CBOR *will*
357+
-- be different resulting in a different script data hash, which is
358+
-- calculated from CBOR. This is because cardano-ledger does not canonicalise
359+
-- CBOR representation, so you can have few slightly different serialised
360+
-- representations of a data structure, which represent the same value.
361+
--
362+
-- See: https://github.com/IntersectMBO/cardano-api/issues/612#issuecomment-2701256007
348363
data ScriptDataJsonSchema
349364
= -- | Use the \"no schema\" mapping between JSON and script data as
350365
-- described above.
@@ -507,6 +522,10 @@ scriptDataJsonToHashable schema scriptDataVal = do
507522
-- JSON conversion using the "detailed schema" style
508523
--
509524

525+
-- | Convert a script data value into JSON using detailed schema.
526+
--
527+
-- This conversion is total but is not necessarily invertible.
528+
-- See 'ScriptDataJsonSchema' for the details.
510529
scriptDataToJsonDetailedSchema :: HashableScriptData -> Aeson.Value
511530
scriptDataToJsonDetailedSchema = conv . getScriptData
512531
where
@@ -538,6 +557,10 @@ scriptDataToJsonDetailedSchema = conv . getScriptData
538557

539558
singleFieldObject name v = Aeson.object [(name, v)]
540559

560+
-- | Convert a script data value from JSON using detailed schema.
561+
--
562+
-- This conversion is total but is not necessarily invertible.
563+
-- See 'ScriptDataJsonSchema' for the details.
541564
scriptDataFromJsonDetailedSchema
542565
:: Aeson.Value
543566
-> Either

0 commit comments

Comments
 (0)