@metamask/snaps-utils 0.34.1-flask.1 → 0.35.0-flask.1

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 (249) hide show
  1. package/CHANGELOG.md +21 -1
  2. package/dist/cjs/array.js +23 -0
  3. package/dist/cjs/array.js.map +1 -0
  4. package/dist/cjs/caveats.js +33 -0
  5. package/dist/cjs/caveats.js.map +1 -0
  6. package/dist/cjs/checksum.js +38 -0
  7. package/dist/cjs/checksum.js.map +1 -0
  8. package/dist/cjs/cronjob.js +84 -0
  9. package/dist/cjs/cronjob.js.map +1 -0
  10. package/dist/cjs/deep-clone.js +22 -0
  11. package/dist/cjs/deep-clone.js.map +1 -0
  12. package/dist/cjs/default-endowments.js +49 -0
  13. package/dist/cjs/default-endowments.js.map +1 -0
  14. package/dist/cjs/entropy.js +23 -0
  15. package/dist/cjs/entropy.js.map +1 -0
  16. package/dist/cjs/enum.js +16 -0
  17. package/dist/cjs/enum.js.map +1 -0
  18. package/dist/{eval-worker.js → cjs/eval-worker.js} +22 -21
  19. package/dist/cjs/eval-worker.js.map +1 -0
  20. package/dist/cjs/eval.js +29 -0
  21. package/dist/cjs/eval.js.map +1 -0
  22. package/dist/cjs/fs.js +126 -0
  23. package/dist/cjs/fs.js.map +1 -0
  24. package/dist/cjs/handlers.js +6 -0
  25. package/dist/cjs/handlers.js.map +1 -0
  26. package/dist/cjs/icon.js +37 -0
  27. package/dist/cjs/icon.js.map +1 -0
  28. package/dist/{iframe.js → cjs/iframe.js} +14 -9
  29. package/dist/cjs/iframe.js.map +1 -0
  30. package/dist/cjs/index.browser.js +40 -0
  31. package/dist/cjs/index.browser.js.map +1 -0
  32. package/dist/cjs/index.executionenv.js +24 -0
  33. package/dist/cjs/index.executionenv.js.map +1 -0
  34. package/dist/cjs/index.js +45 -0
  35. package/dist/cjs/index.js.map +1 -0
  36. package/dist/cjs/json-rpc.js +46 -0
  37. package/dist/cjs/json-rpc.js.map +1 -0
  38. package/dist/cjs/json.js +16 -0
  39. package/dist/cjs/json.js.map +1 -0
  40. package/dist/cjs/logging.js +40 -0
  41. package/dist/cjs/logging.js.map +1 -0
  42. package/dist/cjs/manifest/index.browser.js +20 -0
  43. package/dist/cjs/manifest/index.browser.js.map +1 -0
  44. package/dist/cjs/manifest/index.js +21 -0
  45. package/dist/cjs/manifest/index.js.map +1 -0
  46. package/dist/cjs/manifest/manifest.js +239 -0
  47. package/dist/cjs/manifest/manifest.js.map +1 -0
  48. package/dist/cjs/manifest/validation.js +183 -0
  49. package/dist/cjs/manifest/validation.js.map +1 -0
  50. package/dist/cjs/mock.js +128 -0
  51. package/dist/cjs/mock.js.map +1 -0
  52. package/dist/cjs/namespace.js +124 -0
  53. package/dist/cjs/namespace.js.map +1 -0
  54. package/dist/cjs/npm.js +81 -0
  55. package/dist/cjs/npm.js.map +1 -0
  56. package/dist/cjs/path.js +21 -0
  57. package/dist/cjs/path.js.map +1 -0
  58. package/dist/cjs/post-process.js +328 -0
  59. package/dist/cjs/post-process.js.map +1 -0
  60. package/dist/cjs/snaps.js +230 -0
  61. package/dist/cjs/snaps.js.map +1 -0
  62. package/dist/cjs/types.js +117 -0
  63. package/dist/cjs/types.js.map +1 -0
  64. package/dist/cjs/validation.js +22 -0
  65. package/dist/cjs/validation.js.map +1 -0
  66. package/dist/cjs/versions.js +47 -0
  67. package/dist/cjs/versions.js.map +1 -0
  68. package/dist/cjs/virtual-file/VirtualFile.js +85 -0
  69. package/dist/cjs/virtual-file/VirtualFile.js.map +1 -0
  70. package/dist/cjs/virtual-file/index.browser.js +20 -0
  71. package/dist/cjs/virtual-file/index.browser.js.map +1 -0
  72. package/dist/cjs/virtual-file/index.js +21 -0
  73. package/dist/cjs/virtual-file/index.js.map +1 -0
  74. package/dist/cjs/virtual-file/toVirtualFile.js +33 -0
  75. package/dist/cjs/virtual-file/toVirtualFile.js.map +1 -0
  76. package/dist/esm/array.js +13 -0
  77. package/dist/esm/array.js.map +1 -0
  78. package/dist/esm/caveats.js +23 -0
  79. package/dist/esm/caveats.js.map +1 -0
  80. package/dist/esm/checksum.js +36 -0
  81. package/dist/esm/checksum.js.map +1 -0
  82. package/dist/esm/cronjob.js +66 -0
  83. package/dist/esm/cronjob.js.map +1 -0
  84. package/dist/esm/deep-clone.js +7 -0
  85. package/dist/esm/deep-clone.js.map +1 -0
  86. package/dist/{default-endowments.js → esm/default-endowments.js} +3 -6
  87. package/dist/esm/default-endowments.js.map +1 -0
  88. package/dist/esm/entropy.js +6 -0
  89. package/dist/esm/entropy.js.map +1 -0
  90. package/dist/esm/enum.js +12 -0
  91. package/dist/esm/enum.js.map +1 -0
  92. package/dist/esm/eval-worker.js +47 -0
  93. package/dist/esm/eval-worker.js.map +1 -0
  94. package/dist/esm/eval.js +25 -0
  95. package/dist/esm/eval.js.map +1 -0
  96. package/dist/{fs.js → esm/fs.js} +33 -55
  97. package/dist/esm/fs.js.map +1 -0
  98. package/dist/esm/handlers.js +3 -0
  99. package/dist/esm/handlers.js.map +1 -0
  100. package/dist/esm/icon.js +11 -0
  101. package/dist/esm/icon.js.map +1 -0
  102. package/dist/esm/iframe.js +49 -0
  103. package/dist/esm/iframe.js.map +1 -0
  104. package/dist/esm/index.browser.js +23 -0
  105. package/dist/esm/index.browser.js.map +1 -0
  106. package/dist/esm/index.executionenv.js +7 -0
  107. package/dist/esm/index.executionenv.js.map +1 -0
  108. package/dist/esm/index.js +28 -0
  109. package/dist/esm/index.js.map +1 -0
  110. package/dist/esm/json-rpc.js +39 -0
  111. package/dist/esm/json-rpc.js.map +1 -0
  112. package/dist/esm/json.js +17 -0
  113. package/dist/esm/json.js.map +1 -0
  114. package/dist/{logging.js → esm/logging.js} +6 -14
  115. package/dist/esm/logging.js.map +1 -0
  116. package/dist/esm/manifest/index.browser.js +3 -0
  117. package/dist/esm/manifest/index.browser.js.map +1 -0
  118. package/dist/esm/manifest/index.js +4 -0
  119. package/dist/esm/manifest/index.js.map +1 -0
  120. package/dist/{manifest → esm/manifest}/manifest.js +91 -107
  121. package/dist/esm/manifest/manifest.js.map +1 -0
  122. package/dist/esm/manifest/validation.js +152 -0
  123. package/dist/esm/manifest/validation.js.map +1 -0
  124. package/dist/{mock.js → esm/mock.js} +40 -42
  125. package/dist/esm/mock.js.map +1 -0
  126. package/dist/esm/namespace.js +110 -0
  127. package/dist/esm/namespace.js.map +1 -0
  128. package/dist/{npm.js → esm/npm.js} +23 -30
  129. package/dist/esm/npm.js.map +1 -0
  130. package/dist/esm/path.js +17 -0
  131. package/dist/esm/path.js.map +1 -0
  132. package/dist/{post-process.js → esm/post-process.js} +110 -99
  133. package/dist/esm/post-process.js.map +1 -0
  134. package/dist/esm/snaps.js +215 -0
  135. package/dist/esm/snaps.js.map +1 -0
  136. package/dist/esm/types.js +85 -0
  137. package/dist/esm/types.js.map +1 -0
  138. package/dist/esm/validation.js +17 -0
  139. package/dist/esm/validation.js.map +1 -0
  140. package/dist/{versions.js → esm/versions.js} +15 -18
  141. package/dist/esm/versions.js.map +1 -0
  142. package/dist/{virtual-file → esm/virtual-file}/VirtualFile.js +47 -33
  143. package/dist/esm/virtual-file/VirtualFile.js.map +1 -0
  144. package/dist/esm/virtual-file/index.browser.js +3 -0
  145. package/dist/esm/virtual-file/index.browser.js.map +1 -0
  146. package/dist/esm/virtual-file/index.js +4 -0
  147. package/dist/esm/virtual-file/index.js.map +1 -0
  148. package/dist/esm/virtual-file/toVirtualFile.js +26 -0
  149. package/dist/esm/virtual-file/toVirtualFile.js.map +1 -0
  150. package/dist/types/array.d.ts +10 -0
  151. package/dist/{caveats.d.ts → types/caveats.d.ts} +0 -4
  152. package/dist/types/enum.d.ts +30 -0
  153. package/dist/{handlers.d.ts → types/handlers.d.ts} +1 -38
  154. package/dist/{index.browser.d.ts → types/index.browser.d.ts} +3 -1
  155. package/dist/{index.d.ts → types/index.d.ts} +3 -1
  156. package/dist/types/index.executionenv.d.ts +4 -0
  157. package/dist/types/json.d.ts +13 -0
  158. package/dist/{manifest → types/manifest}/validation.d.ts +6 -69
  159. package/dist/types/namespace.d.ts +124 -0
  160. package/dist/{types.d.ts → types/types.d.ts} +2 -5
  161. package/package.json +46 -21
  162. package/dist/caveats.js +0 -35
  163. package/dist/caveats.js.map +0 -1
  164. package/dist/checksum.js +0 -42
  165. package/dist/checksum.js.map +0 -1
  166. package/dist/cronjob.js +0 -71
  167. package/dist/cronjob.js.map +0 -1
  168. package/dist/deep-clone.js +0 -9
  169. package/dist/deep-clone.js.map +0 -1
  170. package/dist/default-endowments.js.map +0 -1
  171. package/dist/entropy.js +0 -8
  172. package/dist/entropy.js.map +0 -1
  173. package/dist/eval-worker.js.map +0 -1
  174. package/dist/eval.js +0 -27
  175. package/dist/eval.js.map +0 -1
  176. package/dist/fs.js.map +0 -1
  177. package/dist/handlers.js +0 -3
  178. package/dist/handlers.js.map +0 -1
  179. package/dist/icon.js +0 -17
  180. package/dist/icon.js.map +0 -1
  181. package/dist/iframe.js.map +0 -1
  182. package/dist/iframe.test.browser.js +0 -15
  183. package/dist/iframe.test.browser.js.map +0 -1
  184. package/dist/index.browser.js +0 -36
  185. package/dist/index.browser.js.map +0 -1
  186. package/dist/index.js +0 -41
  187. package/dist/index.js.map +0 -1
  188. package/dist/json-rpc.js +0 -46
  189. package/dist/json-rpc.js.map +0 -1
  190. package/dist/logging.js.map +0 -1
  191. package/dist/manifest/index.browser.js +0 -18
  192. package/dist/manifest/index.browser.js.map +0 -1
  193. package/dist/manifest/index.js +0 -19
  194. package/dist/manifest/index.js.map +0 -1
  195. package/dist/manifest/manifest.js.map +0 -1
  196. package/dist/manifest/validation.js +0 -146
  197. package/dist/manifest/validation.js.map +0 -1
  198. package/dist/mock.js.map +0 -1
  199. package/dist/namespace.d.ts +0 -275
  200. package/dist/namespace.js +0 -225
  201. package/dist/namespace.js.map +0 -1
  202. package/dist/notification.d.ts +0 -66
  203. package/dist/notification.js +0 -58
  204. package/dist/notification.js.map +0 -1
  205. package/dist/npm.js.map +0 -1
  206. package/dist/path.js +0 -21
  207. package/dist/path.js.map +0 -1
  208. package/dist/post-process.js.map +0 -1
  209. package/dist/snaps.js +0 -212
  210. package/dist/snaps.js.map +0 -1
  211. package/dist/types.js +0 -103
  212. package/dist/types.js.map +0 -1
  213. package/dist/validation.js +0 -21
  214. package/dist/validation.js.map +0 -1
  215. package/dist/versions.js.map +0 -1
  216. package/dist/virtual-file/VirtualFile.js.map +0 -1
  217. package/dist/virtual-file/index.browser.js +0 -18
  218. package/dist/virtual-file/index.browser.js.map +0 -1
  219. package/dist/virtual-file/index.js +0 -19
  220. package/dist/virtual-file/index.js.map +0 -1
  221. package/dist/virtual-file/toVirtualFile.js +0 -30
  222. package/dist/virtual-file/toVirtualFile.js.map +0 -1
  223. /package/dist/{checksum.d.ts → types/checksum.d.ts} +0 -0
  224. /package/dist/{cronjob.d.ts → types/cronjob.d.ts} +0 -0
  225. /package/dist/{deep-clone.d.ts → types/deep-clone.d.ts} +0 -0
  226. /package/dist/{default-endowments.d.ts → types/default-endowments.d.ts} +0 -0
  227. /package/dist/{entropy.d.ts → types/entropy.d.ts} +0 -0
  228. /package/dist/{eval-worker.d.ts → types/eval-worker.d.ts} +0 -0
  229. /package/dist/{eval.d.ts → types/eval.d.ts} +0 -0
  230. /package/dist/{fs.d.ts → types/fs.d.ts} +0 -0
  231. /package/dist/{icon.d.ts → types/icon.d.ts} +0 -0
  232. /package/dist/{iframe.d.ts → types/iframe.d.ts} +0 -0
  233. /package/dist/{iframe.test.browser.d.ts → types/iframe.test.browser.d.ts} +0 -0
  234. /package/dist/{json-rpc.d.ts → types/json-rpc.d.ts} +0 -0
  235. /package/dist/{logging.d.ts → types/logging.d.ts} +0 -0
  236. /package/dist/{manifest → types/manifest}/index.browser.d.ts +0 -0
  237. /package/dist/{manifest → types/manifest}/index.d.ts +0 -0
  238. /package/dist/{manifest → types/manifest}/manifest.d.ts +0 -0
  239. /package/dist/{mock.d.ts → types/mock.d.ts} +0 -0
  240. /package/dist/{npm.d.ts → types/npm.d.ts} +0 -0
  241. /package/dist/{path.d.ts → types/path.d.ts} +0 -0
  242. /package/dist/{post-process.d.ts → types/post-process.d.ts} +0 -0
  243. /package/dist/{snaps.d.ts → types/snaps.d.ts} +0 -0
  244. /package/dist/{validation.d.ts → types/validation.d.ts} +0 -0
  245. /package/dist/{versions.d.ts → types/versions.d.ts} +0 -0
  246. /package/dist/{virtual-file → types/virtual-file}/VirtualFile.d.ts +0 -0
  247. /package/dist/{virtual-file → types/virtual-file}/index.browser.d.ts +0 -0
  248. /package/dist/{virtual-file → types/virtual-file}/index.d.ts +0 -0
  249. /package/dist/{virtual-file → types/virtual-file}/toVirtualFile.d.ts +0 -0
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.logWarning = exports.logError = exports.logInfo = exports.snapsLogger = void 0;
4
- const utils_1 = require("@metamask/utils");
1
+ import { createProjectLogger } from '@metamask/utils';
5
2
  // The global logger used across the monorepo. Other projects should use this
6
3
  // to create a module logger.
7
- exports.snapsLogger = (0, utils_1.createProjectLogger)('snaps');
4
+ export const snapsLogger = createProjectLogger('snaps');
8
5
  /**
9
6
  * Log a message. Currently, this is just a wrapper around `console.log`, but
10
7
  * the implementation may change in the future. These logs will be included in
@@ -16,12 +13,10 @@ exports.snapsLogger = (0, utils_1.createProjectLogger)('snaps');
16
13
  *
17
14
  * @param message - The message to log.
18
15
  * @param optionalParams - Additional parameters to pass to the logging.
19
- */
20
- function logInfo(message, ...optionalParams) {
16
+ */ export function logInfo(message, ...optionalParams) {
21
17
  // eslint-disable-next-line no-console
22
18
  console.log(message, ...optionalParams);
23
19
  }
24
- exports.logInfo = logInfo;
25
20
  /**
26
21
  * Log an error. Currently, this is just a wrapper around `console.error`, but
27
22
  * the implementation may change in the future. These logs will be included in
@@ -37,12 +32,10 @@ exports.logInfo = logInfo;
37
32
  *
38
33
  * @param error - The error to log.
39
34
  * @param optionalParams - Additional parameters to pass to the logging.
40
- */
41
- function logError(error, ...optionalParams) {
35
+ */ export function logError(error, ...optionalParams) {
42
36
  // eslint-disable-next-line no-console
43
37
  console.error(error, ...optionalParams);
44
38
  }
45
- exports.logError = logError;
46
39
  /**
47
40
  * Log a warning. Currently, this is just a wrapper around `console.warn`, but
48
41
  * the implementation may change in the future. These logs will be included in
@@ -58,10 +51,9 @@ exports.logError = logError;
58
51
  *
59
52
  * @param message - The message to log.
60
53
  * @param optionalParams - Additional parameters to pass to the logging.
61
- */
62
- function logWarning(message, ...optionalParams) {
54
+ */ export function logWarning(message, ...optionalParams) {
63
55
  // eslint-disable-next-line no-console
64
56
  console.warn(message, ...optionalParams);
65
57
  }
66
- exports.logWarning = logWarning;
58
+
67
59
  //# sourceMappingURL=logging.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/logging.ts"],"sourcesContent":["import { createProjectLogger } from '@metamask/utils';\n\n// The global logger used across the monorepo. Other projects should use this\n// to create a module logger.\nexport const snapsLogger = createProjectLogger('snaps');\n\n/**\n * Log a message. Currently, this is just a wrapper around `console.log`, but\n * the implementation may change in the future. These logs will be included in\n * production builds, so they should be used sparingly, and not contain any\n * sensitive information.\n *\n * This function makes it easy to swap out the logging implementation in all\n * files at once.\n *\n * @param message - The message to log.\n * @param optionalParams - Additional parameters to pass to the logging.\n */\nexport function logInfo(message: string, ...optionalParams: unknown[]): void {\n // eslint-disable-next-line no-console\n console.log(message, ...optionalParams);\n}\n\n/**\n * Log an error. Currently, this is just a wrapper around `console.error`, but\n * the implementation may change in the future. These logs will be included in\n * production builds, so they should be used sparingly, and not contain any\n * sensitive information.\n *\n * These logs should always be visible, without requiring the user to enable\n * verbose logging (like setting a `DEBUG` environment variable), as they are\n * important for debugging snaps.\n *\n * This function makes it easy to swap out the logging implementation in all\n * files at once.\n *\n * @param error - The error to log.\n * @param optionalParams - Additional parameters to pass to the logging.\n */\nexport function logError(error: unknown, ...optionalParams: unknown[]): void {\n // eslint-disable-next-line no-console\n console.error(error, ...optionalParams);\n}\n\n/**\n * Log a warning. Currently, this is just a wrapper around `console.warn`, but\n * the implementation may change in the future. These logs will be included in\n * production builds, so they should be used sparingly, and not contain any\n * sensitive information.\n *\n * These logs should always be visible, without requiring the user to enable\n * verbose logging (like setting a `DEBUG` environment variable), as they are\n * important for debugging snaps.\n *\n * This function makes it easy to swap out the logging implementation in all\n * files at once.\n *\n * @param message - The message to log.\n * @param optionalParams - Additional parameters to pass to the logging.\n */\nexport function logWarning(\n message: string,\n ...optionalParams: unknown[]\n): void {\n // eslint-disable-next-line no-console\n console.warn(message, ...optionalParams);\n}\n"],"names":["createProjectLogger","snapsLogger","logInfo","message","optionalParams","console","log","logError","error","logWarning","warn"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,kBAAkB;AAEtD,6EAA6E;AAC7E,6BAA6B;AAC7B,OAAO,MAAMC,cAAcD,oBAAoB,SAAS;AAExD;;;;;;;;;;;CAWC,GACD,OAAO,SAASE,QAAQC,OAAe,EAAE,GAAGC,cAAyB;IACnE,sCAAsC;IACtCC,QAAQC,GAAG,CAACH,YAAYC;AAC1B;AAEA;;;;;;;;;;;;;;;CAeC,GACD,OAAO,SAASG,SAASC,KAAc,EAAE,GAAGJ,cAAyB;IACnE,sCAAsC;IACtCC,QAAQG,KAAK,CAACA,UAAUJ;AAC1B;AAEA;;;;;;;;;;;;;;;CAeC,GACD,OAAO,SAASK,WACdN,OAAe,EACf,GAAGC,cAAyB;IAE5B,sCAAsC;IACtCC,QAAQK,IAAI,CAACP,YAAYC;AAC3B"}
@@ -0,0 +1,3 @@
1
+ export * from './validation';
2
+
3
+ //# sourceMappingURL=index.browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/manifest/index.browser.ts"],"sourcesContent":["export * from './validation';\n"],"names":[],"mappings":"AAAA,cAAc,eAAe"}
@@ -0,0 +1,4 @@
1
+ export * from './manifest';
2
+ export * from './validation';
3
+
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/manifest/index.ts"],"sourcesContent":["export * from './manifest';\nexport * from './validation';\n"],"names":[],"mappings":"AAAA,cAAc,aAAa;AAC3B,cAAc,eAAe"}
@@ -1,19 +1,13 @@
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.validateNpmSnapManifest = exports.getWritableManifest = exports.getSnapIcon = exports.getSnapSourceCode = exports.fixManifest = exports.checkManifest = void 0;
7
- const utils_1 = require("@metamask/utils");
8
- const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
9
- const fs_1 = require("fs");
10
- const path_1 = __importDefault(require("path"));
11
- const deep_clone_1 = require("../deep-clone");
12
- const fs_2 = require("../fs");
13
- const npm_1 = require("../npm");
14
- const snaps_1 = require("../snaps");
15
- const types_1 = require("../types");
16
- const virtual_file_1 = require("../virtual-file");
1
+ import { assertExhaustive, assert, isPlainObject } from '@metamask/utils';
2
+ import deepEqual from 'fast-deep-equal';
3
+ import { promises as fs } from 'fs';
4
+ import pathUtils from 'path';
5
+ import { deepClone } from '../deep-clone';
6
+ import { readJsonFile } from '../fs';
7
+ import { validateNpmSnap } from '../npm';
8
+ import { getSnapChecksum, ProgrammaticallyFixableSnapError, validateSnapShasum } from '../snaps';
9
+ import { NpmSnapFileNames, SnapValidationFailureReason } from '../types';
10
+ import { readVirtualFile, VirtualFile } from '../virtual-file';
17
11
  const MANIFEST_SORT_ORDER = {
18
12
  $schema: 1,
19
13
  version: 2,
@@ -22,7 +16,7 @@ const MANIFEST_SORT_ORDER = {
22
16
  repository: 5,
23
17
  source: 6,
24
18
  initialPermissions: 7,
25
- manifestVersion: 8,
19
+ manifestVersion: 8
26
20
  };
27
21
  /**
28
22
  * Validates a snap.manifest.json file. Attempts to fix the manifest and write
@@ -34,70 +28,70 @@ const MANIFEST_SORT_ORDER = {
34
28
  * @param sourceCode - The source code of the Snap.
35
29
  * @returns Whether the manifest was updated, and an array of warnings that
36
30
  * were encountered during processing of the manifest files.
37
- */
38
- async function checkManifest(basePath, writeManifest = true, sourceCode) {
31
+ */ export async function checkManifest(basePath, writeManifest = true, sourceCode) {
39
32
  const warnings = [];
40
33
  const errors = [];
41
34
  let updated = false;
42
- const manifestPath = path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest);
43
- const manifestFile = await (0, fs_2.readJsonFile)(manifestPath);
35
+ const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);
36
+ const manifestFile = await readJsonFile(manifestPath);
44
37
  const unvalidatedManifest = manifestFile.result;
45
- const packageFile = await (0, fs_2.readJsonFile)(path_1.default.join(basePath, types_1.NpmSnapFileNames.PackageJson));
38
+ const packageFile = await readJsonFile(pathUtils.join(basePath, NpmSnapFileNames.PackageJson));
46
39
  const snapFiles = {
47
40
  manifest: manifestFile,
48
41
  packageJson: packageFile,
49
42
  sourceCode: await getSnapSourceCode(basePath, unvalidatedManifest, sourceCode),
50
- svgIcon: await getSnapIcon(basePath, unvalidatedManifest),
43
+ svgIcon: await getSnapIcon(basePath, unvalidatedManifest)
51
44
  };
52
45
  let manifest;
53
46
  try {
54
- ({ manifest } = (0, npm_1.validateNpmSnap)(snapFiles));
55
- }
56
- catch (error) {
57
- if (error instanceof snaps_1.ProgrammaticallyFixableSnapError) {
47
+ ({ manifest } = validateNpmSnap(snapFiles));
48
+ } catch (error) {
49
+ if (error instanceof ProgrammaticallyFixableSnapError) {
58
50
  errors.push(error.message);
59
51
  // If we get here, the files at least have the correct shape.
60
52
  const partiallyValidatedFiles = snapFiles;
61
53
  let isInvalid = true;
62
54
  let currentError = error;
63
- const maxAttempts = Object.keys(types_1.SnapValidationFailureReason).length;
55
+ const maxAttempts = Object.keys(SnapValidationFailureReason).length;
64
56
  // Attempt to fix all fixable validation failure reasons. All such reasons
65
57
  // are enumerated by the `SnapValidationFailureReason` enum, so we only
66
58
  // attempt to fix the manifest the same amount of times as there are
67
59
  // reasons in the enum.
68
- for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {
69
- manifest = fixManifest(manifest
70
- ? { ...partiallyValidatedFiles, manifest }
71
- : partiallyValidatedFiles, currentError);
60
+ for(let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++){
61
+ manifest = fixManifest(manifest ? {
62
+ ...partiallyValidatedFiles,
63
+ manifest
64
+ } : partiallyValidatedFiles, currentError);
72
65
  try {
73
- validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });
66
+ validateNpmSnapManifest({
67
+ ...partiallyValidatedFiles,
68
+ manifest
69
+ });
74
70
  isInvalid = false;
75
- }
76
- catch (nextValidationError) {
71
+ } catch (nextValidationError) {
77
72
  currentError = nextValidationError;
78
- /* istanbul ignore next: this should be impossible */
79
- if (!(nextValidationError instanceof snaps_1.ProgrammaticallyFixableSnapError) ||
80
- (attempts === maxAttempts && !isInvalid)) {
73
+ /* istanbul ignore next: this should be impossible */ if (!(nextValidationError instanceof ProgrammaticallyFixableSnapError) || attempts === maxAttempts && !isInvalid) {
81
74
  throw new Error(`Internal error: Failed to fix manifest. This is a bug, please report it. Reason:\n${error.message}`);
82
75
  }
83
76
  errors.push(currentError.message);
84
77
  }
85
78
  }
86
79
  updated = true;
87
- }
88
- else {
80
+ } else {
89
81
  throw error;
90
82
  }
91
83
  }
92
84
  // TypeScript assumes `manifest` can still be undefined, that is not the case.
93
85
  // But we assert to keep TypeScript happy.
94
- (0, utils_1.assert)(manifest);
86
+ assert(manifest);
95
87
  const validatedManifest = manifest.result;
96
88
  // Check presence of recommended keys
97
- const recommendedFields = ['repository'];
98
- const missingRecommendedFields = recommendedFields.filter((key) => !validatedManifest[key]);
89
+ const recommendedFields = [
90
+ 'repository'
91
+ ];
92
+ const missingRecommendedFields = recommendedFields.filter((key)=>!validatedManifest[key]);
99
93
  if (missingRecommendedFields.length > 0) {
100
- warnings.push(`Missing recommended package.json properties:\n${missingRecommendedFields.reduce((allMissing, currentField) => {
94
+ warnings.push(`Missing recommended package.json properties:\n${missingRecommendedFields.reduce((allMissing, currentField)=>{
101
95
  return `${allMissing}\t${currentField}\n`;
102
96
  }, '')}`);
103
97
  }
@@ -105,18 +99,21 @@ async function checkManifest(basePath, writeManifest = true, sourceCode) {
105
99
  try {
106
100
  const newManifest = `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\n`;
107
101
  if (updated || newManifest !== manifestFile.value) {
108
- await fs_1.promises.writeFile(path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest), newManifest);
102
+ await fs.writeFile(pathUtils.join(basePath, NpmSnapFileNames.Manifest), newManifest);
109
103
  }
110
- }
111
- catch (error) {
104
+ } catch (error) {
112
105
  // Note: This error isn't pushed to the errors array, because it's not an
113
106
  // error in the manifest itself.
114
107
  throw new Error(`Failed to update snap.manifest.json: ${error.message}`);
115
108
  }
116
109
  }
117
- return { manifest: validatedManifest, updated, warnings, errors };
110
+ return {
111
+ manifest: validatedManifest,
112
+ updated,
113
+ warnings,
114
+ errors
115
+ };
118
116
  }
119
- exports.checkManifest = checkManifest;
120
117
  /**
121
118
  * Given the relevant Snap files (manifest, `package.json`, and bundle) and a
122
119
  * Snap manifest validation error, fixes the fault in the manifest that caused
@@ -125,35 +122,30 @@ exports.checkManifest = checkManifest;
125
122
  * @param snapFiles - The contents of all Snap files.
126
123
  * @param error - The {@link ProgrammaticallyFixableSnapError} that was thrown.
127
124
  * @returns A copy of the manifest file where the cause of the error is fixed.
128
- */
129
- function fixManifest(snapFiles, error) {
125
+ */ export function fixManifest(snapFiles, error) {
130
126
  const { manifest, packageJson } = snapFiles;
131
127
  const clonedFile = manifest.clone();
132
128
  const manifestCopy = clonedFile.result;
133
- switch (error.reason) {
134
- case types_1.SnapValidationFailureReason.NameMismatch:
129
+ switch(error.reason){
130
+ case SnapValidationFailureReason.NameMismatch:
135
131
  manifestCopy.source.location.npm.packageName = packageJson.result.name;
136
132
  break;
137
- case types_1.SnapValidationFailureReason.VersionMismatch:
133
+ case SnapValidationFailureReason.VersionMismatch:
138
134
  manifestCopy.version = packageJson.result.version;
139
135
  break;
140
- case types_1.SnapValidationFailureReason.RepositoryMismatch:
141
- manifestCopy.repository = packageJson.result.repository
142
- ? (0, deep_clone_1.deepClone)(packageJson.result.repository)
143
- : undefined;
136
+ case SnapValidationFailureReason.RepositoryMismatch:
137
+ manifestCopy.repository = packageJson.result.repository ? deepClone(packageJson.result.repository) : undefined;
144
138
  break;
145
- case types_1.SnapValidationFailureReason.ShasumMismatch:
146
- manifestCopy.source.shasum = (0, snaps_1.getSnapChecksum)(snapFiles);
139
+ case SnapValidationFailureReason.ShasumMismatch:
140
+ manifestCopy.source.shasum = getSnapChecksum(snapFiles);
147
141
  break;
148
- /* istanbul ignore next */
149
- default:
150
- (0, utils_1.assertExhaustive)(error.reason);
142
+ /* istanbul ignore next */ default:
143
+ assertExhaustive(error.reason);
151
144
  }
152
145
  clonedFile.result = manifestCopy;
153
146
  clonedFile.value = JSON.stringify(manifestCopy);
154
147
  return clonedFile;
155
148
  }
156
- exports.fixManifest = fixManifest;
157
149
  /**
158
150
  * Given an unvalidated Snap manifest, attempts to extract the location of the
159
151
  * bundle source file location and read the file.
@@ -162,31 +154,27 @@ exports.fixManifest = fixManifest;
162
154
  * @param manifest - The unvalidated Snap manifest file contents.
163
155
  * @param sourceCode - Override source code for plugins.
164
156
  * @returns The contents of the bundle file, if any.
165
- */
166
- async function getSnapSourceCode(basePath, manifest, sourceCode) {
167
- if (!(0, utils_1.isPlainObject)(manifest)) {
157
+ */ export async function getSnapSourceCode(basePath, manifest, sourceCode) {
158
+ if (!isPlainObject(manifest)) {
168
159
  return undefined;
169
160
  }
170
- const sourceFilePath = manifest.source?.location
171
- ?.npm?.filePath;
161
+ const sourceFilePath = manifest.source?.location?.npm?.filePath;
172
162
  if (!sourceFilePath) {
173
163
  return undefined;
174
164
  }
175
165
  if (sourceCode) {
176
- return new virtual_file_1.VirtualFile({
177
- path: path_1.default.join(basePath, sourceFilePath),
178
- value: sourceCode,
166
+ return new VirtualFile({
167
+ path: pathUtils.join(basePath, sourceFilePath),
168
+ value: sourceCode
179
169
  });
180
170
  }
181
171
  try {
182
- const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, sourceFilePath), 'utf8');
172
+ const virtualFile = await readVirtualFile(pathUtils.join(basePath, sourceFilePath), 'utf8');
183
173
  return virtualFile;
184
- }
185
- catch (error) {
174
+ } catch (error) {
186
175
  throw new Error(`Failed to read Snap bundle file: ${error.message}`);
187
176
  }
188
177
  }
189
- exports.getSnapSourceCode = getSnapSourceCode;
190
178
  /**
191
179
  * Given an unvalidated Snap manifest, attempts to extract the location of the
192
180
  * icon and read the file.
@@ -194,44 +182,39 @@ exports.getSnapSourceCode = getSnapSourceCode;
194
182
  * @param basePath - The path to the folder with the manifest files.
195
183
  * @param manifest - The unvalidated Snap manifest file contents.
196
184
  * @returns The contents of the icon, if any.
197
- */
198
- async function getSnapIcon(basePath, manifest) {
199
- if (!(0, utils_1.isPlainObject)(manifest)) {
185
+ */ export async function getSnapIcon(basePath, manifest) {
186
+ if (!isPlainObject(manifest)) {
200
187
  return undefined;
201
188
  }
202
- const iconPath = manifest.source?.location?.npm
203
- ?.iconPath;
189
+ const iconPath = manifest.source?.location?.npm?.iconPath;
204
190
  if (!iconPath) {
205
191
  return undefined;
206
192
  }
207
193
  try {
208
- const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, iconPath), 'utf8');
194
+ const virtualFile = await readVirtualFile(pathUtils.join(basePath, iconPath), 'utf8');
209
195
  return virtualFile;
210
- }
211
- catch (error) {
196
+ } catch (error) {
212
197
  throw new Error(`Failed to read Snap icon file: ${error.message}`);
213
198
  }
214
199
  }
215
- exports.getSnapIcon = getSnapIcon;
216
200
  /**
217
201
  * Sorts the given manifest in our preferred sort order and removes the
218
202
  * `repository` field if it is falsy (it may be `null`).
219
203
  *
220
204
  * @param manifest - The manifest to sort and modify.
221
205
  * @returns The disk-ready manifest.
222
- */
223
- function getWritableManifest(manifest) {
206
+ */ export function getWritableManifest(manifest) {
224
207
  const { repository, ...remaining } = manifest;
225
- const keys = Object.keys(repository ? { ...remaining, repository } : remaining);
226
- const writableManifest = keys
227
- .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])
228
- .reduce((result, key) => ({
229
- ...result,
230
- [key]: manifest[key],
231
- }), {});
208
+ const keys = Object.keys(repository ? {
209
+ ...remaining,
210
+ repository
211
+ } : remaining);
212
+ const writableManifest = keys.sort((a, b)=>MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b]).reduce((result, key)=>({
213
+ ...result,
214
+ [key]: manifest[key]
215
+ }), {});
232
216
  return writableManifest;
233
217
  }
234
- exports.getWritableManifest = getWritableManifest;
235
218
  /**
236
219
  * Validates the fields of an npm Snap manifest that has already passed JSON
237
220
  * Schema validation.
@@ -241,8 +224,7 @@ exports.getWritableManifest = getWritableManifest;
241
224
  * @param snapFiles.packageJson - The npm Snap's `package.json`.
242
225
  * @param snapFiles.sourceCode - The Snap's source code.
243
226
  * @param snapFiles.svgIcon - The Snap's optional icon.
244
- */
245
- function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, }) {
227
+ */ export function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon }) {
246
228
  const packageJsonName = packageJson.result.name;
247
229
  const packageJsonVersion = packageJson.result.version;
248
230
  const packageJsonRepository = packageJson.result.repository;
@@ -250,19 +232,21 @@ function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, }
250
232
  const manifestPackageVersion = manifest.result.version;
251
233
  const manifestRepository = manifest.result.repository;
252
234
  if (packageJsonName !== manifestPackageName) {
253
- throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" npm package name ("${manifestPackageName}") does not match the "${types_1.NpmSnapFileNames.PackageJson}" "name" field ("${packageJsonName}").`, types_1.SnapValidationFailureReason.NameMismatch);
235
+ throw new ProgrammaticallyFixableSnapError(`"${NpmSnapFileNames.Manifest}" npm package name ("${manifestPackageName}") does not match the "${NpmSnapFileNames.PackageJson}" "name" field ("${packageJsonName}").`, SnapValidationFailureReason.NameMismatch);
254
236
  }
255
237
  if (packageJsonVersion !== manifestPackageVersion) {
256
- throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" npm package version ("${manifestPackageVersion}") does not match the "${types_1.NpmSnapFileNames.PackageJson}" "version" field ("${packageJsonVersion}").`, types_1.SnapValidationFailureReason.VersionMismatch);
238
+ throw new ProgrammaticallyFixableSnapError(`"${NpmSnapFileNames.Manifest}" npm package version ("${manifestPackageVersion}") does not match the "${NpmSnapFileNames.PackageJson}" "version" field ("${packageJsonVersion}").`, SnapValidationFailureReason.VersionMismatch);
257
239
  }
258
- if (
259
- // The repository may be `undefined` in package.json but can only be defined
240
+ if (// The repository may be `undefined` in package.json but can only be defined
260
241
  // or `null` in the Snap manifest due to TS@<4.4 issues.
261
- (packageJsonRepository || manifestRepository) &&
262
- !(0, fast_deep_equal_1.default)(packageJsonRepository, manifestRepository)) {
263
- throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" "repository" field does not match the "${types_1.NpmSnapFileNames.PackageJson}" "repository" field.`, types_1.SnapValidationFailureReason.RepositoryMismatch);
264
- }
265
- (0, snaps_1.validateSnapShasum)({ manifest, sourceCode, svgIcon }, `"${types_1.NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
242
+ (packageJsonRepository || manifestRepository) && !deepEqual(packageJsonRepository, manifestRepository)) {
243
+ throw new ProgrammaticallyFixableSnapError(`"${NpmSnapFileNames.Manifest}" "repository" field does not match the "${NpmSnapFileNames.PackageJson}" "repository" field.`, SnapValidationFailureReason.RepositoryMismatch);
244
+ }
245
+ validateSnapShasum({
246
+ manifest,
247
+ sourceCode,
248
+ svgIcon
249
+ }, `"${NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
266
250
  }
267
- exports.validateNpmSnapManifest = validateNpmSnapManifest;
251
+
268
252
  //# sourceMappingURL=manifest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/manifest/manifest.ts"],"sourcesContent":["import { Json, assertExhaustive, assert, isPlainObject } from '@metamask/utils';\nimport deepEqual from 'fast-deep-equal';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { validateNpmSnap } from '../npm';\nimport {\n getSnapChecksum,\n ProgrammaticallyFixableSnapError,\n validateSnapShasum,\n} from '../snaps';\nimport {\n NpmSnapFileNames,\n SnapFiles,\n SnapValidationFailureReason,\n UnvalidatedSnapFiles,\n} from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file';\nimport { SnapManifest } from './validation';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialPermissions: 7,\n manifestVersion: 8,\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was updated.\n * @property warnings - An array of warnings that were encountered during\n * processing of the manifest files. These warnings are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n * @property errors - An array of errors that were encountered during\n * processing of the manifest files. These errors are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n */\nexport type CheckManifestResult = {\n manifest: SnapManifest;\n updated?: boolean;\n warnings: string[];\n errors: string[];\n};\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param writeManifest - Whether to write the fixed manifest to disk.\n * @param sourceCode - The source code of the Snap.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n writeManifest = true,\n sourceCode?: string,\n): Promise<CheckManifestResult> {\n const warnings: string[] = [];\n const errors: string[] = [];\n\n let updated = false;\n\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n };\n\n let manifest: VirtualFile<SnapManifest> | undefined;\n try {\n ({ manifest } = validateNpmSnap(snapFiles));\n } catch (error) {\n if (error instanceof ProgrammaticallyFixableSnapError) {\n errors.push(error.message);\n\n // If we get here, the files at least have the correct shape.\n const partiallyValidatedFiles = snapFiles as SnapFiles;\n\n let isInvalid = true;\n let currentError = error;\n const maxAttempts = Object.keys(SnapValidationFailureReason).length;\n\n // Attempt to fix all fixable validation failure reasons. All such reasons\n // are enumerated by the `SnapValidationFailureReason` enum, so we only\n // attempt to fix the manifest the same amount of times as there are\n // reasons in the enum.\n for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {\n manifest = fixManifest(\n manifest\n ? { ...partiallyValidatedFiles, manifest }\n : partiallyValidatedFiles,\n currentError,\n );\n\n try {\n validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });\n\n isInvalid = false;\n } catch (nextValidationError) {\n currentError = nextValidationError;\n /* istanbul ignore next: this should be impossible */\n if (\n !(\n nextValidationError instanceof ProgrammaticallyFixableSnapError\n ) ||\n (attempts === maxAttempts && !isInvalid)\n ) {\n throw new Error(\n `Internal error: Failed to fix manifest. This is a bug, please report it. Reason:\\n${error.message}`,\n );\n }\n\n errors.push(currentError.message);\n }\n }\n\n updated = true;\n } else {\n throw error;\n }\n }\n\n // TypeScript assumes `manifest` can still be undefined, that is not the case.\n // But we assert to keep TypeScript happy.\n assert(manifest);\n\n const validatedManifest = manifest.result;\n\n // Check presence of recommended keys\n const recommendedFields = ['repository'] as const;\n\n const missingRecommendedFields = recommendedFields.filter(\n (key) => !validatedManifest[key],\n );\n\n if (missingRecommendedFields.length > 0) {\n warnings.push(\n `Missing recommended package.json properties:\\n${missingRecommendedFields.reduce(\n (allMissing, currentField) => {\n return `${allMissing}\\t${currentField}\\n`;\n },\n '',\n )}`,\n );\n }\n\n if (writeManifest) {\n try {\n const newManifest = `${JSON.stringify(\n getWritableManifest(validatedManifest),\n null,\n 2,\n )}\\n`;\n\n if (updated || newManifest !== manifestFile.value) {\n await fs.writeFile(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n newManifest,\n );\n }\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(`Failed to update snap.manifest.json: ${error.message}`);\n }\n }\n\n return { manifest: validatedManifest, updated, warnings, errors };\n}\n\n/**\n * Given the relevant Snap files (manifest, `package.json`, and bundle) and a\n * Snap manifest validation error, fixes the fault in the manifest that caused\n * the error.\n *\n * @param snapFiles - The contents of all Snap files.\n * @param error - The {@link ProgrammaticallyFixableSnapError} that was thrown.\n * @returns A copy of the manifest file where the cause of the error is fixed.\n */\nexport function fixManifest(\n snapFiles: SnapFiles,\n error: ProgrammaticallyFixableSnapError,\n): VirtualFile<SnapManifest> {\n const { manifest, packageJson } = snapFiles;\n const clonedFile = manifest.clone();\n const manifestCopy = clonedFile.result;\n\n switch (error.reason) {\n case SnapValidationFailureReason.NameMismatch:\n manifestCopy.source.location.npm.packageName = packageJson.result.name;\n break;\n\n case SnapValidationFailureReason.VersionMismatch:\n manifestCopy.version = packageJson.result.version;\n break;\n\n case SnapValidationFailureReason.RepositoryMismatch:\n manifestCopy.repository = packageJson.result.repository\n ? deepClone(packageJson.result.repository)\n : undefined;\n break;\n\n case SnapValidationFailureReason.ShasumMismatch:\n manifestCopy.source.shasum = getSnapChecksum(snapFiles);\n break;\n\n /* istanbul ignore next */\n default:\n assertExhaustive(error.reason);\n }\n\n clonedFile.result = manifestCopy;\n clonedFile.value = JSON.stringify(manifestCopy);\n return clonedFile;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read Snap bundle file: ${error.message}`);\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read Snap icon file: ${error.message}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n\n/**\n * Validates the fields of an npm Snap manifest that has already passed JSON\n * Schema validation.\n *\n * @param snapFiles - The relevant snap files to validate.\n * @param snapFiles.manifest - The npm Snap manifest to validate.\n * @param snapFiles.packageJson - The npm Snap's `package.json`.\n * @param snapFiles.sourceCode - The Snap's source code.\n * @param snapFiles.svgIcon - The Snap's optional icon.\n */\nexport function validateNpmSnapManifest({\n manifest,\n packageJson,\n sourceCode,\n svgIcon,\n}: SnapFiles) {\n const packageJsonName = packageJson.result.name;\n const packageJsonVersion = packageJson.result.version;\n const packageJsonRepository = packageJson.result.repository;\n\n const manifestPackageName = manifest.result.source.location.npm.packageName;\n const manifestPackageVersion = manifest.result.version;\n const manifestRepository = manifest.result.repository;\n\n if (packageJsonName !== manifestPackageName) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package name (\"${manifestPackageName}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"name\" field (\"${packageJsonName}\").`,\n SnapValidationFailureReason.NameMismatch,\n );\n }\n\n if (packageJsonVersion !== manifestPackageVersion) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package version (\"${manifestPackageVersion}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"version\" field (\"${packageJsonVersion}\").`,\n SnapValidationFailureReason.VersionMismatch,\n );\n }\n\n if (\n // The repository may be `undefined` in package.json but can only be defined\n // or `null` in the Snap manifest due to TS@<4.4 issues.\n (packageJsonRepository || manifestRepository) &&\n !deepEqual(packageJsonRepository, manifestRepository)\n ) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" \"repository\" field does not match the \"${NpmSnapFileNames.PackageJson}\" \"repository\" field.`,\n SnapValidationFailureReason.RepositoryMismatch,\n );\n }\n\n validateSnapShasum(\n { manifest, sourceCode, svgIcon },\n `\"${NpmSnapFileNames.Manifest}\" \"shasum\" field does not match computed shasum.`,\n );\n}\n"],"names":["assertExhaustive","assert","isPlainObject","deepEqual","promises","fs","pathUtils","deepClone","readJsonFile","validateNpmSnap","getSnapChecksum","ProgrammaticallyFixableSnapError","validateSnapShasum","NpmSnapFileNames","SnapValidationFailureReason","readVirtualFile","VirtualFile","MANIFEST_SORT_ORDER","$schema","version","description","proposedName","repository","source","initialPermissions","manifestVersion","checkManifest","basePath","writeManifest","sourceCode","warnings","errors","updated","manifestPath","join","Manifest","manifestFile","unvalidatedManifest","result","packageFile","PackageJson","snapFiles","manifest","packageJson","getSnapSourceCode","svgIcon","getSnapIcon","error","push","message","partiallyValidatedFiles","isInvalid","currentError","maxAttempts","Object","keys","length","attempts","fixManifest","validateNpmSnapManifest","nextValidationError","Error","validatedManifest","recommendedFields","missingRecommendedFields","filter","key","reduce","allMissing","currentField","newManifest","JSON","stringify","getWritableManifest","value","writeFile","clonedFile","clone","manifestCopy","reason","NameMismatch","location","npm","packageName","name","VersionMismatch","RepositoryMismatch","undefined","ShasumMismatch","shasum","sourceFilePath","filePath","path","virtualFile","iconPath","remaining","writableManifest","sort","a","b","packageJsonName","packageJsonVersion","packageJsonRepository","manifestPackageName","manifestPackageVersion","manifestRepository"],"mappings":"AAAA,SAAeA,gBAAgB,EAAEC,MAAM,EAAEC,aAAa,QAAQ,kBAAkB;AAChF,OAAOC,eAAe,kBAAkB;AACxC,SAASC,YAAYC,EAAE,QAAQ,KAAK;AACpC,OAAOC,eAAe,OAAO;AAE7B,SAASC,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iBsB,QAAQ,CAAC,yCAAyC,EAAEtB,iBAAiB2B,WAAW,CAAC,qBAAqB,CAAC,EAC5H1B,4BAA4BwE,kBAAkB;IAElD;IAEA1E,mBACE;QAAE8B;QAAUb;QAAYgB;IAAQ,GAChC,CAAC,CAAC,EAAEhC,iBAAiBsB,QAAQ,CAAC,gDAAgD,CAAC;AAEnF"}
@@ -0,0 +1,152 @@
1
+ import { isValidBIP32PathSegment } from '@metamask/key-tree';
2
+ import { assertStruct, ChecksumStruct, VersionStruct, isValidSemVerRange } from '@metamask/utils';
3
+ import { array, boolean, create, enums, integer, is, literal, object, optional, pattern, refine, record, size, string, type, union } from 'superstruct';
4
+ import { isEqual } from '../array';
5
+ import { CronjobSpecificationArrayStruct } from '../cronjob';
6
+ import { SIP_6_MAGIC_VALUE, STATE_ENCRYPTION_MAGIC_VALUE } from '../entropy';
7
+ import { RpcOriginsStruct } from '../json-rpc';
8
+ import { SnapIdStruct } from '../snaps';
9
+ import { NameStruct, NpmSnapFileNames } from '../types';
10
+ // BIP-43 purposes that cannot be used for entropy derivation. These are in the
11
+ // string form, ending with `'`.
12
+ const FORBIDDEN_PURPOSES = [
13
+ SIP_6_MAGIC_VALUE,
14
+ STATE_ENCRYPTION_MAGIC_VALUE
15
+ ];
16
+ export const FORBIDDEN_COIN_TYPES = [
17
+ 60
18
+ ];
19
+ const FORBIDDEN_PATHS = FORBIDDEN_COIN_TYPES.map((coinType)=>[
20
+ 'm',
21
+ "44'",
22
+ `${coinType}'`
23
+ ]);
24
+ export const Bip32PathStruct = refine(array(string()), 'BIP-32 path', (path)=>{
25
+ if (path.length === 0) {
26
+ return 'Path must be a non-empty BIP-32 derivation path array';
27
+ }
28
+ if (path[0] !== 'm') {
29
+ return 'Path must start with "m".';
30
+ }
31
+ if (path.length < 3) {
32
+ return 'Paths must have a length of at least three.';
33
+ }
34
+ if (path.slice(1).some((part)=>!isValidBIP32PathSegment(part))) {
35
+ return 'Path must be a valid BIP-32 derivation path array.';
36
+ }
37
+ if (FORBIDDEN_PURPOSES.includes(path[1])) {
38
+ return `The purpose "${path[1]}" is not allowed for entropy derivation.`;
39
+ }
40
+ if (FORBIDDEN_PATHS.some((forbiddenPath)=>isEqual(path.slice(0, forbiddenPath.length), forbiddenPath))) {
41
+ return `The path "${path.join('/')}" is not allowed for entropy derivation.`;
42
+ }
43
+ return true;
44
+ });
45
+ export const bip32entropy = (struct)=>refine(struct, 'BIP-32 entropy', (value)=>{
46
+ if (value.curve === 'ed25519' && value.path.slice(1).some((part)=>!part.endsWith("'"))) {
47
+ return 'Ed25519 does not support unhardened paths.';
48
+ }
49
+ return true;
50
+ });
51
+ // Used outside @metamask/snap-utils
52
+ export const Bip32EntropyStruct = bip32entropy(type({
53
+ path: Bip32PathStruct,
54
+ curve: enums([
55
+ 'ed25519',
56
+ 'secp256k1'
57
+ ])
58
+ }));
59
+ export const SnapGetBip32EntropyPermissionsStruct = size(array(Bip32EntropyStruct), 1, Infinity);
60
+ export const SemVerRangeStruct = refine(string(), 'SemVer range', (value)=>{
61
+ if (isValidSemVerRange(value)) {
62
+ return true;
63
+ }
64
+ return 'Expected a valid SemVer range.';
65
+ });
66
+ export const SnapIdsStruct = refine(record(SnapIdStruct, object({
67
+ version: optional(SemVerRangeStruct)
68
+ })), 'SnapIds', (value)=>{
69
+ if (Object.keys(value).length === 0) {
70
+ return false;
71
+ }
72
+ return true;
73
+ });
74
+ /* eslint-disable @typescript-eslint/naming-convention */ export const PermissionsStruct = type({
75
+ 'endowment:long-running': optional(object({})),
76
+ 'endowment:network-access': optional(object({})),
77
+ 'endowment:webassembly': optional(object({})),
78
+ 'endowment:transaction-insight': optional(object({
79
+ allowTransactionOrigin: optional(boolean())
80
+ })),
81
+ 'endowment:cronjob': optional(object({
82
+ jobs: CronjobSpecificationArrayStruct
83
+ })),
84
+ 'endowment:rpc': optional(RpcOriginsStruct),
85
+ snap_dialog: optional(object({})),
86
+ // TODO: Remove
87
+ snap_confirm: optional(object({})),
88
+ snap_manageState: optional(object({})),
89
+ snap_manageAccounts: optional(object({})),
90
+ snap_notify: optional(object({})),
91
+ snap_getBip32Entropy: optional(SnapGetBip32EntropyPermissionsStruct),
92
+ snap_getBip32PublicKey: optional(SnapGetBip32EntropyPermissionsStruct),
93
+ snap_getBip44Entropy: optional(size(array(object({
94
+ coinType: size(integer(), 0, 2 ** 32 - 1)
95
+ })), 1, Infinity)),
96
+ snap_getEntropy: optional(object({})),
97
+ wallet_snap: optional(SnapIdsStruct)
98
+ });
99
+ export const SnapManifestStruct = object({
100
+ version: VersionStruct,
101
+ description: size(string(), 1, 280),
102
+ proposedName: size(pattern(string(), /^(?:[A-Za-z0-9-_]+( [A-Za-z0-9-_]+)*)|(?:(?:@[A-Za-z0-9-*~][A-Za-z0-9-*._~]*\/)?[A-Za-z0-9-~][A-Za-z0-9-._~]*)$/u), 1, 214),
103
+ repository: optional(object({
104
+ type: size(string(), 1, Infinity),
105
+ url: size(string(), 1, Infinity)
106
+ })),
107
+ source: object({
108
+ shasum: ChecksumStruct,
109
+ location: object({
110
+ npm: object({
111
+ filePath: size(string(), 1, Infinity),
112
+ iconPath: optional(size(string(), 1, Infinity)),
113
+ packageName: NameStruct,
114
+ registry: union([
115
+ literal('https://registry.npmjs.org'),
116
+ literal('https://registry.npmjs.org/')
117
+ ])
118
+ })
119
+ })
120
+ }),
121
+ initialPermissions: PermissionsStruct,
122
+ manifestVersion: literal('0.1'),
123
+ $schema: optional(string())
124
+ });
125
+ /**
126
+ * Check if the given value is a valid {@link SnapManifest} object.
127
+ *
128
+ * @param value - The value to check.
129
+ * @returns Whether the value is a valid {@link SnapManifest} object.
130
+ */ export function isSnapManifest(value) {
131
+ return is(value, SnapManifestStruct);
132
+ }
133
+ /**
134
+ * Assert that the given value is a valid {@link SnapManifest} object.
135
+ *
136
+ * @param value - The value to check.
137
+ * @throws If the value is not a valid {@link SnapManifest} object.
138
+ */ export function assertIsSnapManifest(value) {
139
+ assertStruct(value, SnapManifestStruct, `"${NpmSnapFileNames.Manifest}" is invalid`);
140
+ }
141
+ /**
142
+ * Creates a {@link SnapManifest} object from JSON.
143
+ *
144
+ * @param value - The value to check.
145
+ * @throws If the value cannot be coerced to a {@link SnapManifest} object.
146
+ * @returns The created {@link SnapManifest} object.
147
+ */ export function createSnapManifest(value) {
148
+ // TODO: Add a utility to prefix these errors similar to assertStruct
149
+ return create(value, SnapManifestStruct);
150
+ }
151
+
152
+ //# sourceMappingURL=validation.js.map