@metamask/snaps-jest 4.0.0 → 5.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 (217) hide show
  1. package/CHANGELOG.md +21 -1
  2. package/README.md +66 -179
  3. package/dist/cjs/environment.js +21 -82
  4. package/dist/cjs/environment.js.map +1 -1
  5. package/dist/cjs/helpers.js +118 -43
  6. package/dist/cjs/helpers.js.map +1 -1
  7. package/dist/cjs/internals/environment.js +1 -1
  8. package/dist/cjs/internals/environment.js.map +1 -1
  9. package/dist/cjs/internals/index.js +1 -4
  10. package/dist/cjs/internals/index.js.map +1 -1
  11. package/dist/cjs/internals/request.js +42 -94
  12. package/dist/cjs/internals/request.js.map +1 -1
  13. package/dist/cjs/internals/server.js +0 -4
  14. package/dist/cjs/internals/server.js.map +1 -1
  15. package/dist/cjs/internals/simulation/constants.js +29 -0
  16. package/dist/cjs/internals/simulation/constants.js.map +1 -0
  17. package/dist/cjs/internals/simulation/controllers.js +95 -0
  18. package/dist/cjs/internals/simulation/controllers.js.map +1 -0
  19. package/dist/cjs/internals/simulation/files.js +22 -0
  20. package/dist/cjs/internals/simulation/files.js.map +1 -0
  21. package/dist/cjs/internals/simulation/index.js +24 -0
  22. package/dist/cjs/internals/simulation/index.js.map +1 -0
  23. package/dist/cjs/internals/simulation/interface.js +98 -0
  24. package/dist/cjs/internals/simulation/interface.js.map +1 -0
  25. package/dist/cjs/internals/simulation/methods/constants.js +84 -0
  26. package/dist/cjs/internals/simulation/methods/constants.js.map +1 -0
  27. package/dist/cjs/internals/simulation/methods/hooks/encryption.js +33 -0
  28. package/dist/cjs/internals/simulation/methods/hooks/encryption.js.map +1 -0
  29. package/dist/cjs/internals/simulation/methods/hooks/get-locale.js +17 -0
  30. package/dist/cjs/internals/simulation/methods/hooks/get-locale.js.map +1 -0
  31. package/dist/cjs/internals/simulation/methods/hooks/index.js +25 -0
  32. package/dist/cjs/internals/simulation/methods/hooks/index.js.map +1 -0
  33. package/dist/cjs/internals/simulation/methods/hooks/interface.js +26 -0
  34. package/dist/cjs/internals/simulation/methods/hooks/interface.js.map +1 -0
  35. package/dist/cjs/internals/simulation/methods/hooks/notifications.js +66 -0
  36. package/dist/cjs/internals/simulation/methods/hooks/notifications.js.map +1 -0
  37. package/dist/cjs/internals/simulation/methods/hooks/show-dialog.js +43 -0
  38. package/dist/cjs/internals/simulation/methods/hooks/show-dialog.js.map +1 -0
  39. package/dist/cjs/internals/simulation/methods/hooks/state.js +80 -0
  40. package/dist/cjs/internals/simulation/methods/hooks/state.js.map +1 -0
  41. package/dist/cjs/internals/simulation/methods/index.js +20 -0
  42. package/dist/cjs/internals/simulation/methods/index.js.map +1 -0
  43. package/dist/cjs/internals/simulation/methods/specifications.js +81 -0
  44. package/dist/cjs/internals/simulation/methods/specifications.js.map +1 -0
  45. package/dist/cjs/internals/simulation/middleware/engine.js +31 -0
  46. package/dist/cjs/internals/simulation/middleware/engine.js.map +1 -0
  47. package/dist/cjs/internals/simulation/middleware/index.js +20 -0
  48. package/dist/cjs/internals/simulation/middleware/index.js.map +1 -0
  49. package/dist/cjs/internals/simulation/middleware/internal-methods/accounts.js +30 -0
  50. package/dist/cjs/internals/simulation/middleware/internal-methods/accounts.js.map +1 -0
  51. package/dist/cjs/internals/simulation/middleware/internal-methods/index.js +20 -0
  52. package/dist/cjs/internals/simulation/middleware/internal-methods/index.js.map +1 -0
  53. package/dist/cjs/internals/simulation/middleware/internal-methods/middleware.js +37 -0
  54. package/dist/cjs/internals/simulation/middleware/internal-methods/middleware.js.map +1 -0
  55. package/dist/cjs/internals/simulation/middleware/internal-methods/provider-state.js +23 -0
  56. package/dist/cjs/internals/simulation/middleware/internal-methods/provider-state.js.map +1 -0
  57. package/dist/cjs/internals/simulation/middleware/mock.js +23 -0
  58. package/dist/cjs/internals/simulation/middleware/mock.js.map +1 -0
  59. package/dist/cjs/internals/simulation/options.js +24 -0
  60. package/dist/cjs/internals/simulation/options.js.map +1 -0
  61. package/dist/cjs/internals/simulation/simulation.js +124 -0
  62. package/dist/cjs/internals/simulation/simulation.js.map +1 -0
  63. package/dist/cjs/internals/simulation/store/index.js +23 -0
  64. package/dist/cjs/internals/simulation/store/index.js.map +1 -0
  65. package/dist/cjs/internals/simulation/store/mocks.js +52 -0
  66. package/dist/cjs/internals/simulation/store/mocks.js.map +1 -0
  67. package/dist/cjs/internals/simulation/store/notifications.js +52 -0
  68. package/dist/cjs/internals/simulation/store/notifications.js.map +1 -0
  69. package/dist/cjs/internals/simulation/store/state.js +64 -0
  70. package/dist/cjs/internals/simulation/store/state.js.map +1 -0
  71. package/dist/cjs/internals/simulation/store/store.js +57 -0
  72. package/dist/cjs/internals/simulation/store/store.js.map +1 -0
  73. package/dist/cjs/internals/simulation/store/ui.js +48 -0
  74. package/dist/cjs/internals/simulation/store/ui.js.map +1 -0
  75. package/dist/cjs/internals/structs.js +47 -5
  76. package/dist/cjs/internals/structs.js.map +1 -1
  77. package/dist/cjs/matchers.js +5 -2
  78. package/dist/cjs/matchers.js.map +1 -1
  79. package/dist/cjs/options.js +0 -6
  80. package/dist/cjs/options.js.map +1 -1
  81. package/dist/esm/environment.js +22 -83
  82. package/dist/esm/environment.js.map +1 -1
  83. package/dist/esm/helpers.js +127 -46
  84. package/dist/esm/helpers.js.map +1 -1
  85. package/dist/esm/internals/environment.js +1 -1
  86. package/dist/esm/internals/environment.js.map +1 -1
  87. package/dist/esm/internals/index.js +1 -5
  88. package/dist/esm/internals/index.js.map +1 -1
  89. package/dist/esm/internals/request.js +61 -104
  90. package/dist/esm/internals/request.js.map +1 -1
  91. package/dist/esm/internals/server.js +1 -5
  92. package/dist/esm/internals/server.js.map +1 -1
  93. package/dist/esm/internals/simulation/constants.js +12 -0
  94. package/dist/esm/internals/simulation/constants.js.map +1 -0
  95. package/dist/esm/internals/simulation/controllers.js +90 -0
  96. package/dist/esm/internals/simulation/controllers.js.map +1 -0
  97. package/dist/esm/internals/simulation/files.js +19 -0
  98. package/dist/esm/internals/simulation/files.js.map +1 -0
  99. package/dist/esm/internals/simulation/index.js +7 -0
  100. package/dist/esm/internals/simulation/index.js.map +1 -0
  101. package/dist/esm/internals/simulation/interface.js +95 -0
  102. package/dist/esm/internals/simulation/interface.js.map +1 -0
  103. package/dist/esm/internals/simulation/methods/constants.js +69 -0
  104. package/dist/esm/internals/simulation/methods/constants.js.map +1 -0
  105. package/dist/esm/internals/simulation/methods/hooks/encryption.js +39 -0
  106. package/dist/esm/internals/simulation/methods/hooks/encryption.js.map +1 -0
  107. package/dist/esm/internals/simulation/methods/hooks/get-locale.js +13 -0
  108. package/dist/esm/internals/simulation/methods/hooks/get-locale.js.map +1 -0
  109. package/dist/esm/internals/simulation/methods/hooks/index.js +8 -0
  110. package/dist/esm/internals/simulation/methods/hooks/index.js.map +1 -0
  111. package/dist/esm/internals/simulation/methods/hooks/interface.js +18 -0
  112. package/dist/esm/internals/simulation/methods/hooks/interface.js.map +1 -0
  113. package/dist/esm/internals/simulation/methods/hooks/notifications.js +58 -0
  114. package/dist/esm/internals/simulation/methods/hooks/notifications.js.map +1 -0
  115. package/dist/esm/internals/simulation/methods/hooks/show-dialog.js +38 -0
  116. package/dist/esm/internals/simulation/methods/hooks/show-dialog.js.map +1 -0
  117. package/dist/esm/internals/simulation/methods/hooks/state.js +74 -0
  118. package/dist/esm/internals/simulation/methods/hooks/state.js.map +1 -0
  119. package/dist/esm/internals/simulation/methods/index.js +3 -0
  120. package/dist/esm/internals/simulation/methods/index.js.map +1 -0
  121. package/dist/esm/internals/simulation/methods/specifications.js +84 -0
  122. package/dist/esm/internals/simulation/methods/specifications.js.map +1 -0
  123. package/dist/esm/internals/simulation/middleware/engine.js +33 -0
  124. package/dist/esm/internals/simulation/middleware/engine.js.map +1 -0
  125. package/dist/esm/internals/simulation/middleware/index.js +3 -0
  126. package/dist/esm/internals/simulation/middleware/index.js.map +1 -0
  127. package/dist/esm/internals/simulation/middleware/internal-methods/accounts.js +31 -0
  128. package/dist/esm/internals/simulation/middleware/internal-methods/accounts.js.map +1 -0
  129. package/dist/esm/internals/simulation/middleware/internal-methods/index.js +3 -0
  130. package/dist/esm/internals/simulation/middleware/internal-methods/index.js.map +1 -0
  131. package/dist/esm/internals/simulation/middleware/internal-methods/middleware.js +37 -0
  132. package/dist/esm/internals/simulation/middleware/internal-methods/middleware.js.map +1 -0
  133. package/dist/esm/internals/simulation/middleware/internal-methods/provider-state.js +23 -0
  134. package/dist/esm/internals/simulation/middleware/internal-methods/provider-state.js.map +1 -0
  135. package/dist/esm/internals/simulation/middleware/mock.js +18 -0
  136. package/dist/esm/internals/simulation/middleware/mock.js.map +1 -0
  137. package/dist/esm/internals/simulation/options.js +20 -0
  138. package/dist/esm/internals/simulation/options.js.map +1 -0
  139. package/dist/esm/internals/simulation/simulation.js +128 -0
  140. package/dist/esm/internals/simulation/simulation.js.map +1 -0
  141. package/dist/esm/internals/simulation/store/index.js +6 -0
  142. package/dist/esm/internals/simulation/store/index.js.map +1 -0
  143. package/dist/esm/internals/simulation/store/mocks.js +32 -0
  144. package/dist/esm/internals/simulation/store/mocks.js.map +1 -0
  145. package/dist/esm/internals/simulation/store/notifications.js +30 -0
  146. package/dist/esm/internals/simulation/store/notifications.js.map +1 -0
  147. package/dist/esm/internals/simulation/store/state.js +47 -0
  148. package/dist/esm/internals/simulation/store/state.js.map +1 -0
  149. package/dist/esm/internals/simulation/store/store.js +50 -0
  150. package/dist/esm/internals/simulation/store/store.js.map +1 -0
  151. package/dist/esm/internals/simulation/store/ui.js +21 -0
  152. package/dist/esm/internals/simulation/store/ui.js.map +1 -0
  153. package/dist/esm/internals/structs.js +41 -5
  154. package/dist/esm/internals/structs.js.map +1 -1
  155. package/dist/esm/matchers.js +5 -2
  156. package/dist/esm/matchers.js.map +1 -1
  157. package/dist/esm/options.js +1 -7
  158. package/dist/esm/options.js.map +1 -1
  159. package/dist/esm/types.js.map +1 -1
  160. package/dist/types/environment.d.ts +14 -15
  161. package/dist/types/helpers.d.ts +64 -3
  162. package/dist/types/internals/index.d.ts +1 -4
  163. package/dist/types/internals/request.d.ts +38 -56
  164. package/dist/types/internals/simulation/constants.d.ts +13 -0
  165. package/dist/types/internals/simulation/controllers.d.ts +40 -0
  166. package/dist/types/internals/simulation/files.d.ts +11 -0
  167. package/dist/types/internals/simulation/index.d.ts +5 -0
  168. package/dist/types/internals/simulation/interface.d.ts +25 -0
  169. package/dist/types/internals/simulation/methods/constants.d.ts +10 -0
  170. package/dist/types/internals/simulation/methods/hooks/encryption.d.ts +29 -0
  171. package/dist/types/internals/simulation/methods/hooks/get-locale.d.ts +9 -0
  172. package/dist/types/internals/simulation/methods/hooks/index.d.ts +6 -0
  173. package/dist/types/internals/simulation/methods/hooks/interface.d.ts +16 -0
  174. package/dist/types/internals/simulation/methods/hooks/notifications.d.ts +16 -0
  175. package/dist/types/internals/simulation/methods/hooks/show-dialog.d.ts +9 -0
  176. package/dist/types/internals/simulation/methods/hooks/state.d.ts +22 -0
  177. package/dist/types/internals/simulation/methods/index.d.ts +1 -0
  178. package/dist/types/internals/simulation/methods/specifications.d.ts +56 -0
  179. package/dist/types/internals/simulation/middleware/engine.d.ts +26 -0
  180. package/dist/types/internals/simulation/middleware/index.d.ts +1 -0
  181. package/dist/types/internals/simulation/middleware/internal-methods/accounts.d.ts +18 -0
  182. package/dist/types/internals/simulation/middleware/internal-methods/index.d.ts +1 -0
  183. package/dist/types/internals/simulation/middleware/internal-methods/middleware.d.ts +22 -0
  184. package/dist/types/internals/simulation/middleware/internal-methods/provider-state.d.ts +14 -0
  185. package/dist/types/internals/simulation/middleware/mock.d.ts +10 -0
  186. package/dist/types/internals/simulation/options.d.ts +37 -0
  187. package/dist/types/internals/simulation/simulation.d.ts +98 -0
  188. package/dist/types/internals/simulation/store/index.d.ts +4 -0
  189. package/dist/types/internals/simulation/store/mocks.d.ts +35 -0
  190. package/dist/types/internals/simulation/store/notifications.d.ts +44 -0
  191. package/dist/types/internals/simulation/store/state.d.ts +55 -0
  192. package/dist/types/internals/simulation/store/store.d.ts +22 -0
  193. package/dist/types/internals/simulation/store/ui.d.ts +25 -0
  194. package/dist/types/internals/structs.d.ts +229 -16
  195. package/dist/types/options.d.ts +3 -35
  196. package/dist/types/types.d.ts +88 -41
  197. package/package.json +21 -12
  198. package/dist/cjs/internals/interface.js +0 -103
  199. package/dist/cjs/internals/interface.js.map +0 -1
  200. package/dist/cjs/internals/network.js +0 -148
  201. package/dist/cjs/internals/network.js.map +0 -1
  202. package/dist/cjs/internals/types.js +0 -6
  203. package/dist/cjs/internals/types.js.map +0 -1
  204. package/dist/cjs/internals/wait-for.js +0 -63
  205. package/dist/cjs/internals/wait-for.js.map +0 -1
  206. package/dist/esm/internals/interface.js +0 -100
  207. package/dist/esm/internals/interface.js.map +0 -1
  208. package/dist/esm/internals/network.js +0 -143
  209. package/dist/esm/internals/network.js.map +0 -1
  210. package/dist/esm/internals/types.js +0 -3
  211. package/dist/esm/internals/types.js.map +0 -1
  212. package/dist/esm/internals/wait-for.js +0 -63
  213. package/dist/esm/internals/wait-for.js.map +0 -1
  214. package/dist/types/internals/interface.d.ts +0 -25
  215. package/dist/types/internals/network.d.ts +0 -87
  216. package/dist/types/internals/types.d.ts +0 -18
  217. package/dist/types/internals/wait-for.d.ts +0 -38
@@ -0,0 +1,22 @@
1
+ import type { RunSagaFunction } from '../../store';
2
+ /**
3
+ * Get the implementation of the `getSnapState` hook.
4
+ *
5
+ * @param runSaga - The function to run a saga outside the usual Redux flow.
6
+ * @returns The implementation of the `getSnapState` hook.
7
+ */
8
+ export declare function getGetSnapStateMethodImplementation(runSaga: RunSagaFunction): (_snapId: string, encrypted?: boolean | undefined) => any;
9
+ /**
10
+ * Get the implementation of the `updateSnapState` hook.
11
+ *
12
+ * @param runSaga - The function to run a saga outside the usual Redux flow.
13
+ * @returns The implementation of the `updateSnapState` hook.
14
+ */
15
+ export declare function getUpdateSnapStateMethodImplementation(runSaga: RunSagaFunction): (_snapId: string, newState: string, encrypted?: boolean | undefined) => void;
16
+ /**
17
+ * Get the implementation of the `clearSnapState` hook.
18
+ *
19
+ * @param runSaga - The function to run a saga outside the usual Redux flow.
20
+ * @returns The implementation of the `clearSnapState` hook.
21
+ */
22
+ export declare function getClearSnapStateMethodImplementation(runSaga: RunSagaFunction): (_snapId: string, encrypted?: boolean | undefined) => Promise<void>;
@@ -0,0 +1 @@
1
+ export * from './specifications';
@@ -0,0 +1,56 @@
1
+ import type { GenericPermissionController } from '@metamask/permission-controller';
2
+ import type { SnapId } from '@metamask/snaps-sdk';
3
+ import type { RootControllerMessenger } from '../controllers';
4
+ import type { SimulationOptions } from '../options';
5
+ import type { RunSagaFunction } from '../store';
6
+ export declare type PermissionSpecificationsHooks = {
7
+ /**
8
+ * A hook that returns the user's secret recovery phrase.
9
+ *
10
+ * @returns The user's secret recovery phrase.
11
+ */
12
+ getMnemonic: () => Promise<Uint8Array>;
13
+ };
14
+ export declare type GetPermissionSpecificationsOptions = {
15
+ controllerMessenger: RootControllerMessenger;
16
+ hooks: PermissionSpecificationsHooks;
17
+ runSaga: RunSagaFunction;
18
+ options: SimulationOptions;
19
+ };
20
+ /**
21
+ * Get a function which resolves with the specified result.
22
+ *
23
+ * @param result - The result to return.
24
+ * @returns The function implementation.
25
+ */
26
+ export declare function resolve(result: unknown): () => unknown;
27
+ /**
28
+ * Get a function which resolves with the specified result.
29
+ *
30
+ * @param result - The result to return. If not specified, the function will
31
+ * resolve with `undefined`.
32
+ * @returns The function implementation.
33
+ */
34
+ export declare function asyncResolve(result?: unknown): () => Promise<unknown>;
35
+ /**
36
+ * Get the permission specifications for the Snap.
37
+ *
38
+ * @param options - The options.
39
+ * @param options.controllerMessenger - The controller messenger.
40
+ * @param options.hooks - The hooks.
41
+ * @param options.runSaga - The function to run a saga outside the usual Redux
42
+ * flow.
43
+ * @param options.options - The simulation options.
44
+ * @returns The permission specifications for the Snap.
45
+ */
46
+ export declare function getPermissionSpecifications({ controllerMessenger, hooks, runSaga, options, }: GetPermissionSpecificationsOptions): {
47
+ [x: string]: import("@metamask/permission-controller").PermissionSpecificationConstraint;
48
+ };
49
+ /**
50
+ * Get the endowments for the Snap.
51
+ *
52
+ * @param permissionController - The permission controller.
53
+ * @param snapId - The ID of the Snap.
54
+ * @returns The endowments for the Snap.
55
+ */
56
+ export declare function getEndowments(permissionController: GenericPermissionController, snapId: SnapId): Promise<string[]>;
@@ -0,0 +1,26 @@
1
+ import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
2
+ import { JsonRpcEngine } from '@metamask/json-rpc-engine';
3
+ import type { RestrictedMethodParameters } from '@metamask/permission-controller';
4
+ import type { Json } from '@metamask/utils';
5
+ import type { MiddlewareHooks } from '../simulation';
6
+ import type { Store } from '../store';
7
+ export declare type CreateJsonRpcEngineOptions = {
8
+ store: Store;
9
+ hooks: MiddlewareHooks;
10
+ permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;
11
+ endpoint?: string;
12
+ };
13
+ /**
14
+ * Create a JSON-RPC engine for use in a simulated environment. This engine
15
+ * should be used to handle all JSON-RPC requests. It is set up to handle
16
+ * requests that would normally be handled internally by the MetaMask client, as
17
+ * well as Snap-specific requests.
18
+ *
19
+ * @param options - The options to use when creating the engine.
20
+ * @param options.store - The Redux store to use.
21
+ * @param options.hooks - Any hooks used by the middleware handlers.
22
+ * @param options.permissionMiddleware - The permission middleware to use.
23
+ * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.
24
+ * @returns A JSON-RPC engine.
25
+ */
26
+ export declare function createJsonRpcEngine({ store, hooks, permissionMiddleware, endpoint, }: CreateJsonRpcEngineOptions): JsonRpcEngine;
@@ -0,0 +1 @@
1
+ export * from './engine';
@@ -0,0 +1,18 @@
1
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from '@metamask/json-rpc-engine';
2
+ import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
3
+ export declare type GetAccountsHandlerHooks = {
4
+ getMnemonic: () => Promise<Uint8Array>;
5
+ };
6
+ /**
7
+ * A mock handler for account related methods that always returns the first
8
+ * address for the selected secret recovery phrase.
9
+ *
10
+ * @param _request - Incoming JSON-RPC request. This is ignored for this
11
+ * specific handler.
12
+ * @param response - The outgoing JSON-RPC response, modified to return the
13
+ * result.
14
+ * @param _next - The `json-rpc-engine` middleware next handler.
15
+ * @param end - The `json-rpc-engine` middleware end handler.
16
+ * @param hooks - Any hooks required by this handler.
17
+ */
18
+ export declare function getAccountsHandler(_request: JsonRpcRequest, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: GetAccountsHandlerHooks): Promise<void>;
@@ -0,0 +1 @@
1
+ export * from './middleware';
@@ -0,0 +1,22 @@
1
+ import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
2
+ import type { Json, JsonRpcParams } from '@metamask/utils';
3
+ export declare type InternalMethodsMiddlewareHooks = {
4
+ /**
5
+ * A hook that returns the user's secret recovery phrase.
6
+ *
7
+ * @returns The user's secret recovery phrase.
8
+ */
9
+ getMnemonic: () => Promise<Uint8Array>;
10
+ };
11
+ /**
12
+ * Create a middleware for handling JSON-RPC methods normally handled internally
13
+ * by the MetaMask client.
14
+ *
15
+ * NOTE: This middleware provides all `hooks` to all handlers and should
16
+ * therefore NOT be used outside of the simulation environment. It is intended
17
+ * for testing purposes only.
18
+ *
19
+ * @param hooks - Any hooks used by the middleware handlers.
20
+ * @returns A middleware function.
21
+ */
22
+ export declare function createInternalMethodsMiddleware(hooks: InternalMethodsMiddlewareHooks): JsonRpcMiddleware<JsonRpcParams, Json>;
@@ -0,0 +1,14 @@
1
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from '@metamask/json-rpc-engine';
2
+ import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
3
+ /**
4
+ * A mock handler for metamask_getProviderState that always returns a specific
5
+ * hardcoded result.
6
+ *
7
+ * @param _request - Incoming JSON-RPC request. Ignored for this specific
8
+ * handler.
9
+ * @param response - The outgoing JSON-RPC response, modified to return the
10
+ * result.
11
+ * @param _next - The `json-rpc-engine` middleware next handler.
12
+ * @param end - The `json-rpc-engine` middleware end handler.
13
+ */
14
+ export declare function getProviderStateHandler(_request: JsonRpcRequest, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback): Promise<void>;
@@ -0,0 +1,10 @@
1
+ import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
2
+ import type { Json, JsonRpcParams } from '@metamask/utils';
3
+ import type { Store } from '../store';
4
+ /**
5
+ * Create a middleware for handling JSON-RPC methods that have been mocked.
6
+ *
7
+ * @param store - The Redux store to use.
8
+ * @returns A middleware function.
9
+ */
10
+ export declare function createMockMiddleware(store: Store): JsonRpcMiddleware<JsonRpcParams, Json>;
@@ -0,0 +1,37 @@
1
+ import type { Infer } from 'superstruct';
2
+ declare const SimulationOptionsStruct: import("superstruct").Struct<{
3
+ state?: Record<string, import("@metamask/utils").Json> | null | undefined;
4
+ locale?: string | undefined;
5
+ secretRecoveryPhrase?: string | undefined;
6
+ unencryptedState?: Record<string, import("@metamask/utils").Json> | null | undefined;
7
+ }, {
8
+ secretRecoveryPhrase: import("superstruct").Struct<string | undefined, null>;
9
+ locale: import("superstruct").Struct<string | undefined, null>;
10
+ state: import("superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
11
+ unencryptedState: import("superstruct").Struct<Record<string, import("@metamask/utils").Json> | null | undefined, null>;
12
+ }>;
13
+ /**
14
+ * Options for the simulation.
15
+ *
16
+ * @property secretRecoveryPhrase - The secret recovery phrase to use. This is
17
+ * used to derive addresses and private keys. Defaults to a test recovery
18
+ * phrase.
19
+ * @property locale - The locale to use. Defaults to `en`.
20
+ * @property state - The initial state of the Snap, if any. Defaults to `null`.
21
+ */
22
+ export declare type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;
23
+ /**
24
+ * Options for the simulation, with defaults filled in.
25
+ *
26
+ * See {@link SimulationUserOptions} for documentation.
27
+ */
28
+ export declare type SimulationOptions = Required<SimulationUserOptions>;
29
+ /**
30
+ * Get the options for the simulation.
31
+ *
32
+ * @param options - The user options. Any options not specified will be filled
33
+ * in with default values.
34
+ * @returns The simulation options.
35
+ */
36
+ export declare function getOptions(options: SimulationUserOptions): SimulationOptions;
37
+ export {};
@@ -0,0 +1,98 @@
1
+ import type { ActionConstraint, EventConstraint } from '@metamask/base-controller';
2
+ import { ControllerMessenger } from '@metamask/base-controller';
3
+ import type { AbstractExecutionService } from '@metamask/snaps-controllers';
4
+ import type { SnapId, AuxiliaryFileEncoding, Component, InterfaceState } from '@metamask/snaps-sdk';
5
+ import type { FetchedSnapFiles } from '@metamask/snaps-utils';
6
+ import type { RootControllerMessenger } from './controllers';
7
+ import type { SimulationOptions, SimulationUserOptions } from './options';
8
+ import type { RunSagaFunction, Store } from './store';
9
+ /**
10
+ * Options for the execution service, without the options that are shared
11
+ * between all execution services.
12
+ *
13
+ * @template Service - The type of the execution service, i.e., the class that
14
+ * creates the execution service.
15
+ */
16
+ export declare type ExecutionServiceOptions<Service extends new (...args: any[]) => any> = Omit<ConstructorParameters<Service>[0], keyof ConstructorParameters<typeof AbstractExecutionService<unknown>>[0]>;
17
+ /**
18
+ * The options for running a Snap in a simulated environment.
19
+ *
20
+ * @property executionService - The execution service to use.
21
+ * @property executionServiceOptions - The options to use when creating the
22
+ * execution service, if any. This should only include options specific to the
23
+ * provided execution service.
24
+ * @property options - The simulation options.
25
+ * @template Service - The type of the execution service.
26
+ */
27
+ export declare type InstallSnapOptions<Service extends new (...args: any[]) => InstanceType<typeof AbstractExecutionService<unknown>>> = ExecutionServiceOptions<Service> extends Record<string, never> ? {
28
+ executionService: Service;
29
+ executionServiceOptions?: ExecutionServiceOptions<Service>;
30
+ options?: SimulationUserOptions;
31
+ } : {
32
+ executionService: Service;
33
+ executionServiceOptions: ExecutionServiceOptions<Service>;
34
+ options?: SimulationUserOptions;
35
+ };
36
+ export declare type InstalledSnap = {
37
+ snapId: SnapId;
38
+ store: Store;
39
+ executionService: InstanceType<typeof AbstractExecutionService>;
40
+ controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;
41
+ runSaga: RunSagaFunction;
42
+ };
43
+ export declare type MiddlewareHooks = {
44
+ /**
45
+ * A hook that returns the user's secret recovery phrase.
46
+ *
47
+ * @returns The user's secret recovery phrase.
48
+ */
49
+ getMnemonic: () => Promise<Uint8Array>;
50
+ /**
51
+ * A hook that returns the Snap's auxiliary file for the given path.
52
+ *
53
+ * @param path - The path of the auxiliary file to get.
54
+ * @param encoding - The encoding to use when returning the file.
55
+ * @returns The Snap's auxiliary file for the given path.
56
+ */
57
+ getSnapFile: (path: string, encoding: AuxiliaryFileEncoding) => Promise<string | null>;
58
+ /**
59
+ * A hook that returns whether the client is locked or not.
60
+ *
61
+ * @returns A boolean flag signaling whether the client is locked.
62
+ */
63
+ getIsLocked: () => boolean;
64
+ createInterface: (content: Component) => Promise<string>;
65
+ updateInterface: (id: string, content: Component) => Promise<void>;
66
+ getInterfaceState: (id: string) => InterfaceState;
67
+ };
68
+ /**
69
+ * Install a Snap in a simulated environment. This will fetch the Snap files,
70
+ * create a Redux store, set up the controllers and JSON-RPC stack, register the
71
+ * Snap, and run the Snap code in the execution service.
72
+ *
73
+ * @param snapId - The ID of the Snap to install.
74
+ * @param options - The options to use when installing the Snap.
75
+ * @param options.executionService - The execution service to use.
76
+ * @param options.executionServiceOptions - The options to use when creating the
77
+ * execution service, if any. This should only include options specific to the
78
+ * provided execution service.
79
+ * @param options.options - The simulation options.
80
+ * @template Service - The type of the execution service.
81
+ */
82
+ export declare function handleInstallSnap<Service extends new (...args: any[]) => InstanceType<typeof AbstractExecutionService>>(snapId: SnapId, { executionService, executionServiceOptions, options: rawOptions, }?: Partial<InstallSnapOptions<Service>>): Promise<InstalledSnap>;
83
+ /**
84
+ * Get the hooks for the simulation.
85
+ *
86
+ * @param options - The simulation options.
87
+ * @param snapFiles - The Snap files.
88
+ * @param snapId - The Snap ID.
89
+ * @param controllerMessenger - The controller messenger.
90
+ * @returns The hooks for the simulation.
91
+ */
92
+ export declare function getHooks(options: SimulationOptions, snapFiles: FetchedSnapFiles, snapId: SnapId, controllerMessenger: RootControllerMessenger): MiddlewareHooks;
93
+ /**
94
+ * Register mocked action handlers.
95
+ *
96
+ * @param controllerMessenger - The controller messenger.
97
+ */
98
+ export declare function registerActions(controllerMessenger: RootControllerMessenger): void;
@@ -0,0 +1,4 @@
1
+ export * from './notifications';
2
+ export * from './state';
3
+ export * from './store';
4
+ export * from './ui';
@@ -0,0 +1,35 @@
1
+ import type { Json } from '@metamask/utils';
2
+ import type { PayloadAction } from '@reduxjs/toolkit';
3
+ import type { ApplicationState } from './store';
4
+ export declare type JsonRpcMock = {
5
+ method: string;
6
+ result: Json;
7
+ };
8
+ export declare type MocksState = {
9
+ jsonRpc: Record<string, Json>;
10
+ };
11
+ export declare const mocksSlice: import("@reduxjs/toolkit").Slice<MocksState, {
12
+ addJsonRpcMock: (state: import("immer/dist/internal").WritableDraft<MocksState>, action: PayloadAction<JsonRpcMock>) => void;
13
+ removeJsonRpcMock: (state: import("immer/dist/internal").WritableDraft<MocksState>, action: PayloadAction<string>) => void;
14
+ }, "mocks">;
15
+ export declare const addJsonRpcMock: import("@reduxjs/toolkit").ActionCreatorWithPayload<JsonRpcMock, "mocks/addJsonRpcMock">, removeJsonRpcMock: import("@reduxjs/toolkit").ActionCreatorWithPayload<string, "mocks/removeJsonRpcMock">;
16
+ /**
17
+ * Get the JSON-RPC mocks from the state.
18
+ *
19
+ * @param state - The application state.
20
+ * @returns The JSON-RPC mocks.
21
+ */
22
+ export declare const getJsonRpcMocks: (state: ApplicationState) => Record<string, Json>;
23
+ /**
24
+ * Get the JSON-RPC mock for a given method from the state.
25
+ */
26
+ export declare const getJsonRpcMock: ((state: {
27
+ mocks: MocksState;
28
+ notifications: import("./notifications").NotificationsState;
29
+ state: import("./state").State;
30
+ ui: import("./ui").UiState;
31
+ }, method: string) => Json) & import("reselect").OutputSelectorFields<(args_0: Record<string, Json>, args_1: string) => Json, {
32
+ clearCache: () => void;
33
+ }> & {
34
+ clearCache: () => void;
35
+ };
@@ -0,0 +1,44 @@
1
+ import type { NotificationType } from '@metamask/snaps-sdk';
2
+ import type { PayloadAction } from '@reduxjs/toolkit';
3
+ /**
4
+ * A notification object.
5
+ *
6
+ * @property id - A unique ID for the notification.
7
+ * @property message - The notification message.
8
+ * @property type - The notification type.
9
+ */
10
+ export declare type Notification = {
11
+ id: string;
12
+ message: string;
13
+ type: NotificationType;
14
+ };
15
+ /**
16
+ * The notifications state.
17
+ *
18
+ * @property notifications - An array of notifications.
19
+ */
20
+ export declare type NotificationsState = {
21
+ notifications: Notification[];
22
+ };
23
+ export declare const notificationsSlice: import("@reduxjs/toolkit").Slice<NotificationsState, {
24
+ addNotification: (state: import("immer/dist/internal").WritableDraft<NotificationsState>, action: PayloadAction<Notification>) => void;
25
+ removeNotification: (state: import("immer/dist/internal").WritableDraft<NotificationsState>, action: PayloadAction<string>) => void;
26
+ clearNotifications: (state: import("immer/dist/internal").WritableDraft<NotificationsState>) => void;
27
+ }, "notifications">;
28
+ export declare const addNotification: import("@reduxjs/toolkit").ActionCreatorWithPayload<Notification, "notifications/addNotification">, removeNotification: import("@reduxjs/toolkit").ActionCreatorWithPayload<string, "notifications/removeNotification">, clearNotifications: import("@reduxjs/toolkit").ActionCreatorWithoutPayload<"notifications/clearNotifications">;
29
+ /**
30
+ * Get the notifications from the state.
31
+ *
32
+ * @param state - The application state.
33
+ * @returns An array of notifications.
34
+ */
35
+ export declare const getNotifications: ((state: {
36
+ mocks: import("./mocks").MocksState;
37
+ notifications: NotificationsState;
38
+ state: import("./state").State;
39
+ ui: import("./ui").UiState;
40
+ }) => Notification[]) & import("reselect").OutputSelectorFields<(args_0: NotificationsState) => Notification[], {
41
+ clearCache: () => void;
42
+ }> & {
43
+ clearCache: () => void;
44
+ };
@@ -0,0 +1,55 @@
1
+ import type { PayloadAction } from '@reduxjs/toolkit';
2
+ /**
3
+ * The Snap state object.
4
+ *
5
+ * @property encrypted - The encrypted state. Can be null if the Snap does not
6
+ * have an encrypted state.
7
+ * @property unencrypted - The unencrypted state. Can be null if the Snap does
8
+ * not have an unencrypted state.
9
+ */
10
+ export declare type State = {
11
+ encrypted: string | null;
12
+ unencrypted: string | null;
13
+ };
14
+ /**
15
+ * The state slice, which stores the state of the Snap.
16
+ */
17
+ export declare const stateSlice: import("@reduxjs/toolkit").Slice<State, {
18
+ setState: (state: import("immer/dist/internal").WritableDraft<State>, action: {
19
+ payload: {
20
+ state: string | null;
21
+ encrypted: boolean;
22
+ };
23
+ type: string;
24
+ }) => import("immer/dist/internal").WritableDraft<State>;
25
+ clearState: (state: import("immer/dist/internal").WritableDraft<State>, action: PayloadAction<{
26
+ encrypted: boolean;
27
+ }>) => import("immer/dist/internal").WritableDraft<State>;
28
+ }, "state">;
29
+ export declare const setState: import("@reduxjs/toolkit").ActionCreatorWithPayload<{
30
+ state: string | null;
31
+ encrypted: boolean;
32
+ }, "state/setState">, clearState: import("@reduxjs/toolkit").ActionCreatorWithPayload<{
33
+ encrypted: boolean;
34
+ }, "state/clearState">;
35
+ /**
36
+ * Get the state from the store.
37
+ *
38
+ * @param encrypted - Whether to get the encrypted or unencrypted state.
39
+ * @returns A selector that returns the state.
40
+ */
41
+ export declare function getState(encrypted: boolean): ((state: {
42
+ mocks: import("./mocks").MocksState;
43
+ notifications: import("./notifications").NotificationsState;
44
+ state: State;
45
+ ui: import("./ui").UiState;
46
+ }) => string | null) & import("reselect").OutputSelectorFields<(args_0: {
47
+ mocks: import("./mocks").MocksState;
48
+ notifications: import("./notifications").NotificationsState;
49
+ state: State;
50
+ ui: import("./ui").UiState;
51
+ }) => string | null, {
52
+ clearCache: () => void;
53
+ }> & {
54
+ clearCache: () => void;
55
+ };
@@ -0,0 +1,22 @@
1
+ import type { SimulationOptions } from '../options';
2
+ /**
3
+ * Create a Redux store.
4
+ *
5
+ * @param password - The password to use for state encryption.
6
+ * @param options - The simulation options.
7
+ * @param options.state - The initial state for the Snap.
8
+ * @param options.unencryptedState - The initial unencrypted state for the Snap.
9
+ * @returns A Redux store with the default state.
10
+ */
11
+ export declare function createStore(password: string, { state, unencryptedState }: SimulationOptions): {
12
+ store: import("@reduxjs/toolkit/dist/configureStore").ToolkitStore<{
13
+ mocks: import("./mocks").MocksState;
14
+ notifications: import("./notifications").NotificationsState;
15
+ state: import("./state").State;
16
+ ui: import("./ui").UiState;
17
+ }, import("redux").AnyAction, import("@reduxjs/toolkit").MiddlewareArray<[import("redux-saga").SagaMiddleware<object>]>>;
18
+ runSaga: <S extends import("redux-saga").Saga<any[]>>(saga: S, ...args: Parameters<S>) => import("redux-saga").Task<any>;
19
+ };
20
+ export declare type Store = ReturnType<typeof createStore>['store'];
21
+ export declare type ApplicationState = ReturnType<Store['getState']>;
22
+ export declare type RunSagaFunction = ReturnType<typeof createStore>['runSaga'];
@@ -0,0 +1,25 @@
1
+ import type { DialogType } from '@metamask/snaps-sdk';
2
+ import type { PayloadAction } from '@reduxjs/toolkit';
3
+ export declare type Interface = {
4
+ type: DialogType;
5
+ id: string;
6
+ };
7
+ export declare type UiState = {
8
+ current?: Interface | null;
9
+ };
10
+ export declare const uiSlice: import("@reduxjs/toolkit").Slice<UiState, {
11
+ setInterface(state: import("immer/dist/internal").WritableDraft<UiState>, action: PayloadAction<Interface>): void;
12
+ closeInterface(state: import("immer/dist/internal").WritableDraft<UiState>): void;
13
+ }, "ui">;
14
+ export declare const resolveInterface: import("@reduxjs/toolkit").ActionCreatorWithNonInferrablePayload<string>;
15
+ export declare const setInterface: import("@reduxjs/toolkit").ActionCreatorWithPayload<Interface, "ui/setInterface">, closeInterface: import("@reduxjs/toolkit").ActionCreatorWithoutPayload<"ui/closeInterface">;
16
+ export declare const getCurrentInterface: ((state: {
17
+ mocks: import("./mocks").MocksState;
18
+ notifications: import("./notifications").NotificationsState;
19
+ state: import("./state").State;
20
+ ui: UiState;
21
+ }) => Interface | null | undefined) & import("reselect").OutputSelectorFields<(args_0: UiState) => Interface | null | undefined, {
22
+ clearCache: () => void;
23
+ }> & {
24
+ clearCache: () => void;
25
+ };