@metamask/snaps-simulation 3.4.1 → 3.5.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 (52) hide show
  1. package/CHANGELOG.md +16 -1
  2. package/dist/helpers.cjs +28 -9
  3. package/dist/helpers.cjs.map +1 -1
  4. package/dist/helpers.d.cts +61 -0
  5. package/dist/helpers.d.cts.map +1 -1
  6. package/dist/helpers.d.mts +61 -0
  7. package/dist/helpers.d.mts.map +1 -1
  8. package/dist/helpers.mjs +29 -9
  9. package/dist/helpers.mjs.map +1 -1
  10. package/dist/middleware/mock.cjs +16 -7
  11. package/dist/middleware/mock.cjs.map +1 -1
  12. package/dist/middleware/mock.d.cts +1 -1
  13. package/dist/middleware/mock.d.cts.map +1 -1
  14. package/dist/middleware/mock.d.mts +1 -1
  15. package/dist/middleware/mock.d.mts.map +1 -1
  16. package/dist/middleware/mock.mjs +17 -8
  17. package/dist/middleware/mock.mjs.map +1 -1
  18. package/dist/request.cjs +1 -1
  19. package/dist/request.cjs.map +1 -1
  20. package/dist/request.mjs +1 -1
  21. package/dist/request.mjs.map +1 -1
  22. package/dist/simulation.cjs +3 -2
  23. package/dist/simulation.cjs.map +1 -1
  24. package/dist/simulation.d.cts +9 -2
  25. package/dist/simulation.d.cts.map +1 -1
  26. package/dist/simulation.d.mts +9 -2
  27. package/dist/simulation.d.mts.map +1 -1
  28. package/dist/simulation.mjs +3 -2
  29. package/dist/simulation.mjs.map +1 -1
  30. package/dist/store/mocks.cjs +5 -8
  31. package/dist/store/mocks.cjs.map +1 -1
  32. package/dist/store/mocks.d.cts +6 -19
  33. package/dist/store/mocks.d.cts.map +1 -1
  34. package/dist/store/mocks.d.mts +6 -19
  35. package/dist/store/mocks.d.mts.map +1 -1
  36. package/dist/store/mocks.mjs +5 -8
  37. package/dist/store/mocks.mjs.map +1 -1
  38. package/dist/store/store.cjs +1 -1
  39. package/dist/store/store.cjs.map +1 -1
  40. package/dist/store/store.d.cts.map +1 -1
  41. package/dist/store/store.d.mts.map +1 -1
  42. package/dist/store/store.mjs +1 -1
  43. package/dist/store/store.mjs.map +1 -1
  44. package/dist/structs.d.cts +3 -3
  45. package/dist/structs.d.mts +3 -3
  46. package/dist/types.cjs.map +1 -1
  47. package/dist/types.d.cts +70 -2
  48. package/dist/types.d.cts.map +1 -1
  49. package/dist/types.d.mts +70 -2
  50. package/dist/types.d.mts.map +1 -1
  51. package/dist/types.mjs.map +1 -1
  52. package/package.json +11 -11
package/CHANGELOG.md CHANGED
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [3.5.0]
11
+
12
+ ### Added
13
+
14
+ - Allow mocking JSON-RPC implementations ([#3667](https://github.com/MetaMask/snaps/pull/3667))
15
+
16
+ ## [3.4.2]
17
+
18
+ ### Changed
19
+
20
+ - Adjust hooks after changes in RPC method imlementations ([#3361](https://github.com/MetaMask/snaps/pull/3361), [#3660](https://github.com/MetaMask/snaps/pull/3660))
21
+ - Bump MetaMask dependencies ([#3651](https://github.com/MetaMask/snaps/pull/3651), [#3638](https://github.com/MetaMask/snaps/pull/3638), [#3648](https://github.com/MetaMask/snaps/pull/3648), [#3630](https://github.com/MetaMask/snaps/pull/3630), [#3628](https://github.com/MetaMask/snaps/pull/3628), [#3629](https://github.com/MetaMask/snaps/pull/3629), [#3607](https://github.com/MetaMask/snaps/pull/3607), [#3623](https://github.com/MetaMask/snaps/pull/3623), [#3612](https://github.com/MetaMask/snaps/pull/3612))
22
+
10
23
  ## [3.4.1]
11
24
 
12
25
  ### Fixed
@@ -210,7 +223,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
210
223
 
211
224
  - Initial release of `@metamask/snaps-simulation` package ([#2727](https://github.com/MetaMask/snaps/pull/2727))
212
225
 
213
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@3.4.1...HEAD
226
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@3.5.0...HEAD
227
+ [3.5.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@3.4.2...@metamask/snaps-simulation@3.5.0
228
+ [3.4.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@3.4.1...@metamask/snaps-simulation@3.4.2
214
229
  [3.4.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@3.4.0...@metamask/snaps-simulation@3.4.1
215
230
  [3.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@3.3.0...@metamask/snaps-simulation@3.4.0
216
231
  [3.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@3.2.0...@metamask/snaps-simulation@3.3.0
package/dist/helpers.cjs CHANGED
@@ -4,6 +4,7 @@ exports.getHelpers = void 0;
4
4
  const snaps_utils_1 = require("@metamask/snaps-utils");
5
5
  const superstruct_1 = require("@metamask/superstruct");
6
6
  const utils_1 = require("@metamask/utils");
7
+ const toolkit_1 = require("@reduxjs/toolkit");
7
8
  const logger_1 = require("./logger.cjs");
8
9
  const request_1 = require("./request.cjs");
9
10
  const store_1 = require("./store/index.cjs");
@@ -75,6 +76,29 @@ function getHelpers({ snapId, store, executionService, runSaga, controllerMessen
75
76
  request,
76
77
  });
77
78
  };
79
+ const mockJsonRpc = (mock, once) => {
80
+ log('Mocking JSON-RPC request %o.', mock);
81
+ const id = (0, toolkit_1.nanoid)();
82
+ if (typeof mock === 'function') {
83
+ store.dispatch((0, store_1.addJsonRpcMock)({ id, implementation: mock, once }));
84
+ }
85
+ else {
86
+ const { method, result } = (0, superstruct_1.create)(mock, structs_1.JsonRpcMockOptionsStruct);
87
+ const implementation = (request) => {
88
+ if (request.method === method) {
89
+ return result;
90
+ }
91
+ return undefined;
92
+ };
93
+ store.dispatch((0, store_1.addJsonRpcMock)({ id, implementation, once }));
94
+ }
95
+ return {
96
+ unmock() {
97
+ log('Unmocking JSON-RPC request %o.', mock);
98
+ store.dispatch((0, store_1.removeJsonRpcMock)(id));
99
+ },
100
+ };
101
+ };
78
102
  return {
79
103
  // This can't be async because it returns a `SnapRequest`.
80
104
  // eslint-disable-next-line @typescript-eslint/promise-function-async
@@ -268,15 +292,10 @@ function getHelpers({ snapId, store, executionService, runSaga, controllerMessen
268
292
  });
269
293
  },
270
294
  mockJsonRpc(mock) {
271
- log('Mocking JSON-RPC request %o.', mock);
272
- const { method, result } = (0, superstruct_1.create)(mock, structs_1.JsonRpcMockOptionsStruct);
273
- store.dispatch((0, store_1.addJsonRpcMock)({ method, result }));
274
- return {
275
- unmock() {
276
- log('Unmocking JSON-RPC request %o.', mock);
277
- store.dispatch((0, store_1.removeJsonRpcMock)(method));
278
- },
279
- };
295
+ return mockJsonRpc(mock, false);
296
+ },
297
+ mockJsonRpcOnce(mock) {
298
+ return mockJsonRpc(mock, true);
280
299
  },
281
300
  close: async () => {
282
301
  log('Closing execution service.');
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.cjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AACpD,uDAA+C;AAE/C,2CAAqD;AAErD,yCAAsC;AAEtC,2CAA0C;AAE1C,6CAA4D;AAC5D,2CAMmB;AAcnB,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,mBAAU,EAAE,SAAS,CAAC,CAAC;AA0MtD;;;;;;;;;;;GAWG;AACH,SAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,GACwC;IAC/C,MAAM,aAAa,GAAG,KAAK,EACzB,OAA2B,EACS,EAAE;QACtC,GAAG,CAAC,yBAAyB,EAAE,OAAO,CAAC,CAAC;QAExC,MAAM,EACJ,MAAM,EAAE,iBAAiB,EACzB,OAAO,EACP,GAAG,WAAW,EACf,GAAG,IAAA,oBAAM,EAAC,OAAO,EAAE,kCAAwB,CAAC,CAAC;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CAAC;YAClC,MAAM,gBAAgB,CAAC,iBAAiB,EAAE,CAAC;QAC7C,CAAC;KACF,CAAC;AACJ,CAAC;AA5UD,gCA4UC","sourcesContent":["import { HandlerType } from '@metamask/snaps-utils';\nimport { create } from '@metamask/superstruct';\nimport type { CaipChainId } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n\nimport { rootLogger } from './logger';\nimport type { SimulationOptions } from './options';\nimport { handleRequest } from './request';\nimport type { InstalledSnap } from './simulation';\nimport { addJsonRpcMock, removeJsonRpcMock } from './store';\nimport {\n assertIsResponseWithInterface,\n JsonRpcMockOptionsStruct,\n NameLookupOptionsStruct,\n SignatureOptionsStruct,\n TransactionOptionsStruct,\n} from './structs';\nimport type {\n CronjobOptions,\n JsonRpcMockOptions,\n KeyringOptions,\n NameLookupOptions,\n RequestOptions,\n SignatureOptions,\n SnapRequest,\n SnapResponseWithInterface,\n SnapResponseWithoutInterface,\n TransactionOptions,\n} from './types';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * This is the main entry point to interact with the snap. It is returned by\n * {@link installSnap}, and has methods to send requests to the snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * const snap = await installSnap();\n * const response = await snap.request({ method: 'hello' });\n *\n * expect(response).toRespondWith('Hello, world!');\n */\nexport type SnapHelpers = {\n /**\n * Send a JSON-RPC request to the snap.\n *\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n request(request: RequestOptions): SnapRequest;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n */\n onTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n * @deprecated Use {@link onTransaction} instead.\n */\n sendTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a signature request to the snap.\n *\n * @param signature - The signature request object. Contains the params from\n * the various signature methods, but has an extra `origin` and `signatureMethod` field.\n * Any missing fields will be filled in with default values.\n * @returns The response.\n */\n onSignature(\n signature?: Partial<SignatureOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onCronjob(cronjob?: Partial<CronjobOptions>): SnapRequest;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n * @deprecated Use {@link onCronjob} instead.\n */\n runCronjob(cronjob: CronjobOptions): SnapRequest;\n\n /**\n * Run a background event in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param backgroundEvent - The background event request. This is similar to a JSON-RPC\n * request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest;\n\n /**\n * Get the response from the snap's `onHomePage` method.\n *\n * @returns The response.\n */\n onHomePage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Get the response from the snap's `onSettingsPage` method.\n *\n * @returns The response.\n */\n onSettingsPage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a keyring request to the Snap.\n *\n * @param keyringRequest - Keyring request.\n * @returns The response.\n */\n onKeyringRequest(keyringRequest: KeyringOptions): SnapRequest;\n\n /**\n * Get the response from the Snap's `onInstall` handler.\n *\n * @returns The response.\n */\n onInstall(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onUpdate` handler.\n *\n * @returns The response.\n */\n onUpdate(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onStart` handler.\n *\n * @returns The response.\n */\n onStart(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onNameLookup` handler.\n *\n * @returns The response.\n */\n onNameLookup(\n request: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC protocol request to the Snap.\n *\n * @param scope - A CAIP-2 scope.\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onProtocolRequest(\n scope: CaipChainId,\n request: RequestOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC client request to the Snap.\n *\n * @param request - The JSON-RPC request.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Mock a JSON-RPC request. This will cause the snap to respond with the\n * specified response when a request with the specified method is sent.\n *\n * @param mock - The mock options.\n * @param mock.method - The JSON-RPC request method.\n * @param mock.result - The JSON-RPC response, which will be returned when a\n * request with the specified method is sent.\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpc({ method: 'eth_accounts', result: ['0x1234'] });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n */\n mockJsonRpc(mock: JsonRpcMockOptions): {\n /**\n * Remove the mock.\n */\n unmock(): void;\n };\n\n /**\n * Close the page running the snap. This is mainly useful for cleaning up\n * the test environment, and calling it is not strictly necessary.\n *\n * @returns A promise that resolves when the page is closed.\n */\n close(): Promise<void>;\n};\n\n/**\n * Get the helper functions for the Snap.\n *\n * @param snap - The installed Snap.\n * @param snap.snapId - The ID of the Snap.\n * @param snap.store - The Redux store.\n * @param snap.executionService - The execution service.\n * @param snap.runSaga - The `runSaga` function.\n * @param snap.controllerMessenger - The controller messenger.\n * @param snap.options - The simulation options.\n * @returns The Snap helpers.\n */\nexport function getHelpers({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n options,\n}: InstalledSnap & { options: SimulationOptions }): SnapHelpers {\n const onTransaction = async (\n request: TransactionOptions,\n ): Promise<SnapResponseWithInterface> => {\n log('Sending transaction %o.', request);\n\n const {\n origin: transactionOrigin,\n chainId,\n ...transaction\n } = create(request, TransactionOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnTransaction,\n request: {\n method: '',\n params: {\n chainId,\n transaction,\n transactionOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onCronjob = (request: CronjobOptions) => {\n log('Running cronjob %o.', options);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnCronjob,\n request,\n });\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onKeyringRequest = (request: KeyringOptions) => {\n log('Sending keyring request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnKeyringRequest,\n request,\n });\n };\n\n return {\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n request: (request) => {\n log('Sending request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnRpcRequest,\n request,\n });\n },\n\n onTransaction,\n sendTransaction: onTransaction,\n\n onKeyringRequest,\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onInstall: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onInstall handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnInstall,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onUpdate: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onUpdate handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnUpdate,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onStart: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onStart handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnStart,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n onNameLookup: async (\n nameLookupOptions: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Requesting name lookup %o.', nameLookupOptions);\n\n const params = create(nameLookupOptions, NameLookupOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnNameLookup,\n request: {\n method: '',\n params,\n },\n });\n\n return response;\n },\n\n onSignature: async (\n request: unknown,\n ): Promise<SnapResponseWithInterface> => {\n log('Requesting signature %o.', request);\n\n const { origin: signatureOrigin, ...signature } = create(\n request,\n SignatureOptionsStruct,\n );\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSignature,\n request: {\n method: '',\n params: {\n signature,\n signatureOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onCronjob,\n runCronjob: onCronjob,\n onBackgroundEvent: onCronjob,\n\n onHomePage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering home page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnHomePage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onSettingsPage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering settings page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSettingsPage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onProtocolRequest: async (\n scope,\n rawRequest,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Sending protocol request.');\n\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? 1,\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnProtocolRequest,\n request: {\n origin: rawRequest.origin,\n method: '',\n params: {\n scope,\n request,\n },\n },\n });\n\n return response;\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onClientRequest: (request) => {\n log('Sending client request.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnClientRequest,\n request,\n });\n },\n\n mockJsonRpc(mock: JsonRpcMockOptions) {\n log('Mocking JSON-RPC request %o.', mock);\n\n const { method, result } = create(mock, JsonRpcMockOptionsStruct);\n store.dispatch(addJsonRpcMock({ method, result }));\n\n return {\n unmock() {\n log('Unmocking JSON-RPC request %o.', mock);\n\n store.dispatch(removeJsonRpcMock(method));\n },\n };\n },\n\n close: async () => {\n log('Closing execution service.');\n await executionService.terminateAllSnaps();\n },\n };\n}\n"]}
1
+ {"version":3,"file":"helpers.cjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AACpD,uDAA+C;AAE/C,2CAAqD;AACrD,8CAA0C;AAE1C,yCAAsC;AAEtC,2CAA0C;AAE1C,6CAA4D;AAC5D,2CAMmB;AAcnB,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,mBAAU,EAAE,SAAS,CAAC,CAAC;AAwQtD;;;;;;;;;;;GAWG;AACH,SAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,GACwC;IAC/C,MAAM,aAAa,GAAG,KAAK,EACzB,OAA2B,EACS,EAAE;QACtC,GAAG,CAAC,yBAAyB,EAAE,OAAO,CAAC,CAAC;QAExC,MAAM,EACJ,MAAM,EAAE,iBAAiB,EACzB,OAAO,EACP,GAAG,WAAW,EACf,GAAG,IAAA,oBAAM,EAAC,OAAO,EAAE,kCAAwB,CAAC,CAAC;QAE9C,MAAM,QAAQ,GAAG,MAAM,IAAA,uBAAa,EAAC;YACnC,MAAM;YACN,KAAK;YACL,gBAAgB;YAChB,OAAO;YACP,mBAAmB;YACnB,iBAAiB,EAAE,OAAO;YAC1B,OAAO,EAAE,yBAAW,CAAC,aAAa;YAClC,OAAO,EAAE;gBACP,MAAM,EAAE,EAAE;gBACV,MAAM,EAAE;oBACN,OAAO;oBACP,WAAW;oBACX,iBAAiB;iBAClB;aACF;SACF,CAAC,CAAC;QAEH,IAAA,uCAA6B,EAAC,QAAQ,CAAC,CAAC;QAExC,OAAO,QAAQ,CAAC;IAClB,C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e;gBACpC,OAAO;aACR,CAAC,CAAC;QACL,CAAC;QAED,WAAW,CAAC,IAAwB;YAClC,OAAO,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAClC,CAAC;QAED,eAAe,CAAC,IAAwB;YACtC,OAAO,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;QAED,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,GAAG,CAAC,4BAA4B,CAAC,CAAC;YAClC,MAAM,gBAAgB,CAAC,iBAAiB,EAAE,CAAC;QAC7C,CAAC;KACF,CAAC;AACJ,CAAC;AAhWD,gCAgWC","sourcesContent":["import { HandlerType } from '@metamask/snaps-utils';\nimport { create } from '@metamask/superstruct';\nimport type { CaipChainId, JsonRpcRequest } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport { rootLogger } from './logger';\nimport type { SimulationOptions } from './options';\nimport { handleRequest } from './request';\nimport type { InstalledSnap } from './simulation';\nimport { addJsonRpcMock, removeJsonRpcMock } from './store';\nimport {\n assertIsResponseWithInterface,\n JsonRpcMockOptionsStruct,\n NameLookupOptionsStruct,\n SignatureOptionsStruct,\n TransactionOptionsStruct,\n} from './structs';\nimport type {\n CronjobOptions,\n JsonRpcMockOptions,\n KeyringOptions,\n NameLookupOptions,\n RequestOptions,\n SignatureOptions,\n SnapRequest,\n SnapResponseWithInterface,\n SnapResponseWithoutInterface,\n TransactionOptions,\n} from './types';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * This is the main entry point to interact with the snap. It is returned by\n * {@link installSnap}, and has methods to send requests to the snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * const snap = await installSnap();\n * const response = await snap.request({ method: 'hello' });\n *\n * expect(response).toRespondWith('Hello, world!');\n */\nexport type SnapHelpers = {\n /**\n * Send a JSON-RPC request to the snap.\n *\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n request(request: RequestOptions): SnapRequest;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n */\n onTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n * @deprecated Use {@link onTransaction} instead.\n */\n sendTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a signature request to the snap.\n *\n * @param signature - The signature request object. Contains the params from\n * the various signature methods, but has an extra `origin` and `signatureMethod` field.\n * Any missing fields will be filled in with default values.\n * @returns The response.\n */\n onSignature(\n signature?: Partial<SignatureOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onCronjob(cronjob?: Partial<CronjobOptions>): SnapRequest;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n * @deprecated Use {@link onCronjob} instead.\n */\n runCronjob(cronjob: CronjobOptions): SnapRequest;\n\n /**\n * Run a background event in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param backgroundEvent - The background event request. This is similar to a JSON-RPC\n * request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest;\n\n /**\n * Get the response from the snap's `onHomePage` method.\n *\n * @returns The response.\n */\n onHomePage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Get the response from the snap's `onSettingsPage` method.\n *\n * @returns The response.\n */\n onSettingsPage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a keyring request to the Snap.\n *\n * @param keyringRequest - Keyring request.\n * @returns The response.\n */\n onKeyringRequest(keyringRequest: KeyringOptions): SnapRequest;\n\n /**\n * Get the response from the Snap's `onInstall` handler.\n *\n * @returns The response.\n */\n onInstall(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onUpdate` handler.\n *\n * @returns The response.\n */\n onUpdate(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onStart` handler.\n *\n * @returns The response.\n */\n onStart(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onNameLookup` handler.\n *\n * @returns The response.\n */\n onNameLookup(\n request: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC protocol request to the Snap.\n *\n * @param scope - A CAIP-2 scope.\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onProtocolRequest(\n scope: CaipChainId,\n request: RequestOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC client request to the Snap.\n *\n * @param request - The JSON-RPC request.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Mock a JSON-RPC request. This will cause the snap to respond with the\n * specified response when a request with the specified method is sent.\n *\n * @param mock - The mock options.\n * @param mock.method - The JSON-RPC request method.\n * @param mock.result - The JSON-RPC response, which will be returned when a\n * request with the specified method is sent.\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpc({ method: 'eth_accounts', result: ['0x1234'] });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpc((request) => {\n * if (request.method === 'eth_accounts') {\n * return ['0x1234'];\n * }\n * });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n */\n mockJsonRpc(mock: JsonRpcMockOptions): {\n /**\n * Remove the mock.\n */\n unmock(): void;\n };\n\n /**\n * Mock a JSON-RPC request once. This will cause the snap to respond with the\n * specified response when a request with the specified method is sent.\n *\n * @param mock - The mock options.\n * @param mock.method - The JSON-RPC request method.\n * @param mock.result - The JSON-RPC response, which will be returned when a\n * request with the specified method is sent.\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpcOnce({ method: 'eth_accounts', result: ['0x1234'] });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n *\n * const response2 =\n * await ethereum.request({ method: 'eth_accounts' }); // Default behavior\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpcOnce((request) => {\n * if (request.method === 'eth_accounts') {\n * return ['0x1234'];\n * }\n * });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n *\n * const response2 =\n * await ethereum.request({ method: 'eth_accounts' }); // Default behavior\n */\n mockJsonRpcOnce(mock: JsonRpcMockOptions): {\n /**\n * Remove the mock.\n */\n unmock(): void;\n };\n\n /**\n * Close the page running the snap. This is mainly useful for cleaning up\n * the test environment, and calling it is not strictly necessary.\n *\n * @returns A promise that resolves when the page is closed.\n */\n close(): Promise<void>;\n};\n\n/**\n * Get the helper functions for the Snap.\n *\n * @param snap - The installed Snap.\n * @param snap.snapId - The ID of the Snap.\n * @param snap.store - The Redux store.\n * @param snap.executionService - The execution service.\n * @param snap.runSaga - The `runSaga` function.\n * @param snap.controllerMessenger - The controller messenger.\n * @param snap.options - The simulation options.\n * @returns The Snap helpers.\n */\nexport function getHelpers({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n options,\n}: InstalledSnap & { options: SimulationOptions }): SnapHelpers {\n const onTransaction = async (\n request: TransactionOptions,\n ): Promise<SnapResponseWithInterface> => {\n log('Sending transaction %o.', request);\n\n const {\n origin: transactionOrigin,\n chainId,\n ...transaction\n } = create(request, TransactionOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnTransaction,\n request: {\n method: '',\n params: {\n chainId,\n transaction,\n transactionOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onCronjob = (request: CronjobOptions) => {\n log('Running cronjob %o.', options);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnCronjob,\n request,\n });\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onKeyringRequest = (request: KeyringOptions) => {\n log('Sending keyring request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnKeyringRequest,\n request,\n });\n };\n\n const mockJsonRpc = (mock: JsonRpcMockOptions, once: boolean) => {\n log('Mocking JSON-RPC request %o.', mock);\n\n const id = nanoid();\n\n if (typeof mock === 'function') {\n store.dispatch(addJsonRpcMock({ id, implementation: mock, once }));\n } else {\n const { method, result } = create(mock, JsonRpcMockOptionsStruct);\n const implementation = (request: JsonRpcRequest) => {\n if (request.method === method) {\n return result;\n }\n return undefined;\n };\n store.dispatch(addJsonRpcMock({ id, implementation, once }));\n }\n\n return {\n unmock() {\n log('Unmocking JSON-RPC request %o.', mock);\n\n store.dispatch(removeJsonRpcMock(id));\n },\n };\n };\n\n return {\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n request: (request) => {\n log('Sending request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnRpcRequest,\n request,\n });\n },\n\n onTransaction,\n sendTransaction: onTransaction,\n\n onKeyringRequest,\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onInstall: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onInstall handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnInstall,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onUpdate: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onUpdate handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnUpdate,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onStart: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onStart handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnStart,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n onNameLookup: async (\n nameLookupOptions: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Requesting name lookup %o.', nameLookupOptions);\n\n const params = create(nameLookupOptions, NameLookupOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnNameLookup,\n request: {\n method: '',\n params,\n },\n });\n\n return response;\n },\n\n onSignature: async (\n request: unknown,\n ): Promise<SnapResponseWithInterface> => {\n log('Requesting signature %o.', request);\n\n const { origin: signatureOrigin, ...signature } = create(\n request,\n SignatureOptionsStruct,\n );\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSignature,\n request: {\n method: '',\n params: {\n signature,\n signatureOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onCronjob,\n runCronjob: onCronjob,\n onBackgroundEvent: onCronjob,\n\n onHomePage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering home page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnHomePage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onSettingsPage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering settings page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSettingsPage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onProtocolRequest: async (\n scope,\n rawRequest,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Sending protocol request.');\n\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? 1,\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnProtocolRequest,\n request: {\n origin: rawRequest.origin,\n method: '',\n params: {\n scope,\n request,\n },\n },\n });\n\n return response;\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onClientRequest: (request) => {\n log('Sending client request.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnClientRequest,\n request,\n });\n },\n\n mockJsonRpc(mock: JsonRpcMockOptions) {\n return mockJsonRpc(mock, false);\n },\n\n mockJsonRpcOnce(mock: JsonRpcMockOptions) {\n return mockJsonRpc(mock, true);\n },\n\n close: async () => {\n log('Closing execution service.');\n await executionService.terminateAllSnaps();\n },\n };\n}\n"]}
@@ -158,6 +158,21 @@ export type SnapHelpers = {
158
158
  * // In the Snap
159
159
  * const response =
160
160
  * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
161
+ *
162
+ * @example
163
+ * import { installSnap } from '@metamask/snaps-jest';
164
+ *
165
+ * // In the test
166
+ * const snap = await installSnap();
167
+ * snap.mockJsonRpc((request) => {
168
+ * if (request.method === 'eth_accounts') {
169
+ * return ['0x1234'];
170
+ * }
171
+ * });
172
+ *
173
+ * // In the Snap
174
+ * const response =
175
+ * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
161
176
  */
162
177
  mockJsonRpc(mock: JsonRpcMockOptions): {
163
178
  /**
@@ -165,6 +180,52 @@ export type SnapHelpers = {
165
180
  */
166
181
  unmock(): void;
167
182
  };
183
+ /**
184
+ * Mock a JSON-RPC request once. This will cause the snap to respond with the
185
+ * specified response when a request with the specified method is sent.
186
+ *
187
+ * @param mock - The mock options.
188
+ * @param mock.method - The JSON-RPC request method.
189
+ * @param mock.result - The JSON-RPC response, which will be returned when a
190
+ * request with the specified method is sent.
191
+ * @example
192
+ * import { installSnap } from '@metamask/snaps-jest';
193
+ *
194
+ * // In the test
195
+ * const snap = await installSnap();
196
+ * snap.mockJsonRpcOnce({ method: 'eth_accounts', result: ['0x1234'] });
197
+ *
198
+ * // In the Snap
199
+ * const response =
200
+ * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
201
+ *
202
+ * const response2 =
203
+ * await ethereum.request({ method: 'eth_accounts' }); // Default behavior
204
+ *
205
+ * @example
206
+ * import { installSnap } from '@metamask/snaps-jest';
207
+ *
208
+ * // In the test
209
+ * const snap = await installSnap();
210
+ * snap.mockJsonRpcOnce((request) => {
211
+ * if (request.method === 'eth_accounts') {
212
+ * return ['0x1234'];
213
+ * }
214
+ * });
215
+ *
216
+ * // In the Snap
217
+ * const response =
218
+ * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
219
+ *
220
+ * const response2 =
221
+ * await ethereum.request({ method: 'eth_accounts' }); // Default behavior
222
+ */
223
+ mockJsonRpcOnce(mock: JsonRpcMockOptions): {
224
+ /**
225
+ * Remove the mock.
226
+ */
227
+ unmock(): void;
228
+ };
168
229
  /**
169
230
  * Close the page running the snap. This is mainly useful for cleaning up
170
231
  * the test environment, and calling it is not strictly necessary.
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.cts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAwB;AAInD,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAkB;AAEnD,OAAO,KAAK,EAAE,aAAa,EAAE,yBAAqB;AASlD,OAAO,KAAK,EACV,cAAc,EACd,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EAChB,WAAW,EACX,yBAAyB,EACzB,4BAA4B,EAC5B,kBAAkB,EACnB,oBAAgB;AAIjB;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB;;;;;;OAMG;IACH,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,WAAW,CAAC;IAE9C;;;;;;;OAOG;IACH,aAAa,CACX,W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CAAC,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,GAAG,WAAW,CAAC;IAEtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,WAAW,CAAC,IAAI,EAAE,kBAAkB,GAAG;QACrC;;WAEG;QACH,MAAM,IAAI,IAAI,CAAC;KAChB,CAAC;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;IACH,eAAe,CAAC,IAAI,EAAE,kBAAkB,GAAG;QACzC;;WAEG;QACH,MAAM,IAAI,IAAI,CAAC;KAChB,CAAC;IAEF;;;;;OAKG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,GACR,EAAE,aAAa,GAAG;IAAE,OAAO,EAAE,iBAAiB,CAAA;CAAE,GAAG,WAAW,CAyV9D"}
@@ -158,6 +158,21 @@ export type SnapHelpers = {
158
158
  * // In the Snap
159
159
  * const response =
160
160
  * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
161
+ *
162
+ * @example
163
+ * import { installSnap } from '@metamask/snaps-jest';
164
+ *
165
+ * // In the test
166
+ * const snap = await installSnap();
167
+ * snap.mockJsonRpc((request) => {
168
+ * if (request.method === 'eth_accounts') {
169
+ * return ['0x1234'];
170
+ * }
171
+ * });
172
+ *
173
+ * // In the Snap
174
+ * const response =
175
+ * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
161
176
  */
162
177
  mockJsonRpc(mock: JsonRpcMockOptions): {
163
178
  /**
@@ -165,6 +180,52 @@ export type SnapHelpers = {
165
180
  */
166
181
  unmock(): void;
167
182
  };
183
+ /**
184
+ * Mock a JSON-RPC request once. This will cause the snap to respond with the
185
+ * specified response when a request with the specified method is sent.
186
+ *
187
+ * @param mock - The mock options.
188
+ * @param mock.method - The JSON-RPC request method.
189
+ * @param mock.result - The JSON-RPC response, which will be returned when a
190
+ * request with the specified method is sent.
191
+ * @example
192
+ * import { installSnap } from '@metamask/snaps-jest';
193
+ *
194
+ * // In the test
195
+ * const snap = await installSnap();
196
+ * snap.mockJsonRpcOnce({ method: 'eth_accounts', result: ['0x1234'] });
197
+ *
198
+ * // In the Snap
199
+ * const response =
200
+ * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
201
+ *
202
+ * const response2 =
203
+ * await ethereum.request({ method: 'eth_accounts' }); // Default behavior
204
+ *
205
+ * @example
206
+ * import { installSnap } from '@metamask/snaps-jest';
207
+ *
208
+ * // In the test
209
+ * const snap = await installSnap();
210
+ * snap.mockJsonRpcOnce((request) => {
211
+ * if (request.method === 'eth_accounts') {
212
+ * return ['0x1234'];
213
+ * }
214
+ * });
215
+ *
216
+ * // In the Snap
217
+ * const response =
218
+ * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']
219
+ *
220
+ * const response2 =
221
+ * await ethereum.request({ method: 'eth_accounts' }); // Default behavior
222
+ */
223
+ mockJsonRpcOnce(mock: JsonRpcMockOptions): {
224
+ /**
225
+ * Remove the mock.
226
+ */
227
+ unmock(): void;
228
+ };
168
229
  /**
169
230
  * Close the page running the snap. This is mainly useful for cleaning up
170
231
  * the test environment, and calling it is not strictly necessary.
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.mts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAwB;AAInD,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAkB;AAEnD,OAAO,KAAK,EAAE,aAAa,EAAE,yBAAqB;AASlD,OAAO,KAAK,EACV,cAAc,EACd,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EAChB,WAAW,EACX,yBAAyB,EACzB,4BAA4B,EAC5B,kBAAkB,EACnB,oBA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BAA4B,CAAC,CAAC;IAEzC;;;;;;;OAOG;IACH,iBAAiB,CACf,KAAK,EAAE,WAAW,EAClB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,4BAA4B,CAAC,CAAC;IAEzC;;;;;OAKG;IACH,eAAe,CAAC,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,GAAG,WAAW,CAAC;IAEtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,WAAW,CAAC,IAAI,EAAE,kBAAkB,GAAG;QACrC;;WAEG;QACH,MAAM,IAAI,IAAI,CAAC;KAChB,CAAC;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;IACH,eAAe,CAAC,IAAI,EAAE,kBAAkB,GAAG;QACzC;;WAEG;QACH,MAAM,IAAI,IAAI,CAAC;KAChB,CAAC;IAEF;;;;;OAKG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,GACR,EAAE,aAAa,GAAG;IAAE,OAAO,EAAE,iBAAiB,CAAA;CAAE,GAAG,WAAW,CAyV9D"}
package/dist/helpers.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
  import { HandlerType } from "@metamask/snaps-utils";
2
2
  import { create } from "@metamask/superstruct";
3
3
  import { createModuleLogger } from "@metamask/utils";
4
+ import $reduxjstoolkit from "@reduxjs/toolkit";
5
+ const { nanoid } = $reduxjstoolkit;
4
6
  import { rootLogger } from "./logger.mjs";
5
7
  import { handleRequest } from "./request.mjs";
6
8
  import { addJsonRpcMock, removeJsonRpcMock } from "./store/index.mjs";
@@ -72,6 +74,29 @@ export function getHelpers({ snapId, store, executionService, runSaga, controlle
72
74
  request,
73
75
  });
74
76
  };
77
+ const mockJsonRpc = (mock, once) => {
78
+ log('Mocking JSON-RPC request %o.', mock);
79
+ const id = nanoid();
80
+ if (typeof mock === 'function') {
81
+ store.dispatch(addJsonRpcMock({ id, implementation: mock, once }));
82
+ }
83
+ else {
84
+ const { method, result } = create(mock, JsonRpcMockOptionsStruct);
85
+ const implementation = (request) => {
86
+ if (request.method === method) {
87
+ return result;
88
+ }
89
+ return undefined;
90
+ };
91
+ store.dispatch(addJsonRpcMock({ id, implementation, once }));
92
+ }
93
+ return {
94
+ unmock() {
95
+ log('Unmocking JSON-RPC request %o.', mock);
96
+ store.dispatch(removeJsonRpcMock(id));
97
+ },
98
+ };
99
+ };
75
100
  return {
76
101
  // This can't be async because it returns a `SnapRequest`.
77
102
  // eslint-disable-next-line @typescript-eslint/promise-function-async
@@ -265,15 +290,10 @@ export function getHelpers({ snapId, store, executionService, runSaga, controlle
265
290
  });
266
291
  },
267
292
  mockJsonRpc(mock) {
268
- log('Mocking JSON-RPC request %o.', mock);
269
- const { method, result } = create(mock, JsonRpcMockOptionsStruct);
270
- store.dispatch(addJsonRpcMock({ method, result }));
271
- return {
272
- unmock() {
273
- log('Unmocking JSON-RPC request %o.', mock);
274
- store.dispatch(removeJsonRpcMock(method));
275
- },
276
- };
293
+ return mockJsonRpc(mock, false);
294
+ },
295
+ mockJsonRpcOnce(mock) {
296
+ return mockJsonRpc(mock, true);
277
297
  },
278
298
  close: async () => {
279
299
  log('Closing execution service.');
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.mjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAE/C,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,UAAU,EAAE,qBAAiB;AAEtC,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAE1C,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,0BAAgB;AAC5D,OAAO,EACL,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;YAClE,KAAK,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;YAEnD,OAAO;gBACL,MAAM;oBACJ,GAAG,CAAC,gCAAgC,EAAE,IAAI,CAAC,CAAC;oBAE5C,KAAK,CAAC,QAAQ,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;gBAC5C,CAAC;aACF,CAAC;QACJ,CAAC;QAED,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,GAAG,CAAC,4BAA4B,CAAC,CAAC;YAClC,MAAM,gBAAgB,CAAC,iBAAiB,EAAE,CAAC;QAC7C,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { HandlerType } from '@metamask/snaps-utils';\nimport { create } from '@metamask/superstruct';\nimport type { CaipChainId } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n\nimport { rootLogger } from './logger';\nimport type { SimulationOptions } from './options';\nimport { handleRequest } from './request';\nimport type { InstalledSnap } from './simulation';\nimport { addJsonRpcMock, removeJsonRpcMock } from './store';\nimport {\n assertIsResponseWithInterface,\n JsonRpcMockOptionsStruct,\n NameLookupOptionsStruct,\n SignatureOptionsStruct,\n TransactionOptionsStruct,\n} from './structs';\nimport type {\n CronjobOptions,\n JsonRpcMockOptions,\n KeyringOptions,\n NameLookupOptions,\n RequestOptions,\n SignatureOptions,\n SnapRequest,\n SnapResponseWithInterface,\n SnapResponseWithoutInterface,\n TransactionOptions,\n} from './types';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * This is the main entry point to interact with the snap. It is returned by\n * {@link installSnap}, and has methods to send requests to the snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * const snap = await installSnap();\n * const response = await snap.request({ method: 'hello' });\n *\n * expect(response).toRespondWith('Hello, world!');\n */\nexport type SnapHelpers = {\n /**\n * Send a JSON-RPC request to the snap.\n *\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n request(request: RequestOptions): SnapRequest;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n */\n onTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n * @deprecated Use {@link onTransaction} instead.\n */\n sendTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a signature request to the snap.\n *\n * @param signature - The signature request object. Contains the params from\n * the various signature methods, but has an extra `origin` and `signatureMethod` field.\n * Any missing fields will be filled in with default values.\n * @returns The response.\n */\n onSignature(\n signature?: Partial<SignatureOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onCronjob(cronjob?: Partial<CronjobOptions>): SnapRequest;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n * @deprecated Use {@link onCronjob} instead.\n */\n runCronjob(cronjob: CronjobOptions): SnapRequest;\n\n /**\n * Run a background event in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param backgroundEvent - The background event request. This is similar to a JSON-RPC\n * request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest;\n\n /**\n * Get the response from the snap's `onHomePage` method.\n *\n * @returns The response.\n */\n onHomePage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Get the response from the snap's `onSettingsPage` method.\n *\n * @returns The response.\n */\n onSettingsPage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a keyring request to the Snap.\n *\n * @param keyringRequest - Keyring request.\n * @returns The response.\n */\n onKeyringRequest(keyringRequest: KeyringOptions): SnapRequest;\n\n /**\n * Get the response from the Snap's `onInstall` handler.\n *\n * @returns The response.\n */\n onInstall(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onUpdate` handler.\n *\n * @returns The response.\n */\n onUpdate(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onStart` handler.\n *\n * @returns The response.\n */\n onStart(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onNameLookup` handler.\n *\n * @returns The response.\n */\n onNameLookup(\n request: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC protocol request to the Snap.\n *\n * @param scope - A CAIP-2 scope.\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onProtocolRequest(\n scope: CaipChainId,\n request: RequestOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC client request to the Snap.\n *\n * @param request - The JSON-RPC request.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Mock a JSON-RPC request. This will cause the snap to respond with the\n * specified response when a request with the specified method is sent.\n *\n * @param mock - The mock options.\n * @param mock.method - The JSON-RPC request method.\n * @param mock.result - The JSON-RPC response, which will be returned when a\n * request with the specified method is sent.\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpc({ method: 'eth_accounts', result: ['0x1234'] });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n */\n mockJsonRpc(mock: JsonRpcMockOptions): {\n /**\n * Remove the mock.\n */\n unmock(): void;\n };\n\n /**\n * Close the page running the snap. This is mainly useful for cleaning up\n * the test environment, and calling it is not strictly necessary.\n *\n * @returns A promise that resolves when the page is closed.\n */\n close(): Promise<void>;\n};\n\n/**\n * Get the helper functions for the Snap.\n *\n * @param snap - The installed Snap.\n * @param snap.snapId - The ID of the Snap.\n * @param snap.store - The Redux store.\n * @param snap.executionService - The execution service.\n * @param snap.runSaga - The `runSaga` function.\n * @param snap.controllerMessenger - The controller messenger.\n * @param snap.options - The simulation options.\n * @returns The Snap helpers.\n */\nexport function getHelpers({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n options,\n}: InstalledSnap & { options: SimulationOptions }): SnapHelpers {\n const onTransaction = async (\n request: TransactionOptions,\n ): Promise<SnapResponseWithInterface> => {\n log('Sending transaction %o.', request);\n\n const {\n origin: transactionOrigin,\n chainId,\n ...transaction\n } = create(request, TransactionOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnTransaction,\n request: {\n method: '',\n params: {\n chainId,\n transaction,\n transactionOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onCronjob = (request: CronjobOptions) => {\n log('Running cronjob %o.', options);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnCronjob,\n request,\n });\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onKeyringRequest = (request: KeyringOptions) => {\n log('Sending keyring request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnKeyringRequest,\n request,\n });\n };\n\n return {\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n request: (request) => {\n log('Sending request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnRpcRequest,\n request,\n });\n },\n\n onTransaction,\n sendTransaction: onTransaction,\n\n onKeyringRequest,\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onInstall: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onInstall handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnInstall,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onUpdate: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onUpdate handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnUpdate,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onStart: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onStart handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnStart,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n onNameLookup: async (\n nameLookupOptions: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Requesting name lookup %o.', nameLookupOptions);\n\n const params = create(nameLookupOptions, NameLookupOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnNameLookup,\n request: {\n method: '',\n params,\n },\n });\n\n return response;\n },\n\n onSignature: async (\n request: unknown,\n ): Promise<SnapResponseWithInterface> => {\n log('Requesting signature %o.', request);\n\n const { origin: signatureOrigin, ...signature } = create(\n request,\n SignatureOptionsStruct,\n );\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSignature,\n request: {\n method: '',\n params: {\n signature,\n signatureOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onCronjob,\n runCronjob: onCronjob,\n onBackgroundEvent: onCronjob,\n\n onHomePage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering home page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnHomePage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onSettingsPage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering settings page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSettingsPage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onProtocolRequest: async (\n scope,\n rawRequest,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Sending protocol request.');\n\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? 1,\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnProtocolRequest,\n request: {\n origin: rawRequest.origin,\n method: '',\n params: {\n scope,\n request,\n },\n },\n });\n\n return response;\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onClientRequest: (request) => {\n log('Sending client request.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnClientRequest,\n request,\n });\n },\n\n mockJsonRpc(mock: JsonRpcMockOptions) {\n log('Mocking JSON-RPC request %o.', mock);\n\n const { method, result } = create(mock, JsonRpcMockOptionsStruct);\n store.dispatch(addJsonRpcMock({ method, result }));\n\n return {\n unmock() {\n log('Unmocking JSON-RPC request %o.', mock);\n\n store.dispatch(removeJsonRpcMock(method));\n },\n };\n },\n\n close: async () => {\n log('Closing execution service.');\n await executionService.terminateAllSnaps();\n },\n };\n}\n"]}
1
+ {"version":3,"file":"helpers.mjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAE/C,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;;;AAGrD,OAAO,EAAE,UAAU,EAAE,qBAAiB;AAEtC,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAE1C,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,0BAAgB;AAC5D,OAAO,EACL,6BAA6B,EAC7B,wBAAwB,EACxB,uBAAuB,EACvB,sBAAsB,EACtB,wBAAwB,EACzB,sBAAkB;AAcnB,MAAM,GAAG,GAAG,kBAAkB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;AAwQtD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,UAAU,CAAC,EACzB,MAAM,EACN,KAAK,EACL,gBAAgB,EAChB,OAAO,EACP,mBAAmB,EACnB,OAAO,GACwC;IAC/C,MAAM,aAAa,GAAG,KAAK,EACzB,OAA2B,EACS,EAAE;QACtC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MAAM,EAAE;wBACN,KAAK;wBACL,OAAO;qBACR;iBACF;aACF,CAAC,CAAC;YAEH,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,0DAA0D;QAC1D,qEAAqE;QACrE,eAAe,EAAE,CAAC,OAAO,EAAE,EAAE;YAC3B,GAAG,CAAC,yBAAyB,CAAC,CAAC;YAE/B,OAAO,aAAa,CAAC;gBACnB,MAAM;gBACN,KAAK;gBACL,gBAAgB;gBAChB,mBAAmB;gBACnB,iBAAiB,EAAE,OAAO;gBAC1B,OAAO;gBACP,OAAO,EAAE,WAAW,CAAC,eAAe;gBACpC,OAAO;aACR,CAAC,CAAC;QACL,CAAC;QAED,WAAW,CAAC,IAAwB;YAClC,OAAO,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAClC,CAAC;QAED,eAAe,CAAC,IAAwB;YACtC,OAAO,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;QAED,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,GAAG,CAAC,4BAA4B,CAAC,CAAC;YAClC,MAAM,gBAAgB,CAAC,iBAAiB,EAAE,CAAC;QAC7C,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { HandlerType } from '@metamask/snaps-utils';\nimport { create } from '@metamask/superstruct';\nimport type { CaipChainId, JsonRpcRequest } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport { nanoid } from '@reduxjs/toolkit';\n\nimport { rootLogger } from './logger';\nimport type { SimulationOptions } from './options';\nimport { handleRequest } from './request';\nimport type { InstalledSnap } from './simulation';\nimport { addJsonRpcMock, removeJsonRpcMock } from './store';\nimport {\n assertIsResponseWithInterface,\n JsonRpcMockOptionsStruct,\n NameLookupOptionsStruct,\n SignatureOptionsStruct,\n TransactionOptionsStruct,\n} from './structs';\nimport type {\n CronjobOptions,\n JsonRpcMockOptions,\n KeyringOptions,\n NameLookupOptions,\n RequestOptions,\n SignatureOptions,\n SnapRequest,\n SnapResponseWithInterface,\n SnapResponseWithoutInterface,\n TransactionOptions,\n} from './types';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * This is the main entry point to interact with the snap. It is returned by\n * {@link installSnap}, and has methods to send requests to the snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * const snap = await installSnap();\n * const response = await snap.request({ method: 'hello' });\n *\n * expect(response).toRespondWith('Hello, world!');\n */\nexport type SnapHelpers = {\n /**\n * Send a JSON-RPC request to the snap.\n *\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n request(request: RequestOptions): SnapRequest;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n */\n onTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a transaction to the snap.\n *\n * @param transaction - The transaction. This is similar to an Ethereum\n * transaction object, but has an extra `origin` field. Any missing fields\n * will be filled in with default values.\n * @returns The response.\n * @deprecated Use {@link onTransaction} instead.\n */\n sendTransaction(\n transaction?: Partial<TransactionOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a signature request to the snap.\n *\n * @param signature - The signature request object. Contains the params from\n * the various signature methods, but has an extra `origin` and `signatureMethod` field.\n * Any missing fields will be filled in with default values.\n * @returns The response.\n */\n onSignature(\n signature?: Partial<SignatureOptions>,\n ): Promise<SnapResponseWithInterface>;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onCronjob(cronjob?: Partial<CronjobOptions>): SnapRequest;\n\n /**\n * Run a cronjob in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param cronjob - The cronjob request. This is similar to a JSON-RPC\n * request, and is normally specified in the snap manifest, under the\n * `endowment:cronjob` permission.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n * @deprecated Use {@link onCronjob} instead.\n */\n runCronjob(cronjob: CronjobOptions): SnapRequest;\n\n /**\n * Run a background event in the snap. This is similar to {@link request}, but the\n * request will be sent to the `onCronjob` method of the snap.\n *\n * @param backgroundEvent - The background event request. This is similar to a JSON-RPC\n * request, and is normally specified in the `request` param of the `snap_scheduleBackgroundEvent` method.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onBackgroundEvent(backgroundEvent: CronjobOptions): SnapRequest;\n\n /**\n * Get the response from the snap's `onHomePage` method.\n *\n * @returns The response.\n */\n onHomePage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Get the response from the snap's `onSettingsPage` method.\n *\n * @returns The response.\n */\n onSettingsPage(): Promise<SnapResponseWithInterface>;\n\n /**\n * Send a keyring request to the Snap.\n *\n * @param keyringRequest - Keyring request.\n * @returns The response.\n */\n onKeyringRequest(keyringRequest: KeyringOptions): SnapRequest;\n\n /**\n * Get the response from the Snap's `onInstall` handler.\n *\n * @returns The response.\n */\n onInstall(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onUpdate` handler.\n *\n * @returns The response.\n */\n onUpdate(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onStart` handler.\n *\n * @returns The response.\n */\n onStart(request?: Pick<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Get the response from the Snap's `onNameLookup` handler.\n *\n * @returns The response.\n */\n onNameLookup(\n request: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC protocol request to the Snap.\n *\n * @param scope - A CAIP-2 scope.\n * @param request - The request. This is similar to a JSON-RPC request, but\n * has an extra `origin` field.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onProtocolRequest(\n scope: CaipChainId,\n request: RequestOptions,\n ): Promise<SnapResponseWithoutInterface>;\n\n /**\n * Send a JSON-RPC client request to the Snap.\n *\n * @param request - The JSON-RPC request.\n * @returns The response promise, with extra {@link SnapRequestObject} fields.\n */\n onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;\n\n /**\n * Mock a JSON-RPC request. This will cause the snap to respond with the\n * specified response when a request with the specified method is sent.\n *\n * @param mock - The mock options.\n * @param mock.method - The JSON-RPC request method.\n * @param mock.result - The JSON-RPC response, which will be returned when a\n * request with the specified method is sent.\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpc({ method: 'eth_accounts', result: ['0x1234'] });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpc((request) => {\n * if (request.method === 'eth_accounts') {\n * return ['0x1234'];\n * }\n * });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n */\n mockJsonRpc(mock: JsonRpcMockOptions): {\n /**\n * Remove the mock.\n */\n unmock(): void;\n };\n\n /**\n * Mock a JSON-RPC request once. This will cause the snap to respond with the\n * specified response when a request with the specified method is sent.\n *\n * @param mock - The mock options.\n * @param mock.method - The JSON-RPC request method.\n * @param mock.result - The JSON-RPC response, which will be returned when a\n * request with the specified method is sent.\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpcOnce({ method: 'eth_accounts', result: ['0x1234'] });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n *\n * const response2 =\n * await ethereum.request({ method: 'eth_accounts' }); // Default behavior\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * // In the test\n * const snap = await installSnap();\n * snap.mockJsonRpcOnce((request) => {\n * if (request.method === 'eth_accounts') {\n * return ['0x1234'];\n * }\n * });\n *\n * // In the Snap\n * const response =\n * await ethereum.request({ method: 'eth_accounts' }); // ['0x1234']\n *\n * const response2 =\n * await ethereum.request({ method: 'eth_accounts' }); // Default behavior\n */\n mockJsonRpcOnce(mock: JsonRpcMockOptions): {\n /**\n * Remove the mock.\n */\n unmock(): void;\n };\n\n /**\n * Close the page running the snap. This is mainly useful for cleaning up\n * the test environment, and calling it is not strictly necessary.\n *\n * @returns A promise that resolves when the page is closed.\n */\n close(): Promise<void>;\n};\n\n/**\n * Get the helper functions for the Snap.\n *\n * @param snap - The installed Snap.\n * @param snap.snapId - The ID of the Snap.\n * @param snap.store - The Redux store.\n * @param snap.executionService - The execution service.\n * @param snap.runSaga - The `runSaga` function.\n * @param snap.controllerMessenger - The controller messenger.\n * @param snap.options - The simulation options.\n * @returns The Snap helpers.\n */\nexport function getHelpers({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n options,\n}: InstalledSnap & { options: SimulationOptions }): SnapHelpers {\n const onTransaction = async (\n request: TransactionOptions,\n ): Promise<SnapResponseWithInterface> => {\n log('Sending transaction %o.', request);\n\n const {\n origin: transactionOrigin,\n chainId,\n ...transaction\n } = create(request, TransactionOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnTransaction,\n request: {\n method: '',\n params: {\n chainId,\n transaction,\n transactionOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onCronjob = (request: CronjobOptions) => {\n log('Running cronjob %o.', options);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnCronjob,\n request,\n });\n };\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const onKeyringRequest = (request: KeyringOptions) => {\n log('Sending keyring request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n runSaga,\n controllerMessenger,\n simulationOptions: options,\n handler: HandlerType.OnKeyringRequest,\n request,\n });\n };\n\n const mockJsonRpc = (mock: JsonRpcMockOptions, once: boolean) => {\n log('Mocking JSON-RPC request %o.', mock);\n\n const id = nanoid();\n\n if (typeof mock === 'function') {\n store.dispatch(addJsonRpcMock({ id, implementation: mock, once }));\n } else {\n const { method, result } = create(mock, JsonRpcMockOptionsStruct);\n const implementation = (request: JsonRpcRequest) => {\n if (request.method === method) {\n return result;\n }\n return undefined;\n };\n store.dispatch(addJsonRpcMock({ id, implementation, once }));\n }\n\n return {\n unmock() {\n log('Unmocking JSON-RPC request %o.', mock);\n\n store.dispatch(removeJsonRpcMock(id));\n },\n };\n };\n\n return {\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n request: (request) => {\n log('Sending request %o.', request);\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnRpcRequest,\n request,\n });\n },\n\n onTransaction,\n sendTransaction: onTransaction,\n\n onKeyringRequest,\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onInstall: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onInstall handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnInstall,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onUpdate: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onUpdate handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnUpdate,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onStart: (request?: Pick<RequestOptions, 'origin'>) => {\n log('Running onStart handler.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnStart,\n request: {\n method: '',\n ...request,\n },\n });\n },\n\n onNameLookup: async (\n nameLookupOptions: NameLookupOptions,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Requesting name lookup %o.', nameLookupOptions);\n\n const params = create(nameLookupOptions, NameLookupOptionsStruct);\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnNameLookup,\n request: {\n method: '',\n params,\n },\n });\n\n return response;\n },\n\n onSignature: async (\n request: unknown,\n ): Promise<SnapResponseWithInterface> => {\n log('Requesting signature %o.', request);\n\n const { origin: signatureOrigin, ...signature } = create(\n request,\n SignatureOptionsStruct,\n );\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSignature,\n request: {\n method: '',\n params: {\n signature,\n signatureOrigin,\n },\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onCronjob,\n runCronjob: onCronjob,\n onBackgroundEvent: onCronjob,\n\n onHomePage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering home page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnHomePage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onSettingsPage: async (): Promise<SnapResponseWithInterface> => {\n log('Rendering settings page.');\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnSettingsPage,\n request: {\n method: '',\n },\n });\n\n assertIsResponseWithInterface(response);\n\n return response;\n },\n\n onProtocolRequest: async (\n scope,\n rawRequest,\n ): Promise<SnapResponseWithoutInterface> => {\n log('Sending protocol request.');\n\n const request = {\n jsonrpc: '2.0' as const,\n id: rawRequest.id ?? 1,\n method: rawRequest.method,\n ...(rawRequest.params ? { params: rawRequest.params } : {}),\n };\n\n const response = await handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnProtocolRequest,\n request: {\n origin: rawRequest.origin,\n method: '',\n params: {\n scope,\n request,\n },\n },\n });\n\n return response;\n },\n\n // This can't be async because it returns a `SnapRequest`.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n onClientRequest: (request) => {\n log('Sending client request.');\n\n return handleRequest({\n snapId,\n store,\n executionService,\n controllerMessenger,\n simulationOptions: options,\n runSaga,\n handler: HandlerType.OnClientRequest,\n request,\n });\n },\n\n mockJsonRpc(mock: JsonRpcMockOptions) {\n return mockJsonRpc(mock, false);\n },\n\n mockJsonRpcOnce(mock: JsonRpcMockOptions) {\n return mockJsonRpc(mock, true);\n },\n\n close: async () => {\n log('Closing execution service.');\n await executionService.terminateAllSnaps();\n },\n };\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createMockMiddleware = void 0;
4
+ const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
4
5
  const mocks_1 = require("../store/mocks.cjs");
5
6
  /**
6
7
  * Create a middleware for handling JSON-RPC methods that have been mocked.
@@ -9,14 +10,22 @@ const mocks_1 = require("../store/mocks.cjs");
9
10
  * @returns A middleware function.
10
11
  */
11
12
  function createMockMiddleware(store) {
12
- return function mockMiddleware(request, response, next, end) {
13
- const result = (0, mocks_1.getJsonRpcMock)(store.getState(), request.method);
14
- if (result) {
15
- response.result = result;
16
- return end();
13
+ return (0, json_rpc_engine_1.createAsyncMiddleware)(async (request, response, next) => {
14
+ const mocks = (0, mocks_1.getJsonRpcMocks)(store.getState());
15
+ const keys = Object.keys(mocks);
16
+ for (const key of keys) {
17
+ const { implementation, once } = mocks[key];
18
+ const result = await implementation(request);
19
+ if (result !== undefined) {
20
+ if (once) {
21
+ store.dispatch((0, mocks_1.removeJsonRpcMock)(key));
22
+ }
23
+ response.result = result;
24
+ return;
25
+ }
17
26
  }
18
- return next();
19
- };
27
+ await next();
28
+ });
20
29
  }
21
30
  exports.createMockMiddleware = createMockMiddleware;
22
31
  //# sourceMappingURL=mock.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"mock.cjs","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":";;;AAIA,8CAAgD;AAEhD;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAY;IAEZ,OAAO,SAAS,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG;QACzD,MAAM,MAAM,GAAG,IAAA,sBAAc,EAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QAChE,IAAI,MAAM,EAAE,CAAC;YACX,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;YACzB,OAAO,GAAG,EAAE,CAAC;QACf,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAZD,oDAYC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { Store } from '../store';\nimport { getJsonRpcMock } from '../store/mocks';\n\n/**\n * Create a middleware for handling JSON-RPC methods that have been mocked.\n *\n * @param store - The Redux store to use.\n * @returns A middleware function.\n */\nexport function createMockMiddleware(\n store: Store,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n return function mockMiddleware(request, response, next, end) {\n const result = getJsonRpcMock(store.getState(), request.method);\n if (result) {\n response.result = result;\n return end();\n }\n\n return next();\n };\n}\n"]}
1
+ {"version":3,"file":"mock.cjs","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":";;;AAAA,+DAGmC;AAInC,8CAAoE;AAEpE;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAY;IAEZ,OAAO,IAAA,uCAAqB,EAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QAC7D,MAAM,KAAK,GAAG,IAAA,uBAAe,EAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;QAChD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,MAAM,EAAE,cAAc,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;YAC5C,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,OAAO,CAAC,CAAC;YAE7C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACzB,IAAI,IAAI,EAAE,CAAC;oBACT,KAAK,CAAC,QAAQ,CAAC,IAAA,yBAAiB,EAAC,GAAG,CAAC,CAAC,CAAC;gBACzC,CAAC;gBAED,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;gBACzB,OAAO;YACT,CAAC;QACH,CAAC;QAED,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;AACL,CAAC;AAtBD,oDAsBC","sourcesContent":["import {\n createAsyncMiddleware,\n type JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport type { Store } from '../store';\nimport { getJsonRpcMocks, removeJsonRpcMock } from '../store/mocks';\n\n/**\n * Create a middleware for handling JSON-RPC methods that have been mocked.\n *\n * @param store - The Redux store to use.\n * @returns A middleware function.\n */\nexport function createMockMiddleware(\n store: Store,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n return createAsyncMiddleware(async (request, response, next) => {\n const mocks = getJsonRpcMocks(store.getState());\n const keys = Object.keys(mocks);\n for (const key of keys) {\n const { implementation, once } = mocks[key];\n const result = await implementation(request);\n\n if (result !== undefined) {\n if (once) {\n store.dispatch(removeJsonRpcMock(key));\n }\n\n response.result = result;\n return;\n }\n }\n\n await next();\n });\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
1
+ import { type JsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
2
  import type { Json, JsonRpcParams } from "@metamask/utils";
3
3
  import type { Store } from "../store/index.cjs";
4
4
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"mock.d.cts","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAGtC;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,KAAK,GACX,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAUxC"}
1
+ {"version":3,"file":"mock.d.cts","sourceRoot":"","sources":["../../src/middleware/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,iBAAiB,EACvB,kCAAkC;AACnC,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,KAAK,EAAE,2BAAiB;AAGtC;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,KAAK,GACX,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAoBxC"}