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C,EACD,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,iEAAiE;IACjE,IAAI,WAAW,KAAK,mCAAW,CAAC,OAAO,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;QACzD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,8DAA8D;IAC9D,OAAO,OAAO,CAAC,WAAW,KAAK,mCAAW,CAAC,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;AACpE,CAAC;AA1BD,0CA0BC;AAED;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAAc;IAEd,IAAI,CAAC,IAAA,wBAAgB,EAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,IAAI,IAAA,wBAAgB,EAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,4BAA4B,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACrE,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,CAAC;AACH,CAAC;AAVD,wDAUC","sourcesContent":["import { SubjectType } from '@metamask/permission-controller';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n  array,\n  boolean,\n  object,\n  optional,\n  refine,\n  string,\n} from '@metamask/superstruct';\nimport type {\n  AssertionErrorConstructor,\n  JsonRpcSuccess,\n} from '@metamask/utils';\nimport {\n  assertStruct,\n  isJsonRpcFailure,\n  isJsonRpcSuccess,\n} from '@metamask/utils';\n\nconst AllowedOriginsStruct = array(\n  refine(string(), 'Allowed origin', (value) => {\n    const wildcards = value.split('*').length - 1;\n    if (wildcards > 2) {\n      return 'No more than two wildcards (\"*\") are allowed in an origin specifier.';\n    }\n\n    return true;\n  }),\n);\n\nexport const RpcOriginsStruct = refine(\n  object({\n    dapps: optional(boolean()),\n    snaps: optional(boolean()),\n    allowedOrigins: optional(AllowedOriginsStruct),\n  }),\n  'RPC origins',\n  (value) => {\n    const hasOrigins = Boolean(\n      value.snaps === true ||\n        value.dapps === true ||\n        (value.allowedOrigins && value.allowedOrigins.length > 0),\n    );\n\n    if (hasOrigins) {\n      return true;\n    }\n\n    return 'Must specify at least one JSON-RPC origin.';\n  },\n);\n\nexport type RpcOrigins = Infer<typeof RpcOriginsStruct>;\n\n/**\n * Asserts that the given value is a valid {@link RpcOrigins} object.\n *\n * @param value - The value to assert.\n * @param ErrorWrapper - An optional error wrapper to use. Defaults to\n * {@link AssertionError}.\n * @throws If the value is not a valid {@link RpcOrigins} object.\n */\nexport function assertIsRpcOrigins(\n  value: unknown,\n  ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is RpcOrigins {\n  assertStruct(\n    value,\n    RpcOriginsStruct,\n    'Invalid JSON-RPC origins',\n    ErrorWrapper,\n  );\n}\n\nexport const KeyringOriginsStruct = object({\n  allowedOrigins: optional(AllowedOriginsStruct),\n});\n\nexport type KeyringOrigins = Infer<typeof KeyringOriginsStruct>;\n\n/**\n * Assert that the given value is a valid {@link KeyringOrigins} object.\n *\n * @param value - The value to assert.\n * @param ErrorWrapper - An optional error wrapper to use. Defaults to\n * {@link AssertionError}.\n * @throws If the value is not a valid {@link KeyringOrigins} object.\n */\nexport function assertIsKeyringOrigins(\n  value: unknown,\n  ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is KeyringOrigins {\n  assertStruct(\n    value,\n    KeyringOriginsStruct,\n    'Invalid keyring origins',\n    ErrorWrapper,\n  );\n}\n\n/**\n * Create regular expression for matching against an origin while allowing wildcards.\n *\n * The \"*\" symbol is treated as a wildcard and will match 0 or more characters.\n *\n * @param matcher - The string to create the regular expression with.\n * @returns The regular expression.\n */\nfunction createOriginRegExp(matcher: string) {\n  // Escape potential Regex characters\n  const escaped = matcher.replace(/[.*+?^${}()|[\\]\\\\]/gu, '\\\\$&');\n  // Support wildcards\n  const regex = escaped.replace(/\\\\\\*/gu, '.*');\n  return RegExp(`^${regex}$`, 'u');\n}\n\n/**\n * Check whether an origin is allowed or not using a matcher string.\n *\n * The matcher string may be a specific origin to match or include wildcards.\n * The \"*\" symbol is treated as a wildcard and will match 0 or more characters.\n * Note: this means that https://*metamask.io matches both https://metamask.io\n * and https://snaps.metamask.io.\n *\n * @param matcher - The matcher string.\n * @param origin - The origin.\n * @returns Whether the origin is allowed.\n */\nfunction checkAllowedOrigin(matcher: string, origin: string) {\n  // If the matcher is a single wildcard or identical to the origin we can return true immediately.\n  if (matcher === '*' || matcher === origin) {\n    return true;\n  }\n\n  const regex = createOriginRegExp(matcher);\n  return regex.test(origin);\n}\n\n/**\n * Check if the given origin is allowed by the given JSON-RPC origins object.\n *\n * @param origins - The JSON-RPC origins object.\n * @param subjectType - The type of the origin.\n * @param origin - The origin to check.\n * @returns Whether the origin is allowed.\n */\nexport function isOriginAllowed(\n  origins: RpcOrigins,\n  subjectType: SubjectType,\n  origin: string,\n) {\n  // The MetaMask client is always allowed.\n  if (origin === 'metamask') {\n    return true;\n  }\n\n  // If the origin is in the `allowedOrigins` list, it is allowed.\n  if (\n    origins.allowedOrigins?.some((matcher) =>\n      checkAllowedOrigin(matcher, origin),\n    )\n  ) {\n    return true;\n  }\n\n  // If the origin is a website and `dapps` is true, it is allowed.\n  if (subjectType === SubjectType.Website && origins.dapps) {\n    return true;\n  }\n\n  // If the origin is a snap and `snaps` is true, it is allowed.\n  return Boolean(subjectType === SubjectType.Snap && origins.snaps);\n}\n\n/**\n * Assert that the given value is a successful JSON-RPC response. If the value\n * is not a success response, an error is thrown. If the value is an JSON-RPC\n * error, the error message is included in the thrown error.\n *\n * @param value - The value to check.\n * @throws If the value is not a JSON-RPC success response.\n */\nexport function assertIsJsonRpcSuccess(\n  value: unknown,\n): asserts value is JsonRpcSuccess {\n  if (!isJsonRpcSuccess(value)) {\n    if (isJsonRpcFailure(value)) {\n      throw new Error(`JSON-RPC request failed: ${value.error.message}`);\n    }\n\n    throw new Error('Invalid JSON-RPC response.');\n  }\n}\n"]}