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

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
@@ -1,3 +1,143 @@
1
+ import type { TRIGGER_TYPES } from "../constants/notification-schema.mjs";
2
+ import type { UserStorage } from "../types/user-storage/user-storage.mjs";
3
+ export type NotificationTrigger = {
4
+ id: string;
5
+ chainId: string;
6
+ kind: string;
7
+ address: string;
8
+ enabled: boolean;
9
+ };
10
+ type MapTriggerFn<Result> = (trigger: NotificationTrigger) => Result | undefined;
11
+ type TraverseTriggerOpts<Result> = {
12
+ address?: string;
13
+ mapTrigger?: MapTriggerFn<Result>;
14
+ };
15
+ /**
16
+ * Create a completely new user storage object with the given accounts and state.
17
+ * This method initializes the user storage with a version key and iterates over each account to populate it with triggers.
18
+ * Each trigger is associated with supported chains, and for each chain, a unique identifier (UUID) is generated.
19
+ * The trigger object contains a kind (`k`) indicating the type of trigger and an enabled state (`e`).
20
+ * The kind and enabled state are stored with abbreviated keys to reduce the JSON size.
21
+ *
22
+ * This is used primarily for creating a new user storage (e.g. when first signing in/enabling notification profile syncing),
23
+ * caution is needed in case you need to remove triggers that you don't want (due to notification setting filters)
24
+ *
25
+ * @param accounts - An array of account objects, each optionally containing an address.
26
+ * @param state - A boolean indicating the initial enabled state for all triggers in the user storage.
27
+ * @param shouldClean - prop to clean the initialized UserStorage (removing any invalid addresses). Only false for testing purposes.
28
+ * @returns A `UserStorage` object populated with triggers for each account and chain.
29
+ */
30
+ export declare function initializeUserStorage(accounts: {
31
+ address?: string;
32
+ }[], state: boolean, shouldClean?: boolean): UserStorage;
33
+ /**
34
+ * This is a fallback to ensure that we are not adding non-hex addresses, and the shape is valid.
35
+ * Any invalid shapes will be removed.
36
+ * NOTE - this method mutates and returns the cleaned User Storage.
37
+ *
38
+ * @param userStorage - notification user storage field we are to clean.
39
+ * @returns a cleaned version of user storage.
40
+ */
41
+ export declare function cleanUserStorage(userStorage: UserStorage): UserStorage;
42
+ /**
43
+ * Iterates over user storage to find and optionally transform notification triggers.
44
+ * This method allows for flexible retrieval and transformation of triggers based on provided options.
45
+ *
46
+ * @param userStorage - The user storage object containing notification triggers.
47
+ * @param options - Optional parameters to filter and map triggers:
48
+ * - `address`: If provided, only triggers for this address are considered.
49
+ * - `mapTrigger`: A function to transform each trigger. If not provided, triggers are returned as is.
50
+ * @returns An array of triggers, potentially transformed by the `mapTrigger` function.
51
+ */
52
+ export declare function traverseUserStorageTriggers<ResultTriggers = NotificationTrigger>(userStorage: UserStorage, options?: TraverseTriggerOpts<ResultTriggers>): ResultTriggers[];
53
+ /**
54
+ * Verifies the presence of specified accounts and their chains in the user storage.
55
+ * This method checks if each provided account exists in the user storage and if all its supported chains are present.
56
+ *
57
+ * @param userStorage - The user storage object containing notification triggers.
58
+ * @param accounts - An array of account addresses to check for presence.
59
+ * @returns A record where each key is an account address and each value is a boolean indicating whether the account and all its supported chains are present in the user storage.
60
+ */
61
+ export declare function checkAccountsPresence(userStorage: UserStorage, accounts: string[]): Record<string, boolean>;
62
+ /**
63
+ * Infers and returns an array of enabled notification trigger kinds from the user storage.
64
+ * This method counts the occurrences of each kind of trigger and returns the kinds that are present.
65
+ *
66
+ * @param userStorage - The user storage object containing notification triggers.
67
+ * @returns An array of trigger kinds (`TRIGGER_TYPES`) that are enabled in the user storage.
68
+ */
69
+ export declare function inferEnabledKinds(userStorage: UserStorage): TRIGGER_TYPES[];
70
+ /**
71
+ * Retrieves all UUIDs associated with a specific account address from the user storage.
72
+ * This function utilizes `traverseUserStorageTriggers` with a mapping function to extract
73
+ * just the UUIDs of the notification triggers for the given address.
74
+ *
75
+ * @param userStorage - The user storage object containing notification triggers.
76
+ * @param address - The specific account address to retrieve UUIDs for.
77
+ * @returns An array of UUID strings associated with the given account address.
78
+ */
79
+ export declare function getUUIDsForAccount(userStorage: UserStorage, address: string): string[];
80
+ /**
81
+ * Retrieves all UUIDs from the user storage, regardless of the account address or chain ID.
82
+ * This method leverages `traverseUserStorageTriggers` with a specific mapping function (`triggerToId`)
83
+ * to extract only the UUIDs from all notification triggers present in the user storage.
84
+ *
85
+ * @param userStorage - The user storage object containing notification triggers.
86
+ * @returns An array of UUID strings from all notification triggers in the user storage.
87
+ */
88
+ export declare function getAllUUIDs(userStorage: UserStorage): string[];
89
+ /**
90
+ * Retrieves UUIDs for notification triggers that match any of the specified kinds.
91
+ * This method filters triggers based on their kind and returns an array of UUIDs for those that match the allowed kinds.
92
+ * It utilizes `traverseUserStorageTriggers` with a custom mapping function that checks if a trigger's kind is in the allowed list.
93
+ *
94
+ * @param userStorage - The user storage object containing notification triggers.
95
+ * @param allowedKinds - An array of kinds (as strings) to filter the triggers by.
96
+ * @returns An array of UUID strings for triggers that match the allowed kinds.
97
+ */
98
+ export declare function getUUIDsForKinds(userStorage: UserStorage, allowedKinds: string[]): string[];
99
+ /**
100
+ * Retrieves notification triggers for a specific account address that match any of the specified kinds.
101
+ * This method filters triggers both by the account address and their kind, returning triggers that match the allowed kinds for the specified address.
102
+ * It leverages `traverseUserStorageTriggers` with a custom mapping function to filter and return only the relevant triggers.
103
+ *
104
+ * @param userStorage - The user storage object containing notification triggers.
105
+ * @param address - The specific account address for which to retrieve triggers.
106
+ * @param allowedKinds - An array of trigger kinds (`TRIGGER_TYPES`) to filter the triggers by.
107
+ * @returns An array of `NotificationTrigger` objects that match the allowed kinds for the specified account address.
108
+ */
109
+ export declare function getUUIDsForAccountByKinds(userStorage: UserStorage, address: string, allowedKinds: TRIGGER_TYPES[]): NotificationTrigger[];
110
+ /**
111
+ * Upserts (updates or inserts) notification triggers for a given account across all supported chains.
112
+ * This method ensures that each supported trigger type exists for each chain associated with the account.
113
+ * If a trigger type does not exist for a chain, it creates a new trigger with a unique UUID.
114
+ *
115
+ * @param _account - The account address for which to upsert triggers. The address is normalized to lowercase.
116
+ * @param userStorage - The user storage object to be updated with new or existing triggers.
117
+ * @returns The updated user storage object with upserted triggers for the specified account.
118
+ */
119
+ export declare function upsertAddressTriggers(_account: string, userStorage: UserStorage): UserStorage;
120
+ /**
121
+ * Upserts (updates or inserts) notification triggers of a specific type across all accounts and chains in user storage.
122
+ * This method ensures that a trigger of the specified type exists for each account and chain. If a trigger of the specified type
123
+ * does not exist for an account and chain, it creates a new trigger with a unique UUID.
124
+ *
125
+ * @param triggerType - The type of trigger to upsert across all accounts and chains.
126
+ * @param userStorage - The user storage object to be updated with new or existing triggers of the specified type.
127
+ * @returns The updated user storage object with upserted triggers of the specified type for all accounts and chains.
128
+ */
129
+ export declare function upsertTriggerTypeTriggers(triggerType: TRIGGER_TYPES, userStorage: UserStorage): UserStorage;
130
+ /**
131
+ * Toggles the enabled status of a user storage trigger.
132
+ *
133
+ * @param userStorage - The user storage object.
134
+ * @param address - The user's address.
135
+ * @param chainId - The chain ID.
136
+ * @param uuid - The unique identifier for the trigger.
137
+ * @param enabled - The new enabled status.
138
+ * @returns The updated user storage object.
139
+ */
140
+ export declare function toggleUserStorageTriggerStatus(userStorage: UserStorage, address: string, chainId: string, uuid: string, enabled: boolean): UserStorage;
1
141
  /**
2
142
  * Performs an API call with automatic retries on failure.
3
143
  *
@@ -8,4 +148,5 @@
8
148
  * @returns A Promise that resolves to the response of the fetch request.
9
149
  */
10
150
  export declare function makeApiCall<Body>(bearerToken: string, endpoint: string, method: 'POST' | 'DELETE', body: Body): Promise<Response>;
151
+ export {};
11
152
  //# sourceMappingURL=utils.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,IAAI,EACpC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GAAG,QAAQ,EACzB,IAAI,EAAE,IAAI,GACT,OAAO,CAAC,QAAQ,CAAC,CAWnB"}
1
+ {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,6CAAyC;AAEtE,OAAO,KAAK,EAAE,WAAW,EAAE,+CAA2C;AAEtE,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,KAAK,YAAY,CAAC,MAAM,IAAI,CAC1B,OAAO,EAAE,mBAAmB,KACzB,MAAM,GAAG,SAAS,CAAC;AAExB,KAAK,mBAAmB,CAAC,MAAM,IAAI;IACjC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;CACnC,CAAC;AAuBF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,EAChC,KAAK,EAAE,OAAO,EACd,WAAW,UAAO,GACjB,WAAW,CAkCb;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,WAAW,eAaxD;AAED;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,cAAc,GAAG,mBAAmB,EAEpC,WAAW,EAAE,WAAW,EACxB,OAAO,CAAC,EAAE,mBAAmB,CAAC,cAAc,CAAC,GAC5C,cAAc,EAAE,CAkClB;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAA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C,CAWnB"}
@@ -1,3 +1,340 @@
1
+ import { isValidHexAddress } from "@metamask/controller-utils";
2
+ import { v4 as uuidv4 } from "uuid";
3
+ import { USER_STORAGE_VERSION_KEY, USER_STORAGE_VERSION } from "../constants/constants.mjs";
4
+ import { TRIGGERS } from "../constants/notification-schema.mjs";
5
+ /**
6
+ * Extracts and returns the ID from a notification trigger.
7
+ * This utility function is primarily used as a mapping function in `traverseUserStorageTriggers`
8
+ * to convert a full trigger object into its ID string.
9
+ *
10
+ * @param trigger - The notification trigger from which the ID is extracted.
11
+ * @returns The ID of the provided notification trigger.
12
+ */
13
+ const triggerToId = (trigger) => trigger.id;
14
+ /**
15
+ * A utility function that returns the input trigger without any transformation.
16
+ * This function is used as the default mapping function in `traverseUserStorageTriggers`
17
+ * when no custom mapping function is provided.
18
+ *
19
+ * @param trigger - The notification trigger to be returned as is.
20
+ * @returns The same notification trigger that was passed in.
21
+ */
22
+ const triggerIdentity = (trigger) => trigger;
23
+ /**
24
+ * Create a completely new user storage object with the given accounts and state.
25
+ * This method initializes the user storage with a version key and iterates over each account to populate it with triggers.
26
+ * Each trigger is associated with supported chains, and for each chain, a unique identifier (UUID) is generated.
27
+ * The trigger object contains a kind (`k`) indicating the type of trigger and an enabled state (`e`).
28
+ * The kind and enabled state are stored with abbreviated keys to reduce the JSON size.
29
+ *
30
+ * This is used primarily for creating a new user storage (e.g. when first signing in/enabling notification profile syncing),
31
+ * caution is needed in case you need to remove triggers that you don't want (due to notification setting filters)
32
+ *
33
+ * @param accounts - An array of account objects, each optionally containing an address.
34
+ * @param state - A boolean indicating the initial enabled state for all triggers in the user storage.
35
+ * @param shouldClean - prop to clean the initialized UserStorage (removing any invalid addresses). Only false for testing purposes.
36
+ * @returns A `UserStorage` object populated with triggers for each account and chain.
37
+ */
38
+ export function initializeUserStorage(accounts, state, shouldClean = true) {
39
+ const userStorage = {
40
+ [USER_STORAGE_VERSION_KEY]: USER_STORAGE_VERSION,
41
+ };
42
+ accounts.forEach((account) => {
43
+ const address = account.address?.toLowerCase();
44
+ if (!address) {
45
+ return;
46
+ }
47
+ if (!userStorage[address]) {
48
+ userStorage[address] = {};
49
+ }
50
+ Object.entries(TRIGGERS).forEach(([trigger, { supported_chains: supportedChains }]) => {
51
+ supportedChains.forEach((chain) => {
52
+ if (!userStorage[address]?.[chain]) {
53
+ userStorage[address][chain] = {};
54
+ }
55
+ userStorage[address][chain][uuidv4()] = {
56
+ k: trigger,
57
+ e: state, // use 'e' instead of 'enabled' to reduce the json weight
58
+ };
59
+ });
60
+ });
61
+ });
62
+ if (shouldClean) {
63
+ cleanUserStorage(userStorage);
64
+ }
65
+ return userStorage;
66
+ }
67
+ /**
68
+ * This is a fallback to ensure that we are not adding non-hex addresses, and the shape is valid.
69
+ * Any invalid shapes will be removed.
70
+ * NOTE - this method mutates and returns the cleaned User Storage.
71
+ *
72
+ * @param userStorage - notification user storage field we are to clean.
73
+ * @returns a cleaned version of user storage.
74
+ */
75
+ export function cleanUserStorage(userStorage) {
76
+ const addresses = new Set();
77
+ traverseUserStorageTriggers(userStorage, {
78
+ mapTrigger: (t) => addresses.add(t.address),
79
+ });
80
+ addresses.forEach((addr) => {
81
+ if (!isValidHexAddress(addr)) {
82
+ delete userStorage[addr];
83
+ }
84
+ });
85
+ return userStorage;
86
+ }
87
+ /**
88
+ * Iterates over user storage to find and optionally transform notification triggers.
89
+ * This method allows for flexible retrieval and transformation of triggers based on provided options.
90
+ *
91
+ * @param userStorage - The user storage object containing notification triggers.
92
+ * @param options - Optional parameters to filter and map triggers:
93
+ * - `address`: If provided, only triggers for this address are considered.
94
+ * - `mapTrigger`: A function to transform each trigger. If not provided, triggers are returned as is.
95
+ * @returns An array of triggers, potentially transformed by the `mapTrigger` function.
96
+ */
97
+ export function traverseUserStorageTriggers(userStorage, options) {
98
+ const triggers = [];
99
+ const mapTrigger = options?.mapTrigger ?? triggerIdentity;
100
+ for (const address in userStorage) {
101
+ if (address === USER_STORAGE_VERSION_KEY) {
102
+ continue;
103
+ }
104
+ if (options?.address && address !== options.address) {
105
+ continue;
106
+ }
107
+ for (const chainId in userStorage[address]) {
108
+ if (chainId in userStorage[address]) {
109
+ for (const uuid in userStorage[address][chainId]) {
110
+ if (uuid) {
111
+ const mappedTrigger = mapTrigger({
112
+ id: uuid,
113
+ kind: userStorage[address]?.[chainId]?.[uuid]?.k,
114
+ chainId,
115
+ address,
116
+ enabled: userStorage[address]?.[chainId]?.[uuid]?.e ?? false,
117
+ });
118
+ if (mappedTrigger) {
119
+ triggers.push(mappedTrigger);
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+ }
126
+ return triggers;
127
+ }
128
+ /**
129
+ * Verifies the presence of specified accounts and their chains in the user storage.
130
+ * This method checks if each provided account exists in the user storage and if all its supported chains are present.
131
+ *
132
+ * @param userStorage - The user storage object containing notification triggers.
133
+ * @param accounts - An array of account addresses to check for presence.
134
+ * @returns A record where each key is an account address and each value is a boolean indicating whether the account and all its supported chains are present in the user storage.
135
+ */
136
+ export function checkAccountsPresence(userStorage, accounts) {
137
+ const presenceRecord = {};
138
+ // Initialize presence record for all accounts as false
139
+ accounts.forEach((account) => {
140
+ presenceRecord[account.toLowerCase()] = isAccountEnabled(account, userStorage);
141
+ });
142
+ return presenceRecord;
143
+ }
144
+ /**
145
+ * Internal method to check if a given account should be marked as enabled by introspecting user storage
146
+ * Introspection: check if account exists; and also see if has all triggers in schema enabled
147
+ *
148
+ * @param accountAddress - address to check in user storage
149
+ * @param userStorage - user storage object to traverse/introspect
150
+ * @returns boolean if the account is enabled or disabled
151
+ */
152
+ function isAccountEnabled(accountAddress, userStorage) {
153
+ const accountObject = userStorage[accountAddress?.toLowerCase()];
154
+ // If the account address is not present in the userStorage, return true
155
+ if (!accountObject) {
156
+ return false;
157
+ }
158
+ // Check if all available chains are present
159
+ for (const [triggerKind, triggerConfig] of Object.entries(TRIGGERS)) {
160
+ for (const chain of triggerConfig.supported_chains) {
161
+ if (!accountObject[chain]) {
162
+ return false;
163
+ }
164
+ const triggerExists = Object.values(accountObject[chain]).some((obj) => obj.k === triggerKind);
165
+ if (!triggerExists) {
166
+ return false;
167
+ }
168
+ // Check if any trigger is disabled
169
+ for (const uuid in accountObject[chain]) {
170
+ if (!accountObject[chain][uuid].e) {
171
+ return false;
172
+ }
173
+ }
174
+ }
175
+ }
176
+ return true;
177
+ }
178
+ /**
179
+ * Infers and returns an array of enabled notification trigger kinds from the user storage.
180
+ * This method counts the occurrences of each kind of trigger and returns the kinds that are present.
181
+ *
182
+ * @param userStorage - The user storage object containing notification triggers.
183
+ * @returns An array of trigger kinds (`TRIGGER_TYPES`) that are enabled in the user storage.
184
+ */
185
+ export function inferEnabledKinds(userStorage) {
186
+ const allSupportedKinds = new Set();
187
+ traverseUserStorageTriggers(userStorage, {
188
+ mapTrigger: (t) => {
189
+ allSupportedKinds.add(t.kind);
190
+ },
191
+ });
192
+ return Array.from(allSupportedKinds);
193
+ }
194
+ /**
195
+ * Retrieves all UUIDs associated with a specific account address from the user storage.
196
+ * This function utilizes `traverseUserStorageTriggers` with a mapping function to extract
197
+ * just the UUIDs of the notification triggers for the given address.
198
+ *
199
+ * @param userStorage - The user storage object containing notification triggers.
200
+ * @param address - The specific account address to retrieve UUIDs for.
201
+ * @returns An array of UUID strings associated with the given account address.
202
+ */
203
+ export function getUUIDsForAccount(userStorage, address) {
204
+ return traverseUserStorageTriggers(userStorage, {
205
+ address,
206
+ mapTrigger: triggerToId,
207
+ });
208
+ }
209
+ /**
210
+ * Retrieves all UUIDs from the user storage, regardless of the account address or chain ID.
211
+ * This method leverages `traverseUserStorageTriggers` with a specific mapping function (`triggerToId`)
212
+ * to extract only the UUIDs from all notification triggers present in the user storage.
213
+ *
214
+ * @param userStorage - The user storage object containing notification triggers.
215
+ * @returns An array of UUID strings from all notification triggers in the user storage.
216
+ */
217
+ export function getAllUUIDs(userStorage) {
218
+ return traverseUserStorageTriggers(userStorage, {
219
+ mapTrigger: triggerToId,
220
+ });
221
+ }
222
+ /**
223
+ * Retrieves UUIDs for notification triggers that match any of the specified kinds.
224
+ * This method filters triggers based on their kind and returns an array of UUIDs for those that match the allowed kinds.
225
+ * It utilizes `traverseUserStorageTriggers` with a custom mapping function that checks if a trigger's kind is in the allowed list.
226
+ *
227
+ * @param userStorage - The user storage object containing notification triggers.
228
+ * @param allowedKinds - An array of kinds (as strings) to filter the triggers by.
229
+ * @returns An array of UUID strings for triggers that match the allowed kinds.
230
+ */
231
+ export function getUUIDsForKinds(userStorage, allowedKinds) {
232
+ const kindsSet = new Set(allowedKinds);
233
+ return traverseUserStorageTriggers(userStorage, {
234
+ mapTrigger: (t) => (kindsSet.has(t.kind) ? t.id : undefined),
235
+ });
236
+ }
237
+ /**
238
+ * Retrieves notification triggers for a specific account address that match any of the specified kinds.
239
+ * This method filters triggers both by the account address and their kind, returning triggers that match the allowed kinds for the specified address.
240
+ * It leverages `traverseUserStorageTriggers` with a custom mapping function to filter and return only the relevant triggers.
241
+ *
242
+ * @param userStorage - The user storage object containing notification triggers.
243
+ * @param address - The specific account address for which to retrieve triggers.
244
+ * @param allowedKinds - An array of trigger kinds (`TRIGGER_TYPES`) to filter the triggers by.
245
+ * @returns An array of `NotificationTrigger` objects that match the allowed kinds for the specified account address.
246
+ */
247
+ export function getUUIDsForAccountByKinds(userStorage, address, allowedKinds) {
248
+ const allowedKindsSet = new Set(allowedKinds);
249
+ return traverseUserStorageTriggers(userStorage, {
250
+ address,
251
+ mapTrigger: (trigger) => {
252
+ if (allowedKindsSet.has(trigger.kind)) {
253
+ return trigger;
254
+ }
255
+ return undefined;
256
+ },
257
+ });
258
+ }
259
+ /**
260
+ * Upserts (updates or inserts) notification triggers for a given account across all supported chains.
261
+ * This method ensures that each supported trigger type exists for each chain associated with the account.
262
+ * If a trigger type does not exist for a chain, it creates a new trigger with a unique UUID.
263
+ *
264
+ * @param _account - The account address for which to upsert triggers. The address is normalized to lowercase.
265
+ * @param userStorage - The user storage object to be updated with new or existing triggers.
266
+ * @returns The updated user storage object with upserted triggers for the specified account.
267
+ */
268
+ export function upsertAddressTriggers(_account, userStorage) {
269
+ // Ensure the account exists in userStorage
270
+ const account = _account.toLowerCase();
271
+ userStorage[account] = userStorage[account] || {};
272
+ // Iterate over each trigger and its supported chains
273
+ for (const [trigger, { supported_chains: supportedChains }] of Object.entries(TRIGGERS)) {
274
+ for (const chain of supportedChains) {
275
+ // Ensure the chain exists for the account
276
+ userStorage[account][chain] = userStorage[account][chain] || {};
277
+ // Check if the trigger exists for the chain
278
+ const existingTrigger = Object.values(userStorage[account][chain]).find((obj) => obj.k === trigger);
279
+ if (!existingTrigger) {
280
+ // If the trigger doesn't exist, create a new one with a new UUID
281
+ const uuid = uuidv4();
282
+ userStorage[account][chain][uuid] = {
283
+ k: trigger,
284
+ e: false,
285
+ };
286
+ }
287
+ }
288
+ }
289
+ return userStorage;
290
+ }
291
+ /**
292
+ * Upserts (updates or inserts) notification triggers of a specific type across all accounts and chains in user storage.
293
+ * This method ensures that a trigger of the specified type exists for each account and chain. If a trigger of the specified type
294
+ * does not exist for an account and chain, it creates a new trigger with a unique UUID.
295
+ *
296
+ * @param triggerType - The type of trigger to upsert across all accounts and chains.
297
+ * @param userStorage - The user storage object to be updated with new or existing triggers of the specified type.
298
+ * @returns The updated user storage object with upserted triggers of the specified type for all accounts and chains.
299
+ */
300
+ export function upsertTriggerTypeTriggers(triggerType, userStorage) {
301
+ // Iterate over each account in userStorage
302
+ Object.entries(userStorage).forEach(([account, chains]) => {
303
+ if (account === USER_STORAGE_VERSION_KEY) {
304
+ return;
305
+ }
306
+ // Iterate over each chain for the account
307
+ Object.entries(chains).forEach(([chain, triggers]) => {
308
+ // Check if the trigger type exists for the chain
309
+ const existingTrigger = Object.values(triggers).find((obj) => obj.k === triggerType);
310
+ if (!existingTrigger) {
311
+ // If the trigger type doesn't exist, create a new one with a new UUID
312
+ const uuid = uuidv4();
313
+ userStorage[account][chain][uuid] = {
314
+ k: triggerType,
315
+ e: false,
316
+ };
317
+ }
318
+ });
319
+ });
320
+ return userStorage;
321
+ }
322
+ /**
323
+ * Toggles the enabled status of a user storage trigger.
324
+ *
325
+ * @param userStorage - The user storage object.
326
+ * @param address - The user's address.
327
+ * @param chainId - The chain ID.
328
+ * @param uuid - The unique identifier for the trigger.
329
+ * @param enabled - The new enabled status.
330
+ * @returns The updated user storage object.
331
+ */
332
+ export function toggleUserStorageTriggerStatus(userStorage, address, chainId, uuid, enabled) {
333
+ if (userStorage?.[address]?.[chainId]?.[uuid]) {
334
+ userStorage[address][chainId][uuid].e = enabled;
335
+ }
336
+ return userStorage;
337
+ }
1
338
  /**
2
339
  * Performs an API call with automatic retries on failure.
3
340
  *
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,WAAmB,EACnB,QAAgB,EAChB,MAAyB,EACzB,IAAU;IAEV,MAAM,OAAO,GAAgB;QAC3B,MAAM;QACN,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;KAC3B,CAAC;IAEF,OAAO,MAAM,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC","sourcesContent":["/**\n * Performs an API call with automatic retries on failure.\n *\n * @param bearerToken - The JSON Web Token for authorization.\n * @param endpoint - The URL of the API endpoint to call.\n * @param method - The HTTP method ('POST' or 'DELETE').\n * @param body - The body of the request. It should be an object that can be serialized to JSON.\n * @returns A Promise that resolves to the response of the fetch request.\n */\nexport async function makeApiCall<Body>(\n bearerToken: string,\n endpoint: string,\n method: 'POST' | 'DELETE',\n body: Body,\n): Promise<Response> {\n const options: RequestInit = {\n method,\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify(body),\n };\n\n return await fetch(endpoint, options);\n}\n"]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,mCAAmC;AAC/D,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAEpC,OAAO,EACL,wBAAwB,EACxB,oBAAoB,EACrB,mCAA+B;AAEhC,OAAO,EAAE,QAAQ,EAAE,6CAAyC;AAoB5D;;;;;;;GAOG;AACH,MAAM,WAAW,GAAG,CAAC,OAA4B,EAAU,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;AAEzE;;;;;;;GAOG;AACH,MAAM,eAAe,GAAG,CAAC,OAA4B,EAAuB,EAAE,CAC5E,OAAO,CAAC;AAEV;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,qBAAqB,CACnC,QAAgC,EAChC,KAAc,EACd,WAAW,GAAG,IAAI;IAElB,MAAM,WAAW,GAAgB;QAC/B,CAAC,wBAAwB,CAAC,EAAE,oBAAoB;KACjD,CAAC;IAEF,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,WAAW,EAAE,CAAC;QAC/C,IAAI,CAAC,OAAO,EAAE;YACZ,OAAO;SACR;QACD,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE;YACzB,WAAW,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC;SAC3B;QAED,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAC9B,CAAC,CAAC,OAAO,EAAE,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC,EAAE,EAAE;YACnD,eAAe,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;gBAChC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE;oBAClC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AAC;YAEF,IAAI,CAAC,eAAe,EAAE;gBACpB,sEAAsE;gBACtE,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC;gBACtB,WAAW,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG;oBAClC,CAAC,EAAE,WAAW;oBACd,CAAC,EAAE,KAAK;iBACT,CAAC;aACH;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,8BAA8B,CAC5C,WAAwB,EACxB,OAAe,EACf,OAAe,EACf,IAAY,EACZ,OAAgB;IAEhB,IAAI,WAAW,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE;QAC7C,WAAW,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;KACjD;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,WAAmB,EACnB,QAAgB,EAChB,MAAyB,EACzB,IAAU;IAEV,MAAM,OAAO,GAAgB;QAC3B,MAAM;QACN,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,aAAa,EAAE,UAAU,WAAW,EAAE;SACvC;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;KAC3B,CAAC;IAEF,OAAO,MAAM,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC","sourcesContent":["import { isValidHexAddress } from '@metamask/controller-utils';\nimport { v4 as uuidv4 } from 'uuid';\n\nimport {\n USER_STORAGE_VERSION_KEY,\n USER_STORAGE_VERSION,\n} from '../constants/constants';\nimport type { TRIGGER_TYPES } from '../constants/notification-schema';\nimport { TRIGGERS } from '../constants/notification-schema';\nimport type { UserStorage } from '../types/user-storage/user-storage';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n enabled: boolean;\n};\n\ntype MapTriggerFn<Result> = (\n trigger: NotificationTrigger,\n) => Result | undefined;\n\ntype TraverseTriggerOpts<Result> = {\n address?: string;\n mapTrigger?: MapTriggerFn<Result>;\n};\n\n/**\n * Extracts and returns the ID from a notification trigger.\n * This utility function is primarily used as a mapping function in `traverseUserStorageTriggers`\n * to convert a full trigger object into its ID string.\n *\n * @param trigger - The notification trigger from which the ID is extracted.\n * @returns The ID of the provided notification trigger.\n */\nconst triggerToId = (trigger: NotificationTrigger): string => trigger.id;\n\n/**\n * A utility function that returns the input trigger without any transformation.\n * This function is used as the default mapping function in `traverseUserStorageTriggers`\n * when no custom mapping function is provided.\n *\n * @param trigger - The notification trigger to be returned as is.\n * @returns The same notification trigger that was passed in.\n */\nconst triggerIdentity = (trigger: NotificationTrigger): NotificationTrigger =>\n trigger;\n\n/**\n * Create a completely new user storage object with the given accounts and state.\n * This method initializes the user storage with a version key and iterates over each account to populate it with triggers.\n * Each trigger is associated with supported chains, and for each chain, a unique identifier (UUID) is generated.\n * The trigger object contains a kind (`k`) indicating the type of trigger and an enabled state (`e`).\n * The kind and enabled state are stored with abbreviated keys to reduce the JSON size.\n *\n * This is used primarily for creating a new user storage (e.g. when first signing in/enabling notification profile syncing),\n * caution is needed in case you need to remove triggers that you don't want (due to notification setting filters)\n *\n * @param accounts - An array of account objects, each optionally containing an address.\n * @param state - A boolean indicating the initial enabled state for all triggers in the user storage.\n * @param shouldClean - prop to clean the initialized UserStorage (removing any invalid addresses). Only false for testing purposes.\n * @returns A `UserStorage` object populated with triggers for each account and chain.\n */\nexport function initializeUserStorage(\n accounts: { address?: string }[],\n state: boolean,\n shouldClean = true,\n): UserStorage {\n const userStorage: UserStorage = {\n [USER_STORAGE_VERSION_KEY]: USER_STORAGE_VERSION,\n };\n\n accounts.forEach((account) => {\n const address = account.address?.toLowerCase();\n if (!address) {\n return;\n }\n if (!userStorage[address]) {\n userStorage[address] = {};\n }\n\n Object.entries(TRIGGERS).forEach(\n ([trigger, { supported_chains: supportedChains }]) => {\n supportedChains.forEach((chain) => {\n if (!userStorage[address]?.[chain]) {\n userStorage[address][chain] = {};\n }\n\n userStorage[address][chain][uuidv4()] = {\n k: trigger as TRIGGER_TYPES, // use 'k' instead of 'kind' to reduce the json weight\n e: state, // use 'e' instead of 'enabled' to reduce the json weight\n };\n });\n },\n );\n });\n\n if (shouldClean) {\n cleanUserStorage(userStorage);\n }\n return userStorage;\n}\n\n/**\n * This is a fallback to ensure that we are not adding non-hex addresses, and the shape is valid.\n * Any invalid shapes will be removed.\n * NOTE - this method mutates and returns the cleaned User Storage.\n *\n * @param userStorage - notification user storage field we are to clean.\n * @returns a cleaned version of user storage.\n */\nexport function cleanUserStorage(userStorage: UserStorage) {\n const addresses = new Set<string>();\n traverseUserStorageTriggers(userStorage, {\n mapTrigger: (t) => addresses.add(t.address),\n });\n\n addresses.forEach((addr) => {\n if (!isValidHexAddress(addr)) {\n delete userStorage[addr];\n }\n });\n\n return userStorage;\n}\n\n/**\n * Iterates over user storage to find and optionally transform notification triggers.\n * This method allows for flexible retrieval and transformation of triggers based on provided options.\n *\n * @param userStorage - The user storage object containing notification triggers.\n * @param options - Optional parameters to filter and map triggers:\n * - `address`: If provided, only triggers for this address are considered.\n * - `mapTrigger`: A function to transform each trigger. If not provided, triggers are returned as is.\n * @returns An array of triggers, potentially transformed by the `mapTrigger` function.\n */\nexport function traverseUserStorageTriggers<\n ResultTriggers = NotificationTrigger,\n>(\n userStorage: UserStorage,\n options?: TraverseTriggerOpts<ResultTriggers>,\n): ResultTriggers[] {\n const triggers: ResultTriggers[] = [];\n const mapTrigger =\n options?.mapTrigger ?? (triggerIdentity as MapTriggerFn<ResultTriggers>);\n\n for (const address in userStorage) {\n if (address === (USER_STORAGE_VERSION_KEY as unknown as string)) {\n continue;\n }\n if (options?.address && address !== options.address) {\n continue;\n }\n\n for (const chainId in userStorage[address]) {\n if (chainId in userStorage[address]) {\n for (const uuid in userStorage[address][chainId]) {\n if (uuid) {\n const mappedTrigger = mapTrigger({\n id: uuid,\n kind: userStorage[address]?.[chainId]?.[uuid]?.k,\n chainId,\n address,\n enabled: userStorage[address]?.[chainId]?.[uuid]?.e ?? false,\n });\n if (mappedTrigger) {\n triggers.push(mappedTrigger);\n }\n }\n }\n }\n }\n }\n\n return triggers;\n}\n\n/**\n * Verifies the presence of specified accounts and their chains in the user storage.\n * This method checks if each provided account exists in the user storage and if all its supported chains are present.\n *\n * @param userStorage - The user storage object containing notification triggers.\n * @param accounts - An array of account addresses to check for presence.\n * @returns A record where each key is an account address and each value is a boolean indicating whether the account and all its supported chains are present in the user storage.\n */\nexport function checkAccountsPresence(\n userStorage: UserStorage,\n accounts: string[],\n): Record<string, boolean> {\n const presenceRecord: Record<string, boolean> = {};\n\n // Initialize presence record for all accounts as false\n accounts.forEach((account) => {\n presenceRecord[account.toLowerCase()] = isAccountEnabled(\n account,\n userStorage,\n );\n });\n\n return presenceRecord;\n}\n\n/**\n * Internal method to check if a given account should be marked as enabled by introspecting user storage\n * Introspection: check if account exists; and also see if has all triggers in schema enabled\n *\n * @param accountAddress - address to check in user storage\n * @param userStorage - user storage object to traverse/introspect\n * @returns boolean if the account is enabled or disabled\n */\nfunction isAccountEnabled(\n accountAddress: string,\n userStorage: UserStorage,\n): boolean {\n const accountObject = userStorage[accountAddress?.toLowerCase()];\n\n // If the account address is not present in the userStorage, return true\n if (!accountObject) {\n return false;\n }\n\n // Check if all available chains are present\n for (const [triggerKind, triggerConfig] of Object.entries(TRIGGERS)) {\n for (const chain of triggerConfig.supported_chains) {\n if (!accountObject[chain]) {\n return false;\n }\n\n const triggerExists = Object.values(accountObject[chain]).some(\n (obj) => obj.k === (triggerKind as TRIGGER_TYPES),\n );\n if (!triggerExists) {\n return false;\n }\n\n // Check if any trigger is disabled\n for (const uuid in accountObject[chain]) {\n if (!accountObject[chain][uuid].e) {\n return false;\n }\n }\n }\n }\n\n return true;\n}\n\n/**\n * Infers and returns an array of enabled notification trigger kinds from the user storage.\n * This method counts the occurrences of each kind of trigger and returns the kinds that are present.\n *\n * @param userStorage - The user storage object containing notification triggers.\n * @returns An array of trigger kinds (`TRIGGER_TYPES`) that are enabled in the user storage.\n */\nexport function inferEnabledKinds(userStorage: UserStorage): TRIGGER_TYPES[] {\n const allSupportedKinds = new Set<TRIGGER_TYPES>();\n\n traverseUserStorageTriggers(userStorage, {\n mapTrigger: (t) => {\n allSupportedKinds.add(t.kind as TRIGGER_TYPES);\n },\n });\n\n return Array.from(allSupportedKinds);\n}\n\n/**\n * Retrieves all UUIDs associated with a specific account address from the user storage.\n * This function utilizes `traverseUserStorageTriggers` with a mapping function to extract\n * just the UUIDs of the notification triggers for the given address.\n *\n * @param userStorage - The user storage object containing notification triggers.\n * @param address - The specific account address to retrieve UUIDs for.\n * @returns An array of UUID strings associated with the given account address.\n */\nexport function getUUIDsForAccount(\n userStorage: UserStorage,\n address: string,\n): string[] {\n return traverseUserStorageTriggers(userStorage, {\n address,\n mapTrigger: triggerToId,\n });\n}\n\n/**\n * Retrieves all UUIDs from the user storage, regardless of the account address or chain ID.\n * This method leverages `traverseUserStorageTriggers` with a specific mapping function (`triggerToId`)\n * to extract only the UUIDs from all notification triggers present in the user storage.\n *\n * @param userStorage - The user storage object containing notification triggers.\n * @returns An array of UUID strings from all notification triggers in the user storage.\n */\nexport function getAllUUIDs(userStorage: UserStorage): string[] {\n return traverseUserStorageTriggers(userStorage, {\n mapTrigger: triggerToId,\n });\n}\n\n/**\n * Retrieves UUIDs for notification triggers that match any of the specified kinds.\n * This method filters triggers based on their kind and returns an array of UUIDs for those that match the allowed kinds.\n * It utilizes `traverseUserStorageTriggers` with a custom mapping function that checks if a trigger's kind is in the allowed list.\n *\n * @param userStorage - The user storage object containing notification triggers.\n * @param allowedKinds - An array of kinds (as strings) to filter the triggers by.\n * @returns An array of UUID strings for triggers that match the allowed kinds.\n */\nexport function getUUIDsForKinds(\n userStorage: UserStorage,\n allowedKinds: string[],\n): string[] {\n const kindsSet = new Set(allowedKinds);\n\n return traverseUserStorageTriggers(userStorage, {\n mapTrigger: (t) => (kindsSet.has(t.kind) ? t.id : undefined),\n });\n}\n\n/**\n * Retrieves notification triggers for a specific account address that match any of the specified kinds.\n * This method filters triggers both by the account address and their kind, returning triggers that match the allowed kinds for the specified address.\n * It leverages `traverseUserStorageTriggers` with a custom mapping function to filter and return only the relevant triggers.\n *\n * @param userStorage - The user storage object containing notification triggers.\n * @param address - The specific account address for which to retrieve triggers.\n * @param allowedKinds - An array of trigger kinds (`TRIGGER_TYPES`) to filter the triggers by.\n * @returns An array of `NotificationTrigger` objects that match the allowed kinds for the specified account address.\n */\nexport function getUUIDsForAccountByKinds(\n userStorage: UserStorage,\n address: string,\n allowedKinds: TRIGGER_TYPES[],\n): NotificationTrigger[] {\n const allowedKindsSet = new Set(allowedKinds);\n return traverseUserStorageTriggers(userStorage, {\n address,\n mapTrigger: (trigger) => {\n if (allowedKindsSet.has(trigger.kind as TRIGGER_TYPES)) {\n return trigger;\n }\n return undefined;\n },\n });\n}\n\n/**\n * Upserts (updates or inserts) notification triggers for a given account across all supported chains.\n * This method ensures that each supported trigger type exists for each chain associated with the account.\n * If a trigger type does not exist for a chain, it creates a new trigger with a unique UUID.\n *\n * @param _account - The account address for which to upsert triggers. The address is normalized to lowercase.\n * @param userStorage - The user storage object to be updated with new or existing triggers.\n * @returns The updated user storage object with upserted triggers for the specified account.\n */\nexport function upsertAddressTriggers(\n _account: string,\n userStorage: UserStorage,\n): UserStorage {\n // Ensure the account exists in userStorage\n const account = _account.toLowerCase();\n userStorage[account] = userStorage[account] || {};\n\n // Iterate over each trigger and its supported chains\n for (const [trigger, { supported_chains: supportedChains }] of Object.entries(\n TRIGGERS,\n )) {\n for (const chain of supportedChains) {\n // Ensure the chain exists for the account\n userStorage[account][chain] = userStorage[account][chain] || {};\n\n // Check if the trigger exists for the chain\n const existingTrigger = Object.values(userStorage[account][chain]).find(\n (obj) => obj.k === (trigger as TRIGGER_TYPES),\n );\n\n if (!existingTrigger) {\n // If the trigger doesn't exist, create a new one with a new UUID\n const uuid = uuidv4();\n userStorage[account][chain][uuid] = {\n k: trigger as TRIGGER_TYPES,\n e: false,\n };\n }\n }\n }\n\n return userStorage;\n}\n\n/**\n * Upserts (updates or inserts) notification triggers of a specific type across all accounts and chains in user storage.\n * This method ensures that a trigger of the specified type exists for each account and chain. If a trigger of the specified type\n * does not exist for an account and chain, it creates a new trigger with a unique UUID.\n *\n * @param triggerType - The type of trigger to upsert across all accounts and chains.\n * @param userStorage - The user storage object to be updated with new or existing triggers of the specified type.\n * @returns The updated user storage object with upserted triggers of the specified type for all accounts and chains.\n */\nexport function upsertTriggerTypeTriggers(\n triggerType: TRIGGER_TYPES,\n userStorage: UserStorage,\n): UserStorage {\n // Iterate over each account in userStorage\n Object.entries(userStorage).forEach(([account, chains]) => {\n if (account === (USER_STORAGE_VERSION_KEY as unknown as string)) {\n return;\n }\n\n // Iterate over each chain for the account\n Object.entries(chains).forEach(([chain, triggers]) => {\n // Check if the trigger type exists for the chain\n const existingTrigger = Object.values(triggers).find(\n (obj) => obj.k === triggerType,\n );\n\n if (!existingTrigger) {\n // If the trigger type doesn't exist, create a new one with a new UUID\n const uuid = uuidv4();\n userStorage[account][chain][uuid] = {\n k: triggerType,\n e: false,\n };\n }\n });\n });\n\n return userStorage;\n}\n\n/**\n * Toggles the enabled status of a user storage trigger.\n *\n * @param userStorage - The user storage object.\n * @param address - The user's address.\n * @param chainId - The chain ID.\n * @param uuid - The unique identifier for the trigger.\n * @param enabled - The new enabled status.\n * @returns The updated user storage object.\n */\nexport function toggleUserStorageTriggerStatus(\n userStorage: UserStorage,\n address: string,\n chainId: string,\n uuid: string,\n enabled: boolean,\n): UserStorage {\n if (userStorage?.[address]?.[chainId]?.[uuid]) {\n userStorage[address][chainId][uuid].e = enabled;\n }\n\n return userStorage;\n}\n\n/**\n * Performs an API call with automatic retries on failure.\n *\n * @param bearerToken - The JSON Web Token for authorization.\n * @param endpoint - The URL of the API endpoint to call.\n * @param method - The HTTP method ('POST' or 'DELETE').\n * @param body - The body of the request. It should be an object that can be serialized to JSON.\n * @returns A Promise that resolves to the response of the fetch request.\n */\nexport async function makeApiCall<Body>(\n bearerToken: string,\n endpoint: string,\n method: 'POST' | 'DELETE',\n body: Body,\n): Promise<Response> {\n const options: RequestInit = {\n method,\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${bearerToken}`,\n },\n body: JSON.stringify(body),\n };\n\n return await fetch(endpoint, options);\n}\n"]}
@@ -96,9 +96,9 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
96
96
  * 2. Fetching the Firebase Cloud Messaging (FCM) token from Firebase.
97
97
  * 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
98
98
  *
99
- * @param addresses - An array of addresses to enable push notifications for.
99
+ * @param UUIDs - An array of UUIDs to enable push notifications for.
100
100
  */
101
- async enablePushNotifications(addresses) {
101
+ async enablePushNotifications(UUIDs) {
102
102
  if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
103
103
  return;
104
104
  }
@@ -113,15 +113,12 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
113
113
  // Activate Push Notifications
114
114
  const fcmToken = await (0, services_1.activatePushNotifications)({
115
115
  bearerToken,
116
- addresses,
116
+ triggers: UUIDs,
117
117
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
118
118
  createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
119
- regToken: {
120
- platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
121
- locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
122
- oldToken: this.state.fcmToken,
123
- },
124
- });
119
+ platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
120
+ locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
121
+ }).catch(() => null);
125
122
  if (fcmToken) {
126
123
  __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'enable', fcmToken });
127
124
  }
@@ -177,13 +174,12 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
177
174
  }
178
175
  /**
179
176
  * Updates the triggers for push notifications.
180
- * This method is responsible for updating the server with the new set of addresses that should trigger push notifications.
177
+ * This method is responsible for updating the server with the new set of UUIDs that should trigger push notifications.
181
178
  * It uses the current FCM token and a BearerToken for authentication.
182
179
  *
183
- * @param addresses - An array of addresses that should trigger push notifications.
184
- * @deprecated - this is not used anymore and will most likely be removed
180
+ * @param UUIDs - An array of UUIDs that should trigger push notifications.
185
181
  */
186
- async updateTriggerPushNotifications(addresses) {
182
+ async updateTriggerPushNotifications(UUIDs) {
187
183
  if (!__classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").isPushFeatureEnabled) {
188
184
  return;
189
185
  }
@@ -192,16 +188,14 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
192
188
  });
193
189
  try {
194
190
  const bearerToken = await __classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_getAndAssertBearerToken).call(this);
195
- const fcmToken = await (0, services_1.activatePushNotifications)({
191
+ const { fcmToken } = await (0, services_1.updateTriggerPushNotifications)({
196
192
  bearerToken,
197
- addresses,
193
+ triggers: UUIDs,
198
194
  env: __classPrivateFieldGet(this, _NotificationServicesPushController_env, "f"),
199
195
  createRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.createRegToken,
200
- regToken: {
201
- platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
202
- locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
203
- oldToken: this.state.fcmToken,
204
- },
196
+ deleteRegToken: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").pushService.deleteRegToken,
197
+ platform: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").platform,
198
+ locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
205
199
  });
206
200
  // update the state with the new FCM token
207
201
  if (fcmToken) {
@@ -246,9 +240,8 @@ _NotificationServicesPushController_pushListenerUnsubscribe = new WeakMap(), _No
246
240
  }
247
241
  if (command.type === 'disable') {
248
242
  this.update((state) => {
249
- // Note we do not want to clear the old FCM token
250
- // We can send it as an old token to our backend to cleanup next time turned on
251
243
  state.isPushEnabled = false;
244
+ state.fcmToken = '';
252
245
  state.isUpdatingFCMToken = false;
253
246
  });
254
247
  }