@metamask/snaps-simulation 1.4.1 → 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 +24 -1
  2. package/dist/controllers.cjs.map +1 -1
  3. package/dist/controllers.d.cts +5 -4
  4. package/dist/controllers.d.cts.map +1 -1
  5. package/dist/controllers.d.mts +5 -4
  6. package/dist/controllers.d.mts.map +1 -1
  7. package/dist/controllers.mjs.map +1 -1
  8. package/dist/helpers.cjs +17 -0
  9. package/dist/helpers.cjs.map +1 -1
  10. package/dist/helpers.d.cts +15 -0
  11. package/dist/helpers.d.cts.map +1 -1
  12. package/dist/helpers.d.mts +15 -0
  13. package/dist/helpers.d.mts.map +1 -1
  14. package/dist/helpers.mjs +17 -0
  15. package/dist/helpers.mjs.map +1 -1
  16. package/dist/interface.cjs +39 -1
  17. package/dist/interface.cjs.map +1 -1
  18. package/dist/interface.d.cts +12 -2
  19. package/dist/interface.d.cts.map +1 -1
  20. package/dist/interface.d.mts +12 -2
  21. package/dist/interface.d.mts.map +1 -1
  22. package/dist/interface.mjs +41 -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 +25 -0
  124. package/dist/types.d.cts.map +1 -1
  125. package/dist/types.d.mts +25 -0
  126. package/dist/types.d.mts.map +1 -1
  127. package/dist/types.mjs.map +1 -1
  128. package/package.json +15 -14
@@ -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
  *
@@ -1 +1 @@
1
- {"version":3,"file":"simulation.cjs","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":";;;AAIA,+DAAgE;AAChE,qFAA0E;AAC1E,iDAG4B;AAC5B,uEAA2E;AAE3E,2DAK0C;AAC1C,mEAAoE;AASpE,uDAAiD;AAGjD,qDAA2C;AAE3C,gDAA4C;AAG5C,mDAA6D;AAC7D,uCAAsC;AAEtC,2CAAuC;AACvC,+CAA8C;AAC9C,iDAA0C;AAC1C,uDAAmD;AAEnD,2CAAuC;AAEvC,6CAA2D;AA+F3D;;;;;;;;;;;;;;GAcG;AACI,KAAK,UAAU,WAAW,CAK/B,MAAc,EACd,EACE,gBAAgB,EAChB,uBAAuB,EACvB,OAAO,EAAE,UAAU,GAAG,EAAE,MACgB,EAAE;IAE5C,MAAM,OAAO,GAAG,IAAA,oBAAU,EAAC,UAAU,CAAC,CAAC;IAEvC,oBAAoB;IACpB,MAAM,QAAQ,GAAG,IAAA,yBAAkB,EAAC,MAAM,EAAE;QAC1C,UAAU,EAAE,IAAI;KACjB,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,MAAM,IAAA,gBAAS,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAEpD,sBAAsB;IACtB,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,IAAA,mBAAW,EAAC,OAAO,CAAC,CAAC;IAEhD,MAAM,m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AgB,CAAC;QAC1B,CAAC;QAED,MAAM,gBAAgB,GAA0B,OAAO,CACrD,uBAAuB,CACxB,CAAC,MAAM,EAAE,CAAC;QACX,OAAO,CACL,gBAAgB,EAAE,IAAI,KAAK,yCAAqB,CAAC,OAAO;YACxD,gBAAgB,EAAE,EAAE,KAAK,IAAI,EAAE,EAAE,CAClC,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,mBAAmB,CAAC,qBAAqB,CACvC,kCAAkC,EAClC,KAAK,EAAE,GAAW,EAAE,KAAc,EAAE,EAAE;QACpC,MAAM,OAAO,CAAC,2BAAe,EAAE,KAAK,CAAC,CAAC,SAAS,EAAE,CAAC;QAElD,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC,CACF,CAAC;AACJ,CAAC;AA9CD,0CA8CC","sourcesContent":["import type {\n ActionConstraint,\n EventConstraint,\n} from '@metamask/base-controller';\nimport { ControllerMessenger } from '@metamask/base-controller';\nimport { createEngineStream } from '@metamask/json-rpc-middleware-stream';\nimport {\n type CryptographicFunctions,\n mnemonicPhraseToBytes,\n} from '@metamask/key-tree';\nimport { PhishingDetectorResultType } from '@metamask/phishing-controller';\nimport type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n detectSnapLocation,\n fetchSnap,\n NodeThreadExecutionService,\n setupMultiplex,\n} from '@metamask/snaps-controllers/node';\nimport { DIALOG_APPROVAL_TYPES } from '@metamask/snaps-rpc-methods';\nimport type {\n AuxiliaryFileEncoding,\n Component,\n InterfaceState,\n InterfaceContext,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport type { FetchedSnapFiles } from '@metamask/snaps-utils';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport type { Duplex } from 'readable-stream';\nimport { pipeline } from 'readable-stream';\nimport type { SagaIterator } from 'redux-saga';\nimport { select } from 'redux-saga/effects';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getControllers, registerSnap } from './controllers';\nimport { getSnapFile } from './files';\nimport type { SnapHelpers } from './helpers';\nimport { getHelpers } from './helpers';\nimport { resolveWithSaga } from './interface';\nimport { getEndowments } from './methods';\nimport { createJsonRpcEngine } from './middleware';\nimport type { SimulationOptions, SimulationUserOptions } from './options';\nimport { getOptions } from './options';\nimport type { Interface, RunSagaFunction, Store } from './store';\nimport { createStore, getCurrentInterface } from './store';\n\n/**\n * Options for the execution service, without the options that are shared\n * between all execution services.\n *\n * @template Service - The type of the execution service, i.e., the class that\n * creates the execution service.\n */\nexport type ExecutionServiceOptions<\n Service extends new (...args: any[]) => any,\n> = Omit<\n ConstructorParameters<Service>[0],\n keyof ConstructorParameters<typeof AbstractExecutionService<unknown>>[0]\n>;\n\n/**\n * The options for running a Snap in a simulated environment.\n *\n * @property executionService - The execution service to use.\n * @property executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @property options - The simulation options.\n * @template Service - The type of the execution service.\n */\nexport type InstallSnapOptions<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService<unknown>\n >,\n> = ExecutionServiceOptions<Service> extends Record<string, never>\n ? {\n executionService: Service;\n executionServiceOptions?: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n }\n : {\n executionService: Service;\n executionServiceOptions: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n };\n\nexport type InstalledSnap = {\n snapId: SnapId;\n store: Store;\n executionService: InstanceType<typeof AbstractExecutionService>;\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n runSaga: RunSagaFunction;\n};\n\nexport type MiddlewareHooks = {\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 /**\n * A hook that returns the Snap's auxiliary file for the given path.\n *\n * @param path - The path of the auxiliary file to get.\n * @param encoding - The encoding to use when returning the file.\n * @returns The Snap's auxiliary file for the given path.\n */\n getSnapFile: (\n path: string,\n encoding: AuxiliaryFileEncoding,\n ) => Promise<string | null>;\n\n /**\n * A hook that returns whether the client is locked or not.\n *\n * @returns A boolean flag signaling whether the client is locked.\n */\n getIsLocked: () => boolean;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object to fall back to the default cryptographic functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions;\n\n createInterface: (\n content: Component,\n context?: InterfaceContext,\n ) => Promise<string>;\n updateInterface: (id: string, content: Component) => Promise<void>;\n getInterfaceState: (id: string) => InterfaceState;\n getInterfaceContext: (id: string) => InterfaceContext | null;\n resolveInterface: (id: string, value: Json) => Promise<void>;\n};\n\n/**\n * Install a Snap in a simulated environment. This will fetch the Snap files,\n * create a Redux store, set up the controllers and JSON-RPC stack, register the\n * Snap, and run the Snap code in the execution service.\n *\n * @param snapId - The ID of the Snap to install.\n * @param options - The options to use when installing the Snap.\n * @param options.executionService - The execution service to use.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The installed Snap object.\n * @template Service - The type of the execution service.\n */\nexport async function installSnap<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService\n >,\n>(\n snapId: SnapId,\n {\n executionService,\n executionServiceOptions,\n options: rawOptions = {},\n }: Partial<InstallSnapOptions<Service>> = {},\n): Promise<InstalledSnap & SnapHelpers> {\n const options = getOptions(rawOptions);\n\n // Fetch Snap files.\n const location = detectSnapLocation(snapId, {\n allowLocal: true,\n });\n\n const snapFiles = await fetchSnap(snapId, location);\n\n // Create Redux store.\n const { store, runSaga } = createStore(options);\n\n const controllerMessenger = new ControllerMessenger<any, any>();\n\n registerActions(controllerMessenger, runSaga);\n\n // Set up controllers and JSON-RPC stack.\n const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);\n\n const { subjectMetadataController, permissionController } = getControllers({\n controllerMessenger,\n hooks,\n runSaga,\n options,\n });\n\n const engine = createJsonRpcEngine({\n store,\n hooks,\n permissionMiddleware: permissionController.createPermissionMiddleware({\n origin: snapId,\n }),\n });\n\n // Create execution service.\n const ExecutionService = executionService ?? NodeThreadExecutionService;\n const service = new ExecutionService({\n ...executionServiceOptions,\n messenger: controllerMessenger.getRestricted({\n name: 'ExecutionService',\n allowedActions: [],\n allowedEvents: [],\n }),\n setupSnapProvider: (_snapId: string, rpcStream: Duplex) => {\n const mux = setupMultiplex(rpcStream, 'snapStream');\n const stream = mux.createStream('metamask-provider');\n const providerStream = createEngineStream({ engine });\n\n // Error function is difficult to test, so we ignore it.\n /* istanbul ignore next 2 */\n pipeline(stream, providerStream, stream, (error: unknown) => {\n if (error) {\n logError(`Provider stream failure.`, error);\n }\n });\n },\n });\n\n // Register the Snap. This sets up the Snap's permissions and subject\n // metadata.\n await registerSnap(snapId, snapFiles.manifest.result, {\n permissionController,\n subjectMetadataController,\n });\n\n // Run the Snap code in the execution service.\n await service.executeSnap({\n snapId,\n sourceCode: snapFiles.sourceCode.toString('utf8'),\n endowments: await getEndowments(permissionController, snapId),\n });\n\n const helpers = getHelpers({\n snapId,\n store,\n controllerMessenger,\n runSaga,\n executionService: service,\n options,\n });\n\n return {\n snapId,\n store,\n executionService: service,\n controllerMessenger,\n runSaga,\n ...helpers,\n };\n}\n\n/**\n * Get the hooks for the simulation.\n *\n * @param options - The simulation options.\n * @param snapFiles - The Snap files.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The hooks for the simulation.\n */\nexport function getHooks(\n options: SimulationOptions,\n snapFiles: FetchedSnapFiles,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n): MiddlewareHooks {\n return {\n getMnemonic: async () =>\n Promise.resolve(mnemonicPhraseToBytes(options.secretRecoveryPhrase)),\n getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>\n await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),\n getIsLocked: () => false,\n getClientCryptography: () => ({}),\n createInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n ...args,\n ),\n updateInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:updateInterface',\n snapId,\n ...args,\n ),\n getInterfaceState: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).state,\n getInterfaceContext: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).context,\n resolveInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:resolveInterface',\n snapId,\n ...args,\n ),\n };\n}\n\n/**\n * Register mocked action handlers.\n *\n * @param controllerMessenger - The controller messenger.\n * @param runSaga - The run saga function.\n */\nexport function registerActions(\n controllerMessenger: RootControllerMessenger,\n runSaga: RunSagaFunction,\n) {\n controllerMessenger.registerActionHandler(\n 'PhishingController:maybeUpdateState',\n async () => Promise.resolve(),\n );\n\n controllerMessenger.registerActionHandler(\n 'PhishingController:testOrigin',\n () => ({ result: false, type: PhishingDetectorResultType.All }),\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:hasRequest',\n (opts) => {\n /**\n * Get the current interface from the store.\n *\n * @yields Selects the current interface from the store.\n * @returns The current interface.\n */\n function* getCurrentInterfaceSaga(): SagaIterator {\n const currentInterface: Interface = yield select(getCurrentInterface);\n return currentInterface;\n }\n\n const currentInterface: Interface | undefined = runSaga(\n getCurrentInterfaceSaga,\n ).result();\n return (\n currentInterface?.type === DIALOG_APPROVAL_TYPES.default &&\n currentInterface?.id === opts?.id\n );\n },\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:acceptRequest',\n async (_id: string, value: unknown) => {\n await runSaga(resolveWithSaga, value).toPromise();\n\n return { value };\n },\n );\n}\n"]}
1
+ {"version":3,"file":"simulation.cjs","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":";;;AAIA,+DAAgE;AAChE,qFAA0E;AAC1E,iDAG4B;AAC5B,uEAA2E;AAE3E,2DAK0C;A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D,uBAAuB,CACxB,CAAC,MAAM,EAAE,CAAC;QACX,OAAO,CACL,gBAAgB,EAAE,IAAI,KAAK,yCAAqB,CAAC,OAAO;YACxD,gBAAgB,EAAE,EAAE,KAAK,IAAI,EAAE,EAAE,CAClC,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,mBAAmB,CAAC,qBAAqB,CACvC,kCAAkC,EAClC,KAAK,EAAE,GAAW,EAAE,KAAc,EAAE,EAAE;QACpC,MAAM,OAAO,CAAC,2BAAe,EAAE,KAAK,CAAC,CAAC,SAAS,EAAE,CAAC;QAElD,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC,CACF,CAAC;AACJ,CAAC;AA9CD,0CA8CC","sourcesContent":["import type {\n ActionConstraint,\n EventConstraint,\n} from '@metamask/base-controller';\nimport { ControllerMessenger } from '@metamask/base-controller';\nimport { createEngineStream } from '@metamask/json-rpc-middleware-stream';\nimport {\n type CryptographicFunctions,\n mnemonicPhraseToBytes,\n} from '@metamask/key-tree';\nimport { PhishingDetectorResultType } from '@metamask/phishing-controller';\nimport type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n detectSnapLocation,\n fetchSnap,\n NodeThreadExecutionService,\n setupMultiplex,\n} from '@metamask/snaps-controllers/node';\nimport { DIALOG_APPROVAL_TYPES } from '@metamask/snaps-rpc-methods';\nimport type {\n AuxiliaryFileEncoding,\n Component,\n InterfaceState,\n InterfaceContext,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport type { FetchedSnapFiles } from '@metamask/snaps-utils';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport type { Duplex } from 'readable-stream';\nimport { pipeline } from 'readable-stream';\nimport type { SagaIterator } from 'redux-saga';\nimport { select } from 'redux-saga/effects';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getControllers, registerSnap } from './controllers';\nimport { getSnapFile } from './files';\nimport type { SnapHelpers } from './helpers';\nimport { getHelpers } from './helpers';\nimport { resolveWithSaga } from './interface';\nimport { asyncResolve, getEndowments } from './methods';\nimport {\n getPermittedClearSnapStateMethodImplementation,\n getPermittedGetSnapStateMethodImplementation,\n getPermittedUpdateSnapStateMethodImplementation,\n} from './methods/hooks';\nimport { createJsonRpcEngine } from './middleware';\nimport type { SimulationOptions, SimulationUserOptions } from './options';\nimport { getOptions } from './options';\nimport type { Interface, RunSagaFunction, Store } from './store';\nimport { createStore, getCurrentInterface } from './store';\n\n/**\n * Options for the execution service, without the options that are shared\n * between all execution services.\n *\n * @template Service - The type of the execution service, i.e., the class that\n * creates the execution service.\n */\nexport type ExecutionServiceOptions<\n Service extends new (...args: any[]) => any,\n> = Omit<\n ConstructorParameters<Service>[0],\n keyof ConstructorParameters<typeof AbstractExecutionService<unknown>>[0]\n>;\n\n/**\n * The options for running a Snap in a simulated environment.\n *\n * @property executionService - The execution service to use.\n * @property executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @property options - The simulation options.\n * @template Service - The type of the execution service.\n */\nexport type InstallSnapOptions<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService<unknown>\n >,\n> = ExecutionServiceOptions<Service> extends Record<string, never>\n ? {\n executionService: Service;\n executionServiceOptions?: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n }\n : {\n executionService: Service;\n executionServiceOptions: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n };\n\nexport type InstalledSnap = {\n snapId: SnapId;\n store: Store;\n executionService: InstanceType<typeof AbstractExecutionService>;\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n runSaga: RunSagaFunction;\n};\n\nexport type RestrictedMiddlewareHooks = {\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 /**\n * A hook that returns whether the client is locked or not.\n *\n * @returns A boolean flag signaling whether the client is locked.\n */\n getIsLocked: () => boolean;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object to fall back to the default cryptographic functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions;\n};\n\nexport type PermittedMiddlewareHooks = {\n /**\n * A hook that gets whether the requesting origin has a given permission.\n *\n * @param permissionName - The name of the permission to check.\n * @returns Whether the origin has the permission.\n */\n hasPermission: (permissionName: string) => boolean;\n\n /**\n * A hook that returns a promise that resolves once the extension is unlocked.\n *\n * @param shouldShowUnlockRequest - Whether to show the unlock request.\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * A hook that returns whether the client is locked or not.\n *\n * @returns A boolean flag signaling whether the client is locked.\n */\n getIsLocked: () => boolean;\n\n /**\n * A hook that returns the Snap's auxiliary file for the given path. This hook\n * is bound to the Snap ID.\n *\n * @param path - The path of the auxiliary file to get.\n * @param encoding - The encoding to use when returning the file.\n * @returns The Snap's auxiliary file for the given path.\n */\n getSnapFile: (\n path: string,\n encoding: AuxiliaryFileEncoding,\n ) => Promise<string | null>;\n\n /**\n * A hook that gets the state of the Snap. This hook is bound to the Snap ID.\n *\n * @param encrypted - Whether to get the encrypted or unencrypted state.\n * @returns The current state of the Snap.\n */\n getSnapState: (encrypted: boolean) => Promise<Record<string, Json>>;\n\n /**\n * A hook that updates the state of the Snap. This hook is bound to the Snap\n * ID.\n *\n * @param newState - The new state.\n * @param encrypted - Whether to update the encrypted or unencrypted state.\n */\n updateSnapState: (\n newState: Record<string, Json>,\n encrypted: boolean,\n ) => Promise<void>;\n\n /**\n * A hook that clears the state of the Snap. This hook is bound to the Snap\n * ID.\n *\n * @param encrypted - Whether to clear the encrypted or unencrypted state.\n */\n clearSnapState: (encrypted: boolean) => Promise<void>;\n\n /**\n * A hook that creates an interface for the Snap. This hook is bound to the\n * Snap ID.\n *\n * @param content - The content of the interface.\n * @param context - The context of the interface.\n * @returns The ID of the created interface.\n */\n createInterface: (\n content: Component,\n context?: InterfaceContext,\n ) => Promise<string>;\n\n /**\n * A hook that updates an interface for the Snap. This hook is bound to the\n * Snap ID.\n *\n * @param id - The ID of the interface to update.\n * @param content - The content of the interface.\n */\n updateInterface: (id: string, content: Component) => Promise<void>;\n\n /**\n * A hook that gets the state of an interface for the Snap. This hook is bound\n * to the Snap ID.\n *\n * @param id - The ID of the interface to get.\n * @returns The state of the interface.\n */\n getInterfaceState: (id: string) => InterfaceState;\n\n /**\n * A hook that gets the context of an interface for the Snap. This hook is\n * bound to the Snap ID.\n *\n * @param id - The ID of the interface to get.\n * @returns The context of the interface.\n */\n getInterfaceContext: (id: string) => InterfaceContext | null;\n\n /**\n * A hook that resolves an interface for the Snap. This hook is bound to the\n * Snap ID.\n *\n * @param id - The ID of the interface to resolve.\n * @param value - The value to resolve the interface with.\n */\n resolveInterface: (id: string, value: Json) => Promise<void>;\n};\n\n/**\n * Install a Snap in a simulated environment. This will fetch the Snap files,\n * create a Redux store, set up the controllers and JSON-RPC stack, register the\n * Snap, and run the Snap code in the execution service.\n *\n * @param snapId - The ID of the Snap to install.\n * @param options - The options to use when installing the Snap.\n * @param options.executionService - The execution service to use.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The installed Snap object.\n * @template Service - The type of the execution service.\n */\nexport async function installSnap<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService\n >,\n>(\n snapId: SnapId,\n {\n executionService,\n executionServiceOptions,\n options: rawOptions = {},\n }: Partial<InstallSnapOptions<Service>> = {},\n): Promise<InstalledSnap & SnapHelpers> {\n const options = getOptions(rawOptions);\n\n // Fetch Snap files.\n const location = detectSnapLocation(snapId, {\n allowLocal: true,\n });\n\n const snapFiles = await fetchSnap(snapId, location);\n\n // Create Redux store.\n const { store, runSaga } = createStore(options);\n\n const controllerMessenger = new ControllerMessenger<any, any>();\n\n registerActions(controllerMessenger, runSaga);\n\n // Set up controllers and JSON-RPC stack.\n const restrictedHooks = getRestrictedHooks(options);\n const permittedHooks = getPermittedHooks(\n snapId,\n snapFiles,\n controllerMessenger,\n runSaga,\n );\n\n const { subjectMetadataController, permissionController } = getControllers({\n controllerMessenger,\n hooks: restrictedHooks,\n runSaga,\n options,\n });\n\n const engine = createJsonRpcEngine({\n store,\n restrictedHooks,\n permittedHooks,\n permissionMiddleware: permissionController.createPermissionMiddleware({\n origin: snapId,\n }),\n });\n\n // Create execution service.\n const ExecutionService = executionService ?? NodeThreadExecutionService;\n const service = new ExecutionService({\n ...executionServiceOptions,\n messenger: controllerMessenger.getRestricted({\n name: 'ExecutionService',\n allowedActions: [],\n allowedEvents: [],\n }),\n setupSnapProvider: (_snapId: string, rpcStream: Duplex) => {\n const mux = setupMultiplex(rpcStream, 'snapStream');\n const stream = mux.createStream('metamask-provider');\n const providerStream = createEngineStream({ engine });\n\n // Error function is difficult to test, so we ignore it.\n /* istanbul ignore next 2 */\n pipeline(stream, providerStream, stream, (error: unknown) => {\n if (error) {\n logError(`Provider stream failure.`, error);\n }\n });\n },\n });\n\n // Register the Snap. This sets up the Snap's permissions and subject\n // metadata.\n await registerSnap(snapId, snapFiles.manifest.result, {\n permissionController,\n subjectMetadataController,\n });\n\n // Run the Snap code in the execution service.\n await service.executeSnap({\n snapId,\n sourceCode: snapFiles.sourceCode.toString('utf8'),\n endowments: await getEndowments(permissionController, snapId),\n });\n\n const helpers = getHelpers({\n snapId,\n store,\n controllerMessenger,\n runSaga,\n executionService: service,\n options,\n });\n\n return {\n snapId,\n store,\n executionService: service,\n controllerMessenger,\n runSaga,\n ...helpers,\n };\n}\n\n/**\n * Get the hooks for the simulation.\n *\n * @param options - The simulation options.\n * @returns The hooks for the simulation.\n */\nexport function getRestrictedHooks(\n options: SimulationOptions,\n): RestrictedMiddlewareHooks {\n return {\n getMnemonic: async () =>\n Promise.resolve(mnemonicPhraseToBytes(options.secretRecoveryPhrase)),\n getIsLocked: () => false,\n getClientCryptography: () => ({}),\n };\n}\n\n/**\n * Get the permitted hooks for the simulation.\n *\n * @param snapId - The ID of the Snap.\n * @param snapFiles - The fetched Snap files.\n * @param controllerMessenger - The controller messenger.\n * @param runSaga - The run saga function.\n * @returns The permitted hooks for the simulation.\n */\nexport function getPermittedHooks(\n snapId: SnapId,\n snapFiles: FetchedSnapFiles,\n controllerMessenger: RootControllerMessenger,\n runSaga: RunSagaFunction,\n): PermittedMiddlewareHooks {\n return {\n hasPermission: () => true,\n getUnlockPromise: asyncResolve(),\n getIsLocked: () => false,\n\n getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>\n await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),\n\n createInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n ...args,\n ),\n updateInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:updateInterface',\n snapId,\n ...args,\n ),\n getInterfaceState: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).state,\n getInterfaceContext: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).context,\n resolveInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:resolveInterface',\n snapId,\n ...args,\n ),\n\n getSnapState: getPermittedGetSnapStateMethodImplementation(runSaga),\n updateSnapState: getPermittedUpdateSnapStateMethodImplementation(runSaga),\n clearSnapState: getPermittedClearSnapStateMethodImplementation(runSaga),\n };\n}\n\n/**\n * Register mocked action handlers.\n *\n * @param controllerMessenger - The controller messenger.\n * @param runSaga - The run saga function.\n */\nexport function registerActions(\n controllerMessenger: RootControllerMessenger,\n runSaga: RunSagaFunction,\n) {\n controllerMessenger.registerActionHandler(\n 'PhishingController:maybeUpdateState',\n async () => Promise.resolve(),\n );\n\n controllerMessenger.registerActionHandler(\n 'PhishingController:testOrigin',\n () => ({ result: false, type: PhishingDetectorResultType.All }),\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:hasRequest',\n (opts) => {\n /**\n * Get the current interface from the store.\n *\n * @yields Selects the current interface from the store.\n * @returns The current interface.\n */\n function* getCurrentInterfaceSaga(): SagaIterator {\n const currentInterface: Interface = yield select(getCurrentInterface);\n return currentInterface;\n }\n\n const currentInterface: Interface | undefined = runSaga(\n getCurrentInterfaceSaga,\n ).result();\n return (\n currentInterface?.type === DIALOG_APPROVAL_TYPES.default &&\n currentInterface?.id === opts?.id\n );\n },\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:acceptRequest',\n async (_id: string, value: unknown) => {\n await runSaga(resolveWithSaga, value).toPromise();\n\n return { value };\n },\n );\n}\n"]}
@@ -43,21 +43,13 @@ export type InstalledSnap = {
43
43
  controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;
44
44
  runSaga: RunSagaFunction;
45
45
  };
46
- export type MiddlewareHooks = {
46
+ export type RestrictedMiddlewareHooks = {
47
47
  /**
48
48
  * A hook that returns the user's secret recovery phrase.
49
49
  *
50
50
  * @returns The user's secret recovery phrase.
51
51
  */
52
52
  getMnemonic: () => Promise<Uint8Array>;
53
- /**
54
- * A hook that returns the Snap's auxiliary file for the given path.
55
- *
56
- * @param path - The path of the auxiliary file to get.
57
- * @param encoding - The encoding to use when returning the file.
58
- * @returns The Snap's auxiliary file for the given path.
59
- */
60
- getSnapFile: (path: string, encoding: AuxiliaryFileEncoding) => Promise<string | null>;
61
53
  /**
62
54
  * A hook that returns whether the client is locked or not.
63
55
  *
@@ -71,10 +63,99 @@ export type MiddlewareHooks = {
71
63
  * @returns The cryptographic functions to use for the client.
72
64
  */
73
65
  getClientCryptography: () => CryptographicFunctions;
66
+ };
67
+ export type PermittedMiddlewareHooks = {
68
+ /**
69
+ * A hook that gets whether the requesting origin has a given permission.
70
+ *
71
+ * @param permissionName - The name of the permission to check.
72
+ * @returns Whether the origin has the permission.
73
+ */
74
+ hasPermission: (permissionName: string) => boolean;
75
+ /**
76
+ * A hook that returns a promise that resolves once the extension is unlocked.
77
+ *
78
+ * @param shouldShowUnlockRequest - Whether to show the unlock request.
79
+ * @returns A promise that resolves once the extension is unlocked.
80
+ */
81
+ getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
82
+ /**
83
+ * A hook that returns whether the client is locked or not.
84
+ *
85
+ * @returns A boolean flag signaling whether the client is locked.
86
+ */
87
+ getIsLocked: () => boolean;
88
+ /**
89
+ * A hook that returns the Snap's auxiliary file for the given path. This hook
90
+ * is bound to the Snap ID.
91
+ *
92
+ * @param path - The path of the auxiliary file to get.
93
+ * @param encoding - The encoding to use when returning the file.
94
+ * @returns The Snap's auxiliary file for the given path.
95
+ */
96
+ getSnapFile: (path: string, encoding: AuxiliaryFileEncoding) => Promise<string | null>;
97
+ /**
98
+ * A hook that gets the state of the Snap. This hook is bound to the Snap ID.
99
+ *
100
+ * @param encrypted - Whether to get the encrypted or unencrypted state.
101
+ * @returns The current state of the Snap.
102
+ */
103
+ getSnapState: (encrypted: boolean) => Promise<Record<string, Json>>;
104
+ /**
105
+ * A hook that updates the state of the Snap. This hook is bound to the Snap
106
+ * ID.
107
+ *
108
+ * @param newState - The new state.
109
+ * @param encrypted - Whether to update the encrypted or unencrypted state.
110
+ */
111
+ updateSnapState: (newState: Record<string, Json>, encrypted: boolean) => Promise<void>;
112
+ /**
113
+ * A hook that clears the state of the Snap. This hook is bound to the Snap
114
+ * ID.
115
+ *
116
+ * @param encrypted - Whether to clear the encrypted or unencrypted state.
117
+ */
118
+ clearSnapState: (encrypted: boolean) => Promise<void>;
119
+ /**
120
+ * A hook that creates an interface for the Snap. This hook is bound to the
121
+ * Snap ID.
122
+ *
123
+ * @param content - The content of the interface.
124
+ * @param context - The context of the interface.
125
+ * @returns The ID of the created interface.
126
+ */
74
127
  createInterface: (content: Component, context?: InterfaceContext) => Promise<string>;
128
+ /**
129
+ * A hook that updates an interface for the Snap. This hook is bound to the
130
+ * Snap ID.
131
+ *
132
+ * @param id - The ID of the interface to update.
133
+ * @param content - The content of the interface.
134
+ */
75
135
  updateInterface: (id: string, content: Component) => Promise<void>;
136
+ /**
137
+ * A hook that gets the state of an interface for the Snap. This hook is bound
138
+ * to the Snap ID.
139
+ *
140
+ * @param id - The ID of the interface to get.
141
+ * @returns The state of the interface.
142
+ */
76
143
  getInterfaceState: (id: string) => InterfaceState;
144
+ /**
145
+ * A hook that gets the context of an interface for the Snap. This hook is
146
+ * bound to the Snap ID.
147
+ *
148
+ * @param id - The ID of the interface to get.
149
+ * @returns The context of the interface.
150
+ */
77
151
  getInterfaceContext: (id: string) => InterfaceContext | null;
152
+ /**
153
+ * A hook that resolves an interface for the Snap. This hook is bound to the
154
+ * Snap ID.
155
+ *
156
+ * @param id - The ID of the interface to resolve.
157
+ * @param value - The value to resolve the interface with.
158
+ */
78
159
  resolveInterface: (id: string, value: Json) => Promise<void>;
79
160
  };
80
161
  /**
@@ -97,12 +178,19 @@ export declare function installSnap<Service extends new (...args: any[]) => Inst
97
178
  * Get the hooks for the simulation.
98
179
  *
99
180
  * @param options - The simulation options.
100
- * @param snapFiles - The Snap files.
101
- * @param snapId - The Snap ID.
102
- * @param controllerMessenger - The controller messenger.
103
181
  * @returns The hooks for the simulation.
104
182
  */
105
- export declare function getHooks(options: SimulationOptions, snapFiles: FetchedSnapFiles, snapId: SnapId, controllerMessenger: RootControllerMessenger): MiddlewareHooks;
183
+ export declare function getRestrictedHooks(options: SimulationOptions): RestrictedMiddlewareHooks;
184
+ /**
185
+ * Get the permitted hooks for the simulation.
186
+ *
187
+ * @param snapId - The ID of the Snap.
188
+ * @param snapFiles - The fetched Snap files.
189
+ * @param controllerMessenger - The controller messenger.
190
+ * @param runSaga - The run saga function.
191
+ * @returns The permitted hooks for the simulation.
192
+ */
193
+ export declare function getPermittedHooks(snapId: SnapId, snapFiles: FetchedSnapFiles, controllerMessenger: RootControllerMessenger, runSaga: RunSagaFunction): PermittedMiddlewareHooks;
106
194
  /**
107
195
  * Register mocked action handlers.
108
196
  *
@@ -1 +1 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IAElD;;;;;;OAMG;IACH,mBAAmB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,gBAAgB,GAAG,IAAI,CAAC;IAE7D;;;;;;OAMG;IACH,gBAAgB,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9D,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,WAAW,CAC/B,OAAO,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,YAAY,CAClD,OAAO,wBAAwB,CAChC,EAED,MAAM,EAAE,MAAM,EACd,EACE,gBAAgB,EAChB,uBAAuB,EACvB,OAAO,EAAE,UAAe,GACzB,GAAE,OAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAM,GAC3C,OAAO,CAAC,aAAa,GAAG,WAAW,CAAC,CAiGtC;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,iBAAiB,GACzB,yBAAyB,CAO3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,gBAAgB,EAC3B,mBAAmB,EAAE,uBAAuB,EAC5C,OAAO,EAAE,eAAe,GACvB,wBAAwB,CA4C1B;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,mBAAmB,EAAE,uBAAuB,EAC5C,OAAO,EAAE,eAAe,QA4CzB"}
@@ -43,21 +43,13 @@ export type InstalledSnap = {
43
43
  controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;
44
44
  runSaga: RunSagaFunction;
45
45
  };
46
- export type MiddlewareHooks = {
46
+ export type RestrictedMiddlewareHooks = {
47
47
  /**
48
48
  * A hook that returns the user's secret recovery phrase.
49
49
  *
50
50
  * @returns The user's secret recovery phrase.
51
51
  */
52
52
  getMnemonic: () => Promise<Uint8Array>;
53
- /**
54
- * A hook that returns the Snap's auxiliary file for the given path.
55
- *
56
- * @param path - The path of the auxiliary file to get.
57
- * @param encoding - The encoding to use when returning the file.
58
- * @returns The Snap's auxiliary file for the given path.
59
- */
60
- getSnapFile: (path: string, encoding: AuxiliaryFileEncoding) => Promise<string | null>;
61
53
  /**
62
54
  * A hook that returns whether the client is locked or not.
63
55
  *
@@ -71,10 +63,99 @@ export type MiddlewareHooks = {
71
63
  * @returns The cryptographic functions to use for the client.
72
64
  */
73
65
  getClientCryptography: () => CryptographicFunctions;
66
+ };
67
+ export type PermittedMiddlewareHooks = {
68
+ /**
69
+ * A hook that gets whether the requesting origin has a given permission.
70
+ *
71
+ * @param permissionName - The name of the permission to check.
72
+ * @returns Whether the origin has the permission.
73
+ */
74
+ hasPermission: (permissionName: string) => boolean;
75
+ /**
76
+ * A hook that returns a promise that resolves once the extension is unlocked.
77
+ *
78
+ * @param shouldShowUnlockRequest - Whether to show the unlock request.
79
+ * @returns A promise that resolves once the extension is unlocked.
80
+ */
81
+ getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
82
+ /**
83
+ * A hook that returns whether the client is locked or not.
84
+ *
85
+ * @returns A boolean flag signaling whether the client is locked.
86
+ */
87
+ getIsLocked: () => boolean;
88
+ /**
89
+ * A hook that returns the Snap's auxiliary file for the given path. This hook
90
+ * is bound to the Snap ID.
91
+ *
92
+ * @param path - The path of the auxiliary file to get.
93
+ * @param encoding - The encoding to use when returning the file.
94
+ * @returns The Snap's auxiliary file for the given path.
95
+ */
96
+ getSnapFile: (path: string, encoding: AuxiliaryFileEncoding) => Promise<string | null>;
97
+ /**
98
+ * A hook that gets the state of the Snap. This hook is bound to the Snap ID.
99
+ *
100
+ * @param encrypted - Whether to get the encrypted or unencrypted state.
101
+ * @returns The current state of the Snap.
102
+ */
103
+ getSnapState: (encrypted: boolean) => Promise<Record<string, Json>>;
104
+ /**
105
+ * A hook that updates the state of the Snap. This hook is bound to the Snap
106
+ * ID.
107
+ *
108
+ * @param newState - The new state.
109
+ * @param encrypted - Whether to update the encrypted or unencrypted state.
110
+ */
111
+ updateSnapState: (newState: Record<string, Json>, encrypted: boolean) => Promise<void>;
112
+ /**
113
+ * A hook that clears the state of the Snap. This hook is bound to the Snap
114
+ * ID.
115
+ *
116
+ * @param encrypted - Whether to clear the encrypted or unencrypted state.
117
+ */
118
+ clearSnapState: (encrypted: boolean) => Promise<void>;
119
+ /**
120
+ * A hook that creates an interface for the Snap. This hook is bound to the
121
+ * Snap ID.
122
+ *
123
+ * @param content - The content of the interface.
124
+ * @param context - The context of the interface.
125
+ * @returns The ID of the created interface.
126
+ */
74
127
  createInterface: (content: Component, context?: InterfaceContext) => Promise<string>;
128
+ /**
129
+ * A hook that updates an interface for the Snap. This hook is bound to the
130
+ * Snap ID.
131
+ *
132
+ * @param id - The ID of the interface to update.
133
+ * @param content - The content of the interface.
134
+ */
75
135
  updateInterface: (id: string, content: Component) => Promise<void>;
136
+ /**
137
+ * A hook that gets the state of an interface for the Snap. This hook is bound
138
+ * to the Snap ID.
139
+ *
140
+ * @param id - The ID of the interface to get.
141
+ * @returns The state of the interface.
142
+ */
76
143
  getInterfaceState: (id: string) => InterfaceState;
144
+ /**
145
+ * A hook that gets the context of an interface for the Snap. This hook is
146
+ * bound to the Snap ID.
147
+ *
148
+ * @param id - The ID of the interface to get.
149
+ * @returns The context of the interface.
150
+ */
77
151
  getInterfaceContext: (id: string) => InterfaceContext | null;
152
+ /**
153
+ * A hook that resolves an interface for the Snap. This hook is bound to the
154
+ * Snap ID.
155
+ *
156
+ * @param id - The ID of the interface to resolve.
157
+ * @param value - The value to resolve the interface with.
158
+ */
78
159
  resolveInterface: (id: string, value: Json) => Promise<void>;
79
160
  };
80
161
  /**
@@ -97,12 +178,19 @@ export declare function installSnap<Service extends new (...args: any[]) => Inst
97
178
  * Get the hooks for the simulation.
98
179
  *
99
180
  * @param options - The simulation options.
100
- * @param snapFiles - The Snap files.
101
- * @param snapId - The Snap ID.
102
- * @param controllerMessenger - The controller messenger.
103
181
  * @returns The hooks for the simulation.
104
182
  */
105
- export declare function getHooks(options: SimulationOptions, snapFiles: FetchedSnapFiles, snapId: SnapId, controllerMessenger: RootControllerMessenger): MiddlewareHooks;
183
+ export declare function getRestrictedHooks(options: SimulationOptions): RestrictedMiddlewareHooks;
184
+ /**
185
+ * Get the permitted hooks for the simulation.
186
+ *
187
+ * @param snapId - The ID of the Snap.
188
+ * @param snapFiles - The fetched Snap files.
189
+ * @param controllerMessenger - The controller messenger.
190
+ * @param runSaga - The run saga function.
191
+ * @returns The permitted hooks for the simulation.
192
+ */
193
+ export declare function getPermittedHooks(snapId: SnapId, snapFiles: FetchedSnapFiles, controllerMessenger: RootControllerMessenger, runSaga: RunSagaFunction): PermittedMiddlewareHooks;
106
194
  /**
107
195
  * Register mocked action handlers.
108
196
  *
@@ -1 +1 @@
1
- {"version":3,"file":"simulation.d.mts","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EAChB,kCAAkC;AACnC,OAAO,EAAE,mBAAmB,EAAE,kCAAkC;AAEhE,OAAO,EACL,KAAK,sBAAsB,EAE5B,2BAA2B;AAE5B,OAAO,KAAK,EAAE,wBAAwB,EAAE,oCAAoC;AAQ5E,OAAO,KAAK,EACV,qBAAqB,EACrB,SAAS,EACT,cAAc,EACd,gBAAgB,EAChB,MAAM,EACP,4BAA4B;AAC7B,OAAO,KAAK,EAAE,gBAAgB,EAAE,8BAA8B;AAE9D,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAM5C,OAAO,KAAK,EAAE,uBAAuB,EAAE,0BAAsB;AAG7D,OAAO,KAAK,EAAE,WAAW,EAAE,sBAAkB;AAK7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,sBAAkB;AAE1E,OAAO,KAAK,EAAa,eAAe,EAAE,KAAK,EAAE,0BAAgB;AAGjE;;;;;;GAMG;AACH,MAAM,MAAM,uBAAuB,CACjC,OAAO,SAAS,KAA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AC3B,mBAAmB,EAAE,uBAAuB,EAC5C,OAAO,EAAE,eAAe,GACvB,wBAAwB,CA4C1B;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,mBAAmB,EAAE,uBAAuB,EAC5C,OAAO,EAAE,eAAe,QA4CzB"}
@@ -11,7 +11,8 @@ import { getControllers, registerSnap } from "./controllers.mjs";
11
11
  import { getSnapFile } from "./files.mjs";
12
12
  import { getHelpers } from "./helpers.mjs";
13
13
  import { resolveWithSaga } from "./interface.mjs";
14
- import { getEndowments } from "./methods/index.mjs";
14
+ import { asyncResolve, getEndowments } from "./methods/index.mjs";
15
+ import { getPermittedClearSnapStateMethodImplementation, getPermittedGetSnapStateMethodImplementation, getPermittedUpdateSnapStateMethodImplementation } from "./methods/hooks/index.mjs";
15
16
  import { createJsonRpcEngine } from "./middleware/index.mjs";
16
17
  import { getOptions } from "./options.mjs";
17
18
  import { createStore, getCurrentInterface } from "./store/index.mjs";
@@ -42,16 +43,18 @@ export async function installSnap(snapId, { executionService, executionServiceOp
42
43
  const controllerMessenger = new ControllerMessenger();
43
44
  registerActions(controllerMessenger, runSaga);
44
45
  // Set up controllers and JSON-RPC stack.
45
- const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);
46
+ const restrictedHooks = getRestrictedHooks(options);
47
+ const permittedHooks = getPermittedHooks(snapId, snapFiles, controllerMessenger, runSaga);
46
48
  const { subjectMetadataController, permissionController } = getControllers({
47
49
  controllerMessenger,
48
- hooks,
50
+ hooks: restrictedHooks,
49
51
  runSaga,
50
52
  options,
51
53
  });
52
54
  const engine = createJsonRpcEngine({
53
55
  store,
54
- hooks,
56
+ restrictedHooks,
57
+ permittedHooks,
55
58
  permissionMiddleware: permissionController.createPermissionMiddleware({
56
59
  origin: snapId,
57
60
  }),
@@ -111,22 +114,38 @@ export async function installSnap(snapId, { executionService, executionServiceOp
111
114
  * Get the hooks for the simulation.
112
115
  *
113
116
  * @param options - The simulation options.
114
- * @param snapFiles - The Snap files.
115
- * @param snapId - The Snap ID.
116
- * @param controllerMessenger - The controller messenger.
117
117
  * @returns The hooks for the simulation.
118
118
  */
119
- export function getHooks(options, snapFiles, snapId, controllerMessenger) {
119
+ export function getRestrictedHooks(options) {
120
120
  return {
121
121
  getMnemonic: async () => Promise.resolve(mnemonicPhraseToBytes(options.secretRecoveryPhrase)),
122
- getSnapFile: async (path, encoding) => await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),
123
122
  getIsLocked: () => false,
124
123
  getClientCryptography: () => ({}),
124
+ };
125
+ }
126
+ /**
127
+ * Get the permitted hooks for the simulation.
128
+ *
129
+ * @param snapId - The ID of the Snap.
130
+ * @param snapFiles - The fetched Snap files.
131
+ * @param controllerMessenger - The controller messenger.
132
+ * @param runSaga - The run saga function.
133
+ * @returns The permitted hooks for the simulation.
134
+ */
135
+ export function getPermittedHooks(snapId, snapFiles, controllerMessenger, runSaga) {
136
+ return {
137
+ hasPermission: () => true,
138
+ getUnlockPromise: asyncResolve(),
139
+ getIsLocked: () => false,
140
+ getSnapFile: async (path, encoding) => await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),
125
141
  createInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:createInterface', snapId, ...args),
126
142
  updateInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:updateInterface', snapId, ...args),
127
143
  getInterfaceState: (...args) => controllerMessenger.call('SnapInterfaceController:getInterface', snapId, ...args).state,
128
144
  getInterfaceContext: (...args) => controllerMessenger.call('SnapInterfaceController:getInterface', snapId, ...args).context,
129
145
  resolveInterface: async (...args) => controllerMessenger.call('SnapInterfaceController:resolveInterface', snapId, ...args),
146
+ getSnapState: getPermittedGetSnapStateMethodImplementation(runSaga),
147
+ updateSnapState: getPermittedUpdateSnapStateMethodImplementation(runSaga),
148
+ clearSnapState: getPermittedClearSnapStateMethodImplementation(runSaga),
130
149
  };
131
150
  }
132
151
  /**