@helios-lang/contract-utils 0.2.46 → 0.2.47

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.47",
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,35 @@ 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
+ return new MapData(pairs)
251
+ }
230
252
  default:
231
253
  throw new Error(
232
254
  `unhandled struct format '${schema.format}'`
@@ -412,22 +434,34 @@ function uplcToSchema(schema, data, config, defs = {}) {
412
434
  )
413
435
  }
414
436
 
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
437
+ return Object.fromEntries(
438
+ schema.fieldTypes
439
+ .map(({ name, type }) => {
440
+ const i = expectSome(
441
+ entries.findIndex(
442
+ ([key, _]) =>
443
+ decodeUtf8(
444
+ ByteArrayData.expect(key).bytes
445
+ ) == name
446
+ )
423
447
  )
424
- )
425
448
 
426
- return [
427
- name,
428
- uplcToSchema(type, pair[1], config, defs)
429
- ]
430
- })
449
+ if (i == -1) {
450
+ throw new Error(
451
+ `field ${name} not found in MapData`
452
+ )
453
+ }
454
+
455
+ const pair = entries[i]
456
+
457
+ return /** @type {const} */ ([
458
+ i,
459
+ name,
460
+ uplcToSchema(type, pair[1], config, defs)
461
+ ])
462
+ })
463
+ .sort((a, b) => a[0] - b[0])
464
+ .map(([_, name, x]) => [name, x])
431
465
  )
432
466
  }
433
467
  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,UAAU,CAEd;IAEN;;;OAGG;IACH,iBAFU,UAAU,CAEd;IAWN;;;OAGG;IACH,mBAHW,QAAQ,GACN,OAAO,CAInB;IAED;;;OAGG;IACH,cAHW,WAAW,GACT,QAAQ,CAIpB;CACJ;uBAhEY,OAAO,mBAAmB,EAAE,QAAQ;yBACpC,OAAO,qBAAqB,EAAE,UAAU;yBAIxC;IACZ,SAAa,EAAE,OAAO,CA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