@morpho-org/midnight-sdk 1.7.0-next.0 → 1.8.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 (92) hide show
  1. package/README.md +61 -61
  2. package/lib/cjs/abis.d.ts +794 -0
  3. package/lib/cjs/abis.js +502 -1
  4. package/lib/cjs/api/version.generated.d.ts +1 -1
  5. package/lib/cjs/api/version.generated.js +2 -2
  6. package/lib/cjs/constants.d.ts +34 -0
  7. package/lib/cjs/constants.js +35 -1
  8. package/lib/cjs/errors.d.ts +107 -0
  9. package/lib/cjs/errors.js +139 -1
  10. package/lib/cjs/fetch/Ratifier.d.ts +4 -3
  11. package/lib/cjs/fetch/Ratifier.js +5 -4
  12. package/lib/cjs/index.d.ts +7 -0
  13. package/lib/cjs/index.js +16 -1
  14. package/lib/cjs/offers/Offer.d.ts +10 -0
  15. package/lib/cjs/offers/Offer.js +15 -1
  16. package/lib/cjs/offers/OfferUtils.d.ts +3 -0
  17. package/lib/cjs/offers/OfferUtils.js +3 -0
  18. package/lib/cjs/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  19. package/lib/cjs/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  20. package/lib/cjs/signatures/GroupUtils.d.ts +55 -2
  21. package/lib/cjs/signatures/GroupUtils.js +64 -5
  22. package/lib/cjs/signatures/Payload.d.ts +3 -3
  23. package/lib/cjs/signatures/PriceRatifierV1.d.ts +472 -0
  24. package/lib/cjs/signatures/PriceRatifierV1.js +521 -0
  25. package/lib/cjs/signatures/RateRatifierV1.d.ts +581 -0
  26. package/lib/cjs/signatures/RateRatifierV1.js +696 -0
  27. package/lib/{esm/signatures/RatifierUtils.d.ts → cjs/signatures/Ratifier.d.ts} +14 -12
  28. package/lib/cjs/signatures/{RatifierUtils.js → Ratifier.js} +29 -29
  29. package/lib/cjs/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  30. package/lib/cjs/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  31. package/lib/cjs/signatures/Tree.d.ts +94 -17
  32. package/lib/cjs/signatures/Tree.js +146 -111
  33. package/lib/cjs/signatures/TreeUtils.d.ts +191 -21
  34. package/lib/cjs/signatures/TreeUtils.js +320 -134
  35. package/lib/cjs/signatures/index.d.ts +7 -3
  36. package/lib/cjs/signatures/index.js +5 -3
  37. package/lib/cjs/signatures/ratifierRequests.d.ts +55 -0
  38. package/lib/cjs/signatures/ratifierRequests.js +2 -0
  39. package/lib/cjs/signatures/ratifierV1Internal.d.ts +57 -0
  40. package/lib/cjs/signatures/ratifierV1Internal.js +183 -0
  41. package/lib/cjs/signatures/standardRatifierInternal.d.ts +4 -0
  42. package/lib/cjs/signatures/standardRatifierInternal.js +12 -0
  43. package/lib/cjs/signatures/treeMathInternal.d.ts +4 -0
  44. package/lib/cjs/signatures/treeMathInternal.js +12 -0
  45. package/lib/cjs/signatures/treeTypes.d.ts +109 -0
  46. package/lib/cjs/signatures/treeTypes.js +2 -0
  47. package/lib/esm/abis.d.ts +794 -0
  48. package/lib/esm/abis.js +501 -0
  49. package/lib/esm/api/version.generated.d.ts +1 -1
  50. package/lib/esm/api/version.generated.js +2 -2
  51. package/lib/esm/constants.d.ts +34 -0
  52. package/lib/esm/constants.js +34 -0
  53. package/lib/esm/errors.d.ts +107 -0
  54. package/lib/esm/errors.js +132 -0
  55. package/lib/esm/fetch/Ratifier.d.ts +4 -3
  56. package/lib/esm/fetch/Ratifier.js +5 -4
  57. package/lib/esm/index.d.ts +7 -0
  58. package/lib/esm/index.js +5 -0
  59. package/lib/esm/offers/Offer.d.ts +10 -0
  60. package/lib/esm/offers/Offer.js +15 -1
  61. package/lib/esm/offers/OfferUtils.d.ts +3 -0
  62. package/lib/esm/offers/OfferUtils.js +3 -0
  63. package/lib/esm/signatures/{EcrecoverRatifierUtils.d.ts → EcrecoverRatifier.d.ts} +118 -2
  64. package/lib/esm/signatures/{EcrecoverRatifierUtils.js → EcrecoverRatifier.js} +146 -9
  65. package/lib/esm/signatures/GroupUtils.d.ts +55 -2
  66. package/lib/esm/signatures/GroupUtils.js +64 -5
  67. package/lib/esm/signatures/Payload.d.ts +3 -3
  68. package/lib/esm/signatures/PriceRatifierV1.d.ts +472 -0
  69. package/lib/esm/signatures/PriceRatifierV1.js +520 -0
  70. package/lib/esm/signatures/RateRatifierV1.d.ts +581 -0
  71. package/lib/esm/signatures/RateRatifierV1.js +695 -0
  72. package/lib/{cjs/signatures/RatifierUtils.d.ts → esm/signatures/Ratifier.d.ts} +14 -12
  73. package/lib/esm/signatures/{RatifierUtils.js → Ratifier.js} +31 -29
  74. package/lib/esm/signatures/{SetterRatifierUtils.d.ts → SetterRatifier.d.ts} +50 -3
  75. package/lib/esm/signatures/{SetterRatifierUtils.js → SetterRatifier.js} +65 -7
  76. package/lib/esm/signatures/Tree.d.ts +94 -17
  77. package/lib/esm/signatures/Tree.js +147 -112
  78. package/lib/esm/signatures/TreeUtils.d.ts +191 -21
  79. package/lib/esm/signatures/TreeUtils.js +318 -132
  80. package/lib/esm/signatures/index.d.ts +7 -3
  81. package/lib/esm/signatures/index.js +5 -3
  82. package/lib/esm/signatures/ratifierRequests.d.ts +55 -0
  83. package/lib/esm/signatures/ratifierRequests.js +1 -0
  84. package/lib/esm/signatures/ratifierV1Internal.d.ts +57 -0
  85. package/lib/esm/signatures/ratifierV1Internal.js +176 -0
  86. package/lib/esm/signatures/standardRatifierInternal.d.ts +4 -0
  87. package/lib/esm/signatures/standardRatifierInternal.js +9 -0
  88. package/lib/esm/signatures/treeMathInternal.d.ts +4 -0
  89. package/lib/esm/signatures/treeMathInternal.js +8 -0
  90. package/lib/esm/signatures/treeTypes.d.ts +109 -0
  91. package/lib/esm/signatures/treeTypes.js +1 -0
  92. package/package.json +4 -4
@@ -6,8 +6,9 @@ import { ChainIdMismatchError, InvalidEcrecoverSignatureVError, InvalidTreeError
6
6
  import { MarketParams } from "../market/index.js";
7
7
  import { Offer } from "../offers/index.js";
8
8
  import { eip712Digest } from "./eip712.js";
9
- import { RatifierUtils } from "./RatifierUtils.js";
10
- import { TreeUtils, } from "./TreeUtils.js";
9
+ import { Ratifier } from "./Ratifier.js";
10
+ import { parseStandardRatifierTree } from "./standardRatifierInternal.js";
11
+ import { TreeUtils } from "./TreeUtils.js";
11
12
  const treeTypeHashes = [
12
13
  "0x270da1ebafc0f24637af3612fb8c3a1d828fcb56d3637c24e86dd006b12ca7f9",
13
14
  "0x828b9cdf8326a1cf234328e4d5229546a98fb72ef73624f5b6b31538e555b96c",
@@ -127,6 +128,7 @@ function toCanonicalYParity(v) {
127
128
  *
128
129
  * console.log(EcrecoverRatifierUtils.treeTypeHash(0));
129
130
  * ```
131
+ * @deprecated Use {@link EcrecoverRatifier} with a tagged tree.
130
132
  */
131
133
  export var EcrecoverRatifierUtils;
132
134
  (function (EcrecoverRatifierUtils) {
@@ -203,7 +205,7 @@ export var EcrecoverRatifierUtils;
203
205
  * ```
204
206
  */
205
207
  function typedData(params) {
206
- const { tree, ratifier: verifyingContract } = RatifierUtils.normalizeRatifierTree({
208
+ const { tree, ratifier: verifyingContract } = Ratifier.normalizeRatifierTree({
207
209
  tree: params.tree,
208
210
  label: "Ecrecover",
209
211
  });
@@ -272,7 +274,7 @@ export var EcrecoverRatifierUtils;
272
274
  * ```
273
275
  */
274
276
  function digest(params) {
275
- const { tree, ratifier: verifyingContract } = RatifierUtils.normalizeRatifierTree({
277
+ const { tree, ratifier: verifyingContract } = Ratifier.normalizeRatifierTree({
276
278
  tree: params.tree,
277
279
  label: "Ecrecover",
278
280
  });
@@ -372,7 +374,7 @@ export var EcrecoverRatifierUtils;
372
374
  * Verifies an Ecrecover ratifier-data proof and recovers its signer.
373
375
  *
374
376
  * This helper intentionally does not check `Midnight.isAuthorized` or
375
- * `EcrecoverRatifier.isRootCanceled` state. Consumers must query both at the
377
+ * `EcrecoverRatifierUtils.isRootCanceled` state. Consumers must query both at the
376
378
  * observed block before treating a recovered item as executable: the returned
377
379
  * signer must be the maker or authorized by the maker, the offer ratifier must
378
380
  * be authorized by the maker, and `isRootCanceled(maker, root)` must be false.
@@ -489,7 +491,7 @@ export var EcrecoverRatifierUtils;
489
491
  * ```
490
492
  */
491
493
  async function sign(params) {
492
- const { tree } = RatifierUtils.normalizeRatifierTree({
494
+ const { tree } = Ratifier.normalizeRatifierTree({
493
495
  tree: params.tree,
494
496
  label: "Ecrecover",
495
497
  });
@@ -697,7 +699,7 @@ export var EcrecoverRatifierUtils;
697
699
  * ```
698
700
  */
699
701
  function ratifierData(params) {
700
- const { tree } = RatifierUtils.normalizeRatifierTree({
702
+ const { tree } = Ratifier.normalizeRatifierTree({
701
703
  tree: params.tree,
702
704
  label: "Ecrecover",
703
705
  });
@@ -771,7 +773,7 @@ export var EcrecoverRatifierUtils;
771
773
  * ```
772
774
  */
773
775
  async function ratify(params) {
774
- const { tree } = RatifierUtils.normalizeRatifierTree({
776
+ const { tree } = Ratifier.normalizeRatifierTree({
775
777
  tree: params.tree,
776
778
  label: "Ecrecover",
777
779
  });
@@ -814,13 +816,148 @@ export var EcrecoverRatifierUtils;
814
816
  account: params.account,
815
817
  }));
816
818
  }
819
+ const proofs = TreeUtils.buildProofs({
820
+ tree,
821
+ count: tree.offers.length,
822
+ });
817
823
  return tree.offers.map((offer, leafIndex) => ({
818
824
  offer,
819
825
  ratifierData: encodeRatifierData({
820
826
  signature,
821
- ...TreeUtils.buildProof({ tree, leafIndex }),
827
+ ...proofs[leafIndex],
822
828
  }),
823
829
  }));
824
830
  }
825
831
  EcrecoverRatifierUtils.ratify = ratify;
826
832
  })(EcrecoverRatifierUtils || (EcrecoverRatifierUtils = {}));
833
+ /**
834
+ * Tagged-tree API for EcrecoverRatifier with validated portable leaf commitments.
835
+ * Legacy untagged inputs remain available through {@link EcrecoverRatifierUtils}.
836
+ */
837
+ export var EcrecoverRatifier;
838
+ (function (EcrecoverRatifier) {
839
+ /**
840
+ * Builds Ecrecover signing data from a tagged tree.
841
+ * @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
842
+ * @param request.chainId - Chain defining the EIP-712 domain.
843
+ * @returns EIP-712 domain, type definitions, and offer-tree message.
844
+ * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
845
+ * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
846
+ * @example
847
+ * ```ts
848
+ * import { EcrecoverRatifier, type EcrecoverRatifierTypedDataRequest } from "@morpho-org/midnight-sdk";
849
+ * function execute(request: EcrecoverRatifierTypedDataRequest) {
850
+ * return EcrecoverRatifier.typedData(request);
851
+ * }
852
+ * ```
853
+ */
854
+ function typedData(request) {
855
+ const tree = parseStandardRatifierTree(request.tree, "ecrecover");
856
+ return EcrecoverRatifierUtils.typedData({ ...request, tree });
857
+ }
858
+ EcrecoverRatifier.typedData = typedData;
859
+ /**
860
+ * Computes the signing digest of a tagged Ecrecover tree.
861
+ * @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
862
+ * @param request.chainId - Chain defining the EIP-712 domain.
863
+ * @returns The EIP-712 signing digest.
864
+ * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
865
+ * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
866
+ * @example
867
+ * ```ts
868
+ * import { EcrecoverRatifier, type EcrecoverRatifierTypedDataRequest } from "@morpho-org/midnight-sdk";
869
+ * function execute(request: EcrecoverRatifierTypedDataRequest) {
870
+ * return EcrecoverRatifier.digest(request);
871
+ * }
872
+ * ```
873
+ */
874
+ function digest(request) {
875
+ const tree = parseStandardRatifierTree(request.tree, "ecrecover");
876
+ return EcrecoverRatifierUtils.digest({ ...request, tree });
877
+ }
878
+ EcrecoverRatifier.digest = digest;
879
+ /**
880
+ * Signs a tagged Ecrecover tree using the supplied client and account.
881
+ * @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
882
+ * @param request.account - Maker or authorized signer.
883
+ * @param request.client - Signing client; omit when ratify receives a precomputed signature.
884
+ * @returns A verified hex signature over the tree.
885
+ * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
886
+ * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
887
+ * @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
888
+ * @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
889
+ * @example
890
+ * ```ts
891
+ * import { EcrecoverRatifier, type EcrecoverRatifierSignRequest } from "@morpho-org/midnight-sdk";
892
+ * function execute(request: EcrecoverRatifierSignRequest) {
893
+ * return EcrecoverRatifier.sign(request);
894
+ * }
895
+ * ```
896
+ */
897
+ async function sign(request) {
898
+ const tree = parseStandardRatifierTree(request.tree, "ecrecover");
899
+ return EcrecoverRatifierUtils.sign({ ...request, tree });
900
+ }
901
+ EcrecoverRatifier.sign = sign;
902
+ /**
903
+ * Encodes the signature and proof for one tagged Ecrecover leaf.
904
+ * @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
905
+ * @param request.signature - Tree signature; ratify may instead request one through the supplied client.
906
+ * @param request.leafIndex - Leaf to prove.
907
+ * @returns ABI-encoded ratifier data for the requested leaf.
908
+ * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
909
+ * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
910
+ * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
911
+ * @example
912
+ * ```ts
913
+ * import { EcrecoverRatifier, type EcrecoverRatifierDataRequest } from "@morpho-org/midnight-sdk";
914
+ * function execute(request: EcrecoverRatifierDataRequest) {
915
+ * return EcrecoverRatifier.ratifierData(request);
916
+ * }
917
+ * ```
918
+ */
919
+ function ratifierData(request) {
920
+ const tree = parseStandardRatifierTree(request.tree, "ecrecover");
921
+ return EcrecoverRatifierUtils.ratifierData({ ...request, tree });
922
+ }
923
+ EcrecoverRatifier.ratifierData = ratifierData;
924
+ /**
925
+ * Builds payload items from a tagged tree and a verified signature.
926
+ * @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
927
+ * @param request.account - Maker or authorized signer.
928
+ * @param request.client - Signing client; omit when ratify receives a precomputed signature.
929
+ * @param request.signature - Tree signature; ratify may instead request one through the supplied client.
930
+ * @returns One payload item per non-padding offer, including its ratifier data.
931
+ * @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
932
+ * @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
933
+ * @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
934
+ * @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
935
+ * @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
936
+ * @example
937
+ * ```ts
938
+ * import { EcrecoverRatifier, type EcrecoverRatifierRatifyRequest } from "@morpho-org/midnight-sdk";
939
+ * function execute(request: EcrecoverRatifierRatifyRequest) {
940
+ * return EcrecoverRatifier.ratify(request);
941
+ * }
942
+ * ```
943
+ */
944
+ async function ratify(request) {
945
+ const tree = parseStandardRatifierTree(request.tree, "ecrecover");
946
+ return EcrecoverRatifierUtils.ratify({ ...request, tree });
947
+ }
948
+ EcrecoverRatifier.ratify = ratify;
949
+ /** {@inheritDoc EcrecoverRatifierUtils.treeTypeHash} */
950
+ EcrecoverRatifier.treeTypeHash = EcrecoverRatifierUtils.treeTypeHash;
951
+ /** {@inheritDoc EcrecoverRatifierUtils.digestForRoot} */
952
+ EcrecoverRatifier.digestForRoot = EcrecoverRatifierUtils.digestForRoot;
953
+ /** {@inheritDoc EcrecoverRatifierUtils.digestRatifierData} */
954
+ EcrecoverRatifier.digestRatifierData = EcrecoverRatifierUtils.digestRatifierData;
955
+ /** {@inheritDoc EcrecoverRatifierUtils.verifyRatifierData} */
956
+ EcrecoverRatifier.verifyRatifierData = EcrecoverRatifierUtils.verifyRatifierData;
957
+ /** {@inheritDoc EcrecoverRatifierUtils.toSignature} */
958
+ EcrecoverRatifier.toSignature = EcrecoverRatifierUtils.toSignature;
959
+ /** {@inheritDoc EcrecoverRatifierUtils.encodeRatifierData} */
960
+ EcrecoverRatifier.encodeRatifierData = EcrecoverRatifierUtils.encodeRatifierData;
961
+ /** {@inheritDoc EcrecoverRatifierUtils.decodeRatifierData} */
962
+ EcrecoverRatifier.decodeRatifierData = EcrecoverRatifierUtils.decodeRatifierData;
963
+ })(EcrecoverRatifier || (EcrecoverRatifier = {}));
@@ -154,8 +154,11 @@ export declare namespace GroupUtils {
154
154
  /**
155
155
  * Derives the deterministic content-addressed id for a group of offers.
156
156
  *
157
- * This mirrors the router implementation: hash each offer with `group = 0`,
158
- * sort those hashes, concatenate them, then keccak the result.
157
+ * Hashes each offer with `group = 0` using the protocol offer hash, sorts
158
+ * those hashes, concatenates them, then keccaks the result. This matches the
159
+ * router for Ecrecover and Setter offers only; RateRatifierV1 and
160
+ * PriceRatifierV1 commit to scheme leaf hashes, so use
161
+ * `RateRatifierV1.groupId` / `PriceRatifierV1.groupId` for those.
159
162
  *
160
163
  * @param offers - Offers to hash as one group.
161
164
  * @returns Content-addressed group id.
@@ -195,6 +198,56 @@ export declare namespace GroupUtils {
195
198
  * ```
196
199
  */
197
200
  function hash(offers: Iterable<IOffer>): Hash;
201
+ /**
202
+ * Derives a content-addressed group id from ratifier-specific member hashes.
203
+ *
204
+ * This is the ratifier-agnostic core of {@link hash}: each member hash must
205
+ * commit the offer with `group = 0` under the scheme the offer is ratified
206
+ * with (the protocol offer hash for Ecrecover/Setter, the zero-group leaf
207
+ * hash for `RateRatifierV1`/`PriceRatifierV1`). Hashes are lowercased,
208
+ * sorted, concatenated, then keccak'd, so neither member order nor hex
209
+ * casing affects the id.
210
+ *
211
+ * @param memberHashes - Zero-group member hashes of one consumption group.
212
+ * @returns Content-addressed group id.
213
+ * @throws {InvalidOfferGroupError} when `memberHashes` is empty.
214
+ * @example
215
+ * ```ts
216
+ * import { GroupUtils, Offer, RateRatifierV1 } from "@morpho-org/midnight-sdk";
217
+ * import { zeroAddress } from "viem";
218
+ *
219
+ * const offer = Offer.create({
220
+ * market: {
221
+ * chainId: 8453,
222
+ * midnight: "0x0000000000000000000000000000000000001000",
223
+ * loanToken: "0x0000000000000000000000000000000000006000",
224
+ * collateralParams: [
225
+ * {
226
+ * token: "0x0000000000000000000000000000000000007000",
227
+ * lltv: 770000000000000000n,
228
+ * liquidationCursor: 250000000000000000n,
229
+ * oracle: "0x0000000000000000000000000000000000008000",
230
+ * },
231
+ * ],
232
+ * maturity: 54_000n,
233
+ * rcfThreshold: 0n,
234
+ * enterGate: zeroAddress,
235
+ * liquidatorGate: zeroAddress,
236
+ * },
237
+ * buy: true,
238
+ * maker: "0x0000000000000000000000000000000000009000",
239
+ * tick: 5_000n,
240
+ * expiry: 3_600n,
241
+ * ratifier: "0x000000000000000000000000000000000000a111",
242
+ * maxUnits: 100n,
243
+ * });
244
+ * const id = GroupUtils.hashMembers([
245
+ * RateRatifierV1.memberHash({ offer, rate: 50_000_000_000_000_000n }),
246
+ * ]);
247
+ * console.log(id);
248
+ * ```
249
+ */
250
+ function hashMembers(memberHashes: Iterable<Hash>): Hash;
198
251
  /**
199
252
  * Converts a group into ABI-compatible offers carrying the derived group id.
200
253
  *
@@ -73,8 +73,11 @@ export var GroupUtils;
73
73
  /**
74
74
  * Derives the deterministic content-addressed id for a group of offers.
75
75
  *
76
- * This mirrors the router implementation: hash each offer with `group = 0`,
77
- * sort those hashes, concatenate them, then keccak the result.
76
+ * Hashes each offer with `group = 0` using the protocol offer hash, sorts
77
+ * those hashes, concatenates them, then keccaks the result. This matches the
78
+ * router for Ecrecover and Setter offers only; RateRatifierV1 and
79
+ * PriceRatifierV1 commit to scheme leaf hashes, so use
80
+ * `RateRatifierV1.groupId` / `PriceRatifierV1.groupId` for those.
78
81
  *
79
82
  * @param offers - Offers to hash as one group.
80
83
  * @returns Content-addressed group id.
@@ -118,11 +121,67 @@ export var GroupUtils;
118
121
  if (offerInputs.length === 0) {
119
122
  throw new InvalidOfferGroupError("Provide at least one offer in the group.");
120
123
  }
121
- const offerHashes = offerInputs.map((offer) => OfferUtils.groupHash(offer));
122
- const sorted = offerHashes.length > 1 ? [...offerHashes].sort() : offerHashes;
123
- return keccak256(concat(sorted));
124
+ return hashMembers(offerInputs.map((offer) => OfferUtils.groupHash(offer)));
124
125
  }
125
126
  GroupUtils.hash = hash;
127
+ /**
128
+ * Derives a content-addressed group id from ratifier-specific member hashes.
129
+ *
130
+ * This is the ratifier-agnostic core of {@link hash}: each member hash must
131
+ * commit the offer with `group = 0` under the scheme the offer is ratified
132
+ * with (the protocol offer hash for Ecrecover/Setter, the zero-group leaf
133
+ * hash for `RateRatifierV1`/`PriceRatifierV1`). Hashes are lowercased,
134
+ * sorted, concatenated, then keccak'd, so neither member order nor hex
135
+ * casing affects the id.
136
+ *
137
+ * @param memberHashes - Zero-group member hashes of one consumption group.
138
+ * @returns Content-addressed group id.
139
+ * @throws {InvalidOfferGroupError} when `memberHashes` is empty.
140
+ * @example
141
+ * ```ts
142
+ * import { GroupUtils, Offer, RateRatifierV1 } from "@morpho-org/midnight-sdk";
143
+ * import { zeroAddress } from "viem";
144
+ *
145
+ * const offer = Offer.create({
146
+ * market: {
147
+ * chainId: 8453,
148
+ * midnight: "0x0000000000000000000000000000000000001000",
149
+ * loanToken: "0x0000000000000000000000000000000000006000",
150
+ * collateralParams: [
151
+ * {
152
+ * token: "0x0000000000000000000000000000000000007000",
153
+ * lltv: 770000000000000000n,
154
+ * liquidationCursor: 250000000000000000n,
155
+ * oracle: "0x0000000000000000000000000000000000008000",
156
+ * },
157
+ * ],
158
+ * maturity: 54_000n,
159
+ * rcfThreshold: 0n,
160
+ * enterGate: zeroAddress,
161
+ * liquidatorGate: zeroAddress,
162
+ * },
163
+ * buy: true,
164
+ * maker: "0x0000000000000000000000000000000000009000",
165
+ * tick: 5_000n,
166
+ * expiry: 3_600n,
167
+ * ratifier: "0x000000000000000000000000000000000000a111",
168
+ * maxUnits: 100n,
169
+ * });
170
+ * const id = GroupUtils.hashMembers([
171
+ * RateRatifierV1.memberHash({ offer, rate: 50_000_000_000_000_000n }),
172
+ * ]);
173
+ * console.log(id);
174
+ * ```
175
+ */
176
+ function hashMembers(memberHashes) {
177
+ const hashes = Array.from(memberHashes, (h) => h.toLowerCase());
178
+ if (hashes.length === 0) {
179
+ throw new InvalidOfferGroupError("Provide at least one member hash in the group.");
180
+ }
181
+ const sorted = hashes.length > 1 ? [...hashes].sort() : hashes;
182
+ return keccak256(concat(sorted));
183
+ }
184
+ GroupUtils.hashMembers = hashMembers;
126
185
  /**
127
186
  * Converts a group into ABI-compatible offers carrying the derived group id.
128
187
  *
@@ -13,12 +13,12 @@ export declare namespace Payload {
13
13
  * `ratifierData` blob a taker hands to `Midnight.take(..., ratifierData)`.
14
14
  *
15
15
  * `ratifierData` is owned by the ratifier scheme the maker used (e.g.
16
- * `EcrecoverRatifierUtils.ratifierData`). The payload codec treats it as
16
+ * `EcrecoverRatifier.ratifierData`). The payload codec treats it as
17
17
  * opaque bytes — simulators that only need to forward `ratifierData` can
18
18
  * stay completely ratifier-agnostic.
19
19
  *
20
- * Build items with `EcrecoverRatifierUtils.ratify` or
21
- * `SetterRatifierUtils.ratify` after the tree is signed or approved, then pass
20
+ * Build items with `EcrecoverRatifier.ratify` or
21
+ * `SetterRatifier.ratify` after the tree is signed or approved, then pass
22
22
  * the items to `Payload.encode` for publication.
23
23
  *
24
24
  * @example