@metamask/subscription-controller 10.0.1 → 12.0.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 (55) hide show
  1. package/CHANGELOG.md +57 -1
  2. package/dist/SubscriptionController-method-action-types.d.ts +17 -1
  3. package/dist/SubscriptionController-method-action-types.d.ts.map +1 -1
  4. package/dist/SubscriptionController-method-action-types.js.map +1 -1
  5. package/dist/SubscriptionController.d.ts +15 -1
  6. package/dist/SubscriptionController.d.ts.map +1 -1
  7. package/dist/SubscriptionController.js +41 -5
  8. package/dist/SubscriptionController.js.map +1 -1
  9. package/dist/SubscriptionService-method-action-types.d.ts +2 -1
  10. package/dist/SubscriptionService-method-action-types.d.ts.map +1 -1
  11. package/dist/SubscriptionService-method-action-types.js.map +1 -1
  12. package/dist/SubscriptionService-structs.d.ts +33 -47
  13. package/dist/SubscriptionService-structs.d.ts.map +1 -1
  14. package/dist/SubscriptionService-structs.js +2 -6
  15. package/dist/SubscriptionService-structs.js.map +1 -1
  16. package/dist/SubscriptionService.d.ts +2 -1
  17. package/dist/SubscriptionService.d.ts.map +1 -1
  18. package/dist/SubscriptionService.js +4 -3
  19. package/dist/SubscriptionService.js.map +1 -1
  20. package/dist/constants.d.ts +8 -1
  21. package/dist/constants.d.ts.map +1 -1
  22. package/dist/constants.js +7 -0
  23. package/dist/constants.js.map +1 -1
  24. package/dist/index.d.ts +4 -3
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/subscription-delegation/SubscriptionDelegationService-method-action-types.d.ts +29 -5
  29. package/dist/subscription-delegation/SubscriptionDelegationService-method-action-types.d.ts.map +1 -1
  30. package/dist/subscription-delegation/SubscriptionDelegationService-method-action-types.js.map +1 -1
  31. package/dist/subscription-delegation/SubscriptionDelegationService.d.ts +33 -11
  32. package/dist/subscription-delegation/SubscriptionDelegationService.d.ts.map +1 -1
  33. package/dist/subscription-delegation/SubscriptionDelegationService.js +305 -22
  34. package/dist/subscription-delegation/SubscriptionDelegationService.js.map +1 -1
  35. package/dist/subscription-delegation/caveats.d.ts +31 -2
  36. package/dist/subscription-delegation/caveats.d.ts.map +1 -1
  37. package/dist/subscription-delegation/caveats.js +49 -9
  38. package/dist/subscription-delegation/caveats.js.map +1 -1
  39. package/dist/subscription-delegation/fingerprint.d.ts +1 -0
  40. package/dist/subscription-delegation/fingerprint.d.ts.map +1 -1
  41. package/dist/subscription-delegation/fingerprint.js +10 -6
  42. package/dist/subscription-delegation/fingerprint.js.map +1 -1
  43. package/dist/subscription-delegation/typed-data.d.ts +23 -0
  44. package/dist/subscription-delegation/typed-data.d.ts.map +1 -0
  45. package/dist/subscription-delegation/typed-data.js +75 -0
  46. package/dist/subscription-delegation/typed-data.js.map +1 -0
  47. package/dist/subscription-delegation/types.d.ts +105 -12
  48. package/dist/subscription-delegation/types.d.ts.map +1 -1
  49. package/dist/subscription-delegation/types.js +3 -4
  50. package/dist/subscription-delegation/types.js.map +1 -1
  51. package/dist/types.d.ts +15 -6
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/types.js +2 -0
  54. package/dist/types.js.map +1 -1
  55. package/package.json +19 -16
@@ -1,13 +1,15 @@
1
+ import { ORIGIN_METAMASK } from '@metamask/controller-utils';
1
2
  import { hashDelegation } from '@metamask/delegation-core';
2
3
  import { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';
3
- import { getMoneyAccountVaultConfig, MUSD_DECIMALS, } from '@metamask/money-account-utils';
4
- import { add0x, hexToNumber } from '@metamask/utils';
5
- import { SubscriptionDelegationServiceErrorMessage } from '../constants.js';
4
+ import { getMoneyAccountDepositAssetAddress, getMoneyAccountVaultConfig, MUSD_DECIMALS, } from '@metamask/money-account-utils';
5
+ import { add0x, hexToNumber, isStrictHexString } from '@metamask/utils';
6
+ import { ACTIVE_SUBSCRIPTION_STATUSES, SubscriptionControllerErrorMessage, SubscriptionDelegationServiceErrorMessage, } from '../constants.js';
6
7
  import { CRYPTO_AUTH_METHODS, PAYMENT_TYPES, PRODUCT_TYPES } from '../types.js';
7
8
  import { assertPositiveInteger, calculatePeriodAmount, getDelegationStartDate, getPeriodDuration, } from './amount.js';
8
9
  import { buildUnsignedSubscriptionDelegation } from './caveats.js';
9
10
  import { equalsIgnoreCase, makeMatchesSubscriptionDelegation, pickLatestMatchingSubscriptionDelegation, } from './fingerprint.js';
10
- import { CASH_SUBSCRIPTION_DELEGATION_TYPE } from './types.js';
11
+ import { buildDelegationTypedData, decodeSubscriptionAuthority, } from './typed-data.js';
12
+ import { CASH_SUBSCRIPTION_DELEGATION_TYPE, SUBSCRIPTION_DELEGATION_APPROVAL_TYPE, SUBSCRIPTION_DELEGATION_POLICY_VERSION, } from './types.js';
11
13
  /**
12
14
  * The name of the {@link SubscriptionDelegationService}, used to namespace the
13
15
  * service's actions and events.
@@ -16,18 +18,24 @@ export const serviceName = 'SubscriptionDelegationService';
16
18
  const MESSENGER_EXPOSED_METHODS = [
17
19
  'prepareDelegation',
18
20
  'checkMoneyAccountBalance',
21
+ 'startSubscriptionWithDelegation',
19
22
  ];
20
23
  const DELEGATION_FRAMEWORK_VERSION = '1.3.0';
21
24
  function resolveEnforcers(chainId) {
22
25
  const contracts = DELEGATOR_CONTRACTS[DELEGATION_FRAMEWORK_VERSION]?.[hexToNumber(chainId)];
23
- if (!contracts?.ValueLteEnforcer || !contracts.ERC20PeriodTransferEnforcer) {
26
+ if (!contracts?.ERC20PeriodTransferEnforcer ||
27
+ !contracts.AllowedCalldataEnforcer) {
24
28
  throw new Error(`${SubscriptionDelegationServiceErrorMessage.DelegationContractsNotFound}: ${chainId}`);
25
29
  }
26
30
  return {
27
- valueLte: contracts.ValueLteEnforcer,
28
31
  erc20TokenPeriodTransfer: contracts.ERC20PeriodTransferEnforcer,
32
+ allowedCalldata: contracts.AllowedCalldataEnforcer,
29
33
  };
30
34
  }
35
+ function removeDelegationSignature(delegation) {
36
+ const { signature: _signature, ...unsignedDelegation } = delegation;
37
+ return unsignedDelegation;
38
+ }
31
39
  /**
32
40
  * Stateless orchestrator for cash-subscription delegation setup.
33
41
  *
@@ -35,10 +43,8 @@ function resolveEnforcers(chainId) {
35
43
  * Authenticated User Storage → register CHOMP intent. Returns a verified
36
44
  * `delegationHash` for `SubscriptionController.startSubscriptionWithCrypto`.
37
45
  *
38
- * Alpha callers must pass `skipChompInteractions: true` until a follow-up
39
- * `@metamask/chomp-api-service` release accepts `'cash-subscription'` intent
40
- * metadata. The CHOMP-enabled path (`skipChompInteractions` unset/false)
41
- * remains dormant and is not production-ready without that package support.
46
+ * Pass `skipChompInteractions: true` to skip CHOMP verify and intent
47
+ * registration and return a locally computed delegation hash.
42
48
  *
43
49
  * Each call resolves the Money Account chain from remote feature flags, then
44
50
  * resolves its price, payment token, and delegate from `SubscriptionController`
@@ -80,6 +86,284 @@ export class SubscriptionDelegationService {
80
86
  requiredBalance: requiredBalance.toString(),
81
87
  };
82
88
  }
89
+ /**
90
+ * Runs the complete Money Account subscription checkout authorization flow.
91
+ *
92
+ * Subscriptions are refreshed first. An active subscription for the product
93
+ * is rejected before Money Account upgrade, approval, delegation signing,
94
+ * persistence, or CHOMP registration.
95
+ *
96
+ * Before the approval, `MoneyAccountUpgradeController` is asked to ensure
97
+ * the Money Account vault delegations and CHOMP intents exist; the
98
+ * Subscription API validates them server-side. The custom approval is the
99
+ * sole consent and funding boundary for the payment permission. No payment
100
+ * delegation signing, persistence, or intent mutation occurs before it
101
+ * returns a funding transaction hash.
102
+ *
103
+ * When `request.skipApproval` is `true`, the approval request and its
104
+ * result validation are skipped and the flow proceeds directly to signing.
105
+ * The caller is then responsible for consent and funding.
106
+ *
107
+ * @param request - Product selection, Money Account identity, and optional
108
+ * `skipApproval` flag.
109
+ * @returns The result from `SubscriptionController:startSubscriptionWithCrypto`.
110
+ */
111
+ async startSubscriptionWithDelegation(request) {
112
+ if (request.product !== PRODUCT_TYPES.MONEY_ACCOUNT_PLUS) {
113
+ throw new Error(SubscriptionDelegationServiceErrorMessage.UnsupportedProduct);
114
+ }
115
+ const config = await this.#resolveConfiguration(request.product, request.recurringInterval);
116
+ if (!equalsIgnoreCase(request.chainId, config.chainId)) {
117
+ throw new Error(SubscriptionDelegationServiceErrorMessage.ChainMismatch);
118
+ }
119
+ const subscriptions = await this.#messenger.call('SubscriptionController:getSubscriptions');
120
+ this.#assertUserNotSubscribed(subscriptions, request.product);
121
+ const { trialedProducts } = this.#messenger.call('SubscriptionController:getState');
122
+ const isTrialRequested = config.price.trialPeriodDays > 0 &&
123
+ !trialedProducts.includes(request.product);
124
+ await this.#messenger.call('MoneyAccountUpgradeController:forceUpgradeAccount', request.payerAddress);
125
+ const paymentPermission = await this.#preparePaymentPermission(request, config, isTrialRequested);
126
+ const bundle = {
127
+ policyVersion: SUBSCRIPTION_DELEGATION_POLICY_VERSION,
128
+ account: request.payerAddress,
129
+ chainId: request.chainId,
130
+ permissions: [paymentPermission],
131
+ };
132
+ const approval = request.skipApproval
133
+ ? undefined
134
+ : await this.#requestApproval({
135
+ request,
136
+ price: config.price,
137
+ musdAddress: config.musdAddress,
138
+ bundle,
139
+ });
140
+ try {
141
+ if (approval) {
142
+ this.#validateApprovalResult(approval);
143
+ }
144
+ const paymentDelegation = await this.#signPaymentPermissionIfRequired(paymentPermission, config);
145
+ const paymentDelegationHash = await this.#commitPaymentPermission({
146
+ request,
147
+ paymentPermission,
148
+ paymentDelegation,
149
+ config,
150
+ });
151
+ const result = await this.#messenger.call('SubscriptionController:startSubscriptionWithCrypto', {
152
+ products: [request.product],
153
+ isTrialRequested,
154
+ recurringInterval: request.recurringInterval,
155
+ billingCycles: config.price.minBillingCycles,
156
+ chainId: request.chainId,
157
+ payerAddress: request.payerAddress,
158
+ tokenSymbol: config.token.symbol,
159
+ cryptoAuthMethod: CRYPTO_AUTH_METHODS.DELEGATION,
160
+ delegationHash: paymentDelegationHash,
161
+ // Reject if eligibility changed during approval, because the signed
162
+ // delegation start date was derived from the original trial state.
163
+ // This local guard is removed before the backend request.
164
+ assertTrialEligibility: true,
165
+ });
166
+ approval?.resultCallbacks?.success();
167
+ return result;
168
+ }
169
+ catch (error) {
170
+ approval?.resultCallbacks?.error(error);
171
+ throw error;
172
+ }
173
+ }
174
+ /**
175
+ * Rejects when the refreshed list already contains an active subscription
176
+ * for the product. Active includes `active`, `trialing`, and `provisional`.
177
+ *
178
+ * @param subscriptions - Subscriptions returned by the latest refresh.
179
+ * @param product - Product the caller is trying to start.
180
+ */
181
+ #assertUserNotSubscribed(subscriptions, product) {
182
+ const alreadySubscribed = subscriptions.some((subscription) => ACTIVE_SUBSCRIPTION_STATUSES.includes(subscription.status) &&
183
+ subscription.products.some((entry) => entry.name === product));
184
+ if (alreadySubscribed) {
185
+ throw new Error(SubscriptionControllerErrorMessage.UserAlreadySubscribed);
186
+ }
187
+ }
188
+ async #requestApproval({ request, price, musdAddress, bundle, }) {
189
+ const targetAmount = calculatePeriodAmount({
190
+ unitAmount: price.unitAmount,
191
+ unitDecimals: price.unitDecimals,
192
+ tokenDecimals: MUSD_DECIMALS,
193
+ }) * BigInt(price.minBillingCyclesForBalance);
194
+ return (await this.#messenger.call('ApprovalController:addRequest', {
195
+ origin: ORIGIN_METAMASK,
196
+ type: SUBSCRIPTION_DELEGATION_APPROVAL_TYPE,
197
+ expectsResult: true,
198
+ requestData: {
199
+ bundle,
200
+ funding: {
201
+ useCase: 'subscription',
202
+ destinationAccount: request.payerAddress,
203
+ chainId: request.chainId,
204
+ targetToken: {
205
+ symbol: 'mUSD',
206
+ address: musdAddress,
207
+ },
208
+ targetAmount: targetAmount.toString(),
209
+ },
210
+ },
211
+ }, true));
212
+ }
213
+ async #preparePaymentPermission(request, config, isTrialRequested) {
214
+ const periodAmount = calculatePeriodAmount({
215
+ unitAmount: config.price.unitAmount,
216
+ unitDecimals: config.price.unitDecimals,
217
+ tokenDecimals: config.token.decimals,
218
+ });
219
+ const periodDuration = getPeriodDuration(request.recurringInterval);
220
+ const nowSeconds = Math.floor(Date.now() / 1000);
221
+ const startDate = getDelegationStartDate({
222
+ nowSeconds,
223
+ trialPeriodDays: isTrialRequested
224
+ ? config.price.trialPeriodDays
225
+ : undefined,
226
+ });
227
+ const matches = makeMatchesSubscriptionDelegation({
228
+ delegatorAddress: request.payerAddress,
229
+ delegateAddress: config.delegateAddress,
230
+ chainId: request.chainId,
231
+ recipientAddress: config.paymentAddress,
232
+ tokenAddress: config.token.address,
233
+ periodAmount,
234
+ periodDuration,
235
+ nowSeconds,
236
+ isTrialDeferred: startDate > nowSeconds,
237
+ enforcers: config.enforcers,
238
+ });
239
+ const reusable = pickLatestMatchingSubscriptionDelegation((await this.#messenger.call('AuthenticatedUserStorageService:listDelegations')).filter(matches), config.enforcers);
240
+ const delegation = reusable
241
+ ? removeDelegationSignature(reusable.signedDelegation)
242
+ : buildUnsignedSubscriptionDelegation({
243
+ delegateAddress: config.delegateAddress,
244
+ delegatorAddress: request.payerAddress,
245
+ enforcers: config.enforcers,
246
+ recipientAddress: config.paymentAddress,
247
+ tokenAddress: config.token.address,
248
+ periodAmount,
249
+ periodDuration,
250
+ startDate,
251
+ });
252
+ const typedData = buildDelegationTypedData({
253
+ delegation,
254
+ chainId: request.chainId,
255
+ delegationManager: config.delegationManager,
256
+ });
257
+ return {
258
+ id: CASH_SUBSCRIPTION_DELEGATION_TYPE,
259
+ owner: 'subscription',
260
+ disposition: reusable ? 'reused' : 'new',
261
+ delegation,
262
+ typedData,
263
+ decodedAuthority: decodeSubscriptionAuthority(delegation, config.enforcers),
264
+ ...(reusable
265
+ ? { existingDelegationHash: reusable.metadata.delegationHash }
266
+ : {}),
267
+ };
268
+ }
269
+ #validateApprovalResult(approval) {
270
+ if (!approval?.value || typeof approval.value !== 'object') {
271
+ throw new Error(SubscriptionDelegationServiceErrorMessage.ApprovalResultMissing);
272
+ }
273
+ const value = approval.value;
274
+ if (typeof value.fundingTransactionHash !== 'string' ||
275
+ !isStrictHexString(value.fundingTransactionHash) ||
276
+ value.fundingTransactionHash.length !== 66) {
277
+ throw new Error(SubscriptionDelegationServiceErrorMessage.InvalidFundingTransactionHash);
278
+ }
279
+ return {
280
+ fundingTransactionHash: value.fundingTransactionHash,
281
+ };
282
+ }
283
+ async #signPaymentPermissionIfRequired(permission, config) {
284
+ if (permission.disposition === 'reused') {
285
+ return undefined;
286
+ }
287
+ const signature = (await this.#messenger.call('DelegationController:signDelegation', {
288
+ delegation: permission.delegation,
289
+ chainId: config.chainId,
290
+ }));
291
+ return { ...permission.delegation, signature };
292
+ }
293
+ async #commitPaymentPermission({ request, paymentPermission, paymentDelegation, config, }) {
294
+ if (paymentPermission.disposition === 'reused') {
295
+ const delegationHash = paymentPermission.existingDelegationHash;
296
+ const storedDelegation = (await this.#messenger.call('AuthenticatedUserStorageService:listDelegations')).find(({ metadata }) => equalsIgnoreCase(metadata.delegationHash, delegationHash));
297
+ if (!storedDelegation) {
298
+ throw new Error(SubscriptionDelegationServiceErrorMessage.ReusableDelegationInvalid);
299
+ }
300
+ await this.#verifySignedPaymentDelegation(storedDelegation.signedDelegation, delegationHash, request.chainId);
301
+ await this.#ensureIntent({
302
+ account: request.payerAddress,
303
+ chainId: request.chainId,
304
+ delegationHash,
305
+ allowance: add0x(BigInt(paymentPermission.decodedAuthority.periodAmount).toString(16)),
306
+ tokenSymbol: config.token.symbol,
307
+ tokenAddress: config.token.address,
308
+ });
309
+ await this.#assertIntentActive(request.payerAddress, delegationHash);
310
+ return delegationHash;
311
+ }
312
+ const signedDelegation = paymentDelegation;
313
+ const delegationHash = hashDelegation({
314
+ ...signedDelegation,
315
+ salt: BigInt(signedDelegation.salt),
316
+ });
317
+ await this.#verifySignedPaymentDelegation(signedDelegation, delegationHash, request.chainId);
318
+ const allowance = add0x(BigInt(paymentPermission.decodedAuthority.periodAmount).toString(16));
319
+ await this.#messenger.call('AuthenticatedUserStorageService:createDelegation', {
320
+ signedDelegation,
321
+ metadata: {
322
+ delegationHash,
323
+ chainIdHex: request.chainId,
324
+ allowance,
325
+ tokenSymbol: config.token.symbol,
326
+ tokenAddress: config.token.address,
327
+ type: CASH_SUBSCRIPTION_DELEGATION_TYPE,
328
+ },
329
+ });
330
+ await this.#createIntent({
331
+ account: request.payerAddress,
332
+ chainId: request.chainId,
333
+ delegationHash,
334
+ allowance,
335
+ tokenSymbol: config.token.symbol,
336
+ tokenAddress: config.token.address,
337
+ });
338
+ await this.#assertIntentActive(request.payerAddress, delegationHash);
339
+ return delegationHash;
340
+ }
341
+ async #assertIntentActive(account, delegationHash) {
342
+ const intents = await this.#messenger.call('ChompApiService:getIntentsByAddress', account);
343
+ const intent = intents.find((candidate) => equalsIgnoreCase(candidate.delegationHash, delegationHash));
344
+ if (intent?.status !== 'active') {
345
+ throw new Error(SubscriptionDelegationServiceErrorMessage.ChompIntentNotActive);
346
+ }
347
+ }
348
+ async #verifySignedPaymentDelegation(signedDelegation, expectedDelegationHash, chainId) {
349
+ const localHash = hashDelegation({
350
+ ...signedDelegation,
351
+ salt: BigInt(signedDelegation.salt),
352
+ });
353
+ if (!equalsIgnoreCase(localHash, expectedDelegationHash)) {
354
+ throw new Error(SubscriptionDelegationServiceErrorMessage.ReusableDelegationInvalid);
355
+ }
356
+ const verifyResult = await this.#messenger.call('ChompApiService:verifyDelegation', { signedDelegation, chainId });
357
+ if (!verifyResult.valid) {
358
+ throw new Error(`${SubscriptionDelegationServiceErrorMessage.ChompRejectedDelegation}: ${verifyResult.errors?.join(', ') ?? 'unknown error'}`);
359
+ }
360
+ if (!verifyResult.delegationHash) {
361
+ throw new Error(SubscriptionDelegationServiceErrorMessage.ChompMissingDelegationHash);
362
+ }
363
+ if (!equalsIgnoreCase(verifyResult.delegationHash, expectedDelegationHash)) {
364
+ throw new Error(SubscriptionDelegationServiceErrorMessage.ChompDelegationHashMismatch);
365
+ }
366
+ }
83
367
  /**
84
368
  * Prepares a cash-subscription delegation and returns its hash.
85
369
  *
@@ -93,10 +377,8 @@ export class SubscriptionDelegationService {
93
377
  * If there is no match, builds, signs, optionally verifies with CHOMP,
94
378
  * persists, and optionally registers a new delegation.
95
379
  *
96
- * When `skipChompInteractions` is true (required for alpha), CHOMP verify
97
- * and intent calls are skipped; the returned hash is computed locally. The
98
- * default CHOMP-enabled path requires a follow-up chomp-api-service release
99
- * that accepts `'cash-subscription'` intent metadata.
380
+ * When `skipChompInteractions` is true, CHOMP verify and intent calls are
381
+ * skipped; the returned hash is computed locally.
100
382
  *
101
383
  * @param request - Authoritative pricing and payer details for the delegation.
102
384
  * @param forceNew - Whether to create a replacement instead of reusing a
@@ -109,7 +391,7 @@ export class SubscriptionDelegationService {
109
391
  throw new Error(SubscriptionDelegationServiceErrorMessage.UnsupportedProduct);
110
392
  }
111
393
  const skipChomp = Boolean(request.skipChompInteractions);
112
- const { chainId, delegateAddress, enforcers, price, token } = await this.#resolveConfiguration(request.product, request.recurringInterval);
394
+ const { chainId, delegateAddress, paymentAddress, enforcers, price, token, } = await this.#resolveConfiguration(request.product, request.recurringInterval);
113
395
  if (request.checkBalance) {
114
396
  const { hasSufficientBalance } = await this.#compareMoneyAccountBalance(request.payerAddress, price);
115
397
  if (!hasSufficientBalance) {
@@ -136,6 +418,7 @@ export class SubscriptionDelegationService {
136
418
  request,
137
419
  chainId,
138
420
  delegateAddress,
421
+ paymentAddress,
139
422
  tokenAddress: token.address,
140
423
  periodAmount,
141
424
  periodDuration,
@@ -162,6 +445,7 @@ export class SubscriptionDelegationService {
162
445
  const unsigned = buildUnsignedSubscriptionDelegation({
163
446
  delegateAddress,
164
447
  delegatorAddress: request.payerAddress,
448
+ recipientAddress: paymentAddress,
165
449
  enforcers,
166
450
  tokenAddress: token.address,
167
451
  periodAmount,
@@ -217,10 +501,11 @@ export class SubscriptionDelegationService {
217
501
  disposition: 'created',
218
502
  };
219
503
  }
220
- async #findReusableDelegation({ request, chainId, delegateAddress, tokenAddress, periodAmount, periodDuration, nowSeconds, isTrialDeferred, enforcers, }) {
504
+ async #findReusableDelegation({ request, chainId, delegateAddress, paymentAddress, tokenAddress, periodAmount, periodDuration, nowSeconds, isTrialDeferred, enforcers, }) {
221
505
  const matches = makeMatchesSubscriptionDelegation({
222
506
  delegatorAddress: request.payerAddress,
223
507
  delegateAddress,
508
+ recipientAddress: paymentAddress,
224
509
  chainId,
225
510
  tokenAddress,
226
511
  periodAmount,
@@ -255,7 +540,11 @@ export class SubscriptionDelegationService {
255
540
  return {
256
541
  chainId,
257
542
  delegateAddress: chain.delegateAddress,
543
+ delegationManager: DELEGATOR_CONTRACTS[DELEGATION_FRAMEWORK_VERSION][hexToNumber(chainId)]
544
+ .DelegationManager,
545
+ paymentAddress: chain.paymentAddress,
258
546
  enforcers,
547
+ musdAddress: getMoneyAccountDepositAssetAddress(chainId),
259
548
  price,
260
549
  token,
261
550
  };
@@ -282,12 +571,6 @@ export class SubscriptionDelegationService {
282
571
  await this.#createIntent(params);
283
572
  }
284
573
  async #createIntent(params) {
285
- // Published `@metamask/chomp-api-service` only types intent metadata as
286
- // `'cash-deposit' | 'cash-withdrawal'`. The dormant production path still
287
- // passes `'cash-subscription'`; a follow-up chomp-api-service release must
288
- // accept that discriminator before this path is production-ready. Alpha
289
- // callers must use `skipChompInteractions: true` so this method is not
290
- // reached.
291
574
  await this.#messenger.call('ChompApiService:createIntents', [
292
575
  {
293
576
  account: params.account,
@@ -1 +1 @@
1
- {"version":3,"file":"SubscriptionDelegationService.js","sourceRoot":"","sources":["../../src/subscription-delegation/SubscriptionDelegationService.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAGvE,OAAO,EACL,0BAA0B,EAC1B,aAAa,GACd,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGrD,OAAO,EAAE,yCAAyC,EAAE,MAAM,iBAAiB,CAAC;AAE5E,OAAO,EAAE,mBAAmB,EAAE,a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A0E;QAC1E,2EAA2E;QAC3E,wEAAwE;QACxE,uEAAuE;QACvE,WAAW;QACX,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,+BAA+B,EAAE;YAC1D;gBACE,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,cAAc,EAAE,MAAM,CAAC,cAAc;gBACrC,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,QAAQ,EAAE;oBACR,SAAS,EAAE,MAAM,CAAC,SAAS;oBAC3B,WAAW,EAAE,MAAM,CAAC,WAAW;oBAC/B,YAAY,EAAE,MAAM,CAAC,YAAY;oBACjC,IAAI,EAAE,iCAEe;iBACtB;aACF;SACF,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n DelegationResponse,\n AuthenticatedUserStorageServiceCreateDelegationAction,\n AuthenticatedUserStorageServiceListDelegationsAction,\n} from '@metamask/authenticated-user-storage';\nimport type {\n ChompApiServiceCreateIntentsAction,\n ChompApiServiceGetIntentsByAddressAction,\n ChompApiServiceVerifyDelegationAction,\n} from '@metamask/chomp-api-service';\nimport type { DelegationControllerSignDelegationAction } from '@metamask/delegation-controller';\nimport { hashDelegation } from '@metamask/delegation-core';\nimport { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\nimport type { Messenger } from '@metamask/messenger';\nimport type { MoneyAccountBalanceServiceFetchBalanceWithFallbackAction } from '@metamask/money-account-balance-service';\nimport {\n getMoneyAccountVaultConfig,\n MUSD_DECIMALS,\n} from '@metamask/money-account-utils';\nimport type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';\nimport { add0x, hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { SubscriptionDelegationServiceErrorMessage } from '../constants.js';\nimport type { SubscriptionControllerGetPricingAction } from '../SubscriptionController-method-action-types.js';\nimport { CRYPTO_AUTH_METHODS, PAYMENT_TYPES, PRODUCT_TYPES } from '../types.js';\nimport type {\n ProductPrice,\n ProductType,\n PricingCryptoPaymentMethod,\n RecurringInterval,\n TokenPaymentInfo,\n} from '../types.js';\nimport {\n assertPositiveInteger,\n calculatePeriodAmount,\n getDelegationStartDate,\n getPeriodDuration,\n} from './amount.js';\nimport { buildUnsignedSubscriptionDelegation } from './caveats.js';\nimport {\n equalsIgnoreCase,\n makeMatchesSubscriptionDelegation,\n pickLatestMatchingSubscriptionDelegation,\n} from './fingerprint.js';\nimport type { SubscriptionDelegationServiceMethodActions } from './SubscriptionDelegationService-method-action-types.js';\nimport type {\n MoneyAccountBalanceCheckRequest,\n MoneyAccountBalanceCheckResult,\n PrepareSubscriptionDelegationRequest,\n PreparedSubscriptionDelegation,\n SubscriptionDelegationEnforcers,\n} from './types.js';\nimport { CASH_SUBSCRIPTION_DELEGATION_TYPE } from './types.js';\n\n/**\n * The name of the {@link SubscriptionDelegationService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'SubscriptionDelegationService';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'prepareDelegation',\n 'checkMoneyAccountBalance',\n] as const;\n\nconst DELEGATION_FRAMEWORK_VERSION = '1.3.0';\n\nfunction resolveEnforcers(chainId: Hex): SubscriptionDelegationEnforcers {\n const contracts =\n DELEGATOR_CONTRACTS[DELEGATION_FRAMEWORK_VERSION]?.[hexToNumber(chainId)];\n\n if (!contracts?.ValueLteEnforcer || !contracts.ERC20PeriodTransferEnforcer) {\n throw new Error(\n `${SubscriptionDelegationServiceErrorMessage.DelegationContractsNotFound}: ${chainId}`,\n );\n }\n\n return {\n valueLte: contracts.ValueLteEnforcer,\n erc20TokenPeriodTransfer: contracts.ERC20PeriodTransferEnforcer,\n };\n}\n\n/**\n * Actions that {@link SubscriptionDelegationService} exposes to other consumers.\n */\nexport type SubscriptionDelegationServiceActions =\n SubscriptionDelegationServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link SubscriptionDelegationServiceMessenger} calls.\n */\ntype AllowedActions =\n | AuthenticatedUserStorageServiceListDelegationsAction\n | AuthenticatedUserStorageServiceCreateDelegationAction\n | ChompApiServiceVerifyDelegationAction\n | ChompApiServiceCreateIntentsAction\n | ChompApiServiceGetIntentsByAddressAction\n | DelegationControllerSignDelegationAction\n | MoneyAccountBalanceServiceFetchBalanceWithFallbackAction\n | RemoteFeatureFlagControllerGetStateAction\n | SubscriptionControllerGetPricingAction;\n\n/**\n * Events that {@link SubscriptionDelegationService} exposes to other consumers.\n */\nexport type SubscriptionDelegationServiceEvents = never;\n\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link SubscriptionDelegationService}.\n */\nexport type SubscriptionDelegationServiceMessenger = Messenger<\n typeof serviceName,\n SubscriptionDelegationServiceActions | AllowedActions,\n SubscriptionDelegationServiceEvents | AllowedEvents\n>;\n\n/**\n * Options for constructing {@link SubscriptionDelegationService}.\n */\nexport type SubscriptionDelegationServiceOptions = {\n messenger: SubscriptionDelegationServiceMessenger;\n};\n\ntype SubscriptionIntentParams = {\n account: Hex;\n chainId: Hex;\n delegationHash: Hex;\n allowance: Hex;\n tokenSymbol: string;\n tokenAddress: Hex;\n};\n\ntype ResolvedSubscriptionDelegationConfig = {\n chainId: Hex;\n delegateAddress: Hex;\n enforcers: SubscriptionDelegationEnforcers;\n price: ProductPrice;\n token: TokenPaymentInfo;\n};\n\n/**\n * Stateless orchestrator for cash-subscription delegation setup.\n *\n * Owns the workflow: size periodic caveats → sign → CHOMP verify → persist to\n * Authenticated User Storage → register CHOMP intent. Returns a verified\n * `delegationHash` for `SubscriptionController.startSubscriptionWithCrypto`.\n *\n * Alpha callers must pass `skipChompInteractions: true` until a follow-up\n * `@metamask/chomp-api-service` release accepts `'cash-subscription'` intent\n * metadata. The CHOMP-enabled path (`skipChompInteractions` unset/false)\n * remains dormant and is not production-ready without that package support.\n *\n * Each call resolves the Money Account chain from remote feature flags, then\n * resolves its price, payment token, and delegate from `SubscriptionController`\n * pricing. The pricing `delegateAddress` is used as the delegation `delegate`.\n *\n * Does not own subscription state; `SubscriptionController` does not depend on\n * this service. Only Money Account Plus is supported.\n */\nexport class SubscriptionDelegationService {\n readonly name: typeof serviceName = serviceName;\n\n readonly #messenger: SubscriptionDelegationServiceMessenger;\n\n constructor(options: SubscriptionDelegationServiceOptions) {\n this.#messenger = options.messenger;\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Checks whether the Money Account holds enough convertible mUSD value to\n * cover pricing `unitAmount × minBillingCyclesForBalance`.\n *\n * @param request - Payer address and pricing amount fields.\n * @returns Balance comparison in mUSD base units (6 decimals).\n */\n async checkMoneyAccountBalance(\n request: MoneyAccountBalanceCheckRequest,\n ): Promise<MoneyAccountBalanceCheckResult> {\n const { price } = await this.#resolveConfiguration(\n request.product,\n request.recurringInterval,\n );\n return this.#compareMoneyAccountBalance(request.payerAddress, price);\n }\n\n async #compareMoneyAccountBalance(\n payerAddress: Hex,\n price: ProductPrice,\n ): Promise<MoneyAccountBalanceCheckResult> {\n assertPositiveInteger(\n price.minBillingCyclesForBalance,\n SubscriptionDelegationServiceErrorMessage.InvalidMinimumFundingCycles,\n );\n\n const periodAmount = calculatePeriodAmount({\n unitAmount: price.unitAmount,\n unitDecimals: price.unitDecimals,\n tokenDecimals: MUSD_DECIMALS,\n });\n const requiredBalance =\n periodAmount * BigInt(price.minBillingCyclesForBalance);\n\n const { totalBalance } = await this.#messenger.call(\n 'MoneyAccountBalanceService:fetchBalanceWithFallback',\n payerAddress,\n );\n\n return {\n hasSufficientBalance: BigInt(totalBalance) >= requiredBalance,\n balance: totalBalance,\n requiredBalance: requiredBalance.toString(),\n };\n }\n\n /**\n * Prepares a cash-subscription delegation and returns its hash.\n *\n * Reuses a stored AUS delegation that matches the semantic fingerprint when\n * one exists (ensuring a CHOMP intent is active for its hash, unless\n * `skipChompInteractions` is true). Reuse classifies period `startDate` as\n * trial-deferred (`> now`) vs immediately redeemable, matching creation.\n * When several records match, the latest period `startDate` is reused\n * so a `forceNew` replacement is preferred over an older equivalent\n * permission.\n * If there is no match, builds, signs, optionally verifies with CHOMP,\n * persists, and optionally registers a new delegation.\n *\n * When `skipChompInteractions` is true (required for alpha), CHOMP verify\n * and intent calls are skipped; the returned hash is computed locally. The\n * default CHOMP-enabled path requires a follow-up chomp-api-service release\n * that accepts `'cash-subscription'` intent metadata.\n *\n * @param request - Authoritative pricing and payer details for the delegation.\n * @param forceNew - Whether to create a replacement instead of reusing a\n * matching stored delegation.\n * @returns The delegation hash (CHOMP-verified unless skipped) and whether it\n * was created or reused.\n */\n async prepareDelegation(\n request: PrepareSubscriptionDelegationRequest,\n forceNew = false,\n ): Promise<PreparedSubscriptionDelegation> {\n if (request.product !== PRODUCT_TYPES.MONEY_ACCOUNT_PLUS) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.UnsupportedProduct,\n );\n }\n\n const skipChomp = Boolean(request.skipChompInteractions);\n\n const { chainId, delegateAddress, enforcers, price, token } =\n await this.#resolveConfiguration(\n request.product,\n request.recurringInterval,\n );\n\n if (request.checkBalance) {\n const { hasSufficientBalance } = await this.#compareMoneyAccountBalance(\n request.payerAddress,\n price,\n );\n if (!hasSufficientBalance) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.InsufficientBalance,\n );\n }\n }\n\n const periodAmount = calculatePeriodAmount({\n unitAmount: price.unitAmount,\n unitDecimals: price.unitDecimals,\n tokenDecimals: token.decimals,\n });\n const periodDuration = getPeriodDuration(request.recurringInterval);\n const nowSeconds = Math.floor(Date.now() / 1000);\n const startDate = getDelegationStartDate({\n nowSeconds,\n trialPeriodDays: request.isTrialRequested\n ? price.trialPeriodDays\n : undefined,\n });\n const isTrialDeferred = startDate > nowSeconds;\n\n const reusable = forceNew\n ? undefined\n : await this.#findReusableDelegation({\n request,\n chainId,\n delegateAddress,\n tokenAddress: token.address,\n periodAmount,\n periodDuration,\n nowSeconds,\n isTrialDeferred,\n enforcers,\n });\n if (reusable) {\n if (!skipChomp) {\n await this.#ensureIntent({\n account: request.payerAddress,\n chainId,\n delegationHash: reusable.metadata.delegationHash,\n allowance: reusable.metadata.allowance,\n tokenSymbol: reusable.metadata.tokenSymbol,\n tokenAddress: reusable.metadata.tokenAddress,\n });\n }\n return {\n delegationHash: reusable.metadata.delegationHash,\n disposition: 'reused',\n };\n }\n\n const unsigned = buildUnsignedSubscriptionDelegation({\n delegateAddress,\n delegatorAddress: request.payerAddress,\n enforcers,\n tokenAddress: token.address,\n periodAmount,\n periodDuration,\n startDate,\n });\n\n const signature = (await this.#messenger.call(\n 'DelegationController:signDelegation',\n { delegation: unsigned, chainId },\n )) as Hex;\n\n const signedDelegation = { ...unsigned, signature };\n\n const delegationHash = hashDelegation({\n ...unsigned,\n salt: BigInt(unsigned.salt),\n signature,\n });\n\n if (!skipChomp) {\n const verifyResult = await this.#messenger.call(\n 'ChompApiService:verifyDelegation',\n {\n signedDelegation,\n chainId,\n },\n );\n\n if (!verifyResult.valid) {\n throw new Error(\n `${SubscriptionDelegationServiceErrorMessage.ChompRejectedDelegation}: ${\n verifyResult.errors?.join(', ') ?? 'unknown error'\n }`,\n );\n }\n\n if (!verifyResult.delegationHash) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ChompMissingDelegationHash,\n );\n }\n if (!equalsIgnoreCase(verifyResult.delegationHash, delegationHash)) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ChompDelegationHashMismatch,\n );\n }\n }\n\n const allowance: Hex = add0x(periodAmount.toString(16));\n\n await this.#messenger.call(\n 'AuthenticatedUserStorageService:createDelegation',\n {\n signedDelegation,\n metadata: {\n delegationHash,\n chainIdHex: chainId,\n allowance,\n tokenSymbol: token.symbol,\n tokenAddress: token.address,\n type: CASH_SUBSCRIPTION_DELEGATION_TYPE,\n },\n },\n );\n\n if (!skipChomp) {\n await this.#createIntent({\n account: request.payerAddress,\n chainId,\n delegationHash,\n allowance,\n tokenSymbol: token.symbol,\n tokenAddress: token.address,\n });\n }\n\n return {\n delegationHash,\n disposition: 'created',\n };\n }\n\n async #findReusableDelegation({\n request,\n chainId,\n delegateAddress,\n tokenAddress,\n periodAmount,\n periodDuration,\n nowSeconds,\n isTrialDeferred,\n enforcers,\n }: {\n request: PrepareSubscriptionDelegationRequest;\n chainId: Hex;\n delegateAddress: Hex;\n tokenAddress: Hex;\n periodAmount: bigint;\n periodDuration: number;\n nowSeconds: number;\n isTrialDeferred: boolean;\n enforcers: SubscriptionDelegationEnforcers;\n }): Promise<DelegationResponse | undefined> {\n const matches = makeMatchesSubscriptionDelegation({\n delegatorAddress: request.payerAddress,\n delegateAddress,\n chainId,\n tokenAddress,\n periodAmount,\n periodDuration,\n nowSeconds,\n isTrialDeferred,\n enforcers,\n });\n\n const existingDelegations = await this.#messenger.call(\n 'AuthenticatedUserStorageService:listDelegations',\n );\n\n return pickLatestMatchingSubscriptionDelegation(\n existingDelegations.filter(matches),\n enforcers,\n );\n }\n\n async #resolveConfiguration(\n product: ProductType,\n recurringInterval: RecurringInterval,\n ): Promise<ResolvedSubscriptionDelegationConfig> {\n const { remoteFeatureFlags } = this.#messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n const vaultConfig = getMoneyAccountVaultConfig(remoteFeatureFlags);\n if (!vaultConfig) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.MissingMoneyAccountVaultConfig,\n );\n }\n\n const { chainId } = vaultConfig;\n const enforcers = resolveEnforcers(chainId);\n const pricing = await this.#messenger.call(\n 'SubscriptionController:getPricing',\n );\n const price = pricing.products\n .find((entry) => entry.name === product)\n ?.prices?.find((entry) => entry.interval === recurringInterval);\n const paymentMethod = pricing.paymentMethods.find(\n (entry): entry is PricingCryptoPaymentMethod =>\n entry.type === PAYMENT_TYPES.byCrypto &&\n entry.cryptoAuthMethod === CRYPTO_AUTH_METHODS.DELEGATION &&\n entry.products?.includes(product) === true,\n );\n const chain = paymentMethod?.chains?.find(\n (entry) => entry.chainId === chainId,\n );\n const token = chain?.tokens[0];\n if (!price || !chain?.delegateAddress || !token) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.PricingConfigurationNotFound,\n );\n }\n\n return {\n chainId,\n delegateAddress: chain.delegateAddress,\n enforcers,\n price,\n token,\n };\n }\n\n /**\n * Ensures an active CHOMP intent exists for the given delegation hash,\n * registering one when missing or revoked.\n *\n * @param params - Intent identity and metadata.\n * @param params.account - Delegator / payer address.\n * @param params.chainId - Chain ID of the delegation.\n * @param params.delegationHash - Hash of the stored delegation.\n * @param params.allowance - Period allowance stored with the delegation.\n * @param params.tokenSymbol - Payment token symbol.\n * @param params.tokenAddress - Payment token address.\n */\n async #ensureIntent(params: SubscriptionIntentParams): Promise<void> {\n const existingIntents = await this.#messenger.call(\n 'ChompApiService:getIntentsByAddress',\n params.account,\n );\n\n const hasActiveIntent = existingIntents.some(\n (intent) =>\n equalsIgnoreCase(intent.delegationHash, params.delegationHash) &&\n intent.status === 'active',\n );\n\n if (hasActiveIntent) {\n return;\n }\n\n await this.#createIntent(params);\n }\n\n async #createIntent(params: SubscriptionIntentParams): Promise<void> {\n // Published `@metamask/chomp-api-service` only types intent metadata as\n // `'cash-deposit' | 'cash-withdrawal'`. The dormant production path still\n // passes `'cash-subscription'`; a follow-up chomp-api-service release must\n // accept that discriminator before this path is production-ready. Alpha\n // callers must use `skipChompInteractions: true` so this method is not\n // reached.\n await this.#messenger.call('ChompApiService:createIntents', [\n {\n account: params.account,\n delegationHash: params.delegationHash,\n chainId: params.chainId,\n metadata: {\n allowance: params.allowance,\n tokenSymbol: params.tokenSymbol,\n tokenAddress: params.tokenAddress,\n type: CASH_SUBSCRIPTION_DELEGATION_TYPE as\n | 'cash-deposit'\n | 'cash-withdrawal',\n },\n },\n ]);\n }\n}\n"]}
1
+ {"version":3,"file":"SubscriptionDelegationService.js","sourceRoot":"","sources":["../../src/subscription-delegation/SubscriptionDelegationService.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D,OAAO,EAAE,cAAc,EAAE,MAAM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AC;QAED,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,MAAgC;QAClD,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,+BAA+B,EAAE;YAC1D;gBACE,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,cAAc,EAAE,MAAM,CAAC,cAAc;gBACrC,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,QAAQ,EAAE;oBACR,SAAS,EAAE,MAAM,CAAC,SAAS;oBAC3B,WAAW,EAAE,MAAM,CAAC,WAAW;oBAC/B,YAAY,EAAE,MAAM,CAAC,YAAY;oBACjC,IAAI,EAAE,iCAAiC;iBACxC;aACF;SACF,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n AddResult,\n ApprovalControllerAddRequestAction,\n} from '@metamask/approval-controller';\nimport type {\n DelegationResponse,\n AuthenticatedUserStorageServiceCreateDelegationAction,\n AuthenticatedUserStorageServiceListDelegationsAction,\n} from '@metamask/authenticated-user-storage';\nimport type {\n ChompApiServiceCreateIntentsAction,\n ChompApiServiceGetIntentsByAddressAction,\n ChompApiServiceVerifyDelegationAction,\n} from '@metamask/chomp-api-service';\nimport { ORIGIN_METAMASK } from '@metamask/controller-utils';\nimport type { DelegationControllerSignDelegationAction } from '@metamask/delegation-controller';\nimport { hashDelegation } from '@metamask/delegation-core';\nimport { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\nimport type { Messenger } from '@metamask/messenger';\nimport type { MoneyAccountBalanceServiceFetchBalanceWithFallbackAction } from '@metamask/money-account-balance-service';\nimport type { MoneyAccountUpgradeControllerForceUpgradeAccountAction } from '@metamask/money-account-upgrade-controller';\nimport {\n getMoneyAccountDepositAssetAddress,\n getMoneyAccountVaultConfig,\n MUSD_DECIMALS,\n} from '@metamask/money-account-utils';\nimport type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';\nimport { add0x, hexToNumber, isStrictHexString } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n ACTIVE_SUBSCRIPTION_STATUSES,\n SubscriptionControllerErrorMessage,\n SubscriptionDelegationServiceErrorMessage,\n} from '../constants.js';\nimport type {\n SubscriptionControllerGetPricingAction,\n SubscriptionControllerGetSubscriptionsAction,\n SubscriptionControllerStartSubscriptionWithCryptoAction,\n} from '../SubscriptionController-method-action-types.js';\nimport type { SubscriptionControllerGetStateAction } from '../SubscriptionController.js';\nimport { CRYPTO_AUTH_METHODS, PAYMENT_TYPES, PRODUCT_TYPES } from '../types.js';\nimport type {\n ProductPrice,\n ProductType,\n PricingCryptoPaymentMethod,\n RecurringInterval,\n Subscription,\n TokenPaymentInfo,\n} from '../types.js';\nimport {\n assertPositiveInteger,\n calculatePeriodAmount,\n getDelegationStartDate,\n getPeriodDuration,\n} from './amount.js';\nimport { buildUnsignedSubscriptionDelegation } from './caveats.js';\nimport {\n equalsIgnoreCase,\n makeMatchesSubscriptionDelegation,\n pickLatestMatchingSubscriptionDelegation,\n} from './fingerprint.js';\nimport type { SubscriptionDelegationServiceMethodActions } from './SubscriptionDelegationService-method-action-types.js';\nimport {\n buildDelegationTypedData,\n decodeSubscriptionAuthority,\n} from './typed-data.js';\nimport type {\n MoneyAccountBalanceCheckRequest,\n MoneyAccountBalanceCheckResult,\n PrepareSubscriptionDelegationRequest,\n PreparedSubscriptionDelegationBundle,\n PreparedSubscriptionPermission,\n PreparedSubscriptionDelegation,\n SignedSubscriptionDelegation,\n StartSubscriptionWithDelegationRequest,\n StartSubscriptionWithDelegationResult,\n SubscriptionDelegationApprovalResult,\n SubscriptionDelegationEnforcers,\n UnsignedSubscriptionDelegation,\n} from './types.js';\nimport {\n CASH_SUBSCRIPTION_DELEGATION_TYPE,\n SUBSCRIPTION_DELEGATION_APPROVAL_TYPE,\n SUBSCRIPTION_DELEGATION_POLICY_VERSION,\n} from './types.js';\n\n/**\n * The name of the {@link SubscriptionDelegationService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'SubscriptionDelegationService';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'prepareDelegation',\n 'checkMoneyAccountBalance',\n 'startSubscriptionWithDelegation',\n] as const;\n\nconst DELEGATION_FRAMEWORK_VERSION = '1.3.0';\n\nfunction resolveEnforcers(chainId: Hex): SubscriptionDelegationEnforcers {\n const contracts =\n DELEGATOR_CONTRACTS[DELEGATION_FRAMEWORK_VERSION]?.[hexToNumber(chainId)];\n\n if (\n !contracts?.ERC20PeriodTransferEnforcer ||\n !contracts.AllowedCalldataEnforcer\n ) {\n throw new Error(\n `${SubscriptionDelegationServiceErrorMessage.DelegationContractsNotFound}: ${chainId}`,\n );\n }\n\n return {\n erc20TokenPeriodTransfer: contracts.ERC20PeriodTransferEnforcer,\n allowedCalldata: contracts.AllowedCalldataEnforcer,\n };\n}\n\nfunction removeDelegationSignature(\n delegation: SignedSubscriptionDelegation,\n): UnsignedSubscriptionDelegation {\n const { signature: _signature, ...unsignedDelegation } = delegation;\n return unsignedDelegation;\n}\n\n/**\n * Actions that {@link SubscriptionDelegationService} exposes to other consumers.\n */\nexport type SubscriptionDelegationServiceActions =\n SubscriptionDelegationServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link SubscriptionDelegationServiceMessenger} calls.\n */\ntype AllowedActions =\n | AuthenticatedUserStorageServiceListDelegationsAction\n | AuthenticatedUserStorageServiceCreateDelegationAction\n | ChompApiServiceVerifyDelegationAction\n | ChompApiServiceCreateIntentsAction\n | ChompApiServiceGetIntentsByAddressAction\n | DelegationControllerSignDelegationAction\n | ApprovalControllerAddRequestAction\n | MoneyAccountUpgradeControllerForceUpgradeAccountAction\n | MoneyAccountBalanceServiceFetchBalanceWithFallbackAction\n | RemoteFeatureFlagControllerGetStateAction\n | SubscriptionControllerGetStateAction\n | SubscriptionControllerGetPricingAction\n | SubscriptionControllerGetSubscriptionsAction\n | SubscriptionControllerStartSubscriptionWithCryptoAction;\n\n/**\n * Events that {@link SubscriptionDelegationService} exposes to other consumers.\n */\nexport type SubscriptionDelegationServiceEvents = never;\n\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link SubscriptionDelegationService}.\n */\nexport type SubscriptionDelegationServiceMessenger = Messenger<\n typeof serviceName,\n SubscriptionDelegationServiceActions | AllowedActions,\n SubscriptionDelegationServiceEvents | AllowedEvents\n>;\n\n/**\n * Options for constructing {@link SubscriptionDelegationService}.\n */\nexport type SubscriptionDelegationServiceOptions = {\n messenger: SubscriptionDelegationServiceMessenger;\n};\n\ntype SubscriptionIntentParams = {\n account: Hex;\n chainId: Hex;\n delegationHash: Hex;\n allowance: Hex;\n tokenSymbol: string;\n tokenAddress: Hex;\n};\n\ntype ResolvedSubscriptionDelegationConfig = {\n chainId: Hex;\n delegateAddress: Hex;\n delegationManager: Hex;\n paymentAddress: Hex;\n enforcers: SubscriptionDelegationEnforcers;\n musdAddress: Hex;\n price: ProductPrice;\n token: TokenPaymentInfo;\n};\n\n/**\n * Stateless orchestrator for cash-subscription delegation setup.\n *\n * Owns the workflow: size periodic caveats → sign → CHOMP verify → persist to\n * Authenticated User Storage → register CHOMP intent. Returns a verified\n * `delegationHash` for `SubscriptionController.startSubscriptionWithCrypto`.\n *\n * Pass `skipChompInteractions: true` to skip CHOMP verify and intent\n * registration and return a locally computed delegation hash.\n *\n * Each call resolves the Money Account chain from remote feature flags, then\n * resolves its price, payment token, and delegate from `SubscriptionController`\n * pricing. The pricing `delegateAddress` is used as the delegation `delegate`.\n *\n * Does not own subscription state; `SubscriptionController` does not depend on\n * this service. Only Money Account Plus is supported.\n */\nexport class SubscriptionDelegationService {\n readonly name: typeof serviceName = serviceName;\n\n readonly #messenger: SubscriptionDelegationServiceMessenger;\n\n constructor(options: SubscriptionDelegationServiceOptions) {\n this.#messenger = options.messenger;\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Checks whether the Money Account holds enough convertible mUSD value to\n * cover pricing `unitAmount × minBillingCyclesForBalance`.\n *\n * @param request - Payer address and pricing amount fields.\n * @returns Balance comparison in mUSD base units (6 decimals).\n */\n async checkMoneyAccountBalance(\n request: MoneyAccountBalanceCheckRequest,\n ): Promise<MoneyAccountBalanceCheckResult> {\n const { price } = await this.#resolveConfiguration(\n request.product,\n request.recurringInterval,\n );\n return this.#compareMoneyAccountBalance(request.payerAddress, price);\n }\n\n async #compareMoneyAccountBalance(\n payerAddress: Hex,\n price: ProductPrice,\n ): Promise<MoneyAccountBalanceCheckResult> {\n assertPositiveInteger(\n price.minBillingCyclesForBalance,\n SubscriptionDelegationServiceErrorMessage.InvalidMinimumFundingCycles,\n );\n\n const periodAmount = calculatePeriodAmount({\n unitAmount: price.unitAmount,\n unitDecimals: price.unitDecimals,\n tokenDecimals: MUSD_DECIMALS,\n });\n const requiredBalance =\n periodAmount * BigInt(price.minBillingCyclesForBalance);\n\n const { totalBalance } = await this.#messenger.call(\n 'MoneyAccountBalanceService:fetchBalanceWithFallback',\n payerAddress,\n );\n\n return {\n hasSufficientBalance: BigInt(totalBalance) >= requiredBalance,\n balance: totalBalance,\n requiredBalance: requiredBalance.toString(),\n };\n }\n\n /**\n * Runs the complete Money Account subscription checkout authorization flow.\n *\n * Subscriptions are refreshed first. An active subscription for the product\n * is rejected before Money Account upgrade, approval, delegation signing,\n * persistence, or CHOMP registration.\n *\n * Before the approval, `MoneyAccountUpgradeController` is asked to ensure\n * the Money Account vault delegations and CHOMP intents exist; the\n * Subscription API validates them server-side. The custom approval is the\n * sole consent and funding boundary for the payment permission. No payment\n * delegation signing, persistence, or intent mutation occurs before it\n * returns a funding transaction hash.\n *\n * When `request.skipApproval` is `true`, the approval request and its\n * result validation are skipped and the flow proceeds directly to signing.\n * The caller is then responsible for consent and funding.\n *\n * @param request - Product selection, Money Account identity, and optional\n * `skipApproval` flag.\n * @returns The result from `SubscriptionController:startSubscriptionWithCrypto`.\n */\n async startSubscriptionWithDelegation(\n request: StartSubscriptionWithDelegationRequest,\n ): Promise<StartSubscriptionWithDelegationResult> {\n if (request.product !== PRODUCT_TYPES.MONEY_ACCOUNT_PLUS) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.UnsupportedProduct,\n );\n }\n\n const config = await this.#resolveConfiguration(\n request.product,\n request.recurringInterval,\n );\n if (!equalsIgnoreCase(request.chainId, config.chainId)) {\n throw new Error(SubscriptionDelegationServiceErrorMessage.ChainMismatch);\n }\n const subscriptions = await this.#messenger.call(\n 'SubscriptionController:getSubscriptions',\n );\n this.#assertUserNotSubscribed(subscriptions, request.product);\n const { trialedProducts } = this.#messenger.call(\n 'SubscriptionController:getState',\n );\n const isTrialRequested =\n config.price.trialPeriodDays > 0 &&\n !trialedProducts.includes(request.product);\n\n await this.#messenger.call(\n 'MoneyAccountUpgradeController:forceUpgradeAccount',\n request.payerAddress,\n );\n\n const paymentPermission = await this.#preparePaymentPermission(\n request,\n config,\n isTrialRequested,\n );\n const bundle: PreparedSubscriptionDelegationBundle = {\n policyVersion: SUBSCRIPTION_DELEGATION_POLICY_VERSION,\n account: request.payerAddress,\n chainId: request.chainId,\n permissions: [paymentPermission],\n };\n const approval = request.skipApproval\n ? undefined\n : await this.#requestApproval({\n request,\n price: config.price,\n musdAddress: config.musdAddress,\n bundle,\n });\n\n try {\n if (approval) {\n this.#validateApprovalResult(approval);\n }\n\n const paymentDelegation = await this.#signPaymentPermissionIfRequired(\n paymentPermission,\n config,\n );\n const paymentDelegationHash = await this.#commitPaymentPermission({\n request,\n paymentPermission,\n paymentDelegation,\n config,\n });\n const result = await this.#messenger.call(\n 'SubscriptionController:startSubscriptionWithCrypto',\n {\n products: [request.product],\n isTrialRequested,\n recurringInterval: request.recurringInterval,\n billingCycles: config.price.minBillingCycles,\n chainId: request.chainId,\n payerAddress: request.payerAddress,\n tokenSymbol: config.token.symbol,\n cryptoAuthMethod: CRYPTO_AUTH_METHODS.DELEGATION,\n delegationHash: paymentDelegationHash,\n // Reject if eligibility changed during approval, because the signed\n // delegation start date was derived from the original trial state.\n // This local guard is removed before the backend request.\n assertTrialEligibility: true,\n },\n );\n approval?.resultCallbacks?.success();\n return result;\n } catch (error) {\n approval?.resultCallbacks?.error(error as Error);\n throw error;\n }\n }\n\n /**\n * Rejects when the refreshed list already contains an active subscription\n * for the product. Active includes `active`, `trialing`, and `provisional`.\n *\n * @param subscriptions - Subscriptions returned by the latest refresh.\n * @param product - Product the caller is trying to start.\n */\n #assertUserNotSubscribed(\n subscriptions: Subscription[],\n product: ProductType,\n ): void {\n const alreadySubscribed = subscriptions.some(\n (subscription) =>\n ACTIVE_SUBSCRIPTION_STATUSES.includes(subscription.status) &&\n subscription.products.some((entry) => entry.name === product),\n );\n if (alreadySubscribed) {\n throw new Error(SubscriptionControllerErrorMessage.UserAlreadySubscribed);\n }\n }\n\n async #requestApproval({\n request,\n price,\n musdAddress,\n bundle,\n }: {\n request: StartSubscriptionWithDelegationRequest;\n price: ProductPrice;\n musdAddress: Hex;\n bundle: PreparedSubscriptionDelegationBundle;\n }): Promise<AddResult> {\n const targetAmount =\n calculatePeriodAmount({\n unitAmount: price.unitAmount,\n unitDecimals: price.unitDecimals,\n tokenDecimals: MUSD_DECIMALS,\n }) * BigInt(price.minBillingCyclesForBalance);\n\n return (await this.#messenger.call(\n 'ApprovalController:addRequest',\n {\n origin: ORIGIN_METAMASK,\n type: SUBSCRIPTION_DELEGATION_APPROVAL_TYPE,\n expectsResult: true,\n requestData: {\n bundle,\n funding: {\n useCase: 'subscription',\n destinationAccount: request.payerAddress,\n chainId: request.chainId,\n targetToken: {\n symbol: 'mUSD',\n address: musdAddress,\n },\n targetAmount: targetAmount.toString(),\n },\n },\n },\n true,\n )) as AddResult;\n }\n\n async #preparePaymentPermission(\n request: StartSubscriptionWithDelegationRequest,\n config: ResolvedSubscriptionDelegationConfig,\n isTrialRequested: boolean,\n ): Promise<PreparedSubscriptionPermission> {\n const periodAmount = calculatePeriodAmount({\n unitAmount: config.price.unitAmount,\n unitDecimals: config.price.unitDecimals,\n tokenDecimals: config.token.decimals,\n });\n const periodDuration = getPeriodDuration(request.recurringInterval);\n const nowSeconds = Math.floor(Date.now() / 1000);\n const startDate = getDelegationStartDate({\n nowSeconds,\n trialPeriodDays: isTrialRequested\n ? config.price.trialPeriodDays\n : undefined,\n });\n const matches = makeMatchesSubscriptionDelegation({\n delegatorAddress: request.payerAddress,\n delegateAddress: config.delegateAddress,\n chainId: request.chainId,\n recipientAddress: config.paymentAddress,\n tokenAddress: config.token.address,\n periodAmount,\n periodDuration,\n nowSeconds,\n isTrialDeferred: startDate > nowSeconds,\n enforcers: config.enforcers,\n });\n const reusable = pickLatestMatchingSubscriptionDelegation(\n (\n await this.#messenger.call(\n 'AuthenticatedUserStorageService:listDelegations',\n )\n ).filter(matches),\n config.enforcers,\n );\n const delegation = reusable\n ? removeDelegationSignature(reusable.signedDelegation)\n : buildUnsignedSubscriptionDelegation({\n delegateAddress: config.delegateAddress,\n delegatorAddress: request.payerAddress,\n enforcers: config.enforcers,\n recipientAddress: config.paymentAddress,\n tokenAddress: config.token.address,\n periodAmount,\n periodDuration,\n startDate,\n });\n const typedData = buildDelegationTypedData({\n delegation,\n chainId: request.chainId,\n delegationManager: config.delegationManager,\n });\n\n return {\n id: CASH_SUBSCRIPTION_DELEGATION_TYPE,\n owner: 'subscription',\n disposition: reusable ? 'reused' : 'new',\n delegation,\n typedData,\n decodedAuthority: decodeSubscriptionAuthority(\n delegation,\n config.enforcers,\n ),\n ...(reusable\n ? { existingDelegationHash: reusable.metadata.delegationHash }\n : {}),\n };\n }\n\n #validateApprovalResult(\n approval: AddResult,\n ): SubscriptionDelegationApprovalResult {\n if (!approval?.value || typeof approval.value !== 'object') {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ApprovalResultMissing,\n );\n }\n const value = approval.value as Record<string, unknown>;\n if (\n typeof value.fundingTransactionHash !== 'string' ||\n !isStrictHexString(value.fundingTransactionHash) ||\n value.fundingTransactionHash.length !== 66\n ) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.InvalidFundingTransactionHash,\n );\n }\n return {\n fundingTransactionHash: value.fundingTransactionHash,\n };\n }\n\n async #signPaymentPermissionIfRequired(\n permission: PreparedSubscriptionPermission,\n config: ResolvedSubscriptionDelegationConfig,\n ): Promise<SignedSubscriptionDelegation | undefined> {\n if (permission.disposition === 'reused') {\n return undefined;\n }\n const signature = (await this.#messenger.call(\n 'DelegationController:signDelegation',\n {\n delegation: permission.delegation,\n chainId: config.chainId,\n },\n )) as Hex;\n return { ...permission.delegation, signature };\n }\n\n async #commitPaymentPermission({\n request,\n paymentPermission,\n paymentDelegation,\n config,\n }: {\n request: StartSubscriptionWithDelegationRequest;\n paymentPermission: PreparedSubscriptionPermission;\n paymentDelegation?: SignedSubscriptionDelegation;\n config: ResolvedSubscriptionDelegationConfig;\n }): Promise<Hex> {\n if (paymentPermission.disposition === 'reused') {\n const delegationHash = paymentPermission.existingDelegationHash as Hex;\n const storedDelegation = (\n await this.#messenger.call(\n 'AuthenticatedUserStorageService:listDelegations',\n )\n ).find(({ metadata }) =>\n equalsIgnoreCase(metadata.delegationHash, delegationHash),\n );\n if (!storedDelegation) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ReusableDelegationInvalid,\n );\n }\n await this.#verifySignedPaymentDelegation(\n storedDelegation.signedDelegation,\n delegationHash,\n request.chainId,\n );\n await this.#ensureIntent({\n account: request.payerAddress,\n chainId: request.chainId,\n delegationHash,\n allowance: add0x(\n BigInt(paymentPermission.decodedAuthority.periodAmount).toString(16),\n ),\n tokenSymbol: config.token.symbol,\n tokenAddress: config.token.address,\n });\n await this.#assertIntentActive(request.payerAddress, delegationHash);\n return delegationHash;\n }\n\n const signedDelegation = paymentDelegation as SignedSubscriptionDelegation;\n const delegationHash = hashDelegation({\n ...signedDelegation,\n salt: BigInt(signedDelegation.salt),\n });\n await this.#verifySignedPaymentDelegation(\n signedDelegation,\n delegationHash,\n request.chainId,\n );\n\n const allowance = add0x(\n BigInt(paymentPermission.decodedAuthority.periodAmount).toString(16),\n );\n await this.#messenger.call(\n 'AuthenticatedUserStorageService:createDelegation',\n {\n signedDelegation,\n metadata: {\n delegationHash,\n chainIdHex: request.chainId,\n allowance,\n tokenSymbol: config.token.symbol,\n tokenAddress: config.token.address,\n type: CASH_SUBSCRIPTION_DELEGATION_TYPE,\n },\n },\n );\n await this.#createIntent({\n account: request.payerAddress,\n chainId: request.chainId,\n delegationHash,\n allowance,\n tokenSymbol: config.token.symbol,\n tokenAddress: config.token.address,\n });\n await this.#assertIntentActive(request.payerAddress, delegationHash);\n return delegationHash;\n }\n\n async #assertIntentActive(account: Hex, delegationHash: Hex): Promise<void> {\n const intents = await this.#messenger.call(\n 'ChompApiService:getIntentsByAddress',\n account,\n );\n const intent = intents.find((candidate) =>\n equalsIgnoreCase(candidate.delegationHash, delegationHash),\n );\n if (intent?.status !== 'active') {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ChompIntentNotActive,\n );\n }\n }\n\n async #verifySignedPaymentDelegation(\n signedDelegation: SignedSubscriptionDelegation,\n expectedDelegationHash: Hex,\n chainId: Hex,\n ): Promise<void> {\n const localHash = hashDelegation({\n ...signedDelegation,\n salt: BigInt(signedDelegation.salt),\n });\n if (!equalsIgnoreCase(localHash, expectedDelegationHash)) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ReusableDelegationInvalid,\n );\n }\n\n const verifyResult = await this.#messenger.call(\n 'ChompApiService:verifyDelegation',\n { signedDelegation, chainId },\n );\n if (!verifyResult.valid) {\n throw new Error(\n `${SubscriptionDelegationServiceErrorMessage.ChompRejectedDelegation}: ${\n verifyResult.errors?.join(', ') ?? 'unknown error'\n }`,\n );\n }\n if (!verifyResult.delegationHash) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ChompMissingDelegationHash,\n );\n }\n if (\n !equalsIgnoreCase(verifyResult.delegationHash, expectedDelegationHash)\n ) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ChompDelegationHashMismatch,\n );\n }\n }\n\n /**\n * Prepares a cash-subscription delegation and returns its hash.\n *\n * Reuses a stored AUS delegation that matches the semantic fingerprint when\n * one exists (ensuring a CHOMP intent is active for its hash, unless\n * `skipChompInteractions` is true). Reuse classifies period `startDate` as\n * trial-deferred (`> now`) vs immediately redeemable, matching creation.\n * When several records match, the latest period `startDate` is reused\n * so a `forceNew` replacement is preferred over an older equivalent\n * permission.\n * If there is no match, builds, signs, optionally verifies with CHOMP,\n * persists, and optionally registers a new delegation.\n *\n * When `skipChompInteractions` is true, CHOMP verify and intent calls are\n * skipped; the returned hash is computed locally.\n *\n * @param request - Authoritative pricing and payer details for the delegation.\n * @param forceNew - Whether to create a replacement instead of reusing a\n * matching stored delegation.\n * @returns The delegation hash (CHOMP-verified unless skipped) and whether it\n * was created or reused.\n */\n async prepareDelegation(\n request: PrepareSubscriptionDelegationRequest,\n forceNew = false,\n ): Promise<PreparedSubscriptionDelegation> {\n if (request.product !== PRODUCT_TYPES.MONEY_ACCOUNT_PLUS) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.UnsupportedProduct,\n );\n }\n\n const skipChomp = Boolean(request.skipChompInteractions);\n\n const {\n chainId,\n delegateAddress,\n paymentAddress,\n enforcers,\n price,\n token,\n } = await this.#resolveConfiguration(\n request.product,\n request.recurringInterval,\n );\n\n if (request.checkBalance) {\n const { hasSufficientBalance } = await this.#compareMoneyAccountBalance(\n request.payerAddress,\n price,\n );\n if (!hasSufficientBalance) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.InsufficientBalance,\n );\n }\n }\n\n const periodAmount = calculatePeriodAmount({\n unitAmount: price.unitAmount,\n unitDecimals: price.unitDecimals,\n tokenDecimals: token.decimals,\n });\n const periodDuration = getPeriodDuration(request.recurringInterval);\n const nowSeconds = Math.floor(Date.now() / 1000);\n const startDate = getDelegationStartDate({\n nowSeconds,\n trialPeriodDays: request.isTrialRequested\n ? price.trialPeriodDays\n : undefined,\n });\n const isTrialDeferred = startDate > nowSeconds;\n\n const reusable = forceNew\n ? undefined\n : await this.#findReusableDelegation({\n request,\n chainId,\n delegateAddress,\n paymentAddress,\n tokenAddress: token.address,\n periodAmount,\n periodDuration,\n nowSeconds,\n isTrialDeferred,\n enforcers,\n });\n if (reusable) {\n if (!skipChomp) {\n await this.#ensureIntent({\n account: request.payerAddress,\n chainId,\n delegationHash: reusable.metadata.delegationHash,\n allowance: reusable.metadata.allowance,\n tokenSymbol: reusable.metadata.tokenSymbol,\n tokenAddress: reusable.metadata.tokenAddress,\n });\n }\n return {\n delegationHash: reusable.metadata.delegationHash,\n disposition: 'reused',\n };\n }\n\n const unsigned = buildUnsignedSubscriptionDelegation({\n delegateAddress,\n delegatorAddress: request.payerAddress,\n recipientAddress: paymentAddress,\n enforcers,\n tokenAddress: token.address,\n periodAmount,\n periodDuration,\n startDate,\n });\n\n const signature = (await this.#messenger.call(\n 'DelegationController:signDelegation',\n { delegation: unsigned, chainId },\n )) as Hex;\n\n const signedDelegation = { ...unsigned, signature };\n\n const delegationHash = hashDelegation({\n ...unsigned,\n salt: BigInt(unsigned.salt),\n signature,\n });\n\n if (!skipChomp) {\n const verifyResult = await this.#messenger.call(\n 'ChompApiService:verifyDelegation',\n {\n signedDelegation,\n chainId,\n },\n );\n\n if (!verifyResult.valid) {\n throw new Error(\n `${SubscriptionDelegationServiceErrorMessage.ChompRejectedDelegation}: ${\n verifyResult.errors?.join(', ') ?? 'unknown error'\n }`,\n );\n }\n\n if (!verifyResult.delegationHash) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ChompMissingDelegationHash,\n );\n }\n if (!equalsIgnoreCase(verifyResult.delegationHash, delegationHash)) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.ChompDelegationHashMismatch,\n );\n }\n }\n\n const allowance: Hex = add0x(periodAmount.toString(16));\n\n await this.#messenger.call(\n 'AuthenticatedUserStorageService:createDelegation',\n {\n signedDelegation,\n metadata: {\n delegationHash,\n chainIdHex: chainId,\n allowance,\n tokenSymbol: token.symbol,\n tokenAddress: token.address,\n type: CASH_SUBSCRIPTION_DELEGATION_TYPE,\n },\n },\n );\n\n if (!skipChomp) {\n await this.#createIntent({\n account: request.payerAddress,\n chainId,\n delegationHash,\n allowance,\n tokenSymbol: token.symbol,\n tokenAddress: token.address,\n });\n }\n\n return {\n delegationHash,\n disposition: 'created',\n };\n }\n\n async #findReusableDelegation({\n request,\n chainId,\n delegateAddress,\n paymentAddress,\n tokenAddress,\n periodAmount,\n periodDuration,\n nowSeconds,\n isTrialDeferred,\n enforcers,\n }: {\n request: PrepareSubscriptionDelegationRequest;\n chainId: Hex;\n delegateAddress: Hex;\n paymentAddress: Hex;\n tokenAddress: Hex;\n periodAmount: bigint;\n periodDuration: number;\n nowSeconds: number;\n isTrialDeferred: boolean;\n enforcers: SubscriptionDelegationEnforcers;\n }): Promise<DelegationResponse | undefined> {\n const matches = makeMatchesSubscriptionDelegation({\n delegatorAddress: request.payerAddress,\n delegateAddress,\n recipientAddress: paymentAddress,\n chainId,\n tokenAddress,\n periodAmount,\n periodDuration,\n nowSeconds,\n isTrialDeferred,\n enforcers,\n });\n\n const existingDelegations = await this.#messenger.call(\n 'AuthenticatedUserStorageService:listDelegations',\n );\n\n return pickLatestMatchingSubscriptionDelegation(\n existingDelegations.filter(matches),\n enforcers,\n );\n }\n\n async #resolveConfiguration(\n product: ProductType,\n recurringInterval: RecurringInterval,\n ): Promise<ResolvedSubscriptionDelegationConfig> {\n const { remoteFeatureFlags } = this.#messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n const vaultConfig = getMoneyAccountVaultConfig(remoteFeatureFlags);\n if (!vaultConfig) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.MissingMoneyAccountVaultConfig,\n );\n }\n\n const { chainId } = vaultConfig;\n const enforcers = resolveEnforcers(chainId);\n const pricing = await this.#messenger.call(\n 'SubscriptionController:getPricing',\n );\n const price = pricing.products\n .find((entry) => entry.name === product)\n ?.prices?.find((entry) => entry.interval === recurringInterval);\n const paymentMethod = pricing.paymentMethods.find(\n (entry): entry is PricingCryptoPaymentMethod =>\n entry.type === PAYMENT_TYPES.byCrypto &&\n entry.cryptoAuthMethod === CRYPTO_AUTH_METHODS.DELEGATION &&\n entry.products?.includes(product) === true,\n );\n const chain = paymentMethod?.chains?.find(\n (entry) => entry.chainId === chainId,\n );\n const token = chain?.tokens[0];\n if (!price || !chain?.delegateAddress || !token) {\n throw new Error(\n SubscriptionDelegationServiceErrorMessage.PricingConfigurationNotFound,\n );\n }\n\n return {\n chainId,\n delegateAddress: chain.delegateAddress,\n delegationManager:\n DELEGATOR_CONTRACTS[DELEGATION_FRAMEWORK_VERSION][hexToNumber(chainId)]\n .DelegationManager,\n paymentAddress: chain.paymentAddress,\n enforcers,\n musdAddress: getMoneyAccountDepositAssetAddress(chainId),\n price,\n token,\n };\n }\n\n /**\n * Ensures an active CHOMP intent exists for the given delegation hash,\n * registering one when missing or revoked.\n *\n * @param params - Intent identity and metadata.\n * @param params.account - Delegator / payer address.\n * @param params.chainId - Chain ID of the delegation.\n * @param params.delegationHash - Hash of the stored delegation.\n * @param params.allowance - Period allowance stored with the delegation.\n * @param params.tokenSymbol - Payment token symbol.\n * @param params.tokenAddress - Payment token address.\n */\n async #ensureIntent(params: SubscriptionIntentParams): Promise<void> {\n const existingIntents = await this.#messenger.call(\n 'ChompApiService:getIntentsByAddress',\n params.account,\n );\n\n const hasActiveIntent = existingIntents.some(\n (intent) =>\n equalsIgnoreCase(intent.delegationHash, params.delegationHash) &&\n intent.status === 'active',\n );\n\n if (hasActiveIntent) {\n return;\n }\n\n await this.#createIntent(params);\n }\n\n async #createIntent(params: SubscriptionIntentParams): Promise<void> {\n await this.#messenger.call('ChompApiService:createIntents', [\n {\n account: params.account,\n delegationHash: params.delegationHash,\n chainId: params.chainId,\n metadata: {\n allowance: params.allowance,\n tokenSymbol: params.tokenSymbol,\n tokenAddress: params.tokenAddress,\n type: CASH_SUBSCRIPTION_DELEGATION_TYPE,\n },\n },\n ]);\n }\n}\n"]}