{"version":3,"file":"specifications.cjs","sourceRoot":"","sources":["../../src/methods/specifications.ts"],"names":[],"mappings":";;;AAAA,mFAA6E;AAQ7E,mEAIqC;AAErC,uDAA2D;AAE3D,+CAGqB;AACrB,6CAAgE;AAoBhE;;;;;GAKG;AACH,SAAgB,OAAO,CAAC,MAAe;IACrC,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC;AACtB,CAAC;AAFD,0BAEC;AAED;;;;;;GAMG;AACH,SAAgB,YAAY,CAAO,MAAa;IAC9C,OAAO,KAAK,IAAI,EAAE,CAAC,MAAM,CAAC;AAC5B,CAAC;AAFD,oCAEC;AAED;;;;;;;;GAQG;AACH,SAAgB,2BAA2B,CAAC,EAC1C,mBAAmB,EACnB,KAAK,EACL,OAAO,GAC4B;IACnC,MAAM,EAAE,qBAAqB,EAAE,GAAG,KAAK,CAAC;IACxC,OAAO;QACL,CAAC,kDAAsB,CAAC,UAAU,CAAC,EACjC,kDAAsB,CAAC,oBAAoB,CAAC,EAAE,CAAC;QACjD,GAAG,IAAA,oDAAgC,EAAC,oCAAwB,CAAC;QAC7D,GAAG,IAAA,2DAAuC,EACxC,qCAAyB,EACzB;YACE,gBAAgB;YAChB,qBAAqB;YAErB,wBAAwB;YACxB,cAAc,EAAE,IAAA,6CAAqC,EAAC,OAAO,CAAC;YAC9D,gBAAgB,EAAE,YAAY,CAAC,IAAI,CAAC;YACpC,cAAc,EAAE,YAAY,CAAC,IAAI,CAAC;YAElC,+DAA+D;YAC/D,gBAAgB,EAAE,OAAO,CAAC,KAAK,CAAC;YAEhC,uBAAuB,EAAE,YAAY,EAAE;SACxC,EACD,mBAAgD,CACjD;KACF,CAAC;AACJ,CAAC;AA7BD,kEA6BC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,aAAa,CACjC,oBAAiD,EACjD,MAAc;IAEd,MAAM,aAAa,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,+CAA2B,CAAC,CAAC,MAAM,CAEzE,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,EAAE;QAClC,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC;QAClC,IAAI,oBAAoB,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,EAAE,CAAC;YAC/D,MAAM,UAAU,GAAG,MAAM,oBAAoB,CAAC,aAAa,CACzD,MAAM,EACN,cAAc,CACf,CAAC;YAEF,IAAI,UAAU,EAAE,CAAC;gBACf,OAAO,WAAW,CAAC,MAAM,CAAC,UAAsB,CAAC,CAAC;YACpD,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;IAExB,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,gCAAkB,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;AACjE,CAAC;AAvBD,sCAuBC","sourcesContent":["import { caip25EndowmentBuilder } from '@metamask/chain-agnostic-permission';\nimport type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n  GenericPermissionController,\n  PermissionSpecificationConstraint,\n  PermissionSpecificationMap,\n} from '@metamask/permission-controller';\nimport type { RestrictedMethodMessenger } from '@metamask/snaps-rpc-methods';\nimport {\n  endowmentPermissionBuilders,\n  buildSnapEndowmentSpecifications,\n  buildSnapRestrictedMethodSpecifications,\n} from '@metamask/snaps-rpc-methods';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { DEFAULT_ENDOWMENTS } from '@metamask/snaps-utils';\n\nimport {\n  EXCLUDED_SNAP_ENDOWMENTS,\n  EXCLUDED_SNAP_PERMISSIONS,\n} from './constants';\nimport { getGetPreferencesMethodImplementation } from './hooks';\nimport type { RootControllerMessenger } from '../controllers';\nimport type { SimulationOptions } from '../options';\n\nexport type PermissionSpecificationsHooks = {\n  /**\n   * Get the cryptographic functions to use for the client. This may return an\n   * empty object to fall back to the default cryptographic functions.\n   *\n   * @returns The cryptographic functions to use for the client.\n   */\n  getClientCryptography: () => CryptographicFunctions;\n};\n\nexport type GetPermissionSpecificationsOptions = {\n  controllerMessenger: RootControllerMessenger;\n  hooks: PermissionSpecificationsHooks;\n  options: SimulationOptions;\n};\n\n/**\n * Get a function which resolves with the specified result.\n *\n * @param result - The result to return.\n * @returns The function implementation.\n */\nexport function resolve(result: unknown) {\n  return () => result;\n}\n\n/**\n * Get a function which resolves with the specified result.\n *\n * @param result - The result to return. If not specified, the function will\n * resolve with `undefined`.\n * @returns The function implementation.\n */\nexport function asyncResolve<Type>(result?: Type) {\n  return async () => result;\n}\n\n/**\n * Get the permission specifications for the Snap.\n *\n * @param options - The options.\n * @param options.controllerMessenger - The controller messenger.\n * @param options.hooks - The hooks.\n * @param options.options - The simulation options.\n * @returns The permission specifications for the Snap.\n */\nexport function getPermissionSpecifications({\n  controllerMessenger,\n  hooks,\n  options,\n}: GetPermissionSpecificationsOptions): PermissionSpecificationMap<PermissionSpecificationConstraint> {\n  const { getClientCryptography } = hooks;\n  return {\n    [caip25EndowmentBuilder.targetName]:\n      caip25EndowmentBuilder.specificationBuilder({}),\n    ...buildSnapEndowmentSpecifications(EXCLUDED_SNAP_ENDOWMENTS),\n    ...buildSnapRestrictedMethodSpecifications(\n      EXCLUDED_SNAP_PERMISSIONS,\n      {\n        // Shared hooks.\n        getClientCryptography,\n\n        // Snaps-specific hooks.\n        getPreferences: getGetPreferencesMethodImplementation(options),\n        getUnlockPromise: asyncResolve(true),\n        getSnapKeyring: asyncResolve(null),\n\n        // TODO: Allow the user to specify the result of this function.\n        isOnPhishingList: resolve(false),\n\n        maybeUpdatePhishingList: asyncResolve(),\n      },\n      controllerMessenger as RestrictedMethodMessenger,\n    ),\n  };\n}\n\n/**\n * Get the endowments for the Snap.\n *\n * @param permissionController - The permission controller.\n * @param snapId - The ID of the Snap.\n * @returns The endowments for the Snap.\n */\nexport async function getEndowments(\n  permissionController: GenericPermissionController,\n  snapId: SnapId,\n) {\n  const allEndowments = await Object.keys(endowmentPermissionBuilders).reduce<\n    Promise<string[]>\n  >(async (promise, permissionName) => {\n    const accumulator = await promise;\n    if (permissionController.hasPermission(snapId, permissionName)) {\n      const endowments = await permissionController.getEndowments(\n        snapId,\n        permissionName,\n      );\n\n      if (endowments) {\n        return accumulator.concat(endowments as string[]);\n      }\n    }\n\n    return accumulator;\n  }, Promise.resolve([]));\n\n  return [...new Set([...DEFAULT_ENDOWMENTS, ...allEndowments])];\n}\n"]}