@metamask-previews/notification-services-controller 0.21.0-preview-ea165590 → 0.21.0-preview-7a21cbc
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.
- package/dist/NotificationServicesController/NotificationServicesController.cjs +4 -1
- package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.mjs +5 -2
- package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
- package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.cjs +1 -1
- package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.cjs.map +1 -1
- package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.d.cts.map +1 -1
- package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.d.mts.map +1 -1
- package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.mjs +1 -1
- package/dist/NotificationServicesController/__fixtures__/mock-notification-user-storage.mjs.map +1 -1
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.cjs +6 -0
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.cjs.map +1 -0
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.cts +3 -0
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.cts.map +1 -0
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.mts +3 -0
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.d.mts.map +1 -0
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.mjs +3 -0
- package/dist/NotificationServicesController/__fixtures__/mockAddresses.mjs.map +1 -0
- package/dist/NotificationServicesController/services/onchain-notifications.cjs +1 -0
- package/dist/NotificationServicesController/services/onchain-notifications.cjs.map +1 -1
- package/dist/NotificationServicesController/services/onchain-notifications.d.cts.map +1 -1
- package/dist/NotificationServicesController/services/onchain-notifications.d.mts.map +1 -1
- package/dist/NotificationServicesController/services/onchain-notifications.mjs +2 -1
- package/dist/NotificationServicesController/services/onchain-notifications.mjs.map +1 -1
- package/dist/NotificationServicesController/utils/utils.cjs +28 -2
- package/dist/NotificationServicesController/utils/utils.cjs.map +1 -1
- package/dist/NotificationServicesController/utils/utils.d.cts +11 -1
- package/dist/NotificationServicesController/utils/utils.d.cts.map +1 -1
- package/dist/NotificationServicesController/utils/utils.d.mts +11 -1
- package/dist/NotificationServicesController/utils/utils.d.mts.map +1 -1
- package/dist/NotificationServicesController/utils/utils.mjs +26 -1
- package/dist/NotificationServicesController/utils/utils.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -8,7 +8,7 @@ import { UserStorageController } from "@metamask/profile-sync-controller";
|
|
|
8
8
|
import $log from "loglevel";
|
|
9
9
|
const log = $importDefault($log);
|
|
10
10
|
import { toRawOnChainNotification } from "../../shared/to-raw-notification.mjs";
|
|
11
|
-
import { makeApiCall, toggleUserStorageTriggerStatus, traverseUserStorageTriggers } from "../utils/utils.mjs";
|
|
11
|
+
import { cleanUserStorage, makeApiCall, toggleUserStorageTriggerStatus, traverseUserStorageTriggers } from "../utils/utils.mjs";
|
|
12
12
|
export const TRIGGER_API = 'https://trigger.api.cx.metamask.io';
|
|
13
13
|
export const NOTIFICATION_API = 'https://notification.api.cx.metamask.io';
|
|
14
14
|
export const TRIGGER_API_BATCH_ENDPOINT = `${TRIGGER_API}/api/v1/triggers/batch`;
|
|
@@ -51,6 +51,7 @@ export async function createOnChainTriggers(userStorage, storageKey, bearerToken
|
|
|
51
51
|
for (const trigger of triggersToCreate) {
|
|
52
52
|
toggleUserStorageTriggerStatus(userStorage, trigger.config.address, String(trigger.config.chain_id), trigger.id, true);
|
|
53
53
|
}
|
|
54
|
+
cleanUserStorage(userStorage);
|
|
54
55
|
}
|
|
55
56
|
/**
|
|
56
57
|
* Deletes on-chain triggers based on the provided UUIDs.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"onchain-notifications.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/onchain-notifications.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,qBAAqB,EAAE,0CAA0C;AAC1E,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EAAE,wBAAwB,EAAE,6CAAyC;AAM5E,OAAO,EACL,WAAW,EACX,8BAA8B,EAC9B,2BAA2B,EAC5B,2BAAuB;AASxB,MAAM,CAAC,MAAM,WAAW,GAAG,oCAAoC,CAAC;AAChE,MAAM,CAAC,MAAM,gBAAgB,GAAG,yCAAyC,CAAC;AAC1E,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,WAAW,wBAAwB,CAAC;AACjF,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,gBAAgB,uBAAuB,CAAC;AACzF,MAAM,CAAC,MAAM,yCAAyC,GAAG,CAAC,IAAY,EAAE,EAAE,CACxE,GAAG,8BAA8B,SAAS,IAAI,eAAe,CAAC;AAChE,MAAM,CAAC,MAAM,0CAA0C,GAAG,GAAG,gBAAgB,oCAAoC,CAAC;AAElH;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,WAAwB,EACxB,UAAkB,EAClB,WAAmB,EACnB,QAA+B;IAY/B,MAAM,gBAAgB,GAA4B,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACrE,EAAE,EAAE,CAAC,CAAC,EAAE;QACR,KAAK,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,GAAG,UAAU,CAAC;QAChE,MAAM,EAAE;YACN,IAAI,EAAE,CAAC,CAAC,IAAI;YACZ,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;YAC3B,OAAO,EAAE,CAAC,CAAC,OAAO;SACnB;KACF,CAAC,CAAC,CAAC;IAEJ,IAAI,gBAAgB,CAAC,MAAM,KAAK,CAA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,UAAU,GAAG,CAAC,CAAC;gBACtB,MAAM;aACP;SACF;QAAC,OAAO,GAAG,EAAE;YACZ,GAAG,CAAC,KAAK,CACP,yDAAyD,UAAU,CAAC,IAAI,CACtE,IAAI,CACL,GAAG,EACJ,GAAG,CACJ,CAAC;YACF,aAAa;SACd;KACF;IAED,OAAO,oBAAoB,CAAC;AAC9B,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,WAAmB,EACnB,eAAyB;IAEzB,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE;QAChC,OAAO;KACR;IAED,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,WAAW,CAChC,WAAW,EACX,0CAA0C,EAC1C,MAAM,EACN,EAAE,GAAG,EAAE,eAAe,EAAE,CACzB,CAAC;QAEF,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;YAC3B,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YAC/D,MAAM,IAAI,KAAK,CACb,wCAAwC,SAAS,EAAE,OAAiB,EAAE,CACvE,CAAC;SACH;KACF;IAAC,OAAO,GAAG,EAAE;QACZ,GAAG,CAAC,KAAK,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;QACvD,MAAM,GAAG,CAAC;KACX;AACH,CAAC","sourcesContent":["import { UserStorageController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport { toRawOnChainNotification } from '../../shared/to-raw-notification';\nimport type {\n OnChainRawNotification,\n UnprocessedOnChainRawNotification,\n} from '../types/on-chain-notification/on-chain-notification';\nimport type { UserStorage } from '../types/user-storage/user-storage';\nimport {\n makeApiCall,\n toggleUserStorageTriggerStatus,\n traverseUserStorageTriggers,\n} from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport const TRIGGER_API = 'https://trigger.api.cx.metamask.io';\nexport const NOTIFICATION_API = 'https://notification.api.cx.metamask.io';\nexport const TRIGGER_API_BATCH_ENDPOINT = `${TRIGGER_API}/api/v1/triggers/batch`;\nexport const NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications`;\nexport const NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY = (page: number) =>\n `${NOTIFICATION_API_LIST_ENDPOINT}?page=${page}&per_page=100`;\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications/mark-as-read`;\n\n/**\n * Creates on-chain triggers based on the provided notification triggers.\n * This method generates a unique token for each trigger using the trigger ID and storage key,\n * proving ownership of the trigger being updated. It then makes an API call to create these triggers.\n * Upon successful creation, it updates the userStorage to reflect the new trigger status.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the trigger ID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param triggers - An array of notification triggers to be created. Each trigger includes an ID, chain ID, kind, and address.\n * @returns A promise that resolves to void. Throws an error if the API call fails or if there's an issue creating the triggers.\n */\nexport async function createOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n triggers: NotificationTrigger[],\n): Promise<void> {\n type RequestPayloadTrigger = {\n id: string;\n // this is the trigger token, generated by using the uuid + storage key. It proves you own the trigger you are updating\n token: string;\n config: {\n kind: string;\n chain_id: number;\n address: string;\n };\n };\n const triggersToCreate: RequestPayloadTrigger[] = triggers.map((t) => ({\n id: t.id,\n token: UserStorageController.createSHA256Hash(t.id + storageKey),\n config: {\n kind: t.kind,\n chain_id: Number(t.chainId),\n address: t.address,\n },\n }));\n\n if (triggersToCreate.length === 0) {\n return;\n }\n\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'POST',\n triggersToCreate,\n );\n\n if (!response.ok) {\n const errorData = await response.json().catch(() => undefined);\n log.error('Error creating triggers:', errorData);\n throw new Error('OnChain Notifications - unable to create triggers');\n }\n\n // If the trigger creation was fine\n // then update the userStorage\n for (const trigger of triggersToCreate) {\n toggleUserStorageTriggerStatus(\n userStorage,\n trigger.config.address,\n String(trigger.config.chain_id),\n trigger.id,\n true,\n );\n }\n}\n\n/**\n * Deletes on-chain triggers based on the provided UUIDs.\n * This method generates a unique token for each trigger using the UUID and storage key,\n * proving ownership of the trigger being deleted. It then makes an API call to delete these triggers.\n * Upon successful deletion, it updates the userStorage to remove the deleted trigger statuses.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the UUID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param uuids - An array of UUIDs representing the triggers to be deleted.\n * @returns A promise that resolves to the updated UserStorage object. Throws an error if the API call fails or if there's an issue deleting the triggers.\n */\nexport async function deleteOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n uuids: string[],\n): Promise<UserStorage> {\n const triggersToDelete = uuids.map((uuid) => ({\n id: uuid,\n token: UserStorageController.createSHA256Hash(uuid + storageKey),\n }));\n\n try {\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'DELETE',\n triggersToDelete,\n );\n\n if (!response.ok) {\n throw new Error(\n `Failed to delete on-chain notifications for uuids ${uuids.join(', ')}`,\n );\n }\n\n // Update the state of the deleted trigger to false\n for (const uuid of uuids) {\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n if (userStorage?.[address]?.[chainId]?.[uuid]) {\n delete userStorage[address][chainId][uuid];\n }\n }\n }\n }\n }\n\n // Follow-up cleanup, if an address had no triggers whatsoever, then we can delete the address\n const isEmpty = (obj = {}) => Object.keys(obj).length === 0;\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n // Chain isEmpty Check\n if (isEmpty(userStorage?.[address]?.[chainId])) {\n delete userStorage[address][chainId];\n }\n }\n\n // Address isEmpty Check\n if (isEmpty(userStorage?.[address])) {\n delete userStorage[address];\n }\n }\n }\n } catch (err) {\n log.error(\n `Error deleting on-chain notifications for uuids ${uuids.join(', ')}:`,\n err,\n );\n throw err;\n }\n\n return userStorage;\n}\n\n/**\n * Fetches on-chain notifications for the given user storage and BearerToken.\n * This method iterates through the userStorage to find enabled triggers and fetches notifications for those triggers.\n * It makes paginated API calls to the notifications service, transforming and aggregating the notifications into a single array.\n * The process stops either when all pages have been fetched or when a page has less than 100 notifications, indicating the end of the data.\n *\n * @param userStorage - The user's storage object containing trigger information.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @returns A promise that resolves to an array of OnChainRawNotification objects. If no triggers are enabled or an error occurs, it may return an empty array.\n */\nexport async function getOnChainNotifications(\n userStorage: UserStorage,\n bearerToken: string,\n): Promise<OnChainRawNotification[]> {\n const triggerIds = traverseUserStorageTriggers(userStorage, {\n mapTrigger: (t) => {\n if (!t.enabled) {\n return undefined;\n }\n return t.id;\n },\n });\n\n if (triggerIds.length === 0) {\n return [];\n }\n\n const onChainNotifications: OnChainRawNotification[] = [];\n const PAGE_LIMIT = 2;\n for (let page = 1; page <= PAGE_LIMIT; page++) {\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY(page),\n 'POST',\n { trigger_ids: triggerIds },\n );\n\n const notifications =\n (await response.json()) as UnprocessedOnChainRawNotification[];\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n .map((n): OnChainRawNotification | undefined => {\n if (!n.data?.kind) {\n return undefined;\n }\n\n return toRawOnChainNotification(n);\n })\n .filter((n): n is OnChainRawNotification => Boolean(n));\n\n onChainNotifications.push(...transformedNotifications);\n\n // if less than 100 notifications on page, then means we reached end\n if (notifications.length < 100) {\n page = PAGE_LIMIT + 1;\n break;\n }\n } catch (err) {\n log.error(\n `Error fetching on-chain notifications for trigger IDs ${triggerIds.join(\n ', ',\n )}:`,\n err,\n );\n // do nothing\n }\n }\n\n return onChainNotifications;\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,\n 'POST',\n { ids: notificationIds },\n );\n\n if (response.status !== 200) {\n const errorData = await response.json().catch(() => undefined);\n throw new Error(\n `Error marking notifications as read: ${errorData?.message as string}`,\n );\n }\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n throw err;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"onchain-notifications.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/onchain-notifications.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,qBAAqB,EAAE,0CAA0C;AAC1E,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EAAE,wBAAwB,EAAE,6CAAyC;AAM5E,OAAO,EACL,gBAAgB,EAChB,WAAW,EACX,8BAA8B,EAC9B,2BAA2B,EAC5B,2BAAuB;AASxB,MAAM,CAAC,MAAM,WAAW,GAAG,oCAAoC,CAAC;AAChE,MAAM,CAAC,MAAM,gBAAgB,GAAG,yCAAyC,CAAC;AAC1E,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,WAAW,wBAAwB,CAAC;AACjF,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,gBAAgB,uBAAuB,CAAC;AACzF,MAAM,CAAC,MAAM,yCAAyC,GAAG,CAAC,IAAY,EAAE,EAAE,CACxE,GA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AK,GAAG,EAAE;YAC3B,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YAC/D,MAAM,IAAI,KAAK,CACb,wCAAwC,SAAS,EAAE,OAAiB,EAAE,CACvE,CAAC;SACH;KACF;IAAC,OAAO,GAAG,EAAE;QACZ,GAAG,CAAC,KAAK,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;QACvD,MAAM,GAAG,CAAC;KACX;AACH,CAAC","sourcesContent":["import { UserStorageController } from '@metamask/profile-sync-controller';\nimport log from 'loglevel';\n\nimport { toRawOnChainNotification } from '../../shared/to-raw-notification';\nimport type {\n OnChainRawNotification,\n UnprocessedOnChainRawNotification,\n} from '../types/on-chain-notification/on-chain-notification';\nimport type { UserStorage } from '../types/user-storage/user-storage';\nimport {\n cleanUserStorage,\n makeApiCall,\n toggleUserStorageTriggerStatus,\n traverseUserStorageTriggers,\n} from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport const TRIGGER_API = 'https://trigger.api.cx.metamask.io';\nexport const NOTIFICATION_API = 'https://notification.api.cx.metamask.io';\nexport const TRIGGER_API_BATCH_ENDPOINT = `${TRIGGER_API}/api/v1/triggers/batch`;\nexport const NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications`;\nexport const NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY = (page: number) =>\n `${NOTIFICATION_API_LIST_ENDPOINT}?page=${page}&per_page=100`;\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v1/notifications/mark-as-read`;\n\n/**\n * Creates on-chain triggers based on the provided notification triggers.\n * This method generates a unique token for each trigger using the trigger ID and storage key,\n * proving ownership of the trigger being updated. It then makes an API call to create these triggers.\n * Upon successful creation, it updates the userStorage to reflect the new trigger status.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the trigger ID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param triggers - An array of notification triggers to be created. Each trigger includes an ID, chain ID, kind, and address.\n * @returns A promise that resolves to void. Throws an error if the API call fails or if there's an issue creating the triggers.\n */\nexport async function createOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n triggers: NotificationTrigger[],\n): Promise<void> {\n type RequestPayloadTrigger = {\n id: string;\n // this is the trigger token, generated by using the uuid + storage key. It proves you own the trigger you are updating\n token: string;\n config: {\n kind: string;\n chain_id: number;\n address: string;\n };\n };\n const triggersToCreate: RequestPayloadTrigger[] = triggers.map((t) => ({\n id: t.id,\n token: UserStorageController.createSHA256Hash(t.id + storageKey),\n config: {\n kind: t.kind,\n chain_id: Number(t.chainId),\n address: t.address,\n },\n }));\n\n if (triggersToCreate.length === 0) {\n return;\n }\n\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'POST',\n triggersToCreate,\n );\n\n if (!response.ok) {\n const errorData = await response.json().catch(() => undefined);\n log.error('Error creating triggers:', errorData);\n throw new Error('OnChain Notifications - unable to create triggers');\n }\n\n // If the trigger creation was fine\n // then update the userStorage\n for (const trigger of triggersToCreate) {\n toggleUserStorageTriggerStatus(\n userStorage,\n trigger.config.address,\n String(trigger.config.chain_id),\n trigger.id,\n true,\n );\n }\n\n cleanUserStorage(userStorage);\n}\n\n/**\n * Deletes on-chain triggers based on the provided UUIDs.\n * This method generates a unique token for each trigger using the UUID and storage key,\n * proving ownership of the trigger being deleted. It then makes an API call to delete these triggers.\n * Upon successful deletion, it updates the userStorage to remove the deleted trigger statuses.\n *\n * @param userStorage - The user's storage object where triggers and their statuses are stored.\n * @param storageKey - A key used along with the UUID to generate a unique token for each trigger.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param uuids - An array of UUIDs representing the triggers to be deleted.\n * @returns A promise that resolves to the updated UserStorage object. Throws an error if the API call fails or if there's an issue deleting the triggers.\n */\nexport async function deleteOnChainTriggers(\n userStorage: UserStorage,\n storageKey: string,\n bearerToken: string,\n uuids: string[],\n): Promise<UserStorage> {\n const triggersToDelete = uuids.map((uuid) => ({\n id: uuid,\n token: UserStorageController.createSHA256Hash(uuid + storageKey),\n }));\n\n try {\n const response = await makeApiCall(\n bearerToken,\n TRIGGER_API_BATCH_ENDPOINT,\n 'DELETE',\n triggersToDelete,\n );\n\n if (!response.ok) {\n throw new Error(\n `Failed to delete on-chain notifications for uuids ${uuids.join(', ')}`,\n );\n }\n\n // Update the state of the deleted trigger to false\n for (const uuid of uuids) {\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n if (userStorage?.[address]?.[chainId]?.[uuid]) {\n delete userStorage[address][chainId][uuid];\n }\n }\n }\n }\n }\n\n // Follow-up cleanup, if an address had no triggers whatsoever, then we can delete the address\n const isEmpty = (obj = {}) => Object.keys(obj).length === 0;\n for (const address in userStorage) {\n if (address in userStorage) {\n for (const chainId in userStorage[address]) {\n // Chain isEmpty Check\n if (isEmpty(userStorage?.[address]?.[chainId])) {\n delete userStorage[address][chainId];\n }\n }\n\n // Address isEmpty Check\n if (isEmpty(userStorage?.[address])) {\n delete userStorage[address];\n }\n }\n }\n } catch (err) {\n log.error(\n `Error deleting on-chain notifications for uuids ${uuids.join(', ')}:`,\n err,\n );\n throw err;\n }\n\n return userStorage;\n}\n\n/**\n * Fetches on-chain notifications for the given user storage and BearerToken.\n * This method iterates through the userStorage to find enabled triggers and fetches notifications for those triggers.\n * It makes paginated API calls to the notifications service, transforming and aggregating the notifications into a single array.\n * The process stops either when all pages have been fetched or when a page has less than 100 notifications, indicating the end of the data.\n *\n * @param userStorage - The user's storage object containing trigger information.\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @returns A promise that resolves to an array of OnChainRawNotification objects. If no triggers are enabled or an error occurs, it may return an empty array.\n */\nexport async function getOnChainNotifications(\n userStorage: UserStorage,\n bearerToken: string,\n): Promise<OnChainRawNotification[]> {\n const triggerIds = traverseUserStorageTriggers(userStorage, {\n mapTrigger: (t) => {\n if (!t.enabled) {\n return undefined;\n }\n return t.id;\n },\n });\n\n if (triggerIds.length === 0) {\n return [];\n }\n\n const onChainNotifications: OnChainRawNotification[] = [];\n const PAGE_LIMIT = 2;\n for (let page = 1; page <= PAGE_LIMIT; page++) {\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT_PAGE_QUERY(page),\n 'POST',\n { trigger_ids: triggerIds },\n );\n\n const notifications =\n (await response.json()) as UnprocessedOnChainRawNotification[];\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n .map((n): OnChainRawNotification | undefined => {\n if (!n.data?.kind) {\n return undefined;\n }\n\n return toRawOnChainNotification(n);\n })\n .filter((n): n is OnChainRawNotification => Boolean(n));\n\n onChainNotifications.push(...transformedNotifications);\n\n // if less than 100 notifications on page, then means we reached end\n if (notifications.length < 100) {\n page = PAGE_LIMIT + 1;\n break;\n }\n } catch (err) {\n log.error(\n `Error fetching on-chain notifications for trigger IDs ${triggerIds.join(\n ', ',\n )}:`,\n err,\n );\n // do nothing\n }\n }\n\n return onChainNotifications;\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n try {\n const response = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,\n 'POST',\n { ids: notificationIds },\n );\n\n if (response.status !== 200) {\n const errorData = await response.json().catch(() => undefined);\n throw new Error(\n `Error marking notifications as read: ${errorData?.message as string}`,\n );\n }\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n throw err;\n }\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeApiCall = exports.toggleUserStorageTriggerStatus = exports.upsertTriggerTypeTriggers = exports.upsertAddressTriggers = exports.getUUIDsForAccountByKinds = exports.getUUIDsForKinds = exports.getAllUUIDs = exports.getUUIDsForAccount = exports.inferEnabledKinds = exports.checkAccountsPresence = exports.traverseUserStorageTriggers = exports.initializeUserStorage = void 0;
|
|
3
|
+
exports.makeApiCall = exports.toggleUserStorageTriggerStatus = exports.upsertTriggerTypeTriggers = exports.upsertAddressTriggers = exports.getUUIDsForAccountByKinds = exports.getUUIDsForKinds = exports.getAllUUIDs = exports.getUUIDsForAccount = exports.inferEnabledKinds = exports.checkAccountsPresence = exports.traverseUserStorageTriggers = exports.cleanUserStorage = exports.initializeUserStorage = void 0;
|
|
4
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
4
5
|
const uuid_1 = require("uuid");
|
|
5
6
|
const constants_1 = require("../constants/constants.cjs");
|
|
6
7
|
const notification_schema_1 = require("../constants/notification-schema.cjs");
|
|
@@ -34,9 +35,10 @@ const triggerIdentity = (trigger) => trigger;
|
|
|
34
35
|
*
|
|
35
36
|
* @param accounts - An array of account objects, each optionally containing an address.
|
|
36
37
|
* @param state - A boolean indicating the initial enabled state for all triggers in the user storage.
|
|
38
|
+
* @param shouldClean - prop to clean the initialized UserStorage (removing any invalid addresses). Only false for testing purposes.
|
|
37
39
|
* @returns A `UserStorage` object populated with triggers for each account and chain.
|
|
38
40
|
*/
|
|
39
|
-
function initializeUserStorage(accounts, state) {
|
|
41
|
+
function initializeUserStorage(accounts, state, shouldClean = true) {
|
|
40
42
|
const userStorage = {
|
|
41
43
|
[constants_1.USER_STORAGE_VERSION_KEY]: constants_1.USER_STORAGE_VERSION,
|
|
42
44
|
};
|
|
@@ -60,9 +62,33 @@ function initializeUserStorage(accounts, state) {
|
|
|
60
62
|
});
|
|
61
63
|
});
|
|
62
64
|
});
|
|
65
|
+
if (shouldClean) {
|
|
66
|
+
cleanUserStorage(userStorage);
|
|
67
|
+
}
|
|
63
68
|
return userStorage;
|
|
64
69
|
}
|
|
65
70
|
exports.initializeUserStorage = initializeUserStorage;
|
|
71
|
+
/**
|
|
72
|
+
* This is a fallback to ensure that we are not adding non-hex addresses, and the shape is valid.
|
|
73
|
+
* Any invalid shapes will be removed.
|
|
74
|
+
* NOTE - this method mutates and returns the cleaned User Storage.
|
|
75
|
+
*
|
|
76
|
+
* @param userStorage - notification user storage field we are to clean.
|
|
77
|
+
* @returns a cleaned version of user storage.
|
|
78
|
+
*/
|
|
79
|
+
function cleanUserStorage(userStorage) {
|
|
80
|
+
const addresses = new Set();
|
|
81
|
+
traverseUserStorageTriggers(userStorage, {
|
|
82
|
+
mapTrigger: (t) => addresses.add(t.address),
|
|
83
|
+
});
|
|
84
|
+
addresses.forEach((addr) => {
|
|
85
|
+
if (!(0, controller_utils_1.isValidHexAddress)(addr)) {
|
|
86
|
+
delete userStorage[addr];
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return userStorage;
|
|
90
|
+
}
|
|
91
|
+
exports.cleanUserStorage = cleanUserStorage;
|
|
66
92
|
/**
|
|
67
93
|
* Iterates over user storage to find and optionally transform notification triggers.
|
|
68
94
|
* This method allows for flexible retrieval and transformation of triggers based on provided options.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":";;;AAAA,+BAAoC;AAEpC,0DAGgC;AAEhC,8EAA4D;AAoB5D;;;;;;;GAOG;AACH,MAAM,WAAW,GAAG,CAAC,OAA4B,EAAU,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;AAEz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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;AAhBD,kCAgBC","sourcesContent":["import { 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 * @returns A `UserStorage` object populated with triggers for each account and chain.\n */\nexport function initializeUserStorage(\n accounts: { address?: string }[],\n state: boolean,\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 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"]}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":";;;AAAA,iEAA+D;AAC/D,+BAAoC;AAEpC,0DAGgC;AAEhC,8EAA4D;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,SAAgB,qBAAqB,CACnC,QAAgC,EAChC,KAAc,EACd,WAAW,GAAG,IAAI;IAElB,MAAM,WAAW,GAAgB;QAC/B,CAAC,oCAAwB,CAAC,EAAE,gCAAoB;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,8BAAQ,CAAC,CAAC,OAAO,CAC9B,CAAC,CAAC,OAAO,EAAE,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC,EAAE,EAAE;YACnD,eAAe,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AAQ,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC;AAhBD,kCAgBC","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"]}
|
|
@@ -24,11 +24,21 @@ type TraverseTriggerOpts<Result> = {
|
|
|
24
24
|
*
|
|
25
25
|
* @param accounts - An array of account objects, each optionally containing an address.
|
|
26
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.
|
|
27
28
|
* @returns A `UserStorage` object populated with triggers for each account and chain.
|
|
28
29
|
*/
|
|
29
30
|
export declare function initializeUserStorage(accounts: {
|
|
30
31
|
address?: string;
|
|
31
|
-
}[], state: boolean): UserStorage;
|
|
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;
|
|
32
42
|
/**
|
|
33
43
|
* Iterates over user storage to find and optionally transform notification triggers.
|
|
34
44
|
* This method allows for flexible retrieval and transformation of triggers based on provided options.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.cts","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,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Mb;AAED;;;;;;;;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"}
|
|
@@ -24,11 +24,21 @@ type TraverseTriggerOpts<Result> = {
|
|
|
24
24
|
*
|
|
25
25
|
* @param accounts - An array of account objects, each optionally containing an address.
|
|
26
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.
|
|
27
28
|
* @returns A `UserStorage` object populated with triggers for each account and chain.
|
|
28
29
|
*/
|
|
29
30
|
export declare function initializeUserStorage(accounts: {
|
|
30
31
|
address?: string;
|
|
31
|
-
}[], state: boolean): UserStorage;
|
|
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;
|
|
32
42
|
/**
|
|
33
43
|
* Iterates over user storage to find and optionally transform notification triggers.
|
|
34
44
|
* This method allows for flexible retrieval and transformation of triggers based on provided options.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesController/utils/utils.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EA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E,MAAM,GACd,MAAM,EAAE,CAKV;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,WAAW,EAAE,WAAW,GAAG,MAAM,EAAE,CAI9D;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,WAAW,EACxB,YAAY,EAAE,MAAM,EAAE,GACrB,MAAM,EAAE,CAMV;AAED;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,aAAa,EAAE,GAC5B,mBAAmB,EAAE,CAWvB;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,WAAW,EAAE,WAAW,GACvB,WAAW,CA8Bb;AAED;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,aAAa,EAC1B,WAAW,EAAE,WAAW,GACvB,WAAW,CA0Bb;AAED;;;;;;;;;GASG;AACH,wBAAgB,8BAA8B,CAC5C,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GACf,WAAW,CAMb;AAED;;;;;;;;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,3 +1,4 @@
|
|
|
1
|
+
import { isValidHexAddress } from "@metamask/controller-utils";
|
|
1
2
|
import { v4 as uuidv4 } from "uuid";
|
|
2
3
|
import { USER_STORAGE_VERSION_KEY, USER_STORAGE_VERSION } from "../constants/constants.mjs";
|
|
3
4
|
import { TRIGGERS } from "../constants/notification-schema.mjs";
|
|
@@ -31,9 +32,10 @@ const triggerIdentity = (trigger) => trigger;
|
|
|
31
32
|
*
|
|
32
33
|
* @param accounts - An array of account objects, each optionally containing an address.
|
|
33
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.
|
|
34
36
|
* @returns A `UserStorage` object populated with triggers for each account and chain.
|
|
35
37
|
*/
|
|
36
|
-
export function initializeUserStorage(accounts, state) {
|
|
38
|
+
export function initializeUserStorage(accounts, state, shouldClean = true) {
|
|
37
39
|
const userStorage = {
|
|
38
40
|
[USER_STORAGE_VERSION_KEY]: USER_STORAGE_VERSION,
|
|
39
41
|
};
|
|
@@ -57,6 +59,29 @@ export function initializeUserStorage(accounts, state) {
|
|
|
57
59
|
});
|
|
58
60
|
});
|
|
59
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
|
+
});
|
|
60
85
|
return userStorage;
|
|
61
86
|
}
|
|
62
87
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRo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AAM,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 { 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 * @returns A `UserStorage` object populated with triggers for each account and chain.\n */\nexport function initializeUserStorage(\n accounts: { address?: string }[],\n state: boolean,\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 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"]}
|
|
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,C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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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/notification-services-controller",
|
|
3
|
-
"version": "0.21.0-preview-
|
|
3
|
+
"version": "0.21.0-preview-7a21cbc",
|
|
4
4
|
"description": "Manages New MetaMask decentralized Notification system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"@contentful/rich-text-html-renderer": "^16.5.2",
|
|
103
103
|
"@metamask/base-controller": "^8.0.0",
|
|
104
104
|
"@metamask/controller-utils": "^11.5.0",
|
|
105
|
-
"@metamask/utils": "^11.
|
|
105
|
+
"@metamask/utils": "^11.2.0",
|
|
106
106
|
"bignumber.js": "^9.1.2",
|
|
107
107
|
"firebase": "^11.2.0",
|
|
108
108
|
"loglevel": "^1.8.1",
|