|
| 1 | +# Data |
| 2 | + |
| 3 | +Data module provides functionality for working with data types in Cardano. |
| 4 | + |
| 5 | +## Data Example |
| 6 | + |
| 7 | +```typescript |
| 8 | +import { Data } from "@evolution-sdk/evolution" |
| 9 | + |
| 10 | +// Create a complex nested data structure with: |
| 11 | +// - Constructor with index 1 containing multiple fields |
| 12 | +// - Nested constructors with different indices |
| 13 | +// - A Map with mixed key-value pairs |
| 14 | +// - An array of numbers |
| 15 | +const nestedUnsortedData = new Data.Constr({ |
| 16 | + index: 1n, |
| 17 | + fields: [ |
| 18 | + // Nested constructor: 121_0([123_0([])]) |
| 19 | + new Data.Constr({ |
| 20 | + index: 0n, |
| 21 | + fields: [ |
| 22 | + new Data.Constr({ |
| 23 | + index: 2n, |
| 24 | + fields: [] |
| 25 | + }) |
| 26 | + ] |
| 27 | + }), |
| 28 | + // Map with unsorted keys (will be sorted in canonical mode) |
| 29 | + new Map<Data.Data, Data.Data>([ |
| 30 | + ["deadbeef01", new Data.Constr({ index: 0n, fields: [] })], |
| 31 | + ["beef", 19n], |
| 32 | + ["deadbeef03", new Data.Constr({ index: 1n, fields: [] })] |
| 33 | + ]), |
| 34 | + // Array of numbers |
| 35 | + [10n, 5n, 2n, 3n, 1n, 4n] |
| 36 | + ] |
| 37 | +}) |
| 38 | + |
| 39 | +// Encode using default codec (indefinite-length encoding) |
| 40 | +const cborHex = Data.Codec().Encode.cborHex(nestedUnsortedData) |
| 41 | +// Output: d87a9fd8799fd87b80ffbf45deadbeef01d8798042beef1345deadbeef03d87a80ff9f0a0502030104ffff |
| 42 | + |
| 43 | +// CBOR diagnostic notation (indefinite-length): |
| 44 | +// 122_0([_ |
| 45 | +// 121_0([_ 123_0([])]), |
| 46 | +// {_ |
| 47 | +// h'deadbeef01': 121_0([]), |
| 48 | +// h'beef': 19, |
| 49 | +// h'deadbeef03': 122_0([]), |
| 50 | +// }, |
| 51 | +// [_ 10, 5, 2, 3, 1, 4], |
| 52 | +// ]) |
| 53 | +// Visualize at: https://cbor.nemo157.com/ |
| 54 | + |
| 55 | +const decodedData = Data.Codec().Decode.cborHex(cborHex) |
| 56 | + |
| 57 | +// Create a canonical codec for deterministic encoding |
| 58 | +// This ensures consistent output and sorted map keys |
| 59 | +const canonicalCodec = Data.Codec({ |
| 60 | + options: { |
| 61 | + mode: "canonical" |
| 62 | + } |
| 63 | +}) |
| 64 | + |
| 65 | +// Encode using canonical mode (definite-length, sorted keys) |
| 66 | +const canonicalCborHex = canonicalCodec.Encode.cborHex(nestedUnsortedData) |
| 67 | +// Output: d87a83d87981d87b80a342beef1345deadbeef01d8798045deadbeef03d87a80860a0502030104 |
| 68 | + |
| 69 | +// CBOR diagnostic notation (canonical/definite-length): |
| 70 | +// 122_0([ |
| 71 | +// 121_0([123_0([])]), |
| 72 | +// { |
| 73 | +// h'beef': 19, ← Keys are now sorted |
| 74 | +// h'deadbeef01': 121_0([]), |
| 75 | +// h'deadbeef03': 122_0([]), |
| 76 | +// }, |
| 77 | +// [10, 5, 2, 3, 1, 4], ← Definite-length array |
| 78 | +// ]) |
| 79 | + |
| 80 | +// Verify that decoding works correctly |
| 81 | +assert.deepStrictEqual(decodedData, nestedUnsortedData, "Decoded data should match original") |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +## API Reference |
| 86 | + |
| 87 | +For detailed API documentation, see the generated TypeDoc documentation. |
0 commit comments