@koralabs/kora-labs-common 6.13.4 → 6.14.0

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.
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.decodeCborToJson = exports.encodeJsonToDatum = exports.DupeKey = exports.DefaultTextFormat = exports.subHandleSettingsDatumSchema = exports.socialsSchema = exports.portalSchema = exports.marketplaceDatumSchema = exports.handleDatumSchema = exports.designerSchema = void 0;
36
+ exports.decodeCborToJson = exports.constrToTagged = exports.encodeJsonToDatum = exports.DupeKey = exports.DefaultTextFormat = exports.subHandleSettingsDatumSchema = exports.socialsSchema = exports.portalSchema = exports.marketplaceDatumSchema = exports.handleDatumSchema = exports.designerSchema = void 0;
37
37
  const boolean_1 = require("boolean");
38
38
  const cbor = __importStar(require("cbor"));
39
39
  var designer_1 = require("./schema/designer");
@@ -96,7 +96,6 @@ class JsonToDatumObject {
96
96
  else if (typeof this.json === 'object') {
97
97
  if (this.json !== null) {
98
98
  const fieldsMap = new Map();
99
- let tag = null;
100
99
  const keys = this.json instanceof Map ? this.json.keys() : Object.keys(this.json);
101
100
  for (const key of keys) {
102
101
  if (key instanceof DupeKey) {
@@ -104,11 +103,8 @@ class JsonToDatumObject {
104
103
  }
105
104
  else {
106
105
  const split_key = parseInt((_a = key.split('_').at(1)) !== null && _a !== void 0 ? _a : '');
107
- if (key.startsWith('constructor_') &&
108
- !isNaN(split_key) &&
109
- [0, 1, 2, 3].includes(split_key)) {
110
- tag = 121 + split_key;
111
- return encoder.pushAny(new cbor.Tagged(tag, this.json[key]));
106
+ if (key.startsWith('constructor_') && Number.isInteger(split_key) && split_key >= 0) {
107
+ return encoder.pushAny((0, exports.constrToTagged)(split_key, this.json[key]));
112
108
  }
113
109
  const bufferedKey = this.getFormattedKey(key);
114
110
  fieldsMap.set(bufferedKey, this.json[key]);
@@ -153,17 +149,13 @@ class JsonToDatumObject {
153
149
  indefiniteArrays: (_b = options === null || options === void 0 ? void 0 : options.indefiniteArrays) !== null && _b !== void 0 ? _b : true,
154
150
  defaultToText: options === null || options === void 0 ? void 0 : options.defaultToText
155
151
  };
156
- if (Array.isArray(this.json)) {
157
- for (let i = 0; i < this.json.length; i++) {
158
- this.json[i] = new JsonToDatumObject(this.json[i], this.options);
159
- }
152
+ // Wrap children into NEW containers: the caller's JSON must not be modified (re-encoding the
153
+ // same object used to produce garbage, since its children had been replaced by wrappers).
154
+ if (Array.isArray(json)) {
155
+ this.json = json.map((item) => new JsonToDatumObject(item, this.options));
160
156
  }
161
- else if (typeof this.json === 'object') {
162
- if (this.json !== null) {
163
- Object.keys(this.json).map((key) => {
164
- this.json[key] = new JsonToDatumObject(this.json[key], this.options);
165
- });
166
- }
157
+ else if (typeof json === 'object' && json !== null && !(json instanceof Map)) {
158
+ this.json = Object.fromEntries(Object.keys(json).map((key) => [key, new JsonToDatumObject(json[key], this.options)]));
167
159
  }
168
160
  }
169
161
  }
@@ -393,15 +385,26 @@ const decodeObject = ({ val, constr = null, schema = {}, defaultKeyType = Defaul
393
385
  return val;
394
386
  }
395
387
  };
388
+ // Plutus Constr tag scheme (CIP-0005 / plutus-core): 0-6 -> 121-127, 7-127 -> 1280-1400,
389
+ // anything else -> tag 102 wrapping [index, fields]. Every index must round-trip; an
390
+ // unrecognized `constructor_N` key would otherwise be encoded as a text-keyed map.
391
+ const constrToTagged = (index, fields) => {
392
+ if (index <= 6)
393
+ return new cbor.Tagged(121 + index, fields);
394
+ if (index <= 127)
395
+ return new cbor.Tagged(1280 + index - 7, fields);
396
+ return new cbor.Tagged(102, [index, fields]);
397
+ };
398
+ exports.constrToTagged = constrToTagged;
399
+ const CONSTR_DECODE_TAGS = {
400
+ 102: ([index, fields]) => ({ [`constructor_${index}`]: fields })
401
+ };
402
+ for (let index = 0; index <= 127; index++) {
403
+ CONSTR_DECODE_TAGS[index <= 6 ? 121 + index : 1280 + index - 7] = (val) => ({ [`constructor_${index}`]: val });
404
+ }
396
405
  const decodeCborToJson = ({ cborString, schema, defaultKeyType, forJson }) => {
397
406
  const decoded = cbor.decodeAllSync(Buffer.from(cborString, 'hex'), {
398
- tags: {
399
- 121: (val) => ({ [`constructor_0`]: val }),
400
- 122: (val) => ({ [`constructor_1`]: val }),
401
- 123: (val) => ({ [`constructor_2`]: val }),
402
- 124: (val) => ({ [`constructor_3`]: val }),
403
- 125: (val) => ({ [`constructor_4`]: val })
404
- }
407
+ tags: CONSTR_DECODE_TAGS
405
408
  });
406
409
  let [data] = decoded;
407
410
  data = decodeObject({ val: data, schema, defaultKeyType, forJson });