@morpho-org/midnight-sdk 2.5.0-next.0 → 2.5.1

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.
@@ -1,2 +1,2 @@
1
1
  /** @internal Installed Midnight SDK package version. */
2
- export declare const MIDNIGHT_SDK_VERSION = "2.5.0-next.0";
2
+ export declare const MIDNIGHT_SDK_VERSION = "2.5.1";
@@ -3,4 +3,4 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.MIDNIGHT_SDK_VERSION = void 0;
5
5
  /** @internal Installed Midnight SDK package version. */
6
- exports.MIDNIGHT_SDK_VERSION = "2.5.0-next.0";
6
+ exports.MIDNIGHT_SDK_VERSION = "2.5.1";
@@ -1,4 +1,3 @@
1
- import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError } from "@morpho-org/morpho-ts";
2
1
  import type { Address } from "viem";
3
2
  import type { MempoolPayloadValidationIssue } from "./api/types.js";
4
3
  /**
@@ -71,27 +70,42 @@ export declare class SettlementFeeExceedsPriceError extends Error {
71
70
  /**
72
71
  * Thrown when a local position accrual is requested for an impossible timestamp.
73
72
  *
74
- * @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
73
+ * @example
74
+ * ```ts
75
+ * import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
76
+ *
77
+ * throw new InvalidPositionAccrualTimestampError(1n, 2n);
78
+ * ```
75
79
  */
76
- export declare const InvalidPositionAccrualTimestampError: typeof SharedInvalidPositionAccrualTimestampError;
77
- /** @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major. */
78
- export type InvalidPositionAccrualTimestampError = SharedInvalidPositionAccrualTimestampError;
80
+ export declare class InvalidPositionAccrualTimestampError extends Error {
81
+ constructor(timestamp: bigint, lastAccrual: bigint);
82
+ }
79
83
  /**
80
84
  * Thrown when a local position accrual sees a market loss factor older than the position.
81
85
  *
82
- * @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
86
+ * @example
87
+ * ```ts
88
+ * import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
89
+ *
90
+ * throw new InvalidPositionLossFactorError(1n, 2n);
91
+ * ```
83
92
  */
84
- export declare const InvalidPositionLossFactorError: typeof SharedInvalidPositionLossFactorError;
85
- /** @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major. */
86
- export type InvalidPositionLossFactorError = SharedInvalidPositionLossFactorError;
93
+ export declare class InvalidPositionLossFactorError extends Error {
94
+ constructor(marketLossFactor: bigint, positionLossFactor: bigint);
95
+ }
87
96
  /**
88
97
  * Thrown when local position accrual inputs violate Midnight accounting invariants.
89
98
  *
90
- * @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
99
+ * @example
100
+ * ```ts
101
+ * import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
102
+ *
103
+ * throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
104
+ * ```
91
105
  */
92
- export declare const InvalidPositionAccrualStateError: typeof SharedInvalidPositionAccrualStateError;
93
- /** @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major. */
94
- export type InvalidPositionAccrualStateError = SharedInvalidPositionAccrualStateError;
106
+ export declare class InvalidPositionAccrualStateError extends Error {
107
+ constructor(reason: string);
108
+ }
95
109
  /**
96
110
  * Thrown when a market parameter cannot satisfy Midnight protocol rules.
97
111
  *
@@ -370,6 +384,20 @@ export declare class InvalidTypedDataSignatureError extends Error {
370
384
  readonly signer: Address;
371
385
  constructor(signer: Address, cause?: unknown);
372
386
  }
387
+ /**
388
+ * Thrown when an Ecrecover signature cannot be parsed or recovered.
389
+ *
390
+ * @example
391
+ * ```ts
392
+ * import { InvalidEcrecoverSignatureError } from "@morpho-org/midnight-sdk";
393
+ *
394
+ * const error = new InvalidEcrecoverSignatureError(new TypeError("Invalid recovery input"));
395
+ * console.log(error.cause);
396
+ * ```
397
+ */
398
+ export declare class InvalidEcrecoverSignatureError extends Error {
399
+ constructor(cause: unknown);
400
+ }
373
401
  /**
374
402
  * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value.
375
403
  *
package/lib/cjs/errors.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidMidnightApiQuoteTargetError = exports.InvalidMidnightApiResponseError = exports.MidnightApiError = exports.InvalidEcrecoverSignatureVError = exports.InvalidTypedDataSignatureError = exports.MidnightMempoolValidationError = exports.ChainIdMismatchError = exports.RatifierV1TakerNotAllowedError = exports.InvalidRatifierV1AddressError = exports.InvalidRateRatifierV1TimeError = exports.InvalidRateRatifierV1TickError = exports.RateRatifierV1BoundOverflowError = exports.InvalidRateRatifierV1RateError = exports.InvalidTreeError = exports.InvalidTreeHeightError = exports.PayloadDecodeError = exports.InvalidOfferParameterError = exports.UnknownCollateralIndexError = exports.InvalidMarketParameterError = exports.InvalidPositionAccrualStateError = exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.SettlementFeeExceedsPriceError = exports.InvalidTickSpacingError = exports.PriceGreaterThanOneError = exports.TickOutOfRangeError = exports.InvalidOfferGroupError = void 0;
4
- const morpho_ts_1 = require("@morpho-org/morpho-ts");
3
+ exports.InvalidMidnightApiQuoteTargetError = exports.InvalidMidnightApiResponseError = exports.MidnightApiError = exports.InvalidEcrecoverSignatureVError = exports.InvalidEcrecoverSignatureError = exports.InvalidTypedDataSignatureError = exports.MidnightMempoolValidationError = exports.ChainIdMismatchError = exports.RatifierV1TakerNotAllowedError = exports.InvalidRatifierV1AddressError = exports.InvalidRateRatifierV1TimeError = exports.InvalidRateRatifierV1TickError = exports.RateRatifierV1BoundOverflowError = exports.InvalidRateRatifierV1RateError = exports.InvalidTreeError = exports.InvalidTreeHeightError = exports.PayloadDecodeError = exports.InvalidOfferParameterError = exports.UnknownCollateralIndexError = exports.InvalidMarketParameterError = exports.InvalidPositionAccrualStateError = exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.SettlementFeeExceedsPriceError = exports.InvalidTickSpacingError = exports.PriceGreaterThanOneError = exports.TickOutOfRangeError = exports.InvalidOfferGroupError = void 0;
5
4
  /**
6
5
  * Thrown when a Midnight offer group violates protocol-level group mechanics.
7
6
  *
@@ -90,25 +89,57 @@ class SettlementFeeExceedsPriceError extends Error {
90
89
  }
91
90
  }
92
91
  exports.SettlementFeeExceedsPriceError = SettlementFeeExceedsPriceError;
93
- // Temporary compatibility exception for these shared morpho-ts errors.
94
92
  /**
95
93
  * Thrown when a local position accrual is requested for an impossible timestamp.
96
94
  *
97
- * @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
95
+ * @example
96
+ * ```ts
97
+ * import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
98
+ *
99
+ * throw new InvalidPositionAccrualTimestampError(1n, 2n);
100
+ * ```
98
101
  */
99
- exports.InvalidPositionAccrualTimestampError = morpho_ts_1.InvalidPositionAccrualTimestampError;
102
+ class InvalidPositionAccrualTimestampError extends Error {
103
+ constructor(timestamp, lastAccrual) {
104
+ super(`Position accrual timestamp "${timestamp}" is before last accrual "${lastAccrual}". Use a timestamp greater than or equal to last accrual.`);
105
+ this.name = "InvalidPositionAccrualTimestampError";
106
+ }
107
+ }
108
+ exports.InvalidPositionAccrualTimestampError = InvalidPositionAccrualTimestampError;
100
109
  /**
101
110
  * Thrown when a local position accrual sees a market loss factor older than the position.
102
111
  *
103
- * @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
112
+ * @example
113
+ * ```ts
114
+ * import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
115
+ *
116
+ * throw new InvalidPositionLossFactorError(1n, 2n);
117
+ * ```
104
118
  */
105
- exports.InvalidPositionLossFactorError = morpho_ts_1.InvalidPositionLossFactorError;
119
+ class InvalidPositionLossFactorError extends Error {
120
+ constructor(marketLossFactor, positionLossFactor) {
121
+ super(`Market loss factor "${marketLossFactor}" is below position loss factor "${positionLossFactor}". Fetch a consistent market and position at the same block.`);
122
+ this.name = "InvalidPositionLossFactorError";
123
+ }
124
+ }
125
+ exports.InvalidPositionLossFactorError = InvalidPositionLossFactorError;
106
126
  /**
107
127
  * Thrown when local position accrual inputs violate Midnight accounting invariants.
108
128
  *
109
- * @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
129
+ * @example
130
+ * ```ts
131
+ * import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
132
+ *
133
+ * throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
134
+ * ```
110
135
  */
111
- exports.InvalidPositionAccrualStateError = morpho_ts_1.InvalidPositionAccrualStateError;
136
+ class InvalidPositionAccrualStateError extends Error {
137
+ constructor(reason) {
138
+ super(`Invalid Midnight position accrual state. ${reason}`);
139
+ this.name = "InvalidPositionAccrualStateError";
140
+ }
141
+ }
142
+ exports.InvalidPositionAccrualStateError = InvalidPositionAccrualStateError;
112
143
  /**
113
144
  * Thrown when a market parameter cannot satisfy Midnight protocol rules.
114
145
  *
@@ -452,6 +483,24 @@ class InvalidTypedDataSignatureError extends Error {
452
483
  }
453
484
  }
454
485
  exports.InvalidTypedDataSignatureError = InvalidTypedDataSignatureError;
486
+ /**
487
+ * Thrown when an Ecrecover signature cannot be parsed or recovered.
488
+ *
489
+ * @example
490
+ * ```ts
491
+ * import { InvalidEcrecoverSignatureError } from "@morpho-org/midnight-sdk";
492
+ *
493
+ * const error = new InvalidEcrecoverSignatureError(new TypeError("Invalid recovery input"));
494
+ * console.log(error.cause);
495
+ * ```
496
+ */
497
+ class InvalidEcrecoverSignatureError extends Error {
498
+ constructor(cause) {
499
+ super("Ecrecover signature is malformed. Provide a valid ECDSA signature and try again.", { cause });
500
+ this.name = "InvalidEcrecoverSignatureError";
501
+ }
502
+ }
503
+ exports.InvalidEcrecoverSignatureError = InvalidEcrecoverSignatureError;
455
504
  /**
456
505
  * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value.
457
506
  *
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PositionUtils = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const errors_js_1 = require("../errors.js");
5
6
  /**
6
7
  * Domain helpers for Midnight positions.
7
8
  *
@@ -134,17 +135,40 @@ var PositionUtils;
134
135
  */
135
136
  function accrueInterest(params) {
136
137
  const timestamp = BigInt(params.timestamp);
138
+ const credit = BigInt(params.position.credit);
139
+ const pendingFee = BigInt(params.position.pendingFee);
140
+ const lastLossFactor = BigInt(params.position.lastLossFactor);
141
+ const lastAccrual = BigInt(params.position.lastAccrual);
137
142
  const marketLossFactor = BigInt(params.market.lossFactor);
143
+ if (timestamp < lastAccrual) {
144
+ throw new errors_js_1.InvalidPositionAccrualTimestampError(timestamp, lastAccrual);
145
+ }
146
+ if (marketLossFactor < lastLossFactor) {
147
+ throw new errors_js_1.InvalidPositionLossFactorError(marketLossFactor, lastLossFactor);
148
+ }
149
+ if (pendingFee > credit) {
150
+ throw new errors_js_1.InvalidPositionAccrualStateError("Pending fee must be less than or equal to credit.");
151
+ }
152
+ const postSlashCredit = lastLossFactor < morpho_ts_1.MathLib.MAX_UINT_128
153
+ ? morpho_ts_1.MathLib.mulDivDown(credit, morpho_ts_1.MathLib.MAX_UINT_128 - marketLossFactor, morpho_ts_1.MathLib.MAX_UINT_128 - lastLossFactor)
154
+ : 0n;
155
+ if (postSlashCredit > credit) {
156
+ throw new errors_js_1.InvalidPositionAccrualStateError("Post-slash credit cannot exceed stored credit.");
157
+ }
158
+ const creditDecrease = credit - postSlashCredit;
159
+ const pendingFeeDecrease = credit > 0n ? morpho_ts_1.MathLib.mulDivUp(pendingFee, creditDecrease, credit) : 0n;
160
+ if (pendingFeeDecrease > pendingFee) {
161
+ throw new errors_js_1.InvalidPositionAccrualStateError("Pending-fee decrease cannot exceed pending fee.");
162
+ }
163
+ const postSlashPendingFee = pendingFee - pendingFeeDecrease;
138
164
  const maturity = BigInt(params.market.params.maturity);
139
- const { credit, pendingFee, accruedFee } = morpho_ts_1.MidnightPositionLib.accrue({
140
- credit: params.position.credit,
141
- pendingFee: params.position.pendingFee,
142
- lastLossFactor: params.position.lastLossFactor,
143
- lastAccrual: params.position.lastAccrual,
144
- marketLossFactor,
145
- maturity,
146
- timestamp,
147
- });
165
+ const accrualEnd = timestamp < maturity ? timestamp : maturity;
166
+ const accruedFee = lastAccrual < maturity
167
+ ? morpho_ts_1.MathLib.mulDivDown(postSlashPendingFee, accrualEnd - lastAccrual, maturity - lastAccrual)
168
+ : 0n;
169
+ if (accruedFee > postSlashCredit || accruedFee > postSlashPendingFee) {
170
+ throw new errors_js_1.InvalidPositionAccrualStateError("Accrued fee cannot exceed post-slash credit or pending fee.");
171
+ }
148
172
  const collateral = params.position.collateral.map((assets) => BigInt(assets));
149
173
  const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
150
174
  token: collateralParam.token,
@@ -175,8 +199,8 @@ var PositionUtils;
175
199
  position: {
176
200
  user: params.position.user,
177
201
  marketId: params.position.marketId,
178
- credit,
179
- pendingFee,
202
+ credit: postSlashCredit - accruedFee,
203
+ pendingFee: postSlashPendingFee - accruedFee,
180
204
  lastLossFactor: marketLossFactor,
181
205
  lastAccrual: timestamp,
182
206
  debt: BigInt(params.position.debt),
@@ -610,6 +610,7 @@ export declare namespace EcrecoverRatifierUtils {
610
610
  * @returns Decoded ratifier data plus recovered signer.
611
611
  * @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
612
612
  * @throws {InvalidEcrecoverSignatureVError} when the decoded signature `v` is not 27 or 28.
613
+ * @throws {InvalidEcrecoverSignatureError} when signature recovery fails, preserving the original error as `cause`.
613
614
  * @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
614
615
  * @throws {InvalidTreeHeightError} when the proof height exceeds 20.
615
616
  * @example
@@ -696,6 +697,7 @@ export declare namespace EcrecoverRatifierUtils {
696
697
  *
697
698
  * @param signature - Hex string or tuple signature.
698
699
  * @returns Signature tuple.
700
+ * @throws {InvalidEcrecoverSignatureError} when the serialized signature cannot be parsed, preserving the original error as `cause`.
699
701
  * @example
700
702
  * ```ts
701
703
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -723,6 +725,7 @@ export declare namespace EcrecoverRatifierUtils {
723
725
  * @param params.leafIndex - Leaf index proven by `params.proof`.
724
726
  * @param params.proof - Merkle proof siblings for the leaf.
725
727
  * @returns ABI-encoded ratifier data.
728
+ * @throws {InvalidEcrecoverSignatureError} when signature parsing fails, preserving the original error as `cause`.
726
729
  * @example
727
730
  * ```ts
728
731
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -781,6 +784,7 @@ export declare namespace EcrecoverRatifierUtils {
781
784
  * @param params.signature - Ecrecover signature for the tree root.
782
785
  * @returns ABI-encoded EcrecoverRatifier data.
783
786
  * @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
787
+ * @throws {InvalidEcrecoverSignatureError} when the supplied serialized signature cannot be parsed.
784
788
  * @example
785
789
  * ```ts
786
790
  * import { EcrecoverRatifierUtils, Offer, Tree } from "@morpho-org/midnight-sdk";
@@ -834,7 +838,8 @@ export declare namespace EcrecoverRatifierUtils {
834
838
  * @returns Items containing each offer and its ratifier data.
835
839
  * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
836
840
  * @throws {InvalidTreeHeightError} when the tree height is unsupported.
837
- * @throws {InvalidTypedDataSignatureError} when client signing or a precomputed signature does not recover to `params.account`.
841
+ * @throws {InvalidTypedDataSignatureError} when verification fails after signing or after parsing a precomputed signature, including a signer mismatch.
842
+ * @throws {InvalidEcrecoverSignatureError} when a precomputed serialized signature cannot be parsed.
838
843
  * @example
839
844
  * ```ts
840
845
  * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
@@ -947,6 +952,7 @@ export declare namespace EcrecoverRatifier {
947
952
  * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
948
953
  * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
949
954
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
955
+ * @throws {InvalidEcrecoverSignatureError} When a supplied serialized signature cannot be parsed.
950
956
  * @example
951
957
  * ```ts
952
958
  * import { EcrecoverRatifier, type EcrecoverRatifierDataRequest } from "@morpho-org/midnight-sdk";
@@ -968,6 +974,7 @@ export declare namespace EcrecoverRatifier {
968
974
  * @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
969
975
  * @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
970
976
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
977
+ * @throws {InvalidEcrecoverSignatureError} When a precomputed serialized signature cannot be parsed.
971
978
  * @example
972
979
  * ```ts
973
980
  * import { EcrecoverRatifier, type EcrecoverRatifierRatifyRequest } from "@morpho-org/midnight-sdk";
@@ -388,6 +388,7 @@ var EcrecoverRatifierUtils;
388
388
  * @returns Decoded ratifier data plus recovered signer.
389
389
  * @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
390
390
  * @throws {InvalidEcrecoverSignatureVError} when the decoded signature `v` is not 27 or 28.
391
+ * @throws {InvalidEcrecoverSignatureError} when signature recovery fails, preserving the original error as `cause`.
391
392
  * @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
392
393
  * @throws {InvalidTreeHeightError} when the proof height exceeds 20.
393
394
  * @example
@@ -413,19 +414,27 @@ var EcrecoverRatifierUtils;
413
414
  })) {
414
415
  throw new errors_js_1.InvalidTreeError("Ratifier data proof does not include offer.");
415
416
  }
416
- const signer = await (0, viem_1.recoverAddress)({
417
- hash: digestForRoot({
418
- chainId: params.chainId,
419
- offer,
420
- root: decoded.root,
421
- proofLength: decoded.proof.length,
422
- }),
423
- signature: (0, viem_1.serializeSignature)({
424
- r: decoded.signature.r,
425
- s: decoded.signature.s,
426
- yParity: toCanonicalYParity(decoded.signature.v),
427
- }),
417
+ const hash = digestForRoot({
418
+ chainId: params.chainId,
419
+ offer,
420
+ root: decoded.root,
421
+ proofLength: decoded.proof.length,
428
422
  });
423
+ const yParity = toCanonicalYParity(decoded.signature.v);
424
+ let signer;
425
+ try {
426
+ signer = await (0, viem_1.recoverAddress)({
427
+ hash,
428
+ signature: (0, viem_1.serializeSignature)({
429
+ r: decoded.signature.r,
430
+ s: decoded.signature.s,
431
+ yParity,
432
+ }),
433
+ });
434
+ }
435
+ catch (cause) {
436
+ throw new errors_js_1.InvalidEcrecoverSignatureError(cause);
437
+ }
429
438
  return (0, morpho_ts_1.deepFreeze)({
430
439
  ...decoded,
431
440
  signer,
@@ -542,6 +551,7 @@ var EcrecoverRatifierUtils;
542
551
  *
543
552
  * @param signature - Hex string or tuple signature.
544
553
  * @returns Signature tuple.
554
+ * @throws {InvalidEcrecoverSignatureError} when the serialized signature cannot be parsed, preserving the original error as `cause`.
545
555
  * @example
546
556
  * ```ts
547
557
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -564,7 +574,13 @@ var EcrecoverRatifierUtils;
564
574
  s: signature.s,
565
575
  };
566
576
  }
567
- const parsed = (0, viem_1.parseSignature)(signature);
577
+ let parsed;
578
+ try {
579
+ parsed = (0, viem_1.parseSignature)(signature);
580
+ }
581
+ catch (cause) {
582
+ throw new errors_js_1.InvalidEcrecoverSignatureError(cause);
583
+ }
568
584
  const v = Number(parsed.v ?? BigInt(parsed.yParity));
569
585
  return {
570
586
  v: v < 27 ? v + 27 : v,
@@ -584,6 +600,7 @@ var EcrecoverRatifierUtils;
584
600
  * @param params.leafIndex - Leaf index proven by `params.proof`.
585
601
  * @param params.proof - Merkle proof siblings for the leaf.
586
602
  * @returns ABI-encoded ratifier data.
603
+ * @throws {InvalidEcrecoverSignatureError} when signature parsing fails, preserving the original error as `cause`.
587
604
  * @example
588
605
  * ```ts
589
606
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -663,6 +680,7 @@ var EcrecoverRatifierUtils;
663
680
  * @param params.signature - Ecrecover signature for the tree root.
664
681
  * @returns ABI-encoded EcrecoverRatifier data.
665
682
  * @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
683
+ * @throws {InvalidEcrecoverSignatureError} when the supplied serialized signature cannot be parsed.
666
684
  * @example
667
685
  * ```ts
668
686
  * import { EcrecoverRatifierUtils, Offer, Tree } from "@morpho-org/midnight-sdk";
@@ -729,7 +747,8 @@ var EcrecoverRatifierUtils;
729
747
  * @returns Items containing each offer and its ratifier data.
730
748
  * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
731
749
  * @throws {InvalidTreeHeightError} when the tree height is unsupported.
732
- * @throws {InvalidTypedDataSignatureError} when client signing or a precomputed signature does not recover to `params.account`.
750
+ * @throws {InvalidTypedDataSignatureError} when verification fails after signing or after parsing a precomputed signature, including a signer mismatch.
751
+ * @throws {InvalidEcrecoverSignatureError} when a precomputed serialized signature cannot be parsed.
733
752
  * @example
734
753
  * ```ts
735
754
  * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
@@ -911,6 +930,7 @@ var EcrecoverRatifier;
911
930
  * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
912
931
  * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
913
932
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
933
+ * @throws {InvalidEcrecoverSignatureError} When a supplied serialized signature cannot be parsed.
914
934
  * @example
915
935
  * ```ts
916
936
  * import { EcrecoverRatifier, type EcrecoverRatifierDataRequest } from "@morpho-org/midnight-sdk";
@@ -936,6 +956,7 @@ var EcrecoverRatifier;
936
956
  * @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
937
957
  * @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
938
958
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
959
+ * @throws {InvalidEcrecoverSignatureError} When a precomputed serialized signature cannot be parsed.
939
960
  * @example
940
961
  * ```ts
941
962
  * import { EcrecoverRatifier, type EcrecoverRatifierRatifyRequest } from "@morpho-org/midnight-sdk";
@@ -1,2 +1,2 @@
1
1
  /** @internal Installed Midnight SDK package version. */
2
- export declare const MIDNIGHT_SDK_VERSION = "2.5.0-next.0";
2
+ export declare const MIDNIGHT_SDK_VERSION = "2.5.1";
@@ -1,3 +1,3 @@
1
1
  // Generated by scripts/generate-midnight-package-version.ts. Do not edit.
2
2
  /** @internal Installed Midnight SDK package version. */
3
- export const MIDNIGHT_SDK_VERSION = "2.5.0-next.0";
3
+ export const MIDNIGHT_SDK_VERSION = "2.5.1";
@@ -1,4 +1,3 @@
1
- import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError } from "@morpho-org/morpho-ts";
2
1
  import type { Address } from "viem";
3
2
  import type { MempoolPayloadValidationIssue } from "./api/types.js";
4
3
  /**
@@ -71,27 +70,42 @@ export declare class SettlementFeeExceedsPriceError extends Error {
71
70
  /**
72
71
  * Thrown when a local position accrual is requested for an impossible timestamp.
73
72
  *
74
- * @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
73
+ * @example
74
+ * ```ts
75
+ * import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
76
+ *
77
+ * throw new InvalidPositionAccrualTimestampError(1n, 2n);
78
+ * ```
75
79
  */
76
- export declare const InvalidPositionAccrualTimestampError: typeof SharedInvalidPositionAccrualTimestampError;
77
- /** @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major. */
78
- export type InvalidPositionAccrualTimestampError = SharedInvalidPositionAccrualTimestampError;
80
+ export declare class InvalidPositionAccrualTimestampError extends Error {
81
+ constructor(timestamp: bigint, lastAccrual: bigint);
82
+ }
79
83
  /**
80
84
  * Thrown when a local position accrual sees a market loss factor older than the position.
81
85
  *
82
- * @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
86
+ * @example
87
+ * ```ts
88
+ * import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
89
+ *
90
+ * throw new InvalidPositionLossFactorError(1n, 2n);
91
+ * ```
83
92
  */
84
- export declare const InvalidPositionLossFactorError: typeof SharedInvalidPositionLossFactorError;
85
- /** @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major. */
86
- export type InvalidPositionLossFactorError = SharedInvalidPositionLossFactorError;
93
+ export declare class InvalidPositionLossFactorError extends Error {
94
+ constructor(marketLossFactor: bigint, positionLossFactor: bigint);
95
+ }
87
96
  /**
88
97
  * Thrown when local position accrual inputs violate Midnight accounting invariants.
89
98
  *
90
- * @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
99
+ * @example
100
+ * ```ts
101
+ * import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
102
+ *
103
+ * throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
104
+ * ```
91
105
  */
92
- export declare const InvalidPositionAccrualStateError: typeof SharedInvalidPositionAccrualStateError;
93
- /** @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major. */
94
- export type InvalidPositionAccrualStateError = SharedInvalidPositionAccrualStateError;
106
+ export declare class InvalidPositionAccrualStateError extends Error {
107
+ constructor(reason: string);
108
+ }
95
109
  /**
96
110
  * Thrown when a market parameter cannot satisfy Midnight protocol rules.
97
111
  *
@@ -370,6 +384,20 @@ export declare class InvalidTypedDataSignatureError extends Error {
370
384
  readonly signer: Address;
371
385
  constructor(signer: Address, cause?: unknown);
372
386
  }
387
+ /**
388
+ * Thrown when an Ecrecover signature cannot be parsed or recovered.
389
+ *
390
+ * @example
391
+ * ```ts
392
+ * import { InvalidEcrecoverSignatureError } from "@morpho-org/midnight-sdk";
393
+ *
394
+ * const error = new InvalidEcrecoverSignatureError(new TypeError("Invalid recovery input"));
395
+ * console.log(error.cause);
396
+ * ```
397
+ */
398
+ export declare class InvalidEcrecoverSignatureError extends Error {
399
+ constructor(cause: unknown);
400
+ }
373
401
  /**
374
402
  * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value.
375
403
  *
package/lib/esm/errors.js CHANGED
@@ -1,4 +1,3 @@
1
- import { InvalidPositionAccrualStateError as SharedInvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError as SharedInvalidPositionAccrualTimestampError, InvalidPositionLossFactorError as SharedInvalidPositionLossFactorError, } from "@morpho-org/morpho-ts";
2
1
  /**
3
2
  * Thrown when a Midnight offer group violates protocol-level group mechanics.
4
3
  *
@@ -82,25 +81,54 @@ export class SettlementFeeExceedsPriceError extends Error {
82
81
  this.name = "SettlementFeeExceedsPriceError";
83
82
  }
84
83
  }
85
- // Temporary compatibility exception for these shared morpho-ts errors.
86
84
  /**
87
85
  * Thrown when a local position accrual is requested for an impossible timestamp.
88
86
  *
89
- * @deprecated Import `InvalidPositionAccrualTimestampError` from `@morpho-org/morpho-ts`. Removed in the next major.
87
+ * @example
88
+ * ```ts
89
+ * import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk";
90
+ *
91
+ * throw new InvalidPositionAccrualTimestampError(1n, 2n);
92
+ * ```
90
93
  */
91
- export const InvalidPositionAccrualTimestampError = SharedInvalidPositionAccrualTimestampError;
94
+ export class InvalidPositionAccrualTimestampError extends Error {
95
+ constructor(timestamp, lastAccrual) {
96
+ super(`Position accrual timestamp "${timestamp}" is before last accrual "${lastAccrual}". Use a timestamp greater than or equal to last accrual.`);
97
+ this.name = "InvalidPositionAccrualTimestampError";
98
+ }
99
+ }
92
100
  /**
93
101
  * Thrown when a local position accrual sees a market loss factor older than the position.
94
102
  *
95
- * @deprecated Import `InvalidPositionLossFactorError` from `@morpho-org/morpho-ts`. Removed in the next major.
103
+ * @example
104
+ * ```ts
105
+ * import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk";
106
+ *
107
+ * throw new InvalidPositionLossFactorError(1n, 2n);
108
+ * ```
96
109
  */
97
- export const InvalidPositionLossFactorError = SharedInvalidPositionLossFactorError;
110
+ export class InvalidPositionLossFactorError extends Error {
111
+ constructor(marketLossFactor, positionLossFactor) {
112
+ super(`Market loss factor "${marketLossFactor}" is below position loss factor "${positionLossFactor}". Fetch a consistent market and position at the same block.`);
113
+ this.name = "InvalidPositionLossFactorError";
114
+ }
115
+ }
98
116
  /**
99
117
  * Thrown when local position accrual inputs violate Midnight accounting invariants.
100
118
  *
101
- * @deprecated Import `InvalidPositionAccrualStateError` from `@morpho-org/morpho-ts`. Removed in the next major.
119
+ * @example
120
+ * ```ts
121
+ * import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk";
122
+ *
123
+ * throw new InvalidPositionAccrualStateError("Pending fee exceeds credit.");
124
+ * ```
102
125
  */
103
- export const InvalidPositionAccrualStateError = SharedInvalidPositionAccrualStateError;
126
+ export class InvalidPositionAccrualStateError extends Error {
127
+ constructor(reason) {
128
+ super(`Invalid Midnight position accrual state. ${reason}`);
129
+ this.name = "InvalidPositionAccrualStateError";
130
+ }
131
+ }
104
132
  /**
105
133
  * Thrown when a market parameter cannot satisfy Midnight protocol rules.
106
134
  *
@@ -429,6 +457,23 @@ export class InvalidTypedDataSignatureError extends Error {
429
457
  this.signer = signer;
430
458
  }
431
459
  }
460
+ /**
461
+ * Thrown when an Ecrecover signature cannot be parsed or recovered.
462
+ *
463
+ * @example
464
+ * ```ts
465
+ * import { InvalidEcrecoverSignatureError } from "@morpho-org/midnight-sdk";
466
+ *
467
+ * const error = new InvalidEcrecoverSignatureError(new TypeError("Invalid recovery input"));
468
+ * console.log(error.cause);
469
+ * ```
470
+ */
471
+ export class InvalidEcrecoverSignatureError extends Error {
472
+ constructor(cause) {
473
+ super("Ecrecover signature is malformed. Provide a valid ECDSA signature and try again.", { cause });
474
+ this.name = "InvalidEcrecoverSignatureError";
475
+ }
476
+ }
432
477
  /**
433
478
  * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value.
434
479
  *
@@ -1,4 +1,5 @@
1
- import { MathLib, MidnightPositionLib, } from "@morpho-org/morpho-ts";
1
+ import { MathLib } from "@morpho-org/morpho-ts";
2
+ import { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionLossFactorError, } from "../errors.js";
2
3
  /**
3
4
  * Domain helpers for Midnight positions.
4
5
  *
@@ -131,17 +132,40 @@ export var PositionUtils;
131
132
  */
132
133
  function accrueInterest(params) {
133
134
  const timestamp = BigInt(params.timestamp);
135
+ const credit = BigInt(params.position.credit);
136
+ const pendingFee = BigInt(params.position.pendingFee);
137
+ const lastLossFactor = BigInt(params.position.lastLossFactor);
138
+ const lastAccrual = BigInt(params.position.lastAccrual);
134
139
  const marketLossFactor = BigInt(params.market.lossFactor);
140
+ if (timestamp < lastAccrual) {
141
+ throw new InvalidPositionAccrualTimestampError(timestamp, lastAccrual);
142
+ }
143
+ if (marketLossFactor < lastLossFactor) {
144
+ throw new InvalidPositionLossFactorError(marketLossFactor, lastLossFactor);
145
+ }
146
+ if (pendingFee > credit) {
147
+ throw new InvalidPositionAccrualStateError("Pending fee must be less than or equal to credit.");
148
+ }
149
+ const postSlashCredit = lastLossFactor < MathLib.MAX_UINT_128
150
+ ? MathLib.mulDivDown(credit, MathLib.MAX_UINT_128 - marketLossFactor, MathLib.MAX_UINT_128 - lastLossFactor)
151
+ : 0n;
152
+ if (postSlashCredit > credit) {
153
+ throw new InvalidPositionAccrualStateError("Post-slash credit cannot exceed stored credit.");
154
+ }
155
+ const creditDecrease = credit - postSlashCredit;
156
+ const pendingFeeDecrease = credit > 0n ? MathLib.mulDivUp(pendingFee, creditDecrease, credit) : 0n;
157
+ if (pendingFeeDecrease > pendingFee) {
158
+ throw new InvalidPositionAccrualStateError("Pending-fee decrease cannot exceed pending fee.");
159
+ }
160
+ const postSlashPendingFee = pendingFee - pendingFeeDecrease;
135
161
  const maturity = BigInt(params.market.params.maturity);
136
- const { credit, pendingFee, accruedFee } = MidnightPositionLib.accrue({
137
- credit: params.position.credit,
138
- pendingFee: params.position.pendingFee,
139
- lastLossFactor: params.position.lastLossFactor,
140
- lastAccrual: params.position.lastAccrual,
141
- marketLossFactor,
142
- maturity,
143
- timestamp,
144
- });
162
+ const accrualEnd = timestamp < maturity ? timestamp : maturity;
163
+ const accruedFee = lastAccrual < maturity
164
+ ? MathLib.mulDivDown(postSlashPendingFee, accrualEnd - lastAccrual, maturity - lastAccrual)
165
+ : 0n;
166
+ if (accruedFee > postSlashCredit || accruedFee > postSlashPendingFee) {
167
+ throw new InvalidPositionAccrualStateError("Accrued fee cannot exceed post-slash credit or pending fee.");
168
+ }
145
169
  const collateral = params.position.collateral.map((assets) => BigInt(assets));
146
170
  const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
147
171
  token: collateralParam.token,
@@ -172,8 +196,8 @@ export var PositionUtils;
172
196
  position: {
173
197
  user: params.position.user,
174
198
  marketId: params.position.marketId,
175
- credit,
176
- pendingFee,
199
+ credit: postSlashCredit - accruedFee,
200
+ pendingFee: postSlashPendingFee - accruedFee,
177
201
  lastLossFactor: marketLossFactor,
178
202
  lastAccrual: timestamp,
179
203
  debt: BigInt(params.position.debt),
@@ -610,6 +610,7 @@ export declare namespace EcrecoverRatifierUtils {
610
610
  * @returns Decoded ratifier data plus recovered signer.
611
611
  * @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
612
612
  * @throws {InvalidEcrecoverSignatureVError} when the decoded signature `v` is not 27 or 28.
613
+ * @throws {InvalidEcrecoverSignatureError} when signature recovery fails, preserving the original error as `cause`.
613
614
  * @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
614
615
  * @throws {InvalidTreeHeightError} when the proof height exceeds 20.
615
616
  * @example
@@ -696,6 +697,7 @@ export declare namespace EcrecoverRatifierUtils {
696
697
  *
697
698
  * @param signature - Hex string or tuple signature.
698
699
  * @returns Signature tuple.
700
+ * @throws {InvalidEcrecoverSignatureError} when the serialized signature cannot be parsed, preserving the original error as `cause`.
699
701
  * @example
700
702
  * ```ts
701
703
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -723,6 +725,7 @@ export declare namespace EcrecoverRatifierUtils {
723
725
  * @param params.leafIndex - Leaf index proven by `params.proof`.
724
726
  * @param params.proof - Merkle proof siblings for the leaf.
725
727
  * @returns ABI-encoded ratifier data.
728
+ * @throws {InvalidEcrecoverSignatureError} when signature parsing fails, preserving the original error as `cause`.
726
729
  * @example
727
730
  * ```ts
728
731
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -781,6 +784,7 @@ export declare namespace EcrecoverRatifierUtils {
781
784
  * @param params.signature - Ecrecover signature for the tree root.
782
785
  * @returns ABI-encoded EcrecoverRatifier data.
783
786
  * @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
787
+ * @throws {InvalidEcrecoverSignatureError} when the supplied serialized signature cannot be parsed.
784
788
  * @example
785
789
  * ```ts
786
790
  * import { EcrecoverRatifierUtils, Offer, Tree } from "@morpho-org/midnight-sdk";
@@ -834,7 +838,8 @@ export declare namespace EcrecoverRatifierUtils {
834
838
  * @returns Items containing each offer and its ratifier data.
835
839
  * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
836
840
  * @throws {InvalidTreeHeightError} when the tree height is unsupported.
837
- * @throws {InvalidTypedDataSignatureError} when client signing or a precomputed signature does not recover to `params.account`.
841
+ * @throws {InvalidTypedDataSignatureError} when verification fails after signing or after parsing a precomputed signature, including a signer mismatch.
842
+ * @throws {InvalidEcrecoverSignatureError} when a precomputed serialized signature cannot be parsed.
838
843
  * @example
839
844
  * ```ts
840
845
  * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
@@ -947,6 +952,7 @@ export declare namespace EcrecoverRatifier {
947
952
  * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
948
953
  * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
949
954
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
955
+ * @throws {InvalidEcrecoverSignatureError} When a supplied serialized signature cannot be parsed.
950
956
  * @example
951
957
  * ```ts
952
958
  * import { EcrecoverRatifier, type EcrecoverRatifierDataRequest } from "@morpho-org/midnight-sdk";
@@ -968,6 +974,7 @@ export declare namespace EcrecoverRatifier {
968
974
  * @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
969
975
  * @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
970
976
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
977
+ * @throws {InvalidEcrecoverSignatureError} When a precomputed serialized signature cannot be parsed.
971
978
  * @example
972
979
  * ```ts
973
980
  * import { EcrecoverRatifier, type EcrecoverRatifierRatifyRequest } from "@morpho-org/midnight-sdk";
@@ -2,7 +2,7 @@ import { deepFreeze } from "@morpho-org/morpho-ts";
2
2
  import { decodeAbiParameters, encodeAbiParameters, keccak256, parseSignature, recoverAddress, serializeSignature, verifyHash, } from "viem";
3
3
  import { signTypedData } from "viem/actions";
4
4
  import { EIP712_DOMAIN_TYPEHASH } from "../constants.js";
5
- import { ChainIdMismatchError, InvalidEcrecoverSignatureVError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, } from "../errors.js";
5
+ import { ChainIdMismatchError, InvalidEcrecoverSignatureError, InvalidEcrecoverSignatureVError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, } from "../errors.js";
6
6
  import { MarketParams } from "../market/index.js";
7
7
  import { Offer } from "../offers/index.js";
8
8
  import { eip712Digest } from "./eip712.js";
@@ -385,6 +385,7 @@ export var EcrecoverRatifierUtils;
385
385
  * @returns Decoded ratifier data plus recovered signer.
386
386
  * @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
387
387
  * @throws {InvalidEcrecoverSignatureVError} when the decoded signature `v` is not 27 or 28.
388
+ * @throws {InvalidEcrecoverSignatureError} when signature recovery fails, preserving the original error as `cause`.
388
389
  * @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
389
390
  * @throws {InvalidTreeHeightError} when the proof height exceeds 20.
390
391
  * @example
@@ -410,19 +411,27 @@ export var EcrecoverRatifierUtils;
410
411
  })) {
411
412
  throw new InvalidTreeError("Ratifier data proof does not include offer.");
412
413
  }
413
- const signer = await recoverAddress({
414
- hash: digestForRoot({
415
- chainId: params.chainId,
416
- offer,
417
- root: decoded.root,
418
- proofLength: decoded.proof.length,
419
- }),
420
- signature: serializeSignature({
421
- r: decoded.signature.r,
422
- s: decoded.signature.s,
423
- yParity: toCanonicalYParity(decoded.signature.v),
424
- }),
414
+ const hash = digestForRoot({
415
+ chainId: params.chainId,
416
+ offer,
417
+ root: decoded.root,
418
+ proofLength: decoded.proof.length,
425
419
  });
420
+ const yParity = toCanonicalYParity(decoded.signature.v);
421
+ let signer;
422
+ try {
423
+ signer = await recoverAddress({
424
+ hash,
425
+ signature: serializeSignature({
426
+ r: decoded.signature.r,
427
+ s: decoded.signature.s,
428
+ yParity,
429
+ }),
430
+ });
431
+ }
432
+ catch (cause) {
433
+ throw new InvalidEcrecoverSignatureError(cause);
434
+ }
426
435
  return deepFreeze({
427
436
  ...decoded,
428
437
  signer,
@@ -539,6 +548,7 @@ export var EcrecoverRatifierUtils;
539
548
  *
540
549
  * @param signature - Hex string or tuple signature.
541
550
  * @returns Signature tuple.
551
+ * @throws {InvalidEcrecoverSignatureError} when the serialized signature cannot be parsed, preserving the original error as `cause`.
542
552
  * @example
543
553
  * ```ts
544
554
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -561,7 +571,13 @@ export var EcrecoverRatifierUtils;
561
571
  s: signature.s,
562
572
  };
563
573
  }
564
- const parsed = parseSignature(signature);
574
+ let parsed;
575
+ try {
576
+ parsed = parseSignature(signature);
577
+ }
578
+ catch (cause) {
579
+ throw new InvalidEcrecoverSignatureError(cause);
580
+ }
565
581
  const v = Number(parsed.v ?? BigInt(parsed.yParity));
566
582
  return {
567
583
  v: v < 27 ? v + 27 : v,
@@ -581,6 +597,7 @@ export var EcrecoverRatifierUtils;
581
597
  * @param params.leafIndex - Leaf index proven by `params.proof`.
582
598
  * @param params.proof - Merkle proof siblings for the leaf.
583
599
  * @returns ABI-encoded ratifier data.
600
+ * @throws {InvalidEcrecoverSignatureError} when signature parsing fails, preserving the original error as `cause`.
584
601
  * @example
585
602
  * ```ts
586
603
  * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
@@ -660,6 +677,7 @@ export var EcrecoverRatifierUtils;
660
677
  * @param params.signature - Ecrecover signature for the tree root.
661
678
  * @returns ABI-encoded EcrecoverRatifier data.
662
679
  * @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
680
+ * @throws {InvalidEcrecoverSignatureError} when the supplied serialized signature cannot be parsed.
663
681
  * @example
664
682
  * ```ts
665
683
  * import { EcrecoverRatifierUtils, Offer, Tree } from "@morpho-org/midnight-sdk";
@@ -726,7 +744,8 @@ export var EcrecoverRatifierUtils;
726
744
  * @returns Items containing each offer and its ratifier data.
727
745
  * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
728
746
  * @throws {InvalidTreeHeightError} when the tree height is unsupported.
729
- * @throws {InvalidTypedDataSignatureError} when client signing or a precomputed signature does not recover to `params.account`.
747
+ * @throws {InvalidTypedDataSignatureError} when verification fails after signing or after parsing a precomputed signature, including a signer mismatch.
748
+ * @throws {InvalidEcrecoverSignatureError} when a precomputed serialized signature cannot be parsed.
730
749
  * @example
731
750
  * ```ts
732
751
  * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
@@ -908,6 +927,7 @@ export var EcrecoverRatifier;
908
927
  * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
909
928
  * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
910
929
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
930
+ * @throws {InvalidEcrecoverSignatureError} When a supplied serialized signature cannot be parsed.
911
931
  * @example
912
932
  * ```ts
913
933
  * import { EcrecoverRatifier, type EcrecoverRatifierDataRequest } from "@morpho-org/midnight-sdk";
@@ -933,6 +953,7 @@ export var EcrecoverRatifier;
933
953
  * @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
934
954
  * @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
935
955
  * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
956
+ * @throws {InvalidEcrecoverSignatureError} When a precomputed serialized signature cannot be parsed.
936
957
  * @example
937
958
  * ```ts
938
959
  * import { EcrecoverRatifier, type EcrecoverRatifierRatifyRequest } from "@morpho-org/midnight-sdk";
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.5.0-next.0",
4
+ "version": "2.5.1",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "repository": {
7
7
  "type": "git",
@@ -41,12 +41,12 @@
41
41
  "MIGRATION-v1-to-v2.md"
42
42
  ],
43
43
  "peerDependencies": {
44
- "@morpho-org/morpho-ts": "^4.3.0-0",
44
+ "@morpho-org/morpho-ts": "^2.8.0 || ^3.0.0 || ^4.0.0",
45
45
  "viem": "^2.0.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@morpho-org/morpho-ts": "^4.3.0-next.0",
49
- "@morpho-org/test": "^2.13.0-next.1",
48
+ "@morpho-org/morpho-ts": "^4.3.1",
49
+ "@morpho-org/test": "^2.12.1",
50
50
  "fast-check": "^4.9.0",
51
51
  "typescript": "7.0.2",
52
52
  "viem": "^2.55.10",