@metamask/snaps-simulation 1.5.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/CHANGELOG.md +16 -1
  2. package/dist/controllers.cjs.map +1 -1
  3. package/dist/controllers.d.cts +2 -2
  4. package/dist/controllers.d.cts.map +1 -1
  5. package/dist/controllers.d.mts +2 -2
  6. package/dist/controllers.d.mts.map +1 -1
  7. package/dist/controllers.mjs.map +1 -1
  8. package/dist/helpers.cjs +1 -0
  9. package/dist/helpers.cjs.map +1 -1
  10. package/dist/helpers.d.cts +9 -0
  11. package/dist/helpers.d.cts.map +1 -1
  12. package/dist/helpers.d.mts +9 -0
  13. package/dist/helpers.d.mts.map +1 -1
  14. package/dist/helpers.mjs +1 -0
  15. package/dist/helpers.mjs.map +1 -1
  16. package/dist/interface.cjs +6 -0
  17. package/dist/interface.cjs.map +1 -1
  18. package/dist/interface.d.cts +1 -1
  19. package/dist/interface.d.cts.map +1 -1
  20. package/dist/interface.d.mts +1 -1
  21. package/dist/interface.d.mts.map +1 -1
  22. package/dist/interface.mjs +6 -0
  23. package/dist/interface.mjs.map +1 -1
  24. package/dist/methods/hooks/get-preferences.cjs +3 -2
  25. package/dist/methods/hooks/get-preferences.cjs.map +1 -1
  26. package/dist/methods/hooks/get-preferences.d.cts +3 -1
  27. package/dist/methods/hooks/get-preferences.d.cts.map +1 -1
  28. package/dist/methods/hooks/get-preferences.d.mts +3 -1
  29. package/dist/methods/hooks/get-preferences.d.mts.map +1 -1
  30. package/dist/methods/hooks/get-preferences.mjs +3 -2
  31. package/dist/methods/hooks/get-preferences.mjs.map +1 -1
  32. package/dist/methods/hooks/index.cjs +2 -1
  33. package/dist/methods/hooks/index.cjs.map +1 -1
  34. package/dist/methods/hooks/index.d.cts +2 -1
  35. package/dist/methods/hooks/index.d.cts.map +1 -1
  36. package/dist/methods/hooks/index.d.mts +2 -1
  37. package/dist/methods/hooks/index.d.mts.map +1 -1
  38. package/dist/methods/hooks/index.mjs +2 -1
  39. package/dist/methods/hooks/index.mjs.map +1 -1
  40. package/dist/methods/hooks/notifications.cjs +15 -2
  41. package/dist/methods/hooks/notifications.cjs.map +1 -1
  42. package/dist/methods/hooks/notifications.d.cts +13 -1
  43. package/dist/methods/hooks/notifications.d.cts.map +1 -1
  44. package/dist/methods/hooks/notifications.d.mts +13 -1
  45. package/dist/methods/hooks/notifications.d.mts.map +1 -1
  46. package/dist/methods/hooks/notifications.mjs +16 -3
  47. package/dist/methods/hooks/notifications.mjs.map +1 -1
  48. package/dist/methods/hooks/permitted/index.cjs +18 -0
  49. package/dist/methods/hooks/permitted/index.cjs.map +1 -0
  50. package/dist/methods/hooks/permitted/index.d.cts +2 -0
  51. package/dist/methods/hooks/permitted/index.d.cts.map +1 -0
  52. package/dist/methods/hooks/permitted/index.d.mts +2 -0
  53. package/dist/methods/hooks/permitted/index.d.mts.map +1 -0
  54. package/dist/methods/hooks/permitted/index.mjs +2 -0
  55. package/dist/methods/hooks/permitted/index.mjs.map +1 -0
  56. package/dist/methods/hooks/permitted/state.cjs +78 -0
  57. package/dist/methods/hooks/permitted/state.cjs.map +1 -0
  58. package/dist/methods/hooks/permitted/state.d.cts +24 -0
  59. package/dist/methods/hooks/permitted/state.d.cts.map +1 -0
  60. package/dist/methods/hooks/permitted/state.d.mts +24 -0
  61. package/dist/methods/hooks/permitted/state.d.mts.map +1 -0
  62. package/dist/methods/hooks/permitted/state.mjs +72 -0
  63. package/dist/methods/hooks/permitted/state.mjs.map +1 -0
  64. package/dist/methods/specifications.cjs.map +1 -1
  65. package/dist/methods/specifications.d.cts +1 -1
  66. package/dist/methods/specifications.d.cts.map +1 -1
  67. package/dist/methods/specifications.d.mts +1 -1
  68. package/dist/methods/specifications.d.mts.map +1 -1
  69. package/dist/methods/specifications.mjs.map +1 -1
  70. package/dist/middleware/engine.cjs +7 -4
  71. package/dist/middleware/engine.cjs.map +1 -1
  72. package/dist/middleware/engine.d.cts +6 -4
  73. package/dist/middleware/engine.d.cts.map +1 -1
  74. package/dist/middleware/engine.d.mts +6 -4
  75. package/dist/middleware/engine.d.mts.map +1 -1
  76. package/dist/middleware/engine.mjs +7 -4
  77. package/dist/middleware/engine.mjs.map +1 -1
  78. package/dist/options.cjs +1 -0
  79. package/dist/options.cjs.map +1 -1
  80. package/dist/options.d.cts +2 -0
  81. package/dist/options.d.cts.map +1 -1
  82. package/dist/options.d.mts +2 -0
  83. package/dist/options.d.mts.map +1 -1
  84. package/dist/options.mjs +2 -1
  85. package/dist/options.mjs.map +1 -1
  86. package/dist/request.cjs +8 -5
  87. package/dist/request.cjs.map +1 -1
  88. package/dist/request.d.cts +2 -1
  89. package/dist/request.d.cts.map +1 -1
  90. package/dist/request.d.mts +2 -1
  91. package/dist/request.d.mts.map +1 -1
  92. package/dist/request.mjs +8 -5
  93. package/dist/request.mjs.map +1 -1
  94. package/dist/simulation.cjs +30 -10
  95. package/dist/simulation.cjs.map +1 -1
  96. package/dist/simulation.d.cts +101 -13
  97. package/dist/simulation.d.cts.map +1 -1
  98. package/dist/simulation.d.mts +101 -13
  99. package/dist/simulation.d.mts.map +1 -1
  100. package/dist/simulation.mjs +28 -9
  101. package/dist/simulation.mjs.map +1 -1
  102. package/dist/store/notifications.cjs.map +1 -1
  103. package/dist/store/notifications.d.cts +9 -0
  104. package/dist/store/notifications.d.cts.map +1 -1
  105. package/dist/store/notifications.d.mts +9 -0
  106. package/dist/store/notifications.d.mts.map +1 -1
  107. package/dist/store/notifications.mjs.map +1 -1
  108. package/dist/store/ui.cjs.map +1 -1
  109. package/dist/store/ui.d.cts +1 -1
  110. package/dist/store/ui.d.cts.map +1 -1
  111. package/dist/store/ui.d.mts +1 -1
  112. package/dist/store/ui.d.mts.map +1 -1
  113. package/dist/store/ui.mjs.map +1 -1
  114. package/dist/structs.cjs +6 -0
  115. package/dist/structs.cjs.map +1 -1
  116. package/dist/structs.d.cts +66 -0
  117. package/dist/structs.d.cts.map +1 -1
  118. package/dist/structs.d.mts +66 -0
  119. package/dist/structs.d.mts.map +1 -1
  120. package/dist/structs.mjs +6 -0
  121. package/dist/structs.mjs.map +1 -1
  122. package/dist/types.cjs.map +1 -1
  123. package/dist/types.d.cts +15 -0
  124. package/dist/types.d.cts.map +1 -1
  125. package/dist/types.d.mts +15 -0
  126. package/dist/types.d.mts.map +1 -1
  127. package/dist/types.mjs.map +1 -1
  128. package/package.json +14 -14
@@ -1 +1 @@
1
- {"version":3,"file":"specifications.mjs","sourceRoot":"","sources":["../../src/methods/specifications.ts"],"names":[],"mappings":"AACA,OAAO,EACL,2BAA2B,EAC3B,gCAAgC,EAChC,uCAAuC,EACxC,oCAAoC;AAErC,OAAO,EAAE,kBAAkB,EAAE,8BAA8B;AAK3D,OAAO,EACL,wBAAwB,EACxB,yBAAyB,EAC1B,wBAAoB;AACrB,OAAO,EACL,qCAAqC,EACrC,qCAAqC,EACrC,mCAAmC,EACnC,sCAAsC,EACtC,sCAAsC,EACtC,uCAAuC,EACvC,gCAAgC,EAChC,6BAA6B,EAC7B,oCAAoC,EACrC,0BAAgB;AAkBjB;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,MAAe;IACrC,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC;AACtB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,MAAgB;IAC3C,OAAO,KAAK,IAAI,EAAE,CAAC,MAAM,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,2BAA2B,CAAC,EAC1C,mBAAmB,EACnB,KAAK,EACL,OAAO,EACP,OAAO,GAC4B;IACnC,OAAO;QACL,GAAG,gCAAgC,CAAC,wBAAwB,CAAC;QAC7D,GAAG,uCAAuC,CAAC,yBAAyB,EAAE;YACpE,gBAAgB;YAChB,GAAG,KAAK;YAER,wBAAwB;YACxB,cAAc,EAAE,qCAAqC,CAAC,OAAO,CAAC;YAC9D,cAAc,EAAE,qCAAqC,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ACjE,CAAC","sourcesContent":["import type { GenericPermissionController } from '@metamask/permission-controller';\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 type { RootControllerMessenger } from '../controllers';\nimport type { SimulationOptions } from '../options';\nimport type { RunSagaFunction } from '../store';\nimport {\n EXCLUDED_SNAP_ENDOWMENTS,\n EXCLUDED_SNAP_PERMISSIONS,\n} from './constants';\nimport {\n getGetPreferencesMethodImplementation,\n getClearSnapStateMethodImplementation,\n getGetSnapStateMethodImplementation,\n getUpdateSnapStateMethodImplementation,\n getShowInAppNotificationImplementation,\n getShowNativeNotificationImplementation,\n getCreateInterfaceImplementation,\n getGetInterfaceImplementation,\n getRequestUserApprovalImplementation,\n} from './hooks';\n\nexport type PermissionSpecificationsHooks = {\n /**\n * A hook that returns the user's secret recovery phrase.\n *\n * @returns The user's secret recovery phrase.\n */\n getMnemonic: () => Promise<Uint8Array>;\n};\n\nexport type GetPermissionSpecificationsOptions = {\n controllerMessenger: RootControllerMessenger;\n hooks: PermissionSpecificationsHooks;\n runSaga: RunSagaFunction;\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(result?: unknown) {\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.runSaga - The function to run a saga outside the usual Redux\n * flow.\n * @param options.options - The simulation options.\n * @returns The permission specifications for the Snap.\n */\nexport function getPermissionSpecifications({\n controllerMessenger,\n hooks,\n runSaga,\n options,\n}: GetPermissionSpecificationsOptions) {\n return {\n ...buildSnapEndowmentSpecifications(EXCLUDED_SNAP_ENDOWMENTS),\n ...buildSnapRestrictedMethodSpecifications(EXCLUDED_SNAP_PERMISSIONS, {\n // Shared hooks.\n ...hooks,\n\n // Snaps-specific hooks.\n clearSnapState: getClearSnapStateMethodImplementation(runSaga),\n getPreferences: getGetPreferencesMethodImplementation(options),\n getSnapState: getGetSnapStateMethodImplementation(runSaga),\n getUnlockPromise: asyncResolve(true),\n\n // TODO: Allow the user to specify the result of this function.\n isOnPhishingList: resolve(false),\n\n maybeUpdatePhishingList: asyncResolve(),\n requestUserApproval: getRequestUserApprovalImplementation(runSaga),\n showInAppNotification: getShowInAppNotificationImplementation(runSaga),\n showNativeNotification: getShowNativeNotificationImplementation(runSaga),\n updateSnapState: getUpdateSnapStateMethodImplementation(runSaga),\n createInterface: getCreateInterfaceImplementation(controllerMessenger),\n getInterface: getGetInterfaceImplementation(controllerMessenger),\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"]}
1
+ {"version":3,"file":"specifications.mjs","sourceRoot":"","sources":["../../src/methods/specifications.ts"],"names":[],"mappings":"AACA,OAAO,EACL,2BAA2B,EAC3B,gCAAgC,EAChC,uCAAuC,EACxC,oCAAoC;AAErC,OAAO,EAAE,kBAAkB,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B,CAAC,mBAAmB,CAAC;SACjE,CAAC;KACH,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,oBAAiD,EACjD,MAAc;IAEd,MAAM,aAAa,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,2BAA2B,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,kBAAkB,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import type { GenericPermissionController } from '@metamask/permission-controller';\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 type { RootControllerMessenger } from '../controllers';\nimport type { SimulationOptions } from '../options';\nimport type { RunSagaFunction } from '../store';\nimport {\n EXCLUDED_SNAP_ENDOWMENTS,\n EXCLUDED_SNAP_PERMISSIONS,\n} from './constants';\nimport {\n getGetPreferencesMethodImplementation,\n getClearSnapStateMethodImplementation,\n getGetSnapStateMethodImplementation,\n getUpdateSnapStateMethodImplementation,\n getShowInAppNotificationImplementation,\n getShowNativeNotificationImplementation,\n getCreateInterfaceImplementation,\n getGetInterfaceImplementation,\n getRequestUserApprovalImplementation,\n} from './hooks';\n\nexport type PermissionSpecificationsHooks = {\n /**\n * A hook that returns the user's secret recovery phrase.\n *\n * @returns The user's secret recovery phrase.\n */\n getMnemonic: () => Promise<Uint8Array>;\n};\n\nexport type GetPermissionSpecificationsOptions = {\n controllerMessenger: RootControllerMessenger;\n hooks: PermissionSpecificationsHooks;\n runSaga: RunSagaFunction;\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.runSaga - The function to run a saga outside the usual Redux\n * flow.\n * @param options.options - The simulation options.\n * @returns The permission specifications for the Snap.\n */\nexport function getPermissionSpecifications({\n controllerMessenger,\n hooks,\n runSaga,\n options,\n}: GetPermissionSpecificationsOptions) {\n return {\n ...buildSnapEndowmentSpecifications(EXCLUDED_SNAP_ENDOWMENTS),\n ...buildSnapRestrictedMethodSpecifications(EXCLUDED_SNAP_PERMISSIONS, {\n // Shared hooks.\n ...hooks,\n\n // Snaps-specific hooks.\n clearSnapState: getClearSnapStateMethodImplementation(runSaga),\n getPreferences: getGetPreferencesMethodImplementation(options),\n getSnapState: getGetSnapStateMethodImplementation(runSaga),\n getUnlockPromise: asyncResolve(true),\n\n // TODO: Allow the user to specify the result of this function.\n isOnPhishingList: resolve(false),\n\n maybeUpdatePhishingList: asyncResolve(),\n requestUserApproval: getRequestUserApprovalImplementation(runSaga),\n showInAppNotification: getShowInAppNotificationImplementation(runSaga),\n showNativeNotification: getShowNativeNotificationImplementation(runSaga),\n updateSnapState: getUpdateSnapStateMethodImplementation(runSaga),\n createInterface: getCreateInterfaceImplementation(controllerMessenger),\n getInterface: getGetInterfaceImplementation(controllerMessenger),\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"]}
@@ -15,16 +15,19 @@ const mock_1 = require("./mock.cjs");
15
15
  *
16
16
  * @param options - The options to use when creating the engine.
17
17
  * @param options.store - The Redux store to use.
18
- * @param options.hooks - Any hooks used by the middleware handlers.
18
+ * @param options.restrictedHooks - Any hooks used by the middleware handlers.
19
+ * @param options.permittedHooks - Any hooks used by the middleware handlers.
19
20
  * @param options.permissionMiddleware - The permission middleware to use.
20
21
  * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.
21
22
  * @returns A JSON-RPC engine.
22
23
  */
23
- function createJsonRpcEngine({ store, hooks, permissionMiddleware, endpoint = constants_1.DEFAULT_JSON_RPC_ENDPOINT, }) {
24
+ function createJsonRpcEngine({ store, restrictedHooks, permittedHooks, permissionMiddleware, endpoint = constants_1.DEFAULT_JSON_RPC_ENDPOINT, }) {
24
25
  const engine = new json_rpc_engine_1.JsonRpcEngine();
25
26
  engine.push((0, mock_1.createMockMiddleware)(store));
26
- engine.push((0, internal_methods_1.createInternalMethodsMiddleware)(hooks));
27
- engine.push((0, snaps_rpc_methods_1.createSnapsMethodMiddleware)(true, hooks));
27
+ // The hooks here do not match the hooks used by the clients, so this
28
+ // middleware should not be used outside of the simulation environment.
29
+ engine.push((0, internal_methods_1.createInternalMethodsMiddleware)(restrictedHooks));
30
+ engine.push((0, snaps_rpc_methods_1.createSnapsMethodMiddleware)(true, permittedHooks));
28
31
  engine.push(permissionMiddleware);
29
32
  engine.push((0, eth_json_rpc_middleware_1.createFetchMiddleware)({
30
33
  btoa: globalThis.btoa,
@@ -1 +1 @@
1
- {"version":3,"file":"engine.cjs","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":";;;AAAA,+EAA0E;AAE1E,+DAA0D;AAE1D,mEAA0E;AAG1E,gDAAyD;AAGzD,mEAAqE;AACrE,qCAA8C;AAS9C;;;;;;;;;;;;GAYG;AACH,SAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,KAAK,EACL,oBAAoB,EACpB,QAAQ,GAAG,qCAAyB,GACT;IAC3B,MAAM,MAAM,GAAG,IAAI,+BAAa,EAAE,CAAC;IACnC,MAAM,CAAC,IAAI,CAAC,IAAA,2BAAoB,EAAC,KAAK,CAAC,CAAC,CAAC;IACzC,MAAM,CAAC,IAAI,CAAC,IAAA,kDAA+B,EAAC,KAAK,CAAC,CAAC,CAAC;IACpD,MAAM,CAAC,IAAI,CAAC,IAAA,+CAA2B,EAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IACtD,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CACT,IAAA,+CAAqB,EAAC;QACpB,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,MAAM,EAAE,QAAQ;KACjB,CAAC,CACH,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AApBD,kDAoBC","sourcesContent":["import { createFetchMiddleware } from '@metamask/eth-json-rpc-middleware';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { RestrictedMethodParameters } from '@metamask/permission-controller';\nimport { createSnapsMethodMiddleware } from '@metamask/snaps-rpc-methods';\nimport type { Json } from '@metamask/utils';\n\nimport { DEFAULT_JSON_RPC_ENDPOINT } from '../constants';\nimport type { MiddlewareHooks } from '../simulation';\nimport type { Store } from '../store';\nimport { createInternalMethodsMiddleware } from './internal-methods';\nimport { createMockMiddleware } from './mock';\n\nexport type CreateJsonRpcEngineOptions = {\n store: Store;\n hooks: MiddlewareHooks;\n permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;\n endpoint?: string;\n};\n\n/**\n * Create a JSON-RPC engine for use in a simulated environment. This engine\n * should be used to handle all JSON-RPC requests. It is set up to handle\n * requests that would normally be handled internally by the MetaMask client, as\n * well as Snap-specific requests.\n *\n * @param options - The options to use when creating the engine.\n * @param options.store - The Redux store to use.\n * @param options.hooks - Any hooks used by the middleware handlers.\n * @param options.permissionMiddleware - The permission middleware to use.\n * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.\n * @returns A JSON-RPC engine.\n */\nexport function createJsonRpcEngine({\n store,\n hooks,\n permissionMiddleware,\n endpoint = DEFAULT_JSON_RPC_ENDPOINT,\n}: CreateJsonRpcEngineOptions) {\n const engine = new JsonRpcEngine();\n engine.push(createMockMiddleware(store));\n engine.push(createInternalMethodsMiddleware(hooks));\n engine.push(createSnapsMethodMiddleware(true, hooks));\n engine.push(permissionMiddleware);\n engine.push(\n createFetchMiddleware({\n btoa: globalThis.btoa,\n fetch: globalThis.fetch,\n rpcUrl: endpoint,\n }),\n );\n\n return engine;\n}\n"]}
1
+ {"version":3,"file":"engine.cjs","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":";;;AAAA,+EAA0E;AAE1E,+DAA0D;AAE1D,mEAA0E;AAG1E,gDAAyD;AAMzD,mEAAqE;AACrE,qCAA8C;AAU9C;;;;;;;;;;;;;GAaG;AACH,SAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,QAAQ,GAAG,qCAAyB,GACT;IAC3B,MAAM,MAAM,GAAG,IAAI,+BAAa,EAAE,CAAC;IACnC,MAAM,CAAC,IAAI,CAAC,IAAA,2BAAoB,EAAC,KAAK,CAAC,CAAC,CAAC;IAEzC,qEAAqE;IACrE,uEAAuE;IACvE,MAAM,CAAC,IAAI,CAAC,IAAA,kDAA+B,EAAC,eAAe,CAAC,CAAC,CAAC;IAC9D,MAAM,CAAC,IAAI,CAAC,IAAA,+CAA2B,EAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC;IAE/D,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CACT,IAAA,+CAAqB,EAAC;QACpB,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,MAAM,EAAE,QAAQ;KACjB,CAAC,CACH,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAzBD,kDAyBC","sourcesContent":["import { createFetchMiddleware } from '@metamask/eth-json-rpc-middleware';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { RestrictedMethodParameters } from '@metamask/permission-controller';\nimport { createSnapsMethodMiddleware } from '@metamask/snaps-rpc-methods';\nimport type { Json } from '@metamask/utils';\n\nimport { DEFAULT_JSON_RPC_ENDPOINT } from '../constants';\nimport type {\n PermittedMiddlewareHooks,\n RestrictedMiddlewareHooks,\n} from '../simulation';\nimport type { Store } from '../store';\nimport { createInternalMethodsMiddleware } from './internal-methods';\nimport { createMockMiddleware } from './mock';\n\nexport type CreateJsonRpcEngineOptions = {\n store: Store;\n restrictedHooks: RestrictedMiddlewareHooks;\n permittedHooks: PermittedMiddlewareHooks;\n permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;\n endpoint?: string;\n};\n\n/**\n * Create a JSON-RPC engine for use in a simulated environment. This engine\n * should be used to handle all JSON-RPC requests. It is set up to handle\n * requests that would normally be handled internally by the MetaMask client, as\n * well as Snap-specific requests.\n *\n * @param options - The options to use when creating the engine.\n * @param options.store - The Redux store to use.\n * @param options.restrictedHooks - Any hooks used by the middleware handlers.\n * @param options.permittedHooks - Any hooks used by the middleware handlers.\n * @param options.permissionMiddleware - The permission middleware to use.\n * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.\n * @returns A JSON-RPC engine.\n */\nexport function createJsonRpcEngine({\n store,\n restrictedHooks,\n permittedHooks,\n permissionMiddleware,\n endpoint = DEFAULT_JSON_RPC_ENDPOINT,\n}: CreateJsonRpcEngineOptions) {\n const engine = new JsonRpcEngine();\n engine.push(createMockMiddleware(store));\n\n // The hooks here do not match the hooks used by the clients, so this\n // middleware should not be used outside of the simulation environment.\n engine.push(createInternalMethodsMiddleware(restrictedHooks));\n engine.push(createSnapsMethodMiddleware(true, permittedHooks));\n\n engine.push(permissionMiddleware);\n engine.push(\n createFetchMiddleware({\n btoa: globalThis.btoa,\n fetch: globalThis.fetch,\n rpcUrl: endpoint,\n }),\n );\n\n return engine;\n}\n"]}
@@ -2,11 +2,12 @@ import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
2
  import { JsonRpcEngine } from "@metamask/json-rpc-engine";
3
3
  import type { RestrictedMethodParameters } from "@metamask/permission-controller";
4
4
  import type { Json } from "@metamask/utils";
5
- import type { MiddlewareHooks } from "../simulation.cjs";
5
+ import type { PermittedMiddlewareHooks, RestrictedMiddlewareHooks } from "../simulation.cjs";
6
6
  import type { Store } from "../store/index.cjs";
7
7
  export type CreateJsonRpcEngineOptions = {
8
8
  store: Store;
9
- hooks: MiddlewareHooks;
9
+ restrictedHooks: RestrictedMiddlewareHooks;
10
+ permittedHooks: PermittedMiddlewareHooks;
10
11
  permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;
11
12
  endpoint?: string;
12
13
  };
@@ -18,10 +19,11 @@ export type CreateJsonRpcEngineOptions = {
18
19
  *
19
20
  * @param options - The options to use when creating the engine.
20
21
  * @param options.store - The Redux store to use.
21
- * @param options.hooks - Any hooks used by the middleware handlers.
22
+ * @param options.restrictedHooks - Any hooks used by the middleware handlers.
23
+ * @param options.permittedHooks - Any hooks used by the middleware handlers.
22
24
  * @param options.permissionMiddleware - The permission middleware to use.
23
25
  * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.
24
26
  * @returns A JSON-RPC engine.
25
27
  */
26
- export declare function createJsonRpcEngine({ store, hooks, permissionMiddleware, endpoint, }: CreateJsonRpcEngineOptions): JsonRpcEngine;
28
+ export declare function createJsonRpcEngine({ store, restrictedHooks, permittedHooks, permissionMiddleware, endpoint, }: CreateJsonRpcEngineOptions): JsonRpcEngine;
27
29
  //# sourceMappingURL=engine.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"engine.d.cts","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAC1D,OAAO,KAAK,EAAE,0BAA0B,EAAE,wCAAwC;AAElF,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,KAAK,EAAE,eAAe,EAAE,0BAAsB;AACrD,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAItC,MAAM,MAAM,0BAA0B,GAAG;IACvC,KAAK,EAAE,KAAK,CAAC;IACb,KAAK,EAAE,eAAe,CAAC;IACvB,oBAAoB,EAAE,iBAAiB,CAAC,0BAA0B,EAAE,IAAI,CAAC,CAAC;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,KAAK,EACL,oBAAoB,EACpB,QAAoC,GACrC,EAAE,0BAA0B,iBAe5B"}
1
+ {"version":3,"file":"engine.d.cts","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAC1D,OAAO,KAAK,EAAE,0BAA0B,EAAE,wCAAwC;AAElF,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EAC1B,0BAAsB;AACvB,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAItC,MAAM,MAAM,0BAA0B,GAAG;IACvC,KAAK,EAAE,KAAK,CAAC;IACb,eAAe,EAAE,yBAAyB,CAAC;IAC3C,cAAc,EAAE,wBAAwB,CAAC;IACzC,oBAAoB,EAAE,iBAAiB,CAAC,0BAA0B,EAAE,IAAI,CAAC,CAAC;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,QAAoC,GACrC,EAAE,0BAA0B,iBAmB5B"}
@@ -2,11 +2,12 @@ import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
2
  import { JsonRpcEngine } from "@metamask/json-rpc-engine";
3
3
  import type { RestrictedMethodParameters } from "@metamask/permission-controller";
4
4
  import type { Json } from "@metamask/utils";
5
- import type { MiddlewareHooks } from "../simulation.mjs";
5
+ import type { PermittedMiddlewareHooks, RestrictedMiddlewareHooks } from "../simulation.mjs";
6
6
  import type { Store } from "../store/index.mjs";
7
7
  export type CreateJsonRpcEngineOptions = {
8
8
  store: Store;
9
- hooks: MiddlewareHooks;
9
+ restrictedHooks: RestrictedMiddlewareHooks;
10
+ permittedHooks: PermittedMiddlewareHooks;
10
11
  permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;
11
12
  endpoint?: string;
12
13
  };
@@ -18,10 +19,11 @@ export type CreateJsonRpcEngineOptions = {
18
19
  *
19
20
  * @param options - The options to use when creating the engine.
20
21
  * @param options.store - The Redux store to use.
21
- * @param options.hooks - Any hooks used by the middleware handlers.
22
+ * @param options.restrictedHooks - Any hooks used by the middleware handlers.
23
+ * @param options.permittedHooks - Any hooks used by the middleware handlers.
22
24
  * @param options.permissionMiddleware - The permission middleware to use.
23
25
  * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.
24
26
  * @returns A JSON-RPC engine.
25
27
  */
26
- export declare function createJsonRpcEngine({ store, hooks, permissionMiddleware, endpoint, }: CreateJsonRpcEngineOptions): JsonRpcEngine;
28
+ export declare function createJsonRpcEngine({ store, restrictedHooks, permittedHooks, permissionMiddleware, endpoint, }: CreateJsonRpcEngineOptions): JsonRpcEngine;
27
29
  //# sourceMappingURL=engine.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"engine.d.mts","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAC1D,OAAO,KAAK,EAAE,0BAA0B,EAAE,wCAAwC;AAElF,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,KAAK,EAAE,eAAe,EAAE,0BAAsB;AACrD,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAItC,MAAM,MAAM,0BAA0B,GAAG;IACvC,KAAK,EAAE,KAAK,CAAC;IACb,KAAK,EAAE,eAAe,CAAC;IACvB,oBAAoB,EAAE,iBAAiB,CAAC,0BAA0B,EAAE,IAAI,CAAC,CAAC;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,KAAK,EACL,oBAAoB,EACpB,QAAoC,GACrC,EAAE,0BAA0B,iBAe5B"}
1
+ {"version":3,"file":"engine.d.mts","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAC1D,OAAO,KAAK,EAAE,0BAA0B,EAAE,wCAAwC;AAElF,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAG5C,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EAC1B,0BAAsB;AACvB,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAItC,MAAM,MAAM,0BAA0B,GAAG;IACvC,KAAK,EAAE,KAAK,CAAC;IACb,eAAe,EAAE,yBAAyB,CAAC;IAC3C,cAAc,EAAE,wBAAwB,CAAC;IACzC,oBAAoB,EAAE,iBAAiB,CAAC,0BAA0B,EAAE,IAAI,CAAC,CAAC;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,QAAoC,GACrC,EAAE,0BAA0B,iBAmB5B"}
@@ -12,16 +12,19 @@ import { createMockMiddleware } from "./mock.mjs";
12
12
  *
13
13
  * @param options - The options to use when creating the engine.
14
14
  * @param options.store - The Redux store to use.
15
- * @param options.hooks - Any hooks used by the middleware handlers.
15
+ * @param options.restrictedHooks - Any hooks used by the middleware handlers.
16
+ * @param options.permittedHooks - Any hooks used by the middleware handlers.
16
17
  * @param options.permissionMiddleware - The permission middleware to use.
17
18
  * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.
18
19
  * @returns A JSON-RPC engine.
19
20
  */
20
- export function createJsonRpcEngine({ store, hooks, permissionMiddleware, endpoint = DEFAULT_JSON_RPC_ENDPOINT, }) {
21
+ export function createJsonRpcEngine({ store, restrictedHooks, permittedHooks, permissionMiddleware, endpoint = DEFAULT_JSON_RPC_ENDPOINT, }) {
21
22
  const engine = new JsonRpcEngine();
22
23
  engine.push(createMockMiddleware(store));
23
- engine.push(createInternalMethodsMiddleware(hooks));
24
- engine.push(createSnapsMethodMiddleware(true, hooks));
24
+ // The hooks here do not match the hooks used by the clients, so this
25
+ // middleware should not be used outside of the simulation environment.
26
+ engine.push(createInternalMethodsMiddleware(restrictedHooks));
27
+ engine.push(createSnapsMethodMiddleware(true, permittedHooks));
25
28
  engine.push(permissionMiddleware);
26
29
  engine.push(createFetchMiddleware({
27
30
  btoa: globalThis.btoa,
@@ -1 +1 @@
1
- {"version":3,"file":"engine.mjs","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,0CAA0C;AAE1E,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAE1D,OAAO,EAAE,2BAA2B,EAAE,oCAAoC;AAG1E,OAAO,EAAE,yBAAyB,EAAE,yBAAqB;AAGzD,OAAO,EAAE,+BAA+B,EAAE,qCAA2B;AACrE,OAAO,EAAE,oBAAoB,EAAE,mBAAe;AAS9C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,KAAK,EACL,KAAK,EACL,oBAAoB,EACpB,QAAQ,GAAG,yBAAyB,GACT;IAC3B,MAAM,MAAM,GAAG,IAAI,aAAa,EAAE,CAAC;IACnC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IACzC,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,KAAK,CAAC,CAAC,CAAC;IACpD,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IACtD,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CACT,qBAAqB,CAAC;QACpB,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,MAAM,EAAE,QAAQ;KACjB,CAAC,CACH,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { createFetchMiddleware } from '@metamask/eth-json-rpc-middleware';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { RestrictedMethodParameters } from '@metamask/permission-controller';\nimport { createSnapsMethodMiddleware } from '@metamask/snaps-rpc-methods';\nimport type { Json } from '@metamask/utils';\n\nimport { DEFAULT_JSON_RPC_ENDPOINT } from '../constants';\nimport type { MiddlewareHooks } from '../simulation';\nimport type { Store } from '../store';\nimport { createInternalMethodsMiddleware } from './internal-methods';\nimport { createMockMiddleware } from './mock';\n\nexport type CreateJsonRpcEngineOptions = {\n store: Store;\n hooks: MiddlewareHooks;\n permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;\n endpoint?: string;\n};\n\n/**\n * Create a JSON-RPC engine for use in a simulated environment. This engine\n * should be used to handle all JSON-RPC requests. It is set up to handle\n * requests that would normally be handled internally by the MetaMask client, as\n * well as Snap-specific requests.\n *\n * @param options - The options to use when creating the engine.\n * @param options.store - The Redux store to use.\n * @param options.hooks - Any hooks used by the middleware handlers.\n * @param options.permissionMiddleware - The permission middleware to use.\n * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.\n * @returns A JSON-RPC engine.\n */\nexport function createJsonRpcEngine({\n store,\n hooks,\n permissionMiddleware,\n endpoint = DEFAULT_JSON_RPC_ENDPOINT,\n}: CreateJsonRpcEngineOptions) {\n const engine = new JsonRpcEngine();\n engine.push(createMockMiddleware(store));\n engine.push(createInternalMethodsMiddleware(hooks));\n engine.push(createSnapsMethodMiddleware(true, hooks));\n engine.push(permissionMiddleware);\n engine.push(\n createFetchMiddleware({\n btoa: globalThis.btoa,\n fetch: globalThis.fetch,\n rpcUrl: endpoint,\n }),\n );\n\n return engine;\n}\n"]}
1
+ {"version":3,"file":"engine.mjs","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,0CAA0C;AAE1E,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAE1D,OAAO,EAAE,2BAA2B,EAAE,oCAAoC;AAG1E,OAAO,EAAE,yBAAyB,EAAE,yBAAqB;AAMzD,OAAO,EAAE,+BAA+B,EAAE,qCAA2B;AACrE,OAAO,EAAE,oBAAoB,EAAE,mBAAe;AAU9C;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,KAAK,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,QAAQ,GAAG,yBAAyB,GACT;IAC3B,MAAM,MAAM,GAAG,IAAI,aAAa,EAAE,CAAC;IACnC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IAEzC,qEAAqE;IACrE,uEAAuE;IACvE,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,eAAe,CAAC,CAAC,CAAC;IAC9D,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC;IAE/D,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CACT,qBAAqB,CAAC;QACpB,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,MAAM,EAAE,QAAQ;KACjB,CAAC,CACH,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { createFetchMiddleware } from '@metamask/eth-json-rpc-middleware';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { RestrictedMethodParameters } from '@metamask/permission-controller';\nimport { createSnapsMethodMiddleware } from '@metamask/snaps-rpc-methods';\nimport type { Json } from '@metamask/utils';\n\nimport { DEFAULT_JSON_RPC_ENDPOINT } from '../constants';\nimport type {\n PermittedMiddlewareHooks,\n RestrictedMiddlewareHooks,\n} from '../simulation';\nimport type { Store } from '../store';\nimport { createInternalMethodsMiddleware } from './internal-methods';\nimport { createMockMiddleware } from './mock';\n\nexport type CreateJsonRpcEngineOptions = {\n store: Store;\n restrictedHooks: RestrictedMiddlewareHooks;\n permittedHooks: PermittedMiddlewareHooks;\n permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;\n endpoint?: string;\n};\n\n/**\n * Create a JSON-RPC engine for use in a simulated environment. This engine\n * should be used to handle all JSON-RPC requests. It is set up to handle\n * requests that would normally be handled internally by the MetaMask client, as\n * well as Snap-specific requests.\n *\n * @param options - The options to use when creating the engine.\n * @param options.store - The Redux store to use.\n * @param options.restrictedHooks - Any hooks used by the middleware handlers.\n * @param options.permittedHooks - Any hooks used by the middleware handlers.\n * @param options.permissionMiddleware - The permission middleware to use.\n * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.\n * @returns A JSON-RPC engine.\n */\nexport function createJsonRpcEngine({\n store,\n restrictedHooks,\n permittedHooks,\n permissionMiddleware,\n endpoint = DEFAULT_JSON_RPC_ENDPOINT,\n}: CreateJsonRpcEngineOptions) {\n const engine = new JsonRpcEngine();\n engine.push(createMockMiddleware(store));\n\n // The hooks here do not match the hooks used by the clients, so this\n // middleware should not be used outside of the simulation environment.\n engine.push(createInternalMethodsMiddleware(restrictedHooks));\n engine.push(createSnapsMethodMiddleware(true, permittedHooks));\n\n engine.push(permissionMiddleware);\n engine.push(\n createFetchMiddleware({\n btoa: globalThis.btoa,\n fetch: globalThis.fetch,\n rpcUrl: endpoint,\n }),\n );\n\n return engine;\n}\n"]}
package/dist/options.cjs CHANGED
@@ -10,6 +10,7 @@ const SimulationOptionsStruct = (0, superstruct_1.object)({
10
10
  locale: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.string)()), constants_1.DEFAULT_LOCALE),
11
11
  state: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.nullable)((0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct))), null),
12
12
  unencryptedState: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.nullable)((0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct))), null),
13
+ hideBalances: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.boolean)()), false),
13
14
  });
14
15
  /**
15
16
  * Get the options for the simulation.
@@ -1 +1 @@
1
- {"version":3,"file":"options.cjs","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":";;;AACA,uDAQ+B;AAC/B,2CAA6C;AAE7C,+CAA4E;AAE5E,MAAM,uBAAuB,GAAG,IAAA,oBAAM,EAAC;IACrC,QAAQ,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,4BAAgB,CAAC;IACzD,oBAAoB,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,uBAAW,CAAC;IAChE,MAAM,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,0BAAc,CAAC;IACrD,KAAK,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;IACxE,gBAAgB,EAAE,IAAA,uBAAS,EACzB,IAAA,sBAAQ,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC,EAChD,IAAI,CACL;CACF,CAAC,CAAC;AAoBH;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,OAA8B;IACvD,OAAO,IAAA,oBAAM,EACX,OAAO,EACP,uBAAuB,CACW,CAAC;AACvC,CAAC;AALD,gCAKC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n create,\n defaulted,\n nullable,\n object,\n optional,\n record,\n string,\n} from '@metamask/superstruct';\nimport { JsonStruct } from '@metamask/utils';\n\nimport { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from './constants';\n\nconst SimulationOptionsStruct = object({\n currency: defaulted(optional(string()), DEFAULT_CURRENCY),\n secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),\n locale: defaulted(optional(string()), DEFAULT_LOCALE),\n state: defaulted(optional(nullable(record(string(), JsonStruct))), null),\n unencryptedState: defaulted(\n optional(nullable(record(string(), JsonStruct))),\n null,\n ),\n});\n\n/**\n * Options for the simulation.\n *\n * @property secretRecoveryPhrase - The secret recovery phrase to use. This is\n * used to derive addresses and private keys. Defaults to a test recovery\n * phrase.\n * @property locale - The locale to use. Defaults to `en`.\n * @property state - The initial state of the Snap, if any. Defaults to `null`.\n */\nexport type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;\n\n/**\n * Options for the simulation, with defaults filled in.\n *\n * See {@link SimulationUserOptions} for documentation.\n */\nexport type SimulationOptions = Required<SimulationUserOptions>;\n\n/**\n * Get the options for the simulation.\n *\n * @param options - The user options. Any options not specified will be filled\n * in with default values.\n * @returns The simulation options.\n */\nexport function getOptions(options: SimulationUserOptions): SimulationOptions {\n return create(\n options,\n SimulationOptionsStruct,\n ) as Required<SimulationUserOptions>;\n}\n"]}
1
+ {"version":3,"file":"options.cjs","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":";;;AACA,uDAS+B;AAC/B,2CAA6C;AAE7C,+CAA4E;AAE5E,MAAM,uBAAuB,GAAG,IAAA,oBAAM,EAAC;IACrC,QAAQ,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,4BAAgB,CAAC;IACzD,oBAAoB,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,uBAAW,CAAC;IAChE,MAAM,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC,EAAE,0BAAc,CAAC;IACrD,KAAK,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;IACxE,gBAAgB,EAAE,IAAA,uBAAS,EACzB,IAAA,sBAAQ,EAAC,IAAA,sBAAQ,EAAC,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,CAAC,CAAC,CAAC,EAChD,IAAI,CACL;IACD,YAAY,EAAE,IAAA,uBAAS,EAAC,IAAA,sBAAQ,EAAC,IAAA,qBAAO,GAAE,CAAC,EAAE,KAAK,CAAC;CACpD,CAAC,CAAC;AAoBH;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,OAA8B;IACvD,OAAO,IAAA,oBAAM,EACX,OAAO,EACP,uBAAuB,CACW,CAAC;AACvC,CAAC;AALD,gCAKC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n boolean,\n create,\n defaulted,\n nullable,\n object,\n optional,\n record,\n string,\n} from '@metamask/superstruct';\nimport { JsonStruct } from '@metamask/utils';\n\nimport { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from './constants';\n\nconst SimulationOptionsStruct = object({\n currency: defaulted(optional(string()), DEFAULT_CURRENCY),\n secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),\n locale: defaulted(optional(string()), DEFAULT_LOCALE),\n state: defaulted(optional(nullable(record(string(), JsonStruct))), null),\n unencryptedState: defaulted(\n optional(nullable(record(string(), JsonStruct))),\n null,\n ),\n hideBalances: defaulted(optional(boolean()), false),\n});\n\n/**\n * Options for the simulation.\n *\n * @property secretRecoveryPhrase - The secret recovery phrase to use. This is\n * used to derive addresses and private keys. Defaults to a test recovery\n * phrase.\n * @property locale - The locale to use. Defaults to `en`.\n * @property state - The initial state of the Snap, if any. Defaults to `null`.\n */\nexport type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;\n\n/**\n * Options for the simulation, with defaults filled in.\n *\n * See {@link SimulationUserOptions} for documentation.\n */\nexport type SimulationOptions = Required<SimulationUserOptions>;\n\n/**\n * Get the options for the simulation.\n *\n * @param options - The user options. Any options not specified will be filled\n * in with default values.\n * @returns The simulation options.\n */\nexport function getOptions(options: SimulationUserOptions): SimulationOptions {\n return create(\n options,\n SimulationOptionsStruct,\n ) as Required<SimulationUserOptions>;\n}\n"]}
@@ -4,6 +4,7 @@ declare const SimulationOptionsStruct: import("@metamask/superstruct").Struct<{
4
4
  currency?: string | undefined;
5
5
  locale?: string | undefined;
6
6
  secretRecoveryPhrase?: string | undefined;
7
+ hideBalances?: boolean | undefined;
7
8
  unencryptedState?: Record<string, import("@metamask/utils").Json> | null | undefined;
8
9
  }, {
9
10
  currency: import("@metamask/superstruct").Struct<string | undefined, null>;
@@ -11,6 +12,7 @@ declare const SimulationOptionsStruct: import("@metamask/superstruct").Struct<{
11
12
  locale: import("@metamask/superstruct").Struct<string | undefined, null>;
12
13
  state: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
13
14
  unencryptedState: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
15
+ hideBalances: import("@metamask/superstruct").Struct<boolean | undefined, null>;
14
16
  }>;
15
17
  /**
16
18
  * Options for the simulation.
@@ -1 +1 @@
1
- {"version":3,"file":"options.d.cts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAcnD,QAAA,MAAM,uBAAuB;;;;;;;;;;;;EAS3B,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,qBAAqB,GAAG,iBAAiB,CAK5E"}
1
+ {"version":3,"file":"options.d.cts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAenD,QAAA,MAAM,uBAAuB;;;;;;;;;;;;;;EAU3B,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,qBAAqB,GAAG,iBAAiB,CAK5E"}
@@ -4,6 +4,7 @@ declare const SimulationOptionsStruct: import("@metamask/superstruct").Struct<{
4
4
  currency?: string | undefined;
5
5
  locale?: string | undefined;
6
6
  secretRecoveryPhrase?: string | undefined;
7
+ hideBalances?: boolean | undefined;
7
8
  unencryptedState?: Record<string, import("@metamask/utils").Json> | null | undefined;
8
9
  }, {
9
10
  currency: import("@metamask/superstruct").Struct<string | undefined, null>;
@@ -11,6 +12,7 @@ declare const SimulationOptionsStruct: import("@metamask/superstruct").Struct<{
11
12
  locale: import("@metamask/superstruct").Struct<string | undefined, null>;
12
13
  state: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
13
14
  unencryptedState: import("@metamask/superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
15
+ hideBalances: import("@metamask/superstruct").Struct<boolean | undefined, null>;
14
16
  }>;
15
17
  /**
16
18
  * Options for the simulation.
@@ -1 +1 @@
1
- {"version":3,"file":"options.d.mts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAcnD,QAAA,MAAM,uBAAuB;;;;;;;;;;;;EAS3B,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,qBAAqB,GAAG,iBAAiB,CAK5E"}
1
+ {"version":3,"file":"options.d.mts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAenD,QAAA,MAAM,uBAAuB;;;;;;;;;;;;;;EAU3B,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,qBAAqB,GAAG,iBAAiB,CAK5E"}
package/dist/options.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { create, defaulted, nullable, object, optional, record, string } from "@metamask/superstruct";
1
+ import { boolean, create, defaulted, nullable, object, optional, record, string } from "@metamask/superstruct";
2
2
  import { JsonStruct } from "@metamask/utils";
3
3
  import { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from "./constants.mjs";
4
4
  const SimulationOptionsStruct = object({
@@ -7,6 +7,7 @@ const SimulationOptionsStruct = object({
7
7
  locale: defaulted(optional(string()), DEFAULT_LOCALE),
8
8
  state: defaulted(optional(nullable(record(string(), JsonStruct))), null),
9
9
  unencryptedState: defaulted(optional(nullable(record(string(), JsonStruct))), null),
10
+ hideBalances: defaulted(optional(boolean()), false),
10
11
  });
11
12
  /**
12
13
  * Get the options for the simulation.
@@ -1 +1 @@
1
- {"version":3,"file":"options.mjs","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AACA,OAAO,EACL,MAAM,EACN,SAAS,EACT,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACP,8BAA8B;AAC/B,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAE7C,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,WAAW,EAAE,wBAAoB;AAE5E,MAAM,uBAAuB,GAAG,MAAM,CAAC;IACrC,QAAQ,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,gBAAgB,CAAC;IACzD,oBAAoB,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,WAAW,CAAC;IAChE,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,cAAc,CAAC;IACrD,KAAK,EAAE,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;IACxE,gBAAgB,EAAE,SAAS,CACzB,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,EAChD,IAAI,CACL;CACF,CAAC,CAAC;AAoBH;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,OAA8B;IACvD,OAAO,MAAM,CACX,OAAO,EACP,uBAAuB,CACW,CAAC;AACvC,CAAC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n create,\n defaulted,\n nullable,\n object,\n optional,\n record,\n string,\n} from '@metamask/superstruct';\nimport { JsonStruct } from '@metamask/utils';\n\nimport { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from './constants';\n\nconst SimulationOptionsStruct = object({\n currency: defaulted(optional(string()), DEFAULT_CURRENCY),\n secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),\n locale: defaulted(optional(string()), DEFAULT_LOCALE),\n state: defaulted(optional(nullable(record(string(), JsonStruct))), null),\n unencryptedState: defaulted(\n optional(nullable(record(string(), JsonStruct))),\n null,\n ),\n});\n\n/**\n * Options for the simulation.\n *\n * @property secretRecoveryPhrase - The secret recovery phrase to use. This is\n * used to derive addresses and private keys. Defaults to a test recovery\n * phrase.\n * @property locale - The locale to use. Defaults to `en`.\n * @property state - The initial state of the Snap, if any. Defaults to `null`.\n */\nexport type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;\n\n/**\n * Options for the simulation, with defaults filled in.\n *\n * See {@link SimulationUserOptions} for documentation.\n */\nexport type SimulationOptions = Required<SimulationUserOptions>;\n\n/**\n * Get the options for the simulation.\n *\n * @param options - The user options. Any options not specified will be filled\n * in with default values.\n * @returns The simulation options.\n */\nexport function getOptions(options: SimulationUserOptions): SimulationOptions {\n return create(\n options,\n SimulationOptionsStruct,\n ) as Required<SimulationUserOptions>;\n}\n"]}
1
+ {"version":3,"file":"options.mjs","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AACA,OAAO,EACL,OAAO,EACP,MAAM,EACN,SAAS,EACT,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACP,8BAA8B;AAC/B,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAE7C,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,WAAW,EAAE,wBAAoB;AAE5E,MAAM,uBAAuB,GAAG,MAAM,CAAC;IACrC,QAAQ,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,gBAAgB,CAAC;IACzD,oBAAoB,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,WAAW,CAAC;IAChE,MAAM,EAAE,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,cAAc,CAAC;IACrD,KAAK,EAAE,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;IACxE,gBAAgB,EAAE,SAAS,CACzB,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,EAChD,IAAI,CACL;IACD,YAAY,EAAE,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,EAAE,KAAK,CAAC;CACpD,CAAC,CAAC;AAoBH;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,OAA8B;IACvD,OAAO,MAAM,CACX,OAAO,EACP,uBAAuB,CACW,CAAC;AACvC,CAAC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n boolean,\n create,\n defaulted,\n nullable,\n object,\n optional,\n record,\n string,\n} from '@metamask/superstruct';\nimport { JsonStruct } from '@metamask/utils';\n\nimport { DEFAULT_CURRENCY, DEFAULT_LOCALE, DEFAULT_SRP } from './constants';\n\nconst SimulationOptionsStruct = object({\n currency: defaulted(optional(string()), DEFAULT_CURRENCY),\n secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),\n locale: defaulted(optional(string()), DEFAULT_LOCALE),\n state: defaulted(optional(nullable(record(string(), JsonStruct))), null),\n unencryptedState: defaulted(\n optional(nullable(record(string(), JsonStruct))),\n null,\n ),\n hideBalances: defaulted(optional(boolean()), false),\n});\n\n/**\n * Options for the simulation.\n *\n * @property secretRecoveryPhrase - The secret recovery phrase to use. This is\n * used to derive addresses and private keys. Defaults to a test recovery\n * phrase.\n * @property locale - The locale to use. Defaults to `en`.\n * @property state - The initial state of the Snap, if any. Defaults to `null`.\n */\nexport type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;\n\n/**\n * Options for the simulation, with defaults filled in.\n *\n * See {@link SimulationUserOptions} for documentation.\n */\nexport type SimulationOptions = Required<SimulationUserOptions>;\n\n/**\n * Get the options for the simulation.\n *\n * @param options - The user options. Any options not specified will be filled\n * in with default values.\n * @returns The simulation options.\n */\nexport function getOptions(options: SimulationUserOptions): SimulationOptions {\n return create(\n options,\n SimulationOptionsStruct,\n ) as Required<SimulationUserOptions>;\n}\n"]}
package/dist/request.cjs CHANGED
@@ -45,9 +45,10 @@ function handleRequest({ snapId, store, executionService, handler, controllerMes
45
45
  })
46
46
  .then(async (result) => {
47
47
  const notifications = (0, store_1.getNotifications)(store.getState());
48
+ const interfaceId = notifications[0]?.content;
48
49
  store.dispatch((0, store_1.clearNotifications)());
49
50
  try {
50
- const getInterfaceFn = await getInterfaceApi(result, snapId, controllerMessenger);
51
+ const getInterfaceFn = await getInterfaceApi(result, snapId, controllerMessenger, interfaceId);
51
52
  return {
52
53
  id: String(id),
53
54
  response: {
@@ -121,15 +122,17 @@ exports.getInterfaceFromResult = getInterfaceFromResult;
121
122
  * @param result - The handler result object.
122
123
  * @param snapId - The Snap ID.
123
124
  * @param controllerMessenger - The controller messenger.
125
+ * @param contentId - The id of the interface if it exists outside of the result.
124
126
  * @returns The content components if any.
125
127
  */
126
- async function getInterfaceApi(result, snapId, controllerMessenger) {
128
+ async function getInterfaceApi(result, snapId, controllerMessenger, contentId) {
127
129
  const interfaceId = await getInterfaceFromResult(result, snapId, controllerMessenger);
128
- if (interfaceId) {
130
+ const id = interfaceId ?? contentId;
131
+ if (id) {
129
132
  return () => {
130
- const { content } = controllerMessenger.call('SnapInterfaceController:getInterface', snapId, interfaceId);
133
+ const { content } = controllerMessenger.call('SnapInterfaceController:getInterface', snapId, id);
131
134
  const actions = (0, interface_1.getInterfaceActions)(snapId, controllerMessenger, {
132
- id: interfaceId,
135
+ id,
133
136
  content,
134
137
  });
135
138
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"request.cjs","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":";;;AACA,mDAK6B;AAE7B,uDAAoD;AACpD,uDAA2C;AAC3C,2CAKyB;AACzB,8CAA0C;AAG1C,+CAAgE;AAChE,6CAA+D;AAE/D,2CAA+C;AAiB/C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,EACP,OAAO,EAAE,EAAE,EAAE,GAAG,IAAA,gBAAM,GAAE,EAAE,MAAM,GAAG,qBAAqB,EAAE,GAAG,OAAO,EAAE,GACjD;IACrB,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,MAAM,IAAI,KAAK,CACb,4EAA4E,CAC7E,CAAC;IACJ,CAAC,CAAC;IAEF,MAAM,OAAO,GAAG,gBAAgB;SAC7B,gBAAgB,CAAC,MAAM,EAAE;QACxB,MAAM;QACN,OAAO;QACP,OAAO,EAAE;YACP,OAAO,EAAE,KAAK;YACd,EAAE,EAAE,CAAC;YACL,GAAG,OAAO;SACX;KACF,CAAC;SACD,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;QACrB,MAAM,aAAa,GAAG,IAAA,wBAAgB,EAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;QACzD,KAAK,CAAC,QAAQ,CAAC,IAAA,0BAAkB,GAAE,CAAC,CAAC;QAErC,IAAI,CAAC;YACH,MAAM,cAAc,GAAG,MAAM,eAAe,CAC1C,MAAM,EACN,MAAM,EACN,mBAAmB,CACpB,CAAC;YAEF,OAAO;gBACL,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC;gBACd,QAAQ,EAAE;oBACR,MAAM,EAAE,IAAA,mBAAW,EAAC,MAAM,CAAC;iBAC5B;gBACD,aAAa;gBACb,GAAG,CAAC,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,CAAC,CAAC;YAEH,OAAO;gBACL,OAAO;gBACP,GAAG,OAAO;aACX,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAhCD,0CAgCC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n type ComponentOrElement,\n ComponentOrElementStruct,\n type JsonRpcError,\n type SnapId,\n} from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport { unwrapError } from '@metamask/snaps-utils';\nimport { is } from '@metamask/superstruct';\nimport {\n assert,\n getSafeJson,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getInterface, getInterfaceActions } from './interface';\nimport { clearNotifications, getNotifications } from './store';\nimport type { RunSagaFunction, Store } from './store';\nimport { SnapResponseStruct } from './structs';\nimport type {\n RequestOptions,\n SnapHandlerInterface,\n SnapRequest,\n} from './types';\n\nexport type HandleRequestOptions = {\n snapId: SnapId;\n store: Store;\n executionService: AbstractExecutionService<unknown>;\n handler: HandlerType;\n controllerMessenger: RootControllerMessenger;\n runSaga: RunSagaFunction;\n request: RequestOptions;\n};\n\n/**\n * Send a JSON-RPC request to the Snap, and wrap the response in a\n * {@link SnapResponse} object.\n *\n * @param options - The request options.\n * @param options.snapId - The ID of the Snap to send the request to.\n * @param options.store - The Redux store.\n * @param options.executionService - The execution service to use to send the\n * request.\n * @param options.handler - The handler to use to send the request.\n * @param options.controllerMessenger - The controller messenger used to call actions.\n * @param options.runSaga - A function to run a saga outside the usual Redux\n * flow.\n * @param options.request - The request to send.\n * @param options.request.id - The ID of the request. If not provided, a random\n * ID will be generated.\n * @param options.request.origin - The origin of the request. Defaults to\n * `https://metamask.io`.\n * @returns The response, wrapped in a {@link SnapResponse} object.\n */\nexport function handleRequest({\n snapId,\n store,\n executionService,\n handler,\n controllerMessenger,\n runSaga,\n request: { id = nanoid(), origin = 'https://metamask.io', ...options },\n}: HandleRequestOptions): SnapRequest {\n const getInterfaceError = () => {\n throw new Error(\n 'Unable to get the interface from the Snap: The request to the Snap failed.',\n );\n };\n\n const promise = executionService\n .handleRpcRequest(snapId, {\n origin,\n handler,\n request: {\n jsonrpc: '2.0',\n id: 1,\n ...options,\n },\n })\n .then(async (result) => {\n const notifications = getNotifications(store.getState());\n store.dispatch(clearNotifications());\n\n try {\n const getInterfaceFn = await getInterfaceApi(\n result,\n snapId,\n controllerMessenger,\n );\n\n return {\n id: String(id),\n response: {\n result: getSafeJson(result),\n },\n notifications,\n ...(getInterfaceFn ? { getInterface: getInterfaceFn } : {}),\n };\n } catch (error) {\n const [unwrappedError] = unwrapError(error);\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }\n })\n .catch((error) => {\n const [unwrappedError] = unwrapError(error);\n\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }) as unknown as SnapRequest;\n\n promise.getInterface = async () => {\n const sagaPromise = runSaga(\n getInterface,\n runSaga,\n snapId,\n controllerMessenger,\n ).toPromise();\n const result = await Promise.race([promise, sagaPromise]);\n\n // If the request promise has resolved to an error, we should throw\n // instead of waiting for an interface that likely will never be displayed\n if (\n is(result, SnapResponseStruct) &&\n hasProperty(result.response, 'error')\n ) {\n throw new Error(\n `Unable to get the interface from the Snap: The returned interface may be invalid. The error message received was: ${\n (result.response.error as JsonRpcError).message\n }`,\n );\n }\n\n return await sagaPromise;\n };\n\n return promise;\n}\n\n/**\n * Get the interface ID from the result if it's available or create a new interface if the result contains static components.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The interface ID or undefined if the result doesn't include content.\n */\nexport async function getInterfaceFromResult(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n) {\n if (isPlainObject(result) && hasProperty(result, 'id')) {\n return result.id as string;\n }\n\n if (isPlainObject(result) && hasProperty(result, 'content')) {\n assert(\n is(result.content, ComponentOrElementStruct),\n 'The Snap returned an invalid interface.',\n );\n const id = await controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n result.content as ComponentOrElement,\n );\n\n return id;\n }\n\n return undefined;\n}\n\n/**\n * Get the response content from the `SnapInterfaceController` and include the\n * interaction methods.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The content components if any.\n */\nexport async function getInterfaceApi(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n): Promise<(() => SnapHandlerInterface) | undefined> {\n const interfaceId = await getInterfaceFromResult(\n result,\n snapId,\n controllerMessenger,\n );\n\n if (interfaceId) {\n return () => {\n const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n interfaceId,\n );\n\n const actions = getInterfaceActions(snapId, controllerMessenger, {\n id: interfaceId,\n content,\n });\n\n return {\n content,\n ...actions,\n };\n };\n }\n\n return undefined;\n}\n"]}
1
+ {"version":3,"file":"request.cjs","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":";;;AACA,mDAK6B;AAE7B,uDAAoD;AACpD,uDAA2C;AAC3C,2CAKyB;AACzB,8CAA0C;AAG1C,+CAAgE;AAChE,6CAA+D;AAE/D,2CAA+C;AAiB/C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,EACP,OAAO,EAAE,EAAE,EAAE,GAAG,IAAA,gBAAM,GAAE,EAAE,MAAM,GAAG,qBAAqB,EAAE,GAAG,OAAO,EAAE,GACjD;IACrB,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,MAAM,IAAI,KAAK,CACb,4EAA4E,CAC7E,CAAC;IACJ,CAAC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,IAAA,gBAAE,EAAC,MAAM,CAAC,OAAO,EAAE,oCAAwB,CAAC,EAC5C,yCAAyC,CAC1C,CAAC;QAEF,MAAM,EAAE,GAAG,MAAM,mBAAmB,CAAC,IAAI,CACvC,yCAAyC,EACzC,MAAM,EACN,MAAM,CAAC,OAA6B,CACrC,CAAC;QAEF,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAzBD,wDAyBC;AAED;;;;;;;;;GASG;AACI,KAAK,UAAU,eAAe,CACnC,MAAe,EACf,MAAc,EACd,mBAA4C,EAC5C,SAAkB;IAElB,MAAM,WAAW,GAAG,MAAM,sBAAsB,CAC9C,MAAM,EACN,MAAM,EACN,mBAAmB,CACpB,CAAC;IAEF,MAAM,EAAE,GAAG,WAAW,IAAI,SAAS,CAAC;IAEpC,IAAI,EAAE,EAAE,CAAC;QACP,OAAO,GAAG,EAAE;YACV,MAAM,EAAE,OAAO,EAAE,GAAG,mBAAmB,CAAC,IAAI,CAC1C,sCAAsC,EACtC,MAAM,EACN,EAAE,CACH,CAAC;YAEF,MAAM,OAAO,GAAG,IAAA,+BAAmB,EAAC,MAAM,EAAE,mBAAmB,EAAE;gBAC/D,EAAE;gBACF,OAAO;aACR,CAAC,CAAC;YAEH,OAAO;gBACL,OAAO;gBACP,GAAG,OAAO;aACX,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAnCD,0CAmCC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n type ComponentOrElement,\n ComponentOrElementStruct,\n type JsonRpcError,\n type SnapId,\n} from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport { unwrapError } from '@metamask/snaps-utils';\nimport { is } from '@metamask/superstruct';\nimport {\n assert,\n getSafeJson,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getInterface, getInterfaceActions } from './interface';\nimport { clearNotifications, getNotifications } from './store';\nimport type { RunSagaFunction, Store } from './store';\nimport { SnapResponseStruct } from './structs';\nimport type {\n RequestOptions,\n SnapHandlerInterface,\n SnapRequest,\n} from './types';\n\nexport type HandleRequestOptions = {\n snapId: SnapId;\n store: Store;\n executionService: AbstractExecutionService<unknown>;\n handler: HandlerType;\n controllerMessenger: RootControllerMessenger;\n runSaga: RunSagaFunction;\n request: RequestOptions;\n};\n\n/**\n * Send a JSON-RPC request to the Snap, and wrap the response in a\n * {@link SnapResponse} object.\n *\n * @param options - The request options.\n * @param options.snapId - The ID of the Snap to send the request to.\n * @param options.store - The Redux store.\n * @param options.executionService - The execution service to use to send the\n * request.\n * @param options.handler - The handler to use to send the request.\n * @param options.controllerMessenger - The controller messenger used to call actions.\n * @param options.runSaga - A function to run a saga outside the usual Redux\n * flow.\n * @param options.request - The request to send.\n * @param options.request.id - The ID of the request. If not provided, a random\n * ID will be generated.\n * @param options.request.origin - The origin of the request. Defaults to\n * `https://metamask.io`.\n * @returns The response, wrapped in a {@link SnapResponse} object.\n */\nexport function handleRequest({\n snapId,\n store,\n executionService,\n handler,\n controllerMessenger,\n runSaga,\n request: { id = nanoid(), origin = 'https://metamask.io', ...options },\n}: HandleRequestOptions): SnapRequest {\n const getInterfaceError = () => {\n throw new Error(\n 'Unable to get the interface from the Snap: The request to the Snap failed.',\n );\n };\n\n const promise = executionService\n .handleRpcRequest(snapId, {\n origin,\n handler,\n request: {\n jsonrpc: '2.0',\n id: 1,\n ...options,\n },\n })\n .then(async (result) => {\n const notifications = getNotifications(store.getState());\n const interfaceId = notifications[0]?.content;\n store.dispatch(clearNotifications());\n\n try {\n const getInterfaceFn = await getInterfaceApi(\n result,\n snapId,\n controllerMessenger,\n interfaceId,\n );\n\n return {\n id: String(id),\n response: {\n result: getSafeJson(result),\n },\n notifications,\n ...(getInterfaceFn ? { getInterface: getInterfaceFn } : {}),\n };\n } catch (error) {\n const [unwrappedError] = unwrapError(error);\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }\n })\n .catch((error) => {\n const [unwrappedError] = unwrapError(error);\n\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }) as unknown as SnapRequest;\n\n promise.getInterface = async () => {\n const sagaPromise = runSaga(\n getInterface,\n runSaga,\n snapId,\n controllerMessenger,\n ).toPromise();\n const result = await Promise.race([promise, sagaPromise]);\n\n // If the request promise has resolved to an error, we should throw\n // instead of waiting for an interface that likely will never be displayed\n if (\n is(result, SnapResponseStruct) &&\n hasProperty(result.response, 'error')\n ) {\n throw new Error(\n `Unable to get the interface from the Snap: The returned interface may be invalid. The error message received was: ${\n (result.response.error as JsonRpcError).message\n }`,\n );\n }\n\n return await sagaPromise;\n };\n\n return promise;\n}\n\n/**\n * Get the interface ID from the result if it's available or create a new interface if the result contains static components.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The interface ID or undefined if the result doesn't include content.\n */\nexport async function getInterfaceFromResult(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n) {\n if (isPlainObject(result) && hasProperty(result, 'id')) {\n return result.id as string;\n }\n\n if (isPlainObject(result) && hasProperty(result, 'content')) {\n assert(\n is(result.content, ComponentOrElementStruct),\n 'The Snap returned an invalid interface.',\n );\n\n const id = await controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n result.content as ComponentOrElement,\n );\n\n return id;\n }\n\n return undefined;\n}\n\n/**\n * Get the response content from the `SnapInterfaceController` and include the\n * interaction methods.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @param contentId - The id of the interface if it exists outside of the result.\n * @returns The content components if any.\n */\nexport async function getInterfaceApi(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n contentId?: string,\n): Promise<(() => SnapHandlerInterface) | undefined> {\n const interfaceId = await getInterfaceFromResult(\n result,\n snapId,\n controllerMessenger,\n );\n\n const id = interfaceId ?? contentId;\n\n if (id) {\n return () => {\n const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n id,\n );\n\n const actions = getInterfaceActions(snapId, controllerMessenger, {\n id,\n content,\n });\n\n return {\n content,\n ...actions,\n };\n };\n }\n\n return undefined;\n}\n"]}
@@ -50,7 +50,8 @@ export declare function getInterfaceFromResult(result: unknown, snapId: SnapId,
50
50
  * @param result - The handler result object.
51
51
  * @param snapId - The Snap ID.
52
52
  * @param controllerMessenger - The controller messenger.
53
+ * @param contentId - The id of the interface if it exists outside of the result.
53
54
  * @returns The content components if any.
54
55
  */
55
- export declare function getInterfaceApi(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger): Promise<(() => SnapHandlerInterface) | undefined>;
56
+ export declare function getInterfaceApi(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger, contentId?: string): Promise<(() => SnapHandlerInterface) | undefined>;
56
57
  //# sourceMappingURL=request.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"request.d.cts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,oCAAoC;AAC5E,OAAO,EAIL,KAAK,MAAM,EACZ,4BAA4B;AAC7B,OAAO,KAAK,EAAE,WAAW,EAAE,8BAA8B;AAWzD,OAAO,KAAK,EAAE,uBAAuB,EAAE,0BAAsB;AAG7D,OAAO,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE,0BAAgB;AAEtD,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACpB,WAAW,EACZ,oBAAgB;AAEjB,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;IACb,gBAAgB,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AAC,OAAO,CAAC,CAAC;IACpD,OAAO,EAAE,WAAW,CAAC;IACrB,mBAAmB,EAAE,uBAAuB,CAAC;IAC7C,OAAO,EAAE,eAAe,CAAC;IACzB,OAAO,EAAE,cAAc,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,EACP,OAAO,EAAE,EAAE,EAAa,EAAE,MAA8B,EAAE,GAAG,OAAO,EAAE,GACvE,EAAE,oBAAoB,GAAG,WAAW,CAyFpC;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,uBAAuB,+BAsB7C;AAED;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,uBAAuB,EAC5C,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,CAAC,MAAM,oBAAoB,CAAC,GAAG,SAAS,CAAC,CA8BnD"}
@@ -50,7 +50,8 @@ export declare function getInterfaceFromResult(result: unknown, snapId: SnapId,
50
50
  * @param result - The handler result object.
51
51
  * @param snapId - The Snap ID.
52
52
  * @param controllerMessenger - The controller messenger.
53
+ * @param contentId - The id of the interface if it exists outside of the result.
53
54
  * @returns The content components if any.
54
55
  */
55
- export declare function getInterfaceApi(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger): Promise<(() => SnapHandlerInterface) | undefined>;
56
+ export declare function getInterfaceApi(result: unknown, snapId: SnapId, controllerMessenger: RootControllerMessenger, contentId?: string): Promise<(() => SnapHandlerInterface) | undefined>;
56
57
  //# sourceMappingURL=request.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"request.d.mts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":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contro
43
43
  })
44
44
  .then(async (result) => {
45
45
  const notifications = getNotifications(store.getState());
46
+ const interfaceId = notifications[0]?.content;
46
47
  store.dispatch(clearNotifications());
47
48
  try {
48
- const getInterfaceFn = await getInterfaceApi(result, snapId, controllerMessenger);
49
+ const getInterfaceFn = await getInterfaceApi(result, snapId, controllerMessenger, interfaceId);
49
50
  return {
50
51
  id: String(id),
51
52
  response: {
@@ -117,15 +118,17 @@ export async function getInterfaceFromResult(result, snapId, controllerMessenger
117
118
  * @param result - The handler result object.
118
119
  * @param snapId - The Snap ID.
119
120
  * @param controllerMessenger - The controller messenger.
121
+ * @param contentId - The id of the interface if it exists outside of the result.
120
122
  * @returns The content components if any.
121
123
  */
122
- export async function getInterfaceApi(result, snapId, controllerMessenger) {
124
+ export async function getInterfaceApi(result, snapId, controllerMessenger, contentId) {
123
125
  const interfaceId = await getInterfaceFromResult(result, snapId, controllerMessenger);
124
- if (interfaceId) {
126
+ const id = interfaceId ?? contentId;
127
+ if (id) {
125
128
  return () => {
126
- const { content } = controllerMessenger.call('SnapInterfaceController:getInterface', snapId, interfaceId);
129
+ const { content } = controllerMessenger.call('SnapInterfaceController:getInterface', snapId, id);
127
130
  const actions = getInterfaceActions(snapId, controllerMessenger, {
128
- id: interfaceId,
131
+ id,
129
132
  content,
130
133
  });
131
134
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"request.mjs","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,wBAAwB,EAGzB,4BAA4B;AAE7B,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,EAAE,EAAE,8BAA8B;AAC3C,OAAO,EACL,MAAM,EACN,WAAW,EACX,WAAW,EACX,aAAa,EACd,wBAAwB;;;AAIzB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,wBAAoB;AAChE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,0BAAgB;AAE/D,OAAO,EAAE,kBAAkB,EAAE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,EAAE,IAAI,CAAC,EAAE,CAAC;QACvD,OAAO,MAAM,CAAC,EAAY,CAAC;IAC7B,CAAC;IAED,IAAI,aAAa,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC;QAC5D,MAAM,CACJ,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,wBAAwB,CAAC,EAC5C,yCAAyC,CAC1C,CAAC;QACF,MAAM,EAAE,GAAG,MAAM,mBAAmB,CAAC,IAAI,CACvC,yCAAyC,EACzC,MAAM,EACN,MAAM,CAAC,OAA6B,CACrC,CAAC;QAEF,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAe,EACf,MAAc,EACd,mBAA4C;IAE5C,MAAM,WAAW,GAAG,MAAM,sBAAsB,CAC9C,MAAM,EACN,MAAM,EACN,mBAAmB,CACpB,CAAC;IAEF,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,GAAG,EAAE;YACV,MAAM,EAAE,OAAO,EAAE,GAAG,mBAAmB,CAAC,IAAI,CAC1C,sCAAsC,EACtC,MAAM,EACN,WAAW,CACZ,CAAC;YAEF,MAAM,OAAO,GAAG,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,EAAE;gBAC/D,EAAE,EAAE,WAAW;gBACf,OAAO;aACR,CAAC,CAAC;YAEH,OAAO;gBACL,OAAO;gBACP,GAAG,OAAO;aACX,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n type ComponentOrElement,\n ComponentOrElementStruct,\n type JsonRpcError,\n type SnapId,\n} from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport { unwrapError } from '@metamask/snaps-utils';\nimport { is } from '@metamask/superstruct';\nimport {\n assert,\n getSafeJson,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getInterface, getInterfaceActions } from './interface';\nimport { clearNotifications, getNotifications } from './store';\nimport type { RunSagaFunction, Store } from './store';\nimport { SnapResponseStruct } from './structs';\nimport type {\n RequestOptions,\n SnapHandlerInterface,\n SnapRequest,\n} from './types';\n\nexport type HandleRequestOptions = {\n snapId: SnapId;\n store: Store;\n executionService: AbstractExecutionService<unknown>;\n handler: HandlerType;\n controllerMessenger: RootControllerMessenger;\n runSaga: RunSagaFunction;\n request: RequestOptions;\n};\n\n/**\n * Send a JSON-RPC request to the Snap, and wrap the response in a\n * {@link SnapResponse} object.\n *\n * @param options - The request options.\n * @param options.snapId - The ID of the Snap to send the request to.\n * @param options.store - The Redux store.\n * @param options.executionService - The execution service to use to send the\n * request.\n * @param options.handler - The handler to use to send the request.\n * @param options.controllerMessenger - The controller messenger used to call actions.\n * @param options.runSaga - A function to run a saga outside the usual Redux\n * flow.\n * @param options.request - The request to send.\n * @param options.request.id - The ID of the request. If not provided, a random\n * ID will be generated.\n * @param options.request.origin - The origin of the request. Defaults to\n * `https://metamask.io`.\n * @returns The response, wrapped in a {@link SnapResponse} object.\n */\nexport function handleRequest({\n snapId,\n store,\n executionService,\n handler,\n controllerMessenger,\n runSaga,\n request: { id = nanoid(), origin = 'https://metamask.io', ...options },\n}: HandleRequestOptions): SnapRequest {\n const getInterfaceError = () => {\n throw new Error(\n 'Unable to get the interface from the Snap: The request to the Snap failed.',\n );\n };\n\n const promise = executionService\n .handleRpcRequest(snapId, {\n origin,\n handler,\n request: {\n jsonrpc: '2.0',\n id: 1,\n ...options,\n },\n })\n .then(async (result) => {\n const notifications = getNotifications(store.getState());\n store.dispatch(clearNotifications());\n\n try {\n const getInterfaceFn = await getInterfaceApi(\n result,\n snapId,\n controllerMessenger,\n );\n\n return {\n id: String(id),\n response: {\n result: getSafeJson(result),\n },\n notifications,\n ...(getInterfaceFn ? { getInterface: getInterfaceFn } : {}),\n };\n } catch (error) {\n const [unwrappedError] = unwrapError(error);\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }\n })\n .catch((error) => {\n const [unwrappedError] = unwrapError(error);\n\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }) as unknown as SnapRequest;\n\n promise.getInterface = async () => {\n const sagaPromise = runSaga(\n getInterface,\n runSaga,\n snapId,\n controllerMessenger,\n ).toPromise();\n const result = await Promise.race([promise, sagaPromise]);\n\n // If the request promise has resolved to an error, we should throw\n // instead of waiting for an interface that likely will never be displayed\n if (\n is(result, SnapResponseStruct) &&\n hasProperty(result.response, 'error')\n ) {\n throw new Error(\n `Unable to get the interface from the Snap: The returned interface may be invalid. The error message received was: ${\n (result.response.error as JsonRpcError).message\n }`,\n );\n }\n\n return await sagaPromise;\n };\n\n return promise;\n}\n\n/**\n * Get the interface ID from the result if it's available or create a new interface if the result contains static components.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The interface ID or undefined if the result doesn't include content.\n */\nexport async function getInterfaceFromResult(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n) {\n if (isPlainObject(result) && hasProperty(result, 'id')) {\n return result.id as string;\n }\n\n if (isPlainObject(result) && hasProperty(result, 'content')) {\n assert(\n is(result.content, ComponentOrElementStruct),\n 'The Snap returned an invalid interface.',\n );\n const id = await controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n result.content as ComponentOrElement,\n );\n\n return id;\n }\n\n return undefined;\n}\n\n/**\n * Get the response content from the `SnapInterfaceController` and include the\n * interaction methods.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The content components if any.\n */\nexport async function getInterfaceApi(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n): Promise<(() => SnapHandlerInterface) | undefined> {\n const interfaceId = await getInterfaceFromResult(\n result,\n snapId,\n controllerMessenger,\n );\n\n if (interfaceId) {\n return () => {\n const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n interfaceId,\n );\n\n const actions = getInterfaceActions(snapId, controllerMessenger, {\n id: interfaceId,\n content,\n });\n\n return {\n content,\n ...actions,\n };\n };\n }\n\n return undefined;\n}\n"]}
1
+ {"version":3,"file":"request.mjs","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,wBAAwB,EAGzB,4BAA4B;AAE7B,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,EAAE,EAAE,8BAA8B;AAC3C,OAAO,EACL,MAAM,EACN,WAAW,EACX,WAAW,EACX,aAAa,EACd,wBAAwB;;;AAIzB,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,wBAAoB;AAChE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,0BAAgB;AAE/D,OAAO,EAAE,kBAAkB,EAAE,sBAAkB;AAiB/C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAAC,EAC5B,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,EACP,OAAO,EAAE,EAAE,EAAE,GAAG,MAAM,EAAE,EAAE,MAAM,GAAG,qBAAqB,EAAE,GAAG,OAAO,EAAE,GACjD;IACrB,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,MAAM,IAAI,KAAK,CACb,4EAA4E,CAC7E,CAAC;IACJ,CAAC,CAAC;IAEF,MAAM,OAAO,GAAG,gBAAgB;SAC7B,gBAAgB,CAAC,MAAM,EAAE;QACxB,MAAM;QACN,OAAO;QACP,OAAO,EAAE;YACP,OAAO,EAAE,K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ACZ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAe,EACf,MAAc,EACd,mBAA4C,EAC5C,SAAkB;IAElB,MAAM,WAAW,GAAG,MAAM,sBAAsB,CAC9C,MAAM,EACN,MAAM,EACN,mBAAmB,CACpB,CAAC;IAEF,MAAM,EAAE,GAAG,WAAW,IAAI,SAAS,CAAC;IAEpC,IAAI,EAAE,EAAE,CAAC;QACP,OAAO,GAAG,EAAE;YACV,MAAM,EAAE,OAAO,EAAE,GAAG,mBAAmB,CAAC,IAAI,CAC1C,sCAAsC,EACtC,MAAM,EACN,EAAE,CACH,CAAC;YAEF,MAAM,OAAO,GAAG,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,EAAE;gBAC/D,EAAE;gBACF,OAAO;aACR,CAAC,CAAC;YAEH,OAAO;gBACL,OAAO;gBACP,GAAG,OAAO;aACX,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n type ComponentOrElement,\n ComponentOrElementStruct,\n type JsonRpcError,\n type SnapId,\n} from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport { unwrapError } from '@metamask/snaps-utils';\nimport { is } from '@metamask/superstruct';\nimport {\n assert,\n getSafeJson,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getInterface, getInterfaceActions } from './interface';\nimport { clearNotifications, getNotifications } from './store';\nimport type { RunSagaFunction, Store } from './store';\nimport { SnapResponseStruct } from './structs';\nimport type {\n RequestOptions,\n SnapHandlerInterface,\n SnapRequest,\n} from './types';\n\nexport type HandleRequestOptions = {\n snapId: SnapId;\n store: Store;\n executionService: AbstractExecutionService<unknown>;\n handler: HandlerType;\n controllerMessenger: RootControllerMessenger;\n runSaga: RunSagaFunction;\n request: RequestOptions;\n};\n\n/**\n * Send a JSON-RPC request to the Snap, and wrap the response in a\n * {@link SnapResponse} object.\n *\n * @param options - The request options.\n * @param options.snapId - The ID of the Snap to send the request to.\n * @param options.store - The Redux store.\n * @param options.executionService - The execution service to use to send the\n * request.\n * @param options.handler - The handler to use to send the request.\n * @param options.controllerMessenger - The controller messenger used to call actions.\n * @param options.runSaga - A function to run a saga outside the usual Redux\n * flow.\n * @param options.request - The request to send.\n * @param options.request.id - The ID of the request. If not provided, a random\n * ID will be generated.\n * @param options.request.origin - The origin of the request. Defaults to\n * `https://metamask.io`.\n * @returns The response, wrapped in a {@link SnapResponse} object.\n */\nexport function handleRequest({\n snapId,\n store,\n executionService,\n handler,\n controllerMessenger,\n runSaga,\n request: { id = nanoid(), origin = 'https://metamask.io', ...options },\n}: HandleRequestOptions): SnapRequest {\n const getInterfaceError = () => {\n throw new Error(\n 'Unable to get the interface from the Snap: The request to the Snap failed.',\n );\n };\n\n const promise = executionService\n .handleRpcRequest(snapId, {\n origin,\n handler,\n request: {\n jsonrpc: '2.0',\n id: 1,\n ...options,\n },\n })\n .then(async (result) => {\n const notifications = getNotifications(store.getState());\n const interfaceId = notifications[0]?.content;\n store.dispatch(clearNotifications());\n\n try {\n const getInterfaceFn = await getInterfaceApi(\n result,\n snapId,\n controllerMessenger,\n interfaceId,\n );\n\n return {\n id: String(id),\n response: {\n result: getSafeJson(result),\n },\n notifications,\n ...(getInterfaceFn ? { getInterface: getInterfaceFn } : {}),\n };\n } catch (error) {\n const [unwrappedError] = unwrapError(error);\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }\n })\n .catch((error) => {\n const [unwrappedError] = unwrapError(error);\n\n return {\n id: String(id),\n response: {\n error: unwrappedError.serialize(),\n },\n notifications: [],\n getInterface: getInterfaceError,\n };\n }) as unknown as SnapRequest;\n\n promise.getInterface = async () => {\n const sagaPromise = runSaga(\n getInterface,\n runSaga,\n snapId,\n controllerMessenger,\n ).toPromise();\n const result = await Promise.race([promise, sagaPromise]);\n\n // If the request promise has resolved to an error, we should throw\n // instead of waiting for an interface that likely will never be displayed\n if (\n is(result, SnapResponseStruct) &&\n hasProperty(result.response, 'error')\n ) {\n throw new Error(\n `Unable to get the interface from the Snap: The returned interface may be invalid. The error message received was: ${\n (result.response.error as JsonRpcError).message\n }`,\n );\n }\n\n return await sagaPromise;\n };\n\n return promise;\n}\n\n/**\n * Get the interface ID from the result if it's available or create a new interface if the result contains static components.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The interface ID or undefined if the result doesn't include content.\n */\nexport async function getInterfaceFromResult(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n) {\n if (isPlainObject(result) && hasProperty(result, 'id')) {\n return result.id as string;\n }\n\n if (isPlainObject(result) && hasProperty(result, 'content')) {\n assert(\n is(result.content, ComponentOrElementStruct),\n 'The Snap returned an invalid interface.',\n );\n\n const id = await controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n result.content as ComponentOrElement,\n );\n\n return id;\n }\n\n return undefined;\n}\n\n/**\n * Get the response content from the `SnapInterfaceController` and include the\n * interaction methods.\n *\n * @param result - The handler result object.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @param contentId - The id of the interface if it exists outside of the result.\n * @returns The content components if any.\n */\nexport async function getInterfaceApi(\n result: unknown,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n contentId?: string,\n): Promise<(() => SnapHandlerInterface) | undefined> {\n const interfaceId = await getInterfaceFromResult(\n result,\n snapId,\n controllerMessenger,\n );\n\n const id = interfaceId ?? contentId;\n\n if (id) {\n return () => {\n const { content } = controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n id,\n );\n\n const actions = getInterfaceActions(snapId, controllerMessenger, {\n id,\n content,\n });\n\n return {\n content,\n ...actions,\n };\n };\n }\n\n return undefined;\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.registerActions = exports.getHooks = exports.installSnap = void 0;
3
+ exports.registerActions = exports.getPermittedHooks = exports.getRestrictedHooks = exports.installSnap = void 0;
4
4
  const base_controller_1 = require("@metamask/base-controller");
5
5
  const json_rpc_middleware_stream_1 = require("@metamask/json-rpc-middleware-stream");
6
6
  const key_tree_1 = require("@metamask/key-tree");
@@ -15,6 +15,7 @@ const files_1 = require("./files.cjs");
15
15
  const helpers_1 = require("./helpers.cjs");
16
16
  const interface_1 = require("./interface.cjs");
17
17
  const methods_1 = require("./methods/index.cjs");
18
+ const hooks_1 = require("./methods/hooks/index.cjs");
18
19
  const middleware_1 = require("./middleware/index.cjs");
19
20
  const options_1 = require("./options.cjs");
20
21
  const store_1 = require("./store/index.cjs");
@@ -45,16 +46,18 @@ async function installSnap(snapId, { executionService, executionServiceOptions,
45
46
  const controllerMessenger = new base_controller_1.ControllerMessenger();
46
47
  registerActions(controllerMessenger, runSaga);
47
48
  // Set up controllers and JSON-RPC stack.
48
- const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);
49
+ const restrictedHooks = getRestrictedHooks(options);
50
+ const permittedHooks = getPermittedHooks(snapId, snapFiles, controllerMessenger, runSaga);
49
51
  const { subjectMetadataController, permissionController } = (0, controllers_1.getControllers)({
50
52
  controllerMessenger,
51
- hooks,
53
+ hooks: restrictedHooks,
52
54
  runSaga,
53
55
  options,
54
56
  });
55
57
  const engine = (0, middleware_1.createJsonRpcEngine)({
56
58
  store,
57
- hooks,
59
+ restrictedHooks,
60
+ permittedHooks,
58
61
  permissionMiddleware: permissionController.createPermissionMiddleware({
59
62
  origin: snapId,
60
63
  }),
@@ -115,25 +118,42 @@ exports.installSnap = installSnap;
115
118
  * Get the hooks for the simulation.
116
119
  *
117
120
  * @param options - The simulation options.
118
- * @param snapFiles - The Snap files.
119
- * @param snapId - The Snap ID.
120
- * @param controllerMessenger - The controller messenger.
121
121
  * @returns The hooks for the simulation.
122
122
  */
123
- function getHooks(options, snapFiles, snapId, controllerMessenger) {
123
+ function getRestrictedHooks(options) {
124
124
  return {
125
125
  getMnemonic: async () => Promise.resolve((0, key_tree_1.mnemonicPhraseToBytes)(options.secretRecoveryPhrase)),
126
- getSnapFile: async (path, encoding) => await (0, files_1.getSnapFile)(snapFiles.auxiliaryFiles, path, encoding),
127
126
  getIsLocked: () => false,
128
127
  getClientCryptography: () => ({}),
128
+ };
129
+ }
130
+ exports.getRestrictedHooks = getRestrictedHooks;
131
+ /**
132
+ * Get the permitted hooks for the simulation.
133
+ *
134
+ * @param snapId - The ID of the Snap.
135
+ * @param snapFiles - The fetched Snap files.
136
+ * @param controllerMessenger - The controller messenger.
137
+ * @param runSaga - The run saga function.
138
+ * @returns The permitted hooks for the simulation.
139
+ */
140
+ function getPermittedHooks(snapId, snapFiles, controllerMessenger, runSaga) {
141
+ return {
142
+ hasPermission: () => true,
143
+ getUnlockPromise: (0, methods_1.asyncResolve)(),
144
+ getIsLocked: () => false,
145
+ getSnapFile: async (path, encoding) => await (0, files_1.getSnapFile)(snapFiles.auxiliaryFiles, path, encoding),
129
146
  createInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:createInterface', snapId, ...args),
130
147
  updateInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:updateInterface', snapId, ...args),
131
148
  getInterfaceState: (...args) => controllerMessenger.call('SnapInterfaceController:getInterface', snapId, ...args).state,
132
149
  getInterfaceContext: (...args) => controllerMessenger.call('SnapInterfaceController:getInterface', snapId, ...args).context,
133
150
  resolveInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:resolveInterface', snapId, ...args),
151
+ getSnapState: (0, hooks_1.getPermittedGetSnapStateMethodImplementation)(runSaga),
152
+ updateSnapState: (0, hooks_1.getPermittedUpdateSnapStateMethodImplementation)(runSaga),
153
+ clearSnapState: (0, hooks_1.getPermittedClearSnapStateMethodImplementation)(runSaga),
134
154
  };
135
155
  }
136
- exports.getHooks = getHooks;
156
+ exports.getPermittedHooks = getPermittedHooks;
137
157
  /**
138
158
  * Register mocked action handlers.
139
159
  *