@helios-lang/contract-utils 0.2.46 → 0.2.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helios-lang/contract-utils",
3
- "version": "0.2.46",
3
+ "version": "0.2.48",
4
4
  "description": "Convenience and type-safety utilities for using Helios validators from within Typescript",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
package/src/cast/Cast.js CHANGED
@@ -19,7 +19,7 @@ import {
19
19
  ValidatorHash,
20
20
  Value
21
21
  } from "@helios-lang/ledger"
22
- import { None, expectSome } from "@helios-lang/type-utils"
22
+ import { None, expectSome, isSome } from "@helios-lang/type-utils"
23
23
  import {
24
24
  ByteArrayData,
25
25
  ConstrData,
@@ -220,13 +220,36 @@ function schemaToUplc(schema, x, defs = {}) {
220
220
  schemaToUplc(type, x[name])
221
221
  )
222
222
  )
223
- case "map":
224
- return new MapData(
225
- schema.fieldTypes.map(({ name, type }) => [
226
- new ByteArrayData(encodeUtf8(name)),
227
- schemaToUplc(type, x[name], defs)
228
- ])
229
- )
223
+ case "map": {
224
+ // first make sure all fields are present
225
+ schema.fieldTypes.forEach((ft) => {
226
+ if (!(ft.name in x)) {
227
+ throw new Error(`missing field ${ft.name}`)
228
+ }
229
+ })
230
+
231
+ // respect order of entries
232
+ /**
233
+ * @type {[UplcData, UplcData][]}
234
+ */
235
+ const pairs = []
236
+
237
+ Object.entries(x).forEach(([key, value]) => {
238
+ const ft = schema.fieldTypes.find(
239
+ (ft) => ft.name == key
240
+ )
241
+
242
+ if (ft) {
243
+ const keyData = new ByteArrayData(encodeUtf8(key))
244
+ const valueData = schemaToUplc(ft.type, value, defs)
245
+
246
+ pairs.push([keyData, valueData])
247
+ }
248
+ })
249
+
250
+ // wrapped in ConstrData
251
+ return new ConstrData(0, [new MapData(pairs)])
252
+ }
230
253
  default:
231
254
  throw new Error(
232
255
  `unhandled struct format '${schema.format}'`
@@ -404,7 +427,10 @@ function uplcToSchema(schema, data, config, defs = {}) {
404
427
  )
405
428
  }
406
429
  case "map": {
407
- const entries = MapData.expect(data).items
430
+ // wrapped in ConstrData
431
+ const entries = MapData.expect(
432
+ ConstrData.expect(data).fields[0]
433
+ ).items
408
434
 
409
435
  if (entries.length != schema.fieldTypes.length) {
410
436
  throw new Error(
@@ -412,22 +438,34 @@ function uplcToSchema(schema, data, config, defs = {}) {
412
438
  )
413
439
  }
414
440
 
415
- return new Map(
416
- schema.fieldTypes.map(({ name, type }) => {
417
- const pair = expectSome(
418
- entries.find(
419
- ([key, _]) =>
420
- decodeUtf8(
421
- ByteArrayData.expect(key).bytes
422
- ) == name
441
+ return Object.fromEntries(
442
+ schema.fieldTypes
443
+ .map(({ name, type }) => {
444
+ const i = expectSome(
445
+ entries.findIndex(
446
+ ([key, _]) =>
447
+ decodeUtf8(
448
+ ByteArrayData.expect(key).bytes
449
+ ) == name
450
+ )
423
451
  )
424
- )
425
452
 
426
- return [
427
- name,
428
- uplcToSchema(type, pair[1], config, defs)
429
- ]
430
- })
453
+ if (i == -1) {
454
+ throw new Error(
455
+ `field ${name} not found in MapData`
456
+ )
457
+ }
458
+
459
+ const pair = entries[i]
460
+
461
+ return /** @type {const} */ ([
462
+ i,
463
+ name,
464
+ uplcToSchema(type, pair[1], config, defs)
465
+ ])
466
+ })
467
+ .sort((a, b) => a[0] - b[0])
468
+ .map(([_, name, x]) => [name, x])
431
469
  )
432
470
  }
433
471
  default:
@@ -1 +1 @@
1
- {"version":3,"file":"Cast.d.ts","sourceRoot":"","sources":["../../src/cast/Cast.js"],"names":[],"mappings":"AAseA;;;;;;GAMG;AACH,kGAHW,UAAU,8BASpB;AA/cD;;;GAGG;AAEH;;;;GAIG;AAEH;;;;GAIG;AAEH;;;;;GAKG;AAEH;;;GAGG;AACH;IAaI;;;OAGG;IACH,oBAHW,UAAU,UACV,UAAU,EAKpB;IAnBD;;;OAGG;IACH,iBAFU,UA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