@metamask/snaps-simulation 1.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 (268) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +15 -0
  3. package/README.md +14 -0
  4. package/dist/constants.cjs +21 -0
  5. package/dist/constants.cjs.map +1 -0
  6. package/dist/constants.d.cts +18 -0
  7. package/dist/constants.d.cts.map +1 -0
  8. package/dist/constants.d.mts +18 -0
  9. package/dist/constants.d.mts.map +1 -0
  10. package/dist/constants.mjs +18 -0
  11. package/dist/constants.mjs.map +1 -0
  12. package/dist/controllers.cjs +101 -0
  13. package/dist/controllers.cjs.map +1 -0
  14. package/dist/controllers.d.cts +41 -0
  15. package/dist/controllers.d.cts.map +1 -0
  16. package/dist/controllers.d.mts +41 -0
  17. package/dist/controllers.d.mts.map +1 -0
  18. package/dist/controllers.mjs +96 -0
  19. package/dist/controllers.mjs.map +1 -0
  20. package/dist/files.cjs +94 -0
  21. package/dist/files.cjs.map +1 -0
  22. package/dist/files.d.cts +49 -0
  23. package/dist/files.d.cts.map +1 -0
  24. package/dist/files.d.mts +49 -0
  25. package/dist/files.d.mts.map +1 -0
  26. package/dist/files.mjs +91 -0
  27. package/dist/files.mjs.map +1 -0
  28. package/dist/index.cjs +27 -0
  29. package/dist/index.cjs.map +1 -0
  30. package/dist/index.d.cts +11 -0
  31. package/dist/index.d.cts.map +1 -0
  32. package/dist/index.d.mts +11 -0
  33. package/dist/index.d.mts.map +1 -0
  34. package/dist/index.mjs +11 -0
  35. package/dist/index.mjs.map +1 -0
  36. package/dist/interface.cjs +569 -0
  37. package/dist/interface.cjs.map +1 -0
  38. package/dist/interface.d.cts +163 -0
  39. package/dist/interface.d.cts.map +1 -0
  40. package/dist/interface.d.mts +163 -0
  41. package/dist/interface.d.mts.map +1 -0
  42. package/dist/interface.mjs +554 -0
  43. package/dist/interface.mjs.map +1 -0
  44. package/dist/methods/constants.cjs +72 -0
  45. package/dist/methods/constants.cjs.map +1 -0
  46. package/dist/methods/constants.d.cts +11 -0
  47. package/dist/methods/constants.d.cts.map +1 -0
  48. package/dist/methods/constants.d.mts +11 -0
  49. package/dist/methods/constants.d.mts.map +1 -0
  50. package/dist/methods/constants.mjs +69 -0
  51. package/dist/methods/constants.mjs.map +1 -0
  52. package/dist/methods/hooks/get-preferences.cjs +18 -0
  53. package/dist/methods/hooks/get-preferences.cjs.map +1 -0
  54. package/dist/methods/hooks/get-preferences.d.cts +14 -0
  55. package/dist/methods/hooks/get-preferences.d.cts.map +1 -0
  56. package/dist/methods/hooks/get-preferences.d.mts +14 -0
  57. package/dist/methods/hooks/get-preferences.d.mts.map +1 -0
  58. package/dist/methods/hooks/get-preferences.mjs +14 -0
  59. package/dist/methods/hooks/get-preferences.mjs.map +1 -0
  60. package/dist/methods/hooks/index.cjs +22 -0
  61. package/dist/methods/hooks/index.cjs.map +1 -0
  62. package/dist/methods/hooks/index.d.cts +6 -0
  63. package/dist/methods/hooks/index.d.cts.map +1 -0
  64. package/dist/methods/hooks/index.d.mts +6 -0
  65. package/dist/methods/hooks/index.d.mts.map +1 -0
  66. package/dist/methods/hooks/index.mjs +6 -0
  67. package/dist/methods/hooks/index.mjs.map +1 -0
  68. package/dist/methods/hooks/interface.cjs +24 -0
  69. package/dist/methods/hooks/interface.cjs.map +1 -0
  70. package/dist/methods/hooks/interface.d.cts +17 -0
  71. package/dist/methods/hooks/interface.d.cts.map +1 -0
  72. package/dist/methods/hooks/interface.d.mts +17 -0
  73. package/dist/methods/hooks/interface.d.mts.map +1 -0
  74. package/dist/methods/hooks/interface.mjs +19 -0
  75. package/dist/methods/hooks/interface.mjs.map +1 -0
  76. package/dist/methods/hooks/notifications.cjs +58 -0
  77. package/dist/methods/hooks/notifications.cjs.map +1 -0
  78. package/dist/methods/hooks/notifications.d.cts +17 -0
  79. package/dist/methods/hooks/notifications.d.cts.map +1 -0
  80. package/dist/methods/hooks/notifications.d.mts +17 -0
  81. package/dist/methods/hooks/notifications.d.mts.map +1 -0
  82. package/dist/methods/hooks/notifications.mjs +55 -0
  83. package/dist/methods/hooks/notifications.mjs.map +1 -0
  84. package/dist/methods/hooks/request-user-approval.cjs +38 -0
  85. package/dist/methods/hooks/request-user-approval.cjs.map +1 -0
  86. package/dist/methods/hooks/request-user-approval.d.cts +16 -0
  87. package/dist/methods/hooks/request-user-approval.d.cts.map +1 -0
  88. package/dist/methods/hooks/request-user-approval.d.mts +16 -0
  89. package/dist/methods/hooks/request-user-approval.d.mts.map +1 -0
  90. package/dist/methods/hooks/request-user-approval.mjs +35 -0
  91. package/dist/methods/hooks/request-user-approval.mjs.map +1 -0
  92. package/dist/methods/hooks/state.cjs +84 -0
  93. package/dist/methods/hooks/state.cjs.map +1 -0
  94. package/dist/methods/hooks/state.d.cts +24 -0
  95. package/dist/methods/hooks/state.d.cts.map +1 -0
  96. package/dist/methods/hooks/state.d.mts +24 -0
  97. package/dist/methods/hooks/state.d.mts.map +1 -0
  98. package/dist/methods/hooks/state.mjs +79 -0
  99. package/dist/methods/hooks/state.mjs.map +1 -0
  100. package/dist/methods/index.cjs +18 -0
  101. package/dist/methods/index.cjs.map +1 -0
  102. package/dist/methods/index.d.cts +2 -0
  103. package/dist/methods/index.d.cts.map +1 -0
  104. package/dist/methods/index.d.mts +2 -0
  105. package/dist/methods/index.d.mts.map +1 -0
  106. package/dist/methods/index.mjs +2 -0
  107. package/dist/methods/index.mjs.map +1 -0
  108. package/dist/methods/specifications.cjs +85 -0
  109. package/dist/methods/specifications.cjs.map +1 -0
  110. package/dist/methods/specifications.d.cts +57 -0
  111. package/dist/methods/specifications.d.cts.map +1 -0
  112. package/dist/methods/specifications.d.mts +57 -0
  113. package/dist/methods/specifications.d.mts.map +1 -0
  114. package/dist/methods/specifications.mjs +78 -0
  115. package/dist/methods/specifications.mjs.map +1 -0
  116. package/dist/middleware/engine.cjs +37 -0
  117. package/dist/middleware/engine.cjs.map +1 -0
  118. package/dist/middleware/engine.d.cts +27 -0
  119. package/dist/middleware/engine.d.cts.map +1 -0
  120. package/dist/middleware/engine.d.mts +27 -0
  121. package/dist/middleware/engine.d.mts.map +1 -0
  122. package/dist/middleware/engine.mjs +34 -0
  123. package/dist/middleware/engine.mjs.map +1 -0
  124. package/dist/middleware/index.cjs +18 -0
  125. package/dist/middleware/index.cjs.map +1 -0
  126. package/dist/middleware/index.d.cts +2 -0
  127. package/dist/middleware/index.d.cts.map +1 -0
  128. package/dist/middleware/index.d.mts +2 -0
  129. package/dist/middleware/index.d.mts.map +1 -0
  130. package/dist/middleware/index.mjs +2 -0
  131. package/dist/middleware/index.mjs.map +1 -0
  132. package/dist/middleware/internal-methods/accounts.cjs +34 -0
  133. package/dist/middleware/internal-methods/accounts.cjs.map +1 -0
  134. package/dist/middleware/internal-methods/accounts.d.cts +20 -0
  135. package/dist/middleware/internal-methods/accounts.d.cts.map +1 -0
  136. package/dist/middleware/internal-methods/accounts.d.mts +20 -0
  137. package/dist/middleware/internal-methods/accounts.d.mts.map +1 -0
  138. package/dist/middleware/internal-methods/accounts.mjs +30 -0
  139. package/dist/middleware/internal-methods/accounts.mjs.map +1 -0
  140. package/dist/middleware/internal-methods/index.cjs +18 -0
  141. package/dist/middleware/internal-methods/index.cjs.map +1 -0
  142. package/dist/middleware/internal-methods/index.d.cts +2 -0
  143. package/dist/middleware/internal-methods/index.d.cts.map +1 -0
  144. package/dist/middleware/internal-methods/index.d.mts +2 -0
  145. package/dist/middleware/internal-methods/index.d.mts.map +1 -0
  146. package/dist/middleware/internal-methods/index.mjs +2 -0
  147. package/dist/middleware/internal-methods/index.mjs.map +1 -0
  148. package/dist/middleware/internal-methods/middleware.cjs +44 -0
  149. package/dist/middleware/internal-methods/middleware.cjs.map +1 -0
  150. package/dist/middleware/internal-methods/middleware.d.cts +23 -0
  151. package/dist/middleware/internal-methods/middleware.d.cts.map +1 -0
  152. package/dist/middleware/internal-methods/middleware.d.mts +23 -0
  153. package/dist/middleware/internal-methods/middleware.d.mts.map +1 -0
  154. package/dist/middleware/internal-methods/middleware.mjs +40 -0
  155. package/dist/middleware/internal-methods/middleware.mjs.map +1 -0
  156. package/dist/middleware/internal-methods/provider-state.cjs +28 -0
  157. package/dist/middleware/internal-methods/provider-state.cjs.map +1 -0
  158. package/dist/middleware/internal-methods/provider-state.d.cts +16 -0
  159. package/dist/middleware/internal-methods/provider-state.d.cts.map +1 -0
  160. package/dist/middleware/internal-methods/provider-state.d.mts +16 -0
  161. package/dist/middleware/internal-methods/provider-state.d.mts.map +1 -0
  162. package/dist/middleware/internal-methods/provider-state.mjs +24 -0
  163. package/dist/middleware/internal-methods/provider-state.mjs.map +1 -0
  164. package/dist/middleware/mock.cjs +22 -0
  165. package/dist/middleware/mock.cjs.map +1 -0
  166. package/dist/middleware/mock.d.cts +11 -0
  167. package/dist/middleware/mock.d.cts.map +1 -0
  168. package/dist/middleware/mock.d.mts +11 -0
  169. package/dist/middleware/mock.d.mts.map +1 -0
  170. package/dist/middleware/mock.mjs +18 -0
  171. package/dist/middleware/mock.mjs.map +1 -0
  172. package/dist/options.cjs +25 -0
  173. package/dist/options.cjs.map +1 -0
  174. package/dist/options.d.cts +40 -0
  175. package/dist/options.d.cts.map +1 -0
  176. package/dist/options.d.mts +40 -0
  177. package/dist/options.d.mts.map +1 -0
  178. package/dist/options.mjs +21 -0
  179. package/dist/options.mjs.map +1 -0
  180. package/dist/request.cjs +144 -0
  181. package/dist/request.cjs.map +1 -0
  182. package/dist/request.d.cts +56 -0
  183. package/dist/request.d.cts.map +1 -0
  184. package/dist/request.d.mts +56 -0
  185. package/dist/request.d.mts.map +1 -0
  186. package/dist/request.mjs +139 -0
  187. package/dist/request.mjs.map +1 -0
  188. package/dist/simulation.cjs +155 -0
  189. package/dist/simulation.cjs.map +1 -0
  190. package/dist/simulation.d.cts +103 -0
  191. package/dist/simulation.d.cts.map +1 -0
  192. package/dist/simulation.d.mts +103 -0
  193. package/dist/simulation.d.mts.map +1 -0
  194. package/dist/simulation.mjs +150 -0
  195. package/dist/simulation.mjs.map +1 -0
  196. package/dist/store/index.cjs +22 -0
  197. package/dist/store/index.cjs.map +1 -0
  198. package/dist/store/index.d.cts +6 -0
  199. package/dist/store/index.d.cts.map +1 -0
  200. package/dist/store/index.d.mts +6 -0
  201. package/dist/store/index.d.mts.map +1 -0
  202. package/dist/store/index.mjs +6 -0
  203. package/dist/store/index.mjs.map +1 -0
  204. package/dist/store/mocks.cjs +39 -0
  205. package/dist/store/mocks.cjs.map +1 -0
  206. package/dist/store/mocks.d.cts +36 -0
  207. package/dist/store/mocks.d.cts.map +1 -0
  208. package/dist/store/mocks.d.mts +36 -0
  209. package/dist/store/mocks.d.mts.map +1 -0
  210. package/dist/store/mocks.mjs +35 -0
  211. package/dist/store/mocks.mjs.map +1 -0
  212. package/dist/store/notifications.cjs +35 -0
  213. package/dist/store/notifications.cjs.map +1 -0
  214. package/dist/store/notifications.d.cts +45 -0
  215. package/dist/store/notifications.d.cts.map +1 -0
  216. package/dist/store/notifications.d.mts +45 -0
  217. package/dist/store/notifications.d.mts.map +1 -0
  218. package/dist/store/notifications.mjs +32 -0
  219. package/dist/store/notifications.mjs.map +1 -0
  220. package/dist/store/state.cjs +54 -0
  221. package/dist/store/state.cjs.map +1 -0
  222. package/dist/store/state.d.cts +56 -0
  223. package/dist/store/state.d.cts.map +1 -0
  224. package/dist/store/state.d.mts +56 -0
  225. package/dist/store/state.d.mts.map +1 -0
  226. package/dist/store/state.mjs +50 -0
  227. package/dist/store/state.mjs.map +1 -0
  228. package/dist/store/store.cjs +51 -0
  229. package/dist/store/store.cjs.map +1 -0
  230. package/dist/store/store.d.cts +22 -0
  231. package/dist/store/store.d.cts.map +1 -0
  232. package/dist/store/store.d.mts +22 -0
  233. package/dist/store/store.d.mts.map +1 -0
  234. package/dist/store/store.mjs +45 -0
  235. package/dist/store/store.mjs.map +1 -0
  236. package/dist/store/ui.cjs +24 -0
  237. package/dist/store/ui.cjs.map +1 -0
  238. package/dist/store/ui.d.cts +27 -0
  239. package/dist/store/ui.d.cts.map +1 -0
  240. package/dist/store/ui.d.mts +27 -0
  241. package/dist/store/ui.d.mts.map +1 -0
  242. package/dist/store/ui.mjs +21 -0
  243. package/dist/store/ui.mjs.map +1 -0
  244. package/dist/structs.cjs +157 -0
  245. package/dist/structs.cjs.map +1 -0
  246. package/dist/structs.d.cts +205 -0
  247. package/dist/structs.d.cts.map +1 -0
  248. package/dist/structs.d.mts +205 -0
  249. package/dist/structs.d.mts.map +1 -0
  250. package/dist/structs.mjs +154 -0
  251. package/dist/structs.mjs.map +1 -0
  252. package/dist/types.cjs +3 -0
  253. package/dist/types.cjs.map +1 -0
  254. package/dist/types.d.cts +393 -0
  255. package/dist/types.d.cts.map +1 -0
  256. package/dist/types.d.mts +393 -0
  257. package/dist/types.d.mts.map +1 -0
  258. package/dist/types.mjs +2 -0
  259. package/dist/types.mjs.map +1 -0
  260. package/dist/validation.cjs +74 -0
  261. package/dist/validation.cjs.map +1 -0
  262. package/dist/validation.d.cts +44 -0
  263. package/dist/validation.d.cts.map +1 -0
  264. package/dist/validation.d.mts +44 -0
  265. package/dist/validation.d.mts.map +1 -0
  266. package/dist/validation.mjs +64 -0
  267. package/dist/validation.mjs.map +1 -0
  268. package/package.json +101 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.0]
10
+ ### Added
11
+ - Initial release of `@metamask/snaps-simulation` package ([#2727](https://github.com/MetaMask/snaps/pull/2727))
12
+
13
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-simulation@1.0.0...HEAD
14
+ [1.0.0]: https://github.com/MetaMask/snaps/releases/tag/@metamask/snaps-simulation@1.0.0
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2024 MetaMask
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # @metamask/snaps-simulation
2
+
3
+ A simulation framework for MetaMask Snaps, which runs in Node.js, and can be
4
+ used to test Snaps in a headless environment.
5
+
6
+ ## Installation
7
+
8
+ Use Node.js `18.0.0` or later. We recommend using [nvm](https://github.com/nvm-sh/nvm)
9
+ for managing Node.js versions.
10
+
11
+ Install a dependency in your snap project using `yarn` or `npm`:
12
+
13
+ - `npm install @metamask/snaps-simulation`
14
+ - `yarn add @metamask/snaps-simulation`
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_JSON_RPC_ENDPOINT = exports.DEFAULT_CURRENCY = exports.DEFAULT_LOCALE = exports.DEFAULT_SRP = void 0;
4
+ /**
5
+ * A secret recovery phrase that is used for testing purposes. Do not use this
6
+ * to store any real funds!
7
+ */
8
+ exports.DEFAULT_SRP = 'test test test test test test test test test test test ball';
9
+ /**
10
+ * The default locale.
11
+ */
12
+ exports.DEFAULT_LOCALE = 'en';
13
+ /**
14
+ * The default currency.
15
+ */
16
+ exports.DEFAULT_CURRENCY = 'usd';
17
+ /**
18
+ * The default JSON-RPC endpoint for Ethereum requests.
19
+ */
20
+ exports.DEFAULT_JSON_RPC_ENDPOINT = 'https://cloudflare-eth.com/';
21
+ //# sourceMappingURL=constants.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACU,QAAA,WAAW,GACtB,6DAA6D,CAAC;AAEhE;;GAEG;AACU,QAAA,cAAc,GAAG,IAAI,CAAC;AAEnC;;GAEG;AACU,QAAA,gBAAgB,GAAG,KAAK,CAAC;AAEtC;;GAEG;AACU,QAAA,yBAAyB,GAAG,6BAA6B,CAAC","sourcesContent":["/**\n * A secret recovery phrase that is used for testing purposes. Do not use this\n * to store any real funds!\n */\nexport const DEFAULT_SRP =\n 'test test test test test test test test test test test ball';\n\n/**\n * The default locale.\n */\nexport const DEFAULT_LOCALE = 'en';\n\n/**\n * The default currency.\n */\nexport const DEFAULT_CURRENCY = 'usd';\n\n/**\n * The default JSON-RPC endpoint for Ethereum requests.\n */\nexport const DEFAULT_JSON_RPC_ENDPOINT = 'https://cloudflare-eth.com/';\n"]}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A secret recovery phrase that is used for testing purposes. Do not use this
3
+ * to store any real funds!
4
+ */
5
+ export declare const DEFAULT_SRP = "test test test test test test test test test test test ball";
6
+ /**
7
+ * The default locale.
8
+ */
9
+ export declare const DEFAULT_LOCALE = "en";
10
+ /**
11
+ * The default currency.
12
+ */
13
+ export declare const DEFAULT_CURRENCY = "usd";
14
+ /**
15
+ * The default JSON-RPC endpoint for Ethereum requests.
16
+ */
17
+ export declare const DEFAULT_JSON_RPC_ENDPOINT = "https://cloudflare-eth.com/";
18
+ //# sourceMappingURL=constants.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,WAAW,gEACuC,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,cAAc,OAAO,CAAC;AAEnC;;GAEG;AACH,eAAO,MAAM,gBAAgB,QAAQ,CAAC;AAEtC;;GAEG;AACH,eAAO,MAAM,yBAAyB,gCAAgC,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A secret recovery phrase that is used for testing purposes. Do not use this
3
+ * to store any real funds!
4
+ */
5
+ export declare const DEFAULT_SRP = "test test test test test test test test test test test ball";
6
+ /**
7
+ * The default locale.
8
+ */
9
+ export declare const DEFAULT_LOCALE = "en";
10
+ /**
11
+ * The default currency.
12
+ */
13
+ export declare const DEFAULT_CURRENCY = "usd";
14
+ /**
15
+ * The default JSON-RPC endpoint for Ethereum requests.
16
+ */
17
+ export declare const DEFAULT_JSON_RPC_ENDPOINT = "https://cloudflare-eth.com/";
18
+ //# sourceMappingURL=constants.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,WAAW,gEACuC,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,cAAc,OAAO,CAAC;AAEnC;;GAEG;AACH,eAAO,MAAM,gBAAgB,QAAQ,CAAC;AAEtC;;GAEG;AACH,eAAO,MAAM,yBAAyB,gCAAgC,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A secret recovery phrase that is used for testing purposes. Do not use this
3
+ * to store any real funds!
4
+ */
5
+ export const DEFAULT_SRP = 'test test test test test test test test test test test ball';
6
+ /**
7
+ * The default locale.
8
+ */
9
+ export const DEFAULT_LOCALE = 'en';
10
+ /**
11
+ * The default currency.
12
+ */
13
+ export const DEFAULT_CURRENCY = 'usd';
14
+ /**
15
+ * The default JSON-RPC endpoint for Ethereum requests.
16
+ */
17
+ export const DEFAULT_JSON_RPC_ENDPOINT = 'https://cloudflare-eth.com/';
18
+ //# sourceMappingURL=constants.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,6DAA6D,CAAC;AAEhE;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC;AAEnC;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAEtC;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,6BAA6B,CAAC","sourcesContent":["/**\n * A secret recovery phrase that is used for testing purposes. Do not use this\n * to store any real funds!\n */\nexport const DEFAULT_SRP =\n 'test test test test test test test test test test test ball';\n\n/**\n * The default locale.\n */\nexport const DEFAULT_LOCALE = 'en';\n\n/**\n * The default currency.\n */\nexport const DEFAULT_CURRENCY = 'usd';\n\n/**\n * The default JSON-RPC endpoint for Ethereum requests.\n */\nexport const DEFAULT_JSON_RPC_ENDPOINT = 'https://cloudflare-eth.com/';\n"]}
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerSnap = exports.getControllers = void 0;
4
+ const permission_controller_1 = require("@metamask/permission-controller");
5
+ const snaps_controllers_1 = require("@metamask/snaps-controllers");
6
+ const snaps_rpc_methods_1 = require("@metamask/snaps-rpc-methods");
7
+ const utils_1 = require("@metamask/utils");
8
+ const methods_1 = require("./methods/index.cjs");
9
+ const constants_1 = require("./methods/constants.cjs");
10
+ /**
11
+ * Get the controllers for the Snap.
12
+ *
13
+ * @param options - The options.
14
+ * @returns The controllers for the Snap.
15
+ */
16
+ function getControllers(options) {
17
+ const { controllerMessenger } = options;
18
+ const subjectMetadataController = new permission_controller_1.SubjectMetadataController({
19
+ messenger: controllerMessenger.getRestricted({
20
+ name: 'SubjectMetadataController',
21
+ allowedActions: [],
22
+ allowedEvents: [],
23
+ }),
24
+ subjectCacheLimit: 100,
25
+ });
26
+ const interfaceController = new snaps_controllers_1.SnapInterfaceController({
27
+ messenger: controllerMessenger.getRestricted({
28
+ name: 'SnapInterfaceController',
29
+ allowedActions: [
30
+ 'PhishingController:maybeUpdateState',
31
+ 'PhishingController:testOrigin',
32
+ 'ApprovalController:hasRequest',
33
+ 'ApprovalController:acceptRequest',
34
+ ],
35
+ allowedEvents: [],
36
+ }),
37
+ });
38
+ const permissionController = getPermissionController(options);
39
+ return {
40
+ permissionController,
41
+ subjectMetadataController,
42
+ interfaceController,
43
+ };
44
+ }
45
+ exports.getControllers = getControllers;
46
+ /**
47
+ * Get the permission controller for the Snap.
48
+ *
49
+ * @param options - The options.
50
+ * @param options.controllerMessenger - The controller messenger.
51
+ * @param options.options - Miscellaneous options.
52
+ * @returns The permission controller for the Snap.
53
+ */
54
+ function getPermissionController(options) {
55
+ const { controllerMessenger } = options;
56
+ const permissionSpecifications = (0, methods_1.getPermissionSpecifications)(options);
57
+ return new permission_controller_1.PermissionController({
58
+ messenger: controllerMessenger.getRestricted({
59
+ name: 'PermissionController',
60
+ allowedActions: [
61
+ `ApprovalController:addRequest`,
62
+ `ApprovalController:hasRequest`,
63
+ `ApprovalController:acceptRequest`,
64
+ `ApprovalController:rejectRequest`,
65
+ `SnapController:getPermitted`,
66
+ `SnapController:install`,
67
+ `SubjectMetadataController:getSubjectMetadata`,
68
+ ],
69
+ allowedEvents: [],
70
+ }),
71
+ caveatSpecifications: {
72
+ ...snaps_rpc_methods_1.caveatSpecifications,
73
+ ...snaps_rpc_methods_1.endowmentCaveatSpecifications,
74
+ },
75
+ permissionSpecifications,
76
+ unrestrictedMethods: constants_1.UNRESTRICTED_METHODS,
77
+ });
78
+ }
79
+ /**
80
+ * Register the Snap. This sets up the Snap's permissions and subject metadata.
81
+ *
82
+ * @param snapId - The ID of the Snap to install.
83
+ * @param manifest - The parsed manifest.
84
+ * @param controllers - The controllers for the Snap.
85
+ * @param controllers.permissionController - The permission controller.
86
+ * @param controllers.subjectMetadataController - The subject metadata controller.
87
+ */
88
+ async function registerSnap(snapId, manifest, { permissionController, subjectMetadataController, }) {
89
+ subjectMetadataController.addSubjectMetadata({
90
+ origin: snapId,
91
+ subjectType: permission_controller_1.SubjectType.Snap,
92
+ });
93
+ const approvedPermissions = (0, snaps_rpc_methods_1.processSnapPermissions)((0, utils_1.getSafeJson)(manifest.initialPermissions));
94
+ permissionController.grantPermissions({
95
+ approvedPermissions,
96
+ subject: { origin: snapId },
97
+ preserveExistingPermissions: false,
98
+ });
99
+ }
100
+ exports.registerSnap = registerSnap;
101
+ //# sourceMappingURL=controllers.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controllers.cjs","sourceRoot":"","sources":["../src/controllers.ts"],"names":[],"mappings":";;;AAOA,2EAIyC;AACzC,mEAAsE;AAMtE,mEAIqC;AAGrC,2CAA8C;AAE9C,iDAAwD;AACxD,uDAA2D;AAiC3D;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,OAA8B;IAC3D,MAAM,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC;IACxC,MAAM,yBAAyB,GAAG,IAAI,iDAAyB,CAAC;QAC9D,SAAS,EAAE,mBAAmB,CAAC,aAAa,CAAC;YAC3C,IAAI,EAAE,2BAA2B;YACjC,cAAc,EAAE,EAAE;YAClB,aAAa,EAAE,EAAE;SAClB,CAAC;QACF,iBAAiB,EAAE,GAAG;KACvB,CAAC,CAAC;IAEH,MAAM,mBAAmB,GAAG,IAAI,2CAAuB,CAAC;QACtD,SAAS,EAAE,mBAAmB,CAAC,aAAa,CAAC;YAC3C,IAAI,EAAE,yBAAyB;YAC/B,cAAc,EAAE;gBACd,qCAAqC;gBACrC,+BAA+B;gBAC/B,+BAA+B;gBAC/B,kCAAkC;aACnC;YACD,aAAa,EAAE,EAAE;SAClB,CAAC;KACH,CAAC,CAAC;IAEH,MAAM,oBAAoB,GAAG,uBAAuB,CAAC,OAAO,CAAC,CAAC;IAE9D,OAAO;QACL,oBAAoB;QACpB,yBAAyB;QACzB,mBAAmB;KACpB,CAAC;AACJ,CAAC;AA/BD,wCA+BC;AAED;;;;;;;GAOG;AACH,SAAS,uBAAuB,CAAC,OAA8B;IAC7D,MAAM,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC;IACxC,MAAM,wBAAwB,GAAG,IAAA,qCAA2B,EAAC,OAAO,CAAC,CAAC;IACtE,OAAO,IAAI,4CAAoB,CAAC;QAC9B,SAAS,EAAE,mBAAmB,CAAC,aAAa,CAAC;YAC3C,IAAI,EAAE,sBAAsB;YAC5B,cAAc,EAAE;gBACd,+BAA+B;gBAC/B,+BAA+B;gBAC/B,kCAAkC;gBAClC,kCAAkC;gBAClC,6BAA6B;gBAC7B,wBAAwB;gBACxB,8CAA8C;aAC/C;YACD,aAAa,EAAE,EAAE;SAClB,CAAC;QACF,oBAAoB,EAAE;YACpB,GAAG,wCAA0B;YAC7B,GAAG,iDAAkC;SACtC;QACD,wBAAwB;QACxB,mBAAmB,EAAE,gCAAoB;KAC1C,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACI,KAAK,UAAU,YAAY,CAChC,MAAc,EACd,QAAsB,EACtB,EACE,oBAAoB,EACpB,yBAAyB,GACgB;IAE3C,yBAAyB,CAAC,kBAAkB,CAAC;QAC3C,MAAM,EAAE,MAAM;QACd,WAAW,EAAE,mCAAW,CAAC,IAAI;KAC9B,CAAC,CAAC;IAEH,MAAM,mBAAmB,GAAG,IAAA,0CAAsB,EAChD,IAAA,mBAAW,EAAC,QAAQ,CAAC,kBAAkB,CAAC,CACzC,CAAC;IAEF,oBAAoB,CAAC,gBAAgB,CAAC;QACpC,mBAAmB;QACnB,OAAO,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE;QAC3B,2BAA2B,EAAE,KAAK;KACnC,CAAC,CAAC;AACL,CAAC;AAtBD,oCAsBC","sourcesContent":["import type { ControllerMessenger } from '@metamask/base-controller';\nimport type {\n CaveatSpecificationConstraint,\n PermissionSpecificationConstraint,\n PermissionControllerActions,\n SubjectMetadataControllerActions,\n} from '@metamask/permission-controller';\nimport {\n PermissionController,\n SubjectMetadataController,\n SubjectType,\n} from '@metamask/permission-controller';\nimport { SnapInterfaceController } from '@metamask/snaps-controllers';\nimport type {\n ExecutionServiceActions,\n SnapInterfaceControllerActions,\n SnapInterfaceControllerAllowedActions,\n} from '@metamask/snaps-controllers';\nimport {\n caveatSpecifications as snapsCaveatsSpecifications,\n endowmentCaveatSpecifications as snapsEndowmentCaveatSpecifications,\n processSnapPermissions,\n} from '@metamask/snaps-rpc-methods';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport type { SnapManifest } from '@metamask/snaps-utils';\nimport { getSafeJson } from '@metamask/utils';\n\nimport { getPermissionSpecifications } from './methods';\nimport { UNRESTRICTED_METHODS } from './methods/constants';\nimport type { SimulationOptions } from './options';\nimport type { MiddlewareHooks } from './simulation';\nimport type { RunSagaFunction } from './store';\n\nexport type RootControllerAllowedActions =\n | SnapInterfaceControllerActions\n | SnapInterfaceControllerAllowedActions\n | PermissionControllerActions\n | ExecutionServiceActions\n | SubjectMetadataControllerActions;\n\nexport type RootControllerMessenger = ControllerMessenger<\n RootControllerAllowedActions,\n any\n>;\n\nexport type GetControllersOptions = {\n controllerMessenger: ControllerMessenger<any, any>;\n hooks: MiddlewareHooks;\n runSaga: RunSagaFunction;\n options: SimulationOptions;\n};\n\nexport type Controllers = {\n permissionController: PermissionController<\n PermissionSpecificationConstraint,\n CaveatSpecificationConstraint\n >;\n subjectMetadataController: SubjectMetadataController;\n interfaceController: SnapInterfaceController;\n};\n\n/**\n * Get the controllers for the Snap.\n *\n * @param options - The options.\n * @returns The controllers for the Snap.\n */\nexport function getControllers(options: GetControllersOptions): Controllers {\n const { controllerMessenger } = options;\n const subjectMetadataController = new SubjectMetadataController({\n messenger: controllerMessenger.getRestricted({\n name: 'SubjectMetadataController',\n allowedActions: [],\n allowedEvents: [],\n }),\n subjectCacheLimit: 100,\n });\n\n const interfaceController = new SnapInterfaceController({\n messenger: controllerMessenger.getRestricted({\n name: 'SnapInterfaceController',\n allowedActions: [\n 'PhishingController:maybeUpdateState',\n 'PhishingController:testOrigin',\n 'ApprovalController:hasRequest',\n 'ApprovalController:acceptRequest',\n ],\n allowedEvents: [],\n }),\n });\n\n const permissionController = getPermissionController(options);\n\n return {\n permissionController,\n subjectMetadataController,\n interfaceController,\n };\n}\n\n/**\n * Get the permission controller for the Snap.\n *\n * @param options - The options.\n * @param options.controllerMessenger - The controller messenger.\n * @param options.options - Miscellaneous options.\n * @returns The permission controller for the Snap.\n */\nfunction getPermissionController(options: GetControllersOptions) {\n const { controllerMessenger } = options;\n const permissionSpecifications = getPermissionSpecifications(options);\n return new PermissionController({\n messenger: controllerMessenger.getRestricted({\n name: 'PermissionController',\n allowedActions: [\n `ApprovalController:addRequest`,\n `ApprovalController:hasRequest`,\n `ApprovalController:acceptRequest`,\n `ApprovalController:rejectRequest`,\n `SnapController:getPermitted`,\n `SnapController:install`,\n `SubjectMetadataController:getSubjectMetadata`,\n ],\n allowedEvents: [],\n }),\n caveatSpecifications: {\n ...snapsCaveatsSpecifications,\n ...snapsEndowmentCaveatSpecifications,\n },\n permissionSpecifications,\n unrestrictedMethods: UNRESTRICTED_METHODS,\n });\n}\n\n/**\n * Register the Snap. This sets up the Snap's permissions and subject metadata.\n *\n * @param snapId - The ID of the Snap to install.\n * @param manifest - The parsed manifest.\n * @param controllers - The controllers for the Snap.\n * @param controllers.permissionController - The permission controller.\n * @param controllers.subjectMetadataController - The subject metadata controller.\n */\nexport async function registerSnap(\n snapId: SnapId,\n manifest: SnapManifest,\n {\n permissionController,\n subjectMetadataController,\n }: Omit<Controllers, 'interfaceController'>,\n) {\n subjectMetadataController.addSubjectMetadata({\n origin: snapId,\n subjectType: SubjectType.Snap,\n });\n\n const approvedPermissions = processSnapPermissions(\n getSafeJson(manifest.initialPermissions),\n );\n\n permissionController.grantPermissions({\n approvedPermissions,\n subject: { origin: snapId },\n preserveExistingPermissions: false,\n });\n}\n"]}
@@ -0,0 +1,41 @@
1
+ import type { ControllerMessenger } from "@metamask/base-controller";
2
+ import type { CaveatSpecificationConstraint, PermissionSpecificationConstraint, PermissionControllerActions, SubjectMetadataControllerActions } from "@metamask/permission-controller";
3
+ import { PermissionController, SubjectMetadataController } from "@metamask/permission-controller";
4
+ import { SnapInterfaceController } from "@metamask/snaps-controllers";
5
+ import type { ExecutionServiceActions, SnapInterfaceControllerActions, SnapInterfaceControllerAllowedActions } from "@metamask/snaps-controllers";
6
+ import type { SnapId } from "@metamask/snaps-sdk";
7
+ import type { SnapManifest } from "@metamask/snaps-utils";
8
+ import type { SimulationOptions } from "./options.cjs";
9
+ import type { MiddlewareHooks } from "./simulation.cjs";
10
+ import type { RunSagaFunction } from "./store/index.cjs";
11
+ export type RootControllerAllowedActions = SnapInterfaceControllerActions | SnapInterfaceControllerAllowedActions | PermissionControllerActions | ExecutionServiceActions | SubjectMetadataControllerActions;
12
+ export type RootControllerMessenger = ControllerMessenger<RootControllerAllowedActions, any>;
13
+ export type GetControllersOptions = {
14
+ controllerMessenger: ControllerMessenger<any, any>;
15
+ hooks: MiddlewareHooks;
16
+ runSaga: RunSagaFunction;
17
+ options: SimulationOptions;
18
+ };
19
+ export type Controllers = {
20
+ permissionController: PermissionController<PermissionSpecificationConstraint, CaveatSpecificationConstraint>;
21
+ subjectMetadataController: SubjectMetadataController;
22
+ interfaceController: SnapInterfaceController;
23
+ };
24
+ /**
25
+ * Get the controllers for the Snap.
26
+ *
27
+ * @param options - The options.
28
+ * @returns The controllers for the Snap.
29
+ */
30
+ export declare function getControllers(options: GetControllersOptions): Controllers;
31
+ /**
32
+ * Register the Snap. This sets up the Snap's permissions and subject metadata.
33
+ *
34
+ * @param snapId - The ID of the Snap to install.
35
+ * @param manifest - The parsed manifest.
36
+ * @param controllers - The controllers for the Snap.
37
+ * @param controllers.permissionController - The permission controller.
38
+ * @param controllers.subjectMetadataController - The subject metadata controller.
39
+ */
40
+ export declare function registerSnap(snapId: SnapId, manifest: SnapManifest, { permissionController, subjectMetadataController, }: Omit<Controllers, 'interfaceController'>): Promise<void>;
41
+ //# sourceMappingURL=controllers.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controllers.d.cts","sourceRoot":"","sources":["../src/controllers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAAkC;AACrE,OAAO,KAAK,EACV,6BAA6B,EAC7B,iCAAiC,EACjC,2BAA2B,EAC3B,gCAAgC,EACjC,wCAAwC;AACzC,OAAO,EACL,oBAAoB,EACpB,yBAAyB,EAE1B,wCAAwC;AACzC,OAAO,EAAE,uBAAuB,EAAE,oCAAoC;AACtE,OAAO,KAAK,EACV,uBAAuB,EACvB,8BAA8B,EAC9B,qCAAqC,EACtC,oCAAoC;AAMrC,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAK1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAkB;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,KAAK,EAAE,eAAe,EAAE,0BAAgB;AAE/C,MAAM,MAAM,4BAA4B,GACpC,8BAA8B,GAC9B,qCAAqC,GACrC,2BAA2B,GAC3B,uBAAuB,GACvB,gCAAgC,CAAC;AAErC,MAAM,MAAM,uBAAuB,GAAG,mBAAmB,CACvD,4BAA4B,EAC5B,GAAG,CACJ,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,mBAAmB,EAAE,mBAAmB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACnD,KAAK,EAAE,eAAe,CAAC;IACvB,OAAO,EAAE,eAAe,CAAC;IACzB,OAAO,EAAE,iBAAiB,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,oBAAoB,EAAE,oBAAoB,CACxC,iCAAiC,EACjC,6BAA6B,CAC9B,CAAC;IACF,yBAAyB,EAAE,yBAAyB,CAAC;IACrD,mBAAmB,EAAE,uBAAuB,CAAC;CAC9C,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,qBAAqB,GAAG,WAAW,CA+B1E;AAoCD;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,YAAY,EACtB,EACE,oBAAoB,EACpB,yBAAyB,GAC1B,EAAE,IAAI,CAAC,WAAW,EAAE,qBAAqB,CAAC,iBAgB5C"}
@@ -0,0 +1,41 @@
1
+ import type { ControllerMessenger } from "@metamask/base-controller";
2
+ import type { CaveatSpecificationConstraint, PermissionSpecificationConstraint, PermissionControllerActions, SubjectMetadataControllerActions } from "@metamask/permission-controller";
3
+ import { PermissionController, SubjectMetadataController } from "@metamask/permission-controller";
4
+ import { SnapInterfaceController } from "@metamask/snaps-controllers";
5
+ import type { ExecutionServiceActions, SnapInterfaceControllerActions, SnapInterfaceControllerAllowedActions } from "@metamask/snaps-controllers";
6
+ import type { SnapId } from "@metamask/snaps-sdk";
7
+ import type { SnapManifest } from "@metamask/snaps-utils";
8
+ import type { SimulationOptions } from "./options.mjs";
9
+ import type { MiddlewareHooks } from "./simulation.mjs";
10
+ import type { RunSagaFunction } from "./store/index.mjs";
11
+ export type RootControllerAllowedActions = SnapInterfaceControllerActions | SnapInterfaceControllerAllowedActions | PermissionControllerActions | ExecutionServiceActions | SubjectMetadataControllerActions;
12
+ export type RootControllerMessenger = ControllerMessenger<RootControllerAllowedActions, any>;
13
+ export type GetControllersOptions = {
14
+ controllerMessenger: ControllerMessenger<any, any>;
15
+ hooks: MiddlewareHooks;
16
+ runSaga: RunSagaFunction;
17
+ options: SimulationOptions;
18
+ };
19
+ export type Controllers = {
20
+ permissionController: PermissionController<PermissionSpecificationConstraint, CaveatSpecificationConstraint>;
21
+ subjectMetadataController: SubjectMetadataController;
22
+ interfaceController: SnapInterfaceController;
23
+ };
24
+ /**
25
+ * Get the controllers for the Snap.
26
+ *
27
+ * @param options - The options.
28
+ * @returns The controllers for the Snap.
29
+ */
30
+ export declare function getControllers(options: GetControllersOptions): Controllers;
31
+ /**
32
+ * Register the Snap. This sets up the Snap's permissions and subject metadata.
33
+ *
34
+ * @param snapId - The ID of the Snap to install.
35
+ * @param manifest - The parsed manifest.
36
+ * @param controllers - The controllers for the Snap.
37
+ * @param controllers.permissionController - The permission controller.
38
+ * @param controllers.subjectMetadataController - The subject metadata controller.
39
+ */
40
+ export declare function registerSnap(snapId: SnapId, manifest: SnapManifest, { permissionController, subjectMetadataController, }: Omit<Controllers, 'interfaceController'>): Promise<void>;
41
+ //# sourceMappingURL=controllers.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controllers.d.mts","sourceRoot":"","sources":["../src/controllers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAAkC;AACrE,OAAO,KAAK,EACV,6BAA6B,EAC7B,iCAAiC,EACjC,2BAA2B,EAC3B,gCAAgC,EACjC,wCAAwC;AACzC,OAAO,EACL,oBAAoB,EACpB,yBAAyB,EAE1B,wCAAwC;AACzC,OAAO,EAAE,uBAAuB,EAAE,oCAAoC;AACtE,OAAO,KAAK,EACV,uBAAuB,EACvB,8BAA8B,EAC9B,qCAAqC,EACtC,oCAAoC;AAMrC,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAK1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAkB;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,KAAK,EAAE,eAAe,EAAE,0BAAgB;AAE/C,MAAM,MAAM,4BAA4B,GACpC,8BAA8B,GAC9B,qCAAqC,GACrC,2BAA2B,GAC3B,uBAAuB,GACvB,gCAAgC,CAAC;AAErC,MAAM,MAAM,uBAAuB,GAAG,mBAAmB,CACvD,4BAA4B,EAC5B,GAAG,CACJ,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,mBAAmB,EAAE,mBAAmB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACnD,KAAK,EAAE,eAAe,CAAC;IACvB,OAAO,EAAE,eAAe,CAAC;IACzB,OAAO,EAAE,iBAAiB,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,oBAAoB,EAAE,oBAAoB,CACxC,iCAAiC,EACjC,6BAA6B,CAC9B,CAAC;IACF,yBAAyB,EAAE,yBAAyB,CAAC;IACrD,mBAAmB,EAAE,uBAAuB,CAAC;CAC9C,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,qBAAqB,GAAG,WAAW,CA+B1E;AAoCD;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,YAAY,EACtB,EACE,oBAAoB,EACpB,yBAAyB,GAC1B,EAAE,IAAI,CAAC,WAAW,EAAE,qBAAqB,CAAC,iBAgB5C"}
@@ -0,0 +1,96 @@
1
+ import { PermissionController, SubjectMetadataController, SubjectType } from "@metamask/permission-controller";
2
+ import { SnapInterfaceController } from "@metamask/snaps-controllers";
3
+ import { caveatSpecifications as snapsCaveatsSpecifications, endowmentCaveatSpecifications as snapsEndowmentCaveatSpecifications, processSnapPermissions } from "@metamask/snaps-rpc-methods";
4
+ import { getSafeJson } from "@metamask/utils";
5
+ import { getPermissionSpecifications } from "./methods/index.mjs";
6
+ import { UNRESTRICTED_METHODS } from "./methods/constants.mjs";
7
+ /**
8
+ * Get the controllers for the Snap.
9
+ *
10
+ * @param options - The options.
11
+ * @returns The controllers for the Snap.
12
+ */
13
+ export function getControllers(options) {
14
+ const { controllerMessenger } = options;
15
+ const subjectMetadataController = new SubjectMetadataController({
16
+ messenger: controllerMessenger.getRestricted({
17
+ name: 'SubjectMetadataController',
18
+ allowedActions: [],
19
+ allowedEvents: [],
20
+ }),
21
+ subjectCacheLimit: 100,
22
+ });
23
+ const interfaceController = new SnapInterfaceController({
24
+ messenger: controllerMessenger.getRestricted({
25
+ name: 'SnapInterfaceController',
26
+ allowedActions: [
27
+ 'PhishingController:maybeUpdateState',
28
+ 'PhishingController:testOrigin',
29
+ 'ApprovalController:hasRequest',
30
+ 'ApprovalController:acceptRequest',
31
+ ],
32
+ allowedEvents: [],
33
+ }),
34
+ });
35
+ const permissionController = getPermissionController(options);
36
+ return {
37
+ permissionController,
38
+ subjectMetadataController,
39
+ interfaceController,
40
+ };
41
+ }
42
+ /**
43
+ * Get the permission controller for the Snap.
44
+ *
45
+ * @param options - The options.
46
+ * @param options.controllerMessenger - The controller messenger.
47
+ * @param options.options - Miscellaneous options.
48
+ * @returns The permission controller for the Snap.
49
+ */
50
+ function getPermissionController(options) {
51
+ const { controllerMessenger } = options;
52
+ const permissionSpecifications = getPermissionSpecifications(options);
53
+ return new PermissionController({
54
+ messenger: controllerMessenger.getRestricted({
55
+ name: 'PermissionController',
56
+ allowedActions: [
57
+ `ApprovalController:addRequest`,
58
+ `ApprovalController:hasRequest`,
59
+ `ApprovalController:acceptRequest`,
60
+ `ApprovalController:rejectRequest`,
61
+ `SnapController:getPermitted`,
62
+ `SnapController:install`,
63
+ `SubjectMetadataController:getSubjectMetadata`,
64
+ ],
65
+ allowedEvents: [],
66
+ }),
67
+ caveatSpecifications: {
68
+ ...snapsCaveatsSpecifications,
69
+ ...snapsEndowmentCaveatSpecifications,
70
+ },
71
+ permissionSpecifications,
72
+ unrestrictedMethods: UNRESTRICTED_METHODS,
73
+ });
74
+ }
75
+ /**
76
+ * Register the Snap. This sets up the Snap's permissions and subject metadata.
77
+ *
78
+ * @param snapId - The ID of the Snap to install.
79
+ * @param manifest - The parsed manifest.
80
+ * @param controllers - The controllers for the Snap.
81
+ * @param controllers.permissionController - The permission controller.
82
+ * @param controllers.subjectMetadataController - The subject metadata controller.
83
+ */
84
+ export async function registerSnap(snapId, manifest, { permissionController, subjectMetadataController, }) {
85
+ subjectMetadataController.addSubjectMetadata({
86
+ origin: snapId,
87
+ subjectType: SubjectType.Snap,
88
+ });
89
+ const approvedPermissions = processSnapPermissions(getSafeJson(manifest.initialPermissions));
90
+ permissionController.grantPermissions({
91
+ approvedPermissions,
92
+ subject: { origin: snapId },
93
+ preserveExistingPermissions: false,
94
+ });
95
+ }
96
+ //# sourceMappingURL=controllers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controllers.mjs","sourceRoot":"","sources":["../src/controllers.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,oBAAoB,EACpB,yBAAyB,EACzB,WAAW,EACZ,wCAAwC;AACzC,OAAO,EAAE,uBAAuB,EAAE,oCAAoC;AAMtE,OAAO,EACL,oBAAoB,IAAI,0BAA0B,EAClD,6BAA6B,IAAI,kCAAkC,EACnE,sBAAsB,EACvB,oCAAoC;AAGrC,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAE9C,OAAO,EAAE,2BAA2B,EAAE,4BAAkB;AACxD,OAAO,EAAE,oBAAoB,EAAE,gCAA4B;AAiC3D;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,OAA8B;IAC3D,MAAM,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC;IACxC,MAAM,yBAAyB,GAAG,IAAI,yBAAyB,CAAC;QAC9D,SAAS,EAAE,mBAAmB,CAAC,aAAa,CAAC;YAC3C,IAAI,EAAE,2BAA2B;YACjC,cAAc,EAAE,EAAE;YAClB,aAAa,EAAE,EAAE;SAClB,CAAC;QACF,iBAAiB,EAAE,GAAG;KACvB,CAAC,CAAC;IAEH,MAAM,mBAAmB,GAAG,IAAI,uBAAuB,CAAC;QACtD,SAAS,EAAE,mBAAmB,CAAC,aAAa,CAAC;YAC3C,IAAI,EAAE,yBAAyB;YAC/B,cAAc,EAAE;gBACd,qCAAqC;gBACrC,+BAA+B;gBAC/B,+BAA+B;gBAC/B,kCAAkC;aACnC;YACD,aAAa,EAAE,EAAE;SAClB,CAAC;KACH,CAAC,CAAC;IAEH,MAAM,oBAAoB,GAAG,uBAAuB,CAAC,OAAO,CAAC,CAAC;IAE9D,OAAO;QACL,oBAAoB;QACpB,yBAAyB;QACzB,mBAAmB;KACpB,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,uBAAuB,CAAC,OAA8B;IAC7D,MAAM,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC;IACxC,MAAM,wBAAwB,GAAG,2BAA2B,CAAC,OAAO,CAAC,CAAC;IACtE,OAAO,IAAI,oBAAoB,CAAC;QAC9B,SAAS,EAAE,mBAAmB,CAAC,aAAa,CAAC;YAC3C,IAAI,EAAE,sBAAsB;YAC5B,cAAc,EAAE;gBACd,+BAA+B;gBAC/B,+BAA+B;gBAC/B,kCAAkC;gBAClC,kCAAkC;gBAClC,6BAA6B;gBAC7B,wBAAwB;gBACxB,8CAA8C;aAC/C;YACD,aAAa,EAAE,EAAE;SAClB,CAAC;QACF,oBAAoB,EAAE;YACpB,GAAG,0BAA0B;YAC7B,GAAG,kCAAkC;SACtC;QACD,wBAAwB;QACxB,mBAAmB,EAAE,oBAAoB;KAC1C,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAAc,EACd,QAAsB,EACtB,EACE,oBAAoB,EACpB,yBAAyB,GACgB;IAE3C,yBAAyB,CAAC,kBAAkB,CAAC;QAC3C,MAAM,EAAE,MAAM;QACd,WAAW,EAAE,WAAW,CAAC,IAAI;KAC9B,CAAC,CAAC;IAEH,MAAM,mBAAmB,GAAG,sBAAsB,CAChD,WAAW,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CACzC,CAAC;IAEF,oBAAoB,CAAC,gBAAgB,CAAC;QACpC,mBAAmB;QACnB,OAAO,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE;QAC3B,2BAA2B,EAAE,KAAK;KACnC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { ControllerMessenger } from '@metamask/base-controller';\nimport type {\n CaveatSpecificationConstraint,\n PermissionSpecificationConstraint,\n PermissionControllerActions,\n SubjectMetadataControllerActions,\n} from '@metamask/permission-controller';\nimport {\n PermissionController,\n SubjectMetadataController,\n SubjectType,\n} from '@metamask/permission-controller';\nimport { SnapInterfaceController } from '@metamask/snaps-controllers';\nimport type {\n ExecutionServiceActions,\n SnapInterfaceControllerActions,\n SnapInterfaceControllerAllowedActions,\n} from '@metamask/snaps-controllers';\nimport {\n caveatSpecifications as snapsCaveatsSpecifications,\n endowmentCaveatSpecifications as snapsEndowmentCaveatSpecifications,\n processSnapPermissions,\n} from '@metamask/snaps-rpc-methods';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport type { SnapManifest } from '@metamask/snaps-utils';\nimport { getSafeJson } from '@metamask/utils';\n\nimport { getPermissionSpecifications } from './methods';\nimport { UNRESTRICTED_METHODS } from './methods/constants';\nimport type { SimulationOptions } from './options';\nimport type { MiddlewareHooks } from './simulation';\nimport type { RunSagaFunction } from './store';\n\nexport type RootControllerAllowedActions =\n | SnapInterfaceControllerActions\n | SnapInterfaceControllerAllowedActions\n | PermissionControllerActions\n | ExecutionServiceActions\n | SubjectMetadataControllerActions;\n\nexport type RootControllerMessenger = ControllerMessenger<\n RootControllerAllowedActions,\n any\n>;\n\nexport type GetControllersOptions = {\n controllerMessenger: ControllerMessenger<any, any>;\n hooks: MiddlewareHooks;\n runSaga: RunSagaFunction;\n options: SimulationOptions;\n};\n\nexport type Controllers = {\n permissionController: PermissionController<\n PermissionSpecificationConstraint,\n CaveatSpecificationConstraint\n >;\n subjectMetadataController: SubjectMetadataController;\n interfaceController: SnapInterfaceController;\n};\n\n/**\n * Get the controllers for the Snap.\n *\n * @param options - The options.\n * @returns The controllers for the Snap.\n */\nexport function getControllers(options: GetControllersOptions): Controllers {\n const { controllerMessenger } = options;\n const subjectMetadataController = new SubjectMetadataController({\n messenger: controllerMessenger.getRestricted({\n name: 'SubjectMetadataController',\n allowedActions: [],\n allowedEvents: [],\n }),\n subjectCacheLimit: 100,\n });\n\n const interfaceController = new SnapInterfaceController({\n messenger: controllerMessenger.getRestricted({\n name: 'SnapInterfaceController',\n allowedActions: [\n 'PhishingController:maybeUpdateState',\n 'PhishingController:testOrigin',\n 'ApprovalController:hasRequest',\n 'ApprovalController:acceptRequest',\n ],\n allowedEvents: [],\n }),\n });\n\n const permissionController = getPermissionController(options);\n\n return {\n permissionController,\n subjectMetadataController,\n interfaceController,\n };\n}\n\n/**\n * Get the permission controller for the Snap.\n *\n * @param options - The options.\n * @param options.controllerMessenger - The controller messenger.\n * @param options.options - Miscellaneous options.\n * @returns The permission controller for the Snap.\n */\nfunction getPermissionController(options: GetControllersOptions) {\n const { controllerMessenger } = options;\n const permissionSpecifications = getPermissionSpecifications(options);\n return new PermissionController({\n messenger: controllerMessenger.getRestricted({\n name: 'PermissionController',\n allowedActions: [\n `ApprovalController:addRequest`,\n `ApprovalController:hasRequest`,\n `ApprovalController:acceptRequest`,\n `ApprovalController:rejectRequest`,\n `SnapController:getPermitted`,\n `SnapController:install`,\n `SubjectMetadataController:getSubjectMetadata`,\n ],\n allowedEvents: [],\n }),\n caveatSpecifications: {\n ...snapsCaveatsSpecifications,\n ...snapsEndowmentCaveatSpecifications,\n },\n permissionSpecifications,\n unrestrictedMethods: UNRESTRICTED_METHODS,\n });\n}\n\n/**\n * Register the Snap. This sets up the Snap's permissions and subject metadata.\n *\n * @param snapId - The ID of the Snap to install.\n * @param manifest - The parsed manifest.\n * @param controllers - The controllers for the Snap.\n * @param controllers.permissionController - The permission controller.\n * @param controllers.subjectMetadataController - The subject metadata controller.\n */\nexport async function registerSnap(\n snapId: SnapId,\n manifest: SnapManifest,\n {\n permissionController,\n subjectMetadataController,\n }: Omit<Controllers, 'interfaceController'>,\n) {\n subjectMetadataController.addSubjectMetadata({\n origin: snapId,\n subjectType: SubjectType.Snap,\n });\n\n const approvedPermissions = processSnapPermissions(\n getSafeJson(manifest.initialPermissions),\n );\n\n permissionController.grantPermissions({\n approvedPermissions,\n subject: { origin: snapId },\n preserveExistingPermissions: false,\n });\n}\n"]}
package/dist/files.cjs ADDED
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getFileToUpload = exports.getFileSize = exports.getContentType = exports.getSnapFile = void 0;
7
+ const snaps_sdk_1 = require("@metamask/snaps-sdk");
8
+ const snaps_utils_1 = require("@metamask/snaps-utils");
9
+ const utils_1 = require("@metamask/utils");
10
+ const promises_1 = require("fs/promises");
11
+ const mime_1 = __importDefault(require("mime/index.js"));
12
+ const path_1 = require("path");
13
+ /**
14
+ * Get a statically defined Snap file from an array of files.
15
+ *
16
+ * @param files - The Snap files.
17
+ * @param path - The file path.
18
+ * @param encoding - The requested file encoding.
19
+ * @returns The file in the requested encoding if found, otherwise null.
20
+ */
21
+ async function getSnapFile(files, path, encoding = snaps_sdk_1.AuxiliaryFileEncoding.Base64) {
22
+ const normalizedPath = (0, snaps_utils_1.normalizeRelative)(path);
23
+ const base64 = files
24
+ .find((file) => file.path === normalizedPath)
25
+ ?.toString('base64');
26
+ if (!base64) {
27
+ return null;
28
+ }
29
+ return await (0, snaps_utils_1.encodeAuxiliaryFile)(base64, encoding);
30
+ }
31
+ exports.getSnapFile = getSnapFile;
32
+ /**
33
+ * Get the content type of a file based on its extension.
34
+ *
35
+ * @param extension - The file extension.
36
+ * @returns The content type of the file. If the content type cannot be inferred
37
+ * from the extension, `application/octet-stream` is returned.
38
+ */
39
+ function getContentType(extension) {
40
+ return mime_1.default.getType(extension) ?? 'application/octet-stream';
41
+ }
42
+ exports.getContentType = getContentType;
43
+ /**
44
+ * Get the size of a file, from a file path or a `Uint8Array`.
45
+ *
46
+ * @param file - The file to get the size of. This can be a path to a file or a
47
+ * `Uint8Array` containing the file contents. If this is a path, the file is
48
+ * resolved relative to the current working directory.
49
+ * @returns The size of the file in bytes.
50
+ */
51
+ async function getFileSize(file) {
52
+ if (typeof file === 'string') {
53
+ const { size } = await (0, promises_1.stat)((0, path_1.resolve)(process.cwd(), file));
54
+ return size;
55
+ }
56
+ return file.length;
57
+ }
58
+ exports.getFileSize = getFileSize;
59
+ /**
60
+ * Get a file object to upload, from a file path or a `Uint8Array`, with an
61
+ * optional file name and content type.
62
+ *
63
+ * @param file - The file to upload. This can be a path to a file or a
64
+ * `Uint8Array` containing the file contents. If this is a path, the file is
65
+ * resolved relative to the current working directory.
66
+ * @param options - The file options.
67
+ * @param options.fileName - The name of the file. By default, this is
68
+ * inferred from the file path if it's a path, and defaults to an empty string
69
+ * if it's a `Uint8Array`.
70
+ * @param options.contentType - The content type of the file. By default, this
71
+ * is inferred from the file name if it's a path, and defaults to
72
+ * `application/octet-stream` if it's a `Uint8Array` or the content type
73
+ * cannot be inferred from the file name.
74
+ * @returns The file object to upload.
75
+ */
76
+ async function getFileToUpload(file, { fileName, contentType } = {}) {
77
+ if (typeof file === 'string') {
78
+ const buffer = await (0, promises_1.readFile)((0, path_1.resolve)(process.cwd(), file));
79
+ return {
80
+ name: fileName ?? (0, path_1.basename)(file),
81
+ size: buffer.byteLength,
82
+ contentType: contentType ?? getContentType((0, path_1.extname)(file)),
83
+ contents: (0, utils_1.bytesToBase64)(buffer),
84
+ };
85
+ }
86
+ return {
87
+ name: fileName ?? '',
88
+ size: file.length,
89
+ contentType: contentType ?? 'application/octet-stream',
90
+ contents: (0, utils_1.bytesToBase64)(file),
91
+ };
92
+ }
93
+ exports.getFileToUpload = getFileToUpload;
94
+ //# sourceMappingURL=files.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"files.cjs","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":";;;;;;AACA,mDAA4D;AAE5D,uDAA+E;AAC/E,2CAAgD;AAChD,0CAA6C;AAC7C,yDAAwB;AACxB,+BAAkD;AAIlD;;;;;;;GAOG;AACI,KAAK,UAAU,WAAW,CAC/B,KAAoB,EACpB,IAAY,EACZ,WAAkC,iCAAqB,CAAC,MAAM;IAE9D,MAAM,cAAc,GAAG,IAAA,+BAAiB,EAAC,IAAI,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,KAAK;SACjB,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC;QAC7C,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAEvB,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,MAAM,IAAA,iCAAmB,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AACrD,CAAC;AAfD,kCAeC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,SAAiB;IAC9C,OAAO,cAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,0BAA0B,CAAC;AAC/D,CAAC;AAFD,wCAEC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,WAAW,CAAC,IAAyB;IACzD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAA,eAAI,EAAC,IAAA,cAAO,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;QAC1D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,IAAI,CAAC,MAAM,CAAC;AACrB,CAAC;AAPD,kCAOC;AAED;;;;;;;;;;;;;;;;GAgBG;AACI,KAAK,UAAU,eAAe,CACnC,IAAyB,EACzB,EAAE,QAAQ,EAAE,WAAW,KAAkB,EAAE;IAE3C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,MAAM,GAAG,MAAM,IAAA,mBAAQ,EAAC,IAAA,cAAO,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;QAE5D,OAAO;YACL,IAAI,EAAE,QAAQ,IAAI,IAAA,eAAQ,EAAC,IAAI,CAAC;YAChC,IAAI,EAAE,MAAM,CAAC,UAAU;YACvB,WAAW,EAAE,WAAW,IAAI,cAAc,CAAC,IAAA,cAAO,EAAC,IAAI,CAAC,CAAC;YACzD,QAAQ,EAAE,IAAA,qBAAa,EAAC,MAAM,CAAC;SAChC,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,QAAQ,IAAI,EAAE;QACpB,IAAI,EAAE,IAAI,CAAC,MAAM;QACjB,WAAW,EAAE,WAAW,IAAI,0BAA0B;QACtD,QAAQ,EAAE,IAAA,qBAAa,EAAC,IAAI,CAAC;KAC9B,CAAC;AACJ,CAAC;AArBD,0CAqBC","sourcesContent":["import type { File } from '@metamask/snaps-sdk';\nimport { AuxiliaryFileEncoding } from '@metamask/snaps-sdk';\nimport type { VirtualFile } from '@metamask/snaps-utils';\nimport { encodeAuxiliaryFile, normalizeRelative } from '@metamask/snaps-utils';\nimport { bytesToBase64 } from '@metamask/utils';\nimport { readFile, stat } from 'fs/promises';\nimport mime from 'mime';\nimport { basename, extname, resolve } from 'path';\n\nimport type { FileOptions } from './types';\n\n/**\n * Get a statically defined Snap file from an array of files.\n *\n * @param files - The Snap files.\n * @param path - The file path.\n * @param encoding - The requested file encoding.\n * @returns The file in the requested encoding if found, otherwise null.\n */\nexport async function getSnapFile(\n files: VirtualFile[],\n path: string,\n encoding: AuxiliaryFileEncoding = AuxiliaryFileEncoding.Base64,\n) {\n const normalizedPath = normalizeRelative(path);\n const base64 = files\n .find((file) => file.path === normalizedPath)\n ?.toString('base64');\n\n if (!base64) {\n return null;\n }\n\n return await encodeAuxiliaryFile(base64, encoding);\n}\n\n/**\n * Get the content type of a file based on its extension.\n *\n * @param extension - The file extension.\n * @returns The content type of the file. If the content type cannot be inferred\n * from the extension, `application/octet-stream` is returned.\n */\nexport function getContentType(extension: string) {\n return mime.getType(extension) ?? 'application/octet-stream';\n}\n\n/**\n * Get the size of a file, from a file path or a `Uint8Array`.\n *\n * @param file - The file to get the size of. This can be a path to a file or a\n * `Uint8Array` containing the file contents. If this is a path, the file is\n * resolved relative to the current working directory.\n * @returns The size of the file in bytes.\n */\nexport async function getFileSize(file: string | Uint8Array) {\n if (typeof file === 'string') {\n const { size } = await stat(resolve(process.cwd(), file));\n return size;\n }\n\n return file.length;\n}\n\n/**\n * Get a file object to upload, from a file path or a `Uint8Array`, with an\n * optional file name and content type.\n *\n * @param file - The file to upload. This can be a path to a file or a\n * `Uint8Array` containing the file contents. If this is a path, the file is\n * resolved relative to the current working directory.\n * @param options - The file options.\n * @param options.fileName - The name of the file. By default, this is\n * inferred from the file path if it's a path, and defaults to an empty string\n * if it's a `Uint8Array`.\n * @param options.contentType - The content type of the file. By default, this\n * is inferred from the file name if it's a path, and defaults to\n * `application/octet-stream` if it's a `Uint8Array` or the content type\n * cannot be inferred from the file name.\n * @returns The file object to upload.\n */\nexport async function getFileToUpload(\n file: string | Uint8Array,\n { fileName, contentType }: FileOptions = {},\n): Promise<File> {\n if (typeof file === 'string') {\n const buffer = await readFile(resolve(process.cwd(), file));\n\n return {\n name: fileName ?? basename(file),\n size: buffer.byteLength,\n contentType: contentType ?? getContentType(extname(file)),\n contents: bytesToBase64(buffer),\n };\n }\n\n return {\n name: fileName ?? '',\n size: file.length,\n contentType: contentType ?? 'application/octet-stream',\n contents: bytesToBase64(file),\n };\n}\n"]}
@@ -0,0 +1,49 @@
1
+ import type { File } from "@metamask/snaps-sdk";
2
+ import { AuxiliaryFileEncoding } from "@metamask/snaps-sdk";
3
+ import type { VirtualFile } from "@metamask/snaps-utils";
4
+ import type { FileOptions } from "./types.cjs";
5
+ /**
6
+ * Get a statically defined Snap file from an array of files.
7
+ *
8
+ * @param files - The Snap files.
9
+ * @param path - The file path.
10
+ * @param encoding - The requested file encoding.
11
+ * @returns The file in the requested encoding if found, otherwise null.
12
+ */
13
+ export declare function getSnapFile(files: VirtualFile[], path: string, encoding?: AuxiliaryFileEncoding): Promise<string | null>;
14
+ /**
15
+ * Get the content type of a file based on its extension.
16
+ *
17
+ * @param extension - The file extension.
18
+ * @returns The content type of the file. If the content type cannot be inferred
19
+ * from the extension, `application/octet-stream` is returned.
20
+ */
21
+ export declare function getContentType(extension: string): string;
22
+ /**
23
+ * Get the size of a file, from a file path or a `Uint8Array`.
24
+ *
25
+ * @param file - The file to get the size of. This can be a path to a file or a
26
+ * `Uint8Array` containing the file contents. If this is a path, the file is
27
+ * resolved relative to the current working directory.
28
+ * @returns The size of the file in bytes.
29
+ */
30
+ export declare function getFileSize(file: string | Uint8Array): Promise<number>;
31
+ /**
32
+ * Get a file object to upload, from a file path or a `Uint8Array`, with an
33
+ * optional file name and content type.
34
+ *
35
+ * @param file - The file to upload. This can be a path to a file or a
36
+ * `Uint8Array` containing the file contents. If this is a path, the file is
37
+ * resolved relative to the current working directory.
38
+ * @param options - The file options.
39
+ * @param options.fileName - The name of the file. By default, this is
40
+ * inferred from the file path if it's a path, and defaults to an empty string
41
+ * if it's a `Uint8Array`.
42
+ * @param options.contentType - The content type of the file. By default, this
43
+ * is inferred from the file name if it's a path, and defaults to
44
+ * `application/octet-stream` if it's a `Uint8Array` or the content type
45
+ * cannot be inferred from the file name.
46
+ * @returns The file object to upload.
47
+ */
48
+ export declare function getFileToUpload(file: string | Uint8Array, { fileName, contentType }?: FileOptions): Promise<File>;
49
+ //# sourceMappingURL=files.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"files.d.cts","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,4BAA4B;AAChD,OAAO,EAAE,qBAAqB,EAAE,4BAA4B;AAC5D,OAAO,KAAK,EAAE,WAAW,EAAE,8BAA8B;AAOzD,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAgB;AAE3C;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,WAAW,EAAE,EACpB,IAAI,EAAE,MAAM,EACZ,QAAQ,GAAE,qBAAoD,0BAY/D;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,UAE/C;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,mBAO1D;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAE,WAAgB,GAC1C,OAAO,CAAC,IAAI,CAAC,CAkBf"}