{"version":3,"file":"requestTime.cjs","sourceRoot":"","sources":["../../../src/endowments/caveats/requestTime.ts"],"names":[],"mappings":";;;AAKA,qDAAiD;AACjD,uDAA6E;AAE7E,2CAAsE;AAItE;;;;;;;GAOG;AACH,SAAS,sBAAsB,CAC7B,KAAc,EACd,YAAwC;IAExC,IAAA,oBAAY,EACV,KAAK,EACL,kCAAoB,EACpB,wBAAwB,EACxB,YAAY,CACb,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,4BAA4B,CAAC,MAA2B;IAC/D,IAAI,CAAC,IAAA,mBAAW,EAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;QAClC,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,gCAAgC;SAC1C,CAAC,CAAC;IACL,CAAC;IAED,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IACzB,sBAAsB,CAAC,KAAK,EAAE,sBAAS,CAAC,aAAa,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,6BAA6B,CAC3C,KAAW;IAEX,IACE,CAAC,KAAK;QACN,CAAC,IAAA,gBAAQ,EAAC,KAAK,CAAC;QAChB,CAAC,IAAA,gBAAQ,EAAC,KAAK,CAAC,IAAI,CAAC,IAAA,mBAAW,EAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC,EAC1D,CAAC;QACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC3B,CAAC;IACD,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,4BAAc,CAAC,cAAc;gBACnC,KAAK,EAAE,KAAK,CAAC,cAAc;aAC5B;SACF;KACF,CAAC;AACJ,CAAC;AAlBD,sEAkBC;AAED;;;;;;GAMG;AACH,SAAgB,0BAA0B,CACxC,MAA4B;IAE5B,OAAO,UAAU,KAAW;QAC1B,2DAA2D;QAC3D,MAAM,EAAE,cAAc,EAAE,GAAG,IAAI,EAAE,GAAG,KAA6B,CAAC;QAElE,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAElC,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,OAAO,YAAY,CAAC;QACtB,CAAC;QAED,OAAO;YACL,GAAG,YAAY;YACf,OAAO,EAAE;gBACP,GAAG,CAAC,YAAY,CAAC,OAAO,IAAI,EAAE,CAAC;gBAC/B;oBACE,IAAI,EAAE,4BAAc,CAAC,cAAc;oBACnC,KAAK,EAAE,cAAc;iBACtB;aACF;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAxBD,gEAwBC;AAED;;;;;GAKG;AACH,SAAgB,uBAAuB,CACrC,UAAiC;IAEjC,MAAM,WAAW,GAAG,UAAU,EAAE,OAAO,EAAE,IAAI,CAC3C,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,4BAAc,CAAC,cAAc,CAC1D,CAAC;IACF,OAAQ,WAAW,EAAE,KAAgB,IAAI,IAAI,CAAC;AAChD,CAAC;AAPD,0DAOC;AAEY,QAAA,kCAAkC,GAG3C;IACF,CAAC,4BAAc,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC7C,IAAI,EAAE,4BAAc,CAAC,cAAc;QACnC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CACzC,4BAA4B,CAAC,MAAM,CAAC;KACvC,CAAC;CACH,CAAC","sourcesContent":["import type {\n  Caveat,\n  CaveatSpecificationConstraint,\n  PermissionConstraint,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { MaxRequestTimeStruct, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { AssertionErrorConstructor, Json } from '@metamask/utils';\nimport { assertStruct, hasProperty, isObject } from '@metamask/utils';\n\nimport type { CaveatMapperFunction, CaveatMapperReturnValue } from './generic';\n\n/**\n * Asserts that the given value is a valid `maxRequestTime` value.\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 `maxRequestTime` value.\n */\nfunction assertIsMaxRequestTime(\n  value: unknown,\n  ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is number {\n  assertStruct(\n    value,\n    MaxRequestTimeStruct,\n    'Invalid maxRequestTime',\n    ErrorWrapper,\n  );\n}\n\n/**\n * Validate the value of a caveat. This does not validate the type of the\n * caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateMaxRequestTimeCaveat(caveat: Caveat<string, any>) {\n  if (!hasProperty(caveat, 'value')) {\n    throw rpcErrors.invalidParams({\n      message: 'Invalid maxRequestTime caveat.',\n    });\n  }\n\n  const { value } = caveat;\n  assertIsMaxRequestTime(value, rpcErrors.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getMaxRequestTimeCaveatMapper(\n  value: Json,\n): CaveatMapperReturnValue {\n  if (\n    !value ||\n    !isObject(value) ||\n    (isObject(value) && !hasProperty(value, 'maxRequestTime'))\n  ) {\n    return { caveats: null };\n  }\n  return {\n    caveats: [\n      {\n        type: SnapCaveatType.MaxRequestTime,\n        value: value.maxRequestTime,\n      },\n    ],\n  };\n}\n\n/**\n * Creates a wrapping caveat mapper that creates the `maxRequestTime` caveat\n * and merges it with any other caveats created by the mapper function.\n *\n * @param mapper - Another caveat mapper function.\n * @returns The caveat specification.\n */\nexport function createMaxRequestTimeMapper(\n  mapper: CaveatMapperFunction,\n): CaveatMapperFunction {\n  return function (value: Json) {\n    // We assume this to be used only with caveats of this type\n    const { maxRequestTime, ...rest } = value as Record<string, Json>;\n\n    const mapperResult = mapper(rest);\n\n    if (!maxRequestTime) {\n      return mapperResult;\n    }\n\n    return {\n      ...mapperResult,\n      caveats: [\n        ...(mapperResult.caveats ?? []),\n        {\n          type: SnapCaveatType.MaxRequestTime,\n          value: maxRequestTime,\n        },\n      ],\n    };\n  };\n}\n\n/**\n * Getter function to get the {@link MaxRequestTime} caveat value from a permission if specified.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value if present, otherwise null.\n */\nexport function getMaxRequestTimeCaveat(\n  permission?: PermissionConstraint,\n): number | null {\n  const foundCaveat = permission?.caveats?.find(\n    (caveat) => caveat.type === SnapCaveatType.MaxRequestTime,\n  );\n  return (foundCaveat?.value as number) ?? null;\n}\n\nexport const maxRequestTimeCaveatSpecifications: Record<\n  SnapCaveatType.MaxRequestTime,\n  CaveatSpecificationConstraint\n> = {\n  [SnapCaveatType.MaxRequestTime]: Object.freeze({\n    type: SnapCaveatType.MaxRequestTime,\n    validator: (caveat: Caveat<string, any>) =>\n      validateMaxRequestTimeCaveat(caveat),\n  }),\n};\n"]}