@morpho-org/midnight-sdk 2.7.0 → 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.
- package/lib/cjs/api/MidnightApi.js +63 -52
- package/lib/cjs/api/helpers.js +30 -17
- package/lib/cjs/errors.d.ts +13 -27
- package/lib/cjs/errors.js +8 -39
- package/lib/cjs/fetch/Position.js +2 -1
- package/lib/cjs/fetch/Ratifier.js +4 -5
- package/lib/cjs/market/Market.js +2 -1
- package/lib/cjs/market/MarketUtils.js +17 -17
- package/lib/cjs/math/TakeAmountsLib.js +8 -4
- package/lib/cjs/math/TickLib.js +12 -6
- package/lib/cjs/offers/OfferChainUtils.js +35 -26
- package/lib/cjs/offers/OfferUtils.js +17 -10
- package/lib/cjs/position/Position.js +2 -1
- package/lib/cjs/position/PositionUtils.js +11 -35
- package/lib/cjs/signatures/EcrecoverRatifier.js +22 -17
- package/lib/cjs/signatures/Group.js +4 -2
- package/lib/cjs/signatures/Payload.js +18 -9
- package/lib/cjs/signatures/PriceRatifierV1.js +5 -4
- package/lib/cjs/signatures/RateRatifierV1.js +19 -11
- package/lib/cjs/signatures/Ratifier.js +4 -4
- package/lib/cjs/signatures/SetterRatifier.js +2 -1
- package/lib/cjs/signatures/Tree.js +8 -4
- package/lib/cjs/signatures/TreeUtils.js +16 -13
- package/lib/cjs/signatures/ratifierV1Internal.js +34 -24
- package/lib/cjs/signatures/treeMathInternal.d.ts +2 -0
- package/lib/cjs/signatures/treeMathInternal.js +10 -0
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/api/MidnightApi.js +63 -52
- package/lib/esm/api/helpers.js +30 -17
- package/lib/esm/errors.d.ts +13 -27
- package/lib/esm/errors.js +8 -36
- package/lib/esm/fetch/Position.js +2 -1
- package/lib/esm/fetch/Ratifier.js +4 -5
- package/lib/esm/market/Market.js +2 -1
- package/lib/esm/market/MarketUtils.js +19 -19
- package/lib/esm/math/TakeAmountsLib.js +8 -4
- package/lib/esm/math/TickLib.js +12 -6
- package/lib/esm/offers/OfferChainUtils.js +35 -26
- package/lib/esm/offers/OfferUtils.js +17 -10
- package/lib/esm/position/Position.js +2 -1
- package/lib/esm/position/PositionUtils.js +12 -36
- package/lib/esm/signatures/EcrecoverRatifier.js +22 -17
- package/lib/esm/signatures/Group.js +4 -2
- package/lib/esm/signatures/Payload.js +18 -9
- package/lib/esm/signatures/PriceRatifierV1.js +5 -4
- package/lib/esm/signatures/RateRatifierV1.js +19 -11
- package/lib/esm/signatures/Ratifier.js +5 -5
- package/lib/esm/signatures/SetterRatifier.js +2 -1
- package/lib/esm/signatures/Tree.js +8 -4
- package/lib/esm/signatures/TreeUtils.js +17 -14
- package/lib/esm/signatures/ratifierV1Internal.js +35 -25
- package/lib/esm/signatures/treeMathInternal.d.ts +2 -0
- package/lib/esm/signatures/treeMathInternal.js +9 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +4 -4
|
@@ -339,8 +339,9 @@ export var Payload;
|
|
|
339
339
|
});
|
|
340
340
|
}
|
|
341
341
|
catch (error) {
|
|
342
|
-
if (error instanceof PayloadDecodeError)
|
|
342
|
+
if (error instanceof PayloadDecodeError) {
|
|
343
343
|
throw error;
|
|
344
|
+
}
|
|
344
345
|
if (error instanceof Error) {
|
|
345
346
|
throw new PayloadDecodeError(`invalid offer bytes: ${error.message}`, error);
|
|
346
347
|
}
|
|
@@ -359,8 +360,9 @@ export var Payload;
|
|
|
359
360
|
}
|
|
360
361
|
}
|
|
361
362
|
function assertApiValidOfferStruct(offer) {
|
|
362
|
-
if (isEmptyOfferStruct(offer))
|
|
363
|
+
if (isEmptyOfferStruct(offer)) {
|
|
363
364
|
return;
|
|
365
|
+
}
|
|
364
366
|
assertCollateralParams(offer);
|
|
365
367
|
assertSafeTimestamp("market.maturity", offer.market.maturity);
|
|
366
368
|
assertMaturityAt15Utc(offer.market.maturity);
|
|
@@ -451,20 +453,23 @@ export var Payload;
|
|
|
451
453
|
}
|
|
452
454
|
}
|
|
453
455
|
function resolveMaxItems(maxItems) {
|
|
454
|
-
if (maxItems === undefined)
|
|
456
|
+
if (maxItems === undefined) {
|
|
455
457
|
return undefined;
|
|
458
|
+
}
|
|
456
459
|
if (!Number.isInteger(maxItems) || maxItems < 1) {
|
|
457
460
|
throw new PayloadDecodeError(`maxItems must be a positive integer: got ${maxItems}`);
|
|
458
461
|
}
|
|
459
462
|
return maxItems;
|
|
460
463
|
}
|
|
461
464
|
function assertNonEmptyItemCount(count) {
|
|
462
|
-
if (count === 0)
|
|
465
|
+
if (count === 0) {
|
|
463
466
|
throw new PayloadDecodeError("items payload is empty");
|
|
467
|
+
}
|
|
464
468
|
}
|
|
465
469
|
function assertCallerItemCountLimit(count, maxItems) {
|
|
466
|
-
if (maxItems === undefined)
|
|
470
|
+
if (maxItems === undefined) {
|
|
467
471
|
return;
|
|
472
|
+
}
|
|
468
473
|
if (count > maxItems) {
|
|
469
474
|
throw new PayloadDecodeError(`items payload exceeds ${maxItems} items`);
|
|
470
475
|
}
|
|
@@ -487,8 +492,9 @@ export var Payload;
|
|
|
487
492
|
return compressed;
|
|
488
493
|
}
|
|
489
494
|
async function decompressItemsPayload(compressed) {
|
|
490
|
-
if (compressed.length === 0)
|
|
495
|
+
if (compressed.length === 0) {
|
|
491
496
|
throw new PayloadDecodeError("decompression failed");
|
|
497
|
+
}
|
|
492
498
|
if (compressed.length > Payload.MAX_COMPRESSED_ITEMS_BYTES) {
|
|
493
499
|
throw new PayloadDecodeError(`compressed items exceed ${Payload.MAX_COMPRESSED_ITEMS_BYTES} bytes`);
|
|
494
500
|
}
|
|
@@ -500,8 +506,9 @@ export var Payload;
|
|
|
500
506
|
try {
|
|
501
507
|
while (true) {
|
|
502
508
|
const { done, value } = await reader.read();
|
|
503
|
-
if (done)
|
|
509
|
+
if (done) {
|
|
504
510
|
break;
|
|
511
|
+
}
|
|
505
512
|
totalLength += value.length;
|
|
506
513
|
if (totalLength > Payload.MAX_DECOMPRESSED_ITEMS_BYTES) {
|
|
507
514
|
await reader.cancel();
|
|
@@ -511,8 +518,9 @@ export var Payload;
|
|
|
511
518
|
}
|
|
512
519
|
}
|
|
513
520
|
catch (error) {
|
|
514
|
-
if (error instanceof PayloadDecodeError)
|
|
521
|
+
if (error instanceof PayloadDecodeError) {
|
|
515
522
|
throw error;
|
|
523
|
+
}
|
|
516
524
|
throw new PayloadDecodeError("decompression failed", error instanceof Error ? error : undefined);
|
|
517
525
|
}
|
|
518
526
|
const result = new Uint8Array(totalLength);
|
|
@@ -535,8 +543,9 @@ export var Payload;
|
|
|
535
543
|
if (decoded.length < ABI_ARRAY_HEAD_BYTES) {
|
|
536
544
|
throw new PayloadDecodeError("items payload truncated before ABI array head");
|
|
537
545
|
}
|
|
538
|
-
if (maxItems === undefined)
|
|
546
|
+
if (maxItems === undefined) {
|
|
539
547
|
return;
|
|
548
|
+
}
|
|
540
549
|
for (let i = 32; i < ABI_LENGTH_LOW_OFFSET; i++) {
|
|
541
550
|
if (decoded[i] !== 0) {
|
|
542
551
|
throw new PayloadDecodeError(`items payload exceeds ${maxItems} items`);
|
|
@@ -9,6 +9,7 @@ import { GroupUtils } from "./GroupUtils.js";
|
|
|
9
9
|
import { EMPTY_OFFER_STRUCT, isEmptyOfferStruct, isZeroAddress, } from "./offerStructInternal.js";
|
|
10
10
|
import { assertRatifierV1Address, assertRatifierV1Taker, assignRatifierV1Groups, buildRatifierV1Descriptor, resolveRatifierV1Tree, } from "./ratifierV1Internal.js";
|
|
11
11
|
import { TreeUtils } from "./TreeUtils.js";
|
|
12
|
+
import { requireTreeItem } from "./treeMathInternal.js";
|
|
12
13
|
const priceRatifierV1DataAbi = [
|
|
13
14
|
{ name: "root", type: "bytes32" },
|
|
14
15
|
{ name: "leafIndex", type: "uint256" },
|
|
@@ -237,7 +238,7 @@ export var PriceRatifierV1;
|
|
|
237
238
|
const assignedOffers = assignRatifierV1Groups(normalized, groupId);
|
|
238
239
|
const offers = [];
|
|
239
240
|
const structs = normalized.map(({ allowedTaker }, index) => {
|
|
240
|
-
const offer = assignedOffers
|
|
241
|
+
const offer = requireTreeItem(assignedOffers, index);
|
|
241
242
|
offers.push(offer);
|
|
242
243
|
return { offer: OfferUtils.toStruct({ offer }), allowedTaker };
|
|
243
244
|
});
|
|
@@ -427,7 +428,7 @@ export var PriceRatifierV1;
|
|
|
427
428
|
function ratifierData(params) {
|
|
428
429
|
const tree = resolveRatifierV1Tree(params.tree, treeHelpers);
|
|
429
430
|
const proof = TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
|
|
430
|
-
const entry = tree.entries
|
|
431
|
+
const entry = requireTreeItem(tree.entries, Number(proof.leafIndex));
|
|
431
432
|
return encodeRatifierData({
|
|
432
433
|
root: proof.root,
|
|
433
434
|
leafIndex: proof.leafIndex,
|
|
@@ -467,8 +468,8 @@ export var PriceRatifierV1;
|
|
|
467
468
|
count: tree.offers.length,
|
|
468
469
|
});
|
|
469
470
|
return tree.offers.map((offer, leafIndex) => {
|
|
470
|
-
const entry = tree.entries
|
|
471
|
-
const proof = proofs
|
|
471
|
+
const entry = requireTreeItem(tree.entries, leafIndex);
|
|
472
|
+
const proof = requireTreeItem(proofs, leafIndex);
|
|
472
473
|
return {
|
|
473
474
|
offer,
|
|
474
475
|
ratifierData: encodeRatifierData({
|
|
@@ -10,6 +10,7 @@ import { GroupUtils } from "./GroupUtils.js";
|
|
|
10
10
|
import { EMPTY_OFFER_STRUCT, isEmptyOfferStruct, isZeroAddress, } from "./offerStructInternal.js";
|
|
11
11
|
import { assertRatifierV1Address, assertRatifierV1Taker, assignRatifierV1Groups, buildRatifierV1Descriptor, resolveRatifierV1Tree, } from "./ratifierV1Internal.js";
|
|
12
12
|
import { TreeUtils } from "./TreeUtils.js";
|
|
13
|
+
import { requireTreeItem } from "./treeMathInternal.js";
|
|
13
14
|
const rateRatifierV1DataAbi = [
|
|
14
15
|
{ name: "root", type: "bytes32" },
|
|
15
16
|
{ name: "leafIndex", type: "uint256" },
|
|
@@ -164,8 +165,9 @@ export var RateRatifierV1;
|
|
|
164
165
|
*/
|
|
165
166
|
function memberHash(leaf) {
|
|
166
167
|
const rate = BigInt(leaf.rate);
|
|
167
|
-
if (rate < 0n)
|
|
168
|
+
if (rate < 0n) {
|
|
168
169
|
throw new InvalidRateRatifierV1RateError(rate);
|
|
170
|
+
}
|
|
169
171
|
return hashLeaf({
|
|
170
172
|
offer: OfferUtils.toStruct({ offer: leaf.offer, group: zeroHash }),
|
|
171
173
|
rate,
|
|
@@ -264,18 +266,20 @@ export var RateRatifierV1;
|
|
|
264
266
|
function buildDescriptor(leaves) {
|
|
265
267
|
const normalized = leaves.map((leaf) => {
|
|
266
268
|
const rate = BigInt(leaf.rate);
|
|
267
|
-
if (rate < 0n)
|
|
269
|
+
if (rate < 0n) {
|
|
268
270
|
throw new InvalidRateRatifierV1RateError(rate);
|
|
271
|
+
}
|
|
269
272
|
const allowedTaker = leaf.allowedTaker ?? zeroAddress;
|
|
270
273
|
const input = Offer.from(leaf.offer);
|
|
271
|
-
if (input.tick < RateRatifierV1.MIN_TICK)
|
|
274
|
+
if (input.tick < RateRatifierV1.MIN_TICK) {
|
|
272
275
|
throw new InvalidRateRatifierV1TickError(input.tick, RateRatifierV1.MIN_TICK);
|
|
276
|
+
}
|
|
273
277
|
return { offer: input, rate, allowedTaker };
|
|
274
278
|
});
|
|
275
279
|
const assignedOffers = assignRatifierV1Groups(normalized, groupId);
|
|
276
280
|
const offers = [];
|
|
277
281
|
const structs = normalized.map(({ rate, allowedTaker }, index) => {
|
|
278
|
-
const offer = assignedOffers
|
|
282
|
+
const offer = requireTreeItem(assignedOffers, index);
|
|
279
283
|
offers.push(offer);
|
|
280
284
|
return { offer: OfferUtils.toStruct({ offer }), rate, allowedTaker };
|
|
281
285
|
});
|
|
@@ -305,10 +309,12 @@ export var RateRatifierV1;
|
|
|
305
309
|
isPadding: isPaddingEntry,
|
|
306
310
|
ratifierOf: (leafStruct) => leafStruct.offer.ratifier,
|
|
307
311
|
validateEntry: (entry) => {
|
|
308
|
-
if (entry.rate < 0n)
|
|
312
|
+
if (entry.rate < 0n) {
|
|
309
313
|
throw new InvalidRateRatifierV1RateError(entry.rate);
|
|
310
|
-
|
|
314
|
+
}
|
|
315
|
+
if (entry.offer.tick < RateRatifierV1.MIN_TICK) {
|
|
311
316
|
throw new InvalidRateRatifierV1TickError(entry.offer.tick, RateRatifierV1.MIN_TICK);
|
|
317
|
+
}
|
|
312
318
|
},
|
|
313
319
|
label: "RateRatifierV1",
|
|
314
320
|
type: "rateV1",
|
|
@@ -491,7 +497,7 @@ export var RateRatifierV1;
|
|
|
491
497
|
function ratifierData(params) {
|
|
492
498
|
const tree = resolveRatifierV1Tree(params.tree, treeHelpers);
|
|
493
499
|
const proof = TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
|
|
494
|
-
const entry = tree.entries
|
|
500
|
+
const entry = requireTreeItem(tree.entries, Number(proof.leafIndex));
|
|
495
501
|
return encodeRatifierData({
|
|
496
502
|
root: proof.root,
|
|
497
503
|
leafIndex: proof.leafIndex,
|
|
@@ -534,8 +540,8 @@ export var RateRatifierV1;
|
|
|
534
540
|
count: tree.offers.length,
|
|
535
541
|
});
|
|
536
542
|
return tree.offers.map((offer, leafIndex) => {
|
|
537
|
-
const entry = tree.entries
|
|
538
|
-
const proof = proofs
|
|
543
|
+
const entry = requireTreeItem(tree.entries, leafIndex);
|
|
544
|
+
const proof = requireTreeItem(proofs, leafIndex);
|
|
539
545
|
return {
|
|
540
546
|
offer,
|
|
541
547
|
ratifierData: encodeRatifierData({
|
|
@@ -578,8 +584,9 @@ export var RateRatifierV1;
|
|
|
578
584
|
*/
|
|
579
585
|
function priceBound(params) {
|
|
580
586
|
const rate = BigInt(params.rate);
|
|
581
|
-
if (rate < 0n)
|
|
587
|
+
if (rate < 0n) {
|
|
582
588
|
throw new InvalidRateRatifierV1RateError(rate);
|
|
589
|
+
}
|
|
583
590
|
const timeToMaturity = BigInt(params.timeToMaturity);
|
|
584
591
|
if (timeToMaturity < 0n) {
|
|
585
592
|
throw new InvalidRateRatifierV1TimeError(timeToMaturity, "timeToMaturity");
|
|
@@ -648,8 +655,9 @@ export var RateRatifierV1;
|
|
|
648
655
|
});
|
|
649
656
|
}
|
|
650
657
|
catch (error) {
|
|
651
|
-
if (error instanceof RateRatifierV1BoundOverflowError)
|
|
658
|
+
if (error instanceof RateRatifierV1BoundOverflowError) {
|
|
652
659
|
return false;
|
|
660
|
+
}
|
|
653
661
|
throw error;
|
|
654
662
|
}
|
|
655
663
|
const offerPrice = TickLib.tickToPrice(params.offer.tick);
|
|
@@ -3,7 +3,7 @@ import { InvalidTreeError, InvalidTreeHeightError } from "../errors.js";
|
|
|
3
3
|
import { OfferUtils } from "../offers/index.js";
|
|
4
4
|
import { isEmptyOfferStruct } from "./offerStructInternal.js";
|
|
5
5
|
import { TreeUtils } from "./TreeUtils.js";
|
|
6
|
-
import { nextPowerOfTwo } from "./treeMathInternal.js";
|
|
6
|
+
import { nextPowerOfTwo, requireTreeItem } from "./treeMathInternal.js";
|
|
7
7
|
function isTreeLike(tree) {
|
|
8
8
|
return (!Array.isArray(tree) &&
|
|
9
9
|
"offers" in tree &&
|
|
@@ -28,13 +28,13 @@ function normalizeTree(tree) {
|
|
|
28
28
|
const computedLeaves = tree.paddedOffers.map(OfferUtils.hashStruct);
|
|
29
29
|
const seen = new Set();
|
|
30
30
|
for (const [index, offer] of tree.offers.entries()) {
|
|
31
|
-
if (isEmptyOfferStruct(tree.paddedOffers
|
|
31
|
+
if (isEmptyOfferStruct(requireTreeItem(tree.paddedOffers, index), {
|
|
32
32
|
allowDefaultGroup: true,
|
|
33
33
|
})) {
|
|
34
34
|
throw new InvalidTreeError("Visible offers must not contain tree padding.");
|
|
35
35
|
}
|
|
36
36
|
const hash = OfferUtils.hashStruct(OfferUtils.toStruct({ offer }));
|
|
37
|
-
if (hash.toLowerCase() !== computedLeaves[index]
|
|
37
|
+
if (hash.toLowerCase() !== computedLeaves[index]?.toLowerCase() ||
|
|
38
38
|
seen.has(hash.toLowerCase())) {
|
|
39
39
|
throw new InvalidTreeError("Visible offers do not match the tree leaves.");
|
|
40
40
|
}
|
|
@@ -45,14 +45,14 @@ function normalizeTree(tree) {
|
|
|
45
45
|
.some((offer) => !isEmptyOfferStruct(offer, { allowDefaultGroup: true }))) {
|
|
46
46
|
throw new InvalidTreeError("Tree padding contains offers hidden from the visible offer list.");
|
|
47
47
|
}
|
|
48
|
-
if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !== tree.leaves[index]
|
|
48
|
+
if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !== tree.leaves[index]?.toLowerCase())) {
|
|
49
49
|
throw new InvalidTreeError("Tree leaves do not match its padded offers.");
|
|
50
50
|
}
|
|
51
51
|
let level = computedLeaves;
|
|
52
52
|
while (level.length > 1) {
|
|
53
53
|
const next = [];
|
|
54
54
|
for (let index = 0; index < level.length; index += 2) {
|
|
55
|
-
next.push(TreeUtils.hashNode(level
|
|
55
|
+
next.push(TreeUtils.hashNode(requireTreeItem(level, index), requireTreeItem(level, index + 1)));
|
|
56
56
|
}
|
|
57
57
|
level = next;
|
|
58
58
|
}
|
|
@@ -5,6 +5,7 @@ import { Offer } from "../offers/index.js";
|
|
|
5
5
|
import { Ratifier } from "./Ratifier.js";
|
|
6
6
|
import { parseStandardRatifierTree } from "./standardRatifierInternal.js";
|
|
7
7
|
import { TreeUtils } from "./TreeUtils.js";
|
|
8
|
+
import { requireTreeItem } from "./treeMathInternal.js";
|
|
8
9
|
const setterRatifierDataAbi = [
|
|
9
10
|
{ name: "root", type: "bytes32" },
|
|
10
11
|
{ name: "leafIndex", type: "uint256" },
|
|
@@ -249,7 +250,7 @@ export var SetterRatifierUtils;
|
|
|
249
250
|
});
|
|
250
251
|
return tree.offers.map((offer, leafIndex) => ({
|
|
251
252
|
offer,
|
|
252
|
-
ratifierData: encodeRatifierData(proofs
|
|
253
|
+
ratifierData: encodeRatifierData(requireTreeItem(proofs, leafIndex)),
|
|
253
254
|
}));
|
|
254
255
|
}
|
|
255
256
|
SetterRatifierUtils.ratify = ratify;
|
|
@@ -73,8 +73,9 @@ export class Tree {
|
|
|
73
73
|
this.height = descriptor.height;
|
|
74
74
|
}
|
|
75
75
|
static from(tree) {
|
|
76
|
-
if (tree instanceof Tree)
|
|
76
|
+
if (tree instanceof Tree) {
|
|
77
77
|
return tree;
|
|
78
|
+
}
|
|
78
79
|
// Portable snapshots resume through descriptor validation instead of rebuilding.
|
|
79
80
|
if (!Array.isArray(tree) &&
|
|
80
81
|
"entries" in tree &&
|
|
@@ -84,8 +85,9 @@ export class Tree {
|
|
|
84
85
|
? Tree.fromDescriptor(tree)
|
|
85
86
|
: Tree.fromDescriptor(tree);
|
|
86
87
|
}
|
|
87
|
-
if ("type" in tree && tree.type != null)
|
|
88
|
+
if ("type" in tree && tree.type != null) {
|
|
88
89
|
return Tree.create(tree);
|
|
90
|
+
}
|
|
89
91
|
return Tree.create(Array.isArray(tree) ? tree : [tree]);
|
|
90
92
|
}
|
|
91
93
|
static create(params) {
|
|
@@ -172,10 +174,12 @@ export class Tree {
|
|
|
172
174
|
? OfferUtils.toStruct({ offer })
|
|
173
175
|
: offer),
|
|
174
176
|
};
|
|
175
|
-
if (descriptor.type === "ecrecover")
|
|
177
|
+
if (descriptor.type === "ecrecover") {
|
|
176
178
|
return new Tree("ecrecover", normalized);
|
|
177
|
-
|
|
179
|
+
}
|
|
180
|
+
if (descriptor.type === "setter") {
|
|
178
181
|
return new Tree("setter", normalized);
|
|
182
|
+
}
|
|
179
183
|
return new Tree(undefined, normalized);
|
|
180
184
|
}
|
|
181
185
|
default:
|
|
@@ -14,10 +14,11 @@ import { RateRatifierV1 } from "./RateRatifierV1.js";
|
|
|
14
14
|
import { Ratifier } from "./Ratifier.js";
|
|
15
15
|
import { SetterRatifierUtils } from "./SetterRatifier.js";
|
|
16
16
|
import { Tree } from "./Tree.js";
|
|
17
|
-
import { isPowerOfTwo, nextPowerOfTwo } from "./treeMathInternal.js";
|
|
17
|
+
import { isPowerOfTwo, nextPowerOfTwo, requireTreeItem, } from "./treeMathInternal.js";
|
|
18
18
|
function padOfferStructs(offers) {
|
|
19
|
-
if (isPowerOfTwo(offers.length))
|
|
19
|
+
if (isPowerOfTwo(offers.length)) {
|
|
20
20
|
return [...offers];
|
|
21
|
+
}
|
|
21
22
|
const paddedLength = nextPowerOfTwo(offers.length);
|
|
22
23
|
return [
|
|
23
24
|
...offers,
|
|
@@ -27,9 +28,10 @@ function padOfferStructs(offers) {
|
|
|
27
28
|
function assertLeafOffers(offers, leafHashes) {
|
|
28
29
|
const seen = new Set();
|
|
29
30
|
for (const [index, offer] of offers.entries()) {
|
|
30
|
-
if (isEmptyOfferStruct(offer, { allowDefaultGroup: true }))
|
|
31
|
+
if (isEmptyOfferStruct(offer, { allowDefaultGroup: true })) {
|
|
31
32
|
continue;
|
|
32
|
-
|
|
33
|
+
}
|
|
34
|
+
const leafHash = requireTreeItem(leafHashes, index);
|
|
33
35
|
if (seen.has(leafHash)) {
|
|
34
36
|
throw new InvalidTreeError(`Duplicate offer hash "${leafHash}" in tree.`);
|
|
35
37
|
}
|
|
@@ -41,12 +43,12 @@ function assertLeafOffers(offers, leafHashes) {
|
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
function buildLayers(leaves) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
let level = [...leaves];
|
|
47
|
+
const layers = [level];
|
|
46
48
|
while (level.length > 1) {
|
|
47
49
|
const next = [];
|
|
48
50
|
for (let i = 0; i < level.length; i += 2) {
|
|
49
|
-
next.push(TreeUtils.hashNode(level
|
|
51
|
+
next.push(TreeUtils.hashNode(requireTreeItem(level, i), requireTreeItem(level, i + 1)));
|
|
50
52
|
}
|
|
51
53
|
layers.push(next);
|
|
52
54
|
level = next;
|
|
@@ -58,14 +60,15 @@ function assertProvableLeaves(leaves) {
|
|
|
58
60
|
throw new InvalidTreeError(`Tree leaf count "${leaves.length}" is not a power of two.`);
|
|
59
61
|
}
|
|
60
62
|
const height = Math.log2(leaves.length);
|
|
61
|
-
if (height > MAX_TREE_HEIGHT)
|
|
63
|
+
if (height > MAX_TREE_HEIGHT) {
|
|
62
64
|
throw new InvalidTreeHeightError(height);
|
|
65
|
+
}
|
|
63
66
|
return height;
|
|
64
67
|
}
|
|
65
68
|
function computeRootFromLeaves(leaves) {
|
|
66
69
|
const height = assertProvableLeaves(leaves);
|
|
67
70
|
const layers = buildLayers(leaves);
|
|
68
|
-
return { root: layers
|
|
71
|
+
return { root: requireTreeItem(requireTreeItem(layers, height), 0), height };
|
|
69
72
|
}
|
|
70
73
|
/**
|
|
71
74
|
* Object-compatible tree hashing, root, proof, and verification helpers.
|
|
@@ -223,17 +226,17 @@ export var TreeUtils;
|
|
|
223
226
|
}
|
|
224
227
|
}
|
|
225
228
|
else if (params.ratification.type === "ecrecover") {
|
|
226
|
-
if (params.ratification.signature
|
|
229
|
+
if (params.ratification.signature == null) {
|
|
227
230
|
items = await EcrecoverRatifierUtils.ratify({
|
|
228
231
|
tree,
|
|
229
|
-
|
|
232
|
+
client: params.ratification.client,
|
|
230
233
|
account: params.ratification.account,
|
|
231
234
|
});
|
|
232
235
|
}
|
|
233
236
|
else {
|
|
234
237
|
items = await EcrecoverRatifierUtils.ratify({
|
|
235
238
|
tree,
|
|
236
|
-
|
|
239
|
+
signature: params.ratification.signature,
|
|
237
240
|
account: params.ratification.account,
|
|
238
241
|
});
|
|
239
242
|
}
|
|
@@ -479,7 +482,7 @@ export var TreeUtils;
|
|
|
479
482
|
const layers = buildLayers(params.tree.leaves);
|
|
480
483
|
const proof = layers
|
|
481
484
|
.slice(0, -1)
|
|
482
|
-
.map((level, layerIndex) => level
|
|
485
|
+
.map((level, layerIndex) => requireTreeItem(level, (index >> layerIndex) ^ 1));
|
|
483
486
|
return deepFreeze({ root: params.tree.root, leafIndex, proof });
|
|
484
487
|
}
|
|
485
488
|
TreeUtils.buildProof = buildProof;
|
|
@@ -547,7 +550,7 @@ export var TreeUtils;
|
|
|
547
550
|
return deepFreeze(Array.from({ length: count }, (_, index) => ({
|
|
548
551
|
root: params.tree.root,
|
|
549
552
|
leafIndex: BigInt(index),
|
|
550
|
-
proof: siblingLayers.map((level, layerIndex) => level
|
|
553
|
+
proof: siblingLayers.map((level, layerIndex) => requireTreeItem(level, (index >> layerIndex) ^ 1)),
|
|
551
554
|
})));
|
|
552
555
|
}
|
|
553
556
|
TreeUtils.buildProofs = buildProofs;
|
|
@@ -5,7 +5,7 @@ import { Offer, OfferUtils, } from "../offers/index.js";
|
|
|
5
5
|
import { GroupUtils } from "./GroupUtils.js";
|
|
6
6
|
import { isZeroAddress } from "./offerStructInternal.js";
|
|
7
7
|
import { TreeUtils } from "./TreeUtils.js";
|
|
8
|
-
import { isPowerOfTwo, nextPowerOfTwo } from "./treeMathInternal.js";
|
|
8
|
+
import { isPowerOfTwo, nextPowerOfTwo, requireTreeItem, } from "./treeMathInternal.js";
|
|
9
9
|
/** @internal Resolves V1 leaf groups mirroring the router's group_identity content ids and validates explicit groups. */
|
|
10
10
|
export function assignRatifierV1Groups(leaves, groupId) {
|
|
11
11
|
const offers = leaves.map((leaf) => leaf.offer.hasExplicitGroup
|
|
@@ -13,17 +13,20 @@ export function assignRatifierV1Groups(leaves, groupId) {
|
|
|
13
13
|
: Offer.from({ ...leaf.offer, group: groupId([leaf]) }));
|
|
14
14
|
const buckets = new Map();
|
|
15
15
|
for (const [index, leaf] of leaves.entries()) {
|
|
16
|
-
if (!leaf.offer.hasExplicitGroup)
|
|
16
|
+
if (!leaf.offer.hasExplicitGroup) {
|
|
17
17
|
continue;
|
|
18
|
+
}
|
|
18
19
|
const label = leaf.offer.group.toLowerCase();
|
|
19
20
|
const bucket = buckets.get(label);
|
|
20
|
-
if (bucket)
|
|
21
|
+
if (bucket) {
|
|
21
22
|
bucket.push(index);
|
|
22
|
-
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
23
25
|
buckets.set(label, [index]);
|
|
26
|
+
}
|
|
24
27
|
}
|
|
25
28
|
for (const [label, indices] of buckets) {
|
|
26
|
-
const bucketLeaves = indices.map((index) => leaves
|
|
29
|
+
const bucketLeaves = indices.map((index) => requireTreeItem(leaves, index));
|
|
27
30
|
// Called only to throw on incompatible group members.
|
|
28
31
|
OfferUtils.validateOfferGroup({
|
|
29
32
|
offers: bucketLeaves.map((leaf) => leaf.offer),
|
|
@@ -34,13 +37,17 @@ export function assignRatifierV1Groups(leaves, groupId) {
|
|
|
34
37
|
}
|
|
35
38
|
const group = groupId(bucketLeaves);
|
|
36
39
|
for (const index of indices) {
|
|
37
|
-
offers[index] = Offer.from({
|
|
40
|
+
offers[index] = Offer.from({
|
|
41
|
+
...requireTreeItem(leaves, index).offer,
|
|
42
|
+
group,
|
|
43
|
+
});
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
const finalGroups = new Map();
|
|
41
47
|
for (const [index, offer] of offers.entries()) {
|
|
42
48
|
const label = offer.group.toLowerCase();
|
|
43
|
-
const hasExplicitGroup = leaves
|
|
49
|
+
const hasExplicitGroup = requireTreeItem(leaves, index).offer
|
|
50
|
+
.hasExplicitGroup;
|
|
44
51
|
const bucket = finalGroups.get(label);
|
|
45
52
|
if (bucket) {
|
|
46
53
|
bucket.offers.push(offer);
|
|
@@ -51,8 +58,9 @@ export function assignRatifierV1Groups(leaves, groupId) {
|
|
|
51
58
|
}
|
|
52
59
|
}
|
|
53
60
|
for (const { offers: groupOffers, hasExplicitGroup, } of finalGroups.values()) {
|
|
54
|
-
if (!hasExplicitGroup)
|
|
61
|
+
if (!hasExplicitGroup) {
|
|
55
62
|
continue;
|
|
63
|
+
}
|
|
56
64
|
// Catches explicit groups that collide with a defaulted or re-derived id.
|
|
57
65
|
OfferUtils.validateOfferGroup({ offers: groupOffers });
|
|
58
66
|
}
|
|
@@ -78,9 +86,9 @@ export function buildRatifierV1Descriptor(params) {
|
|
|
78
86
|
throw new InvalidTreeError(`All offers in a ${label} tree must use one ratifier; expected "${ratifier}", got "${other}". Build separate trees per ratifier.`);
|
|
79
87
|
}
|
|
80
88
|
}
|
|
81
|
-
const
|
|
89
|
+
const leaves = entries.map(hashLeaf);
|
|
82
90
|
const seen = new Set();
|
|
83
|
-
for (const leafHash of
|
|
91
|
+
for (const leafHash of leaves) {
|
|
84
92
|
if (seen.has(leafHash)) {
|
|
85
93
|
throw new InvalidTreeError(`Duplicate leaf hash "${leafHash}" in tree.`);
|
|
86
94
|
}
|
|
@@ -93,11 +101,12 @@ export function buildRatifierV1Descriptor(params) {
|
|
|
93
101
|
...entries,
|
|
94
102
|
...Array.from({ length: paddingCount }, () => padding),
|
|
95
103
|
];
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
if (paddingCount > 0) {
|
|
105
|
+
const paddingHash = hashLeaf(padding);
|
|
106
|
+
for (let index = 0; index < paddingCount; index++) {
|
|
107
|
+
leaves.push(paddingHash);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
101
110
|
const { root, height } = TreeUtils.buildRootFromLeaves(leaves);
|
|
102
111
|
return { entries: paddedEntries, leaves, root, height };
|
|
103
112
|
}
|
|
@@ -113,8 +122,9 @@ export function buildRatifierV1Descriptor(params) {
|
|
|
113
122
|
*/
|
|
114
123
|
export function resolveRatifierV1Tree(tree, helpers) {
|
|
115
124
|
const { buildDescriptor, hashLeaf, isPadding, ratifierOf, label } = helpers;
|
|
116
|
-
if (Array.isArray(tree))
|
|
125
|
+
if (Array.isArray(tree)) {
|
|
117
126
|
return buildDescriptor(tree);
|
|
127
|
+
}
|
|
118
128
|
const descriptor = tree;
|
|
119
129
|
if ("type" in descriptor &&
|
|
120
130
|
descriptor.type != null &&
|
|
@@ -135,8 +145,9 @@ export function resolveRatifierV1Tree(tree, helpers) {
|
|
|
135
145
|
expectedLength !== nextPowerOfTwo(descriptor.offers.length)) {
|
|
136
146
|
throw new InvalidTreeError("Tree entries, leaves, offers, and height describe different trees.");
|
|
137
147
|
}
|
|
138
|
-
const
|
|
139
|
-
const
|
|
148
|
+
const entries = Array.from({ length: expectedLength }, (_, index) => requireTreeItem(descriptor.entries, index));
|
|
149
|
+
const visibleEntries = entries.slice(0, descriptor.offers.length);
|
|
150
|
+
const ratifier = ratifierOf(requireTreeItem(visibleEntries, 0));
|
|
140
151
|
for (const entry of visibleEntries.slice(1)) {
|
|
141
152
|
const other = ratifierOf(entry);
|
|
142
153
|
if (!isAddressEqual(other, ratifier)) {
|
|
@@ -150,25 +161,24 @@ export function resolveRatifierV1Tree(tree, helpers) {
|
|
|
150
161
|
}
|
|
151
162
|
helpers.validateEntry?.(entry);
|
|
152
163
|
}
|
|
153
|
-
const computedLeaves =
|
|
164
|
+
const computedLeaves = entries.map((entry) => hashLeaf(entry));
|
|
154
165
|
const seen = new Set();
|
|
155
166
|
for (const [index, offer] of descriptor.offers.entries()) {
|
|
156
|
-
const entry =
|
|
167
|
+
const entry = requireTreeItem(entries, index);
|
|
157
168
|
if (OfferUtils.hashStruct(OfferUtils.toStruct({ offer: Offer.from(offer) })).toLowerCase() !== OfferUtils.hashStruct(entry.offer).toLowerCase()) {
|
|
158
169
|
throw new InvalidTreeError("Visible offers do not match the tree entries.");
|
|
159
170
|
}
|
|
160
|
-
const leafHash = computedLeaves
|
|
171
|
+
const leafHash = requireTreeItem(computedLeaves, index).toLowerCase();
|
|
161
172
|
if (seen.has(leafHash)) {
|
|
162
173
|
throw new InvalidTreeError(`Duplicate leaf hash "${leafHash}" in tree.`);
|
|
163
174
|
}
|
|
164
175
|
seen.add(leafHash);
|
|
165
176
|
}
|
|
166
|
-
if (descriptor.
|
|
167
|
-
.slice(descriptor.offers.length)
|
|
168
|
-
.some((entry) => !isPadding(entry))) {
|
|
177
|
+
if (entries.slice(descriptor.offers.length).some((entry) => !isPadding(entry))) {
|
|
169
178
|
throw new InvalidTreeError("Tree padding contains entries hidden from the visible offer list.");
|
|
170
179
|
}
|
|
171
|
-
if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !==
|
|
180
|
+
if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !==
|
|
181
|
+
requireTreeItem(descriptor.leaves, index).toLowerCase())) {
|
|
172
182
|
throw new InvalidTreeError("Tree leaves do not match its entries.");
|
|
173
183
|
}
|
|
174
184
|
if (TreeUtils.buildRootFromLeaves(descriptor.leaves).root.toLowerCase() !==
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** @internal Reads a tree item at a required index, rejecting missing entries. */
|
|
2
|
+
export declare function requireTreeItem<T>(items: readonly T[], index: number): T;
|
|
1
3
|
/** @internal Returns whether `value` is a positive power of two. */
|
|
2
4
|
export declare function isPowerOfTwo(value: number): boolean;
|
|
3
5
|
/** @internal Returns the smallest power of two greater than or equal to `value`. */
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import { InvalidTreeError } from "../errors.js";
|
|
2
|
+
/** @internal Reads a tree item at a required index, rejecting missing entries. */
|
|
3
|
+
export function requireTreeItem(items, index) {
|
|
4
|
+
const item = items[index];
|
|
5
|
+
if (item == null) {
|
|
6
|
+
throw new InvalidTreeError(`Tree item at index "${index}" is missing.`);
|
|
7
|
+
}
|
|
8
|
+
return item;
|
|
9
|
+
}
|
|
1
10
|
/** @internal Returns whether `value` is a positive power of two. */
|
|
2
11
|
export function isPowerOfTwo(value) {
|
|
3
12
|
return value > 0 && (value & (value - 1)) === 0;
|
package/lib/esm/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Installed @morpho-org/midnight-sdk package version. */
|
|
2
|
-
export declare const version = "2.
|
|
2
|
+
export declare const version = "2.8.0-next.0";
|
package/lib/esm/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morpho-org/midnight-sdk",
|
|
3
3
|
"description": "Viem-based package for Morpho Midnight that exports protocol utilities, fetch helpers, and Midnight API utilities under a dedicated API subpath.",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.8.0-next.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -49,12 +49,12 @@
|
|
|
49
49
|
"test": "vitest --root ../.. --project midnight-sdk --project midnight-sdk-fork"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@morpho-org/morpho-ts": "^
|
|
52
|
+
"@morpho-org/morpho-ts": "^4.6.0-0",
|
|
53
53
|
"viem": "^2.0.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@morpho-org/morpho-ts": "^4.
|
|
57
|
-
"@morpho-org/test": "^2.
|
|
56
|
+
"@morpho-org/morpho-ts": "^4.6.0-next.0",
|
|
57
|
+
"@morpho-org/test": "^2.15.0-next.0",
|
|
58
58
|
"fast-check": "^4.9.0",
|
|
59
59
|
"typescript": "7.0.2",
|
|
60
60
|
"viem": "^2.55.10",
|