@metamask-previews/notification-services-controller 10.0.0-preview-7dcef379 → 10.0.0-preview-5dcecbb7

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 (144) hide show
  1. package/CHANGELOG.md +0 -16
  2. package/dist/NotificationServicesController/NotificationServicesController.cjs +188 -76
  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 -77
  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 +140 -67
  67. package/dist/NotificationServicesController/services/onchain-notifications.cjs.map +1 -1
  68. package/dist/NotificationServicesController/services/onchain-notifications.d.cts +31 -25
  69. package/dist/NotificationServicesController/services/onchain-notifications.d.cts.map +1 -1
  70. package/dist/NotificationServicesController/services/onchain-notifications.d.mts +31 -25
  71. package/dist/NotificationServicesController/services/onchain-notifications.d.mts.map +1 -1
  72. package/dist/NotificationServicesController/services/onchain-notifications.mjs +137 -65
  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 +15 -22
  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 +16 -23
  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 +1 -1
  137. package/dist/NotificationServicesController/services/notification-config-cache.cjs +0 -72
  138. package/dist/NotificationServicesController/services/notification-config-cache.cjs.map +0 -1
  139. package/dist/NotificationServicesController/services/notification-config-cache.d.cts +0 -19
  140. package/dist/NotificationServicesController/services/notification-config-cache.d.cts.map +0 -1
  141. package/dist/NotificationServicesController/services/notification-config-cache.d.mts +0 -19
  142. package/dist/NotificationServicesController/services/notification-config-cache.d.mts.map +0 -1
  143. package/dist/NotificationServicesController/services/notification-config-cache.mjs +0 -68
  144. package/dist/NotificationServicesController/services/notification-config-cache.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -9,24 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Changed
11
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
12
  - Bump `@metamask/controller-utils` to `^11.10.0` ([#5935](https://github.com/MetaMask/core/pull/5935))
22
13
 
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
14
  ## [10.0.0]
31
15
 
32
16
  ### 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,20 +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.getOnChainNotificationsConfigCached(bearerToken, accounts);
327
- const addresses = addressesWithNotifications
328
- .filter((a) => Boolean(a.enabled))
329
- .map((a) => a.address);
330
- if (addresses.length > 0) {
331
- await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(addresses);
332
- }
333
- }
334
- catch (e) {
335
- 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');
336
351
  }
352
+ const uuids = Utils.getAllUUIDs(storage);
353
+ await __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f").enablePushNotifications(uuids);
337
354
  }
338
355
  /**
339
356
  * Public method to expose disabling push notifications
@@ -345,13 +362,10 @@ class NotificationServicesController extends base_controller_1.BaseController {
345
362
  try {
346
363
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsCheckingAccountsPresence).call(this, true);
347
364
  // Retrieve user storage
348
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
349
- const addressesWithNotifications = await OnChainNotifications.getOnChainNotificationsConfigCached(bearerToken, accounts);
350
- const result = {};
351
- addressesWithNotifications.forEach((a) => {
352
- result[a.address] = a.enabled;
353
- });
354
- 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;
355
369
  }
356
370
  catch (error) {
357
371
  loglevel_1.default.error('Failed to check accounts presence', error);
@@ -395,32 +409,40 @@ class NotificationServicesController extends base_controller_1.BaseController {
395
409
  async createOnChainTriggers(opts) {
396
410
  try {
397
411
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsUpdatingMetamaskNotifications).call(this, true);
398
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
399
- const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
400
- // 1. See if has enabled notifications before
401
- const addressesWithNotifications = await OnChainNotifications.getOnChainNotificationsConfigCached(bearerToken, accounts);
402
- // Notifications API can return array with addresses set to false
403
- // So assert that at least one address is enabled
404
- let accountsWithNotifications = addressesWithNotifications
405
- .filter((a) => Boolean(a.enabled))
406
- .map((a) => a.address);
407
- // 2. Enable Notifications (if no accounts subscribed or we are resetting)
408
- if (accountsWithNotifications.length === 0 || opts?.resetNotifications) {
409
- await OnChainNotifications.updateOnChainNotifications(bearerToken, accounts.map((address) => ({ address, enabled: true })));
410
- accountsWithNotifications = accounts;
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));
411
427
  }
412
- // 3. Lazily enable push notifications (FCM may take some time, so keeps UI unblocked)
413
- __classPrivateFieldGet(this, _NotificationServicesController_pushNotifications, "f")
414
- .enablePushNotifications(accountsWithNotifications)
415
- .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(() => {
416
435
  // Do Nothing
417
436
  });
437
+ // Write the new userStorage (triggers are now "enabled")
438
+ await __classPrivateFieldGet(this, _NotificationServicesController_storage, "f").setNotificationStorage(JSON.stringify(userStorage));
418
439
  // Update the state of the controller
419
440
  this.update((state) => {
420
441
  state.isNotificationServicesEnabled = true;
421
442
  state.isFeatureAnnouncementsEnabled = true;
422
443
  state.isMetamaskNotificationsFeatureSeen = true;
423
444
  });
445
+ return userStorage;
424
446
  }
425
447
  catch (err) {
426
448
  loglevel_1.default.error('Failed to create On Chain triggers', err);
@@ -433,6 +455,10 @@ class NotificationServicesController extends base_controller_1.BaseController {
433
455
  /**
434
456
  * Enables all MetaMask notifications for the user.
435
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
436
462
  *
437
463
  * @throws {Error} If there is an error during the process of enabling notifications.
438
464
  */
@@ -480,11 +506,12 @@ class NotificationServicesController extends base_controller_1.BaseController {
480
506
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_setIsUpdatingMetamaskNotifications).call(this, false);
481
507
  }
482
508
  /**
483
- * Deletes on-chain triggers associated with a specific account/s.
509
+ * Deletes on-chain triggers associated with a specific account.
484
510
  * This method performs several key operations:
485
- * 1. Validates Auth
486
- * 2. Deletes accounts
487
- * (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.
488
515
  *
489
516
  * **Action** - When a user disables notifications for a given account in settings.
490
517
  *
@@ -492,13 +519,29 @@ class NotificationServicesController extends base_controller_1.BaseController {
492
519
  * @returns A promise that resolves to void or an object containing a success message.
493
520
  * @throws {Error} Throws an error if unauthenticated or from other operations.
494
521
  */
495
- async disableAccounts(accounts) {
522
+ async deleteOnChainTriggersByAccount(accounts) {
496
523
  try {
497
524
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
498
525
  // Get and Validate BearerToken and User Storage Key
499
- 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
+ }
500
537
  // Delete these UUIDs (Mutates User Storage)
501
- 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;
502
545
  }
503
546
  catch (err) {
504
547
  loglevel_1.default.error('Failed to delete OnChain triggers', err);
@@ -523,11 +566,45 @@ class NotificationServicesController extends base_controller_1.BaseController {
523
566
  * @returns A promise that resolves to the updated user storage.
524
567
  * @throws {Error} Throws an error if unauthenticated or from other operations.
525
568
  */
526
- async enableAccounts(accounts) {
569
+ async updateOnChainTriggersByAccount(accounts) {
527
570
  try {
528
571
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_updateUpdatingAccountsState).call(this, accounts);
529
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
530
- 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;
531
608
  }
532
609
  catch (err) {
533
610
  loglevel_1.default.error('Failed to update OnChain triggers', err);
@@ -554,31 +631,37 @@ class NotificationServicesController extends base_controller_1.BaseController {
554
631
  // Not used by Snaps
555
632
  const isGlobalNotifsEnabled = this.state.isNotificationServicesEnabled;
556
633
  // Raw Feature Notifications
557
- const rawAnnouncements = isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled
634
+ const rawFeatureAnnouncementNotifications = isGlobalNotifsEnabled && this.state.isFeatureAnnouncementsEnabled
558
635
  ? await FeatureNotifications.getFeatureAnnouncementNotifications(__classPrivateFieldGet(this, _NotificationServicesController_featureAnnouncementEnv, "f"), previewToken).catch(() => [])
559
636
  : [];
560
637
  // Raw On Chain Notifications
561
638
  const rawOnChainNotifications = [];
562
639
  if (isGlobalNotifsEnabled) {
563
- try {
564
- const { bearerToken } = await __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_getBearerToken).call(this);
565
- const { accounts } = __classPrivateFieldGet(this, _NotificationServicesController_accounts, "f").listAccounts();
566
- const addressesWithNotifications = (await OnChainNotifications.getOnChainNotificationsConfigCached(bearerToken, accounts)).map((a) => a.address);
567
- const notifications = await OnChainNotifications.getOnChainNotifications(bearerToken, addressesWithNotifications).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(() => []);
568
647
  rawOnChainNotifications.push(...notifications);
569
648
  }
570
- catch {
571
- // Do nothing
572
- }
573
649
  }
574
650
  // Snap Notifications (original)
575
651
  // We do not want to remove them
576
652
  const snapNotifications = this.state.metamaskNotificationsList.filter((notification) => notification.type === notification_schema_1.TRIGGER_TYPES.SNAP);
653
+ // Process Notifications
577
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);
578
661
  // Combine Notifications
579
662
  const metamaskNotifications = [
580
- ...(0, process_notifications_1.processAndFilterNotifications)(rawAnnouncements, readIds),
581
- ...(0, process_notifications_1.processAndFilterNotifications)(rawOnChainNotifications, readIds),
663
+ ...featureAnnouncementNotifications,
664
+ ...onChainNotifications,
582
665
  ...snapNotifications,
583
666
  ];
584
667
  // Sort Notifications
@@ -746,7 +829,7 @@ class NotificationServicesController extends base_controller_1.BaseController {
746
829
  }
747
830
  }
748
831
  }
749
- _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() {
750
833
  this.messagingSystem.registerActionHandler(`${controllerName}:updateMetamaskNotificationsList`, this.updateMetamaskNotificationsList.bind(this));
751
834
  this.messagingSystem.registerActionHandler(`${controllerName}:disableNotificationServices`, this.disableNotificationServices.bind(this));
752
835
  this.messagingSystem.registerActionHandler(`${controllerName}:getNotificationsByType`, this.getNotificationsByType.bind(this));
@@ -770,13 +853,42 @@ _NotificationServicesController_keyringController = new WeakMap(), _Notification
770
853
  if (!isSignedIn) {
771
854
  await __classPrivateFieldGet(this, _NotificationServicesController_auth, "f").signIn();
772
855
  }
773
- }, _NotificationServicesController_getBearerToken = async function _NotificationServicesController_getBearerToken() {
856
+ }, _NotificationServicesController_getValidStorageKeyAndBearerToken = async function _NotificationServicesController_getValidStorageKeyAndBearerToken() {
774
857
  __classPrivateFieldGet(this, _NotificationServicesController_instances, "m", _NotificationServicesController_assertAuthEnabled).call(this);
775
858
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesController_auth, "f").getBearerToken();
776
- if (!bearerToken) {
777
- 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;
778
891
  }
779
- return { bearerToken };
780
892
  }, _NotificationServicesController_setIsUpdatingMetamaskNotifications = function _NotificationServicesController_setIsUpdatingMetamaskNotifications(isUpdatingMetamaskNotifications) {
781
893
  this.update((state) => {
782
894
  state.isUpdatingMetamaskNotifications = isUpdatingMetamaskNotifications;