@metamask-previews/notification-services-controller 10.0.0-preview-69122ec → 10.0.0-preview-53749f1

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 +20 -0
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs +67 -188
  3. package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
  4. package/dist/NotificationServicesController/NotificationServicesController.d.cts +10 -16
  5. package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
  6. package/dist/NotificationServicesController/NotificationServicesController.d.mts +10 -16
  7. package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
  8. package/dist/NotificationServicesController/NotificationServicesController.mjs +68 -189
  9. package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
  10. package/dist/NotificationServicesController/constants/index.cjs +0 -1
  11. package/dist/NotificationServicesController/constants/index.cjs.map +1 -1
  12. package/dist/NotificationServicesController/constants/index.d.cts +0 -1
  13. package/dist/NotificationServicesController/constants/index.d.cts.map +1 -1
  14. package/dist/NotificationServicesController/constants/index.d.mts +0 -1
  15. package/dist/NotificationServicesController/constants/index.d.mts.map +1 -1
  16. package/dist/NotificationServicesController/constants/index.mjs +0 -1
  17. package/dist/NotificationServicesController/constants/index.mjs.map +1 -1
  18. package/dist/NotificationServicesController/mocks/index.cjs +0 -2
  19. package/dist/NotificationServicesController/mocks/index.cjs.map +1 -1
  20. package/dist/NotificationServicesController/mocks/index.d.cts +0 -2
  21. package/dist/NotificationServicesController/mocks/index.d.cts.map +1 -1
  22. package/dist/NotificationServicesController/mocks/index.d.mts +0 -2
  23. package/dist/NotificationServicesController/mocks/index.d.mts.map +1 -1
  24. package/dist/NotificationServicesController/mocks/index.mjs +0 -2
  25. package/dist/NotificationServicesController/mocks/index.mjs.map +1 -1
  26. package/dist/NotificationServicesController/mocks/mockResponses.cjs +9 -9
  27. package/dist/NotificationServicesController/mocks/mockResponses.cjs.map +1 -1
  28. package/dist/NotificationServicesController/mocks/mockResponses.d.cts +7 -4
  29. package/dist/NotificationServicesController/mocks/mockResponses.d.cts.map +1 -1
  30. package/dist/NotificationServicesController/mocks/mockResponses.d.mts +7 -4
  31. package/dist/NotificationServicesController/mocks/mockResponses.d.mts.map +1 -1
  32. package/dist/NotificationServicesController/mocks/mockResponses.mjs +7 -7
  33. package/dist/NotificationServicesController/mocks/mockResponses.mjs.map +1 -1
  34. package/dist/NotificationServicesController/processors/process-notifications.cjs +4 -1
  35. package/dist/NotificationServicesController/processors/process-notifications.cjs.map +1 -1
  36. package/dist/NotificationServicesController/processors/process-notifications.d.cts +623 -0
  37. package/dist/NotificationServicesController/processors/process-notifications.d.cts.map +1 -1
  38. package/dist/NotificationServicesController/processors/process-notifications.d.mts +623 -0
  39. package/dist/NotificationServicesController/processors/process-notifications.d.mts.map +1 -1
  40. package/dist/NotificationServicesController/processors/process-notifications.mjs +2 -0
  41. package/dist/NotificationServicesController/processors/process-notifications.mjs.map +1 -1
  42. package/dist/NotificationServicesController/services/onchain-notifications.cjs +50 -142
  43. package/dist/NotificationServicesController/services/onchain-notifications.cjs.map +1 -1
  44. package/dist/NotificationServicesController/services/onchain-notifications.d.cts +23 -31
  45. package/dist/NotificationServicesController/services/onchain-notifications.d.cts.map +1 -1
  46. package/dist/NotificationServicesController/services/onchain-notifications.d.mts +23 -31
  47. package/dist/NotificationServicesController/services/onchain-notifications.d.mts.map +1 -1
  48. package/dist/NotificationServicesController/services/onchain-notifications.mjs +48 -139
  49. package/dist/NotificationServicesController/services/onchain-notifications.mjs.map +1 -1
  50. package/dist/NotificationServicesController/types/index.cjs.map +1 -1
  51. package/dist/NotificationServicesController/types/index.d.cts +0 -1
  52. package/dist/NotificationServicesController/types/index.d.cts.map +1 -1
  53. package/dist/NotificationServicesController/types/index.d.mts +0 -1
  54. package/dist/NotificationServicesController/types/index.d.mts.map +1 -1
  55. package/dist/NotificationServicesController/types/index.mjs.map +1 -1
  56. package/dist/NotificationServicesController/utils/utils.cjs +1 -350
  57. package/dist/NotificationServicesController/utils/utils.cjs.map +1 -1
  58. package/dist/NotificationServicesController/utils/utils.d.cts +0 -141
  59. package/dist/NotificationServicesController/utils/utils.d.cts.map +1 -1
  60. package/dist/NotificationServicesController/utils/utils.d.mts +0 -141
  61. package/dist/NotificationServicesController/utils/utils.d.mts.map +1 -1
  62. package/dist/NotificationServicesController/utils/utils.mjs +0 -337
  63. package/dist/NotificationServicesController/utils/utils.mjs.map +1 -1
  64. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +20 -14
  65. package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
  66. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +6 -5
  67. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
  68. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +6 -5
  69. package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
  70. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +21 -15
  71. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
  72. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs +1 -16
  73. package/dist/NotificationServicesPushController/mocks/mockResponse.cjs.map +1 -1
  74. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts +0 -7
  75. package/dist/NotificationServicesPushController/mocks/mockResponse.d.cts.map +1 -1
  76. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts +0 -7
  77. package/dist/NotificationServicesPushController/mocks/mockResponse.d.mts.map +1 -1
  78. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs +0 -14
  79. package/dist/NotificationServicesPushController/mocks/mockResponse.mjs.map +1 -1
  80. package/dist/NotificationServicesPushController/services/endpoints.cjs +1 -1
  81. package/dist/NotificationServicesPushController/services/endpoints.cjs.map +1 -1
  82. package/dist/NotificationServicesPushController/services/endpoints.d.cts +1 -1
  83. package/dist/NotificationServicesPushController/services/endpoints.d.cts.map +1 -1
  84. package/dist/NotificationServicesPushController/services/endpoints.d.mts +1 -1
  85. package/dist/NotificationServicesPushController/services/endpoints.d.mts.map +1 -1
  86. package/dist/NotificationServicesPushController/services/endpoints.mjs +1 -1
  87. package/dist/NotificationServicesPushController/services/endpoints.mjs.map +1 -1
  88. package/dist/NotificationServicesPushController/services/services.cjs +18 -41
  89. package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
  90. package/dist/NotificationServicesPushController/services/services.d.cts +19 -33
  91. package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
  92. package/dist/NotificationServicesPushController/services/services.d.mts +19 -33
  93. package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
  94. package/dist/NotificationServicesPushController/services/services.mjs +17 -39
  95. package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
  96. package/package.json +3 -3
  97. package/dist/NotificationServicesController/constants/constants.cjs +0 -7
  98. package/dist/NotificationServicesController/constants/constants.cjs.map +0 -1
  99. package/dist/NotificationServicesController/constants/constants.d.cts +0 -3
  100. package/dist/NotificationServicesController/constants/constants.d.cts.map +0 -1
  101. package/dist/NotificationServicesController/constants/constants.d.mts +0 -3
  102. package/dist/NotificationServicesController/constants/constants.d.mts.map +0 -1
  103. package/dist/NotificationServicesController/constants/constants.mjs +0 -4
  104. package/dist/NotificationServicesController/constants/constants.mjs.map +0 -1
  105. package/dist/NotificationServicesController/mocks/mock-notification-trigger.cjs +0 -22
  106. package/dist/NotificationServicesController/mocks/mock-notification-trigger.cjs.map +0 -1
  107. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.cts +0 -9
  108. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.cts.map +0 -1
  109. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.mts +0 -9
  110. package/dist/NotificationServicesController/mocks/mock-notification-trigger.d.mts.map +0 -1
  111. package/dist/NotificationServicesController/mocks/mock-notification-trigger.mjs +0 -18
  112. package/dist/NotificationServicesController/mocks/mock-notification-trigger.mjs.map +0 -1
  113. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.cjs +0 -82
  114. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.cjs.map +0 -1
  115. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.cts +0 -35
  116. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.cts.map +0 -1
  117. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.mts +0 -35
  118. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.d.mts.map +0 -1
  119. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.mjs +0 -76
  120. package/dist/NotificationServicesController/mocks/mock-notification-user-storage.mjs.map +0 -1
  121. package/dist/NotificationServicesController/types/user-storage/index.cjs +0 -3
  122. package/dist/NotificationServicesController/types/user-storage/index.cjs.map +0 -1
  123. package/dist/NotificationServicesController/types/user-storage/index.d.cts +0 -2
  124. package/dist/NotificationServicesController/types/user-storage/index.d.cts.map +0 -1
  125. package/dist/NotificationServicesController/types/user-storage/index.d.mts +0 -2
  126. package/dist/NotificationServicesController/types/user-storage/index.d.mts.map +0 -1
  127. package/dist/NotificationServicesController/types/user-storage/index.mjs +0 -2
  128. package/dist/NotificationServicesController/types/user-storage/index.mjs.map +0 -1
  129. package/dist/NotificationServicesController/types/user-storage/user-storage.cjs +0 -3
  130. package/dist/NotificationServicesController/types/user-storage/user-storage.cjs.map +0 -1
  131. package/dist/NotificationServicesController/types/user-storage/user-storage.d.cts +0 -26
  132. package/dist/NotificationServicesController/types/user-storage/user-storage.d.cts.map +0 -1
  133. package/dist/NotificationServicesController/types/user-storage/user-storage.d.mts +0 -26
  134. package/dist/NotificationServicesController/types/user-storage/user-storage.d.mts.map +0 -1
  135. package/dist/NotificationServicesController/types/user-storage/user-storage.mjs +0 -2
  136. package/dist/NotificationServicesController/types/user-storage/user-storage.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -7,6 +7,26 @@ 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
+
10
30
  ## [10.0.0]
11
31
 
12
32
  ### 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_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;
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;
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,12 +44,10 @@ 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");
48
47
  const notification_schema_1 = require("./constants/notification-schema.cjs");
49
48
  const process_notifications_1 = require("./processors/process-notifications.cjs");
50
49
  const FeatureNotifications = __importStar(require("./services/feature-announcements.cjs"));
51
50
  const OnChainNotifications = __importStar(require("./services/onchain-notifications.cjs"));
52
- const Utils = __importStar(require("./utils/utils.cjs"));
53
51
  // Unique name for the controller
54
52
  const controllerName = 'NotificationServicesController';
55
53
  const metadata = {
@@ -159,17 +157,6 @@ class NotificationServicesController extends base_controller_1.BaseController {
159
157
  return await this.messagingSystem.call('AuthenticationController:performSignIn');
160
158
  },
161
159
  });
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
- });
173
160
  _NotificationServicesController_pushNotifications.set(this, {
174
161
  // Flag to check is notifications have been setup when the browser/extension is initialized.
175
162
  // We want to re-initialize push notifications when the browser/extension is refreshed
@@ -178,9 +165,9 @@ class NotificationServicesController extends base_controller_1.BaseController {
178
165
  subscribeToPushNotifications: async () => {
179
166
  await this.messagingSystem.call('NotificationServicesPushController:subscribeToPushNotifications');
180
167
  },
181
- enablePushNotifications: async (UUIDs) => {
168
+ enablePushNotifications: async (addresses) => {
182
169
  try {
183
- await this.messagingSystem.call('NotificationServicesPushController:enablePushNotifications', UUIDs);
170
+ await this.messagingSystem.call('NotificationServicesPushController:enablePushNotifications', addresses);
184
171
  }
185
172
  catch (e) {
186
173
  loglevel_1.default.error('Silently failed to enable push notifications', e);
@@ -194,14 +181,6 @@ class NotificationServicesController extends base_controller_1.BaseController {
194
181
  loglevel_1.default.error('Silently failed to disable push notifications', e);
195
182
  }
196
183
  },
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
- },
205
184
  subscribe: () => {
206
185
  this.messagingSystem.subscribe('NotificationServicesPushController:onNewNotifications', (notification) => {
207
186
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
@@ -236,7 +215,7 @@ class NotificationServicesController extends base_controller_1.BaseController {
236
215
  _NotificationServicesController_accounts.set(this, {
237
216
  // Flag to ensure we only setup once
238
217
  isNotificationAccountsSetup: false,
239
- getNotificationAccounts: async () => {
218
+ getNotificationAccounts: () => {
240
219
  const { keyrings } = this.messagingSystem.call('KeyringController:getState');
241
220
  const firstHDKeyring = keyrings.find((k) => k.type === keyring_controller_1.KeyringTypes.hd.toString());
242
221
  const keyringAccounts = firstHDKeyring?.accounts ?? null;
@@ -247,9 +226,9 @@ class NotificationServicesController extends base_controller_1.BaseController {
247
226
  *
248
227
  * @returns addresses removed, added, and latest list of addresses
249
228
  */
250
- listAccounts: async () => {
229
+ listAccounts: () => {
251
230
  // Get previous and current account sets
252
- const nonChecksumAccounts = await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").getNotificationAccounts();
231
+ const nonChecksumAccounts = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").getNotificationAccounts();
253
232
  if (!nonChecksumAccounts) {
254
233
  return {
255
234
  accountsAdded: [],
@@ -286,13 +265,11 @@ class NotificationServicesController extends base_controller_1.BaseController {
286
265
  },
287
266
  /**
288
267
  * 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
291
268
  */
292
- initialize: async () => {
269
+ initialize: () => {
293
270
  if (__classPrivateFieldGet(this, _NotificationServicesController_keyringController, "f").isUnlocked &&
294
271
  !__classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").isNotificationAccountsSetup) {
295
- await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
272
+ __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
296
273
  __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").isNotificationAccountsSetup = true;
297
274
  }
298
275
  },
@@ -308,15 +285,15 @@ class NotificationServicesController extends base_controller_1.BaseController {
308
285
  !hasTotalAccountsChanged) {
309
286
  return;
310
287
  }
311
- const { accountsAdded, accountsRemoved } = await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
288
+ const { accountsAdded, accountsRemoved } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
312
289
  const promises = [];
313
290
  if (accountsAdded.length > 0) {
314
- promises.push(this.updateOnChainTriggersByAccount(accountsAdded));
291
+ promises.push(this.enableAccounts(accountsAdded));
315
292
  }
316
293
  if (accountsRemoved.length > 0) {
317
- promises.push(this.deleteOnChainTriggersByAccount(accountsRemoved));
294
+ promises.push(this.disableAccounts(accountsRemoved));
318
295
  }
319
- await Promise.all(promises);
296
+ await Promise.allSettled(promises);
320
297
  }, (state) => {
321
298
  return (state?.keyrings?.flatMap?.((keyring) => keyring.accounts)?.length ??
322
299
  0);
@@ -330,10 +307,9 @@ class NotificationServicesController extends base_controller_1.BaseController {
330
307
  }
331
308
  init() {
332
309
  __classPrivateFieldGet(this, _NotificationServicesController_keyringController, "f").setupLockedStateSubscriptions(async () => {
333
- await __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").initialize();
310
+ __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").initialize();
334
311
  await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").initializePushNotifications();
335
312
  });
336
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
337
313
  __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").initialize();
338
314
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
339
315
  __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").initializePushNotifications();
@@ -344,13 +320,18 @@ class NotificationServicesController extends base_controller_1.BaseController {
344
320
  * Public method to expose enabling push notifications
345
321
  */
346
322
  async enablePushNotifications() {
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');
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);
351
334
  }
352
- const uuids = Utils.getAllUUIDs(storage);
353
- await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(uuids);
354
335
  }
355
336
  /**
356
337
  * Public method to expose disabling push notifications
@@ -362,10 +343,13 @@ class NotificationServicesController extends base_controller_1.BaseController {
362
343
  try {
363
344
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsCheckingAccountsPresence).call(this, true);
364
345
  // Retrieve user storage
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;
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;
369
353
  }
370
354
  catch (error) {
371
355
  loglevel_1.default.error('Failed to check accounts presence', error);
@@ -409,40 +393,26 @@ class NotificationServicesController extends base_controller_1.BaseController {
409
393
  async createOnChainTriggers(opts) {
410
394
  try {
411
395
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsUpdatingMetamaskNotifications).call(this, true);
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));
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;
427
403
  }
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(() => {
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(() => {
435
408
  // Do Nothing
436
409
  });
437
- // Write the new userStorage (triggers are now "enabled")
438
- await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").setNotificationStorage(JSON.stringify(userStorage));
439
410
  // Update the state of the controller
440
411
  this.update((state) => {
441
412
  state.isNotificationServicesEnabled = true;
442
413
  state.isFeatureAnnouncementsEnabled = true;
443
414
  state.isMetamaskNotificationsFeatureSeen = true;
444
415
  });
445
- return userStorage;
446
416
  }
447
417
  catch (err) {
448
418
  loglevel_1.default.error('Failed to create On Chain triggers', err);
@@ -455,10 +425,6 @@ class NotificationServicesController extends base_controller_1.BaseController {
455
425
  /**
456
426
  * Enables all MetaMask notifications for the user.
457
427
  * 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
462
428
  *
463
429
  * @throws {Error} If there is an error during the process of enabling notifications.
464
430
  */
@@ -506,12 +472,11 @@ class NotificationServicesController extends base_controller_1.BaseController {
506
472
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsUpdatingMetamaskNotifications).call(this, false);
507
473
  }
508
474
  /**
509
- * Deletes on-chain triggers associated with a specific account.
475
+ * Deletes on-chain triggers associated with a specific account/s.
510
476
  * This method performs several key operations:
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.
477
+ * 1. Validates Auth
478
+ * 2. Deletes accounts
479
+ * (note) We do not need to look through push notifications as we've deleted triggers
515
480
  *
516
481
  * **Action** - When a user disables notifications for a given account in settings.
517
482
  *
@@ -519,29 +484,13 @@ class NotificationServicesController extends base_controller_1.BaseController {
519
484
  * @returns A promise that resolves to void or an object containing a success message.
520
485
  * @throws {Error} Throws an error if unauthenticated or from other operations.
521
486
  */
522
- async deleteOnChainTriggersByAccount(accounts) {
487
+ async disableAccounts(accounts) {
523
488
  try {
524
489
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
525
490
  // Get and Validate BearerToken and User Storage Key
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
- }
491
+ const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
537
492
  // Delete these UUIDs (Mutates User Storage)
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;
493
+ await OnChainNotifications.updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: false })));
545
494
  }
546
495
  catch (err) {
547
496
  loglevel_1.default.error('Failed to delete OnChain triggers', err);
@@ -566,45 +515,11 @@ class NotificationServicesController extends base_controller_1.BaseController {
566
515
  * @returns A promise that resolves to the updated user storage.
567
516
  * @throws {Error} Throws an error if unauthenticated or from other operations.
568
517
  */
569
- async updateOnChainTriggersByAccount(accounts) {
518
+ async enableAccounts(accounts) {
570
519
  try {
571
520
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
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;
521
+ const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
522
+ await OnChainNotifications.updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: true })));
608
523
  }
609
524
  catch (err) {
610
525
  loglevel_1.default.error('Failed to update OnChain triggers', err);
@@ -631,37 +546,30 @@ class NotificationServicesController extends base_controller_1.BaseController {
631
546
  // Not used by Snaps
632
547
  const isGlobalNotifsEnabled = this.state.isNotificationServicesEnabled;
633
548
  // Raw Feature Notifications
634
- const rawFeatureAnnouncementNotifications = isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled
549
+ const rawAnnouncements = isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled
635
550
  ? await FeatureNotifications.getFeatureAnnouncementNotifications(__classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f"), previewToken).catch(() => [])
636
551
  : [];
637
552
  // Raw On Chain Notifications
638
553
  const rawOnChainNotifications = [];
639
554
  if (isGlobalNotifsEnabled) {
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(() => []);
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(() => []);
647
559
  rawOnChainNotifications.push(...notifications);
648
560
  }
561
+ catch {
562
+ // Do nothing
563
+ }
649
564
  }
650
565
  // Snap Notifications (original)
651
566
  // We do not want to remove them
652
567
  const snapNotifications = this.state.metamaskNotificationsList.filter((notification) => notification.type === notification_schema_1.TRIGGER_TYPES.SNAP);
653
- // Process Notifications
654
568
  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);
661
569
  // Combine Notifications
662
570
  const metamaskNotifications = [
663
- ...featureAnnouncementNotifications,
664
- ...onChainNotifications,
571
+ ...(0, process_notifications_1.processAndFilterNotifications)(rawAnnouncements, readIds),
572
+ ...(0, process_notifications_1.processAndFilterNotifications)(rawOnChainNotifications, readIds),
665
573
  ...snapNotifications,
666
574
  ];
667
575
  // Sort Notifications
@@ -829,7 +737,7 @@ class NotificationServicesController extends base_controller_1.BaseController {
829
737
  }
830
738
  }
831
739
  }
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() {
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() {
833
741
  this.messagingSystem.registerActionHandler(`${controllerName}:updateMetamaskNotificationsList`, this.updateMetamaskNotificationsList.bind(this));
834
742
  this.messagingSystem.registerActionHandler(`${controllerName}:disableNotificationServices`, this.disableNotificationServices.bind(this));
835
743
  this.messagingSystem.registerActionHandler(`${controllerName}:getNotificationsByType`, this.getNotificationsByType.bind(this));
@@ -853,42 +761,13 @@ _NotificationServicesController_keyringController = new WeakMap(), _Notification
853
761
  if (!isSignedIn) {
854
762
  await __classPrivateFieldGet(this, _NotificationServicesController_auth, "f").signIn();
855
763
  }
856
- }, _NotificationServicesController_getValidStorageKeyAndBearerToken = async function _NotificationServicesController_getValidStorageKeyAndBearerToken() {
764
+ }, _NotificationServicesController_getBearerToken = async function _NotificationServicesController_getBearerToken() {
857
765
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_assertAuthEnabled).call(this);
858
766
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesController_auth, "f").getBearerToken();
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;
767
+ if (!bearerToken) {
768
+ throw new Error('Missing BearerToken');
891
769
  }
770
+ return { bearerToken };
892
771
  }, _NotificationServicesController_setIsUpdatingMetamaskNotifications = function _NotificationServicesController_setIsUpdatingMetamaskNotifications(isUpdatingMetamaskNotifications) {
893
772
  this.update((state) => {
894
773
  state.isUpdatingMetamaskNotifications = isUpdatingMetamaskNotifications;