@morpho-org/midnight-sdk 2.7.0-next.1 → 2.8.0-next.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.
Files changed (51) hide show
  1. package/lib/cjs/api/MidnightApi.js +63 -52
  2. package/lib/cjs/api/helpers.js +30 -17
  3. package/lib/cjs/fetch/Position.js +2 -1
  4. package/lib/cjs/fetch/Ratifier.js +4 -5
  5. package/lib/cjs/market/Market.js +2 -1
  6. package/lib/cjs/market/MarketUtils.js +16 -14
  7. package/lib/cjs/math/TakeAmountsLib.js +8 -4
  8. package/lib/cjs/math/TickLib.js +12 -6
  9. package/lib/cjs/offers/OfferChainUtils.js +35 -26
  10. package/lib/cjs/offers/OfferUtils.js +17 -10
  11. package/lib/cjs/position/Position.js +2 -1
  12. package/lib/cjs/signatures/EcrecoverRatifier.js +22 -17
  13. package/lib/cjs/signatures/Group.js +4 -2
  14. package/lib/cjs/signatures/Payload.js +18 -9
  15. package/lib/cjs/signatures/PriceRatifierV1.js +5 -4
  16. package/lib/cjs/signatures/RateRatifierV1.js +19 -11
  17. package/lib/cjs/signatures/Ratifier.js +4 -4
  18. package/lib/cjs/signatures/SetterRatifier.js +2 -1
  19. package/lib/cjs/signatures/Tree.js +8 -4
  20. package/lib/cjs/signatures/TreeUtils.js +16 -13
  21. package/lib/cjs/signatures/ratifierV1Internal.js +34 -24
  22. package/lib/cjs/signatures/treeMathInternal.d.ts +2 -0
  23. package/lib/cjs/signatures/treeMathInternal.js +10 -0
  24. package/lib/cjs/version.d.ts +1 -1
  25. package/lib/cjs/version.js +1 -1
  26. package/lib/esm/api/MidnightApi.js +63 -52
  27. package/lib/esm/api/helpers.js +30 -17
  28. package/lib/esm/fetch/Position.js +2 -1
  29. package/lib/esm/fetch/Ratifier.js +4 -5
  30. package/lib/esm/market/Market.js +2 -1
  31. package/lib/esm/market/MarketUtils.js +16 -14
  32. package/lib/esm/math/TakeAmountsLib.js +8 -4
  33. package/lib/esm/math/TickLib.js +12 -6
  34. package/lib/esm/offers/OfferChainUtils.js +35 -26
  35. package/lib/esm/offers/OfferUtils.js +17 -10
  36. package/lib/esm/position/Position.js +2 -1
  37. package/lib/esm/signatures/EcrecoverRatifier.js +22 -17
  38. package/lib/esm/signatures/Group.js +4 -2
  39. package/lib/esm/signatures/Payload.js +18 -9
  40. package/lib/esm/signatures/PriceRatifierV1.js +5 -4
  41. package/lib/esm/signatures/RateRatifierV1.js +19 -11
  42. package/lib/esm/signatures/Ratifier.js +5 -5
  43. package/lib/esm/signatures/SetterRatifier.js +2 -1
  44. package/lib/esm/signatures/Tree.js +8 -4
  45. package/lib/esm/signatures/TreeUtils.js +17 -14
  46. package/lib/esm/signatures/ratifierV1Internal.js +35 -25
  47. package/lib/esm/signatures/treeMathInternal.d.ts +2 -0
  48. package/lib/esm/signatures/treeMathInternal.js +9 -0
  49. package/lib/esm/version.d.ts +1 -1
  50. package/lib/esm/version.js +1 -1
  51. package/package.json +13 -13
@@ -12,6 +12,7 @@ const eip712_js_1 = require("./eip712.js");
12
12
  const Ratifier_js_1 = require("./Ratifier.js");
13
13
  const standardRatifierInternal_js_1 = require("./standardRatifierInternal.js");
14
14
  const TreeUtils_js_1 = require("./TreeUtils.js");
15
+ const treeMathInternal_js_1 = require("./treeMathInternal.js");
15
16
  const treeTypeHashes = [
16
17
  "0x270da1ebafc0f24637af3612fb8c3a1d828fcb56d3637c24e86dd006b12ca7f9",
17
18
  "0x828b9cdf8326a1cf234328e4d5229546a98fb72ef73624f5b6b31538e555b96c",
@@ -97,8 +98,9 @@ const typedDataTypes = {
97
98
  ],
98
99
  };
99
100
  const buildTreeValue = (offers) => {
100
- if (offers.length === 1)
101
- return offers[0];
101
+ if (offers.length === 1) {
102
+ return (0, treeMathInternal_js_1.requireTreeItem)(offers, 0);
103
+ }
102
104
  const mid = offers.length / 2;
103
105
  return [
104
106
  buildTreeValue(offers.slice(0, mid)),
@@ -106,10 +108,12 @@ const buildTreeValue = (offers) => {
106
108
  ];
107
109
  };
108
110
  function toCanonicalYParity(v) {
109
- if (v === 27)
111
+ if (v === 27) {
110
112
  return 0;
111
- if (v === 28)
113
+ }
114
+ if (v === 28) {
112
115
  return 1;
116
+ }
113
117
  throw new errors_js_1.InvalidEcrecoverSignatureVError(v);
114
118
  }
115
119
  /**
@@ -150,8 +154,9 @@ var EcrecoverRatifierUtils;
150
154
  */
151
155
  function treeTypeHash(height) {
152
156
  const typehash = treeTypeHashes[height];
153
- if (typehash == null)
157
+ if (typehash == null) {
154
158
  throw new errors_js_1.InvalidTreeHeightError(height);
159
+ }
155
160
  return typehash;
156
161
  }
157
162
  EcrecoverRatifierUtils.treeTypeHash = treeTypeHash;
@@ -507,7 +512,7 @@ var EcrecoverRatifierUtils;
507
512
  tree: params.tree,
508
513
  label: "Ecrecover",
509
514
  });
510
- const expectedChainId = index_js_1.MarketParams.from(tree.offers[0].market).chainId;
515
+ const expectedChainId = index_js_1.MarketParams.from((0, treeMathInternal_js_1.requireTreeItem)(tree.offers, 0).market).chainId;
511
516
  for (const offer of tree.offers.slice(1)) {
512
517
  const offerChainId = index_js_1.MarketParams.from(offer.market).chainId;
513
518
  if (offerChainId !== expectedChainId) {
@@ -567,7 +572,7 @@ var EcrecoverRatifierUtils;
567
572
  */
568
573
  function toSignature(signature) {
569
574
  if (typeof signature !== "string") {
570
- const v = signature.v != null ? Number(signature.v) : Number(signature.yParity);
575
+ const v = signature.v == null ? Number(signature.yParity) : Number(signature.v);
571
576
  return {
572
577
  v: v < 27 ? v + 27 : v,
573
578
  r: signature.r,
@@ -800,9 +805,16 @@ var EcrecoverRatifierUtils;
800
805
  label: "Ecrecover",
801
806
  });
802
807
  let signature;
803
- if (params.signature != null) {
808
+ if (params.signature == null) {
809
+ signature = toSignature(await sign({
810
+ tree,
811
+ client: params.client,
812
+ account: params.account,
813
+ }));
814
+ }
815
+ else {
804
816
  signature = toSignature(params.signature);
805
- const expectedChainId = index_js_1.MarketParams.from(tree.offers[0].market).chainId;
817
+ const expectedChainId = index_js_1.MarketParams.from((0, treeMathInternal_js_1.requireTreeItem)(tree.offers, 0).market).chainId;
806
818
  for (const offer of tree.offers.slice(1)) {
807
819
  const offerChainId = index_js_1.MarketParams.from(offer.market).chainId;
808
820
  if (offerChainId !== expectedChainId) {
@@ -831,13 +843,6 @@ var EcrecoverRatifierUtils;
831
843
  throw new errors_js_1.InvalidTypedDataSignatureError(signer);
832
844
  }
833
845
  }
834
- else {
835
- signature = toSignature(await sign({
836
- tree,
837
- client: params.client,
838
- account: params.account,
839
- }));
840
- }
841
846
  const proofs = TreeUtils_js_1.TreeUtils.buildProofs({
842
847
  tree,
843
848
  count: tree.offers.length,
@@ -846,7 +851,7 @@ var EcrecoverRatifierUtils;
846
851
  offer,
847
852
  ratifierData: encodeRatifierData({
848
853
  signature,
849
- ...proofs[leafIndex],
854
+ ...(0, treeMathInternal_js_1.requireTreeItem)(proofs, leafIndex),
850
855
  }),
851
856
  }));
852
857
  }
@@ -144,10 +144,12 @@ class Group {
144
144
  * ```
145
145
  */
146
146
  static from(entry) {
147
- if (entry instanceof Group)
147
+ if (entry instanceof Group) {
148
148
  return entry;
149
- if (GroupUtils_js_1.GroupUtils.isGroupInput(entry))
149
+ }
150
+ if (GroupUtils_js_1.GroupUtils.isGroupInput(entry)) {
150
151
  return new Group(entry.offers);
152
+ }
151
153
  return new Group([entry]);
152
154
  }
153
155
  /**
@@ -342,8 +342,9 @@ var Payload;
342
342
  });
343
343
  }
344
344
  catch (error) {
345
- if (error instanceof errors_js_1.PayloadDecodeError)
345
+ if (error instanceof errors_js_1.PayloadDecodeError) {
346
346
  throw error;
347
+ }
347
348
  if (error instanceof Error) {
348
349
  throw new errors_js_1.PayloadDecodeError(`invalid offer bytes: ${error.message}`, error);
349
350
  }
@@ -362,8 +363,9 @@ var Payload;
362
363
  }
363
364
  }
364
365
  function assertApiValidOfferStruct(offer) {
365
- if ((0, offerStructInternal_js_1.isEmptyOfferStruct)(offer))
366
+ if ((0, offerStructInternal_js_1.isEmptyOfferStruct)(offer)) {
366
367
  return;
368
+ }
367
369
  assertCollateralParams(offer);
368
370
  assertSafeTimestamp("market.maturity", offer.market.maturity);
369
371
  assertMaturityAt15Utc(offer.market.maturity);
@@ -454,20 +456,23 @@ var Payload;
454
456
  }
455
457
  }
456
458
  function resolveMaxItems(maxItems) {
457
- if (maxItems === undefined)
459
+ if (maxItems === undefined) {
458
460
  return undefined;
461
+ }
459
462
  if (!Number.isInteger(maxItems) || maxItems < 1) {
460
463
  throw new errors_js_1.PayloadDecodeError(`maxItems must be a positive integer: got ${maxItems}`);
461
464
  }
462
465
  return maxItems;
463
466
  }
464
467
  function assertNonEmptyItemCount(count) {
465
- if (count === 0)
468
+ if (count === 0) {
466
469
  throw new errors_js_1.PayloadDecodeError("items payload is empty");
470
+ }
467
471
  }
468
472
  function assertCallerItemCountLimit(count, maxItems) {
469
- if (maxItems === undefined)
473
+ if (maxItems === undefined) {
470
474
  return;
475
+ }
471
476
  if (count > maxItems) {
472
477
  throw new errors_js_1.PayloadDecodeError(`items payload exceeds ${maxItems} items`);
473
478
  }
@@ -490,8 +495,9 @@ var Payload;
490
495
  return compressed;
491
496
  }
492
497
  async function decompressItemsPayload(compressed) {
493
- if (compressed.length === 0)
498
+ if (compressed.length === 0) {
494
499
  throw new errors_js_1.PayloadDecodeError("decompression failed");
500
+ }
495
501
  if (compressed.length > Payload.MAX_COMPRESSED_ITEMS_BYTES) {
496
502
  throw new errors_js_1.PayloadDecodeError(`compressed items exceed ${Payload.MAX_COMPRESSED_ITEMS_BYTES} bytes`);
497
503
  }
@@ -503,8 +509,9 @@ var Payload;
503
509
  try {
504
510
  while (true) {
505
511
  const { done, value } = await reader.read();
506
- if (done)
512
+ if (done) {
507
513
  break;
514
+ }
508
515
  totalLength += value.length;
509
516
  if (totalLength > Payload.MAX_DECOMPRESSED_ITEMS_BYTES) {
510
517
  await reader.cancel();
@@ -514,8 +521,9 @@ var Payload;
514
521
  }
515
522
  }
516
523
  catch (error) {
517
- if (error instanceof errors_js_1.PayloadDecodeError)
524
+ if (error instanceof errors_js_1.PayloadDecodeError) {
518
525
  throw error;
526
+ }
519
527
  throw new errors_js_1.PayloadDecodeError("decompression failed", error instanceof Error ? error : undefined);
520
528
  }
521
529
  const result = new Uint8Array(totalLength);
@@ -538,8 +546,9 @@ var Payload;
538
546
  if (decoded.length < ABI_ARRAY_HEAD_BYTES) {
539
547
  throw new errors_js_1.PayloadDecodeError("items payload truncated before ABI array head");
540
548
  }
541
- if (maxItems === undefined)
549
+ if (maxItems === undefined) {
542
550
  return;
551
+ }
543
552
  for (let i = 32; i < ABI_LENGTH_LOW_OFFSET; i++) {
544
553
  if (decoded[i] !== 0) {
545
554
  throw new errors_js_1.PayloadDecodeError(`items payload exceeds ${maxItems} items`);
@@ -12,6 +12,7 @@ const GroupUtils_js_1 = require("./GroupUtils.js");
12
12
  const offerStructInternal_js_1 = require("./offerStructInternal.js");
13
13
  const ratifierV1Internal_js_1 = require("./ratifierV1Internal.js");
14
14
  const TreeUtils_js_1 = require("./TreeUtils.js");
15
+ const treeMathInternal_js_1 = require("./treeMathInternal.js");
15
16
  const priceRatifierV1DataAbi = [
16
17
  { name: "root", type: "bytes32" },
17
18
  { name: "leafIndex", type: "uint256" },
@@ -240,7 +241,7 @@ var PriceRatifierV1;
240
241
  const assignedOffers = (0, ratifierV1Internal_js_1.assignRatifierV1Groups)(normalized, groupId);
241
242
  const offers = [];
242
243
  const structs = normalized.map(({ allowedTaker }, index) => {
243
- const offer = assignedOffers[index];
244
+ const offer = (0, treeMathInternal_js_1.requireTreeItem)(assignedOffers, index);
244
245
  offers.push(offer);
245
246
  return { offer: index_js_2.OfferUtils.toStruct({ offer }), allowedTaker };
246
247
  });
@@ -430,7 +431,7 @@ var PriceRatifierV1;
430
431
  function ratifierData(params) {
431
432
  const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers);
432
433
  const proof = TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
433
- const entry = tree.entries[Number(proof.leafIndex)];
434
+ const entry = (0, treeMathInternal_js_1.requireTreeItem)(tree.entries, Number(proof.leafIndex));
434
435
  return encodeRatifierData({
435
436
  root: proof.root,
436
437
  leafIndex: proof.leafIndex,
@@ -470,8 +471,8 @@ var PriceRatifierV1;
470
471
  count: tree.offers.length,
471
472
  });
472
473
  return tree.offers.map((offer, leafIndex) => {
473
- const entry = tree.entries[leafIndex];
474
- const proof = proofs[leafIndex];
474
+ const entry = (0, treeMathInternal_js_1.requireTreeItem)(tree.entries, leafIndex);
475
+ const proof = (0, treeMathInternal_js_1.requireTreeItem)(proofs, leafIndex);
475
476
  return {
476
477
  offer,
477
478
  ratifierData: encodeRatifierData({
@@ -13,6 +13,7 @@ const GroupUtils_js_1 = require("./GroupUtils.js");
13
13
  const offerStructInternal_js_1 = require("./offerStructInternal.js");
14
14
  const ratifierV1Internal_js_1 = require("./ratifierV1Internal.js");
15
15
  const TreeUtils_js_1 = require("./TreeUtils.js");
16
+ const treeMathInternal_js_1 = require("./treeMathInternal.js");
16
17
  const rateRatifierV1DataAbi = [
17
18
  { name: "root", type: "bytes32" },
18
19
  { name: "leafIndex", type: "uint256" },
@@ -167,8 +168,9 @@ var RateRatifierV1;
167
168
  */
168
169
  function memberHash(leaf) {
169
170
  const rate = BigInt(leaf.rate);
170
- if (rate < 0n)
171
+ if (rate < 0n) {
171
172
  throw new errors_js_1.InvalidRateRatifierV1RateError(rate);
173
+ }
172
174
  return hashLeaf({
173
175
  offer: index_js_3.OfferUtils.toStruct({ offer: leaf.offer, group: viem_1.zeroHash }),
174
176
  rate,
@@ -267,18 +269,20 @@ var RateRatifierV1;
267
269
  function buildDescriptor(leaves) {
268
270
  const normalized = leaves.map((leaf) => {
269
271
  const rate = BigInt(leaf.rate);
270
- if (rate < 0n)
272
+ if (rate < 0n) {
271
273
  throw new errors_js_1.InvalidRateRatifierV1RateError(rate);
274
+ }
272
275
  const allowedTaker = leaf.allowedTaker ?? viem_1.zeroAddress;
273
276
  const input = index_js_3.Offer.from(leaf.offer);
274
- if (input.tick < RateRatifierV1.MIN_TICK)
277
+ if (input.tick < RateRatifierV1.MIN_TICK) {
275
278
  throw new errors_js_1.InvalidRateRatifierV1TickError(input.tick, RateRatifierV1.MIN_TICK);
279
+ }
276
280
  return { offer: input, rate, allowedTaker };
277
281
  });
278
282
  const assignedOffers = (0, ratifierV1Internal_js_1.assignRatifierV1Groups)(normalized, groupId);
279
283
  const offers = [];
280
284
  const structs = normalized.map(({ rate, allowedTaker }, index) => {
281
- const offer = assignedOffers[index];
285
+ const offer = (0, treeMathInternal_js_1.requireTreeItem)(assignedOffers, index);
282
286
  offers.push(offer);
283
287
  return { offer: index_js_3.OfferUtils.toStruct({ offer }), rate, allowedTaker };
284
288
  });
@@ -308,10 +312,12 @@ var RateRatifierV1;
308
312
  isPadding: isPaddingEntry,
309
313
  ratifierOf: (leafStruct) => leafStruct.offer.ratifier,
310
314
  validateEntry: (entry) => {
311
- if (entry.rate < 0n)
315
+ if (entry.rate < 0n) {
312
316
  throw new errors_js_1.InvalidRateRatifierV1RateError(entry.rate);
313
- if (entry.offer.tick < RateRatifierV1.MIN_TICK)
317
+ }
318
+ if (entry.offer.tick < RateRatifierV1.MIN_TICK) {
314
319
  throw new errors_js_1.InvalidRateRatifierV1TickError(entry.offer.tick, RateRatifierV1.MIN_TICK);
320
+ }
315
321
  },
316
322
  label: "RateRatifierV1",
317
323
  type: "rateV1",
@@ -494,7 +500,7 @@ var RateRatifierV1;
494
500
  function ratifierData(params) {
495
501
  const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers);
496
502
  const proof = TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
497
- const entry = tree.entries[Number(proof.leafIndex)];
503
+ const entry = (0, treeMathInternal_js_1.requireTreeItem)(tree.entries, Number(proof.leafIndex));
498
504
  return encodeRatifierData({
499
505
  root: proof.root,
500
506
  leafIndex: proof.leafIndex,
@@ -537,8 +543,8 @@ var RateRatifierV1;
537
543
  count: tree.offers.length,
538
544
  });
539
545
  return tree.offers.map((offer, leafIndex) => {
540
- const entry = tree.entries[leafIndex];
541
- const proof = proofs[leafIndex];
546
+ const entry = (0, treeMathInternal_js_1.requireTreeItem)(tree.entries, leafIndex);
547
+ const proof = (0, treeMathInternal_js_1.requireTreeItem)(proofs, leafIndex);
542
548
  return {
543
549
  offer,
544
550
  ratifierData: encodeRatifierData({
@@ -581,8 +587,9 @@ var RateRatifierV1;
581
587
  */
582
588
  function priceBound(params) {
583
589
  const rate = BigInt(params.rate);
584
- if (rate < 0n)
590
+ if (rate < 0n) {
585
591
  throw new errors_js_1.InvalidRateRatifierV1RateError(rate);
592
+ }
586
593
  const timeToMaturity = BigInt(params.timeToMaturity);
587
594
  if (timeToMaturity < 0n) {
588
595
  throw new errors_js_1.InvalidRateRatifierV1TimeError(timeToMaturity, "timeToMaturity");
@@ -651,8 +658,9 @@ var RateRatifierV1;
651
658
  });
652
659
  }
653
660
  catch (error) {
654
- if (error instanceof errors_js_1.RateRatifierV1BoundOverflowError)
661
+ if (error instanceof errors_js_1.RateRatifierV1BoundOverflowError) {
655
662
  return false;
663
+ }
656
664
  throw error;
657
665
  }
658
666
  const offerPrice = index_js_2.TickLib.tickToPrice(params.offer.tick);
@@ -31,13 +31,13 @@ function normalizeTree(tree) {
31
31
  const computedLeaves = tree.paddedOffers.map(index_js_1.OfferUtils.hashStruct);
32
32
  const seen = new Set();
33
33
  for (const [index, offer] of tree.offers.entries()) {
34
- if ((0, offerStructInternal_js_1.isEmptyOfferStruct)(tree.paddedOffers[index], {
34
+ if ((0, offerStructInternal_js_1.isEmptyOfferStruct)((0, treeMathInternal_js_1.requireTreeItem)(tree.paddedOffers, index), {
35
35
  allowDefaultGroup: true,
36
36
  })) {
37
37
  throw new errors_js_1.InvalidTreeError("Visible offers must not contain tree padding.");
38
38
  }
39
39
  const hash = index_js_1.OfferUtils.hashStruct(index_js_1.OfferUtils.toStruct({ offer }));
40
- if (hash.toLowerCase() !== computedLeaves[index].toLowerCase() ||
40
+ if (hash.toLowerCase() !== computedLeaves[index]?.toLowerCase() ||
41
41
  seen.has(hash.toLowerCase())) {
42
42
  throw new errors_js_1.InvalidTreeError("Visible offers do not match the tree leaves.");
43
43
  }
@@ -48,14 +48,14 @@ function normalizeTree(tree) {
48
48
  .some((offer) => !(0, offerStructInternal_js_1.isEmptyOfferStruct)(offer, { allowDefaultGroup: true }))) {
49
49
  throw new errors_js_1.InvalidTreeError("Tree padding contains offers hidden from the visible offer list.");
50
50
  }
51
- if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !== tree.leaves[index].toLowerCase())) {
51
+ if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !== tree.leaves[index]?.toLowerCase())) {
52
52
  throw new errors_js_1.InvalidTreeError("Tree leaves do not match its padded offers.");
53
53
  }
54
54
  let level = computedLeaves;
55
55
  while (level.length > 1) {
56
56
  const next = [];
57
57
  for (let index = 0; index < level.length; index += 2) {
58
- next.push(TreeUtils_js_1.TreeUtils.hashNode(level[index], level[index + 1]));
58
+ next.push(TreeUtils_js_1.TreeUtils.hashNode((0, treeMathInternal_js_1.requireTreeItem)(level, index), (0, treeMathInternal_js_1.requireTreeItem)(level, index + 1)));
59
59
  }
60
60
  level = next;
61
61
  }
@@ -8,6 +8,7 @@ const index_js_1 = require("../offers/index.js");
8
8
  const Ratifier_js_1 = require("./Ratifier.js");
9
9
  const standardRatifierInternal_js_1 = require("./standardRatifierInternal.js");
10
10
  const TreeUtils_js_1 = require("./TreeUtils.js");
11
+ const treeMathInternal_js_1 = require("./treeMathInternal.js");
11
12
  const setterRatifierDataAbi = [
12
13
  { name: "root", type: "bytes32" },
13
14
  { name: "leafIndex", type: "uint256" },
@@ -252,7 +253,7 @@ var SetterRatifierUtils;
252
253
  });
253
254
  return tree.offers.map((offer, leafIndex) => ({
254
255
  offer,
255
- ratifierData: encodeRatifierData(proofs[leafIndex]),
256
+ ratifierData: encodeRatifierData((0, treeMathInternal_js_1.requireTreeItem)(proofs, leafIndex)),
256
257
  }));
257
258
  }
258
259
  SetterRatifierUtils.ratify = ratify;
@@ -76,8 +76,9 @@ class Tree {
76
76
  this.height = descriptor.height;
77
77
  }
78
78
  static from(tree) {
79
- if (tree instanceof Tree)
79
+ if (tree instanceof Tree) {
80
80
  return tree;
81
+ }
81
82
  // Portable snapshots resume through descriptor validation instead of rebuilding.
82
83
  if (!Array.isArray(tree) &&
83
84
  "entries" in tree &&
@@ -87,8 +88,9 @@ class Tree {
87
88
  ? Tree.fromDescriptor(tree)
88
89
  : Tree.fromDescriptor(tree);
89
90
  }
90
- if ("type" in tree && tree.type != null)
91
+ if ("type" in tree && tree.type != null) {
91
92
  return Tree.create(tree);
93
+ }
92
94
  return Tree.create(Array.isArray(tree) ? tree : [tree]);
93
95
  }
94
96
  static create(params) {
@@ -175,10 +177,12 @@ class Tree {
175
177
  ? index_js_1.OfferUtils.toStruct({ offer })
176
178
  : offer),
177
179
  };
178
- if (descriptor.type === "ecrecover")
180
+ if (descriptor.type === "ecrecover") {
179
181
  return new Tree("ecrecover", normalized);
180
- if (descriptor.type === "setter")
182
+ }
183
+ if (descriptor.type === "setter") {
181
184
  return new Tree("setter", normalized);
185
+ }
182
186
  return new Tree(undefined, normalized);
183
187
  }
184
188
  default:
@@ -19,8 +19,9 @@ const SetterRatifier_js_1 = require("./SetterRatifier.js");
19
19
  const Tree_js_1 = require("./Tree.js");
20
20
  const treeMathInternal_js_1 = require("./treeMathInternal.js");
21
21
  function padOfferStructs(offers) {
22
- if ((0, treeMathInternal_js_1.isPowerOfTwo)(offers.length))
22
+ if ((0, treeMathInternal_js_1.isPowerOfTwo)(offers.length)) {
23
23
  return [...offers];
24
+ }
24
25
  const paddedLength = (0, treeMathInternal_js_1.nextPowerOfTwo)(offers.length);
25
26
  return [
26
27
  ...offers,
@@ -30,9 +31,10 @@ function padOfferStructs(offers) {
30
31
  function assertLeafOffers(offers, leafHashes) {
31
32
  const seen = new Set();
32
33
  for (const [index, offer] of offers.entries()) {
33
- if ((0, offerStructInternal_js_1.isEmptyOfferStruct)(offer, { allowDefaultGroup: true }))
34
+ if ((0, offerStructInternal_js_1.isEmptyOfferStruct)(offer, { allowDefaultGroup: true })) {
34
35
  continue;
35
- const leafHash = leafHashes[index];
36
+ }
37
+ const leafHash = (0, treeMathInternal_js_1.requireTreeItem)(leafHashes, index);
36
38
  if (seen.has(leafHash)) {
37
39
  throw new errors_js_1.InvalidTreeError(`Duplicate offer hash "${leafHash}" in tree.`);
38
40
  }
@@ -44,12 +46,12 @@ function assertLeafOffers(offers, leafHashes) {
44
46
  }
45
47
  }
46
48
  function buildLayers(leaves) {
47
- const layers = [[...leaves]];
48
- let level = layers[0];
49
+ let level = [...leaves];
50
+ const layers = [level];
49
51
  while (level.length > 1) {
50
52
  const next = [];
51
53
  for (let i = 0; i < level.length; i += 2) {
52
- next.push(TreeUtils.hashNode(level[i], level[i + 1]));
54
+ next.push(TreeUtils.hashNode((0, treeMathInternal_js_1.requireTreeItem)(level, i), (0, treeMathInternal_js_1.requireTreeItem)(level, i + 1)));
53
55
  }
54
56
  layers.push(next);
55
57
  level = next;
@@ -61,14 +63,15 @@ function assertProvableLeaves(leaves) {
61
63
  throw new errors_js_1.InvalidTreeError(`Tree leaf count "${leaves.length}" is not a power of two.`);
62
64
  }
63
65
  const height = Math.log2(leaves.length);
64
- if (height > constants_js_1.MAX_TREE_HEIGHT)
66
+ if (height > constants_js_1.MAX_TREE_HEIGHT) {
65
67
  throw new errors_js_1.InvalidTreeHeightError(height);
68
+ }
66
69
  return height;
67
70
  }
68
71
  function computeRootFromLeaves(leaves) {
69
72
  const height = assertProvableLeaves(leaves);
70
73
  const layers = buildLayers(leaves);
71
- return { root: layers[height][0], height };
74
+ return { root: (0, treeMathInternal_js_1.requireTreeItem)((0, treeMathInternal_js_1.requireTreeItem)(layers, height), 0), height };
72
75
  }
73
76
  /**
74
77
  * Object-compatible tree hashing, root, proof, and verification helpers.
@@ -226,17 +229,17 @@ var TreeUtils;
226
229
  }
227
230
  }
228
231
  else if (params.ratification.type === "ecrecover") {
229
- if (params.ratification.signature != null) {
232
+ if (params.ratification.signature == null) {
230
233
  items = await EcrecoverRatifier_js_1.EcrecoverRatifierUtils.ratify({
231
234
  tree,
232
- signature: params.ratification.signature,
235
+ client: params.ratification.client,
233
236
  account: params.ratification.account,
234
237
  });
235
238
  }
236
239
  else {
237
240
  items = await EcrecoverRatifier_js_1.EcrecoverRatifierUtils.ratify({
238
241
  tree,
239
- client: params.ratification.client,
242
+ signature: params.ratification.signature,
240
243
  account: params.ratification.account,
241
244
  });
242
245
  }
@@ -482,7 +485,7 @@ var TreeUtils;
482
485
  const layers = buildLayers(params.tree.leaves);
483
486
  const proof = layers
484
487
  .slice(0, -1)
485
- .map((level, layerIndex) => level[(index >> layerIndex) ^ 1]);
488
+ .map((level, layerIndex) => (0, treeMathInternal_js_1.requireTreeItem)(level, (index >> layerIndex) ^ 1));
486
489
  return (0, morpho_ts_1.deepFreeze)({ root: params.tree.root, leafIndex, proof });
487
490
  }
488
491
  TreeUtils.buildProof = buildProof;
@@ -550,7 +553,7 @@ var TreeUtils;
550
553
  return (0, morpho_ts_1.deepFreeze)(Array.from({ length: count }, (_, index) => ({
551
554
  root: params.tree.root,
552
555
  leafIndex: BigInt(index),
553
- proof: siblingLayers.map((level, layerIndex) => level[(index >> layerIndex) ^ 1]),
556
+ proof: siblingLayers.map((level, layerIndex) => (0, treeMathInternal_js_1.requireTreeItem)(level, (index >> layerIndex) ^ 1)),
554
557
  })));
555
558
  }
556
559
  TreeUtils.buildProofs = buildProofs;