@metamask-previews/notification-services-controller 10.0.0-preview-53749f1 → 10.0.0-preview-1a1e4b40

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 (136) hide show
  1. package/CHANGELOG.md +0 -20
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs +188 -67
  3. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  4. package/dist/NotificationServicesController/NotificationServicesController.d.cts +16 -10
  5. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  6. package/dist/NotificationServicesController/NotificationServicesController.d.mts +16 -10
  7. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  8. package/dist/NotificationServicesController/NotificationServicesController.mjs +189 -68
  9. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  10. package/dist/NotificationServicesController/constants/constants.cjs +7 -0
  11. package/dist/NotificationServicesController/constants/constants.cjs.map +1 -0
  12. package/dist/NotificationServicesController/constants/constants.d.cts +3 -0
  13. package/dist/NotificationServicesController/constants/constants.d.cts.map +1 -0
  14. package/dist/NotificationServicesController/constants/constants.d.mts +3 -0
  15. package/dist/NotificationServicesController/constants/constants.d.mts.map +1 -0
  16. package/dist/NotificationServicesController/constants/constants.mjs +4 -0
  17. package/dist/NotificationServicesController/constants/constants.mjs.map +1 -0
  18. package/dist/NotificationServicesController/constants/index.cjs +1 -0
  19. package/dist/NotificationServicesController/constants/index.cjs.map +1 -1
  20. package/dist/NotificationServicesController/constants/index.d.cts +1 -0
  21. package/dist/NotificationServicesController/constants/index.d.cts.map +1 -1
  22. package/dist/NotificationServicesController/constants/index.d.mts +1 -0
  23. package/dist/NotificationServicesController/constants/index.d.mts.map +1 -1
  24. package/dist/NotificationServicesController/constants/index.mjs +1 -0
  25. package/dist/NotificationServicesController/constants/index.mjs.map +1 -1
  26. package/dist/NotificationServicesController/mocks/index.cjs +2 -0
  27. package/dist/NotificationServicesController/mocks/index.cjs.map +1 -1
  28. package/dist/NotificationServicesController/mocks/index.d.cts +2 -0
  29. package/dist/NotificationServicesController/mocks/index.d.cts.map +1 -1
  30. package/dist/NotificationServicesController/mocks/index.d.mts +2 -0
  31. package/dist/NotificationServicesController/mocks/index.d.mts.map +1 -1
  32. package/dist/NotificationServicesController/mocks/index.mjs +2 -0
  33. package/dist/NotificationServicesController/mocks/index.mjs.map +1 -1
  34. package/dist/NotificationServicesController/mocks/mock-notification-trigger.cjs +22 -0
  35. package/dist/NotificationServicesController/mocks/mock-notification-trigger.cjs.map +1 -0
  36. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.cts +9 -0
  37. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.cts.map +1 -0
  38. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.mts +9 -0
  39. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.mts.map +1 -0
  40. package/dist/NotificationServicesController/mocks/mock-notification-trigger.mjs +18 -0
  41. package/dist/NotificationServicesController/mocks/mock-notification-trigger.mjs.map +1 -0
  42. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.cjs +82 -0
  43. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.cjs.map +1 -0
  44. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.cts +35 -0
  45. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.cts.map +1 -0
  46. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.mts +35 -0
  47. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.mts.map +1 -0
  48. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.mjs +76 -0
  49. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.mjs.map +1 -0
  50. package/dist/NotificationServicesController/mocks/mockResponses.cjs +9 -9
  51. package/dist/NotificationServicesController/mocks/mockResponses.cjs.map +1 -1
  52. package/dist/NotificationServicesController/mocks/mockResponses.d.cts +4 -7
  53. package/dist/NotificationServicesController/mocks/mockResponses.d.cts.map +1 -1
  54. package/dist/NotificationServicesController/mocks/mockResponses.d.mts +4 -7
  55. package/dist/NotificationServicesController/mocks/mockResponses.d.mts.map +1 -1
  56. package/dist/NotificationServicesController/mocks/mockResponses.mjs +7 -7
  57. package/dist/NotificationServicesController/mocks/mockResponses.mjs.map +1 -1
  58. package/dist/NotificationServicesController/processors/process-notifications.cjs +1 -4
  59. package/dist/NotificationServicesController/processors/process-notifications.cjs.map +1 -1
  60. package/dist/NotificationServicesController/processors/process-notifications.d.cts +0 -623
  61. package/dist/NotificationServicesController/processors/process-notifications.d.cts.map +1 -1
  62. package/dist/NotificationServicesController/processors/process-notifications.d.mts +0 -623
  63. package/dist/NotificationServicesController/processors/process-notifications.d.mts.map +1 -1
  64. package/dist/NotificationServicesController/processors/process-notifications.mjs +0 -2
  65. package/dist/NotificationServicesController/processors/process-notifications.mjs.map +1 -1
  66. package/dist/NotificationServicesController/services/onchain-notifications.cjs +142 -50
  67. package/dist/NotificationServicesController/services/onchain-notifications.cjs.map +1 -1
  68. package/dist/NotificationServicesController/services/onchain-notifications.d.cts +31 -23
  69. package/dist/NotificationServicesController/services/onchain-notifications.d.cts.map +1 -1
  70. package/dist/NotificationServicesController/services/onchain-notifications.d.mts +31 -23
  71. package/dist/NotificationServicesController/services/onchain-notifications.d.mts.map +1 -1
  72. package/dist/NotificationServicesController/services/onchain-notifications.mjs +139 -48
  73. package/dist/NotificationServicesController/services/onchain-notifications.mjs.map +1 -1
  74. package/dist/NotificationServicesController/types/index.cjs.map +1 -1
  75. package/dist/NotificationServicesController/types/index.d.cts +1 -0
  76. package/dist/NotificationServicesController/types/index.d.cts.map +1 -1
  77. package/dist/NotificationServicesController/types/index.d.mts +1 -0
  78. package/dist/NotificationServicesController/types/index.d.mts.map +1 -1
  79. package/dist/NotificationServicesController/types/index.mjs.map +1 -1
  80. package/dist/NotificationServicesController/types/user-storage/index.cjs +3 -0
  81. package/dist/NotificationServicesController/types/user-storage/index.cjs.map +1 -0
  82. package/dist/NotificationServicesController/types/user-storage/index.d.cts +2 -0
  83. package/dist/NotificationServicesController/types/user-storage/index.d.cts.map +1 -0
  84. package/dist/NotificationServicesController/types/user-storage/index.d.mts +2 -0
  85. package/dist/NotificationServicesController/types/user-storage/index.d.mts.map +1 -0
  86. package/dist/NotificationServicesController/types/user-storage/index.mjs +2 -0
  87. package/dist/NotificationServicesController/types/user-storage/index.mjs.map +1 -0
  88. package/dist/NotificationServicesController/types/user-storage/user-storage.cjs +3 -0
  89. package/dist/NotificationServicesController/types/user-storage/user-storage.cjs.map +1 -0
  90. package/dist/NotificationServicesController/types/user-storage/user-storage.d.cts +26 -0
  91. package/dist/NotificationServicesController/types/user-storage/user-storage.d.cts.map +1 -0
  92. package/dist/NotificationServicesController/types/user-storage/user-storage.d.mts +26 -0
  93. package/dist/NotificationServicesController/types/user-storage/user-storage.d.mts.map +1 -0
  94. package/dist/NotificationServicesController/types/user-storage/user-storage.mjs +2 -0
  95. package/dist/NotificationServicesController/types/user-storage/user-storage.mjs.map +1 -0
  96. package/dist/NotificationServicesController/utils/utils.cjs +350 -1
  97. package/dist/NotificationServicesController/utils/utils.cjs.map +1 -1
  98. package/dist/NotificationServicesController/utils/utils.d.cts +141 -0
  99. package/dist/NotificationServicesController/utils/utils.d.cts.map +1 -1
  100. package/dist/NotificationServicesController/utils/utils.d.mts +141 -0
  101. package/dist/NotificationServicesController/utils/utils.d.mts.map +1 -1
  102. package/dist/NotificationServicesController/utils/utils.mjs +337 -0
  103. package/dist/NotificationServicesController/utils/utils.mjs.map +1 -1
  104. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +14 -20
  105. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  106. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +5 -6
  107. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  108. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +5 -6
  109. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  110. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +15 -21
  111. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  112. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs +16 -1
  113. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs.map +1 -1
  114. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts +7 -0
  115. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts.map +1 -1
  116. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts +7 -0
  117. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts.map +1 -1
  118. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs +14 -0
  119. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs.map +1 -1
  120. package/dist/NotificationServicesPushController/services/endpoints.cjs +1 -1
  121. package/dist/NotificationServicesPushController/services/endpoints.cjs.map +1 -1
  122. package/dist/NotificationServicesPushController/services/endpoints.d.cts +1 -1
  123. package/dist/NotificationServicesPushController/services/endpoints.d.cts.map +1 -1
  124. package/dist/NotificationServicesPushController/services/endpoints.d.mts +1 -1
  125. package/dist/NotificationServicesPushController/services/endpoints.d.mts.map +1 -1
  126. package/dist/NotificationServicesPushController/services/endpoints.mjs +1 -1
  127. package/dist/NotificationServicesPushController/services/endpoints.mjs.map +1 -1
  128. package/dist/NotificationServicesPushController/services/services.cjs +41 -18
  129. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  130. package/dist/NotificationServicesPushController/services/services.d.cts +33 -19
  131. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  132. package/dist/NotificationServicesPushController/services/services.d.mts +33 -19
  133. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  134. package/dist/NotificationServicesPushController/services/services.mjs +39 -17
  135. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  136. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -7,26 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ### Changed
11
-
12
- - **BREAKING:** Migrated to notification v2 endpoints ([#5945](https://github.com/MetaMask/core/pull/5945))
13
-
14
- - `https://trigger.api.cx.metamask.io/api/v2` for managing out notification subscriptions
15
- - `https://notification.api.cx.metamask.io/api/v2` for fetching notifications (in-app notifications)
16
- - `https://push.api.cx.metamask.io/v2` for subscribing push notifications
17
- - Renamed method `updateOnChainTriggersByAccount` to `enableAccounts` in `NotificationServicesController`
18
- - Renamed method `deleteOnChainTriggersByAccount` to `disableAccounts` in `NotificationServicesController`
19
- - Deprecated `updateTriggerPushNotifications` from `NotificationServicesPushController` and will be removed in a subsequent release.
20
-
21
- - Bump `@metamask/controller-utils` to `^11.10.0` ([#5935](https://github.com/MetaMask/core/pull/5935))
22
-
23
- ### Removed
24
-
25
- - **BREAKING:** Migrated to notification v2 endpoints ([#5945](https://github.com/MetaMask/core/pull/5945))
26
-
27
- - removed `NotificationServicesPushController:updateTriggerPushNotifications` action from `NotificationServicesController`
28
- - removed `UserStorageController` usage from `NotificationServicesController`.
29
-
30
10
  ## [10.0.0]
31
11
 
32
12
  ### Changed
@@ -36,7 +36,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
36
36
  var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  return (mod && mod.__esModule) ? mod : { "default": mod };
38
38
  };
39
- var _NotificationServicesController_instances, _NotificationServicesController_keyringController, _NotificationServicesController_auth, _NotificationServicesController_pushNotifications, _NotificationServicesController_accounts, _NotificationServicesController_featureAnnouncementEnv, _NotificationServicesController_registerMessageHandlers, _NotificationServicesController_clearLoadingStates, _NotificationServicesController_assertAuthEnabled, _NotificationServicesController_enableAuth, _NotificationServicesController_getBearerToken, _NotificationServicesController_setIsUpdatingMetamaskNotifications, _NotificationServicesController_setIsFetchingMetamaskNotifications, _NotificationServicesController_setIsCheckingAccountsPresence, _NotificationServicesController_updateUpdatingAccountsState, _NotificationServicesController_clearUpdatingAccountsState;
39
+ var _NotificationServicesController_instances, _NotificationServicesController_keyringController, _NotificationServicesController_auth, _NotificationServicesController_storage, _NotificationServicesController_pushNotifications, _NotificationServicesController_accounts, _NotificationServicesController_featureAnnouncementEnv, _NotificationServicesController_registerMessageHandlers, _NotificationServicesController_clearLoadingStates, _NotificationServicesController_assertAuthEnabled, _NotificationServicesController_enableAuth, _NotificationServicesController_getValidStorageKeyAndBearerToken, _NotificationServicesController_assertUserStorage, _NotificationServicesController_getUserStorage, _NotificationServicesController_setIsUpdatingMetamaskNotifications, _NotificationServicesController_setIsFetchingMetamaskNotifications, _NotificationServicesController_setIsCheckingAccountsPresence, _NotificationServicesController_updateUpdatingAccountsState, _NotificationServicesController_clearUpdatingAccountsState;
40
40
  Object.defineProperty(exports, "__esModule", { value: true });
41
41
  exports.defaultState = void 0;
42
42
  const base_controller_1 = require("@metamask/base-controller");
@@ -44,10 +44,12 @@ const controller_utils_1 = require("@metamask/controller-utils");
44
44
  const keyring_controller_1 = require("@metamask/keyring-controller");
45
45
  const utils_1 = require("@metamask/utils");
46
46
  const loglevel_1 = __importDefault(require("loglevel"));
47
+ const constants_1 = require("./constants/constants.cjs");
47
48
  const notification_schema_1 = require("./constants/notification-schema.cjs");
48
49
  const process_notifications_1 = require("./processors/process-notifications.cjs");
49
50
  const FeatureNotifications = __importStar(require("./services/feature-announcements.cjs"));
50
51
  const OnChainNotifications = __importStar(require("./services/onchain-notifications.cjs"));
52
+ const Utils = __importStar(require("./utils/utils.cjs"));
51
53
  // Unique name for the controller
52
54
  const controllerName = 'NotificationServicesController';
53
55
  const metadata = {
@@ -157,6 +159,17 @@ class NotificationServicesController extends base_controller_1.BaseController {
157
159
  return await this.messagingSystem.call('AuthenticationController:performSignIn');
158
160
  },
159
161
  });
162
+ _NotificationServicesController_storage.set(this, {
163
+ getStorageKey: () => {
164
+ return this.messagingSystem.call('UserStorageController:getStorageKey');
165
+ },
166
+ getNotificationStorage: async () => {
167
+ return await this.messagingSystem.call('UserStorageController:performGetStorage', 'notifications.notification_settings');
168
+ },
169
+ setNotificationStorage: async (state) => {
170
+ return await this.messagingSystem.call('UserStorageController:performSetStorage', 'notifications.notification_settings', state);
171
+ },
172
+ });
160
173
  _NotificationServicesController_pushNotifications.set(this, {
161
174
  // Flag to check is notifications have been setup when the browser/extension is initialized.
162
175
  // We want to re-initialize push notifications when the browser/extension is refreshed
@@ -165,9 +178,9 @@ class NotificationServicesController extends base_controller_1.BaseController {
165
178
  subscribeToPushNotifications: async () => {
166
179
  await this.messagingSystem.call('NotificationServicesPushController:subscribeToPushNotifications');
167
180
  },
168
- enablePushNotifications: async (addresses) => {
181
+ enablePushNotifications: async (UUIDs) => {
169
182
  try {
170
- await this.messagingSystem.call('NotificationServicesPushController:enablePushNotifications', addresses);
183
+ await this.messagingSystem.call('NotificationServicesPushController:enablePushNotifications', UUIDs);
171
184
  }
172
185
  catch (e) {
173
186
  loglevel_1.default.error('Silently failed to enable push notifications', e);
@@ -181,6 +194,14 @@ class NotificationServicesController extends base_controller_1.BaseController {
181
194
  loglevel_1.default.error('Silently failed to disable push notifications', e);
182
195
  }
183
196
  },
197
+ updatePushNotifications: async (UUIDs) => {
198
+ try {
199
+ await this.messagingSystem.call('NotificationServicesPushController:updateTriggerPushNotifications', UUIDs);
200
+ }
201
+ catch (e) {
202
+ loglevel_1.default.error('Silently failed to update push notifications', e);
203
+ }
204
+ },
184
205
  subscribe: () => {
185
206
  this.messagingSystem.subscribe('NotificationServicesPushController:onNewNotifications', (notification) => {
186
207
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
@@ -215,7 +236,7 @@ class NotificationServicesController extends base_controller_1.BaseController {
215
236
  _NotificationServicesController_accounts.set(this, {
216
237
  // Flag to ensure we only setup once
217
238
  isNotificationAccountsSetup: false,
218
- getNotificationAccounts: () => {
239
+ getNotificationAccounts: async () => {
219
240
  const { keyrings } = this.messagingSystem.call('KeyringController:getState');
220
241
  const firstHDKeyring = keyrings.find((k) => k.type === keyring_controller_1.KeyringTypes.hd.toString());
221
242
  const keyringAccounts = firstHDKeyring?.accounts ?? null;
@@ -226,9 +247,9 @@ class NotificationServicesController extends base_controller_1.BaseController {
226
247
  *
227
248
  * @returns addresses removed, added, and latest list of addresses
228
249
  */
229
- listAccounts: () => {
250
+ listAccounts: async () => {
230
251
  // Get previous and current account sets
231
- const nonChecksumAccounts = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").getNotificationAccounts();
252
+ const nonChecksumAccounts = await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").getNotificationAccounts();
232
253
  if (!nonChecksumAccounts) {
233
254
  return {
234
255
  accountsAdded: [],
@@ -265,11 +286,13 @@ class NotificationServicesController extends base_controller_1.BaseController {
265
286
  },
266
287
  /**
267
288
  * Initializes the cache/previous list. This is handy so we have an accurate in-mem state of the previous list of accounts.
289
+ *
290
+ * @returns result from list accounts
268
291
  */
269
- initialize: () => {
292
+ initialize: async () => {
270
293
  if (__classPrivateFieldGet(this, _NotificationServicesController_keyringController, "f").isUnlocked &&
271
294
  !__classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").isNotificationAccountsSetup) {
272
- __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
295
+ await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
273
296
  __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").isNotificationAccountsSetup = true;
274
297
  }
275
298
  },
@@ -285,15 +308,15 @@ class NotificationServicesController extends base_controller_1.BaseController {
285
308
  !hasTotalAccountsChanged) {
286
309
  return;
287
310
  }
288
- const { accountsAdded, accountsRemoved } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
311
+ const { accountsAdded, accountsRemoved } = await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
289
312
  const promises = [];
290
313
  if (accountsAdded.length > 0) {
291
- promises.push(this.enableAccounts(accountsAdded));
314
+ promises.push(this.updateOnChainTriggersByAccount(accountsAdded));
292
315
  }
293
316
  if (accountsRemoved.length > 0) {
294
- promises.push(this.disableAccounts(accountsRemoved));
317
+ promises.push(this.deleteOnChainTriggersByAccount(accountsRemoved));
295
318
  }
296
- await Promise.allSettled(promises);
319
+ await Promise.all(promises);
297
320
  }, (state) => {
298
321
  return (state?.keyrings?.flatMap?.((keyring) => keyring.accounts)?.length ??
299
322
  0);
@@ -307,9 +330,10 @@ class NotificationServicesController extends base_controller_1.BaseController {
307
330
  }
308
331
  init() {
309
332
  __classPrivateFieldGet(this, _NotificationServicesController_keyringController, "f").setupLockedStateSubscriptions(async () => {
310
- __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").initialize();
333
+ await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").initialize();
311
334
  await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").initializePushNotifications();
312
335
  });
336
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
313
337
  __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").initialize();
314
338
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
315
339
  __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").initializePushNotifications();
@@ -320,18 +344,13 @@ class NotificationServicesController extends base_controller_1.BaseController {
320
344
  * Public method to expose enabling push notifications
321
345
  */
322
346
  async enablePushNotifications() {
323
- try {
324
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
325
- const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
326
- const addressesWithNotifications = await OnChainNotifications.getOnChainNotificationsConfig(bearerToken, accounts);
327
- const addresses = addressesWithNotifications.map((a) => a.address);
328
- if (addresses.length > 0) {
329
- await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(addresses);
330
- }
331
- }
332
- catch (e) {
333
- loglevel_1.default.error('Failed to enable push notifications', e);
347
+ await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_enableAuth).call(this);
348
+ const storage = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getUserStorage).call(this);
349
+ if (!storage) {
350
+ throw new Error('Unable to get triggers');
334
351
  }
352
+ const uuids = Utils.getAllUUIDs(storage);
353
+ await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(uuids);
335
354
  }
336
355
  /**
337
356
  * Public method to expose disabling push notifications
@@ -343,13 +362,10 @@ class NotificationServicesController extends base_controller_1.BaseController {
343
362
  try {
344
363
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsCheckingAccountsPresence).call(this, true);
345
364
  // Retrieve user storage
346
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
347
- const addressesWithNotifications = await OnChainNotifications.getOnChainNotificationsConfig(bearerToken, accounts);
348
- const result = {};
349
- addressesWithNotifications.forEach((a) => {
350
- result[a.address] = a.enabled;
351
- });
352
- return result;
365
+ const userStorage = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getUserStorage).call(this);
366
+ __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_assertUserStorage).call(this, userStorage);
367
+ const presence = Utils.checkAccountsPresence(userStorage, accounts);
368
+ return presence;
353
369
  }
354
370
  catch (error) {
355
371
  loglevel_1.default.error('Failed to check accounts presence', error);
@@ -393,26 +409,40 @@ class NotificationServicesController extends base_controller_1.BaseController {
393
409
  async createOnChainTriggers(opts) {
394
410
  try {
395
411
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsUpdatingMetamaskNotifications).call(this, true);
396
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
397
- const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
398
- // 1. See if has enabled notifications before
399
- const addressesWithNotifications = await OnChainNotifications.getOnChainNotificationsConfig(bearerToken, accounts);
400
- // Notifications have been enabled before, so returning early
401
- if (addressesWithNotifications.length > 0 && !opts?.resetNotifications) {
402
- return;
412
+ const { bearerToken, storageKey } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getValidStorageKeyAndBearerToken).call(this);
413
+ const { accounts } = await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
414
+ // Attempt Get User Storage
415
+ // Will be null if entry does not exist, or a user is resetting their notifications
416
+ // Will be defined if entry exists
417
+ // Will throw if fails to get the user storage entry
418
+ let userStorage = opts?.resetNotifications
419
+ ? null
420
+ : await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getUserStorage).call(this);
421
+ // If userStorage does not exist, create a new one
422
+ // All the triggers created are set as: "disabled"
423
+ if (userStorage?.[constants_1.USER_STORAGE_VERSION_KEY] === undefined) {
424
+ userStorage = Utils.initializeUserStorage(accounts.map((account) => ({ address: account })), false);
425
+ // Write the userStorage
426
+ await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").setNotificationStorage(JSON.stringify(userStorage));
403
427
  }
404
- // 2. Create Notifications
405
- await OnChainNotifications.updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: true })));
406
- // 3. Lazily enable push notifications (FCM may take some time, so keeps UI unblocked)
407
- __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(accounts).catch(() => {
428
+ // Create the triggers
429
+ const triggers = Utils.traverseUserStorageTriggers(userStorage);
430
+ await OnChainNotifications.createOnChainTriggers(userStorage, storageKey, bearerToken, triggers);
431
+ // Create push notifications triggers in background
432
+ const allUUIDS = Utils.getAllUUIDs(userStorage);
433
+ // We do not want to wait for this request as it may take a while (e.g. for Firebase to setup)
434
+ __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(allUUIDS).catch(() => {
408
435
  // Do Nothing
409
436
  });
437
+ // Write the new userStorage (triggers are now "enabled")
438
+ await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").setNotificationStorage(JSON.stringify(userStorage));
410
439
  // Update the state of the controller
411
440
  this.update((state) => {
412
441
  state.isNotificationServicesEnabled = true;
413
442
  state.isFeatureAnnouncementsEnabled = true;
414
443
  state.isMetamaskNotificationsFeatureSeen = true;
415
444
  });
445
+ return userStorage;
416
446
  }
417
447
  catch (err) {
418
448
  loglevel_1.default.error('Failed to create On Chain triggers', err);
@@ -425,6 +455,10 @@ class NotificationServicesController extends base_controller_1.BaseController {
425
455
  /**
426
456
  * Enables all MetaMask notifications for the user.
427
457
  * This is identical flow when initializing notifications for the first time.
458
+ * 1. Enable Profile Syncing
459
+ * 2. Get or Create Notification User Storage
460
+ * 3. Upsert Triggers
461
+ * 4. Update Push notifications
428
462
  *
429
463
  * @throws {Error} If there is an error during the process of enabling notifications.
430
464
  */
@@ -472,11 +506,12 @@ class NotificationServicesController extends base_controller_1.BaseController {
472
506
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsUpdatingMetamaskNotifications).call(this, false);
473
507
  }
474
508
  /**
475
- * Deletes on-chain triggers associated with a specific account/s.
509
+ * Deletes on-chain triggers associated with a specific account.
476
510
  * This method performs several key operations:
477
- * 1. Validates Auth
478
- * 2. Deletes accounts
479
- * (note) We do not need to look through push notifications as we've deleted triggers
511
+ * 1. Validates Auth & Storage
512
+ * 2. Finds and deletes all triggers associated with the account
513
+ * 3. Disables any related push notifications
514
+ * 4. Updates Storage to reflect new state.
480
515
  *
481
516
  * **Action** - When a user disables notifications for a given account in settings.
482
517
  *
@@ -484,13 +519,29 @@ class NotificationServicesController extends base_controller_1.BaseController {
484
519
  * @returns A promise that resolves to void or an object containing a success message.
485
520
  * @throws {Error} Throws an error if unauthenticated or from other operations.
486
521
  */
487
- async disableAccounts(accounts) {
522
+ async deleteOnChainTriggersByAccount(accounts) {
488
523
  try {
489
524
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
490
525
  // Get and Validate BearerToken and User Storage Key
491
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
526
+ const { bearerToken, storageKey } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getValidStorageKeyAndBearerToken).call(this);
527
+ // Get & Validate User Storage
528
+ const userStorage = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getUserStorage).call(this);
529
+ __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_assertUserStorage).call(this, userStorage);
530
+ // Get the UUIDs to delete
531
+ const UUIDs = accounts
532
+ .map((a) => Utils.getUUIDsForAccount(userStorage, a.toLowerCase()))
533
+ .flat();
534
+ if (UUIDs.length === 0) {
535
+ return userStorage;
536
+ }
492
537
  // Delete these UUIDs (Mutates User Storage)
493
- await OnChainNotifications.updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: false })));
538
+ await OnChainNotifications.deleteOnChainTriggers(userStorage, storageKey, bearerToken, UUIDs);
539
+ // Update Push Notifications with new list of IDs
540
+ const remainingTriggerIds = Utils.getAllUUIDs(userStorage);
541
+ await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").updatePushNotifications(remainingTriggerIds);
542
+ // Update User Storage
543
+ await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").setNotificationStorage(JSON.stringify(userStorage));
544
+ return userStorage;
494
545
  }
495
546
  catch (err) {
496
547
  loglevel_1.default.error('Failed to delete OnChain triggers', err);
@@ -515,11 +566,45 @@ class NotificationServicesController extends base_controller_1.BaseController {
515
566
  * @returns A promise that resolves to the updated user storage.
516
567
  * @throws {Error} Throws an error if unauthenticated or from other operations.
517
568
  */
518
- async enableAccounts(accounts) {
569
+ async updateOnChainTriggersByAccount(accounts) {
519
570
  try {
520
571
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
521
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
522
- await OnChainNotifications.updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: true })));
572
+ // Get and Validate BearerToken and User Storage Key
573
+ const { bearerToken, storageKey } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getValidStorageKeyAndBearerToken).call(this);
574
+ // Get & Validate User Storage
575
+ const userStorage = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getUserStorage).call(this);
576
+ __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_assertUserStorage).call(this, userStorage);
577
+ // Add any missing triggers
578
+ accounts.forEach((a) => Utils.upsertAddressTriggers(a, userStorage));
579
+ const newTriggers = Utils.traverseUserStorageTriggers(userStorage, {
580
+ mapTrigger: (t) => {
581
+ if (!t.enabled) {
582
+ return t;
583
+ }
584
+ return undefined;
585
+ },
586
+ });
587
+ // Create any missing triggers.
588
+ if (newTriggers.length > 0) {
589
+ // Write te updated userStorage (where triggers are disabled)
590
+ await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").setNotificationStorage(JSON.stringify(userStorage));
591
+ // Create the triggers
592
+ const triggers = Utils.traverseUserStorageTriggers(userStorage, {
593
+ mapTrigger: (t) => {
594
+ if (accounts.some((a) => a.toLowerCase() === t.address.toLowerCase())) {
595
+ return t;
596
+ }
597
+ return undefined;
598
+ },
599
+ });
600
+ await OnChainNotifications.createOnChainTriggers(userStorage, storageKey, bearerToken, triggers);
601
+ }
602
+ // Update Push Notifications Triggers
603
+ const UUIDs = Utils.getAllUUIDs(userStorage);
604
+ await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").updatePushNotifications(UUIDs);
605
+ // Update the userStorage (where triggers are enabled)
606
+ await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").setNotificationStorage(JSON.stringify(userStorage));
607
+ return userStorage;
523
608
  }
524
609
  catch (err) {
525
610
  loglevel_1.default.error('Failed to update OnChain triggers', err);
@@ -546,30 +631,37 @@ class NotificationServicesController extends base_controller_1.BaseController {
546
631
  // Not used by Snaps
547
632
  const isGlobalNotifsEnabled = this.state.isNotificationServicesEnabled;
548
633
  // Raw Feature Notifications
549
- const rawAnnouncements = isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled
634
+ const rawFeatureAnnouncementNotifications = isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled
550
635
  ? await FeatureNotifications.getFeatureAnnouncementNotifications(__classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f"), previewToken).catch(() => [])
551
636
  : [];
552
637
  // Raw On Chain Notifications
553
638
  const rawOnChainNotifications = [];
554
639
  if (isGlobalNotifsEnabled) {
555
- try {
556
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
557
- const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
558
- const notifications = await OnChainNotifications.getOnChainNotifications(bearerToken, accounts).catch(() => []);
640
+ const userStorage = await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f")
641
+ .getNotificationStorage()
642
+ .then((s) => s && JSON.parse(s))
643
+ .catch(() => null);
644
+ const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesController_auth, "f").getBearerToken().catch(() => null);
645
+ if (userStorage && bearerToken) {
646
+ const notifications = await OnChainNotifications.getOnChainNotifications(userStorage, bearerToken).catch(() => []);
559
647
  rawOnChainNotifications.push(...notifications);
560
648
  }
561
- catch {
562
- // Do nothing
563
- }
564
649
  }
565
650
  // Snap Notifications (original)
566
651
  // We do not want to remove them
567
652
  const snapNotifications = this.state.metamaskNotificationsList.filter((notification) => notification.type === notification_schema_1.TRIGGER_TYPES.SNAP);
653
+ // Process Notifications
568
654
  const readIds = this.state.metamaskNotificationsReadList;
655
+ const isNotUndefined = (t) => Boolean(t);
656
+ const processAndFilter = (ns) => ns
657
+ .map((n) => (0, process_notifications_1.safeProcessNotification)(n, readIds))
658
+ .filter(isNotUndefined);
659
+ const featureAnnouncementNotifications = processAndFilter(rawFeatureAnnouncementNotifications);
660
+ const onChainNotifications = processAndFilter(rawOnChainNotifications);
569
661
  // Combine Notifications
570
662
  const metamaskNotifications = [
571
- ...(0, process_notifications_1.processAndFilterNotifications)(rawAnnouncements, readIds),
572
- ...(0, process_notifications_1.processAndFilterNotifications)(rawOnChainNotifications, readIds),
663
+ ...featureAnnouncementNotifications,
664
+ ...onChainNotifications,
573
665
  ...snapNotifications,
574
666
  ];
575
667
  // Sort Notifications
@@ -737,7 +829,7 @@ class NotificationServicesController extends base_controller_1.BaseController {
737
829
  }
738
830
  }
739
831
  }
740
- _NotificationServicesController_keyringController = new WeakMap(), _NotificationServicesController_auth = new WeakMap(), _NotificationServicesController_pushNotifications = new WeakMap(), _NotificationServicesController_accounts = new WeakMap(), _NotificationServicesController_featureAnnouncementEnv = new WeakMap(), _NotificationServicesController_instances = new WeakSet(), _NotificationServicesController_registerMessageHandlers = function _NotificationServicesController_registerMessageHandlers() {
832
+ _NotificationServicesController_keyringController = new WeakMap(), _NotificationServicesController_auth = new WeakMap(), _NotificationServicesController_storage = new WeakMap(), _NotificationServicesController_pushNotifications = new WeakMap(), _NotificationServicesController_accounts = new WeakMap(), _NotificationServicesController_featureAnnouncementEnv = new WeakMap(), _NotificationServicesController_instances = new WeakSet(), _NotificationServicesController_registerMessageHandlers = function _NotificationServicesController_registerMessageHandlers() {
741
833
  this.messagingSystem.registerActionHandler(`${controllerName}:updateMetamaskNotificationsList`, this.updateMetamaskNotificationsList.bind(this));
742
834
  this.messagingSystem.registerActionHandler(`${controllerName}:disableNotificationServices`, this.disableNotificationServices.bind(this));
743
835
  this.messagingSystem.registerActionHandler(`${controllerName}:getNotificationsByType`, this.getNotificationsByType.bind(this));
@@ -761,13 +853,42 @@ _NotificationServicesController_keyringController = new WeakMap(), _Notification
761
853
  if (!isSignedIn) {
762
854
  await __classPrivateFieldGet(this, _NotificationServicesController_auth, "f").signIn();
763
855
  }
764
- }, _NotificationServicesController_getBearerToken = async function _NotificationServicesController_getBearerToken() {
856
+ }, _NotificationServicesController_getValidStorageKeyAndBearerToken = async function _NotificationServicesController_getValidStorageKeyAndBearerToken() {
765
857
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_assertAuthEnabled).call(this);
766
858
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesController_auth, "f").getBearerToken();
767
- if (!bearerToken) {
768
- throw new Error('Missing BearerToken');
859
+ const storageKey = await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").getStorageKey();
860
+ if (!bearerToken || !storageKey) {
861
+ throw new Error('Missing BearerToken or storage key');
862
+ }
863
+ return { bearerToken, storageKey };
864
+ }, _NotificationServicesController_assertUserStorage = function _NotificationServicesController_assertUserStorage(storage) {
865
+ if (!storage) {
866
+ throw new Error('User Storage does not exist');
867
+ }
868
+ }, _NotificationServicesController_getUserStorage =
869
+ /**
870
+ * Retrieves and parses the user storage from the storage key.
871
+ *
872
+ * This method attempts to retrieve the user storage using the specified storage key,
873
+ * then parses the JSON string to an object. If the storage is not found or cannot be parsed,
874
+ * it throws an error.
875
+ *
876
+ * @returns The parsed user storage object or null
877
+ */
878
+ async function _NotificationServicesController_getUserStorage() {
879
+ const userStorageString = await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").getNotificationStorage();
880
+ if (!userStorageString) {
881
+ return null;
882
+ }
883
+ try {
884
+ const userStorage = JSON.parse(userStorageString);
885
+ Utils.cleanUserStorage(userStorage);
886
+ return userStorage;
887
+ }
888
+ catch {
889
+ loglevel_1.default.error('Unable to parse User Storage');
890
+ return null;
769
891
  }
770
- return { bearerToken };
771
892
  }, _NotificationServicesController_setIsUpdatingMetamaskNotifications = function _NotificationServicesController_setIsUpdatingMetamaskNotifications(isUpdatingMetamaskNotifications) {
772
893
  this.update((state) => {
773
894
  state.isUpdatingMetamaskNotifications = isUpdatingMetamaskNotifications;