{"version":3,"file":"middleware.cjs","sourceRoot":"","sources":["../../src/permitted/middleware.ts"],"names":[],"mappings":";;;AACA,qDAAiD;AACjD,uDAAiD;AAGjD,6CAA4C;AAC5C,wCAAuC;AAEvC;;;;;;GAMG;AACH,SAAgB,2BAA2B,CACzC,MAAe,EACf,KAA8B;IAE9B,iEAAiE;IACjE,kEAAkE;IAClE,OAAO,KAAK,UAAU,gBAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG;QACjE,MAAM,OAAO,GACX,yBAAc,CAAC,OAAO,CAAC,MAAqC,CAAC,CAAC;QAChE,IAAI,OAAO,EAAE,CAAC;YACZ,IACE,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC;gBACzD,CAAC,MAAM,EACP,CAAC;gBACD,OAAO,GAAG,CAAC,sBAAS,CAAC,cAAc,EAAE,CAAC,CAAC;YACzC,CAAC;YAED,iFAAiF;YACjF,MAAM,EAAE,cAAc,EAAE,SAAS,EAAE,GAAG,OAAc,CAAC;YACrD,IAAI,CAAC;gBACH,kEAAkE;gBAClE,OAAO,MAAM,cAAc,CACzB,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,GAAG,EACH,IAAA,mBAAW,EAAC,KAAK,EAAE,SAAS,CAAC,CAC9B,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAA,sBAAQ,EAAC,KAAK,CAAC,CAAC;gBAChB,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;YACpB,CAAC;QACH,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AApCD,kEAoCC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport { methodHandlers } from './handlers';\nimport { selectHooks } from '../utils';\n\n/**\n * Creates a middleware that handles permitted snap RPC methods.\n *\n * @param isSnap - A flag that should indicate whether the requesting origin is a snap or not.\n * @param hooks - An object containing the hooks made available to the permitted RPC methods.\n * @returns The middleware.\n */\nexport function createSnapsMethodMiddleware(\n  isSnap: boolean,\n  hooks: Record<string, unknown>,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n  // This is not actually a misused promise, the type is just wrong\n  // eslint-disable-next-line @typescript-eslint/no-misused-promises\n  return async function methodMiddleware(request, response, next, end) {\n    const handler =\n      methodHandlers[request.method as keyof typeof methodHandlers];\n    if (handler) {\n      if (\n        String.prototype.startsWith.call(request.method, 'snap_') &&\n        !isSnap\n      ) {\n        return end(rpcErrors.methodNotFound());\n      }\n\n      // TODO: Once json-rpc-engine types are up to date, we should type this correctly\n      const { implementation, hookNames } = handler as any;\n      try {\n        // Implementations may or may not be async, so we must await them.\n        return await implementation(\n          request,\n          response,\n          next,\n          end,\n          selectHooks(hooks, hookNames),\n        );\n      } catch (error) {\n        logError(error);\n        return end(error);\n      }\n    }\n\n    return next();\n  };\n}\n"]}