@morpho-org/morpho-sdk 10.0.1 → 10.1.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/README.md +31 -10
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +5 -1
  4. package/lib/cjs/actions/blue/refinance.d.ts +1 -1
  5. package/lib/cjs/actions/blue/refinance.js +3 -8
  6. package/lib/cjs/actions/blue/refinanceValidation.d.ts +19 -0
  7. package/lib/cjs/actions/blue/refinanceValidation.js +46 -0
  8. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  9. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -7
  10. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
  11. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +4 -20
  12. package/lib/cjs/actions/requirements/getRequirementsApproval.js +7 -21
  13. package/lib/cjs/actions/requirements/validateClientChainId.d.ts +1 -26
  14. package/lib/cjs/actions/requirements/validateClientChainId.js +2 -31
  15. package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +2 -1
  16. package/lib/cjs/actions/vaultV2/forceRedeem.js +20 -2
  17. package/lib/cjs/blue/abis.d.ts +1 -1
  18. package/lib/cjs/blue/abis.js +5 -1
  19. package/lib/cjs/blue/entities.d.ts +4 -4
  20. package/lib/cjs/blue/entities.js +3 -1
  21. package/lib/cjs/blue/errors.d.ts +2 -2
  22. package/lib/cjs/blue/errors.js +5 -1
  23. package/lib/cjs/blue/fetch.d.ts +2 -2
  24. package/lib/cjs/blue/fetch.js +3 -1
  25. package/lib/cjs/blue/utils.d.ts +1 -1
  26. package/lib/cjs/blue/utils.js +2 -1
  27. package/lib/cjs/entities/blue/blue.js +24 -55
  28. package/lib/cjs/entities/index.d.ts +4 -4
  29. package/lib/cjs/entities/index.js +3 -1
  30. package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +2 -5
  31. package/lib/cjs/entities/requirements/getErc2612Permit.js +9 -58
  32. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +34 -8
  33. package/lib/cjs/entities/vaultV2/vaultV2.js +39 -1
  34. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -2
  35. package/lib/cjs/entities/vaultV2BlueReallocationData.js +6 -2
  36. package/lib/cjs/errors.d.ts +4 -4
  37. package/lib/cjs/errors.js +7 -5
  38. package/lib/cjs/fetch.d.ts +2 -2
  39. package/lib/cjs/fetch.js +3 -1
  40. package/lib/cjs/helpers/deadShares.js +3 -0
  41. package/lib/cjs/helpers/quoteBlueRepay.d.ts +16 -0
  42. package/lib/cjs/helpers/quoteBlueRepay.js +46 -0
  43. package/lib/cjs/helpers/tokenApproval.d.ts +14 -0
  44. package/lib/cjs/helpers/tokenApproval.js +20 -0
  45. package/lib/cjs/helpers/validate.d.ts +1 -1
  46. package/lib/cjs/helpers/validate.js +2 -2
  47. package/lib/cjs/index.d.ts +2 -2
  48. package/lib/cjs/index.js +13 -1
  49. package/lib/cjs/midnight/errors.d.ts +2 -1
  50. package/lib/cjs/midnight/errors.js +5 -4
  51. package/lib/cjs/reads/readErc2612Permit.d.ts +22 -0
  52. package/lib/cjs/reads/readErc2612Permit.js +77 -0
  53. package/lib/cjs/reads/validateClientChainId.d.ts +26 -0
  54. package/lib/cjs/reads/validateClientChainId.js +34 -0
  55. package/lib/cjs/transactionPlan/blue/authorization.d.ts +23 -0
  56. package/lib/cjs/transactionPlan/blue/authorization.js +128 -0
  57. package/lib/cjs/transactionPlan/blue/borrow.d.ts +80 -0
  58. package/lib/cjs/transactionPlan/blue/borrow.js +248 -0
  59. package/lib/cjs/transactionPlan/blue/prepare.d.ts +102 -0
  60. package/lib/cjs/transactionPlan/blue/prepare.js +191 -0
  61. package/lib/cjs/transactionPlan/blue/refinance.d.ts +93 -0
  62. package/lib/cjs/transactionPlan/blue/refinance.js +281 -0
  63. package/lib/cjs/transactionPlan/blue/refinanceValidation.d.ts +11 -0
  64. package/lib/cjs/transactionPlan/blue/refinanceValidation.js +20 -0
  65. package/lib/cjs/transactionPlan/blue/repay.d.ts +80 -0
  66. package/lib/cjs/transactionPlan/blue/repay.js +229 -0
  67. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
  68. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +379 -0
  69. package/lib/cjs/transactionPlan/blue/supply.d.ts +181 -0
  70. package/lib/cjs/transactionPlan/blue/supply.js +238 -0
  71. package/lib/cjs/transactionPlan/blue/supplyCollateral.d.ts +97 -0
  72. package/lib/cjs/transactionPlan/blue/supplyCollateral.js +231 -0
  73. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
  74. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +395 -0
  75. package/lib/cjs/transactionPlan/blue/withdraw.d.ts +136 -0
  76. package/lib/cjs/transactionPlan/blue/withdraw.js +340 -0
  77. package/lib/cjs/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
  78. package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +246 -0
  79. package/lib/cjs/transactionPlan/deposit.js +8 -19
  80. package/lib/cjs/transactionPlan/error.d.ts +50 -0
  81. package/lib/cjs/transactionPlan/error.js +60 -1
  82. package/lib/cjs/transactionPlan/getTokenPermit.d.ts +17 -8
  83. package/lib/cjs/transactionPlan/getTokenPermit.js +35 -7
  84. package/lib/cjs/transactionPlan/index.d.ts +21 -1
  85. package/lib/cjs/transactionPlan/index.js +27 -5
  86. package/lib/cjs/transactionPlan/intent.d.ts +2 -2
  87. package/lib/cjs/transactionPlan/intent.js +8 -4
  88. package/lib/cjs/transactionPlan/permit.d.ts +16 -2
  89. package/lib/cjs/transactionPlan/permit.js +77 -45
  90. package/lib/cjs/transactionPlan/plan.d.ts +67 -12
  91. package/lib/cjs/transactionPlan/plan.js +284 -50
  92. package/lib/cjs/transactionPlan/requirements.d.ts +14 -5
  93. package/lib/cjs/transactionPlan/requirements.js +30 -12
  94. package/lib/cjs/transactionPlan/serde.d.ts +7 -4
  95. package/lib/cjs/transactionPlan/serde.js +80 -61
  96. package/lib/cjs/transactionPlan/types.d.ts +141 -13
  97. package/lib/cjs/transactionPlan/validateActions.js +11 -1
  98. package/lib/cjs/transactionPlan/validation.d.ts +8 -0
  99. package/lib/cjs/transactionPlan/validation.js +90 -0
  100. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +1 -0
  101. package/lib/cjs/transactionPlan/vaultv1/deposit.js +1 -0
  102. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
  103. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +1 -0
  104. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
  105. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +1 -0
  106. package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +1 -0
  107. package/lib/cjs/transactionPlan/vaultv1/redeem.js +1 -0
  108. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +1 -0
  109. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +1 -0
  110. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +1 -0
  111. package/lib/cjs/transactionPlan/vaultv2/deposit.js +1 -0
  112. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
  113. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +1 -0
  114. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
  115. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +1 -0
  116. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
  117. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +1 -0
  118. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +1 -0
  119. package/lib/cjs/transactionPlan/vaultv2/redeem.js +1 -0
  120. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +1 -0
  121. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +1 -0
  122. package/lib/cjs/utils.d.ts +2 -2
  123. package/lib/cjs/utils.js +4 -2
  124. package/lib/cjs/version.d.ts +1 -1
  125. package/lib/cjs/version.js +1 -1
  126. package/lib/esm/abis.d.ts +1 -1
  127. package/lib/esm/abis.js +1 -1
  128. package/lib/esm/actions/blue/refinance.d.ts +1 -1
  129. package/lib/esm/actions/blue/refinance.js +4 -9
  130. package/lib/esm/actions/blue/refinanceValidation.d.ts +19 -0
  131. package/lib/esm/actions/blue/refinanceValidation.js +41 -0
  132. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  133. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -8
  134. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
  135. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -21
  136. package/lib/esm/actions/requirements/getRequirementsApproval.js +7 -21
  137. package/lib/esm/actions/requirements/validateClientChainId.d.ts +1 -26
  138. package/lib/esm/actions/requirements/validateClientChainId.js +1 -30
  139. package/lib/esm/actions/vaultV2/forceRedeem.d.ts +2 -1
  140. package/lib/esm/actions/vaultV2/forceRedeem.js +20 -2
  141. package/lib/esm/blue/abis.d.ts +1 -1
  142. package/lib/esm/blue/abis.js +1 -1
  143. package/lib/esm/blue/entities.d.ts +4 -4
  144. package/lib/esm/blue/entities.js +2 -2
  145. package/lib/esm/blue/errors.d.ts +2 -2
  146. package/lib/esm/blue/errors.js +2 -2
  147. package/lib/esm/blue/fetch.d.ts +2 -2
  148. package/lib/esm/blue/fetch.js +2 -2
  149. package/lib/esm/blue/utils.d.ts +1 -1
  150. package/lib/esm/blue/utils.js +1 -1
  151. package/lib/esm/entities/blue/blue.js +26 -57
  152. package/lib/esm/entities/index.d.ts +4 -4
  153. package/lib/esm/entities/index.js +2 -2
  154. package/lib/esm/entities/requirements/getErc2612Permit.d.ts +2 -5
  155. package/lib/esm/entities/requirements/getErc2612Permit.js +9 -58
  156. package/lib/esm/entities/vaultV2/vaultV2.d.ts +34 -8
  157. package/lib/esm/entities/vaultV2/vaultV2.js +40 -2
  158. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -2
  159. package/lib/esm/entities/vaultV2BlueReallocationData.js +6 -2
  160. package/lib/esm/errors.d.ts +4 -4
  161. package/lib/esm/errors.js +4 -4
  162. package/lib/esm/fetch.d.ts +2 -2
  163. package/lib/esm/fetch.js +2 -2
  164. package/lib/esm/helpers/deadShares.js +4 -1
  165. package/lib/esm/helpers/quoteBlueRepay.d.ts +16 -0
  166. package/lib/esm/helpers/quoteBlueRepay.js +42 -0
  167. package/lib/esm/helpers/tokenApproval.d.ts +14 -0
  168. package/lib/esm/helpers/tokenApproval.js +16 -0
  169. package/lib/esm/helpers/validate.d.ts +1 -1
  170. package/lib/esm/helpers/validate.js +3 -3
  171. package/lib/esm/index.d.ts +2 -2
  172. package/lib/esm/index.js +1 -1
  173. package/lib/esm/midnight/errors.d.ts +2 -1
  174. package/lib/esm/midnight/errors.js +2 -1
  175. package/lib/esm/reads/readErc2612Permit.d.ts +22 -0
  176. package/lib/esm/reads/readErc2612Permit.js +73 -0
  177. package/lib/esm/reads/validateClientChainId.d.ts +26 -0
  178. package/lib/esm/reads/validateClientChainId.js +30 -0
  179. package/lib/esm/transactionPlan/blue/authorization.d.ts +23 -0
  180. package/lib/esm/transactionPlan/blue/authorization.js +122 -0
  181. package/lib/esm/transactionPlan/blue/borrow.d.ts +80 -0
  182. package/lib/esm/transactionPlan/blue/borrow.js +243 -0
  183. package/lib/esm/transactionPlan/blue/prepare.d.ts +102 -0
  184. package/lib/esm/transactionPlan/blue/prepare.js +187 -0
  185. package/lib/esm/transactionPlan/blue/refinance.d.ts +93 -0
  186. package/lib/esm/transactionPlan/blue/refinance.js +276 -0
  187. package/lib/esm/transactionPlan/blue/refinanceValidation.d.ts +11 -0
  188. package/lib/esm/transactionPlan/blue/refinanceValidation.js +17 -0
  189. package/lib/esm/transactionPlan/blue/repay.d.ts +80 -0
  190. package/lib/esm/transactionPlan/blue/repay.js +223 -0
  191. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
  192. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +373 -0
  193. package/lib/esm/transactionPlan/blue/supply.d.ts +181 -0
  194. package/lib/esm/transactionPlan/blue/supply.js +232 -0
  195. package/lib/esm/transactionPlan/blue/supplyCollateral.d.ts +97 -0
  196. package/lib/esm/transactionPlan/blue/supplyCollateral.js +225 -0
  197. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
  198. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +389 -0
  199. package/lib/esm/transactionPlan/blue/withdraw.d.ts +136 -0
  200. package/lib/esm/transactionPlan/blue/withdraw.js +334 -0
  201. package/lib/esm/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
  202. package/lib/esm/transactionPlan/blue/withdrawCollateral.js +240 -0
  203. package/lib/esm/transactionPlan/deposit.js +9 -20
  204. package/lib/esm/transactionPlan/error.d.ts +50 -0
  205. package/lib/esm/transactionPlan/error.js +57 -0
  206. package/lib/esm/transactionPlan/getTokenPermit.d.ts +17 -8
  207. package/lib/esm/transactionPlan/getTokenPermit.js +35 -7
  208. package/lib/esm/transactionPlan/index.d.ts +21 -1
  209. package/lib/esm/transactionPlan/index.js +11 -1
  210. package/lib/esm/transactionPlan/intent.d.ts +2 -2
  211. package/lib/esm/transactionPlan/intent.js +8 -4
  212. package/lib/esm/transactionPlan/permit.d.ts +16 -2
  213. package/lib/esm/transactionPlan/permit.js +76 -44
  214. package/lib/esm/transactionPlan/plan.d.ts +67 -12
  215. package/lib/esm/transactionPlan/plan.js +279 -46
  216. package/lib/esm/transactionPlan/requirements.d.ts +14 -5
  217. package/lib/esm/transactionPlan/requirements.js +30 -12
  218. package/lib/esm/transactionPlan/serde.d.ts +7 -4
  219. package/lib/esm/transactionPlan/serde.js +73 -54
  220. package/lib/esm/transactionPlan/types.d.ts +141 -13
  221. package/lib/esm/transactionPlan/validateActions.js +11 -1
  222. package/lib/esm/transactionPlan/validation.d.ts +8 -0
  223. package/lib/esm/transactionPlan/validation.js +88 -1
  224. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +1 -0
  225. package/lib/esm/transactionPlan/vaultv1/deposit.js +1 -0
  226. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
  227. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +1 -0
  228. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
  229. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +1 -0
  230. package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +1 -0
  231. package/lib/esm/transactionPlan/vaultv1/redeem.js +1 -0
  232. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +1 -0
  233. package/lib/esm/transactionPlan/vaultv1/withdraw.js +1 -0
  234. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +1 -0
  235. package/lib/esm/transactionPlan/vaultv2/deposit.js +1 -0
  236. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
  237. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +1 -0
  238. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
  239. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +1 -0
  240. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
  241. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +1 -0
  242. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +1 -0
  243. package/lib/esm/transactionPlan/vaultv2/redeem.js +1 -0
  244. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +1 -0
  245. package/lib/esm/transactionPlan/vaultv2/withdraw.js +1 -0
  246. package/lib/esm/utils.d.ts +2 -2
  247. package/lib/esm/utils.js +2 -2
  248. package/lib/esm/version.d.ts +1 -1
  249. package/lib/esm/version.js +1 -1
  250. package/package.json +7 -7
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.applySignatures = applySignatures;
4
+ exports.verifyPlanSignature = verifyPlanSignature;
4
5
  exports.getPendingSignatureRequests = getPendingSignatureRequests;
5
6
  exports.buildTransaction = buildTransaction;
6
7
  exports.buildTransactions = buildTransactions;
@@ -11,30 +12,43 @@ const accounts_1 = require("viem/accounts");
11
12
  const common_js_1 = require("../actions/bundles/common.js");
12
13
  const index_js_1 = require("../actions/index.js");
13
14
  const error_js_1 = require("../types/error.js");
15
+ const authorization_js_1 = require("./blue/authorization.js");
16
+ const borrow_js_1 = require("./blue/borrow.js");
17
+ const refinance_js_1 = require("./blue/refinance.js");
18
+ const repay_js_1 = require("./blue/repay.js");
19
+ const repayWithdrawCollateral_js_1 = require("./blue/repayWithdrawCollateral.js");
20
+ const supply_js_1 = require("./blue/supply.js");
21
+ const supplyCollateral_js_1 = require("./blue/supplyCollateral.js");
22
+ const supplyCollateralBorrow_js_1 = require("./blue/supplyCollateralBorrow.js");
23
+ const withdraw_js_1 = require("./blue/withdraw.js");
24
+ const withdrawCollateral_js_1 = require("./blue/withdrawCollateral.js");
14
25
  const error_js_2 = require("./error.js");
15
26
  const permit_js_1 = require("./permit.js");
16
27
  const validateActions_js_1 = require("./validateActions.js");
28
+ const validation_js_1 = require("./validation.js");
17
29
  const deposit_js_1 = require("./vaultv1/deposit.js");
18
30
  const inKindRedeem_js_1 = require("./vaultv1/inKindRedeem.js");
19
31
  const migrateToV2_js_1 = require("./vaultv1/migrateToV2.js");
20
32
  const redeem_js_1 = require("./vaultv1/redeem.js");
21
- const withdraw_js_1 = require("./vaultv1/withdraw.js");
33
+ const withdraw_js_2 = require("./vaultv1/withdraw.js");
22
34
  const deposit_js_2 = require("./vaultv2/deposit.js");
23
35
  const forceRedeem_js_1 = require("./vaultv2/forceRedeem.js");
24
36
  const forceWithdraw_js_1 = require("./vaultv2/forceWithdraw.js");
25
37
  const inKindRedeem_js_2 = require("./vaultv2/inKindRedeem.js");
26
38
  const redeem_js_2 = require("./vaultv2/redeem.js");
27
- const withdraw_js_2 = require("./vaultv2/withdraw.js");
39
+ const withdraw_js_3 = require("./vaultv2/withdraw.js");
28
40
  const version_js_1 = require("./version.js");
29
41
  /**
30
42
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
31
43
  * Validates every request against its actions, verifies supplied ECDSA signatures synchronously,
32
44
  * and attaches serialized signatures by request ID. No client or RPC is used; the EIP-712 domain
33
- * binds the signature to its chain. Only low-s compact or serialized ECDSA permits are supported.
45
+ * binds the signature to its chain. Only low-s compact or serialized ECDSA signatures are supported.
34
46
  * Contract wallets should use `supportSignature: false` for direct approvals: deployed bundles
35
47
  * consume ECDSA `v/r/s`, not arbitrary ERC-1271 or ERC-6492 signatures.
36
48
  * Stored inputs remain trusted; verification does not establish the integrator's intent or protect
37
- * a plan changed afterwards. Builders do not reverify signatures in restored plans.
49
+ * unsigned fields from tampering. Stored signatures are reverified offline at parse, inspection and build.
50
+ * Permits bind owner, token, spender, value, nonce, deadline and chain; Morpho authorizations bind
51
+ * owner, authorized address, nonce, deadline and chain, not operation amounts, markets or referral fees/recipients.
38
52
  * @param plan - Plan to update without mutating its requests or steps.
39
53
  * @param signatures - Compact or serialized ECDSA signatures, each naming a unique existing request ID.
40
54
  * @returns A new plan sharing steps and unpatched requests; omitted IDs are preserved. Empty input validates all requests and returns the original plan.
@@ -42,7 +56,8 @@ const version_js_1 = require("./version.js");
42
56
  * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests or updates.
43
57
  * @throws {TransactionPlanSignatureMismatchError} If a request disagrees with its actions, an ID is unknown, or an input ID is duplicated (`field: "duplicate"`).
44
58
  * @throws {BundlesPermitMismatchError} If a supplied signature cannot be parsed by the bundle permit builders.
45
- * @throws {InvalidSignatureError} If a signature has high s, cannot recover a public key, or does not match the permit owner and typed data.
59
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
60
+ * @throws {InvalidSignatureError} If a signature has high s, cannot recover a public key, or does not match the request owner and typed data.
46
61
  * @example
47
62
  * ```ts
48
63
  * import { applySignatures, type TransactionPlan } from "@morpho-org/morpho-sdk";
@@ -56,13 +71,15 @@ function applySignatures(plan, signatures) {
56
71
  (0, version_js_1.validateTransactionPlanVersion)(plan.version);
57
72
  // Reject unknown actions before reading signature requests or updates.
58
73
  (0, validateActions_js_1.validateTransactionPlanActions)(plan.steps);
59
- const requests = getPendingSignatureRequests({
74
+ const updatedIds = new Set(signatures.map(({ id }) => id));
75
+ // Validate all requests and stored signatures that are not being replaced.
76
+ getPendingSignatureRequests({
60
77
  ...plan,
61
- signatureRequests: plan.signatureRequests.map((request) => ({
62
- ...request,
63
- signature: undefined,
64
- })),
78
+ signatureRequests: plan.signatureRequests.map((request) => updatedIds.has(request.id)
79
+ ? { ...request, signature: undefined }
80
+ : request),
65
81
  });
82
+ const requests = plan.signatureRequests;
66
83
  const updates = new Map();
67
84
  for (const { id, signature } of signatures) {
68
85
  if (!requests.some((request) => request.id === id)) {
@@ -81,25 +98,7 @@ function applySignatures(plan, signatures) {
81
98
  if (request === undefined) {
82
99
  throw new error_js_2.TransactionPlanSignatureMismatchError(id, "reference");
83
100
  }
84
- const normalized = (0, common_js_1.normalizeBundlesSignature)(signature);
85
- let recovered;
86
- try {
87
- const ecdsa = new secp256k1_1.secp256k1.Signature(BigInt(normalized.r), BigInt(normalized.s));
88
- if (ecdsa.hasHighS()) {
89
- throw new error_js_1.InvalidSignatureError();
90
- }
91
- const publicKey = ecdsa
92
- .addRecoveryBit(normalized.v - 27)
93
- .recoverPublicKey((0, viem_1.hashTypedData)(request.typedData).slice(2));
94
- recovered = (0, accounts_1.publicKeyToAddress)((0, viem_1.toHex)(publicKey.toRawBytes(false)));
95
- }
96
- catch {
97
- throw new error_js_1.InvalidSignatureError();
98
- }
99
- if (!(0, viem_1.isAddressEqual)(recovered, request.typedData.message.owner)) {
100
- throw new error_js_1.InvalidSignatureError();
101
- }
102
- updates.set(id, (0, viem_1.serializeSignature)({ ...normalized, v: BigInt(normalized.v) }));
101
+ updates.set(id, verifyPlanSignature(request, signature));
103
102
  }
104
103
  return {
105
104
  ...plan,
@@ -109,6 +108,31 @@ function applySignatures(plan, signatures) {
109
108
  }),
110
109
  };
111
110
  }
111
+ /** @internal Verifies and normalizes a signature offline after the request has been validated against its action. */
112
+ function verifyPlanSignature(request, signature) {
113
+ const normalized = (0, common_js_1.normalizeBundlesSignature)(signature);
114
+ let recovered;
115
+ try {
116
+ const ecdsa = new secp256k1_1.secp256k1.Signature(BigInt(normalized.r), BigInt(normalized.s));
117
+ if (ecdsa.hasHighS()) {
118
+ throw new error_js_1.InvalidSignatureError();
119
+ }
120
+ const publicKey = ecdsa
121
+ .addRecoveryBit(normalized.v - 27)
122
+ .recoverPublicKey((0, viem_1.hashTypedData)(request.typedData).slice(2));
123
+ recovered = (0, accounts_1.publicKeyToAddress)((0, viem_1.toHex)(publicKey.toRawBytes(false)));
124
+ }
125
+ catch {
126
+ throw new error_js_1.InvalidSignatureError();
127
+ }
128
+ const signer = request.kind === "permit"
129
+ ? request.typedData.message.owner
130
+ : request.args.owner;
131
+ if (!(0, viem_1.isAddressEqual)(recovered, signer)) {
132
+ throw new error_js_1.InvalidSignatureError();
133
+ }
134
+ return (0, viem_1.serializeSignature)({ ...normalized, v: BigInt(normalized.v) });
135
+ }
112
136
  /** @internal Lists the signature request IDs a step's action names, in role order. */
113
137
  function getStepSignatureRequestIds(step) {
114
138
  const { action } = step;
@@ -117,16 +141,131 @@ function getStepSignatureRequestIds(step) {
117
141
  }
118
142
  return Object.values(action.signatureRequestIds).filter((id) => id !== undefined);
119
143
  }
144
+ function actionDataKeys(value) {
145
+ const keys = Object.keys(value);
146
+ return Array.isArray(value)
147
+ ? keys
148
+ : keys.filter((key) => value[key] !== undefined);
149
+ }
150
+ function equalActionData(left, right) {
151
+ const pending = [[left, right]];
152
+ const compared = new WeakMap();
153
+ for (const [a, b] of pending) {
154
+ if (Object.is(a, b)) {
155
+ continue;
156
+ }
157
+ if (typeof a !== "object" ||
158
+ a === null ||
159
+ typeof b !== "object" ||
160
+ b === null) {
161
+ return false;
162
+ }
163
+ if (Array.isArray(a) || Array.isArray(b)) {
164
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
165
+ return false;
166
+ }
167
+ }
168
+ else {
169
+ const prototype = Object.getPrototypeOf(a);
170
+ if ((prototype !== Object.prototype && prototype !== null) ||
171
+ prototype !== Object.getPrototypeOf(b)) {
172
+ return false;
173
+ }
174
+ }
175
+ const matches = compared.get(a) ?? new WeakSet();
176
+ if (matches.has(b)) {
177
+ continue;
178
+ }
179
+ matches.add(b);
180
+ compared.set(a, matches);
181
+ const aFields = a;
182
+ const bFields = b;
183
+ const keys = actionDataKeys(aFields);
184
+ if (keys.length !== actionDataKeys(bFields).length) {
185
+ return false;
186
+ }
187
+ for (const key of keys) {
188
+ if (!Object.prototype.propertyIsEnumerable.call(b, key)) {
189
+ return false;
190
+ }
191
+ pending.push([aFields[key], bFields[key]]);
192
+ }
193
+ }
194
+ return true;
195
+ }
196
+ function validateSignatureRequest(request, action) {
197
+ if (action.type === "blueSupplyCollateralBorrow") {
198
+ return (0, supplyCollateralBorrow_js_1.validateSupplyCollateralBorrowRequest)(request, action);
199
+ }
200
+ if (request.kind === "morphoAuthorization" &&
201
+ action.type === "blueWithdraw") {
202
+ return (0, withdraw_js_1.validateBlueWithdrawAuthorizationRequest)(request, action);
203
+ }
204
+ if (request.kind === "morphoAuthorization" &&
205
+ action.type === "blueWithdrawCollateral") {
206
+ return (0, withdrawCollateral_js_1.validateBlueWithdrawCollateralAuthorizationRequest)(request, action);
207
+ }
208
+ if (request.kind === "morphoAuthorization" &&
209
+ action.type === "blueBorrow" &&
210
+ action.signatureRequestIds.morphoAuthorization === request.id) {
211
+ return (0, authorization_js_1.validateMorphoAuthorizationRequest)(request, {
212
+ ...action.inputs,
213
+ nonce: action.inputs.authorizationNonce,
214
+ });
215
+ }
216
+ if (request.kind === "permit" && action.type === "blueSupplyCollateral") {
217
+ return (0, supplyCollateral_js_1.validateSupplyCollateralPermitRequest)(request, action);
218
+ }
219
+ if (request.kind === "permit" &&
220
+ action.type === "blueSupply" &&
221
+ action.signatureRequestIds.tokenPermit === request.id) {
222
+ return (0, supply_js_1.validateBlueSupplyPermitRequest)(request, action);
223
+ }
224
+ if (request.kind === "permit" && action.type === "blueRepay") {
225
+ return (0, repay_js_1.validateBlueRepayPermit)(request, action);
226
+ }
227
+ if (action.type === "blueRepayWithdrawCollateral") {
228
+ return (0, repayWithdrawCollateral_js_1.validateRepayWithdrawCollateralRequest)(request, action);
229
+ }
230
+ if (request.kind === "morphoAuthorization" &&
231
+ action.type === "blueRefinance" &&
232
+ action.signatureRequestIds.morphoAuthorization === request.id &&
233
+ action.inputs.authorizationNonce !== undefined) {
234
+ return (0, authorization_js_1.validateMorphoAuthorizationRequest)(request, {
235
+ ...action.inputs,
236
+ nonce: action.inputs.authorizationNonce,
237
+ });
238
+ }
239
+ if (request.kind === "permit" &&
240
+ (action.type === "vaultV2Deposit" ||
241
+ action.type === "vaultV1Deposit" ||
242
+ action.type === "vaultV1Withdraw" ||
243
+ action.type === "vaultV2Withdraw" ||
244
+ action.type === "vaultV2Redeem" ||
245
+ action.type === "vaultV1Redeem" ||
246
+ action.type === "vaultV2ForceWithdraw" ||
247
+ action.type === "vaultV1MigrateToV2" ||
248
+ action.type === "vaultV2InKindRedeem" ||
249
+ action.type === "vaultV1InKindRedeem")) {
250
+ (0, permit_js_1.validateVaultPermitRequest)(request, action);
251
+ return;
252
+ }
253
+ throw new error_js_2.TransactionPlanSignatureMismatchError(request.id, "reference");
254
+ }
120
255
  /**
121
256
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
122
- * Lists pending signature requests once per request ID after checking each against every referencing action.
257
+ * Lists pending signature requests once per request ID and validates them against their referencing actions.
258
+ * Signed requests are also checked and their signers recovered offline.
123
259
  * Sign each request's typed data with your wallet stack, then verify and attach the results with
124
260
  * {@link applySignatures}.
125
261
  * @param plan - Plan to inspect without changing it.
126
262
  * @returns Pending requests in first-reference order, each carrying its `id`, `kind` and validated `typedData`. Dangling IDs are omitted. The SDK never signs.
263
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
264
+ * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
265
+ * @throws {BundlesPermitMismatchError} If a stored signature cannot be parsed as a compact or serialized ECDSA signature.
127
266
  * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported, including empty plans.
128
267
  * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests.
129
- * @throws {TransactionPlanSignatureMismatchError} If a pending permit disagrees with its action or arguments, including the action deadline, or any request has no referencing step.
268
+ * @throws {TransactionPlanSignatureMismatchError} If a pending request or Morpho authorization disagrees with its action or arguments, or any request has no referencing step.
130
269
  * @example
131
270
  * ```ts
132
271
  * import { getPendingSignatureRequests, type TransactionPlan } from "@morpho-org/morpho-sdk";
@@ -139,9 +278,14 @@ function getPendingSignatureRequests(plan) {
139
278
  (0, version_js_1.validateTransactionPlanVersion)(plan.version);
140
279
  // Reject unknown actions before exposing signature requests.
141
280
  (0, validateActions_js_1.validateTransactionPlanActions)(plan.steps);
281
+ // Validate shared context before resolving requests.
282
+ (0, validation_js_1.validateTransactionPlanHeader)(plan);
142
283
  const referenced = new Map();
143
284
  for (const step of plan.steps) {
144
285
  for (const id of getStepSignatureRequestIds(step)) {
286
+ if (referenced.has(id)) {
287
+ throw new error_js_2.TransactionPlanSignatureMismatchError(id, "reference");
288
+ }
145
289
  referenced.set(id, [...(referenced.get(id) ?? []), step.action]);
146
290
  }
147
291
  }
@@ -150,14 +294,12 @@ function getPendingSignatureRequests(plan) {
150
294
  if (actions === undefined) {
151
295
  throw new error_js_2.TransactionPlanSignatureMismatchError(request.id, "reference");
152
296
  }
153
- if (request.signature === undefined) {
154
- for (const action of actions) {
155
- (0, permit_js_1.validateSignatureRequest)(request, action);
156
- if ("signatureRequestIds" in action &&
157
- request.args.deadline !== action.inputs.deadline) {
158
- throw new error_js_2.TransactionPlanSignatureMismatchError(request.id, "args.deadline");
159
- }
160
- }
297
+ for (const action of actions) {
298
+ validateSignatureRequest(request, action);
299
+ }
300
+ if (request.signature !== undefined) {
301
+ // Recover only after the request has been bound to its action account.
302
+ verifyPlanSignature(request, request.signature);
161
303
  }
162
304
  }
163
305
  return [...referenced.keys()].flatMap((id) => {
@@ -173,6 +315,24 @@ function encodeStep(action, requests) {
173
315
  return (0, migrateToV2_js_1.buildVaultV1MigrateToV2)(action, requests);
174
316
  case "vaultV1InKindRedeem":
175
317
  return (0, inKindRedeem_js_1.buildVaultV1InKindRedeem)(action, requests);
318
+ case "blueSupply":
319
+ return (0, supply_js_1.buildBlueSupply)(action, requests);
320
+ case "blueSupplyCollateral":
321
+ return (0, supplyCollateral_js_1.buildBlueSupplyCollateral)(action, requests);
322
+ case "blueBorrow":
323
+ return (0, borrow_js_1.buildBlueBorrow)(action, requests);
324
+ case "blueSupplyCollateralBorrow":
325
+ return (0, supplyCollateralBorrow_js_1.buildBlueSupplyCollateralBorrow)(action, requests);
326
+ case "blueRepay":
327
+ return (0, repay_js_1.buildBlueRepay)(action, requests);
328
+ case "blueWithdraw":
329
+ return (0, withdraw_js_1.buildBlueWithdraw)(action, requests);
330
+ case "blueWithdrawCollateral":
331
+ return (0, withdrawCollateral_js_1.buildBlueWithdrawCollateral)(action, requests);
332
+ case "blueRepayWithdrawCollateral":
333
+ return (0, repayWithdrawCollateral_js_1.buildBlueRepayWithdrawCollateral)(action, requests);
334
+ case "blueRefinance":
335
+ return (0, refinance_js_1.buildBlueRefinance)(action, requests);
176
336
  case "erc20Approval":
177
337
  return (0, index_js_1.encodeErc20Approval)({
178
338
  ...action.inputs,
@@ -180,16 +340,18 @@ function encodeStep(action, requests) {
180
340
  });
181
341
  case "vaultV1Deposit":
182
342
  return (0, deposit_js_1.buildVaultV1Deposit)(action, requests);
343
+ case "morphoAuthorization":
344
+ return (0, authorization_js_1.buildMorphoAuthorization)(action);
183
345
  case "vaultV2Deposit":
184
346
  return (0, deposit_js_2.buildVaultV2Deposit)(action, requests);
185
347
  case "vaultV2Withdraw":
186
- return (0, withdraw_js_2.buildVaultV2Withdraw)(action, requests);
348
+ return (0, withdraw_js_3.buildVaultV2Withdraw)(action, requests);
187
349
  case "vaultV2Redeem":
188
350
  return (0, redeem_js_2.buildVaultV2Redeem)(action, requests);
189
351
  case "vaultV2ForceWithdraw":
190
352
  return (0, forceWithdraw_js_1.buildVaultV2ForceWithdraw)(action, requests);
191
353
  case "vaultV1Withdraw":
192
- return (0, withdraw_js_1.buildVaultV1Withdraw)(action, requests);
354
+ return (0, withdraw_js_2.buildVaultV1Withdraw)(action, requests);
193
355
  case "vaultV1Redeem":
194
356
  return (0, redeem_js_1.buildVaultV1Redeem)(action, requests);
195
357
  case "vaultV2InKindRedeem":
@@ -202,23 +364,47 @@ function encodeStep(action, requests) {
202
364
  * Plans are trusted as given, whether restored or supplied directly. Signature checks establish
203
365
  * only request/action consistency; they do not validate stored fees, recipients, share-price bounds,
204
366
  * vaults or amounts against the intended operation. Integrators must protect stored plans from tampering.
205
- * This builder does not verify signature ownership; use {@link applySignatures} to verify and attach signatures.
367
+ * Stored signatures are verified offline. Permits bind owner, token, spender, value, nonce, deadline
368
+ * and chain; Morpho authorizations bind owner, authorized address, nonce, deadline and chain,
369
+ * not operation amounts, market parameters or referral fees/recipients. Protect those unsigned fields.
206
370
  * @param plan - Plan supplying referenced signature requests and the calldata origin; chain and account come from the action inputs.
207
371
  * @param step - Recipe to encode; missing signatures on unrelated supported steps do not block it.
208
372
  * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set.
373
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
374
+ * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
209
375
  * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported.
210
376
  * @throws {UnsupportedTransactionPlanActionError} If any plan step or the selected step names an unsupported action type.
211
377
  * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
212
378
  * @throws {SameVaultMigrationError} If a migration's source and destination vaults are identical.
213
379
  * @throws {AmountAndSharesExclusiveError} If a migration supplies both `assets` and `shares`, or neither.
214
- * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a deadline, force-redeem shares or deallocation amount is not positive.
380
+ * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a Blue collateral amount, a deadline, force-redeem shares or deallocation amount is not positive.
381
+ * @throws {MarketIdMismatchError} If a Blue collateral step's market ID doesn't match its market parameters.
382
+ * @throws {NativeAmountOnNonWNativeAssetError} If a native-funded Blue collateral step targets a token other than wNative.
383
+ * @throws {ChainWNativeMissingError} If a native-funded Blue collateral step targets a chain without registered wNative.
215
384
  * @throws {EmptyDeallocationsError} If a force-redeem step has no deallocations.
216
385
  * @throws {EmptyMarketParamsListError} If a Vault V1 in-kind redemption step has no markets.
217
386
  * @throws {ReferralFeePctExceededError} If the action's referral fee is at least WAD.
218
387
  * @throws {ReferralFeeRecipientMissingError} If a positive referral fee has no nonzero recipient.
219
388
  * @throws {IncompleteTransactionPlanError} If a referenced signature request or its signature is missing.
220
- * @throws {TransactionPlanSignatureMismatchError} If a referenced permit disagrees with the step's action or its encoding arguments.
221
- * @throws {BundlesPermitMismatchError} If a signed permit's spender, amount or deadline doesn't match the step.
389
+ * @throws {TransactionPlanSignatureMismatchError} If a referenced request disagrees with the step's action or its encoding arguments.
390
+ * @throws {BundlesPermitMismatchError} If a stored signature cannot be parsed or a signed permit's encoding arguments do not match the step.
391
+ * @throws {MarketIdMismatchError} If a Blue market ID differs from its parameter tuple.
392
+ * @throws {NegativeInputError} If a Blue amount, native value, LTV bound or referral fee is negative.
393
+ * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, or withdrawal-only funding is nonzero.
394
+ * @throws {MutuallyExclusiveRepayAmountsError} If a Blue repayment specifies both nonzero assets and shares.
395
+ * @throws {MaxRepayAssetsBelowRepayAssetsError} If exact-asset funding cannot cover repayment plus referral fees.
396
+ * @throws {NativeFundingAmountMismatchError} If Blue native funding does not equal the full repayment cap.
397
+ * @throws {ChainWNativeMissingError} If native funding targets a chain without wNative.
398
+ * @throws {NativeAmountOnNonWNativeAssetError} If native funding targets a loan token other than wNative.
399
+ * @throws {UnexpectedRequirementSignatureError} If a Blue signature targets an inactive leg or native-funded token pull.
400
+ * @throws {DepositOwnerMismatchError} If a Blue signature belongs to another account.
401
+ * @throws {DepositAssetMismatchError} If a token signature names another loan token.
402
+ * @throws {DepositAmountMismatchError} If a token signature does not cover the repayment cap.
403
+ * @throws {DepositSpenderMismatchError} If a token signature names another spender.
404
+ * @throws {UnsupportedRequirementSignatureError} If a token signature has an unsupported action type.
405
+ * @throws {BundlesRequirementSignatureMismatchError} If a Blue signature cannot be bound or parsed safely.
406
+ * @throws {UnsupportedChainIdError} If a Blue action's chain is absent from the registry.
407
+ * @throws {UnknownAddressError} If BlueBundlesV1 is not registered on the action's chain.
222
408
  * @example
223
409
  * ```ts
224
410
  * import { buildTransaction, parseTransactionPlan } from "@morpho-org/morpho-sdk";
@@ -232,13 +418,37 @@ function buildTransaction(plan, step) {
232
418
  // Reject unknown formats before interpreting stored fields.
233
419
  (0, version_js_1.validateTransactionPlanVersion)(plan.version);
234
420
  // Check the whole plan and any separately selected step before reading requests.
235
- (0, validateActions_js_1.validateTransactionPlanActions)([...plan.steps, step]);
421
+ const isOriginalStep = plan.steps.includes(step);
422
+ const steps = isOriginalStep ? plan.steps : [...plan.steps, step];
423
+ (0, validateActions_js_1.validateTransactionPlanActions)(steps);
424
+ // Validate the plan and the separately selected step without requiring unrelated signatures.
425
+ (0, validation_js_1.validateTransactionPlanHeader)(plan);
426
+ const referencedIds = new Set(plan.steps.flatMap(getStepSignatureRequestIds));
427
+ getPendingSignatureRequests({
428
+ ...plan,
429
+ signatureRequests: plan.signatureRequests.filter((request) => referencedIds.has(request.id)),
430
+ });
431
+ if (!isOriginalStep &&
432
+ !plan.steps.some((candidate) => candidate.id === step.id &&
433
+ equalActionData(candidate.action, step.action))) {
434
+ for (const id of getStepSignatureRequestIds(step)) {
435
+ if (referencedIds.has(id)) {
436
+ throw new error_js_2.TransactionPlanSignatureMismatchError(id, "reference");
437
+ }
438
+ referencedIds.add(id);
439
+ }
440
+ }
441
+ (0, validation_js_1.validateTransactionPlanHeader)({ ...plan, steps: [step] });
236
442
  const requests = getStepSignatureRequestIds(step).map((id) => {
237
443
  const request = plan.signatureRequests.find((candidate) => candidate.id === id);
238
444
  if (request === undefined) {
239
445
  throw new error_js_2.IncompleteTransactionPlanError(id);
240
446
  }
241
- (0, permit_js_1.validateSignatureRequest)(request, step.action);
447
+ validateSignatureRequest(request, step.action);
448
+ if (request.signature !== undefined) {
449
+ // The selected step may not be present in the plan's step list.
450
+ verifyPlanSignature(request, request.signature);
451
+ }
242
452
  return request;
243
453
  });
244
454
  const { to, value, data, action } = encodeStep(step.action, requests);
@@ -256,19 +466,41 @@ function buildTransaction(plan, step) {
256
466
  * Builds every step of the plan in order.
257
467
  * @param plan - Plan whose referenced signature requests all carry a signature.
258
468
  * @returns Frozen SDK transactions in step order; discriminate on `action.type` for per-step handling.
469
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
470
+ * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
259
471
  * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported, including empty plans.
260
472
  * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests.
261
473
  * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
262
474
  * @throws {SameVaultMigrationError} If a migration's source and destination vaults are identical.
263
475
  * @throws {AmountAndSharesExclusiveError} If a migration supplies both `assets` and `shares`, or neither.
264
- * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a deadline, force-redeem shares or deallocation amount is not positive.
476
+ * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a Blue collateral amount, a deadline, force-redeem shares or deallocation amount is not positive.
477
+ * @throws {MarketIdMismatchError} If a Blue collateral step's market ID doesn't match its market parameters.
478
+ * @throws {NativeAmountOnNonWNativeAssetError} If a native-funded Blue collateral step targets a token other than wNative.
479
+ * @throws {ChainWNativeMissingError} If a native-funded Blue collateral step targets a chain without registered wNative.
265
480
  * @throws {EmptyDeallocationsError} If a force-redeem step has no deallocations.
266
481
  * @throws {EmptyMarketParamsListError} If a Vault V1 in-kind redemption step has no markets.
267
482
  * @throws {ReferralFeePctExceededError} If an action's referral fee is at least WAD.
268
483
  * @throws {ReferralFeeRecipientMissingError} If a positive referral fee has no nonzero recipient.
269
484
  * @throws {IncompleteTransactionPlanError} If any step references a signature request or signature that is missing.
270
- * @throws {TransactionPlanSignatureMismatchError} If a referenced permit disagrees with its action or encoding arguments.
271
- * @throws {BundlesPermitMismatchError} If a signed permit's spender, amount or deadline doesn't match the step.
485
+ * @throws {TransactionPlanSignatureMismatchError} If a referenced request disagrees with its action or encoding arguments.
486
+ * @throws {BundlesPermitMismatchError} If a stored signature cannot be parsed or a signed permit's encoding arguments do not match the step.
487
+ * @throws {MarketIdMismatchError} If a Blue market ID differs from its parameter tuple.
488
+ * @throws {NegativeInputError} If a Blue amount, native value, LTV bound or referral fee is negative.
489
+ * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, or withdrawal-only funding is nonzero.
490
+ * @throws {MutuallyExclusiveRepayAmountsError} If a Blue repayment specifies both nonzero assets and shares.
491
+ * @throws {MaxRepayAssetsBelowRepayAssetsError} If exact-asset funding cannot cover repayment plus referral fees.
492
+ * @throws {NativeFundingAmountMismatchError} If Blue native funding does not equal the full repayment cap.
493
+ * @throws {ChainWNativeMissingError} If native funding targets a chain without wNative.
494
+ * @throws {NativeAmountOnNonWNativeAssetError} If native funding targets a loan token other than wNative.
495
+ * @throws {UnexpectedRequirementSignatureError} If a Blue signature targets an inactive leg or native-funded token pull.
496
+ * @throws {DepositOwnerMismatchError} If a Blue signature belongs to another account.
497
+ * @throws {DepositAssetMismatchError} If a token signature names another loan token.
498
+ * @throws {DepositAmountMismatchError} If a token signature does not cover the repayment cap.
499
+ * @throws {DepositSpenderMismatchError} If a token signature names another spender.
500
+ * @throws {UnsupportedRequirementSignatureError} If a token signature has an unsupported action type.
501
+ * @throws {BundlesRequirementSignatureMismatchError} If a Blue signature cannot be bound or parsed safely.
502
+ * @throws {UnsupportedChainIdError} If a Blue action's chain is absent from the registry.
503
+ * @throws {UnknownAddressError} If BlueBundlesV1 is not registered on the action's chain.
272
504
  * @example
273
505
  * ```ts
274
506
  * import { buildTransactions, type TransactionPlan } from "@morpho-org/morpho-sdk";
@@ -281,5 +513,7 @@ function buildTransactions(plan) {
281
513
  (0, version_js_1.validateTransactionPlanVersion)(plan.version);
282
514
  // Validate every action before the first step can interpret requests.
283
515
  (0, validateActions_js_1.validateTransactionPlanActions)(plan.steps);
516
+ // Reject malformed headers even for empty plans.
517
+ (0, validation_js_1.validateTransactionPlanHeader)(plan);
284
518
  return plan.steps.map((step) => buildTransaction(plan, step));
285
519
  }
@@ -8,7 +8,9 @@ import type { Action, SignatureRequest, Step } from "./types.js";
8
8
  * @returns The unsigned request with a concrete nonce.
9
9
  * @throws {UnexpectedRequirementSignatureError} If the permit requirement has no nonce.
10
10
  */
11
- export declare function buildPermitSignatureRequest(requirement: Pick<Requirement<Erc2612RequirementSignature>, "action">, id: string): SignatureRequest;
11
+ export declare function buildPermitSignatureRequest(requirement: Pick<Requirement<Erc2612RequirementSignature>, "action">, id: string): Extract<SignatureRequest, {
12
+ kind: "permit";
13
+ }>;
12
14
  /**
13
15
  * @internal Converts an approval transaction into a step with prepared chain/account inputs.
14
16
  * @param requirement - Direct token approval prerequisite.
@@ -23,8 +25,6 @@ export declare function buildApprovalStep(requirement: Readonly<Transaction<ERC2
23
25
  }): Step<Extract<Action, {
24
26
  type: "erc20Approval";
25
27
  }>>;
26
- /** @internal Token pull prerequisites: direct approvals or an ERC-2612 permit. */
27
- type TokenRequirement = Readonly<Transaction<ERC20ApprovalAction>> | Requirement<Erc2612RequirementSignature>;
28
28
  interface TokenRequirementsParams {
29
29
  readonly token: Address;
30
30
  readonly spender: Address;
@@ -35,11 +35,20 @@ interface TokenRequirementsParams {
35
35
  readonly now: bigint;
36
36
  readonly supportSignature: boolean;
37
37
  readonly deployless: boolean | undefined;
38
+ readonly permitId: string;
39
+ readonly approvalId: (index: number) => string;
38
40
  }
39
41
  /**
40
- * @internal Resolves the token prerequisite of a VaultBundlesV1 pull without Permit2. A sufficient
42
+ * @internal Resolves the token prerequisite of a bundles pull without Permit2. A sufficient
41
43
  * direct allowance yields nothing. Otherwise signature support prefers an ERC-2612 permit when
42
44
  * available and falls back to a direct unlimited approval. Without signature support, always approve.
43
45
  */
44
- export declare function getTokenRequirements(client: Client, params: TokenRequirementsParams): Promise<readonly TokenRequirement[]>;
46
+ export declare function getTokenRequirements(client: Client, params: TokenRequirementsParams): Promise<{
47
+ readonly approvals: Step<Extract<Action, {
48
+ type: "erc20Approval";
49
+ }>>[];
50
+ readonly permit?: Extract<SignatureRequest, {
51
+ kind: "permit";
52
+ }>;
53
+ }>;
45
54
  export {};
@@ -5,7 +5,7 @@ exports.buildApprovalStep = buildApprovalStep;
5
5
  exports.getTokenRequirements = getTokenRequirements;
6
6
  const viem_1 = require("viem");
7
7
  const actions_1 = require("viem/actions");
8
- const getRequirementsApproval_js_1 = require("../actions/requirements/getRequirementsApproval.js");
8
+ const tokenApproval_js_1 = require("../helpers/tokenApproval.js");
9
9
  const error_js_1 = require("../types/error.js");
10
10
  const getTokenPermit_js_1 = require("./getTokenPermit.js");
11
11
  /**
@@ -52,34 +52,52 @@ function buildApprovalStep(requirement, { id, chainId, account, denomination, })
52
52
  };
53
53
  }
54
54
  /**
55
- * @internal Resolves the token prerequisite of a VaultBundlesV1 pull without Permit2. A sufficient
55
+ * @internal Resolves the token prerequisite of a bundles pull without Permit2. A sufficient
56
56
  * direct allowance yields nothing. Otherwise signature support prefers an ERC-2612 permit when
57
57
  * available and falls back to a direct unlimited approval. Without signature support, always approve.
58
58
  */
59
59
  async function getTokenRequirements(client, params) {
60
+ const { token, spender, amount, owner, chainId, permitId, approvalId } = params;
60
61
  // The allowance is trusted as returned by the RPC. See THREAT_MODEL.md, RPC.
61
62
  const allowance = await (0, actions_1.readContract)(client, {
62
63
  abi: viem_1.erc20Abi,
63
- address: params.token,
64
+ address: token,
64
65
  functionName: "allowance",
65
- args: [params.owner, params.spender],
66
+ args: [owner, spender],
66
67
  });
67
- if (allowance >= params.amount) {
68
- return [];
68
+ if (allowance >= amount) {
69
+ return { approvals: [] };
69
70
  }
70
71
  if (params.supportSignature) {
71
72
  const permit = await (0, getTokenPermit_js_1.getTokenPermit)(client, {
72
73
  ...params,
74
+ id: permitId,
73
75
  supportDeployless: params.deployless,
74
76
  });
75
77
  if (permit !== undefined) {
76
- return [permit];
78
+ return { approvals: [], permit };
77
79
  }
78
80
  }
79
- return (0, getRequirementsApproval_js_1.getRequirementsApproval)({
80
- address: params.token,
81
- chainId: params.chainId,
82
- args: { spendAmount: params.amount, spender: params.spender },
83
- allowances: allowance,
81
+ const amounts = (0, tokenApproval_js_1.getTokenApprovalAmounts)({
82
+ token,
83
+ chainId,
84
+ allowance,
85
+ spendAmount: amount,
84
86
  });
87
+ return {
88
+ approvals: amounts.map((approvalAmount, index) => ({
89
+ id: approvalId(index),
90
+ label: "Approve token spending",
91
+ action: {
92
+ type: "erc20Approval",
93
+ inputs: {
94
+ token,
95
+ spender,
96
+ assets: (0, tokenApproval_js_1.capTokenApproval)({ token, chainId, amount: approvalAmount }),
97
+ chainId,
98
+ account: owner,
99
+ },
100
+ },
101
+ })),
102
+ };
85
103
  }
@@ -14,11 +14,14 @@ export declare function serializeTransactionPlan(plan: TransactionPlan): string;
14
14
  /**
15
15
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
16
16
  * Restores bigint fields and validates version, action/request shapes, uint256 ranges, unique IDs,
17
- * references, per-action chain/account and permit/action consistency, including signed requests.
18
- * Extra keys are allowed. No clock or RPC is used: expiry, live state, signature ownership and
19
- * action-layer business constraints are not checked. Parsing does not prove authenticity or intent;
17
+ * references, per-action chain/account and signature-request consistency, including signed requests.
18
+ * Extra keys are allowed. No clock or RPC is used: current expiry, live state and
19
+ * action-layer business constraints are not checked. Stored signatures are recovered offline.
20
+ * Permits bind owner, token, spender, value, nonce, deadline and chain; Morpho authorizations bind
21
+ * owner, authorized address, nonce, deadline and chain, not operation amounts, markets or referral fees/recipients.
22
+ * Parsing does not prove authenticity or intent for unsigned fields;
20
23
  * tampered but well-formed, internally consistent plans pass. Integrators must protect stored plans.
21
- * Use `applySignatures` to verify new signatures; existing restored signatures remain trusted.
24
+ * Integrators must protect unsigned stored fields even when all signatures verify.
22
25
  * @param json - JSON previously returned by `serializeTransactionPlan`.
23
26
  * @returns The restored plan.
24
27
  * @throws {InvalidTransactionPlanError} At the first malformed JSON, field shape or consistency failure, exposing its JSON `path` and `reason`.