@metamask-previews/phishing-controller 17.1.2-preview-e5cef32c6 → 17.1.2-preview-a96479ea9
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/CHANGELOG.md +0 -4
- package/dist/PhishingController-method-action-types.cjs.map +1 -1
- package/dist/PhishingController-method-action-types.d.cts +2 -3
- package/dist/PhishingController-method-action-types.d.cts.map +1 -1
- package/dist/PhishingController-method-action-types.d.mts +2 -3
- package/dist/PhishingController-method-action-types.d.mts.map +1 -1
- package/dist/PhishingController-method-action-types.mjs.map +1 -1
- package/dist/PhishingController.cjs +7 -9
- package/dist/PhishingController.cjs.map +1 -1
- package/dist/PhishingController.d.cts +2 -3
- package/dist/PhishingController.d.cts.map +1 -1
- package/dist/PhishingController.d.mts +2 -3
- package/dist/PhishingController.d.mts.map +1 -1
- package/dist/PhishingController.mjs +8 -10
- package/dist/PhishingController.mjs.map +1 -1
- package/dist/index.cjs +1 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +0 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -1
- package/dist/index.mjs.map +1 -1
- package/dist/utils.cjs +1 -45
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +0 -19
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +0 -19
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +0 -42
- package/dist/utils.mjs.map +1 -1
- package/package.json +1 -1
package/dist/utils.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.splitCacheHits = exports.resolveChainName = exports.isApprovalSupportedChain = exports.buildCacheKey = exports.generateParentDomains = exports.getPathnameFromUrl = exports.
|
|
3
|
+
exports.splitCacheHits = exports.resolveChainName = exports.isApprovalSupportedChain = exports.buildCacheKey = exports.generateParentDomains = exports.getPathnameFromUrl = exports.getHostnameFromWebUrl = exports.getHostnameFromUrl = exports.sha256Hash = exports.matchPartsAgainstList = exports.domainPartsToFuzzyForm = exports.domainPartsToDomain = exports.processConfigs = exports.getDefaultPhishingDetectorConfig = exports.processDomainList = exports.domainToParts = exports.validateConfig = exports.applyDiffs = exports.getHostnameAndPathComponents = exports.roundToNearestMinute = exports.fetchTimeNow = void 0;
|
|
4
4
|
const utils_1 = require("@noble/hashes/utils");
|
|
5
5
|
const sha256_1 = require("ethereum-cryptography/sha256");
|
|
6
6
|
const PathTrie_1 = require("./PathTrie.cjs");
|
|
@@ -314,50 +314,6 @@ const getHostnameFromWebUrl = (url) => {
|
|
|
314
314
|
return [hostname || '', Boolean(hostname)];
|
|
315
315
|
};
|
|
316
316
|
exports.getHostnameFromWebUrl = getHostnameFromWebUrl;
|
|
317
|
-
/**
|
|
318
|
-
* Hosts where PDS single-URL scans include the URL path (shared gateways / hosts where many sites
|
|
319
|
-
* share one origin). For all other hosts, only the hostname is sent.
|
|
320
|
-
*/
|
|
321
|
-
exports.PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS = [
|
|
322
|
-
'ipfs.io',
|
|
323
|
-
'dweb.link',
|
|
324
|
-
'cf-ipfs.com',
|
|
325
|
-
'cloudflare-ipfs.com',
|
|
326
|
-
'irys.xyz',
|
|
327
|
-
'sites.google.com',
|
|
328
|
-
];
|
|
329
|
-
/**
|
|
330
|
-
* @param hostname - Lowercase normalization is applied for matching registered roots and subdomains.
|
|
331
|
-
* @returns Whether {@link getPhishingDetectionScanUrlParam} appends pathname for this hostname.
|
|
332
|
-
*/
|
|
333
|
-
function isPhishingDetectionPathBasedHostname(hostname) {
|
|
334
|
-
const normalizedHost = hostname.toLowerCase();
|
|
335
|
-
return exports.PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS.some((root) => normalizedHost === root || normalizedHost.endsWith(`.${root}`));
|
|
336
|
-
}
|
|
337
|
-
exports.isPhishingDetectionPathBasedHostname = isPhishingDetectionPathBasedHostname;
|
|
338
|
-
/**
|
|
339
|
-
* Builds the `url` query parameter for {@link PhishingController.scanUrl}. For hosts in
|
|
340
|
-
* {@link PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS} (and their subdomains), the value is hostname
|
|
341
|
-
* plus pathname, without protocol, query, or fragment. For all other hosts, only hostname is used.
|
|
342
|
-
*
|
|
343
|
-
* @param url - A web URL string (must use `http:` or `https:` — same rules as {@link getHostnameFromWebUrl}).
|
|
344
|
-
* @returns A tuple of `[scanUrlParam, ok]` where `ok` is false when the URL is not a valid web URL.
|
|
345
|
-
*/
|
|
346
|
-
const getPhishingDetectionScanUrlParam = (url) => {
|
|
347
|
-
const [hostname, ok] = (0, exports.getHostnameFromWebUrl)(url);
|
|
348
|
-
if (!ok) {
|
|
349
|
-
return ['', false];
|
|
350
|
-
}
|
|
351
|
-
if (!isPhishingDetectionPathBasedHostname(hostname)) {
|
|
352
|
-
return [hostname, true];
|
|
353
|
-
}
|
|
354
|
-
// `getHostnameFromWebUrl` already required a successful `new URL(url)` parse.
|
|
355
|
-
const { pathname } = new URL(url);
|
|
356
|
-
const pathSuffix = pathname === '/' ? '' : pathname;
|
|
357
|
-
const scanUrlParam = pathSuffix ? `${hostname}${pathSuffix}` : hostname;
|
|
358
|
-
return [scanUrlParam, true];
|
|
359
|
-
};
|
|
360
|
-
exports.getPhishingDetectionScanUrlParam = getPhishingDetectionScanUrlParam;
|
|
361
317
|
const getPathnameFromUrl = (url) => {
|
|
362
318
|
try {
|
|
363
319
|
const { pathname } = new URL(url);
|
package/dist/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,+CAAiD;AACjD,yDAAsD;AAEtD,6CAA4E;AAE5E,iEAAwE;AAKxE,uCAA8E;AAO9E,MAAM,iBAAiB,GAAG,CAAC,CA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es/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\n\nimport { deleteFromTrie, insertToTrie, deepCopyPathTrie } from './PathTrie';\nimport type { Hotlist, PhishingListState } from './PhishingController';\nimport { ListKeys, phishingListKeyNameMap } from './PhishingController';\nimport type {\n PhishingDetectorList,\n PhishingDetectorConfiguration,\n} from './PhishingDetector';\nimport { APPROVAL_SUPPORTED_CHAINS, DEFAULT_CHAIN_ID_TO_NAME } from './types';\nimport type {\n ApprovalSupportedChain,\n TokenScanCacheData,\n TokenScanResult,\n} from './types';\n\nconst DEFAULT_TOLERANCE = 3;\n\n/**\n * Fetches current epoch time in seconds.\n *\n * @returns the Date.now() time in seconds instead of miliseconds. backend files rely on timestamps in seconds since epoch.\n */\nexport const fetchTimeNow = (): number => Math.round(Date.now() / 1000);\n\n/**\n * Rounds a Unix timestamp down to the nearest minute.\n *\n * @param unixTimestamp - The Unix timestamp to be rounded.\n * @returns The rounded Unix timestamp.\n */\nexport function roundToNearestMinute(unixTimestamp: number): number {\n return Math.floor(unixTimestamp / 60) * 60;\n}\n\n/**\n * Split a string into two pieces, using the first period as the delimiter.\n *\n * @param stringToSplit - The string to split.\n * @returns An array of length two containing the beginning and end of the string.\n */\nconst splitStringByPeriod = <Start extends string, End extends string>(\n stringToSplit: `${Start}.${End}`,\n): [Start, End] => {\n const periodIndex = stringToSplit.indexOf('.');\n return [\n stringToSplit.slice(0, periodIndex) as Start,\n stringToSplit.slice(periodIndex + 1) as End,\n ];\n};\n\nexport const getHostnameAndPathComponents = (\n url: string,\n): { hostname: string; pathComponents: string[] } => {\n const urlWithProtocol = url.startsWith('http') ? url : `https://${url}`;\n try {\n const { hostname, pathname } = new URL(urlWithProtocol);\n return {\n hostname: hostname.toLowerCase(),\n pathComponents: pathname\n .split('/')\n .filter(Boolean)\n .map((component) => decodeURIComponent(component)),\n };\n } catch {\n return {\n hostname: '',\n pathComponents: [],\n };\n }\n};\n\n/**\n * Determines which diffs are applicable to the listState, then applies those diffs.\n *\n * @param listState - the stalelist or the existing liststate that diffs will be applied to.\n * @param hotlistDiffs - the diffs to apply to the listState if valid.\n * @param listKey - the key associated with the input/output phishing list state.\n * @param recentlyAddedC2Domains - list of hashed C2 domains to add to the local c2 domain blocklist\n * @param recentlyRemovedC2Domains - list of hashed C2 domains to remove from the local c2 domain blocklist\n * @returns the new list state\n */\nexport const applyDiffs = (\n listState: PhishingListState,\n hotlistDiffs: Hotlist,\n listKey: ListKeys,\n recentlyAddedC2Domains: string[] = [],\n recentlyRemovedC2Domains: string[] = [],\n): PhishingListState => {\n // filter to remove diffs that were added before the lastUpdate time.\n // filter to remove diffs that aren't applicable to the specified list (by listKey).\n const diffsToApply = hotlistDiffs.filter(\n ({ timestamp, targetList }) =>\n timestamp > listState.lastUpdated &&\n splitStringByPeriod(targetList)[0] === listKey,\n );\n\n // the reason behind using latestDiffTimestamp as the lastUpdated time\n // is so that we can benefit server-side from memoization due to end client's\n // `GET /v1/diffSince/:timestamp` requests lining up with\n // our periodic updates (which create diffs at specific timestamps).\n let latestDiffTimestamp = listState.lastUpdated;\n\n const listSets = {\n allowlist: new Set(listState.allowlist),\n blocklist: new Set(listState.blocklist),\n fuzzylist: new Set(listState.fuzzylist),\n c2DomainBlocklist: new Set(listState.c2DomainBlocklist),\n };\n\n // deep copy of blocklistPaths to avoid mutating the original\n const newBlocklistPaths = deepCopyPathTrie(listState.blocklistPaths);\n\n for (const { isRemoval, targetList, url, timestamp } of diffsToApply) {\n const targetListType = splitStringByPeriod(targetList)[1];\n if (timestamp > latestDiffTimestamp) {\n latestDiffTimestamp = timestamp;\n }\n\n if (isRemoval) {\n if (targetListType === 'blocklistPaths') {\n deleteFromTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].delete(url);\n }\n continue;\n }\n\n if (targetListType === 'blocklistPaths') {\n insertToTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].add(url);\n }\n }\n\n if (listKey === ListKeys.EthPhishingDetectConfig) {\n for (const hash of recentlyAddedC2Domains) {\n listSets.c2DomainBlocklist.add(hash);\n }\n for (const hash of recentlyRemovedC2Domains) {\n listSets.c2DomainBlocklist.delete(hash);\n }\n }\n\n return {\n c2DomainBlocklist: Array.from(listSets.c2DomainBlocklist),\n allowlist: Array.from(listSets.allowlist),\n blocklist: Array.from(listSets.blocklist),\n fuzzylist: Array.from(listSets.fuzzylist),\n blocklistPaths: newBlocklistPaths,\n version: listState.version,\n name: phishingListKeyNameMap[listKey],\n tolerance: listState.tolerance,\n lastUpdated: latestDiffTimestamp,\n };\n};\n\n/**\n * Validates the configuration object for the phishing detector.\n *\n * @param config - the configuration object to validate.\n * @throws an error if the configuration is invalid.\n */\nexport function validateConfig(\n config: unknown,\n): asserts config is PhishingListState {\n if (config === null || typeof config !== 'object') {\n throw new Error('Invalid config');\n }\n\n if ('tolerance' in config && !('fuzzylist' in config)) {\n throw new Error('Fuzzylist tolerance provided without fuzzylist');\n }\n\n if (\n 'name' in config &&\n (typeof config.name !== 'string' || config.name === '')\n ) {\n throw new Error(\"Invalid config parameter: 'name'\");\n }\n\n if (\n 'version' in config &&\n (!['number', 'string'].includes(typeof config.version) ||\n config.version === '')\n ) {\n throw new Error(\"Invalid config parameter: 'version'\");\n }\n}\n\n/**\n * Converts a domain string to a list of domain parts.\n *\n * @param domain - the domain string to convert.\n * @returns the list of domain parts.\n */\nexport const domainToParts = (domain: string) => {\n return domain.split('.').reverse();\n};\n\n/**\n * Converts a list of domain strings to a list of domain parts.\n *\n * @param list - the list of domain strings to convert.\n * @returns the list of domain parts.\n */\nexport const processDomainList = (list: string[]): string[][] => {\n return list.reduce<string[][]>((acc, domain) => {\n if (typeof domain !== 'string') {\n console.warn(`Invalid domain value in list: ${JSON.stringify(domain)}`);\n return acc;\n }\n acc.push(domainToParts(domain));\n return acc;\n }, []);\n};\n\n/**\n * Gets the default phishing detector configuration.\n *\n * @param override - the optional override for the configuration.\n * @param override.allowlist - the optional allowlist to override.\n * @param override.blocklist - the optional blocklist to override.\n * @param override.fuzzylist - the optional fuzzylist to override.\n * @param override.tolerance - the optional tolerance to override.\n * @returns the default phishing detector configuration.\n */\nexport const getDefaultPhishingDetectorConfig = ({\n allowlist = [],\n blocklist = [],\n fuzzylist = [],\n tolerance = DEFAULT_TOLERANCE,\n}: {\n allowlist?: string[];\n blocklist?: string[];\n fuzzylist?: string[];\n tolerance?: number;\n}): PhishingDetectorConfiguration => {\n return {\n allowlist: processDomainList(allowlist),\n // We can assume that blocklist is already separated into hostname-only entries\n // and hostname+path entries so we do not need to separate it again.\n blocklist: processDomainList(blocklist),\n fuzzylist: processDomainList(fuzzylist),\n tolerance,\n };\n};\n\n/**\n * Processes the configurations for the phishing detector, filtering out any invalid configs.\n *\n * @param configs - The configurations to process.\n * @returns An array of processed and valid configurations.\n */\nexport const processConfigs = (\n configs: PhishingDetectorList[] = [],\n): PhishingDetectorConfiguration[] => {\n return configs\n .filter((config) => {\n try {\n validateConfig(config);\n return true;\n } catch (error) {\n console.error(error);\n return false;\n }\n })\n .map((config) => ({\n ...config,\n ...getDefaultPhishingDetectorConfig(config),\n }));\n};\n\n/**\n * Converts a list of domain parts to a domain string.\n *\n * @param domainParts - the list of domain parts.\n * @returns the domain string.\n */\nexport const domainPartsToDomain = (domainParts: string[]) => {\n return domainParts.slice().reverse().join('.');\n};\n\n/**\n * Converts a list of domain parts to a fuzzy form.\n *\n * @param domainParts - the list of domain parts.\n * @returns the fuzzy form of the domain.\n */\nexport const domainPartsToFuzzyForm = (domainParts: string[]) => {\n return domainParts.slice(1).reverse().join('.');\n};\n\n/**\n * Matches the target parts, ignoring extra subdomains on source.\n *\n * @param source - the source domain parts.\n * @param list - the list of domain parts to match against.\n * @returns the parts for the first found matching entry.\n */\nexport const matchPartsAgainstList = (source: string[], list: string[][]) => {\n return list.find((target) => {\n // target domain has more parts than source, fail\n if (target.length > source.length) {\n return false;\n }\n // source matches target or (is deeper subdomain)\n return target.every((part, index) => source[index] === part);\n });\n};\n\n/**\n * Generate the SHA-256 hash of a hostname.\n *\n * @param hostname - The hostname to hash.\n * @returns The SHA-256 hash of the hostname.\n */\nexport const sha256Hash = (hostname: string): string => {\n const hashBuffer = sha256(new TextEncoder().encode(hostname.toLowerCase()));\n return bytesToHex(hashBuffer);\n};\n\n/**\n * Extracts the hostname from a URL.\n *\n * @param url - The URL to extract the hostname from.\n * @returns The hostname extracted from the URL, or null if the URL is invalid.\n */\nexport const getHostnameFromUrl = (url: string): string | null => {\n let hostname;\n try {\n hostname = new URL(url).hostname;\n // above will not throw if 'http://.' is passed. in fact, any string with a dot will pass.\n if (!hostname || hostname.split('.').join('') === '') {\n return null;\n }\n } catch {\n return null;\n }\n return hostname;\n};\n\n/**\n * getHostnameFromWebUrl returns the hostname from a web URL.\n * It returns the hostname and a boolean indicating if the hostname is valid.\n *\n * @param url - The web URL to extract the hostname from.\n * @returns A tuple containing the extracted hostname and a boolean indicating if the hostname is valid.\n * @example\n * getHostnameFromWebUrl('https://example.com') // Returns: ['example.com', true]\n * getHostnameFromWebUrl('example.com') // Returns: ['', false]\n * getHostnameFromWebUrl('https://') // Returns: ['', false]\n * getHostnameFromWebUrl('') // Returns: ['', false]\n */\nexport const getHostnameFromWebUrl = (url: string): [string, boolean] => {\n if (\n !url.toLowerCase().startsWith('http://') &&\n !url.toLowerCase().startsWith('https://')\n ) {\n return ['', false];\n }\n\n const hostname = getHostnameFromUrl(url);\n return [hostname || '', Boolean(hostname)];\n};\n\n/**\n * Hosts where PDS single-URL scans include the URL path (shared gateways / hosts where many sites\n * share one origin). For all other hosts, only the hostname is sent.\n */\nexport const PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS = [\n 'ipfs.io',\n 'dweb.link',\n 'cf-ipfs.com',\n 'cloudflare-ipfs.com',\n 'irys.xyz',\n 'sites.google.com',\n] as const;\n\n/**\n * @param hostname - Lowercase normalization is applied for matching registered roots and subdomains.\n * @returns Whether {@link getPhishingDetectionScanUrlParam} appends pathname for this hostname.\n */\nexport function isPhishingDetectionPathBasedHostname(\n hostname: string,\n): boolean {\n const normalizedHost = hostname.toLowerCase();\n return PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS.some(\n (root) => normalizedHost === root || normalizedHost.endsWith(`.${root}`),\n );\n}\n\n/**\n * Builds the `url` query parameter for {@link PhishingController.scanUrl}. For hosts in\n * {@link PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS} (and their subdomains), the value is hostname\n * plus pathname, without protocol, query, or fragment. For all other hosts, only hostname is used.\n *\n * @param url - A web URL string (must use `http:` or `https:` — same rules as {@link getHostnameFromWebUrl}).\n * @returns A tuple of `[scanUrlParam, ok]` where `ok` is false when the URL is not a valid web URL.\n */\nexport const getPhishingDetectionScanUrlParam = (\n url: string,\n): [scanUrlParam: string, ok: boolean] => {\n const [hostname, ok] = getHostnameFromWebUrl(url);\n if (!ok) {\n return ['', false];\n }\n\n if (!isPhishingDetectionPathBasedHostname(hostname)) {\n return [hostname, true];\n }\n\n // `getHostnameFromWebUrl` already required a successful `new URL(url)` parse.\n const { pathname } = new URL(url);\n const pathSuffix = pathname === '/' ? '' : pathname;\n const scanUrlParam = pathSuffix ? `${hostname}${pathSuffix}` : hostname;\n\n return [scanUrlParam, true];\n};\n\nexport const getPathnameFromUrl = (url: string): string => {\n try {\n const { pathname } = new URL(url);\n return pathname;\n } catch {\n return '';\n }\n};\n\n/**\n * Generates all possible parent domains up to a specified limit.\n *\n * @param sourceParts - The list of domain parts in normal order (e.g., ['evil', 'domain', 'co', 'uk']).\n * @param limit - The maximum number of parent domains to generate (default is 5).\n * @returns An array of parent domains starting from the base TLD to the most specific subdomain.\n * @example\n * generateParentDomains(['evil', 'domain', 'co', 'uk'], 5)\n * // Returns: ['co.uk', 'domain.co.uk', 'evil.domain.co.uk']\n *\n * generateParentDomains(['uk'], 5)\n * // Returns: ['uk']\n *\n * generateParentDomains(['sub', 'example', 'com'], 5)\n * // Returns: ['example.com', 'sub.example.com']\n */\nexport const generateParentDomains = (\n sourceParts: string[],\n limit = 5,\n): string[] => {\n const domains: string[] = [];\n\n if (sourceParts.length === 0) {\n return domains;\n }\n\n if (sourceParts.length === 1) {\n // Single-segment hostname (e.g., 'uk')\n domains.push(sourceParts[0].toLowerCase());\n } else {\n // Start with the base domain or TLD (last two labels, e.g., 'co.uk' or 'example.com')\n const baseDomain = sourceParts.slice(-2).join('.');\n domains.push(baseDomain.toLowerCase());\n\n // Iteratively add one subdomain level at a time, up to the specified limit\n for (\n let i = sourceParts.length - 3;\n i >= 0 && domains.length < limit;\n i--\n ) {\n const domain = sourceParts.slice(i).join('.');\n domains.push(domain.toLowerCase());\n }\n }\n\n return domains;\n};\n\n/**\n * Builds a cache key for a token scan result.\n *\n * @param chainId - The chain ID.\n * @param address - The token address.\n * @param caseSensitive - When `true`, the address is kept as-is (for chains\n * like Solana where addresses are case-sensitive). When `false` (default),\n * the address is lowercased (appropriate for EVM).\n * @returns The cache key.\n */\nexport const buildCacheKey = (\n chainId: string,\n address: string,\n caseSensitive = false,\n) => {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n return `${chainId.toLowerCase()}:${normalizedAddress}`;\n};\n\n/**\n * Determines whether a chain name is supported for token approval scanning.\n *\n * @param chain - The chain name to check.\n * @returns `true` if the chain is supported, `false` otherwise.\n */\nexport const isApprovalSupportedChain = (\n chain: string,\n): chain is ApprovalSupportedChain =>\n (APPROVAL_SUPPORTED_CHAINS as readonly string[]).includes(chain);\n\n/**\n * Resolves the chain name from a chain ID.\n *\n * @param chainId - The chain ID.\n * @param mapping - The mapping of chain IDs to chain names.\n * @returns The chain name.\n */\nexport const resolveChainName = (\n chainId: string,\n mapping = DEFAULT_CHAIN_ID_TO_NAME,\n): string | null => {\n return mapping[chainId.toLowerCase() as keyof typeof mapping] ?? null;\n};\n\n/**\n * Split tokens into cached results and tokens that need to be fetched.\n *\n * @param cache - Cache-like object with get method.\n * @param cache.get - Method to retrieve cached data by key.\n * @param chainId - The chain ID.\n * @param tokens - Array of token addresses.\n * @param caseSensitive - When `true`, token addresses are kept as-is (for\n * chains like Solana where addresses are case-sensitive). When `false`\n * (default), addresses are lowercased (appropriate for EVM).\n * @returns Object containing cached results and tokens to fetch.\n */\nexport const splitCacheHits = (\n cache: { get: (key: string) => TokenScanCacheData | undefined },\n chainId: string,\n tokens: string[],\n caseSensitive = false,\n): {\n cachedResults: Record<string, TokenScanResult>;\n tokensToFetch: string[];\n} => {\n const cachedResults: Record<string, TokenScanResult> = {};\n const tokensToFetch: string[] = [];\n\n for (const address of tokens) {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n const key = buildCacheKey(chainId, normalizedAddress, caseSensitive);\n const hit = cache.get(key);\n if (hit) {\n cachedResults[normalizedAddress] = {\n result_type: hit.result_type,\n chain: chainId,\n address: normalizedAddress,\n };\n } else {\n tokensToFetch.push(normalizedAddress);\n }\n }\n\n return { cachedResults, tokensToFetch };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,+CAAiD;AACjD,yDAAsD;AAEtD,6CAA4E;AAE5E,iEAAwE;AAKxE,uCAA8E;AAO9E,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAE5B;;;;GAIG;AACI,MAAM,YAAY,GAAG,GAAW,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;AAA3D,QAAA,YAAY,gBAA+C;AAExE;;;;;GAKG;AACH,SAAgB,oBAAoB,CAAC,aAAqB;IACxD,OAAO,IAAI,CAAC,KAAK,CAAC,aAAa,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;AAC7C,CAAC;AAFD,oDAEC;AAED;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,CAC1B,aAAgC,EAClB,EAAE;IAChB,MAAM,WAAW,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC/C,OAAO;QACL,aAAa,CAAC,KAAK,CAAC,CAAC,EAAE,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,EAAE;IACF,MAAM,aAAa,GAAoC,EAAE,CAAC;IAC1D,MAAM,aAAa,GAAa,EAAE,CAAC;IAEnC,KAAK,MAAM,OAAO,IAAI,MAAM,EAAE,CAAC;QAC7B,MAAM,iBAAiB,GAAG,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;QAC1E,MAAM,GAAG,GAAG,IAAA,qBAAa,EAAC,OAAO,EAAE,iBAAiB,EAAE,aAAa,CAAC,CAAC;QACrE,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC3B,IAAI,GAAG,EAAE,CAAC;YACR,aAAa,CAAC,iBAAiB,CAAC,GAAG;gBACjC,WAAW,EAAE,GAAG,CAAC,WAAW;gBAC5B,KAAK,EAAE,OAAO;gBACd,OAAO,EAAE,iBAAiB;aAC3B,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,aAAa,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QACxC,CAAC;IACH,CAAC;IAED,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAC1C,CAAC,CAAC;AA5BW,QAAA,cAAc,kBA4BzB","sourcesContent":["import { bytesToHex } from '@noble/hashes/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\n\nimport { deleteFromTrie, insertToTrie, deepCopyPathTrie } from './PathTrie';\nimport type { Hotlist, PhishingListState } from './PhishingController';\nimport { ListKeys, phishingListKeyNameMap } from './PhishingController';\nimport type {\n PhishingDetectorList,\n PhishingDetectorConfiguration,\n} from './PhishingDetector';\nimport { APPROVAL_SUPPORTED_CHAINS, DEFAULT_CHAIN_ID_TO_NAME } from './types';\nimport type {\n ApprovalSupportedChain,\n TokenScanCacheData,\n TokenScanResult,\n} from './types';\n\nconst DEFAULT_TOLERANCE = 3;\n\n/**\n * Fetches current epoch time in seconds.\n *\n * @returns the Date.now() time in seconds instead of miliseconds. backend files rely on timestamps in seconds since epoch.\n */\nexport const fetchTimeNow = (): number => Math.round(Date.now() / 1000);\n\n/**\n * Rounds a Unix timestamp down to the nearest minute.\n *\n * @param unixTimestamp - The Unix timestamp to be rounded.\n * @returns The rounded Unix timestamp.\n */\nexport function roundToNearestMinute(unixTimestamp: number): number {\n return Math.floor(unixTimestamp / 60) * 60;\n}\n\n/**\n * Split a string into two pieces, using the first period as the delimiter.\n *\n * @param stringToSplit - The string to split.\n * @returns An array of length two containing the beginning and end of the string.\n */\nconst splitStringByPeriod = <Start extends string, End extends string>(\n stringToSplit: `${Start}.${End}`,\n): [Start, End] => {\n const periodIndex = stringToSplit.indexOf('.');\n return [\n stringToSplit.slice(0, periodIndex) as Start,\n stringToSplit.slice(periodIndex + 1) as End,\n ];\n};\n\nexport const getHostnameAndPathComponents = (\n url: string,\n): { hostname: string; pathComponents: string[] } => {\n const urlWithProtocol = url.startsWith('http') ? url : `https://${url}`;\n try {\n const { hostname, pathname } = new URL(urlWithProtocol);\n return {\n hostname: hostname.toLowerCase(),\n pathComponents: pathname\n .split('/')\n .filter(Boolean)\n .map((component) => decodeURIComponent(component)),\n };\n } catch {\n return {\n hostname: '',\n pathComponents: [],\n };\n }\n};\n\n/**\n * Determines which diffs are applicable to the listState, then applies those diffs.\n *\n * @param listState - the stalelist or the existing liststate that diffs will be applied to.\n * @param hotlistDiffs - the diffs to apply to the listState if valid.\n * @param listKey - the key associated with the input/output phishing list state.\n * @param recentlyAddedC2Domains - list of hashed C2 domains to add to the local c2 domain blocklist\n * @param recentlyRemovedC2Domains - list of hashed C2 domains to remove from the local c2 domain blocklist\n * @returns the new list state\n */\nexport const applyDiffs = (\n listState: PhishingListState,\n hotlistDiffs: Hotlist,\n listKey: ListKeys,\n recentlyAddedC2Domains: string[] = [],\n recentlyRemovedC2Domains: string[] = [],\n): PhishingListState => {\n // filter to remove diffs that were added before the lastUpdate time.\n // filter to remove diffs that aren't applicable to the specified list (by listKey).\n const diffsToApply = hotlistDiffs.filter(\n ({ timestamp, targetList }) =>\n timestamp > listState.lastUpdated &&\n splitStringByPeriod(targetList)[0] === listKey,\n );\n\n // the reason behind using latestDiffTimestamp as the lastUpdated time\n // is so that we can benefit server-side from memoization due to end client's\n // `GET /v1/diffSince/:timestamp` requests lining up with\n // our periodic updates (which create diffs at specific timestamps).\n let latestDiffTimestamp = listState.lastUpdated;\n\n const listSets = {\n allowlist: new Set(listState.allowlist),\n blocklist: new Set(listState.blocklist),\n fuzzylist: new Set(listState.fuzzylist),\n c2DomainBlocklist: new Set(listState.c2DomainBlocklist),\n };\n\n // deep copy of blocklistPaths to avoid mutating the original\n const newBlocklistPaths = deepCopyPathTrie(listState.blocklistPaths);\n\n for (const { isRemoval, targetList, url, timestamp } of diffsToApply) {\n const targetListType = splitStringByPeriod(targetList)[1];\n if (timestamp > latestDiffTimestamp) {\n latestDiffTimestamp = timestamp;\n }\n\n if (isRemoval) {\n if (targetListType === 'blocklistPaths') {\n deleteFromTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].delete(url);\n }\n continue;\n }\n\n if (targetListType === 'blocklistPaths') {\n insertToTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].add(url);\n }\n }\n\n if (listKey === ListKeys.EthPhishingDetectConfig) {\n for (const hash of recentlyAddedC2Domains) {\n listSets.c2DomainBlocklist.add(hash);\n }\n for (const hash of recentlyRemovedC2Domains) {\n listSets.c2DomainBlocklist.delete(hash);\n }\n }\n\n return {\n c2DomainBlocklist: Array.from(listSets.c2DomainBlocklist),\n allowlist: Array.from(listSets.allowlist),\n blocklist: Array.from(listSets.blocklist),\n fuzzylist: Array.from(listSets.fuzzylist),\n blocklistPaths: newBlocklistPaths,\n version: listState.version,\n name: phishingListKeyNameMap[listKey],\n tolerance: listState.tolerance,\n lastUpdated: latestDiffTimestamp,\n };\n};\n\n/**\n * Validates the configuration object for the phishing detector.\n *\n * @param config - the configuration object to validate.\n * @throws an error if the configuration is invalid.\n */\nexport function validateConfig(\n config: unknown,\n): asserts config is PhishingListState {\n if (config === null || typeof config !== 'object') {\n throw new Error('Invalid config');\n }\n\n if ('tolerance' in config && !('fuzzylist' in config)) {\n throw new Error('Fuzzylist tolerance provided without fuzzylist');\n }\n\n if (\n 'name' in config &&\n (typeof config.name !== 'string' || config.name === '')\n ) {\n throw new Error(\"Invalid config parameter: 'name'\");\n }\n\n if (\n 'version' in config &&\n (!['number', 'string'].includes(typeof config.version) ||\n config.version === '')\n ) {\n throw new Error(\"Invalid config parameter: 'version'\");\n }\n}\n\n/**\n * Converts a domain string to a list of domain parts.\n *\n * @param domain - the domain string to convert.\n * @returns the list of domain parts.\n */\nexport const domainToParts = (domain: string) => {\n return domain.split('.').reverse();\n};\n\n/**\n * Converts a list of domain strings to a list of domain parts.\n *\n * @param list - the list of domain strings to convert.\n * @returns the list of domain parts.\n */\nexport const processDomainList = (list: string[]): string[][] => {\n return list.reduce<string[][]>((acc, domain) => {\n if (typeof domain !== 'string') {\n console.warn(`Invalid domain value in list: ${JSON.stringify(domain)}`);\n return acc;\n }\n acc.push(domainToParts(domain));\n return acc;\n }, []);\n};\n\n/**\n * Gets the default phishing detector configuration.\n *\n * @param override - the optional override for the configuration.\n * @param override.allowlist - the optional allowlist to override.\n * @param override.blocklist - the optional blocklist to override.\n * @param override.fuzzylist - the optional fuzzylist to override.\n * @param override.tolerance - the optional tolerance to override.\n * @returns the default phishing detector configuration.\n */\nexport const getDefaultPhishingDetectorConfig = ({\n allowlist = [],\n blocklist = [],\n fuzzylist = [],\n tolerance = DEFAULT_TOLERANCE,\n}: {\n allowlist?: string[];\n blocklist?: string[];\n fuzzylist?: string[];\n tolerance?: number;\n}): PhishingDetectorConfiguration => {\n return {\n allowlist: processDomainList(allowlist),\n // We can assume that blocklist is already separated into hostname-only entries\n // and hostname+path entries so we do not need to separate it again.\n blocklist: processDomainList(blocklist),\n fuzzylist: processDomainList(fuzzylist),\n tolerance,\n };\n};\n\n/**\n * Processes the configurations for the phishing detector, filtering out any invalid configs.\n *\n * @param configs - The configurations to process.\n * @returns An array of processed and valid configurations.\n */\nexport const processConfigs = (\n configs: PhishingDetectorList[] = [],\n): PhishingDetectorConfiguration[] => {\n return configs\n .filter((config) => {\n try {\n validateConfig(config);\n return true;\n } catch (error) {\n console.error(error);\n return false;\n }\n })\n .map((config) => ({\n ...config,\n ...getDefaultPhishingDetectorConfig(config),\n }));\n};\n\n/**\n * Converts a list of domain parts to a domain string.\n *\n * @param domainParts - the list of domain parts.\n * @returns the domain string.\n */\nexport const domainPartsToDomain = (domainParts: string[]) => {\n return domainParts.slice().reverse().join('.');\n};\n\n/**\n * Converts a list of domain parts to a fuzzy form.\n *\n * @param domainParts - the list of domain parts.\n * @returns the fuzzy form of the domain.\n */\nexport const domainPartsToFuzzyForm = (domainParts: string[]) => {\n return domainParts.slice(1).reverse().join('.');\n};\n\n/**\n * Matches the target parts, ignoring extra subdomains on source.\n *\n * @param source - the source domain parts.\n * @param list - the list of domain parts to match against.\n * @returns the parts for the first found matching entry.\n */\nexport const matchPartsAgainstList = (source: string[], list: string[][]) => {\n return list.find((target) => {\n // target domain has more parts than source, fail\n if (target.length > source.length) {\n return false;\n }\n // source matches target or (is deeper subdomain)\n return target.every((part, index) => source[index] === part);\n });\n};\n\n/**\n * Generate the SHA-256 hash of a hostname.\n *\n * @param hostname - The hostname to hash.\n * @returns The SHA-256 hash of the hostname.\n */\nexport const sha256Hash = (hostname: string): string => {\n const hashBuffer = sha256(new TextEncoder().encode(hostname.toLowerCase()));\n return bytesToHex(hashBuffer);\n};\n\n/**\n * Extracts the hostname from a URL.\n *\n * @param url - The URL to extract the hostname from.\n * @returns The hostname extracted from the URL, or null if the URL is invalid.\n */\nexport const getHostnameFromUrl = (url: string): string | null => {\n let hostname;\n try {\n hostname = new URL(url).hostname;\n // above will not throw if 'http://.' is passed. in fact, any string with a dot will pass.\n if (!hostname || hostname.split('.').join('') === '') {\n return null;\n }\n } catch {\n return null;\n }\n return hostname;\n};\n\n/**\n * getHostnameFromWebUrl returns the hostname from a web URL.\n * It returns the hostname and a boolean indicating if the hostname is valid.\n *\n * @param url - The web URL to extract the hostname from.\n * @returns A tuple containing the extracted hostname and a boolean indicating if the hostname is valid.\n * @example\n * getHostnameFromWebUrl('https://example.com') // Returns: ['example.com', true]\n * getHostnameFromWebUrl('example.com') // Returns: ['', false]\n * getHostnameFromWebUrl('https://') // Returns: ['', false]\n * getHostnameFromWebUrl('') // Returns: ['', false]\n */\nexport const getHostnameFromWebUrl = (url: string): [string, boolean] => {\n if (\n !url.toLowerCase().startsWith('http://') &&\n !url.toLowerCase().startsWith('https://')\n ) {\n return ['', false];\n }\n\n const hostname = getHostnameFromUrl(url);\n return [hostname || '', Boolean(hostname)];\n};\n\nexport const getPathnameFromUrl = (url: string): string => {\n try {\n const { pathname } = new URL(url);\n return pathname;\n } catch {\n return '';\n }\n};\n\n/**\n * Generates all possible parent domains up to a specified limit.\n *\n * @param sourceParts - The list of domain parts in normal order (e.g., ['evil', 'domain', 'co', 'uk']).\n * @param limit - The maximum number of parent domains to generate (default is 5).\n * @returns An array of parent domains starting from the base TLD to the most specific subdomain.\n * @example\n * generateParentDomains(['evil', 'domain', 'co', 'uk'], 5)\n * // Returns: ['co.uk', 'domain.co.uk', 'evil.domain.co.uk']\n *\n * generateParentDomains(['uk'], 5)\n * // Returns: ['uk']\n *\n * generateParentDomains(['sub', 'example', 'com'], 5)\n * // Returns: ['example.com', 'sub.example.com']\n */\nexport const generateParentDomains = (\n sourceParts: string[],\n limit = 5,\n): string[] => {\n const domains: string[] = [];\n\n if (sourceParts.length === 0) {\n return domains;\n }\n\n if (sourceParts.length === 1) {\n // Single-segment hostname (e.g., 'uk')\n domains.push(sourceParts[0].toLowerCase());\n } else {\n // Start with the base domain or TLD (last two labels, e.g., 'co.uk' or 'example.com')\n const baseDomain = sourceParts.slice(-2).join('.');\n domains.push(baseDomain.toLowerCase());\n\n // Iteratively add one subdomain level at a time, up to the specified limit\n for (\n let i = sourceParts.length - 3;\n i >= 0 && domains.length < limit;\n i--\n ) {\n const domain = sourceParts.slice(i).join('.');\n domains.push(domain.toLowerCase());\n }\n }\n\n return domains;\n};\n\n/**\n * Builds a cache key for a token scan result.\n *\n * @param chainId - The chain ID.\n * @param address - The token address.\n * @param caseSensitive - When `true`, the address is kept as-is (for chains\n * like Solana where addresses are case-sensitive). When `false` (default),\n * the address is lowercased (appropriate for EVM).\n * @returns The cache key.\n */\nexport const buildCacheKey = (\n chainId: string,\n address: string,\n caseSensitive = false,\n) => {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n return `${chainId.toLowerCase()}:${normalizedAddress}`;\n};\n\n/**\n * Determines whether a chain name is supported for token approval scanning.\n *\n * @param chain - The chain name to check.\n * @returns `true` if the chain is supported, `false` otherwise.\n */\nexport const isApprovalSupportedChain = (\n chain: string,\n): chain is ApprovalSupportedChain =>\n (APPROVAL_SUPPORTED_CHAINS as readonly string[]).includes(chain);\n\n/**\n * Resolves the chain name from a chain ID.\n *\n * @param chainId - The chain ID.\n * @param mapping - The mapping of chain IDs to chain names.\n * @returns The chain name.\n */\nexport const resolveChainName = (\n chainId: string,\n mapping = DEFAULT_CHAIN_ID_TO_NAME,\n): string | null => {\n return mapping[chainId.toLowerCase() as keyof typeof mapping] ?? null;\n};\n\n/**\n * Split tokens into cached results and tokens that need to be fetched.\n *\n * @param cache - Cache-like object with get method.\n * @param cache.get - Method to retrieve cached data by key.\n * @param chainId - The chain ID.\n * @param tokens - Array of token addresses.\n * @param caseSensitive - When `true`, token addresses are kept as-is (for\n * chains like Solana where addresses are case-sensitive). When `false`\n * (default), addresses are lowercased (appropriate for EVM).\n * @returns Object containing cached results and tokens to fetch.\n */\nexport const splitCacheHits = (\n cache: { get: (key: string) => TokenScanCacheData | undefined },\n chainId: string,\n tokens: string[],\n caseSensitive = false,\n): {\n cachedResults: Record<string, TokenScanResult>;\n tokensToFetch: string[];\n} => {\n const cachedResults: Record<string, TokenScanResult> = {};\n const tokensToFetch: string[] = [];\n\n for (const address of tokens) {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n const key = buildCacheKey(chainId, normalizedAddress, caseSensitive);\n const hit = cache.get(key);\n if (hit) {\n cachedResults[normalizedAddress] = {\n result_type: hit.result_type,\n chain: chainId,\n address: normalizedAddress,\n };\n } else {\n tokensToFetch.push(normalizedAddress);\n }\n }\n\n return { cachedResults, tokensToFetch };\n};\n"]}
|
package/dist/utils.d.cts
CHANGED
|
@@ -123,25 +123,6 @@ export declare const getHostnameFromUrl: (url: string) => string | null;
|
|
|
123
123
|
* getHostnameFromWebUrl('') // Returns: ['', false]
|
|
124
124
|
*/
|
|
125
125
|
export declare const getHostnameFromWebUrl: (url: string) => [string, boolean];
|
|
126
|
-
/**
|
|
127
|
-
* Hosts where PDS single-URL scans include the URL path (shared gateways / hosts where many sites
|
|
128
|
-
* share one origin). For all other hosts, only the hostname is sent.
|
|
129
|
-
*/
|
|
130
|
-
export declare const PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS: readonly ["ipfs.io", "dweb.link", "cf-ipfs.com", "cloudflare-ipfs.com", "irys.xyz", "sites.google.com"];
|
|
131
|
-
/**
|
|
132
|
-
* @param hostname - Lowercase normalization is applied for matching registered roots and subdomains.
|
|
133
|
-
* @returns Whether {@link getPhishingDetectionScanUrlParam} appends pathname for this hostname.
|
|
134
|
-
*/
|
|
135
|
-
export declare function isPhishingDetectionPathBasedHostname(hostname: string): boolean;
|
|
136
|
-
/**
|
|
137
|
-
* Builds the `url` query parameter for {@link PhishingController.scanUrl}. For hosts in
|
|
138
|
-
* {@link PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS} (and their subdomains), the value is hostname
|
|
139
|
-
* plus pathname, without protocol, query, or fragment. For all other hosts, only hostname is used.
|
|
140
|
-
*
|
|
141
|
-
* @param url - A web URL string (must use `http:` or `https:` — same rules as {@link getHostnameFromWebUrl}).
|
|
142
|
-
* @returns A tuple of `[scanUrlParam, ok]` where `ok` is false when the URL is not a valid web URL.
|
|
143
|
-
*/
|
|
144
|
-
export declare const getPhishingDetectionScanUrlParam: (url: string) => [scanUrlParam: string, ok: boolean];
|
|
145
126
|
export declare const getPathnameFromUrl: (url: string) => string;
|
|
146
127
|
/**
|
|
147
128
|
* Generates all possible parent domains up to a specified limit.
|
package/dist/utils.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,iCAA6B;AACvE,OAAO,EAAE,QAAQ,EAA0B,iCAA6B;AACxE,OAAO,KAAK,EACV,oBAAoB,EACpB,6BAA6B,EAC9B,+BAA2B;AAE5B,OAAO,KAAK,EAEV,kBAAkB,EAClB,eAAe,EAChB,oBAAgB;AAIjB;;;;GAIG;AACH,eAAO,MAAM,YAAY,QAAO,MAAuC,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,CAElE;AAkBD,eAAO,MAAM,4BAA4B,QAClC,MAAM,KACV;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,EAAE,CAAA;CAiB9C,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,UAAU,cACV,iBAAiB,kCAEnB,QAAQ,2BACO,MAAM,EAAE,6BACN,MAAM,EAAE,KACjC,iBAmEF,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,OAAO,GACd,OAAO,CAAC,MAAM,IAAI,iBAAiB,CAuBrC;AAED;;;;;GAKG;AACH,eAAO,MAAM,aAAa,WAAY,MAAM,aAE3C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,SAAU,MAAM,EAAE,KAAG,MAAM,EAAE,EAS1D,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,gCAAgC;;;;;MAUzC,6BASH,CAAC;AAEF;;;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;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,UAC5B,MAAM,uGAEmD,CAAC;AAEnE;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,YAClB,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAEd,MAAM,GAAG,IAEX,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,cAAc;eACL,MAAM,KAAK,kBAAkB,GAAG,SAAS;YACpD,MAAM,UACP,MAAM,EAAE;mBAGD,OAAO,MAAM,EAAE,eAAe,CAAC;mBAC/B,MAAM,EAAE;CAqBxB,CAAC"}
|
package/dist/utils.d.mts
CHANGED
|
@@ -123,25 +123,6 @@ export declare const getHostnameFromUrl: (url: string) => string | null;
|
|
|
123
123
|
* getHostnameFromWebUrl('') // Returns: ['', false]
|
|
124
124
|
*/
|
|
125
125
|
export declare const getHostnameFromWebUrl: (url: string) => [string, boolean];
|
|
126
|
-
/**
|
|
127
|
-
* Hosts where PDS single-URL scans include the URL path (shared gateways / hosts where many sites
|
|
128
|
-
* share one origin). For all other hosts, only the hostname is sent.
|
|
129
|
-
*/
|
|
130
|
-
export declare const PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS: readonly ["ipfs.io", "dweb.link", "cf-ipfs.com", "cloudflare-ipfs.com", "irys.xyz", "sites.google.com"];
|
|
131
|
-
/**
|
|
132
|
-
* @param hostname - Lowercase normalization is applied for matching registered roots and subdomains.
|
|
133
|
-
* @returns Whether {@link getPhishingDetectionScanUrlParam} appends pathname for this hostname.
|
|
134
|
-
*/
|
|
135
|
-
export declare function isPhishingDetectionPathBasedHostname(hostname: string): boolean;
|
|
136
|
-
/**
|
|
137
|
-
* Builds the `url` query parameter for {@link PhishingController.scanUrl}. For hosts in
|
|
138
|
-
* {@link PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS} (and their subdomains), the value is hostname
|
|
139
|
-
* plus pathname, without protocol, query, or fragment. For all other hosts, only hostname is used.
|
|
140
|
-
*
|
|
141
|
-
* @param url - A web URL string (must use `http:` or `https:` — same rules as {@link getHostnameFromWebUrl}).
|
|
142
|
-
* @returns A tuple of `[scanUrlParam, ok]` where `ok` is false when the URL is not a valid web URL.
|
|
143
|
-
*/
|
|
144
|
-
export declare const getPhishingDetectionScanUrlParam: (url: string) => [scanUrlParam: string, ok: boolean];
|
|
145
126
|
export declare const getPathnameFromUrl: (url: string) => string;
|
|
146
127
|
/**
|
|
147
128
|
* Generates all possible parent domains up to a specified limit.
|
package/dist/utils.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,iCAA6B;AACvE,OAAO,EAAE,QAAQ,EAA0B,iCAA6B;AACxE,OAAO,KAAK,EACV,oBAAoB,EACpB,6BAA6B,EAC9B,+BAA2B;AAE5B,OAAO,KAAK,EAEV,kBAAkB,EAClB,eAAe,EAChB,oBAAgB;AAIjB;;;;GAIG;AACH,eAAO,MAAM,YAAY,QAAO,MAAuC,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,CAElE;AAkBD,eAAO,MAAM,4BAA4B,QAClC,MAAM,KACV;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,EAAE,CAAA;CAiB9C,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,UAAU,cACV,iBAAiB,kCAEnB,QAAQ,2BACO,MAAM,EAAE,6BACN,MAAM,EAAE,KACjC,iBAmEF,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,OAAO,GACd,OAAO,CAAC,MAAM,IAAI,iBAAiB,CAuBrC;AAED;;;;;GAKG;AACH,eAAO,MAAM,aAAa,WAAY,MAAM,aAE3C,CAAC;AAEF;;;;;GAKG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H,eAAO,MAAM,qBAAqB,gBACnB,MAAM,EAAE,qBAEpB,MAAM,EA2BR,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,YACf,MAAM,WACN,MAAM,oCAKhB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,UAC5B,MAAM,uGAEmD,CAAC;AAEnE;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,YAClB,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAEd,MAAM,GAAG,IAEX,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,cAAc;eACL,MAAM,KAAK,kBAAkB,GAAG,SAAS;YACpD,MAAM,UACP,MAAM,EAAE;mBAGD,OAAO,MAAM,EAAE,eAAe,CAAC;mBAC/B,MAAM,EAAE;CAqBxB,CAAC"}
|
package/dist/utils.mjs
CHANGED
|
@@ -296,48 +296,6 @@ export const getHostnameFromWebUrl = (url) => {
|
|
|
296
296
|
const hostname = getHostnameFromUrl(url);
|
|
297
297
|
return [hostname || '', Boolean(hostname)];
|
|
298
298
|
};
|
|
299
|
-
/**
|
|
300
|
-
* Hosts where PDS single-URL scans include the URL path (shared gateways / hosts where many sites
|
|
301
|
-
* share one origin). For all other hosts, only the hostname is sent.
|
|
302
|
-
*/
|
|
303
|
-
export const PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS = [
|
|
304
|
-
'ipfs.io',
|
|
305
|
-
'dweb.link',
|
|
306
|
-
'cf-ipfs.com',
|
|
307
|
-
'cloudflare-ipfs.com',
|
|
308
|
-
'irys.xyz',
|
|
309
|
-
'sites.google.com',
|
|
310
|
-
];
|
|
311
|
-
/**
|
|
312
|
-
* @param hostname - Lowercase normalization is applied for matching registered roots and subdomains.
|
|
313
|
-
* @returns Whether {@link getPhishingDetectionScanUrlParam} appends pathname for this hostname.
|
|
314
|
-
*/
|
|
315
|
-
export function isPhishingDetectionPathBasedHostname(hostname) {
|
|
316
|
-
const normalizedHost = hostname.toLowerCase();
|
|
317
|
-
return PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS.some((root) => normalizedHost === root || normalizedHost.endsWith(`.${root}`));
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Builds the `url` query parameter for {@link PhishingController.scanUrl}. For hosts in
|
|
321
|
-
* {@link PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS} (and their subdomains), the value is hostname
|
|
322
|
-
* plus pathname, without protocol, query, or fragment. For all other hosts, only hostname is used.
|
|
323
|
-
*
|
|
324
|
-
* @param url - A web URL string (must use `http:` or `https:` — same rules as {@link getHostnameFromWebUrl}).
|
|
325
|
-
* @returns A tuple of `[scanUrlParam, ok]` where `ok` is false when the URL is not a valid web URL.
|
|
326
|
-
*/
|
|
327
|
-
export const getPhishingDetectionScanUrlParam = (url) => {
|
|
328
|
-
const [hostname, ok] = getHostnameFromWebUrl(url);
|
|
329
|
-
if (!ok) {
|
|
330
|
-
return ['', false];
|
|
331
|
-
}
|
|
332
|
-
if (!isPhishingDetectionPathBasedHostname(hostname)) {
|
|
333
|
-
return [hostname, true];
|
|
334
|
-
}
|
|
335
|
-
// `getHostnameFromWebUrl` already required a successful `new URL(url)` parse.
|
|
336
|
-
const { pathname } = new URL(url);
|
|
337
|
-
const pathSuffix = pathname === '/' ? '' : pathname;
|
|
338
|
-
const scanUrlParam = pathSuffix ? `${hostname}${pathSuffix}` : hostname;
|
|
339
|
-
return [scanUrlParam, true];
|
|
340
|
-
};
|
|
341
299
|
export const getPathnameFromUrl = (url) => {
|
|
342
300
|
try {
|
|
343
301
|
const { pathname } = new URL(url);
|
package/dist/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,4BAA4B;AACjD,OAAO,EAAE,MAAM,EAAE,qCAAqC;AAEtD,OAAO,EAAE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O,GAAG,wBAAwB,EACnB,EAAE;IACjB,OAAO,OAAO,CAAC,OAAO,CAAC,WAAW,EAA0B,CAAC,IAAI,IAAI,CAAC;AACxE,CAAC,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,KAA+D,EAC/D,OAAe,EACf,MAAgB,EAChB,aAAa,GAAG,KAAK,EAIrB,EAAE;IACF,MAAM,aAAa,GAAoC,EAAE,CAAC;IAC1D,MAAM,aAAa,GAAa,EAAE,CAAC;IAEnC,KAAK,MAAM,OAAO,IAAI,MAAM,EAAE,CAAC;QAC7B,MAAM,iBAAiB,GAAG,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;QAC1E,MAAM,GAAG,GAAG,aAAa,CAAC,OAAO,EAAE,iBAAiB,EAAE,aAAa,CAAC,CAAC;QACrE,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC3B,IAAI,GAAG,EAAE,CAAC;YACR,aAAa,CAAC,iBAAiB,CAAC,GAAG;gBACjC,WAAW,EAAE,GAAG,CAAC,WAAW;gBAC5B,KAAK,EAAE,OAAO;gBACd,OAAO,EAAE,iBAAiB;aAC3B,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,aAAa,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QACxC,CAAC;IACH,CAAC;IAED,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAC1C,CAAC,CAAC","sourcesContent":["import { bytesToHex } from '@noble/hashes/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\n\nimport { deleteFromTrie, insertToTrie, deepCopyPathTrie } from './PathTrie';\nimport type { Hotlist, PhishingListState } from './PhishingController';\nimport { ListKeys, phishingListKeyNameMap } from './PhishingController';\nimport type {\n PhishingDetectorList,\n PhishingDetectorConfiguration,\n} from './PhishingDetector';\nimport { APPROVAL_SUPPORTED_CHAINS, DEFAULT_CHAIN_ID_TO_NAME } from './types';\nimport type {\n ApprovalSupportedChain,\n TokenScanCacheData,\n TokenScanResult,\n} from './types';\n\nconst DEFAULT_TOLERANCE = 3;\n\n/**\n * Fetches current epoch time in seconds.\n *\n * @returns the Date.now() time in seconds instead of miliseconds. backend files rely on timestamps in seconds since epoch.\n */\nexport const fetchTimeNow = (): number => Math.round(Date.now() / 1000);\n\n/**\n * Rounds a Unix timestamp down to the nearest minute.\n *\n * @param unixTimestamp - The Unix timestamp to be rounded.\n * @returns The rounded Unix timestamp.\n */\nexport function roundToNearestMinute(unixTimestamp: number): number {\n return Math.floor(unixTimestamp / 60) * 60;\n}\n\n/**\n * Split a string into two pieces, using the first period as the delimiter.\n *\n * @param stringToSplit - The string to split.\n * @returns An array of length two containing the beginning and end of the string.\n */\nconst splitStringByPeriod = <Start extends string, End extends string>(\n stringToSplit: `${Start}.${End}`,\n): [Start, End] => {\n const periodIndex = stringToSplit.indexOf('.');\n return [\n stringToSplit.slice(0, periodIndex) as Start,\n stringToSplit.slice(periodIndex + 1) as End,\n ];\n};\n\nexport const getHostnameAndPathComponents = (\n url: string,\n): { hostname: string; pathComponents: string[] } => {\n const urlWithProtocol = url.startsWith('http') ? url : `https://${url}`;\n try {\n const { hostname, pathname } = new URL(urlWithProtocol);\n return {\n hostname: hostname.toLowerCase(),\n pathComponents: pathname\n .split('/')\n .filter(Boolean)\n .map((component) => decodeURIComponent(component)),\n };\n } catch {\n return {\n hostname: '',\n pathComponents: [],\n };\n }\n};\n\n/**\n * Determines which diffs are applicable to the listState, then applies those diffs.\n *\n * @param listState - the stalelist or the existing liststate that diffs will be applied to.\n * @param hotlistDiffs - the diffs to apply to the listState if valid.\n * @param listKey - the key associated with the input/output phishing list state.\n * @param recentlyAddedC2Domains - list of hashed C2 domains to add to the local c2 domain blocklist\n * @param recentlyRemovedC2Domains - list of hashed C2 domains to remove from the local c2 domain blocklist\n * @returns the new list state\n */\nexport const applyDiffs = (\n listState: PhishingListState,\n hotlistDiffs: Hotlist,\n listKey: ListKeys,\n recentlyAddedC2Domains: string[] = [],\n recentlyRemovedC2Domains: string[] = [],\n): PhishingListState => {\n // filter to remove diffs that were added before the lastUpdate time.\n // filter to remove diffs that aren't applicable to the specified list (by listKey).\n const diffsToApply = hotlistDiffs.filter(\n ({ timestamp, targetList }) =>\n timestamp > listState.lastUpdated &&\n splitStringByPeriod(targetList)[0] === listKey,\n );\n\n // the reason behind using latestDiffTimestamp as the lastUpdated time\n // is so that we can benefit server-side from memoization due to end client's\n // `GET /v1/diffSince/:timestamp` requests lining up with\n // our periodic updates (which create diffs at specific timestamps).\n let latestDiffTimestamp = listState.lastUpdated;\n\n const listSets = {\n allowlist: new Set(listState.allowlist),\n blocklist: new Set(listState.blocklist),\n fuzzylist: new Set(listState.fuzzylist),\n c2DomainBlocklist: new Set(listState.c2DomainBlocklist),\n };\n\n // deep copy of blocklistPaths to avoid mutating the original\n const newBlocklistPaths = deepCopyPathTrie(listState.blocklistPaths);\n\n for (const { isRemoval, targetList, url, timestamp } of diffsToApply) {\n const targetListType = splitStringByPeriod(targetList)[1];\n if (timestamp > latestDiffTimestamp) {\n latestDiffTimestamp = timestamp;\n }\n\n if (isRemoval) {\n if (targetListType === 'blocklistPaths') {\n deleteFromTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].delete(url);\n }\n continue;\n }\n\n if (targetListType === 'blocklistPaths') {\n insertToTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].add(url);\n }\n }\n\n if (listKey === ListKeys.EthPhishingDetectConfig) {\n for (const hash of recentlyAddedC2Domains) {\n listSets.c2DomainBlocklist.add(hash);\n }\n for (const hash of recentlyRemovedC2Domains) {\n listSets.c2DomainBlocklist.delete(hash);\n }\n }\n\n return {\n c2DomainBlocklist: Array.from(listSets.c2DomainBlocklist),\n allowlist: Array.from(listSets.allowlist),\n blocklist: Array.from(listSets.blocklist),\n fuzzylist: Array.from(listSets.fuzzylist),\n blocklistPaths: newBlocklistPaths,\n version: listState.version,\n name: phishingListKeyNameMap[listKey],\n tolerance: listState.tolerance,\n lastUpdated: latestDiffTimestamp,\n };\n};\n\n/**\n * Validates the configuration object for the phishing detector.\n *\n * @param config - the configuration object to validate.\n * @throws an error if the configuration is invalid.\n */\nexport function validateConfig(\n config: unknown,\n): asserts config is PhishingListState {\n if (config === null || typeof config !== 'object') {\n throw new Error('Invalid config');\n }\n\n if ('tolerance' in config && !('fuzzylist' in config)) {\n throw new Error('Fuzzylist tolerance provided without fuzzylist');\n }\n\n if (\n 'name' in config &&\n (typeof config.name !== 'string' || config.name === '')\n ) {\n throw new Error(\"Invalid config parameter: 'name'\");\n }\n\n if (\n 'version' in config &&\n (!['number', 'string'].includes(typeof config.version) ||\n config.version === '')\n ) {\n throw new Error(\"Invalid config parameter: 'version'\");\n }\n}\n\n/**\n * Converts a domain string to a list of domain parts.\n *\n * @param domain - the domain string to convert.\n * @returns the list of domain parts.\n */\nexport const domainToParts = (domain: string) => {\n return domain.split('.').reverse();\n};\n\n/**\n * Converts a list of domain strings to a list of domain parts.\n *\n * @param list - the list of domain strings to convert.\n * @returns the list of domain parts.\n */\nexport const processDomainList = (list: string[]): string[][] => {\n return list.reduce<string[][]>((acc, domain) => {\n if (typeof domain !== 'string') {\n console.warn(`Invalid domain value in list: ${JSON.stringify(domain)}`);\n return acc;\n }\n acc.push(domainToParts(domain));\n return acc;\n }, []);\n};\n\n/**\n * Gets the default phishing detector configuration.\n *\n * @param override - the optional override for the configuration.\n * @param override.allowlist - the optional allowlist to override.\n * @param override.blocklist - the optional blocklist to override.\n * @param override.fuzzylist - the optional fuzzylist to override.\n * @param override.tolerance - the optional tolerance to override.\n * @returns the default phishing detector configuration.\n */\nexport const getDefaultPhishingDetectorConfig = ({\n allowlist = [],\n blocklist = [],\n fuzzylist = [],\n tolerance = DEFAULT_TOLERANCE,\n}: {\n allowlist?: string[];\n blocklist?: string[];\n fuzzylist?: string[];\n tolerance?: number;\n}): PhishingDetectorConfiguration => {\n return {\n allowlist: processDomainList(allowlist),\n // We can assume that blocklist is already separated into hostname-only entries\n // and hostname+path entries so we do not need to separate it again.\n blocklist: processDomainList(blocklist),\n fuzzylist: processDomainList(fuzzylist),\n tolerance,\n };\n};\n\n/**\n * Processes the configurations for the phishing detector, filtering out any invalid configs.\n *\n * @param configs - The configurations to process.\n * @returns An array of processed and valid configurations.\n */\nexport const processConfigs = (\n configs: PhishingDetectorList[] = [],\n): PhishingDetectorConfiguration[] => {\n return configs\n .filter((config) => {\n try {\n validateConfig(config);\n return true;\n } catch (error) {\n console.error(error);\n return false;\n }\n })\n .map((config) => ({\n ...config,\n ...getDefaultPhishingDetectorConfig(config),\n }));\n};\n\n/**\n * Converts a list of domain parts to a domain string.\n *\n * @param domainParts - the list of domain parts.\n * @returns the domain string.\n */\nexport const domainPartsToDomain = (domainParts: string[]) => {\n return domainParts.slice().reverse().join('.');\n};\n\n/**\n * Converts a list of domain parts to a fuzzy form.\n *\n * @param domainParts - the list of domain parts.\n * @returns the fuzzy form of the domain.\n */\nexport const domainPartsToFuzzyForm = (domainParts: string[]) => {\n return domainParts.slice(1).reverse().join('.');\n};\n\n/**\n * Matches the target parts, ignoring extra subdomains on source.\n *\n * @param source - the source domain parts.\n * @param list - the list of domain parts to match against.\n * @returns the parts for the first found matching entry.\n */\nexport const matchPartsAgainstList = (source: string[], list: string[][]) => {\n return list.find((target) => {\n // target domain has more parts than source, fail\n if (target.length > source.length) {\n return false;\n }\n // source matches target or (is deeper subdomain)\n return target.every((part, index) => source[index] === part);\n });\n};\n\n/**\n * Generate the SHA-256 hash of a hostname.\n *\n * @param hostname - The hostname to hash.\n * @returns The SHA-256 hash of the hostname.\n */\nexport const sha256Hash = (hostname: string): string => {\n const hashBuffer = sha256(new TextEncoder().encode(hostname.toLowerCase()));\n return bytesToHex(hashBuffer);\n};\n\n/**\n * Extracts the hostname from a URL.\n *\n * @param url - The URL to extract the hostname from.\n * @returns The hostname extracted from the URL, or null if the URL is invalid.\n */\nexport const getHostnameFromUrl = (url: string): string | null => {\n let hostname;\n try {\n hostname = new URL(url).hostname;\n // above will not throw if 'http://.' is passed. in fact, any string with a dot will pass.\n if (!hostname || hostname.split('.').join('') === '') {\n return null;\n }\n } catch {\n return null;\n }\n return hostname;\n};\n\n/**\n * getHostnameFromWebUrl returns the hostname from a web URL.\n * It returns the hostname and a boolean indicating if the hostname is valid.\n *\n * @param url - The web URL to extract the hostname from.\n * @returns A tuple containing the extracted hostname and a boolean indicating if the hostname is valid.\n * @example\n * getHostnameFromWebUrl('https://example.com') // Returns: ['example.com', true]\n * getHostnameFromWebUrl('example.com') // Returns: ['', false]\n * getHostnameFromWebUrl('https://') // Returns: ['', false]\n * getHostnameFromWebUrl('') // Returns: ['', false]\n */\nexport const getHostnameFromWebUrl = (url: string): [string, boolean] => {\n if (\n !url.toLowerCase().startsWith('http://') &&\n !url.toLowerCase().startsWith('https://')\n ) {\n return ['', false];\n }\n\n const hostname = getHostnameFromUrl(url);\n return [hostname || '', Boolean(hostname)];\n};\n\n/**\n * Hosts where PDS single-URL scans include the URL path (shared gateways / hosts where many sites\n * share one origin). For all other hosts, only the hostname is sent.\n */\nexport const PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS = [\n 'ipfs.io',\n 'dweb.link',\n 'cf-ipfs.com',\n 'cloudflare-ipfs.com',\n 'irys.xyz',\n 'sites.google.com',\n] as const;\n\n/**\n * @param hostname - Lowercase normalization is applied for matching registered roots and subdomains.\n * @returns Whether {@link getPhishingDetectionScanUrlParam} appends pathname for this hostname.\n */\nexport function isPhishingDetectionPathBasedHostname(\n hostname: string,\n): boolean {\n const normalizedHost = hostname.toLowerCase();\n return PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS.some(\n (root) => normalizedHost === root || normalizedHost.endsWith(`.${root}`),\n );\n}\n\n/**\n * Builds the `url` query parameter for {@link PhishingController.scanUrl}. For hosts in\n * {@link PHISHING_DETECTION_PATH_BASED_ROOT_DOMAINS} (and their subdomains), the value is hostname\n * plus pathname, without protocol, query, or fragment. For all other hosts, only hostname is used.\n *\n * @param url - A web URL string (must use `http:` or `https:` — same rules as {@link getHostnameFromWebUrl}).\n * @returns A tuple of `[scanUrlParam, ok]` where `ok` is false when the URL is not a valid web URL.\n */\nexport const getPhishingDetectionScanUrlParam = (\n url: string,\n): [scanUrlParam: string, ok: boolean] => {\n const [hostname, ok] = getHostnameFromWebUrl(url);\n if (!ok) {\n return ['', false];\n }\n\n if (!isPhishingDetectionPathBasedHostname(hostname)) {\n return [hostname, true];\n }\n\n // `getHostnameFromWebUrl` already required a successful `new URL(url)` parse.\n const { pathname } = new URL(url);\n const pathSuffix = pathname === '/' ? '' : pathname;\n const scanUrlParam = pathSuffix ? `${hostname}${pathSuffix}` : hostname;\n\n return [scanUrlParam, true];\n};\n\nexport const getPathnameFromUrl = (url: string): string => {\n try {\n const { pathname } = new URL(url);\n return pathname;\n } catch {\n return '';\n }\n};\n\n/**\n * Generates all possible parent domains up to a specified limit.\n *\n * @param sourceParts - The list of domain parts in normal order (e.g., ['evil', 'domain', 'co', 'uk']).\n * @param limit - The maximum number of parent domains to generate (default is 5).\n * @returns An array of parent domains starting from the base TLD to the most specific subdomain.\n * @example\n * generateParentDomains(['evil', 'domain', 'co', 'uk'], 5)\n * // Returns: ['co.uk', 'domain.co.uk', 'evil.domain.co.uk']\n *\n * generateParentDomains(['uk'], 5)\n * // Returns: ['uk']\n *\n * generateParentDomains(['sub', 'example', 'com'], 5)\n * // Returns: ['example.com', 'sub.example.com']\n */\nexport const generateParentDomains = (\n sourceParts: string[],\n limit = 5,\n): string[] => {\n const domains: string[] = [];\n\n if (sourceParts.length === 0) {\n return domains;\n }\n\n if (sourceParts.length === 1) {\n // Single-segment hostname (e.g., 'uk')\n domains.push(sourceParts[0].toLowerCase());\n } else {\n // Start with the base domain or TLD (last two labels, e.g., 'co.uk' or 'example.com')\n const baseDomain = sourceParts.slice(-2).join('.');\n domains.push(baseDomain.toLowerCase());\n\n // Iteratively add one subdomain level at a time, up to the specified limit\n for (\n let i = sourceParts.length - 3;\n i >= 0 && domains.length < limit;\n i--\n ) {\n const domain = sourceParts.slice(i).join('.');\n domains.push(domain.toLowerCase());\n }\n }\n\n return domains;\n};\n\n/**\n * Builds a cache key for a token scan result.\n *\n * @param chainId - The chain ID.\n * @param address - The token address.\n * @param caseSensitive - When `true`, the address is kept as-is (for chains\n * like Solana where addresses are case-sensitive). When `false` (default),\n * the address is lowercased (appropriate for EVM).\n * @returns The cache key.\n */\nexport const buildCacheKey = (\n chainId: string,\n address: string,\n caseSensitive = false,\n) => {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n return `${chainId.toLowerCase()}:${normalizedAddress}`;\n};\n\n/**\n * Determines whether a chain name is supported for token approval scanning.\n *\n * @param chain - The chain name to check.\n * @returns `true` if the chain is supported, `false` otherwise.\n */\nexport const isApprovalSupportedChain = (\n chain: string,\n): chain is ApprovalSupportedChain =>\n (APPROVAL_SUPPORTED_CHAINS as readonly string[]).includes(chain);\n\n/**\n * Resolves the chain name from a chain ID.\n *\n * @param chainId - The chain ID.\n * @param mapping - The mapping of chain IDs to chain names.\n * @returns The chain name.\n */\nexport const resolveChainName = (\n chainId: string,\n mapping = DEFAULT_CHAIN_ID_TO_NAME,\n): string | null => {\n return mapping[chainId.toLowerCase() as keyof typeof mapping] ?? null;\n};\n\n/**\n * Split tokens into cached results and tokens that need to be fetched.\n *\n * @param cache - Cache-like object with get method.\n * @param cache.get - Method to retrieve cached data by key.\n * @param chainId - The chain ID.\n * @param tokens - Array of token addresses.\n * @param caseSensitive - When `true`, token addresses are kept as-is (for\n * chains like Solana where addresses are case-sensitive). When `false`\n * (default), addresses are lowercased (appropriate for EVM).\n * @returns Object containing cached results and tokens to fetch.\n */\nexport const splitCacheHits = (\n cache: { get: (key: string) => TokenScanCacheData | undefined },\n chainId: string,\n tokens: string[],\n caseSensitive = false,\n): {\n cachedResults: Record<string, TokenScanResult>;\n tokensToFetch: string[];\n} => {\n const cachedResults: Record<string, TokenScanResult> = {};\n const tokensToFetch: string[] = [];\n\n for (const address of tokens) {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n const key = buildCacheKey(chainId, normalizedAddress, caseSensitive);\n const hit = cache.get(key);\n if (hit) {\n cachedResults[normalizedAddress] = {\n result_type: hit.result_type,\n chain: chainId,\n address: normalizedAddress,\n };\n } else {\n tokensToFetch.push(normalizedAddress);\n }\n }\n\n return { cachedResults, tokensToFetch };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,4BAA4B;AACjD,OAAO,EAAE,MAAM,EAAE,qCAAqC;AAEtD,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,gBAAgB,EAAE,uBAAmB;AAE5E,OAAO,EAAE,QAAQ,EAAE,sBAAsB,EAAE,iCAA6B;AAKxE,OAAO,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,oBAAgB;AAO9E,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAE5B;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,GAAW,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;AAExE;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,aAAqB;IACxD,OAAO,IAAI,CAAC,KAAK,CAAC,aAAa,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;AAC7C,CAAC;AAED;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,CAC1B,aAAgC,EAClB,EAAE;IAChB,MAAM,WAAW,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC/C,OAAO;QACL,aAAa,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,CAAU;QAC5C,aAAa,CAAC,KAAK,CAAC,WAAW,GAAG,CAAC,CAAQ;KAC5C,CAAC;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,iBAAiB,CAAC,GAAG;gBACjC,WAAW,EAAE,GAAG,CAAC,WAAW;gBAC5B,KAAK,EAAE,OAAO;gBACd,OAAO,EAAE,iBAAiB;aAC3B,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,aAAa,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QACxC,CAAC;IACH,CAAC;IAED,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAC1C,CAAC,CAAC","sourcesContent":["import { bytesToHex } from '@noble/hashes/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\n\nimport { deleteFromTrie, insertToTrie, deepCopyPathTrie } from './PathTrie';\nimport type { Hotlist, PhishingListState } from './PhishingController';\nimport { ListKeys, phishingListKeyNameMap } from './PhishingController';\nimport type {\n PhishingDetectorList,\n PhishingDetectorConfiguration,\n} from './PhishingDetector';\nimport { APPROVAL_SUPPORTED_CHAINS, DEFAULT_CHAIN_ID_TO_NAME } from './types';\nimport type {\n ApprovalSupportedChain,\n TokenScanCacheData,\n TokenScanResult,\n} from './types';\n\nconst DEFAULT_TOLERANCE = 3;\n\n/**\n * Fetches current epoch time in seconds.\n *\n * @returns the Date.now() time in seconds instead of miliseconds. backend files rely on timestamps in seconds since epoch.\n */\nexport const fetchTimeNow = (): number => Math.round(Date.now() / 1000);\n\n/**\n * Rounds a Unix timestamp down to the nearest minute.\n *\n * @param unixTimestamp - The Unix timestamp to be rounded.\n * @returns The rounded Unix timestamp.\n */\nexport function roundToNearestMinute(unixTimestamp: number): number {\n return Math.floor(unixTimestamp / 60) * 60;\n}\n\n/**\n * Split a string into two pieces, using the first period as the delimiter.\n *\n * @param stringToSplit - The string to split.\n * @returns An array of length two containing the beginning and end of the string.\n */\nconst splitStringByPeriod = <Start extends string, End extends string>(\n stringToSplit: `${Start}.${End}`,\n): [Start, End] => {\n const periodIndex = stringToSplit.indexOf('.');\n return [\n stringToSplit.slice(0, periodIndex) as Start,\n stringToSplit.slice(periodIndex + 1) as End,\n ];\n};\n\nexport const getHostnameAndPathComponents = (\n url: string,\n): { hostname: string; pathComponents: string[] } => {\n const urlWithProtocol = url.startsWith('http') ? url : `https://${url}`;\n try {\n const { hostname, pathname } = new URL(urlWithProtocol);\n return {\n hostname: hostname.toLowerCase(),\n pathComponents: pathname\n .split('/')\n .filter(Boolean)\n .map((component) => decodeURIComponent(component)),\n };\n } catch {\n return {\n hostname: '',\n pathComponents: [],\n };\n }\n};\n\n/**\n * Determines which diffs are applicable to the listState, then applies those diffs.\n *\n * @param listState - the stalelist or the existing liststate that diffs will be applied to.\n * @param hotlistDiffs - the diffs to apply to the listState if valid.\n * @param listKey - the key associated with the input/output phishing list state.\n * @param recentlyAddedC2Domains - list of hashed C2 domains to add to the local c2 domain blocklist\n * @param recentlyRemovedC2Domains - list of hashed C2 domains to remove from the local c2 domain blocklist\n * @returns the new list state\n */\nexport const applyDiffs = (\n listState: PhishingListState,\n hotlistDiffs: Hotlist,\n listKey: ListKeys,\n recentlyAddedC2Domains: string[] = [],\n recentlyRemovedC2Domains: string[] = [],\n): PhishingListState => {\n // filter to remove diffs that were added before the lastUpdate time.\n // filter to remove diffs that aren't applicable to the specified list (by listKey).\n const diffsToApply = hotlistDiffs.filter(\n ({ timestamp, targetList }) =>\n timestamp > listState.lastUpdated &&\n splitStringByPeriod(targetList)[0] === listKey,\n );\n\n // the reason behind using latestDiffTimestamp as the lastUpdated time\n // is so that we can benefit server-side from memoization due to end client's\n // `GET /v1/diffSince/:timestamp` requests lining up with\n // our periodic updates (which create diffs at specific timestamps).\n let latestDiffTimestamp = listState.lastUpdated;\n\n const listSets = {\n allowlist: new Set(listState.allowlist),\n blocklist: new Set(listState.blocklist),\n fuzzylist: new Set(listState.fuzzylist),\n c2DomainBlocklist: new Set(listState.c2DomainBlocklist),\n };\n\n // deep copy of blocklistPaths to avoid mutating the original\n const newBlocklistPaths = deepCopyPathTrie(listState.blocklistPaths);\n\n for (const { isRemoval, targetList, url, timestamp } of diffsToApply) {\n const targetListType = splitStringByPeriod(targetList)[1];\n if (timestamp > latestDiffTimestamp) {\n latestDiffTimestamp = timestamp;\n }\n\n if (isRemoval) {\n if (targetListType === 'blocklistPaths') {\n deleteFromTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].delete(url);\n }\n continue;\n }\n\n if (targetListType === 'blocklistPaths') {\n insertToTrie(url, newBlocklistPaths);\n } else {\n listSets[targetListType].add(url);\n }\n }\n\n if (listKey === ListKeys.EthPhishingDetectConfig) {\n for (const hash of recentlyAddedC2Domains) {\n listSets.c2DomainBlocklist.add(hash);\n }\n for (const hash of recentlyRemovedC2Domains) {\n listSets.c2DomainBlocklist.delete(hash);\n }\n }\n\n return {\n c2DomainBlocklist: Array.from(listSets.c2DomainBlocklist),\n allowlist: Array.from(listSets.allowlist),\n blocklist: Array.from(listSets.blocklist),\n fuzzylist: Array.from(listSets.fuzzylist),\n blocklistPaths: newBlocklistPaths,\n version: listState.version,\n name: phishingListKeyNameMap[listKey],\n tolerance: listState.tolerance,\n lastUpdated: latestDiffTimestamp,\n };\n};\n\n/**\n * Validates the configuration object for the phishing detector.\n *\n * @param config - the configuration object to validate.\n * @throws an error if the configuration is invalid.\n */\nexport function validateConfig(\n config: unknown,\n): asserts config is PhishingListState {\n if (config === null || typeof config !== 'object') {\n throw new Error('Invalid config');\n }\n\n if ('tolerance' in config && !('fuzzylist' in config)) {\n throw new Error('Fuzzylist tolerance provided without fuzzylist');\n }\n\n if (\n 'name' in config &&\n (typeof config.name !== 'string' || config.name === '')\n ) {\n throw new Error(\"Invalid config parameter: 'name'\");\n }\n\n if (\n 'version' in config &&\n (!['number', 'string'].includes(typeof config.version) ||\n config.version === '')\n ) {\n throw new Error(\"Invalid config parameter: 'version'\");\n }\n}\n\n/**\n * Converts a domain string to a list of domain parts.\n *\n * @param domain - the domain string to convert.\n * @returns the list of domain parts.\n */\nexport const domainToParts = (domain: string) => {\n return domain.split('.').reverse();\n};\n\n/**\n * Converts a list of domain strings to a list of domain parts.\n *\n * @param list - the list of domain strings to convert.\n * @returns the list of domain parts.\n */\nexport const processDomainList = (list: string[]): string[][] => {\n return list.reduce<string[][]>((acc, domain) => {\n if (typeof domain !== 'string') {\n console.warn(`Invalid domain value in list: ${JSON.stringify(domain)}`);\n return acc;\n }\n acc.push(domainToParts(domain));\n return acc;\n }, []);\n};\n\n/**\n * Gets the default phishing detector configuration.\n *\n * @param override - the optional override for the configuration.\n * @param override.allowlist - the optional allowlist to override.\n * @param override.blocklist - the optional blocklist to override.\n * @param override.fuzzylist - the optional fuzzylist to override.\n * @param override.tolerance - the optional tolerance to override.\n * @returns the default phishing detector configuration.\n */\nexport const getDefaultPhishingDetectorConfig = ({\n allowlist = [],\n blocklist = [],\n fuzzylist = [],\n tolerance = DEFAULT_TOLERANCE,\n}: {\n allowlist?: string[];\n blocklist?: string[];\n fuzzylist?: string[];\n tolerance?: number;\n}): PhishingDetectorConfiguration => {\n return {\n allowlist: processDomainList(allowlist),\n // We can assume that blocklist is already separated into hostname-only entries\n // and hostname+path entries so we do not need to separate it again.\n blocklist: processDomainList(blocklist),\n fuzzylist: processDomainList(fuzzylist),\n tolerance,\n };\n};\n\n/**\n * Processes the configurations for the phishing detector, filtering out any invalid configs.\n *\n * @param configs - The configurations to process.\n * @returns An array of processed and valid configurations.\n */\nexport const processConfigs = (\n configs: PhishingDetectorList[] = [],\n): PhishingDetectorConfiguration[] => {\n return configs\n .filter((config) => {\n try {\n validateConfig(config);\n return true;\n } catch (error) {\n console.error(error);\n return false;\n }\n })\n .map((config) => ({\n ...config,\n ...getDefaultPhishingDetectorConfig(config),\n }));\n};\n\n/**\n * Converts a list of domain parts to a domain string.\n *\n * @param domainParts - the list of domain parts.\n * @returns the domain string.\n */\nexport const domainPartsToDomain = (domainParts: string[]) => {\n return domainParts.slice().reverse().join('.');\n};\n\n/**\n * Converts a list of domain parts to a fuzzy form.\n *\n * @param domainParts - the list of domain parts.\n * @returns the fuzzy form of the domain.\n */\nexport const domainPartsToFuzzyForm = (domainParts: string[]) => {\n return domainParts.slice(1).reverse().join('.');\n};\n\n/**\n * Matches the target parts, ignoring extra subdomains on source.\n *\n * @param source - the source domain parts.\n * @param list - the list of domain parts to match against.\n * @returns the parts for the first found matching entry.\n */\nexport const matchPartsAgainstList = (source: string[], list: string[][]) => {\n return list.find((target) => {\n // target domain has more parts than source, fail\n if (target.length > source.length) {\n return false;\n }\n // source matches target or (is deeper subdomain)\n return target.every((part, index) => source[index] === part);\n });\n};\n\n/**\n * Generate the SHA-256 hash of a hostname.\n *\n * @param hostname - The hostname to hash.\n * @returns The SHA-256 hash of the hostname.\n */\nexport const sha256Hash = (hostname: string): string => {\n const hashBuffer = sha256(new TextEncoder().encode(hostname.toLowerCase()));\n return bytesToHex(hashBuffer);\n};\n\n/**\n * Extracts the hostname from a URL.\n *\n * @param url - The URL to extract the hostname from.\n * @returns The hostname extracted from the URL, or null if the URL is invalid.\n */\nexport const getHostnameFromUrl = (url: string): string | null => {\n let hostname;\n try {\n hostname = new URL(url).hostname;\n // above will not throw if 'http://.' is passed. in fact, any string with a dot will pass.\n if (!hostname || hostname.split('.').join('') === '') {\n return null;\n }\n } catch {\n return null;\n }\n return hostname;\n};\n\n/**\n * getHostnameFromWebUrl returns the hostname from a web URL.\n * It returns the hostname and a boolean indicating if the hostname is valid.\n *\n * @param url - The web URL to extract the hostname from.\n * @returns A tuple containing the extracted hostname and a boolean indicating if the hostname is valid.\n * @example\n * getHostnameFromWebUrl('https://example.com') // Returns: ['example.com', true]\n * getHostnameFromWebUrl('example.com') // Returns: ['', false]\n * getHostnameFromWebUrl('https://') // Returns: ['', false]\n * getHostnameFromWebUrl('') // Returns: ['', false]\n */\nexport const getHostnameFromWebUrl = (url: string): [string, boolean] => {\n if (\n !url.toLowerCase().startsWith('http://') &&\n !url.toLowerCase().startsWith('https://')\n ) {\n return ['', false];\n }\n\n const hostname = getHostnameFromUrl(url);\n return [hostname || '', Boolean(hostname)];\n};\n\nexport const getPathnameFromUrl = (url: string): string => {\n try {\n const { pathname } = new URL(url);\n return pathname;\n } catch {\n return '';\n }\n};\n\n/**\n * Generates all possible parent domains up to a specified limit.\n *\n * @param sourceParts - The list of domain parts in normal order (e.g., ['evil', 'domain', 'co', 'uk']).\n * @param limit - The maximum number of parent domains to generate (default is 5).\n * @returns An array of parent domains starting from the base TLD to the most specific subdomain.\n * @example\n * generateParentDomains(['evil', 'domain', 'co', 'uk'], 5)\n * // Returns: ['co.uk', 'domain.co.uk', 'evil.domain.co.uk']\n *\n * generateParentDomains(['uk'], 5)\n * // Returns: ['uk']\n *\n * generateParentDomains(['sub', 'example', 'com'], 5)\n * // Returns: ['example.com', 'sub.example.com']\n */\nexport const generateParentDomains = (\n sourceParts: string[],\n limit = 5,\n): string[] => {\n const domains: string[] = [];\n\n if (sourceParts.length === 0) {\n return domains;\n }\n\n if (sourceParts.length === 1) {\n // Single-segment hostname (e.g., 'uk')\n domains.push(sourceParts[0].toLowerCase());\n } else {\n // Start with the base domain or TLD (last two labels, e.g., 'co.uk' or 'example.com')\n const baseDomain = sourceParts.slice(-2).join('.');\n domains.push(baseDomain.toLowerCase());\n\n // Iteratively add one subdomain level at a time, up to the specified limit\n for (\n let i = sourceParts.length - 3;\n i >= 0 && domains.length < limit;\n i--\n ) {\n const domain = sourceParts.slice(i).join('.');\n domains.push(domain.toLowerCase());\n }\n }\n\n return domains;\n};\n\n/**\n * Builds a cache key for a token scan result.\n *\n * @param chainId - The chain ID.\n * @param address - The token address.\n * @param caseSensitive - When `true`, the address is kept as-is (for chains\n * like Solana where addresses are case-sensitive). When `false` (default),\n * the address is lowercased (appropriate for EVM).\n * @returns The cache key.\n */\nexport const buildCacheKey = (\n chainId: string,\n address: string,\n caseSensitive = false,\n) => {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n return `${chainId.toLowerCase()}:${normalizedAddress}`;\n};\n\n/**\n * Determines whether a chain name is supported for token approval scanning.\n *\n * @param chain - The chain name to check.\n * @returns `true` if the chain is supported, `false` otherwise.\n */\nexport const isApprovalSupportedChain = (\n chain: string,\n): chain is ApprovalSupportedChain =>\n (APPROVAL_SUPPORTED_CHAINS as readonly string[]).includes(chain);\n\n/**\n * Resolves the chain name from a chain ID.\n *\n * @param chainId - The chain ID.\n * @param mapping - The mapping of chain IDs to chain names.\n * @returns The chain name.\n */\nexport const resolveChainName = (\n chainId: string,\n mapping = DEFAULT_CHAIN_ID_TO_NAME,\n): string | null => {\n return mapping[chainId.toLowerCase() as keyof typeof mapping] ?? null;\n};\n\n/**\n * Split tokens into cached results and tokens that need to be fetched.\n *\n * @param cache - Cache-like object with get method.\n * @param cache.get - Method to retrieve cached data by key.\n * @param chainId - The chain ID.\n * @param tokens - Array of token addresses.\n * @param caseSensitive - When `true`, token addresses are kept as-is (for\n * chains like Solana where addresses are case-sensitive). When `false`\n * (default), addresses are lowercased (appropriate for EVM).\n * @returns Object containing cached results and tokens to fetch.\n */\nexport const splitCacheHits = (\n cache: { get: (key: string) => TokenScanCacheData | undefined },\n chainId: string,\n tokens: string[],\n caseSensitive = false,\n): {\n cachedResults: Record<string, TokenScanResult>;\n tokensToFetch: string[];\n} => {\n const cachedResults: Record<string, TokenScanResult> = {};\n const tokensToFetch: string[] = [];\n\n for (const address of tokens) {\n const normalizedAddress = caseSensitive ? address : address.toLowerCase();\n const key = buildCacheKey(chainId, normalizedAddress, caseSensitive);\n const hit = cache.get(key);\n if (hit) {\n cachedResults[normalizedAddress] = {\n result_type: hit.result_type,\n chain: chainId,\n address: normalizedAddress,\n };\n } else {\n tokensToFetch.push(normalizedAddress);\n }\n }\n\n return { cachedResults, tokensToFetch };\n};\n"]}
|
package/package.json
CHANGED