ox 0.9.13 → 0.9.15

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/Cbor/package.json +6 -0
  3. package/_cjs/core/AbiConstructor.js +1 -1
  4. package/_cjs/core/AbiConstructor.js.map +1 -1
  5. package/_cjs/core/AbiError.js +1 -1
  6. package/_cjs/core/AbiError.js.map +1 -1
  7. package/_cjs/core/AbiFunction.js +1 -1
  8. package/_cjs/core/AbiFunction.js.map +1 -1
  9. package/_cjs/core/Cbor.js +690 -0
  10. package/_cjs/core/Cbor.js.map +1 -0
  11. package/_cjs/core/Errors.js +38 -4
  12. package/_cjs/core/Errors.js.map +1 -1
  13. package/_cjs/index.js +3 -2
  14. package/_cjs/index.js.map +1 -1
  15. package/_cjs/version.js +1 -1
  16. package/_esm/core/AbiConstructor.js +1 -1
  17. package/_esm/core/AbiConstructor.js.map +1 -1
  18. package/_esm/core/AbiError.js +1 -1
  19. package/_esm/core/AbiError.js.map +1 -1
  20. package/_esm/core/AbiFunction.js +1 -1
  21. package/_esm/core/AbiFunction.js.map +1 -1
  22. package/_esm/core/Cbor.js +771 -0
  23. package/_esm/core/Cbor.js.map +1 -0
  24. package/_esm/core/Errors.js +38 -4
  25. package/_esm/core/Errors.js.map +1 -1
  26. package/_esm/index.js +32 -0
  27. package/_esm/index.js.map +1 -1
  28. package/_esm/version.js +1 -1
  29. package/_types/core/Cbor.d.ts +187 -0
  30. package/_types/core/Cbor.d.ts.map +1 -0
  31. package/_types/core/Errors.d.ts +22 -0
  32. package/_types/core/Errors.d.ts.map +1 -1
  33. package/_types/core/WebAuthnP256.d.ts +1 -0
  34. package/_types/core/WebAuthnP256.d.ts.map +1 -1
  35. package/_types/index.d.ts +32 -0
  36. package/_types/index.d.ts.map +1 -1
  37. package/_types/version.d.ts +1 -1
  38. package/core/AbiConstructor.ts +1 -1
  39. package/core/AbiError.ts +1 -1
  40. package/core/AbiFunction.ts +1 -1
  41. package/core/Cbor.ts +912 -0
  42. package/core/Errors.ts +43 -4
  43. package/core/WebAuthnP256.ts +28 -0
  44. package/index.ts +33 -0
  45. package/package.json +6 -1
  46. package/version.ts +1 -1
@@ -0,0 +1,690 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ByteStringTooLargeError = exports.ObjectTooLargeError = exports.ArrayTooLargeError = exports.StringTooLargeError = exports.NumberTooLargeError = exports.UnexpectedTokenError = exports.UnsupportedBigIntError = exports.InvalidSimpleValueError = exports.InvalidIndefiniteLengthChunkError = exports.UnsupportedTagError = exports.Unsupported64BitIntegerError = exports.InvalidAdditionalInfoError = exports.InvalidMajorTypeError = void 0;
4
+ exports.encode = encode;
5
+ exports.decode = decode;
6
+ const Bytes = require("./Bytes.js");
7
+ const Errors = require("./Errors.js");
8
+ const Hex = require("./Hex.js");
9
+ const Cursor = require("./internal/cursor.js");
10
+ function encode(data, options = {}) {
11
+ const { as = 'Hex' } = options;
12
+ const encodable = getEncodable(data);
13
+ const cursor = Cursor.create(new Uint8Array(encodable.length));
14
+ encodable.encode(cursor);
15
+ if (as === 'Hex')
16
+ return Hex.fromBytes(cursor.bytes);
17
+ return cursor.bytes;
18
+ }
19
+ function decode(data) {
20
+ const bytes = (() => {
21
+ if (typeof data === 'string') {
22
+ if (data.length > 3 && data.length % 2 !== 0)
23
+ throw new Hex.InvalidLengthError(data);
24
+ return Bytes.fromHex(data);
25
+ }
26
+ return data;
27
+ })();
28
+ const cursor = Cursor.create(bytes);
29
+ return decodeCursor(cursor);
30
+ }
31
+ class InvalidMajorTypeError extends Errors.BaseError {
32
+ constructor({ majorType }) {
33
+ super(`Invalid CBOR major type: ${majorType}`);
34
+ Object.defineProperty(this, "name", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: 'Cbor.InvalidMajorTypeError'
39
+ });
40
+ }
41
+ }
42
+ exports.InvalidMajorTypeError = InvalidMajorTypeError;
43
+ class InvalidAdditionalInfoError extends Errors.BaseError {
44
+ constructor({ additionalInfo }) {
45
+ super(`Invalid CBOR additional info: ${additionalInfo}`);
46
+ Object.defineProperty(this, "name", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: 'Cbor.InvalidAdditionalInfoError'
51
+ });
52
+ }
53
+ }
54
+ exports.InvalidAdditionalInfoError = InvalidAdditionalInfoError;
55
+ class Unsupported64BitIntegerError extends Errors.BaseError {
56
+ constructor() {
57
+ super('64-bit integers are not supported in CBOR decoding.');
58
+ Object.defineProperty(this, "name", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: 'Cbor.Unsupported64BitIntegerError'
63
+ });
64
+ }
65
+ }
66
+ exports.Unsupported64BitIntegerError = Unsupported64BitIntegerError;
67
+ class UnsupportedTagError extends Errors.BaseError {
68
+ constructor({ tag }) {
69
+ super(`CBOR tagged data (tag ${tag}) is not yet supported.`);
70
+ Object.defineProperty(this, "name", {
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true,
74
+ value: 'Cbor.UnsupportedTagError'
75
+ });
76
+ }
77
+ }
78
+ exports.UnsupportedTagError = UnsupportedTagError;
79
+ class InvalidIndefiniteLengthChunkError extends Errors.BaseError {
80
+ constructor({ type }) {
81
+ super(`Invalid chunk type in indefinite-length ${type}`);
82
+ Object.defineProperty(this, "name", {
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true,
86
+ value: 'Cbor.InvalidIndefiniteLengthChunkError'
87
+ });
88
+ }
89
+ }
90
+ exports.InvalidIndefiniteLengthChunkError = InvalidIndefiniteLengthChunkError;
91
+ class InvalidSimpleValueError extends Errors.BaseError {
92
+ constructor({ value }) {
93
+ super(`Invalid CBOR simple value: ${value}`);
94
+ Object.defineProperty(this, "name", {
95
+ enumerable: true,
96
+ configurable: true,
97
+ writable: true,
98
+ value: 'Cbor.InvalidSimpleValueError'
99
+ });
100
+ }
101
+ }
102
+ exports.InvalidSimpleValueError = InvalidSimpleValueError;
103
+ class UnsupportedBigIntError extends Errors.BaseError {
104
+ constructor() {
105
+ super('BigInt values are not supported in CBOR encoding.');
106
+ Object.defineProperty(this, "name", {
107
+ enumerable: true,
108
+ configurable: true,
109
+ writable: true,
110
+ value: 'Cbor.UnsupportedBigIntError'
111
+ });
112
+ }
113
+ }
114
+ exports.UnsupportedBigIntError = UnsupportedBigIntError;
115
+ class UnexpectedTokenError extends Errors.BaseError {
116
+ constructor({ token }) {
117
+ super(`Unexpected token: ${token}`);
118
+ Object.defineProperty(this, "name", {
119
+ enumerable: true,
120
+ configurable: true,
121
+ writable: true,
122
+ value: 'Cbor.UnexpectedTokenError'
123
+ });
124
+ }
125
+ }
126
+ exports.UnexpectedTokenError = UnexpectedTokenError;
127
+ class NumberTooLargeError extends Errors.BaseError {
128
+ constructor({ number }) {
129
+ super(`Number exceeds maximum safe integer (${Number.MAX_SAFE_INTEGER}): ${number}`);
130
+ Object.defineProperty(this, "name", {
131
+ enumerable: true,
132
+ configurable: true,
133
+ writable: true,
134
+ value: 'Cbor.NumberTooLargeError'
135
+ });
136
+ }
137
+ }
138
+ exports.NumberTooLargeError = NumberTooLargeError;
139
+ class StringTooLargeError extends Errors.BaseError {
140
+ constructor({ size }) {
141
+ super(`String length exceeds maximum (4294967295): ${size}`);
142
+ Object.defineProperty(this, "name", {
143
+ enumerable: true,
144
+ configurable: true,
145
+ writable: true,
146
+ value: 'Cbor.StringTooLargeError'
147
+ });
148
+ }
149
+ }
150
+ exports.StringTooLargeError = StringTooLargeError;
151
+ class ArrayTooLargeError extends Errors.BaseError {
152
+ constructor({ size }) {
153
+ super(`Array length exceeds maximum (4294967295): ${size}`);
154
+ Object.defineProperty(this, "name", {
155
+ enumerable: true,
156
+ configurable: true,
157
+ writable: true,
158
+ value: 'Cbor.ArrayTooLargeError'
159
+ });
160
+ }
161
+ }
162
+ exports.ArrayTooLargeError = ArrayTooLargeError;
163
+ class ObjectTooLargeError extends Errors.BaseError {
164
+ constructor({ size }) {
165
+ super(`Object size exceeds maximum (4294967295): ${size}`);
166
+ Object.defineProperty(this, "name", {
167
+ enumerable: true,
168
+ configurable: true,
169
+ writable: true,
170
+ value: 'Cbor.ObjectTooLargeError'
171
+ });
172
+ }
173
+ }
174
+ exports.ObjectTooLargeError = ObjectTooLargeError;
175
+ class ByteStringTooLargeError extends Errors.BaseError {
176
+ constructor({ size }) {
177
+ super(`Byte string length exceeds maximum (4294967295): ${size}`);
178
+ Object.defineProperty(this, "name", {
179
+ enumerable: true,
180
+ configurable: true,
181
+ writable: true,
182
+ value: 'Cbor.ByteStringTooLargeError'
183
+ });
184
+ }
185
+ }
186
+ exports.ByteStringTooLargeError = ByteStringTooLargeError;
187
+ function getEncodable(value) {
188
+ if (typeof value === 'undefined')
189
+ return { length: 1, encode: (cursor) => cursor.pushUint8(0xf7) };
190
+ if (value === null)
191
+ return { length: 1, encode: (cursor) => cursor.pushUint8(0xf6) };
192
+ if (typeof value === 'boolean')
193
+ return {
194
+ length: 1,
195
+ encode: (cursor) => cursor.pushUint8(value ? 0xf5 : 0xf4),
196
+ };
197
+ if (typeof value === 'number')
198
+ return getEncodable.number(value);
199
+ if (typeof value === 'bigint')
200
+ throw new UnsupportedBigIntError();
201
+ if (typeof value === 'string')
202
+ return getEncodable.string(value);
203
+ if (Array.isArray(value))
204
+ return getEncodable.array(value);
205
+ if (value instanceof Uint8Array)
206
+ return getEncodable.byteString(value);
207
+ if (value instanceof ArrayBuffer)
208
+ return getEncodable.byteString(new Uint8Array(value));
209
+ if (ArrayBuffer.isView(value))
210
+ return getEncodable.byteString(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
211
+ if (typeof value === 'object')
212
+ return getEncodable.object(value);
213
+ throw new UnexpectedTokenError({ token: String(value) });
214
+ }
215
+ (function (getEncodable) {
216
+ function number(value) {
217
+ if (!Number.isSafeInteger(value)) {
218
+ const float32 = Math.fround(value);
219
+ if (Number.isNaN(value) || value === float32)
220
+ return {
221
+ length: 5,
222
+ encode(cursor) {
223
+ cursor.pushUint8(0xfa);
224
+ cursor.dataView.setFloat32(cursor.position, value, false);
225
+ cursor.position += 4;
226
+ },
227
+ };
228
+ return {
229
+ length: 9,
230
+ encode(cursor) {
231
+ cursor.pushUint8(0xfb);
232
+ cursor.dataView.setFloat64(cursor.position, value, false);
233
+ cursor.position += 8;
234
+ },
235
+ };
236
+ }
237
+ if (value >= 0) {
238
+ if (value <= 0x17)
239
+ return { length: 1, encode: (cursor) => cursor.pushUint8(value) };
240
+ if (value <= 0xff)
241
+ return {
242
+ length: 2,
243
+ encode: (cursor) => {
244
+ cursor.pushUint8(0x18);
245
+ cursor.pushUint8(value);
246
+ },
247
+ };
248
+ if (value <= 0xffff)
249
+ return {
250
+ length: 3,
251
+ encode: (cursor) => {
252
+ cursor.pushUint8(0x19);
253
+ cursor.pushUint16(value);
254
+ },
255
+ };
256
+ if (value <= 0xffffffff)
257
+ return {
258
+ length: 5,
259
+ encode: (cursor) => {
260
+ cursor.pushUint8(0x1a);
261
+ cursor.pushUint32(value);
262
+ },
263
+ };
264
+ throw new NumberTooLargeError({ number: value.toString(10) });
265
+ }
266
+ const positiveNumber = -1 - value;
267
+ if (value >= -24)
268
+ return {
269
+ length: 1,
270
+ encode: (cursor) => cursor.pushUint8(0x20 + positiveNumber),
271
+ };
272
+ if (positiveNumber <= 0xff)
273
+ return {
274
+ length: 2,
275
+ encode: (cursor) => {
276
+ cursor.pushUint8(0x38);
277
+ cursor.pushUint8(positiveNumber);
278
+ },
279
+ };
280
+ if (positiveNumber <= 0xffff)
281
+ return {
282
+ length: 3,
283
+ encode: (cursor) => {
284
+ cursor.pushUint8(0x39);
285
+ cursor.pushUint16(positiveNumber);
286
+ },
287
+ };
288
+ if (positiveNumber <= 0xffffffff)
289
+ return {
290
+ length: 5,
291
+ encode: (cursor) => {
292
+ cursor.pushUint8(0x3a);
293
+ cursor.pushUint32(positiveNumber);
294
+ },
295
+ };
296
+ throw new NumberTooLargeError({ number: value.toString(10) });
297
+ }
298
+ getEncodable.number = number;
299
+ function string(value) {
300
+ const encoded = Bytes.fromString(value);
301
+ const size = encoded.length;
302
+ if (size <= 0x17)
303
+ return {
304
+ length: 1 + size,
305
+ encode(cursor) {
306
+ cursor.pushUint8(0x60 + size);
307
+ if (size > 0)
308
+ cursor.pushBytes(encoded);
309
+ },
310
+ };
311
+ if (size <= 0xff)
312
+ return {
313
+ length: 2 + size,
314
+ encode(cursor) {
315
+ cursor.pushUint8(0x78);
316
+ cursor.pushUint8(size);
317
+ cursor.pushBytes(encoded);
318
+ },
319
+ };
320
+ if (size <= 0xffff)
321
+ return {
322
+ length: 3 + size,
323
+ encode(cursor) {
324
+ cursor.pushUint8(0x79);
325
+ cursor.pushUint16(size);
326
+ cursor.pushBytes(encoded);
327
+ },
328
+ };
329
+ if (size <= 0xffffffff)
330
+ return {
331
+ length: 5 + size,
332
+ encode(cursor) {
333
+ cursor.pushUint8(0x7a);
334
+ cursor.pushUint32(size);
335
+ cursor.pushBytes(encoded);
336
+ },
337
+ };
338
+ throw new StringTooLargeError({ size });
339
+ }
340
+ getEncodable.string = string;
341
+ function array(value) {
342
+ const items = value.map((item) => getEncodable(item));
343
+ const bodyLength = items.reduce((acc, item) => acc + item.length, 0);
344
+ const size = value.length;
345
+ if (size <= 0x17)
346
+ return {
347
+ length: 1 + bodyLength,
348
+ encode(cursor) {
349
+ cursor.pushUint8(0x80 + size);
350
+ for (const item of items)
351
+ item.encode(cursor);
352
+ },
353
+ };
354
+ if (size <= 0xff)
355
+ return {
356
+ length: 2 + bodyLength,
357
+ encode(cursor) {
358
+ cursor.pushUint8(0x98);
359
+ cursor.pushUint8(size);
360
+ for (const item of items)
361
+ item.encode(cursor);
362
+ },
363
+ };
364
+ if (size <= 0xffff)
365
+ return {
366
+ length: 3 + bodyLength,
367
+ encode(cursor) {
368
+ cursor.pushUint8(0x99);
369
+ cursor.pushUint16(size);
370
+ for (const item of items)
371
+ item.encode(cursor);
372
+ },
373
+ };
374
+ if (size <= 0xffffffff)
375
+ return {
376
+ length: 5 + bodyLength,
377
+ encode(cursor) {
378
+ cursor.pushUint8(0x9a);
379
+ cursor.pushUint32(size);
380
+ for (const item of items)
381
+ item.encode(cursor);
382
+ },
383
+ };
384
+ throw new ArrayTooLargeError({ size });
385
+ }
386
+ getEncodable.array = array;
387
+ function byteString(value) {
388
+ const size = value.byteLength;
389
+ if (size <= 0x17)
390
+ return {
391
+ length: 1 + size,
392
+ encode(cursor) {
393
+ cursor.pushUint8(0x40 + size);
394
+ cursor.pushBytes(value);
395
+ },
396
+ };
397
+ if (size <= 0xff)
398
+ return {
399
+ length: 2 + size,
400
+ encode(cursor) {
401
+ cursor.pushUint8(0x58);
402
+ cursor.pushUint8(size);
403
+ cursor.pushBytes(value);
404
+ },
405
+ };
406
+ if (size <= 0xffff)
407
+ return {
408
+ length: 3 + size,
409
+ encode(cursor) {
410
+ cursor.pushUint8(0x59);
411
+ cursor.pushUint16(size);
412
+ cursor.pushBytes(value);
413
+ },
414
+ };
415
+ if (size <= 0xffffffff)
416
+ return {
417
+ length: 5 + size,
418
+ encode(cursor) {
419
+ cursor.pushUint8(0x5a);
420
+ cursor.pushUint32(size);
421
+ cursor.pushBytes(value);
422
+ },
423
+ };
424
+ throw new ByteStringTooLargeError({ size });
425
+ }
426
+ getEncodable.byteString = byteString;
427
+ function object(value) {
428
+ const keys = Object.keys(value);
429
+ const entries = keys.map((key) => ({
430
+ key: getEncodable(key),
431
+ value: getEncodable(value[key]),
432
+ }));
433
+ const bodyLength = entries.reduce((acc, entry) => acc + entry.key.length + entry.value.length, 0);
434
+ const size = keys.length;
435
+ if (size <= 0x17)
436
+ return {
437
+ length: 1 + bodyLength,
438
+ encode(cursor) {
439
+ cursor.pushUint8(0xa0 + size);
440
+ for (const entry of entries) {
441
+ entry.key.encode(cursor);
442
+ entry.value.encode(cursor);
443
+ }
444
+ },
445
+ };
446
+ if (size <= 0xff)
447
+ return {
448
+ length: 2 + bodyLength,
449
+ encode(cursor) {
450
+ cursor.pushUint8(0xb8);
451
+ cursor.pushUint8(size);
452
+ for (const entry of entries) {
453
+ entry.key.encode(cursor);
454
+ entry.value.encode(cursor);
455
+ }
456
+ },
457
+ };
458
+ if (size <= 0xffff)
459
+ return {
460
+ length: 3 + bodyLength,
461
+ encode(cursor) {
462
+ cursor.pushUint8(0xb9);
463
+ cursor.pushUint16(size);
464
+ for (const entry of entries) {
465
+ entry.key.encode(cursor);
466
+ entry.value.encode(cursor);
467
+ }
468
+ },
469
+ };
470
+ if (size <= 0xffffffff)
471
+ return {
472
+ length: 5 + bodyLength,
473
+ encode(cursor) {
474
+ cursor.pushUint8(0xba);
475
+ cursor.pushUint32(size);
476
+ for (const entry of entries) {
477
+ entry.key.encode(cursor);
478
+ entry.value.encode(cursor);
479
+ }
480
+ },
481
+ };
482
+ throw new ObjectTooLargeError({ size });
483
+ }
484
+ getEncodable.object = object;
485
+ })(getEncodable || (getEncodable = {}));
486
+ function decodeCursor(cursor) {
487
+ const initialByte = cursor.readUint8();
488
+ const majorType = initialByte >> 5;
489
+ const additionalInfo = initialByte & 0b00011111;
490
+ switch (majorType) {
491
+ case 0:
492
+ return decodeCursor.readUnsignedInteger(cursor, additionalInfo);
493
+ case 1:
494
+ return decodeCursor.readNegativeInteger(cursor, additionalInfo);
495
+ case 2:
496
+ return decodeCursor.readByteString(cursor, additionalInfo);
497
+ case 3:
498
+ return decodeCursor.readTextString(cursor, additionalInfo);
499
+ case 4:
500
+ return decodeCursor.readArray(cursor, additionalInfo);
501
+ case 5:
502
+ return decodeCursor.readMap(cursor, additionalInfo);
503
+ case 6:
504
+ throw new UnsupportedTagError({ tag: additionalInfo });
505
+ case 7:
506
+ return decodeCursor.readSimpleOrFloat(cursor, additionalInfo);
507
+ default:
508
+ throw new InvalidMajorTypeError({ majorType });
509
+ }
510
+ }
511
+ (function (decodeCursor) {
512
+ function readLength(cursor, additionalInfo) {
513
+ if (additionalInfo < 24)
514
+ return additionalInfo;
515
+ if (additionalInfo === 24)
516
+ return cursor.readUint8();
517
+ if (additionalInfo === 25)
518
+ return cursor.readUint16();
519
+ if (additionalInfo === 26)
520
+ return cursor.readUint32();
521
+ if (additionalInfo === 27)
522
+ throw new Unsupported64BitIntegerError();
523
+ throw new InvalidAdditionalInfoError({ additionalInfo });
524
+ }
525
+ function readUnsignedInteger(cursor, additionalInfo) {
526
+ return readLength(cursor, additionalInfo);
527
+ }
528
+ decodeCursor.readUnsignedInteger = readUnsignedInteger;
529
+ function readNegativeInteger(cursor, additionalInfo) {
530
+ const value = readLength(cursor, additionalInfo);
531
+ return -1 - value;
532
+ }
533
+ decodeCursor.readNegativeInteger = readNegativeInteger;
534
+ function readByteString(cursor, additionalInfo) {
535
+ if (additionalInfo === 31) {
536
+ const chunks = [];
537
+ let totalLength = 0;
538
+ while (true) {
539
+ const byte = cursor.inspectUint8();
540
+ if (byte === 0xff) {
541
+ cursor.readUint8();
542
+ break;
543
+ }
544
+ const chunk = decodeCursor(cursor);
545
+ if (!(chunk instanceof Uint8Array))
546
+ throw new InvalidIndefiniteLengthChunkError({ type: 'byte string' });
547
+ chunks.push(chunk);
548
+ totalLength += chunk.length;
549
+ }
550
+ const result = new Uint8Array(totalLength);
551
+ let offset = 0;
552
+ for (const chunk of chunks) {
553
+ result.set(chunk, offset);
554
+ offset += chunk.length;
555
+ }
556
+ return result;
557
+ }
558
+ const length = readLength(cursor, additionalInfo);
559
+ return cursor.readBytes(length);
560
+ }
561
+ decodeCursor.readByteString = readByteString;
562
+ function readTextString(cursor, additionalInfo) {
563
+ if (additionalInfo === 31) {
564
+ const chunks = [];
565
+ while (true) {
566
+ const byte = cursor.inspectUint8();
567
+ if (byte === 0xff) {
568
+ cursor.readUint8();
569
+ break;
570
+ }
571
+ const chunk = decodeCursor(cursor);
572
+ if (typeof chunk !== 'string')
573
+ throw new InvalidIndefiniteLengthChunkError({ type: 'text string' });
574
+ chunks.push(chunk);
575
+ }
576
+ return chunks.join('');
577
+ }
578
+ const length = readLength(cursor, additionalInfo);
579
+ const bytes = cursor.readBytes(length);
580
+ return Bytes.toString(bytes);
581
+ }
582
+ decodeCursor.readTextString = readTextString;
583
+ function readArray(cursor, additionalInfo) {
584
+ if (additionalInfo === 31) {
585
+ const result = [];
586
+ while (true) {
587
+ const byte = cursor.inspectUint8();
588
+ if (byte === 0xff) {
589
+ cursor.readUint8();
590
+ break;
591
+ }
592
+ result.push(decodeCursor(cursor));
593
+ }
594
+ return result;
595
+ }
596
+ const length = readLength(cursor, additionalInfo);
597
+ const result = [];
598
+ for (let i = 0; i < length; i++) {
599
+ result.push(decodeCursor(cursor));
600
+ }
601
+ return result;
602
+ }
603
+ decodeCursor.readArray = readArray;
604
+ function readMap(cursor, additionalInfo) {
605
+ if (additionalInfo === 31) {
606
+ const result = {};
607
+ while (true) {
608
+ const byte = cursor.inspectUint8();
609
+ if (byte === 0xff) {
610
+ cursor.readUint8();
611
+ break;
612
+ }
613
+ const key = decodeCursor(cursor);
614
+ const keyStr = typeof key === 'string'
615
+ ? key
616
+ : typeof key === 'number'
617
+ ? String(key)
618
+ : String(key);
619
+ const value = decodeCursor(cursor);
620
+ result[keyStr] = value;
621
+ }
622
+ return result;
623
+ }
624
+ const length = readLength(cursor, additionalInfo);
625
+ const result = {};
626
+ for (let i = 0; i < length; i++) {
627
+ const key = decodeCursor(cursor);
628
+ const keyStr = typeof key === 'string'
629
+ ? key
630
+ : typeof key === 'number'
631
+ ? String(key)
632
+ : String(key);
633
+ const value = decodeCursor(cursor);
634
+ result[keyStr] = value;
635
+ }
636
+ return result;
637
+ }
638
+ decodeCursor.readMap = readMap;
639
+ function readSimpleOrFloat(cursor, additionalInfo) {
640
+ if (additionalInfo === 20)
641
+ return false;
642
+ if (additionalInfo === 21)
643
+ return true;
644
+ if (additionalInfo === 22)
645
+ return null;
646
+ if (additionalInfo === 23)
647
+ return undefined;
648
+ if (additionalInfo === 25) {
649
+ const bits = cursor.readUint16();
650
+ return getFloat16(bits);
651
+ }
652
+ if (additionalInfo === 26) {
653
+ const value = cursor.dataView.getFloat32(cursor.position, false);
654
+ cursor.position += 4;
655
+ return value;
656
+ }
657
+ if (additionalInfo === 27) {
658
+ const value = cursor.dataView.getFloat64(cursor.position, false);
659
+ cursor.position += 8;
660
+ return value;
661
+ }
662
+ if (additionalInfo === 24) {
663
+ const simpleValue = cursor.readUint8();
664
+ if (simpleValue < 32)
665
+ throw new InvalidSimpleValueError({ value: simpleValue });
666
+ return undefined;
667
+ }
668
+ throw new InvalidAdditionalInfoError({ additionalInfo });
669
+ }
670
+ decodeCursor.readSimpleOrFloat = readSimpleOrFloat;
671
+ function getFloat16(bits) {
672
+ const sign = (bits >> 15) & 0x1;
673
+ const exponent = (bits >> 10) & 0x1f;
674
+ const fraction = bits & 0x3ff;
675
+ if (exponent === 0) {
676
+ if (fraction === 0)
677
+ return sign ? -0 : 0;
678
+ const value = 2 ** -14 * (fraction / 1024);
679
+ return sign ? -value : value;
680
+ }
681
+ if (exponent === 0x1f) {
682
+ if (fraction === 0)
683
+ return sign ? -Infinity : Infinity;
684
+ return NaN;
685
+ }
686
+ const value = 2 ** (exponent - 15) * (1 + fraction / 1024);
687
+ return sign ? -value : value;
688
+ }
689
+ })(decodeCursor || (decodeCursor = {}));
690
+ //# sourceMappingURL=Cbor.js.map