@morpho-org/midnight-sdk 0.1.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 (157) hide show
  1. package/README.md +252 -0
  2. package/lib/cjs/abis.d.ts +3170 -0
  3. package/lib/cjs/abis.js +4073 -0
  4. package/lib/cjs/api/MidnightApi.d.ts +532 -0
  5. package/lib/cjs/api/MidnightApi.js +713 -0
  6. package/lib/cjs/api/helpers.d.ts +20 -0
  7. package/lib/cjs/api/helpers.js +189 -0
  8. package/lib/cjs/api/index.d.ts +2 -0
  9. package/lib/cjs/api/index.js +21 -0
  10. package/lib/cjs/api/types.d.ts +453 -0
  11. package/lib/cjs/api/types.js +2 -0
  12. package/lib/cjs/constants.d.ts +168 -0
  13. package/lib/cjs/constants.js +187 -0
  14. package/lib/cjs/errors.d.ts +253 -0
  15. package/lib/cjs/errors.js +309 -0
  16. package/lib/cjs/fetch/Market.d.ts +65 -0
  17. package/lib/cjs/fetch/Market.js +117 -0
  18. package/lib/cjs/fetch/Offer.d.ts +74 -0
  19. package/lib/cjs/fetch/Offer.js +119 -0
  20. package/lib/cjs/fetch/Position.d.ts +85 -0
  21. package/lib/cjs/fetch/Position.js +148 -0
  22. package/lib/cjs/fetch/Ratifier.d.ts +38 -0
  23. package/lib/cjs/fetch/Ratifier.js +57 -0
  24. package/lib/cjs/fetch/index.d.ts +5 -0
  25. package/lib/cjs/fetch/index.js +21 -0
  26. package/lib/cjs/fetch/types.d.ts +57 -0
  27. package/lib/cjs/fetch/types.js +2 -0
  28. package/lib/cjs/fetch/utils.d.ts +8 -0
  29. package/lib/cjs/fetch/utils.js +18 -0
  30. package/lib/cjs/index.d.ts +18 -0
  31. package/lib/cjs/index.js +57 -0
  32. package/lib/cjs/market/Market.d.ts +613 -0
  33. package/lib/cjs/market/Market.js +503 -0
  34. package/lib/cjs/market/MarketUtils.d.ts +189 -0
  35. package/lib/cjs/market/MarketUtils.js +303 -0
  36. package/lib/cjs/market/Position.d.ts +424 -0
  37. package/lib/cjs/market/Position.js +425 -0
  38. package/lib/cjs/market/PositionUtils.d.ts +115 -0
  39. package/lib/cjs/market/PositionUtils.js +194 -0
  40. package/lib/cjs/market/index.d.ts +4 -0
  41. package/lib/cjs/market/index.js +20 -0
  42. package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
  43. package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
  44. package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
  45. package/lib/cjs/math/TakeAmountsLib.js +199 -0
  46. package/lib/cjs/math/TickLib.d.ts +200 -0
  47. package/lib/cjs/math/TickLib.js +272 -0
  48. package/lib/cjs/math/index.d.ts +3 -0
  49. package/lib/cjs/math/index.js +19 -0
  50. package/lib/cjs/offers/Offer.d.ts +534 -0
  51. package/lib/cjs/offers/Offer.js +343 -0
  52. package/lib/cjs/offers/OfferUtils.d.ts +565 -0
  53. package/lib/cjs/offers/OfferUtils.js +659 -0
  54. package/lib/cjs/offers/index.d.ts +2 -0
  55. package/lib/cjs/offers/index.js +18 -0
  56. package/lib/cjs/package.json +1 -0
  57. package/lib/cjs/queries/GetPosition.d.ts +55 -0
  58. package/lib/cjs/queries/GetPosition.js +74 -0
  59. package/lib/cjs/queries/index.d.ts +1 -0
  60. package/lib/cjs/queries/index.js +37 -0
  61. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  62. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
  63. package/lib/cjs/signatures/Group.d.ts +178 -0
  64. package/lib/cjs/signatures/Group.js +193 -0
  65. package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
  66. package/lib/cjs/signatures/GroupUtils.js +127 -0
  67. package/lib/cjs/signatures/Payload.d.ts +283 -0
  68. package/lib/cjs/signatures/Payload.js +570 -0
  69. package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
  70. package/lib/cjs/signatures/RatifierUtils.js +76 -0
  71. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
  72. package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
  73. package/lib/cjs/signatures/Tree.d.ts +241 -0
  74. package/lib/cjs/signatures/Tree.js +263 -0
  75. package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
  76. package/lib/cjs/signatures/TreeUtils.js +461 -0
  77. package/lib/cjs/signatures/index.d.ts +7 -0
  78. package/lib/cjs/signatures/index.js +23 -0
  79. package/lib/esm/abis.d.ts +3170 -0
  80. package/lib/esm/abis.js +4070 -0
  81. package/lib/esm/api/MidnightApi.d.ts +532 -0
  82. package/lib/esm/api/MidnightApi.js +709 -0
  83. package/lib/esm/api/helpers.d.ts +20 -0
  84. package/lib/esm/api/helpers.js +180 -0
  85. package/lib/esm/api/index.d.ts +2 -0
  86. package/lib/esm/api/index.js +2 -0
  87. package/lib/esm/api/types.d.ts +453 -0
  88. package/lib/esm/api/types.js +1 -0
  89. package/lib/esm/constants.d.ts +168 -0
  90. package/lib/esm/constants.js +184 -0
  91. package/lib/esm/errors.d.ts +253 -0
  92. package/lib/esm/errors.js +291 -0
  93. package/lib/esm/fetch/Market.d.ts +65 -0
  94. package/lib/esm/fetch/Market.js +113 -0
  95. package/lib/esm/fetch/Offer.d.ts +74 -0
  96. package/lib/esm/fetch/Offer.js +116 -0
  97. package/lib/esm/fetch/Position.d.ts +85 -0
  98. package/lib/esm/fetch/Position.js +144 -0
  99. package/lib/esm/fetch/Ratifier.d.ts +38 -0
  100. package/lib/esm/fetch/Ratifier.js +54 -0
  101. package/lib/esm/fetch/index.d.ts +5 -0
  102. package/lib/esm/fetch/index.js +5 -0
  103. package/lib/esm/fetch/types.d.ts +57 -0
  104. package/lib/esm/fetch/types.js +1 -0
  105. package/lib/esm/fetch/utils.d.ts +8 -0
  106. package/lib/esm/fetch/utils.js +12 -0
  107. package/lib/esm/index.d.ts +18 -0
  108. package/lib/esm/index.js +18 -0
  109. package/lib/esm/market/Market.d.ts +613 -0
  110. package/lib/esm/market/Market.js +498 -0
  111. package/lib/esm/market/MarketUtils.d.ts +189 -0
  112. package/lib/esm/market/MarketUtils.js +300 -0
  113. package/lib/esm/market/Position.d.ts +424 -0
  114. package/lib/esm/market/Position.js +420 -0
  115. package/lib/esm/market/PositionUtils.d.ts +115 -0
  116. package/lib/esm/market/PositionUtils.js +191 -0
  117. package/lib/esm/market/index.d.ts +4 -0
  118. package/lib/esm/market/index.js +4 -0
  119. package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
  120. package/lib/esm/math/ConsumableUnitsLib.js +74 -0
  121. package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
  122. package/lib/esm/math/TakeAmountsLib.js +196 -0
  123. package/lib/esm/math/TickLib.d.ts +200 -0
  124. package/lib/esm/math/TickLib.js +269 -0
  125. package/lib/esm/math/index.d.ts +3 -0
  126. package/lib/esm/math/index.js +3 -0
  127. package/lib/esm/offers/Offer.d.ts +534 -0
  128. package/lib/esm/offers/Offer.js +339 -0
  129. package/lib/esm/offers/OfferUtils.d.ts +565 -0
  130. package/lib/esm/offers/OfferUtils.js +656 -0
  131. package/lib/esm/offers/index.d.ts +2 -0
  132. package/lib/esm/offers/index.js +2 -0
  133. package/lib/esm/package.json +1 -0
  134. package/lib/esm/queries/GetPosition.d.ts +55 -0
  135. package/lib/esm/queries/GetPosition.js +71 -0
  136. package/lib/esm/queries/index.d.ts +1 -0
  137. package/lib/esm/queries/index.js +1 -0
  138. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  139. package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
  140. package/lib/esm/signatures/Group.d.ts +178 -0
  141. package/lib/esm/signatures/Group.js +189 -0
  142. package/lib/esm/signatures/GroupUtils.d.ts +191 -0
  143. package/lib/esm/signatures/GroupUtils.js +124 -0
  144. package/lib/esm/signatures/Payload.d.ts +283 -0
  145. package/lib/esm/signatures/Payload.js +564 -0
  146. package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
  147. package/lib/esm/signatures/RatifierUtils.js +73 -0
  148. package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
  149. package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
  150. package/lib/esm/signatures/Tree.d.ts +241 -0
  151. package/lib/esm/signatures/Tree.js +259 -0
  152. package/lib/esm/signatures/TreeUtils.d.ts +573 -0
  153. package/lib/esm/signatures/TreeUtils.js +458 -0
  154. package/lib/esm/signatures/index.d.ts +7 -0
  155. package/lib/esm/signatures/index.js +7 -0
  156. package/package.json +69 -0
  157. package/src/index.ts +19 -0
@@ -0,0 +1,618 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EcrecoverRatifierUtils = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const viem_1 = require("viem");
6
+ const actions_1 = require("viem/actions");
7
+ const constants_js_1 = require("../constants.js");
8
+ const errors_js_1 = require("../errors.js");
9
+ const treeTypeHashes = [
10
+ "0xc27c38e446b48c820ab9c4373dc63a4a750a08165cb4bb488206ebabe045d650",
11
+ "0x4e15d8736f4406e07bf9844b1653474472a827130c61e899bf1f574a88b8d987",
12
+ "0x46d107447b480c38ef5b7f54603dba0cb23b887f302b01a998b9d8a80320dd53",
13
+ "0xd1f3607a8e81454bb3baf5f898274ab47541fffc690278a74f13e174e116be72",
14
+ "0xb2d98adca9d116c9bc02ce59ec599ac3c2d33db1c0d1217c7e411d9198d427be",
15
+ "0x5931e0597fcf986027f3118b2495a9ac22139d133f9ad2c2198e6738dc3886c5",
16
+ "0x3967d37928614a085b47e8758fbc3869a8aed63bdf60ccee8536ff2b5064da06",
17
+ "0xd6b9f5f45915a260f6e521d9b40f86c385730b6bc330590fcde212e2fea64263",
18
+ "0x080caa519dbd5328c119d9907e0fa3d9a50dc2ae4bf6dd42c93c100dbc89b51a",
19
+ "0x45da471048924165ea2ad1855ba940e454b486e71dcf1666c71a928c8844c419",
20
+ "0xa49a9434fc1836bd08097368325b31039b6a0fd44919f53e4d8f4bf814084cb0",
21
+ "0xd3e93e4525132f0187a6964dc01fef33fde414538ffd212e9f2f478c3263e0a0",
22
+ "0x25990db2d26547f92c711988300df317af57bad5cd5d9d8e787a82f95c929474",
23
+ "0x8e0c648afa977572ead40a1d10a6db2c425b8099545006d834a7b849c6166643",
24
+ "0x4b635250efa6243e277fdd0cf6df993c2943b64f10f3a0756ceb1f47ef8f9b18",
25
+ "0xbde1c927f6222c07c8df264e68b42b8382c7c2b85f4729e0df94297cfeebfa91",
26
+ "0x4d58aea1a67f94be21ab1415bf3b602592430eb9112268fd0fc4e141b1a35e76",
27
+ "0x14c03281bce13010b158e5a4a3378be394ac9e16118aedb17d82ced51e66836c",
28
+ "0x99fd3e76f43b2cc221cb9860bc6c96cda95af3fa07ef5f04e071b54aa9386d06",
29
+ "0x1b1c2f1a04968094d8d0453d49838f7a809d1202ae04a1c2e0964e442ff7988b",
30
+ "0xc8ccd3cb3267dd76f563584920ac60f2283b719917481b85fe5e10b754932455",
31
+ ];
32
+ const signatureAbi = [
33
+ {
34
+ type: "tuple",
35
+ components: [
36
+ { name: "v", type: "uint8" },
37
+ { name: "r", type: "bytes32" },
38
+ { name: "s", type: "bytes32" },
39
+ ],
40
+ },
41
+ { name: "root", type: "bytes32" },
42
+ { name: "leafIndex", type: "uint256" },
43
+ { name: "proof", type: "bytes32[]" },
44
+ ];
45
+ const domainSeparatorAbi = [
46
+ { name: "typehash", type: "bytes32" },
47
+ { name: "chainId", type: "uint256" },
48
+ { name: "verifyingContract", type: "address" },
49
+ ];
50
+ const treeStructHashAbi = [
51
+ { name: "typehash", type: "bytes32" },
52
+ { name: "root", type: "bytes32" },
53
+ ];
54
+ const typedDataTypes = {
55
+ EIP712Domain: [
56
+ { name: "chainId", type: "uint256" },
57
+ { name: "verifyingContract", type: "address" },
58
+ ],
59
+ CollateralParams: [
60
+ { name: "token", type: "address" },
61
+ { name: "lltv", type: "uint256" },
62
+ { name: "maxLif", type: "uint256" },
63
+ { name: "oracle", type: "address" },
64
+ ],
65
+ Market: [
66
+ { name: "loanToken", type: "address" },
67
+ { name: "collateralParams", type: "CollateralParams[]" },
68
+ { name: "maturity", type: "uint256" },
69
+ { name: "rcfThreshold", type: "uint256" },
70
+ { name: "enterGate", type: "address" },
71
+ { name: "liquidatorGate", type: "address" },
72
+ ],
73
+ Offer: [
74
+ { name: "market", type: "Market" },
75
+ { name: "buy", type: "bool" },
76
+ { name: "maker", type: "address" },
77
+ { name: "start", type: "uint256" },
78
+ { name: "expiry", type: "uint256" },
79
+ { name: "tick", type: "uint256" },
80
+ { name: "group", type: "bytes32" },
81
+ { name: "callback", type: "address" },
82
+ { name: "callbackData", type: "bytes" },
83
+ { name: "receiverIfMakerIsSeller", type: "address" },
84
+ { name: "ratifier", type: "address" },
85
+ { name: "reduceOnly", type: "bool" },
86
+ { name: "maxUnits", type: "uint256" },
87
+ { name: "maxAssets", type: "uint256" },
88
+ { name: "continuousFeeCap", type: "uint256" },
89
+ ],
90
+ };
91
+ const buildTreeValue = (offers) => {
92
+ if (offers.length === 1)
93
+ return offers[0];
94
+ const mid = offers.length / 2;
95
+ return [
96
+ buildTreeValue(offers.slice(0, mid)),
97
+ buildTreeValue(offers.slice(mid)),
98
+ ];
99
+ };
100
+ const getTreeRatifier = (tree) => {
101
+ const offers = tree.offers;
102
+ const firstOffer = offers[0];
103
+ if (firstOffer == null) {
104
+ throw new errors_js_1.InvalidTreeError("Tree must contain at least one offer.");
105
+ }
106
+ const ratifier = firstOffer.ratifier;
107
+ const comparableRatifier = ratifier.toLowerCase();
108
+ for (const offer of offers.slice(1)) {
109
+ if (offer.ratifier.toLowerCase() !== comparableRatifier) {
110
+ throw new errors_js_1.InvalidTreeError(`All offers in an Ecrecover tree must use one ratifier; expected "${ratifier}", got "${offer.ratifier}". Build separate trees per ratifier.`);
111
+ }
112
+ }
113
+ return ratifier;
114
+ };
115
+ /**
116
+ * EcrecoverRatifier-specific pure utilities.
117
+ *
118
+ * Use this route for EOA and EIP-7702 makers. The make-side sequence is:
119
+ * create offers with the Ecrecover ratifier address, build the group/tree,
120
+ * validate the tree, sign the typed data, call `ratify`, then pass the returned
121
+ * items to `Payload.encode`. Ecrecover trees must contain one ratifier; split
122
+ * trees by ratifier before signing. The signer may be the maker or an address
123
+ * authorized by every maker in the tree.
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
128
+ *
129
+ * console.log(EcrecoverRatifierUtils.treeTypeHash(0));
130
+ * ```
131
+ */
132
+ var EcrecoverRatifierUtils;
133
+ (function (EcrecoverRatifierUtils) {
134
+ /**
135
+ * Returns the Solidity HashLib tree typehash for a tree height.
136
+ *
137
+ * @param height - Tree height.
138
+ * @returns Tree typehash.
139
+ * @throws {InvalidTreeHeightError} when height exceeds 20.
140
+ * @example
141
+ * ```ts
142
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
143
+ *
144
+ * console.log(EcrecoverRatifierUtils.treeTypeHash(0));
145
+ * ```
146
+ */
147
+ function treeTypeHash(height) {
148
+ const typehash = treeTypeHashes[height];
149
+ if (typehash == null)
150
+ throw new errors_js_1.InvalidTreeHeightError(height);
151
+ return typehash;
152
+ }
153
+ EcrecoverRatifierUtils.treeTypeHash = treeTypeHash;
154
+ /**
155
+ * Builds EcrecoverRatifier typed data for a tree.
156
+ *
157
+ * Use after the tree is built and validated, before requesting the signer
158
+ * signature. The EIP-712 verifier is derived from the shared ratifier address
159
+ * on the tree offers. `ratify` calls this for you when given a client and
160
+ * account.
161
+ *
162
+ * @param params.tree - Ecrecover-ratified offer tree to sign.
163
+ * @param params.chainId - EIP-155 chain id included in the EIP-712 domain.
164
+ * @returns EIP-712 typed-data descriptor.
165
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
166
+ * @throws {InvalidTreeHeightError} when the tree height is unsupported.
167
+ * @example
168
+ * ```ts
169
+ * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
170
+ * import { Offer } from "@morpho-org/midnight-sdk";
171
+ * import { zeroAddress } from "viem";
172
+ *
173
+ * const offer = Offer.create({
174
+ * market: {
175
+ * loanToken: "0x0000000000000000000000000000000000006000",
176
+ * collateralParams: [
177
+ * {
178
+ * token: "0x0000000000000000000000000000000000007000",
179
+ * lltv: 770000000000000000n,
180
+ * maxLif: 1061007957559681697n,
181
+ * oracle: "0x0000000000000000000000000000000000008000",
182
+ * },
183
+ * ],
184
+ * maturity: 54_000n,
185
+ * rcfThreshold: 0n,
186
+ * enterGate: zeroAddress,
187
+ * liquidatorGate: zeroAddress,
188
+ * },
189
+ * buy: true,
190
+ * maker: "0x0000000000000000000000000000000000009000",
191
+ * tick: 5_000n,
192
+ * expiry: 3_600n,
193
+ * ratifier: "0x0000000000000000000000000000000000004000",
194
+ * maxUnits: 100n,
195
+ * });
196
+ *
197
+ * const typedData = EcrecoverRatifierUtils.typedData({
198
+ * tree: Tree.create([offer]),
199
+ * chainId: 8453n,
200
+ * });
201
+ * console.log(typedData.primaryType);
202
+ * ```
203
+ */
204
+ function typedData(params) {
205
+ const verifyingContract = getTreeRatifier(params.tree);
206
+ const treeType = params.tree.height === 0
207
+ ? "Offer"
208
+ : `Offer${"[2]".repeat(params.tree.height)}`;
209
+ return (0, morpho_ts_1.deepFreeze)({
210
+ domain: {
211
+ chainId: BigInt(params.chainId),
212
+ verifyingContract,
213
+ },
214
+ types: {
215
+ ...typedDataTypes,
216
+ OfferTree: [{ name: "offerTree", type: treeType }],
217
+ },
218
+ primaryType: "OfferTree",
219
+ message: {
220
+ offerTree: buildTreeValue(params.tree.paddedOffers),
221
+ },
222
+ });
223
+ }
224
+ EcrecoverRatifierUtils.typedData = typedData;
225
+ /**
226
+ * Builds the EcrecoverRatifier digest used by the Solidity ratifier.
227
+ *
228
+ * @param params.tree - Ecrecover-ratified offer tree to hash.
229
+ * @param params.chainId - EIP-155 chain id included in the EIP-712 domain.
230
+ * @returns EIP-712 digest.
231
+ * @throws {InvalidTreeError} when the tree contains multiple ratifiers.
232
+ * @throws {InvalidTreeHeightError} when height exceeds 20.
233
+ * @example
234
+ * ```ts
235
+ * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
236
+ * import { Offer } from "@morpho-org/midnight-sdk";
237
+ * import { zeroAddress } from "viem";
238
+ *
239
+ * const offer = Offer.create({
240
+ * market: {
241
+ * loanToken: "0x0000000000000000000000000000000000006000",
242
+ * collateralParams: [
243
+ * {
244
+ * token: "0x0000000000000000000000000000000000007000",
245
+ * lltv: 770000000000000000n,
246
+ * maxLif: 1061007957559681697n,
247
+ * oracle: "0x0000000000000000000000000000000000008000",
248
+ * },
249
+ * ],
250
+ * maturity: 54_000n,
251
+ * rcfThreshold: 0n,
252
+ * enterGate: zeroAddress,
253
+ * liquidatorGate: zeroAddress,
254
+ * },
255
+ * buy: true,
256
+ * maker: "0x0000000000000000000000000000000000009000",
257
+ * tick: 5_000n,
258
+ * expiry: 3_600n,
259
+ * ratifier: "0x0000000000000000000000000000000000004000",
260
+ * maxUnits: 100n,
261
+ * });
262
+ *
263
+ * const digest = EcrecoverRatifierUtils.digest({
264
+ * tree: Tree.create([offer]),
265
+ * chainId: 8453n,
266
+ * });
267
+ * console.log(digest);
268
+ * ```
269
+ */
270
+ function digest(params) {
271
+ const verifyingContract = getTreeRatifier(params.tree);
272
+ const domainSeparator = (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)(domainSeparatorAbi, [
273
+ constants_js_1.EIP712_DOMAIN_TYPEHASH,
274
+ BigInt(params.chainId),
275
+ verifyingContract,
276
+ ]));
277
+ const structHash = (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)(treeStructHashAbi, [
278
+ treeTypeHash(params.tree.height),
279
+ params.tree.root,
280
+ ]));
281
+ return (0, viem_1.keccak256)((0, viem_1.concat)(["0x1901", domainSeparator, structHash]));
282
+ }
283
+ EcrecoverRatifierUtils.digest = digest;
284
+ /**
285
+ * Signs EcrecoverRatifier typed data through a viem client.
286
+ *
287
+ * Use when app code wants the signature separately from payload item
288
+ * construction. If you only need payload items, call `ratify` with the same
289
+ * client and account. The account may be the maker or an address authorized
290
+ * by every maker in the tree; the protocol checks that authorization onchain.
291
+ *
292
+ * @param params.tree - Ecrecover-ratified offer tree to sign.
293
+ * @param params.client - Viem client whose transport signs the tree typed data.
294
+ * @param params.account - Account used to sign the tree typed data.
295
+ * @returns Signature returned by the client.
296
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
297
+ * @throws {InvalidTreeHeightError} when the tree height is unsupported.
298
+ * @throws {InvalidTypedDataSignatureError} when the returned signature does not recover to `params.account`.
299
+ * @example
300
+ * ```ts
301
+ * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
302
+ * import { Offer } from "@morpho-org/midnight-sdk";
303
+ * import { createWalletClient, http, zeroAddress } from "viem";
304
+ * import { base } from "viem/chains";
305
+ *
306
+ * const offer = Offer.create({
307
+ * market: {
308
+ * loanToken: "0x0000000000000000000000000000000000006000",
309
+ * collateralParams: [
310
+ * {
311
+ * token: "0x0000000000000000000000000000000000007000",
312
+ * lltv: 770000000000000000n,
313
+ * maxLif: 1061007957559681697n,
314
+ * oracle: "0x0000000000000000000000000000000000008000",
315
+ * },
316
+ * ],
317
+ * maturity: 54_000n,
318
+ * rcfThreshold: 0n,
319
+ * enterGate: zeroAddress,
320
+ * liquidatorGate: zeroAddress,
321
+ * },
322
+ * buy: true,
323
+ * maker: "0x0000000000000000000000000000000000009000",
324
+ * tick: 5_000n,
325
+ * expiry: 3_600n,
326
+ * ratifier: "0x0000000000000000000000000000000000004000",
327
+ * maxUnits: 100n,
328
+ * });
329
+ * const client = createWalletClient({
330
+ * chain: base,
331
+ * transport: http(),
332
+ * });
333
+ *
334
+ * const signature = await EcrecoverRatifierUtils.sign({
335
+ * tree: Tree.create([offer]),
336
+ * client,
337
+ * account: offer.maker,
338
+ * });
339
+ * console.log(signature);
340
+ * ```
341
+ */
342
+ async function sign(params) {
343
+ const signer = typeof params.account === "string"
344
+ ? params.account
345
+ : params.account.address;
346
+ const data = typedData({
347
+ tree: params.tree,
348
+ chainId: params.client.chain.id,
349
+ });
350
+ const signature = await (0, actions_1.signTypedData)(params.client, {
351
+ account: params.account,
352
+ ...data,
353
+ });
354
+ const isValidSignature = await (0, viem_1.verifyTypedData)({
355
+ ...data,
356
+ address: signer,
357
+ signature,
358
+ });
359
+ if (!isValidSignature)
360
+ throw new errors_js_1.InvalidTypedDataSignatureError(signer);
361
+ return signature;
362
+ }
363
+ EcrecoverRatifierUtils.sign = sign;
364
+ /**
365
+ * Converts a hex ECDSA signature into the Solidity tuple shape.
366
+ *
367
+ * @param signature - Hex string or tuple signature.
368
+ * @returns Signature tuple.
369
+ * @example
370
+ * ```ts
371
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
372
+ * import { zeroHash } from "viem";
373
+ *
374
+ * const signature = EcrecoverRatifierUtils.toSignature({
375
+ * v: 27,
376
+ * r: zeroHash,
377
+ * s: zeroHash,
378
+ * });
379
+ * console.log(signature.v);
380
+ * ```
381
+ */
382
+ function toSignature(signature) {
383
+ if (typeof signature !== "string") {
384
+ return {
385
+ v: signature.v != null
386
+ ? Number(signature.v)
387
+ : Number(signature.yParity) + 27,
388
+ r: signature.r,
389
+ s: signature.s,
390
+ };
391
+ }
392
+ const parsed = (0, viem_1.parseSignature)(signature);
393
+ return {
394
+ v: Number(parsed.v ?? BigInt(parsed.yParity + 27)),
395
+ r: parsed.r,
396
+ s: parsed.s,
397
+ };
398
+ }
399
+ EcrecoverRatifierUtils.toSignature = toSignature;
400
+ /**
401
+ * Encodes EcrecoverRatifier ratifier data.
402
+ *
403
+ * Use only when you already have a signature and proof. Most maker flows call
404
+ * `ratifierData` for one leaf or `ratify` for every leaf in the tree.
405
+ *
406
+ * @param params.signature - Signature tuple encoded into the ratifier data.
407
+ * @param params.root - Merkle root approved by the signature.
408
+ * @param params.leafIndex - Leaf index proven by `params.proof`.
409
+ * @param params.proof - Merkle proof siblings for the leaf.
410
+ * @returns ABI-encoded ratifier data.
411
+ * @example
412
+ * ```ts
413
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
414
+ *
415
+ * const data = EcrecoverRatifierUtils.encodeRatifierData({
416
+ * signature: {
417
+ * v: 27,
418
+ * r: "0x0000000000000000000000000000000000000000000000000000000000000000",
419
+ * s: "0x0000000000000000000000000000000000000000000000000000000000000000",
420
+ * },
421
+ * root: "0x0000000000000000000000000000000000000000000000000000000000000000",
422
+ * leafIndex: 0n,
423
+ * proof: [],
424
+ * });
425
+ * console.log(data);
426
+ * ```
427
+ */
428
+ function encodeRatifierData(params) {
429
+ const signature = toSignature(params.signature);
430
+ return (0, viem_1.encodeAbiParameters)(signatureAbi, [
431
+ {
432
+ v: signature.v,
433
+ r: signature.r,
434
+ s: signature.s,
435
+ },
436
+ params.root,
437
+ BigInt(params.leafIndex),
438
+ params.proof,
439
+ ]);
440
+ }
441
+ EcrecoverRatifierUtils.encodeRatifierData = encodeRatifierData;
442
+ /**
443
+ * Decodes EcrecoverRatifier ratifier data.
444
+ *
445
+ * Use on the take-side or in tests after `Payload.decode` to inspect the
446
+ * proof/signature attached to a published offer.
447
+ *
448
+ * @param data - ABI-encoded ratifier data.
449
+ * @returns Decoded Ecrecover ratifier data.
450
+ * @example
451
+ * ```ts
452
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
453
+ * import { zeroHash } from "viem";
454
+ *
455
+ * const data = EcrecoverRatifierUtils.encodeRatifierData({
456
+ * signature: { v: 27, r: zeroHash, s: zeroHash },
457
+ * root: zeroHash,
458
+ * leafIndex: 0n,
459
+ * proof: [],
460
+ * });
461
+ * const decoded = EcrecoverRatifierUtils.decodeRatifierData(data);
462
+ * console.log(decoded.leafIndex);
463
+ * ```
464
+ */
465
+ function decodeRatifierData(data) {
466
+ const [signature, root, leafIndex, proof] = (0, viem_1.decodeAbiParameters)(signatureAbi, data);
467
+ return (0, morpho_ts_1.deepFreeze)({
468
+ signature: {
469
+ v: signature.v,
470
+ r: signature.r,
471
+ s: signature.s,
472
+ },
473
+ root,
474
+ leafIndex,
475
+ proof: [...proof],
476
+ });
477
+ }
478
+ EcrecoverRatifierUtils.decodeRatifierData = decodeRatifierData;
479
+ /**
480
+ * Builds one ratifier-data value for a tree leaf.
481
+ *
482
+ * Use after a tree has been signed when a caller needs data for one offer
483
+ * leaf. Use `ratify` to produce payload-ready items for the whole tree.
484
+ *
485
+ * @param params.tree - Ecrecover-ratified offer tree that produced the proof.
486
+ * @param params.leafIndex - Leaf index to prove.
487
+ * @param params.signature - Ecrecover signature for the tree root.
488
+ * @returns ABI-encoded EcrecoverRatifier data.
489
+ * @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
490
+ * @example
491
+ * ```ts
492
+ * import { EcrecoverRatifierUtils, Offer, Tree } from "@morpho-org/midnight-sdk";
493
+ * import { zeroAddress, zeroHash } from "viem";
494
+ *
495
+ * const offer = Offer.create({
496
+ * market: {
497
+ * loanToken: "0x0000000000000000000000000000000000006000",
498
+ * collateralParams: [
499
+ * {
500
+ * token: "0x0000000000000000000000000000000000007000",
501
+ * lltv: 770000000000000000n,
502
+ * maxLif: 1061007957559681697n,
503
+ * oracle: "0x0000000000000000000000000000000000008000",
504
+ * },
505
+ * ],
506
+ * maturity: 54_000n,
507
+ * rcfThreshold: 0n,
508
+ * enterGate: zeroAddress,
509
+ * liquidatorGate: zeroAddress,
510
+ * },
511
+ * buy: true,
512
+ * maker: "0x0000000000000000000000000000000000009000",
513
+ * tick: 5_000n,
514
+ * expiry: 3_600n,
515
+ * ratifier: "0x0000000000000000000000000000000000004000",
516
+ * maxUnits: 100n,
517
+ * });
518
+ * const data = EcrecoverRatifierUtils.ratifierData({
519
+ * tree: Tree.create([offer]),
520
+ * leafIndex: 0n,
521
+ * signature: { v: 27, r: zeroHash, s: zeroHash },
522
+ * });
523
+ * console.log(data);
524
+ * ```
525
+ */
526
+ function ratifierData(params) {
527
+ getTreeRatifier(params.tree);
528
+ const proof = params.tree.proof(params.leafIndex);
529
+ return encodeRatifierData({
530
+ signature: toSignature(params.signature),
531
+ root: proof.root,
532
+ leafIndex: proof.leafIndex,
533
+ proof: proof.proof,
534
+ });
535
+ }
536
+ EcrecoverRatifierUtils.ratifierData = ratifierData;
537
+ /**
538
+ * Signs or consumes a tree signature and returns payload-ready items.
539
+ *
540
+ * Use after `Tree.mempoolValidate` and before `Payload.encode`.
541
+ * The returned items preserve tree leaf order and include ratifier data
542
+ * required by takers. The group id is stored on each inline offer.
543
+ *
544
+ * @param params.tree - Ecrecover-ratified offer tree to ratify.
545
+ * @param params.client - Optional viem client whose transport signs typed data built from `params.tree`.
546
+ * @param params.account - Optional account used to sign typed data built from `params.tree`.
547
+ * @param params.signature - Optional precomputed signature for `params.tree`.
548
+ * @returns Items containing each offer and its ratifier data.
549
+ * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
550
+ * @throws {InvalidTreeHeightError} when the tree height is unsupported.
551
+ * @throws {InvalidTypedDataSignatureError} when client signing returns a signature that does not recover to `params.account`.
552
+ * @example
553
+ * ```ts
554
+ * import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
555
+ * import { Offer } from "@morpho-org/midnight-sdk";
556
+ * import { createWalletClient, http, zeroAddress } from "viem";
557
+ * import { base } from "viem/chains";
558
+ *
559
+ * const offer = Offer.create({
560
+ * market: {
561
+ * loanToken: "0x0000000000000000000000000000000000006000",
562
+ * collateralParams: [
563
+ * {
564
+ * token: "0x0000000000000000000000000000000000007000",
565
+ * lltv: 770000000000000000n,
566
+ * maxLif: 1061007957559681697n,
567
+ * oracle: "0x0000000000000000000000000000000000008000",
568
+ * },
569
+ * ],
570
+ * maturity: 54_000n,
571
+ * rcfThreshold: 0n,
572
+ * enterGate: zeroAddress,
573
+ * liquidatorGate: zeroAddress,
574
+ * },
575
+ * buy: true,
576
+ * maker: "0x0000000000000000000000000000000000009000",
577
+ * tick: 5_000n,
578
+ * expiry: 3_600n,
579
+ * ratifier: "0x0000000000000000000000000000000000004000",
580
+ * maxUnits: 100n,
581
+ * });
582
+ * const client = createWalletClient({
583
+ * chain: base,
584
+ * transport: http(),
585
+ * });
586
+ *
587
+ * const items = await EcrecoverRatifierUtils.ratify({
588
+ * tree: Tree.create([offer]),
589
+ * client,
590
+ * account: offer.maker,
591
+ * });
592
+ * console.log(items.length);
593
+ * ```
594
+ */
595
+ async function ratify(params) {
596
+ let signature;
597
+ if (params.signature != null) {
598
+ getTreeRatifier(params.tree);
599
+ signature = toSignature(params.signature);
600
+ }
601
+ else {
602
+ signature = toSignature(await sign(params));
603
+ }
604
+ const items = [];
605
+ for (const offer of params.tree.offers) {
606
+ items.push({
607
+ offer,
608
+ ratifierData: ratifierData({
609
+ tree: params.tree,
610
+ leafIndex: items.length,
611
+ signature,
612
+ }),
613
+ });
614
+ }
615
+ return items;
616
+ }
617
+ EcrecoverRatifierUtils.ratify = ratify;
618
+ })(EcrecoverRatifierUtils || (exports.EcrecoverRatifierUtils = EcrecoverRatifierUtils = {}));