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ACV,GAAG,CACF,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,OAAO,EAAE,CACxE;SACA,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Struct, StructError } from '@metamask/superstruct';\nimport { validate } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\nimport { isHexAddress } from '@metamask/utils';\n\n/**\n * Validates address format, checks user eth_accounts permissions.\n *\n * @param address - The Ethereum address to validate and normalize.\n * @param origin - The origin string for permission checking.\n * @param getPermittedAccountsForOrigin - Function to retrieve permitted accounts for the origin.\n * @returns A normalized (lowercase) hex address if valid and authorized.\n * @throws JsonRpcError with unauthorized error if the requester doesn't have permission to access the address.\n * @throws JsonRpcError with invalid params if the address format is invalid.\n */\nexport async function validateAndNormalizeAddress(\n  address: Hex,\n  origin: string,\n  getPermittedAccountsForOrigin: (origin: string) => Promise<string[]>,\n): Promise<Hex> {\n  if (\n    typeof address !== 'string' ||\n    address.length === 0 ||\n    !isHexAddress(address)\n  ) {\n    throw rpcErrors.invalidParams({\n      message: `Invalid parameters: must provide an EVM address.`,\n    });\n  }\n\n  // Ensure that an \"unauthorized\" error is thrown if the requester\n  // does not have the `eth_accounts` permission.\n  const accounts = await getPermittedAccountsForOrigin(origin);\n\n  // Validate and convert each account address to normalized Hex\n  const normalizedAccounts: string[] = accounts.map((accountAddress) =>\n    accountAddress.toLowerCase(),\n  );\n\n  if (!normalizedAccounts.includes(address.toLowerCase())) {\n    throw providerErrors.unauthorized();\n  }\n\n  return address;\n}\n\n/**\n * Validates parameters against a Superstruct schema and throws an error if validation fails.\n *\n * @param value - The value to validate against the struct schema.\n * @param struct - The Superstruct schema to validate against.\n * @throws JsonRpcError with invalid params if the value doesn't match the struct schema.\n */\nexport function validateParams<ParamsType>(\n  value: unknown | ParamsType,\n  struct: Struct<ParamsType>,\n): asserts value is ParamsType {\n  const [error] = validate(value, struct);\n\n  if (error) {\n    throw rpcErrors.invalidParams(\n      formatValidationError(error, 'Invalid parameters'),\n    );\n  }\n}\n\n/**\n * Formats a Superstruct validation error into a human-readable string.\n *\n * @param error - The Superstruct validation error to format.\n * @param message - The base error message to prepend to the formatted details.\n * @returns A formatted error message string with validation failure details.\n */\nfunction formatValidationError(error: StructError, message: string): string {\n  return `${message}\\n\\n${error\n    .failures()\n    .map(\n      (f) => `${f.path.join(' > ')}${f.path.length ? ' - ' : ''}${f.message}`,\n    )\n    .join('\\n')}`;\n}\n"]}