@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,18 @@
1
+ import { getJsonRpcMock } from '../store/mocks';
2
+ /**
3
+ * Create a middleware for handling JSON-RPC methods that have been mocked.
4
+ *
5
+ * @param store - The Redux store to use.
6
+ * @returns A middleware function.
7
+ */ export function createMockMiddleware(store) {
8
+ return function mockMiddleware(request, response, next, end) {
9
+ const result = getJsonRpcMock(store.getState(), request.method);
10
+ if (result) {
11
+ response.result = result;
12
+ return end();
13
+ }
14
+ return next();
15
+ };
16
+ }
17
+
18
+ //# sourceMappingURL=mock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/internals/simulation/middleware/mock.ts"],"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"],"names":["getJsonRpcMock","createMockMiddleware","store","mockMiddleware","request","response","next","end","result","getState","method"],"mappings":"AAIA,SAASA,cAAc,QAAQ,iBAAiB;AAEhD;;;;;CAKC,GACD,OAAO,SAASC,qBACdC,KAAY;IAEZ,OAAO,SAASC,eAAeC,OAAO,EAAEC,QAAQ,EAAEC,IAAI,EAAEC,GAAG;QACzD,MAAMC,SAASR,eAAeE,MAAMO,QAAQ,IAAIL,QAAQM,MAAM;QAC9D,IAAIF,QAAQ;YACVH,SAASG,MAAM,GAAGA;YAClB,OAAOD;QACT;QAEA,OAAOD;IACT;AACF"}
@@ -0,0 +1,20 @@
1
+ import { JsonStruct } from '@metamask/utils';
2
+ import { create, defaulted, nullable, object, optional, record, string } from 'superstruct';
3
+ import { DEFAULT_LOCALE, DEFAULT_SRP } from './constants';
4
+ const SimulationOptionsStruct = object({
5
+ secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),
6
+ locale: defaulted(optional(string()), DEFAULT_LOCALE),
7
+ state: defaulted(optional(nullable(record(string(), JsonStruct))), null),
8
+ unencryptedState: defaulted(optional(nullable(record(string(), JsonStruct))), null)
9
+ });
10
+ /**
11
+ * Get the options for the simulation.
12
+ *
13
+ * @param options - The user options. Any options not specified will be filled
14
+ * in with default values.
15
+ * @returns The simulation options.
16
+ */ export function getOptions(options) {
17
+ return create(options, SimulationOptionsStruct);
18
+ }
19
+
20
+ //# sourceMappingURL=options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/internals/simulation/options.ts"],"sourcesContent":["import { JsonStruct } from '@metamask/utils';\nimport type { Infer } from 'superstruct';\nimport {\n create,\n defaulted,\n nullable,\n object,\n optional,\n record,\n string,\n} from 'superstruct';\n\nimport { DEFAULT_LOCALE, DEFAULT_SRP } from './constants';\n\nconst SimulationOptionsStruct = object({\n secretRecoveryPhrase: defaulted(optional(string()), DEFAULT_SRP),\n locale: defaulted(optional(string()), DEFAULT_LOCALE),\n state: defaulted(optional(nullable(record(string(), JsonStruct))), null),\n unencryptedState: defaulted(\n optional(nullable(record(string(), JsonStruct))),\n null,\n ),\n});\n\n/**\n * Options for the simulation.\n *\n * @property secretRecoveryPhrase - The secret recovery phrase to use. This is\n * used to derive addresses and private keys. Defaults to a test recovery\n * phrase.\n * @property locale - The locale to use. Defaults to `en`.\n * @property state - The initial state of the Snap, if any. Defaults to `null`.\n */\nexport type SimulationUserOptions = Infer<typeof SimulationOptionsStruct>;\n\n/**\n * Options for the simulation, with defaults filled in.\n *\n * See {@link SimulationUserOptions} for documentation.\n */\nexport type SimulationOptions = Required<SimulationUserOptions>;\n\n/**\n * Get the options for the simulation.\n *\n * @param options - The user options. Any options not specified will be filled\n * in with default values.\n * @returns The simulation options.\n */\nexport function getOptions(options: SimulationUserOptions): SimulationOptions {\n return create(\n options,\n SimulationOptionsStruct,\n ) as Required<SimulationUserOptions>;\n}\n"],"names":["JsonStruct","create","defaulted","nullable","object","optional","record","string","DEFAULT_LOCALE","DEFAULT_SRP","SimulationOptionsStruct","secretRecoveryPhrase","locale","state","unencryptedState","getOptions","options"],"mappings":"AAAA,SAASA,UAAU,QAAQ,kBAAkB;AAE7C,SACEC,MAAM,EACNC,SAAS,EACTC,QAAQ,EACRC,MAAM,EACNC,QAAQ,EACRC,MAAM,EACNC,MAAM,QACD,cAAc;AAErB,SAASC,cAAc,EAAEC,WAAW,QAAQ,cAAc;AAE1D,MAAMC,0BAA0BN,OAAO;IACrCO,sBAAsBT,UAAUG,SAASE,WAAWE;IACpDG,QAAQV,UAAUG,SAASE,WAAWC;IACtCK,OAAOX,UAAUG,SAASF,SAASG,OAAOC,UAAUP,eAAe;IACnEc,kBAAkBZ,UAChBG,SAASF,SAASG,OAAOC,UAAUP,eACnC;AAEJ;AAoBA;;;;;;CAMC,GACD,OAAO,SAASe,WAAWC,OAA8B;IACvD,OAAOf,OACLe,SACAN;AAEJ"}
@@ -0,0 +1,128 @@
1
+ import { ControllerMessenger } from '@metamask/base-controller';
2
+ import { createEngineStream } from '@metamask/json-rpc-middleware-stream';
3
+ import { mnemonicPhraseToBytes } from '@metamask/key-tree';
4
+ import { fetchSnap, detectSnapLocation, NodeThreadExecutionService, setupMultiplex } from '@metamask/snaps-controllers';
5
+ import { getEncryptionKey } from '@metamask/snaps-rpc-methods';
6
+ import { logError } from '@metamask/snaps-utils';
7
+ import { pipeline } from 'readable-stream';
8
+ import { getControllers, registerSnap } from './controllers';
9
+ import { getSnapFile } from './files';
10
+ import { getEndowments } from './methods';
11
+ import { createJsonRpcEngine } from './middleware';
12
+ import { getOptions } from './options';
13
+ import { createStore } from './store';
14
+ /**
15
+ * Install a Snap in a simulated environment. This will fetch the Snap files,
16
+ * create a Redux store, set up the controllers and JSON-RPC stack, register the
17
+ * Snap, and run the Snap code in the execution service.
18
+ *
19
+ * @param snapId - The ID of the Snap to install.
20
+ * @param options - The options to use when installing the Snap.
21
+ * @param options.executionService - The execution service to use.
22
+ * @param options.executionServiceOptions - The options to use when creating the
23
+ * execution service, if any. This should only include options specific to the
24
+ * provided execution service.
25
+ * @param options.options - The simulation options.
26
+ * @template Service - The type of the execution service.
27
+ */ export async function handleInstallSnap(snapId, { executionService, executionServiceOptions, options: rawOptions = {} } = {}) {
28
+ const options = getOptions(rawOptions);
29
+ // Fetch Snap files.
30
+ const location = detectSnapLocation(snapId, {
31
+ allowLocal: true
32
+ });
33
+ const snapFiles = await fetchSnap(snapId, location);
34
+ // Create Redux store.
35
+ const password = await getEncryptionKey({
36
+ mnemonicPhrase: mnemonicPhraseToBytes(options.secretRecoveryPhrase),
37
+ snapId
38
+ });
39
+ const { store, runSaga } = createStore(password, options);
40
+ const controllerMessenger = new ControllerMessenger();
41
+ registerActions(controllerMessenger);
42
+ // Set up controllers and JSON-RPC stack.
43
+ const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);
44
+ const { subjectMetadataController, permissionController } = getControllers({
45
+ controllerMessenger,
46
+ hooks,
47
+ runSaga,
48
+ options
49
+ });
50
+ const engine = createJsonRpcEngine({
51
+ store,
52
+ hooks,
53
+ permissionMiddleware: permissionController.createPermissionMiddleware({
54
+ origin: snapId
55
+ })
56
+ });
57
+ // Create execution service.
58
+ const ExecutionService = executionService ?? NodeThreadExecutionService;
59
+ const service = new ExecutionService({
60
+ ...executionServiceOptions,
61
+ messenger: controllerMessenger.getRestricted({
62
+ name: 'ExecutionService'
63
+ }),
64
+ setupSnapProvider: (_snapId, rpcStream)=>{
65
+ const mux = setupMultiplex(rpcStream, 'snapStream');
66
+ const stream = mux.createStream('metamask-provider');
67
+ const providerStream = createEngineStream({
68
+ engine
69
+ });
70
+ // Error function is difficult to test, so we ignore it.
71
+ /* istanbul ignore next 2 */ pipeline(stream, providerStream, stream, (error)=>{
72
+ if (error) {
73
+ logError(`Provider stream failure.`, error);
74
+ }
75
+ });
76
+ }
77
+ });
78
+ // Register the Snap. This sets up the Snap's permissions and subject
79
+ // metadata.
80
+ await registerSnap(snapId, snapFiles.manifest.result, {
81
+ permissionController,
82
+ subjectMetadataController
83
+ });
84
+ // Run the Snap code in the execution service.
85
+ await service.executeSnap({
86
+ snapId,
87
+ sourceCode: snapFiles.sourceCode.toString('utf8'),
88
+ endowments: await getEndowments(permissionController, snapId)
89
+ });
90
+ return {
91
+ snapId,
92
+ store,
93
+ executionService: service,
94
+ controllerMessenger,
95
+ runSaga
96
+ };
97
+ }
98
+ /**
99
+ * Get the hooks for the simulation.
100
+ *
101
+ * @param options - The simulation options.
102
+ * @param snapFiles - The Snap files.
103
+ * @param snapId - The Snap ID.
104
+ * @param controllerMessenger - The controller messenger.
105
+ * @returns The hooks for the simulation.
106
+ */ export function getHooks(options, snapFiles, snapId, controllerMessenger) {
107
+ return {
108
+ getMnemonic: async ()=>Promise.resolve(mnemonicPhraseToBytes(options.secretRecoveryPhrase)),
109
+ getSnapFile: async (path, encoding)=>await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),
110
+ getIsLocked: ()=>false,
111
+ createInterface: async (...args)=>controllerMessenger.call('SnapInterfaceController:createInterface', snapId, ...args),
112
+ updateInterface: async (...args)=>controllerMessenger.call('SnapInterfaceController:updateInterface', snapId, ...args),
113
+ getInterfaceState: (...args)=>controllerMessenger.call('SnapInterfaceController:getInterface', snapId, ...args).state
114
+ };
115
+ }
116
+ /**
117
+ * Register mocked action handlers.
118
+ *
119
+ * @param controllerMessenger - The controller messenger.
120
+ */ export function registerActions(controllerMessenger) {
121
+ controllerMessenger.registerActionHandler('PhishingController:maybeUpdateState', async ()=>Promise.resolve());
122
+ controllerMessenger.registerActionHandler('PhishingController:testOrigin', ()=>({
123
+ result: false,
124
+ type: 'all'
125
+ }));
126
+ }
127
+
128
+ //# sourceMappingURL=simulation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/internals/simulation/simulation.ts"],"sourcesContent":["import type {\n ActionConstraint,\n EventConstraint,\n} from '@metamask/base-controller';\nimport { ControllerMessenger } from '@metamask/base-controller';\nimport { createEngineStream } from '@metamask/json-rpc-middleware-stream';\nimport { mnemonicPhraseToBytes } from '@metamask/key-tree';\nimport type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n fetchSnap,\n detectSnapLocation,\n NodeThreadExecutionService,\n setupMultiplex,\n} from '@metamask/snaps-controllers';\nimport { getEncryptionKey } from '@metamask/snaps-rpc-methods';\nimport type {\n SnapId,\n AuxiliaryFileEncoding,\n Component,\n InterfaceState,\n} from '@metamask/snaps-sdk';\nimport type { FetchedSnapFiles } from '@metamask/snaps-utils';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Duplex } from 'readable-stream';\nimport { pipeline } from 'readable-stream';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getControllers, registerSnap } from './controllers';\nimport { getSnapFile } from './files';\nimport { getEndowments } from './methods';\nimport { createJsonRpcEngine } from './middleware';\nimport type { SimulationOptions, SimulationUserOptions } from './options';\nimport { getOptions } from './options';\nimport type { RunSagaFunction, Store } from './store';\nimport { createStore } from './store';\n\n/**\n * Options for the execution service, without the options that are shared\n * between all execution services.\n *\n * @template Service - The type of the execution service, i.e., the class that\n * creates the execution service.\n */\nexport type ExecutionServiceOptions<\n Service extends new (...args: any[]) => any,\n> = Omit<\n ConstructorParameters<Service>[0],\n keyof ConstructorParameters<typeof AbstractExecutionService<unknown>>[0]\n>;\n\n/**\n * The options for running a Snap in a simulated environment.\n *\n * @property executionService - The execution service to use.\n * @property executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @property options - The simulation options.\n * @template Service - The type of the execution service.\n */\nexport type InstallSnapOptions<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService<unknown>\n >,\n> = ExecutionServiceOptions<Service> extends Record<string, never>\n ? {\n executionService: Service;\n executionServiceOptions?: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n }\n : {\n executionService: Service;\n executionServiceOptions: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n };\n\nexport type InstalledSnap = {\n snapId: SnapId;\n store: Store;\n executionService: InstanceType<typeof AbstractExecutionService>;\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n runSaga: RunSagaFunction;\n};\n\nexport type MiddlewareHooks = {\n /**\n * A hook that returns the user's secret recovery phrase.\n *\n * @returns The user's secret recovery phrase.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * A hook that returns the Snap's auxiliary file for the given path.\n *\n * @param path - The path of the auxiliary file to get.\n * @param encoding - The encoding to use when returning the file.\n * @returns The Snap's auxiliary file for the given path.\n */\n getSnapFile: (\n path: string,\n encoding: AuxiliaryFileEncoding,\n ) => Promise<string | null>;\n\n /**\n * A hook that returns whether the client is locked or not.\n *\n * @returns A boolean flag signaling whether the client is locked.\n */\n getIsLocked: () => boolean;\n createInterface: (content: Component) => Promise<string>;\n updateInterface: (id: string, content: Component) => Promise<void>;\n getInterfaceState: (id: string) => InterfaceState;\n};\n\n/**\n * Install a Snap in a simulated environment. This will fetch the Snap files,\n * create a Redux store, set up the controllers and JSON-RPC stack, register the\n * Snap, and run the Snap code in the execution service.\n *\n * @param snapId - The ID of the Snap to install.\n * @param options - The options to use when installing the Snap.\n * @param options.executionService - The execution service to use.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @template Service - The type of the execution service.\n */\nexport async function handleInstallSnap<\n Service extends new (...args: any[]) => InstanceType<\n typeof AbstractExecutionService\n >,\n>(\n snapId: SnapId,\n {\n executionService,\n executionServiceOptions,\n options: rawOptions = {},\n }: Partial<InstallSnapOptions<Service>> = {},\n): Promise<InstalledSnap> {\n const options = getOptions(rawOptions);\n\n // Fetch Snap files.\n const location = detectSnapLocation(snapId, {\n allowLocal: true,\n });\n\n const snapFiles = await fetchSnap(snapId, location);\n\n // Create Redux store.\n const password = await getEncryptionKey({\n mnemonicPhrase: mnemonicPhraseToBytes(options.secretRecoveryPhrase),\n snapId,\n });\n\n const { store, runSaga } = createStore(password, options);\n\n const controllerMessenger = new ControllerMessenger<any, any>();\n\n registerActions(controllerMessenger);\n\n // Set up controllers and JSON-RPC stack.\n const hooks = getHooks(options, snapFiles, snapId, controllerMessenger);\n\n const { subjectMetadataController, permissionController } = getControllers({\n controllerMessenger,\n hooks,\n runSaga,\n options,\n });\n\n const engine = createJsonRpcEngine({\n store,\n hooks,\n permissionMiddleware: permissionController.createPermissionMiddleware({\n origin: snapId,\n }),\n });\n\n // Create execution service.\n const ExecutionService = executionService ?? NodeThreadExecutionService;\n const service = new ExecutionService({\n ...executionServiceOptions,\n messenger: controllerMessenger.getRestricted({\n name: 'ExecutionService',\n }),\n setupSnapProvider: (_snapId: string, rpcStream: Duplex) => {\n const mux = setupMultiplex(rpcStream, 'snapStream');\n const stream = mux.createStream('metamask-provider');\n const providerStream = createEngineStream({ engine });\n\n // Error function is difficult to test, so we ignore it.\n /* istanbul ignore next 2 */\n pipeline(stream, providerStream, stream, (error: unknown) => {\n if (error) {\n logError(`Provider stream failure.`, error);\n }\n });\n },\n });\n\n // Register the Snap. This sets up the Snap's permissions and subject\n // metadata.\n await registerSnap(snapId, snapFiles.manifest.result, {\n permissionController,\n subjectMetadataController,\n });\n\n // Run the Snap code in the execution service.\n await service.executeSnap({\n snapId,\n sourceCode: snapFiles.sourceCode.toString('utf8'),\n endowments: await getEndowments(permissionController, snapId),\n });\n\n return {\n snapId,\n store,\n executionService: service,\n controllerMessenger,\n runSaga,\n };\n}\n\n/**\n * Get the hooks for the simulation.\n *\n * @param options - The simulation options.\n * @param snapFiles - The Snap files.\n * @param snapId - The Snap ID.\n * @param controllerMessenger - The controller messenger.\n * @returns The hooks for the simulation.\n */\nexport function getHooks(\n options: SimulationOptions,\n snapFiles: FetchedSnapFiles,\n snapId: SnapId,\n controllerMessenger: RootControllerMessenger,\n): MiddlewareHooks {\n return {\n getMnemonic: async () =>\n Promise.resolve(mnemonicPhraseToBytes(options.secretRecoveryPhrase)),\n getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>\n await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),\n getIsLocked: () => false,\n createInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n ...args,\n ),\n updateInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:updateInterface',\n snapId,\n ...args,\n ),\n getInterfaceState: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).state,\n };\n}\n\n/**\n * Register mocked action handlers.\n *\n * @param controllerMessenger - The controller messenger.\n */\nexport function registerActions(controllerMessenger: RootControllerMessenger) {\n controllerMessenger.registerActionHandler(\n 'PhishingController:maybeUpdateState',\n async () => Promise.resolve(),\n );\n\n controllerMessenger.registerActionHandler(\n 'PhishingController:testOrigin',\n () => ({ result: false, type: 'all' }),\n );\n}\n"],"names":["ControllerMessenger","createEngineStream","mnemonicPhraseToBytes","fetchSnap","detectSnapLocation","NodeThreadExecutionService","setupMultiplex","getEncryptionKey","logError","pipeline","getControllers","registerSnap","getSnapFile","getEndowments","createJsonRpcEngine","getOptions","createStore","handleInstallSnap","snapId","executionService","executionServiceOptions","options","rawOptions","location","allowLocal","snapFiles","password","mnemonicPhrase","secretRecoveryPhrase","store","runSaga","controllerMessenger","registerActions","hooks","getHooks","subjectMetadataController","permissionController","engine","permissionMiddleware","createPermissionMiddleware","origin","ExecutionService","service","messenger","getRestricted","name","setupSnapProvider","_snapId","rpcStream","mux","stream","createStream","providerStream","error","manifest","result","executeSnap","sourceCode","toString","endowments","getMnemonic","Promise","resolve","path","encoding","auxiliaryFiles","getIsLocked","createInterface","args","call","updateInterface","getInterfaceState","state","registerActionHandler","type"],"mappings":"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"}
@@ -0,0 +1,6 @@
1
+ export * from './notifications';
2
+ export * from './state';
3
+ export * from './store';
4
+ export * from './ui';
5
+
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/internals/simulation/store/index.ts"],"sourcesContent":["export * from './notifications';\nexport * from './state';\nexport * from './store';\nexport * from './ui';\n"],"names":[],"mappings":"AAAA,cAAc,kBAAkB;AAChC,cAAc,UAAU;AACxB,cAAc,UAAU;AACxB,cAAc,OAAO"}
@@ -0,0 +1,32 @@
1
+ import { createSelector, createSlice } from '@reduxjs/toolkit';
2
+ /**
3
+ * The initial notifications state.
4
+ */ const INITIAL_STATE = {
5
+ jsonRpc: {}
6
+ };
7
+ export const mocksSlice = createSlice({
8
+ name: 'mocks',
9
+ initialState: INITIAL_STATE,
10
+ reducers: {
11
+ addJsonRpcMock: (state, action)=>{
12
+ // @ts-expect-error - TS2589: Type instantiation is excessively deep and
13
+ // possibly infinite.
14
+ state.jsonRpc[action.payload.method] = action.payload.result;
15
+ },
16
+ removeJsonRpcMock: (state, action)=>{
17
+ delete state.jsonRpc[action.payload];
18
+ }
19
+ }
20
+ });
21
+ export const { addJsonRpcMock, removeJsonRpcMock } = mocksSlice.actions;
22
+ /**
23
+ * Get the JSON-RPC mocks from the state.
24
+ *
25
+ * @param state - The application state.
26
+ * @returns The JSON-RPC mocks.
27
+ */ export const getJsonRpcMocks = (state)=>state.mocks.jsonRpc;
28
+ /**
29
+ * Get the JSON-RPC mock for a given method from the state.
30
+ */ export const getJsonRpcMock = createSelector(getJsonRpcMocks, (_, method)=>method, (jsonRpcMocks, method)=>jsonRpcMocks[method]);
31
+
32
+ //# sourceMappingURL=mocks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/internals/simulation/store/mocks.ts"],"sourcesContent":["import type { Json } from '@metamask/utils';\nimport type { PayloadAction } from '@reduxjs/toolkit';\nimport { createSelector, createSlice } from '@reduxjs/toolkit';\n\nimport type { ApplicationState } from './store';\n\nexport type JsonRpcMock = {\n method: string;\n result: Json;\n};\n\nexport type MocksState = {\n jsonRpc: Record<string, Json>;\n};\n\n/**\n * The initial notifications state.\n */\nconst INITIAL_STATE: MocksState = {\n jsonRpc: {},\n};\n\nexport const mocksSlice = createSlice({\n name: 'mocks',\n initialState: INITIAL_STATE,\n reducers: {\n addJsonRpcMock: (state, action: PayloadAction<JsonRpcMock>) => {\n // @ts-expect-error - TS2589: Type instantiation is excessively deep and\n // possibly infinite.\n state.jsonRpc[action.payload.method] = action.payload.result;\n },\n removeJsonRpcMock: (state, action: PayloadAction<string>) => {\n delete state.jsonRpc[action.payload];\n },\n },\n});\n\nexport const { addJsonRpcMock, removeJsonRpcMock } = mocksSlice.actions;\n\n/**\n * Get the JSON-RPC mocks from the state.\n *\n * @param state - The application state.\n * @returns The JSON-RPC mocks.\n */\nexport const getJsonRpcMocks = (state: ApplicationState) => state.mocks.jsonRpc;\n\n/**\n * Get the JSON-RPC mock for a given method from the state.\n */\nexport const getJsonRpcMock = createSelector(\n getJsonRpcMocks,\n (_: unknown, method: string) => method,\n (jsonRpcMocks, method) => jsonRpcMocks[method],\n);\n"],"names":["createSelector","createSlice","INITIAL_STATE","jsonRpc","mocksSlice","name","initialState","reducers","addJsonRpcMock","state","action","payload","method","result","removeJsonRpcMock","actions","getJsonRpcMocks","mocks","getJsonRpcMock","_","jsonRpcMocks"],"mappings":"AAEA,SAASA,cAAc,EAAEC,WAAW,QAAQ,mBAAmB;AAa/D;;CAEC,GACD,MAAMC,gBAA4B;IAChCC,SAAS,CAAC;AACZ;AAEA,OAAO,MAAMC,aAAaH,YAAY;IACpCI,MAAM;IACNC,cAAcJ;IACdK,UAAU;QACRC,gBAAgB,CAACC,OAAOC;YACtB,wEAAwE;YACxE,qBAAqB;YACrBD,MAAMN,OAAO,CAACO,OAAOC,OAAO,CAACC,MAAM,CAAC,GAAGF,OAAOC,OAAO,CAACE,MAAM;QAC9D;QACAC,mBAAmB,CAACL,OAAOC;YACzB,OAAOD,MAAMN,OAAO,CAACO,OAAOC,OAAO,CAAC;QACtC;IACF;AACF,GAAG;AAEH,OAAO,MAAM,EAAEH,cAAc,EAAEM,iBAAiB,EAAE,GAAGV,WAAWW,OAAO,CAAC;AAExE;;;;;CAKC,GACD,OAAO,MAAMC,kBAAkB,CAACP,QAA4BA,MAAMQ,KAAK,CAACd,OAAO,CAAC;AAEhF;;CAEC,GACD,OAAO,MAAMe,iBAAiBlB,eAC5BgB,iBACA,CAACG,GAAYP,SAAmBA,QAChC,CAACQ,cAAcR,SAAWQ,YAAY,CAACR,OAAO,EAC9C"}
@@ -0,0 +1,30 @@
1
+ import { createSelector, createSlice } from '@reduxjs/toolkit';
2
+ /**
3
+ * The initial notifications state.
4
+ */ const INITIAL_STATE = {
5
+ notifications: []
6
+ };
7
+ export const notificationsSlice = createSlice({
8
+ name: 'notifications',
9
+ initialState: INITIAL_STATE,
10
+ reducers: {
11
+ addNotification: (state, action)=>{
12
+ state.notifications.push(action.payload);
13
+ },
14
+ removeNotification: (state, action)=>{
15
+ state.notifications = state.notifications.filter((notification)=>notification.id !== action.payload);
16
+ },
17
+ clearNotifications: (state)=>{
18
+ state.notifications = [];
19
+ }
20
+ }
21
+ });
22
+ export const { addNotification, removeNotification, clearNotifications } = notificationsSlice.actions;
23
+ /**
24
+ * Get the notifications from the state.
25
+ *
26
+ * @param state - The application state.
27
+ * @returns An array of notifications.
28
+ */ export const getNotifications = createSelector((state)=>state.notifications, ({ notifications })=>notifications);
29
+
30
+ //# sourceMappingURL=notifications.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/internals/simulation/store/notifications.ts"],"sourcesContent":["import type { NotificationType } from '@metamask/snaps-sdk';\nimport type { PayloadAction } from '@reduxjs/toolkit';\nimport { createSelector, createSlice } from '@reduxjs/toolkit';\n\nimport type { ApplicationState } from './store';\n\n/**\n * A notification object.\n *\n * @property id - A unique ID for the notification.\n * @property message - The notification message.\n * @property type - The notification type.\n */\nexport type Notification = {\n id: string;\n message: string;\n type: NotificationType;\n};\n\n/**\n * The notifications state.\n *\n * @property notifications - An array of notifications.\n */\nexport type NotificationsState = {\n notifications: Notification[];\n};\n\n/**\n * The initial notifications state.\n */\nconst INITIAL_STATE: NotificationsState = {\n notifications: [],\n};\n\nexport const notificationsSlice = createSlice({\n name: 'notifications',\n initialState: INITIAL_STATE,\n reducers: {\n addNotification: (state, action: PayloadAction<Notification>) => {\n state.notifications.push(action.payload);\n },\n removeNotification: (state, action: PayloadAction<string>) => {\n state.notifications = state.notifications.filter(\n (notification) => notification.id !== action.payload,\n );\n },\n clearNotifications: (state) => {\n state.notifications = [];\n },\n },\n});\n\nexport const { addNotification, removeNotification, clearNotifications } =\n notificationsSlice.actions;\n\n/**\n * Get the notifications from the state.\n *\n * @param state - The application state.\n * @returns An array of notifications.\n */\nexport const getNotifications = createSelector(\n (state: ApplicationState) => state.notifications,\n ({ notifications }) => notifications,\n);\n"],"names":["createSelector","createSlice","INITIAL_STATE","notifications","notificationsSlice","name","initialState","reducers","addNotification","state","action","push","payload","removeNotification","filter","notification","id","clearNotifications","actions","getNotifications"],"mappings":"AAEA,SAASA,cAAc,EAAEC,WAAW,QAAQ,mBAAmB;AA0B/D;;CAEC,GACD,MAAMC,gBAAoC;IACxCC,eAAe,EAAE;AACnB;AAEA,OAAO,MAAMC,qBAAqBH,YAAY;IAC5CI,MAAM;IACNC,cAAcJ;IACdK,UAAU;QACRC,iBAAiB,CAACC,OAAOC;YACvBD,MAAMN,aAAa,CAACQ,IAAI,CAACD,OAAOE,OAAO;QACzC;QACAC,oBAAoB,CAACJ,OAAOC;YAC1BD,MAAMN,aAAa,GAAGM,MAAMN,aAAa,CAACW,MAAM,CAC9C,CAACC,eAAiBA,aAAaC,EAAE,KAAKN,OAAOE,OAAO;QAExD;QACAK,oBAAoB,CAACR;YACnBA,MAAMN,aAAa,GAAG,EAAE;QAC1B;IACF;AACF,GAAG;AAEH,OAAO,MAAM,EAAEK,eAAe,EAAEK,kBAAkB,EAAEI,kBAAkB,EAAE,GACtEb,mBAAmBc,OAAO,CAAC;AAE7B;;;;;CAKC,GACD,OAAO,MAAMC,mBAAmBnB,eAC9B,CAACS,QAA4BA,MAAMN,aAAa,EAChD,CAAC,EAAEA,aAAa,EAAE,GAAKA,eACvB"}
@@ -0,0 +1,47 @@
1
+ import { createSelector, createSlice } from '@reduxjs/toolkit';
2
+ /**
3
+ * The initial state.
4
+ */ const INITIAL_STATE = {
5
+ encrypted: null,
6
+ unencrypted: null
7
+ };
8
+ /**
9
+ * The state slice, which stores the state of the Snap.
10
+ */ export const stateSlice = createSlice({
11
+ name: 'state',
12
+ initialState: INITIAL_STATE,
13
+ reducers: {
14
+ setState: (state, action)=>{
15
+ if (action.payload.encrypted) {
16
+ state.encrypted = action.payload.state;
17
+ return state;
18
+ }
19
+ state.unencrypted = action.payload.state;
20
+ return state;
21
+ },
22
+ clearState: (state, action)=>{
23
+ if (action.payload.encrypted) {
24
+ state.encrypted = null;
25
+ return state;
26
+ }
27
+ state.unencrypted = null;
28
+ return state;
29
+ }
30
+ }
31
+ });
32
+ export const { setState, clearState } = stateSlice.actions;
33
+ /**
34
+ * Get the state from the store.
35
+ *
36
+ * @param encrypted - Whether to get the encrypted or unencrypted state.
37
+ * @returns A selector that returns the state.
38
+ */ export function getState(encrypted) {
39
+ return createSelector((state)=>state, ({ state })=>{
40
+ if (encrypted) {
41
+ return state.encrypted;
42
+ }
43
+ return state.unencrypted;
44
+ });
45
+ }
46
+
47
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/internals/simulation/store/state.ts"],"sourcesContent":["import type { PayloadAction } from '@reduxjs/toolkit';\nimport { createSelector, createSlice } from '@reduxjs/toolkit';\n\nimport type { ApplicationState } from './store';\n\n/**\n * The Snap state object.\n *\n * @property encrypted - The encrypted state. Can be null if the Snap does not\n * have an encrypted state.\n * @property unencrypted - The unencrypted state. Can be null if the Snap does\n * not have an unencrypted state.\n */\nexport type State = {\n encrypted: string | null;\n unencrypted: string | null;\n};\n\n/**\n * The initial state.\n */\nconst INITIAL_STATE: State = {\n encrypted: null,\n unencrypted: null,\n};\n\n/**\n * The state slice, which stores the state of the Snap.\n */\nexport const stateSlice = createSlice({\n name: 'state',\n initialState: INITIAL_STATE,\n reducers: {\n setState: (\n state,\n action: PayloadAction<{ state: string | null; encrypted: boolean }>,\n ) => {\n if (action.payload.encrypted) {\n state.encrypted = action.payload.state;\n return state;\n }\n\n state.unencrypted = action.payload.state;\n return state;\n },\n clearState: (state, action: PayloadAction<{ encrypted: boolean }>) => {\n if (action.payload.encrypted) {\n state.encrypted = null;\n return state;\n }\n\n state.unencrypted = null;\n return state;\n },\n },\n});\n\nexport const { setState, clearState } = stateSlice.actions;\n\n/**\n * Get the state from the store.\n *\n * @param encrypted - Whether to get the encrypted or unencrypted state.\n * @returns A selector that returns the state.\n */\nexport function getState(encrypted: boolean) {\n return createSelector(\n (state: ApplicationState) => state,\n ({ state }) => {\n if (encrypted) {\n return state.encrypted;\n }\n\n return state.unencrypted;\n },\n );\n}\n"],"names":["createSelector","createSlice","INITIAL_STATE","encrypted","unencrypted","stateSlice","name","initialState","reducers","setState","state","action","payload","clearState","actions","getState"],"mappings":"AACA,SAASA,cAAc,EAAEC,WAAW,QAAQ,mBAAmB;AAiB/D;;CAEC,GACD,MAAMC,gBAAuB;IAC3BC,WAAW;IACXC,aAAa;AACf;AAEA;;CAEC,GACD,OAAO,MAAMC,aAAaJ,YAAY;IACpCK,MAAM;IACNC,cAAcL;IACdM,UAAU;QACRC,UAAU,CACRC,OACAC;YAEA,IAAIA,OAAOC,OAAO,CAACT,SAAS,EAAE;gBAC5BO,MAAMP,SAAS,GAAGQ,OAAOC,OAAO,CAACF,KAAK;gBACtC,OAAOA;YACT;YAEAA,MAAMN,WAAW,GAAGO,OAAOC,OAAO,CAACF,KAAK;YACxC,OAAOA;QACT;QACAG,YAAY,CAACH,OAAOC;YAClB,IAAIA,OAAOC,OAAO,CAACT,SAAS,EAAE;gBAC5BO,MAAMP,SAAS,GAAG;gBAClB,OAAOO;YACT;YAEAA,MAAMN,WAAW,GAAG;YACpB,OAAOM;QACT;IACF;AACF,GAAG;AAEH,OAAO,MAAM,EAAED,QAAQ,EAAEI,UAAU,EAAE,GAAGR,WAAWS,OAAO,CAAC;AAE3D;;;;;CAKC,GACD,OAAO,SAASC,SAASZ,SAAkB;IACzC,OAAOH,eACL,CAACU,QAA4BA,OAC7B,CAAC,EAAEA,KAAK,EAAE;QACR,IAAIP,WAAW;YACb,OAAOO,MAAMP,SAAS;QACxB;QAEA,OAAOO,MAAMN,WAAW;IAC1B;AAEJ"}
@@ -0,0 +1,50 @@
1
+ import { configureStore } from '@reduxjs/toolkit';
2
+ import createSagaMiddleware from 'redux-saga';
3
+ import { mocksSlice } from './mocks';
4
+ import { notificationsSlice } from './notifications';
5
+ import { setState, stateSlice } from './state';
6
+ import { uiSlice } from './ui';
7
+ /**
8
+ * Create a Redux store.
9
+ *
10
+ * @param password - The password to use for state encryption.
11
+ * @param options - The simulation options.
12
+ * @param options.state - The initial state for the Snap.
13
+ * @param options.unencryptedState - The initial unencrypted state for the Snap.
14
+ * @returns A Redux store with the default state.
15
+ */ export function createStore(password, { state, unencryptedState }) {
16
+ const sagaMiddleware = createSagaMiddleware();
17
+ const store = configureStore({
18
+ reducer: {
19
+ mocks: mocksSlice.reducer,
20
+ notifications: notificationsSlice.reducer,
21
+ state: stateSlice.reducer,
22
+ ui: uiSlice.reducer
23
+ },
24
+ middleware: (getDefaultMiddleware)=>getDefaultMiddleware({
25
+ thunk: false
26
+ }).concat(sagaMiddleware)
27
+ });
28
+ // Set initial state for the Snap.
29
+ if (state) {
30
+ store.dispatch(setState({
31
+ state: JSON.stringify({
32
+ password,
33
+ value: state
34
+ }),
35
+ encrypted: true
36
+ }));
37
+ }
38
+ if (unencryptedState) {
39
+ store.dispatch(setState({
40
+ state: JSON.stringify(unencryptedState),
41
+ encrypted: false
42
+ }));
43
+ }
44
+ return {
45
+ store,
46
+ runSaga: sagaMiddleware.run.bind(sagaMiddleware)
47
+ };
48
+ }
49
+
50
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/internals/simulation/store/store.ts"],"sourcesContent":["import { configureStore } from '@reduxjs/toolkit';\nimport createSagaMiddleware from 'redux-saga';\n\nimport type { SimulationOptions } from '../options';\nimport { mocksSlice } from './mocks';\nimport { notificationsSlice } from './notifications';\nimport { setState, stateSlice } from './state';\nimport { uiSlice } from './ui';\n\n/**\n * Create a Redux store.\n *\n * @param password - The password to use for state encryption.\n * @param options - The simulation options.\n * @param options.state - The initial state for the Snap.\n * @param options.unencryptedState - The initial unencrypted state for the Snap.\n * @returns A Redux store with the default state.\n */\nexport function createStore(\n password: string,\n { state, unencryptedState }: SimulationOptions,\n) {\n const sagaMiddleware = createSagaMiddleware();\n const store = configureStore({\n reducer: {\n mocks: mocksSlice.reducer,\n notifications: notificationsSlice.reducer,\n state: stateSlice.reducer,\n ui: uiSlice.reducer,\n },\n middleware: (getDefaultMiddleware) =>\n getDefaultMiddleware({ thunk: false }).concat(sagaMiddleware),\n });\n\n // Set initial state for the Snap.\n if (state) {\n store.dispatch(\n setState({\n state: JSON.stringify({\n password,\n value: state,\n }),\n encrypted: true,\n }),\n );\n }\n\n if (unencryptedState) {\n store.dispatch(\n setState({\n state: JSON.stringify(unencryptedState),\n encrypted: false,\n }),\n );\n }\n\n return {\n store,\n runSaga: sagaMiddleware.run.bind(sagaMiddleware),\n };\n}\n\nexport type Store = ReturnType<typeof createStore>['store'];\nexport type ApplicationState = ReturnType<Store['getState']>;\nexport type RunSagaFunction = ReturnType<typeof createStore>['runSaga'];\n"],"names":["configureStore","createSagaMiddleware","mocksSlice","notificationsSlice","setState","stateSlice","uiSlice","createStore","password","state","unencryptedState","sagaMiddleware","store","reducer","mocks","notifications","ui","middleware","getDefaultMiddleware","thunk","concat","dispatch","JSON","stringify","value","encrypted","runSaga","run","bind"],"mappings":"AAAA,SAASA,cAAc,QAAQ,mBAAmB;AAClD,OAAOC,0BAA0B,aAAa;AAG9C,SAASC,UAAU,QAAQ,UAAU;AACrC,SAASC,kBAAkB,QAAQ,kBAAkB;AACrD,SAASC,QAAQ,EAAEC,UAAU,QAAQ,UAAU;AAC/C,SAASC,OAAO,QAAQ,OAAO;AAE/B;;;;;;;;CAQC,GACD,OAAO,SAASC,YACdC,QAAgB,EAChB,EAAEC,KAAK,EAAEC,gBAAgB,EAAqB;IAE9C,MAAMC,iBAAiBV;IACvB,MAAMW,QAAQZ,eAAe;QAC3Ba,SAAS;YACPC,OAAOZ,WAAWW,OAAO;YACzBE,eAAeZ,mBAAmBU,OAAO;YACzCJ,OAAOJ,WAAWQ,OAAO;YACzBG,IAAIV,QAAQO,OAAO;QACrB;QACAI,YAAY,CAACC,uBACXA,qBAAqB;gBAAEC,OAAO;YAAM,GAAGC,MAAM,CAACT;IAClD;IAEA,kCAAkC;IAClC,IAAIF,OAAO;QACTG,MAAMS,QAAQ,CACZjB,SAAS;YACPK,OAAOa,KAAKC,SAAS,CAAC;gBACpBf;gBACAgB,OAAOf;YACT;YACAgB,WAAW;QACb;IAEJ;IAEA,IAAIf,kBAAkB;QACpBE,MAAMS,QAAQ,CACZjB,SAAS;YACPK,OAAOa,KAAKC,SAAS,CAACb;YACtBe,WAAW;QACb;IAEJ;IAEA,OAAO;QACLb;QACAc,SAASf,eAAegB,GAAG,CAACC,IAAI,CAACjB;IACnC;AACF"}
@@ -0,0 +1,21 @@
1
+ import { createAction, createSelector, createSlice } from '@reduxjs/toolkit';
2
+ const INITIAL_STATE = {
3
+ current: null
4
+ };
5
+ export const uiSlice = createSlice({
6
+ name: 'ui',
7
+ initialState: INITIAL_STATE,
8
+ reducers: {
9
+ setInterface (state, action) {
10
+ state.current = action.payload;
11
+ },
12
+ closeInterface (state) {
13
+ state.current = null;
14
+ }
15
+ }
16
+ });
17
+ export const resolveInterface = createAction(`${uiSlice.name}/resolveInterface`);
18
+ export const { setInterface, closeInterface } = uiSlice.actions;
19
+ export const getCurrentInterface = createSelector((state)=>state.ui, (ui)=>ui.current);
20
+
21
+ //# sourceMappingURL=ui.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/internals/simulation/store/ui.ts"],"sourcesContent":["import type { DialogType } from '@metamask/snaps-sdk';\nimport type { PayloadAction } from '@reduxjs/toolkit';\nimport { createAction, createSelector, createSlice } from '@reduxjs/toolkit';\n\nimport type { ApplicationState } from './store';\n\nexport type Interface = {\n type: DialogType;\n id: string;\n};\n\nexport type UiState = {\n current?: Interface | null;\n};\n\nconst INITIAL_STATE: UiState = {\n current: null,\n};\n\nexport const uiSlice = createSlice({\n name: 'ui',\n initialState: INITIAL_STATE,\n reducers: {\n setInterface(state, action: PayloadAction<Interface>) {\n state.current = action.payload;\n },\n closeInterface(state) {\n state.current = null;\n },\n },\n});\n\nexport const resolveInterface = createAction<unknown>(\n `${uiSlice.name}/resolveInterface`,\n);\n\nexport const { setInterface, closeInterface } = uiSlice.actions;\n\nexport const getCurrentInterface = createSelector(\n (state: ApplicationState) => state.ui,\n (ui) => ui.current,\n);\n"],"names":["createAction","createSelector","createSlice","INITIAL_STATE","current","uiSlice","name","initialState","reducers","setInterface","state","action","payload","closeInterface","resolveInterface","actions","getCurrentInterface","ui"],"mappings":"AAEA,SAASA,YAAY,EAAEC,cAAc,EAAEC,WAAW,QAAQ,mBAAmB;AAa7E,MAAMC,gBAAyB;IAC7BC,SAAS;AACX;AAEA,OAAO,MAAMC,UAAUH,YAAY;IACjCI,MAAM;IACNC,cAAcJ;IACdK,UAAU;QACRC,cAAaC,KAAK,EAAEC,MAAgC;YAClDD,MAAMN,OAAO,GAAGO,OAAOC,OAAO;QAChC;QACAC,gBAAeH,KAAK;YAClBA,MAAMN,OAAO,GAAG;QAClB;IACF;AACF,GAAG;AAEH,OAAO,MAAMU,mBAAmBd,aAC9B,CAAC,EAAEK,QAAQC,IAAI,CAAC,iBAAiB,CAAC,EAClC;AAEF,OAAO,MAAM,EAAEG,YAAY,EAAEI,cAAc,EAAE,GAAGR,QAAQU,OAAO,CAAC;AAEhE,OAAO,MAAMC,sBAAsBf,eACjC,CAACS,QAA4BA,MAAMO,EAAE,EACrC,CAACA,KAAOA,GAAGb,OAAO,EAClB"}
@@ -1,8 +1,7 @@
1
- import { NotificationType, ComponentStruct } from '@metamask/snaps-sdk';
2
- import { enumValue } from '@metamask/snaps-utils';
1
+ import { NotificationType, ComponentStruct, enumValue } from '@metamask/snaps-sdk';
3
2
  import { bytesToHex, JsonStruct, StrictHexStruct, valueToBytes } from '@metamask/utils';
4
3
  import { randomBytes } from 'crypto';
5
- import { array, assign, bigint, coerce, defaulted, instance, literal, number, object, optional, string, type, union } from 'superstruct';
4
+ import { array, assign, bigint, coerce, defaulted, instance, literal, number, object, optional, string, type, union, record, any } from 'superstruct';
6
5
  // TODO: Export this from `@metamask/utils` instead.
7
6
  const BytesLikeStruct = union([
8
7
  bigint(),
@@ -23,7 +22,7 @@ export const TransactionOptionsStruct = object({
23
22
  */ // TODO: Move this coercer to `@metamask/utils`.
24
23
  from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value)=>{
25
24
  if (value) {
26
- return valueToBytes(value);
25
+ return bytesToHex(valueToBytes(value));
27
26
  }
28
27
  return bytesToHex(randomBytes(20));
29
28
  }),
@@ -33,7 +32,7 @@ export const TransactionOptionsStruct = object({
33
32
  */ // TODO: Move this coercer to `@metamask/utils`.
34
33
  to: coerce(StrictHexStruct, optional(BytesLikeStruct), (value)=>{
35
34
  if (value) {
36
- return valueToBytes(value);
35
+ return bytesToHex(valueToBytes(value));
37
36
  }
38
37
  return bytesToHex(randomBytes(20));
39
38
  }),
@@ -67,12 +66,49 @@ export const TransactionOptionsStruct = object({
67
66
  literal('0x')
68
67
  ]), BytesLikeStruct, (value)=>bytesToHex(valueToBytes(value))), '0x')
69
68
  });
69
+ export const SignatureOptionsStruct = object({
70
+ /**
71
+ * The origin making the signature request.
72
+ */ origin: defaulted(string(), 'metamask.io'),
73
+ /**
74
+ * The address signing the signature request. Defaults to a randomly generated
75
+ * address.
76
+ */ from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value)=>{
77
+ if (value) {
78
+ return bytesToHex(valueToBytes(value));
79
+ }
80
+ return bytesToHex(randomBytes(20));
81
+ }),
82
+ /**
83
+ * The data to send with the transaction. The data may be specified as a
84
+ * `string`, an object, or an array of objects. This covers the data types
85
+ * for the supported signature methods. Defaults to `0x`.
86
+ */ data: defaulted(union([
87
+ StrictHexStruct,
88
+ literal('0x'),
89
+ record(string(), any()),
90
+ array(record(string(), any()))
91
+ ]), '0x'),
92
+ /**
93
+ * The signature method being used.
94
+ */ signatureMethod: defaulted(union([
95
+ literal('eth_sign'),
96
+ literal('personal_sign'),
97
+ literal('eth_signTypedData'),
98
+ literal('eth_signTypedData_v3'),
99
+ literal('eth_signTypedData_v4')
100
+ ]), 'personal_sign')
101
+ });
70
102
  export const SnapOptionsStruct = object({
71
103
  /**
72
104
  * The timeout in milliseconds to use for requests to the snap. Defaults to
73
105
  * `1000`.
74
106
  */ timeout: defaulted(optional(number()), 1000)
75
107
  });
108
+ export const JsonRpcMockOptionsStruct = object({
109
+ method: string(),
110
+ result: JsonStruct
111
+ });
76
112
  export const InterfaceStruct = type({
77
113
  content: optional(ComponentStruct)
78
114
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/internals/structs.ts"],"sourcesContent":["import { NotificationType, ComponentStruct } from '@metamask/snaps-sdk';\nimport { enumValue } from '@metamask/snaps-utils';\nimport {\n bytesToHex,\n JsonStruct,\n StrictHexStruct,\n valueToBytes,\n} from '@metamask/utils';\nimport { randomBytes } from 'crypto';\nimport {\n array,\n assign,\n bigint,\n coerce,\n defaulted,\n instance,\n literal,\n number,\n object,\n optional,\n string,\n type,\n union,\n} from 'superstruct';\n\n// TODO: Export this from `@metamask/utils` instead.\nconst BytesLikeStruct = union([\n bigint(),\n number(),\n string(),\n instance(Uint8Array),\n]);\n\nexport const TransactionOptionsStruct = object({\n /**\n * The CAIP-2 chain ID to send the transaction on. Defaults to `eip155:1`.\n */\n chainId: defaulted(string(), 'eip155:1'),\n\n /**\n * The origin to send the transaction from. Defaults to `metamask.io`.\n */\n origin: defaulted(string(), 'metamask.io'),\n\n /**\n * The address to send the transaction from. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return valueToBytes(value);\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The address to send the transaction to. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n to: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return valueToBytes(value);\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The value to send with the transaction. The value may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n value: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x0',\n ),\n\n /**\n * The gas limit to use for the transaction. The gas limit may be specified\n * as a `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `21_000`.\n */\n gasLimit: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(21_000),\n ),\n\n /**\n * The max fee per gas (in Wei) to use for the transaction. The max fee per\n * gas may be specified as a `number`, `bigint`, `string`, or `Uint8Array`.\n * Defaults to `1`.\n */\n maxFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The max priority fee per gas (in Wei) to use for the transaction. The max\n * priority fee per gas may be specified as a `number`, `bigint`, `string`,\n * or `Uint8Array`. Defaults to `1`.\n */\n maxPriorityFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The nonce to use for the transaction. The nonce may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n nonce: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(0),\n ),\n\n /**\n * The data to send with the transaction. The data may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0x`.\n */\n data: defaulted(\n coerce(union([StrictHexStruct, literal('0x')]), BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x',\n ),\n});\n\nexport const SnapOptionsStruct = object({\n /**\n * The timeout in milliseconds to use for requests to the snap. Defaults to\n * `1000`.\n */\n timeout: defaulted(optional(number()), 1000),\n});\n\nexport const InterfaceStruct = type({\n content: optional(ComponentStruct),\n});\n\nexport const SnapResponseStruct = assign(\n InterfaceStruct,\n object({\n id: string(),\n\n response: union([\n object({\n result: JsonStruct,\n }),\n object({\n error: JsonStruct,\n }),\n ]),\n\n notifications: array(\n object({\n id: string(),\n message: string(),\n type: union([\n enumValue(NotificationType.InApp),\n enumValue(NotificationType.Native),\n ]),\n }),\n ),\n }),\n);\n"],"names":["NotificationType","ComponentStruct","enumValue","bytesToHex","JsonStruct","StrictHexStruct","valueToBytes","randomBytes","array","assign","bigint","coerce","defaulted","instance","literal","number","object","optional","string","type","union","BytesLikeStruct","Uint8Array","TransactionOptionsStruct","chainId","origin","from","value","to","gasLimit","maxFeePerGas","maxPriorityFeePerGas","nonce","data","SnapOptionsStruct","timeout","InterfaceStruct","content","SnapResponseStruct","id","response","result","error","notifications","message","InApp","Native"],"mappings":"AAAA,SAASA,gBAAgB,EAAEC,eAAe,QAAQ,sBAAsB;AACxE,SAASC,SAAS,QAAQ,wBAAwB;AAClD,SACEC,UAAU,EACVC,UAAU,EACVC,eAAe,EACfC,YAAY,QACP,kBAAkB;AACzB,SAASC,WAAW,QAAQ,SAAS;AACrC,SACEC,KAAK,EACLC,MAAM,EACNC,MAAM,EACNC,MAAM,EACNC,SAAS,EACTC,QAAQ,EACRC,OAAO,EACPC,MAAM,EACNC,MAAM,EACNC,QAAQ,EACRC,MAAM,EACNC,IAAI,EACJC,KAAK,QACA,cAAc;AAErB,oDAAoD;AACpD,MAAMC,kBAAkBD,MAAM;IAC5BV;IACAK;IACAG;IACAL,SAASS;CACV;AAED,OAAO,MAAMC,2BAA2BP,OAAO;IAC7C;;GAEC,GACDQ,SAASZ,UAAUM,UAAU;IAE7B;;GAEC,GACDO,QAAQb,UAAUM,UAAU;IAE5B;;;GAGC,GACD,gDAAgD;IAChDQ,MAAMf,OAAON,iBAAiBY,SAASI,kBAAkB,CAACM;QACxD,IAAIA,OAAO;YACT,OAAOrB,aAAaqB;QACtB;QAEA,OAAOxB,WAAWI,YAAY;IAChC;IAEA;;;GAGC,GACD,gDAAgD;IAChDqB,IAAIjB,OAAON,iBAAiBY,SAASI,kBAAkB,CAACM;QACtD,IAAIA,OAAO;YACT,OAAOrB,aAAaqB;QACtB;QAEA,OAAOxB,WAAWI,YAAY;IAChC;IAEA;;;GAGC,GACDoB,OAAOf,UACLD,OAAON,iBAAiBgB,iBAAiB,CAACM,QACxCxB,WAAWG,aAAaqB,UAE1B;IAGF;;;GAGC,GACDE,UAAUjB,UACRD,OAAON,iBAAiBgB,iBAAiB,CAACM,QACxCxB,WAAWG,aAAaqB,UAE1BrB,aAAa;IAGf;;;;GAIC,GACDwB,cAAclB,UACZD,OAAON,iBAAiBgB,iBAAiB,CAACM,QACxCxB,WAAWG,aAAaqB,UAE1BrB,aAAa;IAGf;;;;GAIC,GACDyB,sBAAsBnB,UACpBD,OAAON,iBAAiBgB,iBAAiB,CAACM,QACxCxB,WAAWG,aAAaqB,UAE1BrB,aAAa;IAGf;;;GAGC,GACD0B,OAAOpB,UACLD,OAAON,iBAAiBgB,iBAAiB,CAACM,QACxCxB,WAAWG,aAAaqB,UAE1BrB,aAAa;IAGf;;;GAGC,GACD2B,MAAMrB,UACJD,OAAOS,MAAM;QAACf;QAAiBS,QAAQ;KAAM,GAAGO,iBAAiB,CAACM,QAChExB,WAAWG,aAAaqB,UAE1B;AAEJ,GAAG;AAEH,OAAO,MAAMO,oBAAoBlB,OAAO;IACtC;;;GAGC,GACDmB,SAASvB,UAAUK,SAASF,WAAW;AACzC,GAAG;AAEH,OAAO,MAAMqB,kBAAkBjB,KAAK;IAClCkB,SAASpB,SAAShB;AACpB,GAAG;AAEH,OAAO,MAAMqC,qBAAqB7B,OAChC2B,iBACApB,OAAO;IACLuB,IAAIrB;IAEJsB,UAAUpB,MAAM;QACdJ,OAAO;YACLyB,QAAQrC;QACV;QACAY,OAAO;YACL0B,OAAOtC;QACT;KACD;IAEDuC,eAAenC,MACbQ,OAAO;QACLuB,IAAIrB;QACJ0B,SAAS1B;QACTC,MAAMC,MAAM;YACVlB,UAAUF,iBAAiB6C,KAAK;YAChC3C,UAAUF,iBAAiB8C,MAAM;SAClC;IACH;AAEJ,IACA"}
1
+ {"version":3,"sources":["../../../src/internals/structs.ts"],"sourcesContent":["import {\n NotificationType,\n ComponentStruct,\n enumValue,\n} from '@metamask/snaps-sdk';\nimport {\n bytesToHex,\n JsonStruct,\n StrictHexStruct,\n valueToBytes,\n} from '@metamask/utils';\nimport { randomBytes } from 'crypto';\nimport {\n array,\n assign,\n bigint,\n coerce,\n defaulted,\n instance,\n literal,\n number,\n object,\n optional,\n string,\n type,\n union,\n record,\n any,\n} from 'superstruct';\n\n// TODO: Export this from `@metamask/utils` instead.\nconst BytesLikeStruct = union([\n bigint(),\n number(),\n string(),\n instance(Uint8Array),\n]);\n\nexport const TransactionOptionsStruct = object({\n /**\n * The CAIP-2 chain ID to send the transaction on. Defaults to `eip155:1`.\n */\n chainId: defaulted(string(), 'eip155:1'),\n\n /**\n * The origin to send the transaction from. Defaults to `metamask.io`.\n */\n origin: defaulted(string(), 'metamask.io'),\n\n /**\n * The address to send the transaction from. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The address to send the transaction to. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n to: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The value to send with the transaction. The value may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n value: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x0',\n ),\n\n /**\n * The gas limit to use for the transaction. The gas limit may be specified\n * as a `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `21_000`.\n */\n gasLimit: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(21_000),\n ),\n\n /**\n * The max fee per gas (in Wei) to use for the transaction. The max fee per\n * gas may be specified as a `number`, `bigint`, `string`, or `Uint8Array`.\n * Defaults to `1`.\n */\n maxFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The max priority fee per gas (in Wei) to use for the transaction. The max\n * priority fee per gas may be specified as a `number`, `bigint`, `string`,\n * or `Uint8Array`. Defaults to `1`.\n */\n maxPriorityFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The nonce to use for the transaction. The nonce may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n nonce: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(0),\n ),\n\n /**\n * The data to send with the transaction. The data may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0x`.\n */\n data: defaulted(\n coerce(union([StrictHexStruct, literal('0x')]), BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x',\n ),\n});\n\nexport const SignatureOptionsStruct = object({\n /**\n * The origin making the signature request.\n */\n origin: defaulted(string(), 'metamask.io'),\n\n /**\n * The address signing the signature request. Defaults to a randomly generated\n * address.\n */\n from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The data to send with the transaction. The data may be specified as a\n * `string`, an object, or an array of objects. This covers the data types\n * for the supported signature methods. Defaults to `0x`.\n */\n data: defaulted(\n union([\n StrictHexStruct,\n literal('0x'),\n record(string(), any()),\n array(record(string(), any())),\n ]),\n '0x',\n ),\n\n /**\n * The signature method being used.\n */\n signatureMethod: defaulted(\n union([\n literal('eth_sign'),\n literal('personal_sign'),\n literal('eth_signTypedData'),\n literal('eth_signTypedData_v3'),\n literal('eth_signTypedData_v4'),\n ]),\n 'personal_sign',\n ),\n});\n\nexport const SnapOptionsStruct = object({\n /**\n * The timeout in milliseconds to use for requests to the snap. Defaults to\n * `1000`.\n */\n timeout: defaulted(optional(number()), 1000),\n});\n\nexport const JsonRpcMockOptionsStruct = object({\n method: string(),\n result: JsonStruct,\n});\n\nexport const InterfaceStruct = type({\n content: optional(ComponentStruct),\n});\n\nexport const SnapResponseStruct = assign(\n InterfaceStruct,\n object({\n id: string(),\n\n response: union([\n object({\n result: JsonStruct,\n }),\n object({\n error: JsonStruct,\n }),\n ]),\n\n notifications: array(\n object({\n id: string(),\n message: string(),\n type: union([\n enumValue(NotificationType.InApp),\n enumValue(NotificationType.Native),\n ]),\n }),\n ),\n }),\n);\n"],"names":["NotificationType","ComponentStruct","enumValue","bytesToHex","JsonStruct","StrictHexStruct","valueToBytes","randomBytes","array","assign","bigint","coerce","defaulted","instance","literal","number","object","optional","string","type","union","record","any","BytesLikeStruct","Uint8Array","TransactionOptionsStruct","chainId","origin","from","value","to","gasLimit","maxFeePerGas","maxPriorityFeePerGas","nonce","data","SignatureOptionsStruct","signatureMethod","SnapOptionsStruct","timeout","JsonRpcMockOptionsStruct","method","result","InterfaceStruct","content","SnapResponseStruct","id","response","error","notifications","message","InApp","Native"],"mappings":"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"}
@@ -77,7 +77,7 @@ export const toRespondWithError = function(actual, expected) {
77
77
  * @param expected - The expected notification message.
78
78
  * @param type - The expected notification type.
79
79
  * @returns The status and message.
80
- */ export const toSendNotification = async function(actual, expected, type) {
80
+ */ export const toSendNotification = function(actual, expected, type) {
81
81
  assertActualIsSnapResponse(actual, 'toSendNotification');
82
82
  const { notifications } = actual;
83
83
  const pass = notifications.some((notification)=>this.equals(notification.message, expected) && (type === undefined || notification.type === type));
@@ -91,8 +91,11 @@ export const toRender = function(actual, expected) {
91
91
  assertHasInterface(actual, 'toRender');
92
92
  const { content } = actual;
93
93
  const pass = this.equals(content, expected);
94
+ // This is typed as `string | null`, but in practice it's always a string. The
95
+ // function only returns `null` if both the expected and actual values are
96
+ // numbers, bigints, or booleans, which is never the case here.
94
97
  const difference = diff(expected, content);
95
- const message = pass ? ()=>`${this.utils.matcherHint('.not.toShowInterface')}\n\n` + `Expected: ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(content)}` + `${difference ? `\n\nDifference:\n\n${difference}` : ''}` : ()=>`${this.utils.matcherHint('.toShowInterface')}\n\n` + `Expected: ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(content)}` + `${difference ? `\n\nDifference:\n\n${difference}` : ''}`;
98
+ const message = pass ? ()=>`${this.utils.matcherHint('.not.toRender')}\n\n` + `Expected: ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(content)}` + `\n\nDifference:\n\n${difference}` : ()=>`${this.utils.matcherHint('.toRender')}\n\n` + `Expected: ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(content)}` + `\n\nDifference:\n\n${difference}`;
96
99
  return {
97
100
  message,
98
101
  pass