@metamask/snaps-utils 7.1.0 → 7.3.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 (74) hide show
  1. package/CHANGELOG.md +15 -1
  2. package/dist/{chunk-YP3ZXUME.mjs → chunk-2OJY3BXD.mjs} +3 -3
  3. package/dist/{chunk-MKTAIEG7.js → chunk-4LELSKWG.js} +3 -3
  4. package/dist/chunk-7VJ2BOVU.js +15 -0
  5. package/dist/chunk-7VJ2BOVU.js.map +1 -0
  6. package/dist/{chunk-7EDRFEUH.js → chunk-D7Z3SN2N.js} +7 -7
  7. package/dist/{chunk-X44Z53IG.js → chunk-G6BVXNNZ.js} +3 -3
  8. package/dist/{chunk-I77AVJKV.mjs → chunk-L4DKSTK6.mjs} +5 -5
  9. package/dist/chunk-L4DKSTK6.mjs.map +1 -0
  10. package/dist/{chunk-7Y3WSN44.mjs → chunk-LBCPJOAV.mjs} +2 -2
  11. package/dist/{chunk-E6U344HR.mjs → chunk-PBSBESZZ.mjs} +2 -2
  12. package/dist/{chunk-LSPOXPJJ.mjs → chunk-PWLXPALG.mjs} +2 -2
  13. package/dist/{chunk-AFQY2CNY.js → chunk-R5DO7T2D.js} +3 -3
  14. package/dist/chunk-UNNEBOL4.mjs +15 -0
  15. package/dist/chunk-UNNEBOL4.mjs.map +1 -0
  16. package/dist/{chunk-LBRWAC27.js → chunk-VDJTPG6F.js} +3 -3
  17. package/dist/{chunk-WEOCOYES.mjs → chunk-WZ457PEQ.mjs} +2 -2
  18. package/dist/chunk-YACE3IOJ.mjs +262 -0
  19. package/dist/chunk-YACE3IOJ.mjs.map +1 -0
  20. package/dist/chunk-YAKDZ5LP.js +262 -0
  21. package/dist/chunk-YAKDZ5LP.js.map +1 -0
  22. package/dist/{chunk-3SOYDY4W.js → chunk-ZUSNAQJU.js} +4 -4
  23. package/dist/chunk-ZUSNAQJU.js.map +1 -0
  24. package/dist/eval.js +4 -4
  25. package/dist/eval.mjs +3 -3
  26. package/dist/fs.js +3 -3
  27. package/dist/fs.mjs +2 -2
  28. package/dist/handlers.js +2 -2
  29. package/dist/handlers.mjs +1 -1
  30. package/dist/index.executionenv.js +2 -2
  31. package/dist/index.executionenv.mjs +1 -1
  32. package/dist/index.js +18 -8
  33. package/dist/index.mjs +22 -12
  34. package/dist/json.js +4 -2
  35. package/dist/json.mjs +3 -1
  36. package/dist/localization.js +3 -3
  37. package/dist/localization.mjs +2 -2
  38. package/dist/manifest/manifest.js +5 -5
  39. package/dist/manifest/manifest.mjs +4 -4
  40. package/dist/manifest/node.js +5 -5
  41. package/dist/manifest/node.mjs +4 -4
  42. package/dist/node.js +21 -11
  43. package/dist/node.mjs +24 -14
  44. package/dist/npm.js +5 -5
  45. package/dist/npm.mjs +4 -4
  46. package/dist/tsconfig.build.tsbuildinfo +1 -1
  47. package/dist/types/handlers.d.ts +89 -89
  48. package/dist/types/json.d.ts +9 -0
  49. package/dist/types/ui.d.ts +51 -11
  50. package/dist/ui.js +10 -2
  51. package/dist/ui.mjs +13 -5
  52. package/dist/validation.js +4 -4
  53. package/dist/validation.mjs +3 -3
  54. package/package.json +3 -3
  55. package/dist/chunk-3SOYDY4W.js.map +0 -1
  56. package/dist/chunk-6CHO3LOX.mjs +0 -10
  57. package/dist/chunk-6CHO3LOX.mjs.map +0 -1
  58. package/dist/chunk-HF7HUZ5Z.js +0 -10
  59. package/dist/chunk-HF7HUZ5Z.js.map +0 -1
  60. package/dist/chunk-I77AVJKV.mjs.map +0 -1
  61. package/dist/chunk-KP4ZAWMP.mjs +0 -80
  62. package/dist/chunk-KP4ZAWMP.mjs.map +0 -1
  63. package/dist/chunk-NDIITWO4.js +0 -80
  64. package/dist/chunk-NDIITWO4.js.map +0 -1
  65. /package/dist/{chunk-YP3ZXUME.mjs.map → chunk-2OJY3BXD.mjs.map} +0 -0
  66. /package/dist/{chunk-MKTAIEG7.js.map → chunk-4LELSKWG.js.map} +0 -0
  67. /package/dist/{chunk-7EDRFEUH.js.map → chunk-D7Z3SN2N.js.map} +0 -0
  68. /package/dist/{chunk-X44Z53IG.js.map → chunk-G6BVXNNZ.js.map} +0 -0
  69. /package/dist/{chunk-7Y3WSN44.mjs.map → chunk-LBCPJOAV.mjs.map} +0 -0
  70. /package/dist/{chunk-E6U344HR.mjs.map → chunk-PBSBESZZ.mjs.map} +0 -0
  71. /package/dist/{chunk-LBRWAC27.js.map → chunk-PWLXPALG.mjs.map} +0 -0
  72. /package/dist/{chunk-AFQY2CNY.js.map → chunk-R5DO7T2D.js.map} +0 -0
  73. /package/dist/{chunk-LSPOXPJJ.mjs.map → chunk-VDJTPG6F.js.map} +0 -0
  74. /package/dist/{chunk-WEOCOYES.mjs.map → chunk-WZ457PEQ.mjs.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [7.3.0]
10
+ ### Added
11
+ - Add JSX support for custom UI ([#2258](https://github.com/MetaMask/snaps/pull/2258), [#2383](https://github.com/MetaMask/snaps/pull/2383))
12
+ - This adds utility functions for working with JSX in Snaps.
13
+
14
+ ### Changed
15
+ - Bump `@metamask/base-controller` from `5.0.1` to `5.0.2` ([#2375](https://github.com/MetaMask/snaps/pull/2375))
16
+
17
+ ## [7.2.0]
18
+ ### Added
19
+ - Add `getJsonSizeUnsafe` ([#2342](https://github.com/MetaMask/snaps/pull/2342))
20
+
9
21
  ## [7.1.0]
10
22
  ### Added
11
23
  - Add derivation path for Nimiq ([#2309](https://github.com/MetaMask/snaps/pull/2309))
@@ -213,7 +225,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
213
225
  - The version of the package no longer needs to match the version of all other
214
226
  MetaMask Snaps packages.
215
227
 
216
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.1.0...HEAD
228
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.3.0...HEAD
229
+ [7.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.2.0...@metamask/snaps-utils@7.3.0
230
+ [7.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.1.0...@metamask/snaps-utils@7.2.0
217
231
  [7.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.0.4...@metamask/snaps-utils@7.1.0
218
232
  [7.0.4]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.0.3...@metamask/snaps-utils@7.0.4
219
233
  [7.0.3]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@7.0.2...@metamask/snaps-utils@7.0.3
@@ -17,10 +17,10 @@ import {
17
17
  import {
18
18
  getValidatedLocalizationFiles,
19
19
  validateSnapManifestLocalizations
20
- } from "./chunk-WEOCOYES.mjs";
20
+ } from "./chunk-WZ457PEQ.mjs";
21
21
  import {
22
22
  readJsonFile
23
- } from "./chunk-7Y3WSN44.mjs";
23
+ } from "./chunk-LBCPJOAV.mjs";
24
24
  import {
25
25
  readVirtualFile
26
26
  } from "./chunk-IGMAXVPP.mjs";
@@ -406,4 +406,4 @@ export {
406
406
  getWritableManifest,
407
407
  validateNpmSnapManifest
408
408
  };
409
- //# sourceMappingURL=chunk-YP3ZXUME.mjs.map
409
+ //# sourceMappingURL=chunk-2OJY3BXD.mjs.map
@@ -9,13 +9,13 @@ var _chunkHVTYDKBOjs = require('./chunk-HVTYDKBO.js');
9
9
  var _chunkM3KAAQFKjs = require('./chunk-M3KAAQFK.js');
10
10
 
11
11
 
12
- var _chunkAFQY2CNYjs = require('./chunk-AFQY2CNY.js');
12
+ var _chunkR5DO7T2Djs = require('./chunk-R5DO7T2D.js');
13
13
 
14
14
  // src/validation.ts
15
15
  async function validateFetchedSnap(files) {
16
16
  _chunkE5WSD47Sjs.assertIsSnapManifest.call(void 0, files.manifest.result);
17
17
  await _chunkHVTYDKBOjs.validateSnapShasum.call(void 0, files);
18
- _chunkAFQY2CNYjs.validateSnapManifestLocalizations.call(void 0,
18
+ _chunkR5DO7T2Djs.validateSnapManifestLocalizations.call(void 0,
19
19
  files.manifest.result,
20
20
  files.localizationFiles.map((file) => file.result)
21
21
  );
@@ -27,4 +27,4 @@ async function validateFetchedSnap(files) {
27
27
 
28
28
 
29
29
  exports.validateFetchedSnap = validateFetchedSnap;
30
- //# sourceMappingURL=chunk-MKTAIEG7.js.map
30
+ //# sourceMappingURL=chunk-4LELSKWG.js.map
@@ -0,0 +1,15 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/json.ts
2
+ var _utils = require('@metamask/utils');
3
+ function parseJson(json) {
4
+ return _utils.getSafeJson.call(void 0, JSON.parse(json));
5
+ }
6
+ function getJsonSizeUnsafe(value) {
7
+ const json = JSON.stringify(value);
8
+ return new TextEncoder().encode(json).byteLength;
9
+ }
10
+
11
+
12
+
13
+
14
+ exports.parseJson = parseJson; exports.getJsonSizeUnsafe = getJsonSizeUnsafe;
15
+ //# sourceMappingURL=chunk-7VJ2BOVU.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/json.ts"],"names":[],"mappings":";AACA,SAAS,mBAAmB;AAerB,SAAS,UAAoC,MAAc;AAChE,SAAO,YAAkB,KAAK,MAAM,IAAI,CAAC;AAC3C;AAUO,SAAS,kBAAkB,OAAqB;AACrD,QAAM,OAAO,KAAK,UAAU,KAAK;AACjC,SAAO,IAAI,YAAY,EAAE,OAAO,IAAI,EAAE;AACxC","sourcesContent":["import type { Json } from '@metamask/utils';\nimport { getSafeJson } from '@metamask/utils';\n\n// TODO: Upstream this to @metamask/utils\n\n/**\n * Parse JSON safely.\n *\n * Does multiple kinds of validation and strips unwanted properties like\n * `__proto__` and `constructor`.\n *\n * @param json - A JSON string to be parsed.\n * @returns The parsed JSON object.\n * @template Type - The type of the JSON object. The type is not actually\n * checked, but it is used to infer the return type.\n */\nexport function parseJson<Type extends Json = Json>(json: string) {\n return getSafeJson<Type>(JSON.parse(json));\n}\n\n/**\n * Get the size of a JSON blob without validating that is valid JSON.\n *\n * This may sometimes be preferred over `getJsonSize` for performance reasons.\n *\n * @param value - The JSON value to get the size of.\n * @returns The size of the JSON value in bytes.\n */\nexport function getJsonSizeUnsafe(value: Json): number {\n const json = JSON.stringify(value);\n return new TextEncoder().encode(json).byteLength;\n}\n"]}
@@ -17,10 +17,10 @@ var _chunkCMOSYNZRjs = require('./chunk-CMOSYNZR.js');
17
17
 
18
18
 
19
19
 
20
- var _chunkAFQY2CNYjs = require('./chunk-AFQY2CNY.js');
20
+ var _chunkR5DO7T2Djs = require('./chunk-R5DO7T2D.js');
21
21
 
22
22
 
23
- var _chunkX44Z53IGjs = require('./chunk-X44Z53IG.js');
23
+ var _chunkG6BVXNNZjs = require('./chunk-G6BVXNNZ.js');
24
24
 
25
25
 
26
26
  var _chunkQSCKTRRUjs = require('./chunk-QSCKTRRU.js');
@@ -53,9 +53,9 @@ async function checkManifest(basePath, writeManifest = true, sourceCode, writeFi
53
53
  const errors = [];
54
54
  let updated = false;
55
55
  const manifestPath = _path2.default.join(basePath, "snap.manifest.json" /* Manifest */);
56
- const manifestFile = await _chunkX44Z53IGjs.readJsonFile.call(void 0, manifestPath);
56
+ const manifestFile = await _chunkG6BVXNNZjs.readJsonFile.call(void 0, manifestPath);
57
57
  const unvalidatedManifest = manifestFile.result;
58
- const packageFile = await _chunkX44Z53IGjs.readJsonFile.call(void 0,
58
+ const packageFile = await _chunkG6BVXNNZjs.readJsonFile.call(void 0,
59
59
  _path2.default.join(basePath, "package.json" /* PackageJson */)
60
60
  );
61
61
  const auxiliaryFilePaths = getSnapFilePaths(
@@ -374,8 +374,8 @@ async function validateNpmSnap(snapFiles, errorPrefix) {
374
374
  }
375
375
  if (localizationFiles) {
376
376
  try {
377
- _chunkAFQY2CNYjs.getValidatedLocalizationFiles.call(void 0, localizationFiles);
378
- _chunkAFQY2CNYjs.validateSnapManifestLocalizations.call(void 0,
377
+ _chunkR5DO7T2Djs.getValidatedLocalizationFiles.call(void 0, localizationFiles);
378
+ _chunkR5DO7T2Djs.validateSnapManifestLocalizations.call(void 0,
379
379
  manifest.result,
380
380
  localizationFiles.map((file) => file.result)
381
381
  );
@@ -406,4 +406,4 @@ async function validateNpmSnap(snapFiles, errorPrefix) {
406
406
 
407
407
 
408
408
  exports.EXPECTED_SNAP_FILES = EXPECTED_SNAP_FILES; exports.SnapFileNameFromKey = SnapFileNameFromKey; exports.validateNpmSnap = validateNpmSnap; exports.checkManifest = checkManifest; exports.fixManifest = fixManifest; exports.getSnapSourceCode = getSnapSourceCode; exports.getSnapIcon = getSnapIcon; exports.getSnapFilePaths = getSnapFilePaths; exports.getSnapFiles = getSnapFiles; exports.getWritableManifest = getWritableManifest; exports.validateNpmSnapManifest = validateNpmSnapManifest;
409
- //# sourceMappingURL=chunk-7EDRFEUH.js.map
409
+ //# sourceMappingURL=chunk-D7Z3SN2N.js.map
@@ -3,7 +3,7 @@
3
3
  var _chunkQSCKTRRUjs = require('./chunk-QSCKTRRU.js');
4
4
 
5
5
 
6
- var _chunkHF7HUZ5Zjs = require('./chunk-HF7HUZ5Z.js');
6
+ var _chunk7VJ2BOVUjs = require('./chunk-7VJ2BOVU.js');
7
7
 
8
8
  // src/fs.ts
9
9
  var _fs = require('fs');
@@ -47,7 +47,7 @@ async function readJsonFile(pathString) {
47
47
  }
48
48
  throw error;
49
49
  }
50
- file.result = _chunkHF7HUZ5Zjs.parseJson.call(void 0, file.toString());
50
+ file.result = _chunk7VJ2BOVUjs.parseJson.call(void 0, file.toString());
51
51
  return file;
52
52
  }
53
53
  function getOutfilePath(outDir, outFileName) {
@@ -100,4 +100,4 @@ async function useTemporaryFile(fileName, fileContents, fn) {
100
100
 
101
101
 
102
102
  exports.isDirectory = isDirectory; exports.isFile = isFile; exports.readJsonFile = readJsonFile; exports.getOutfilePath = getOutfilePath; exports.validateOutfileName = validateOutfileName; exports.validateFilePath = validateFilePath; exports.validateDirPath = validateDirPath; exports.useTemporaryFile = useTemporaryFile;
103
- //# sourceMappingURL=chunk-X44Z53IG.js.map
103
+ //# sourceMappingURL=chunk-G6BVXNNZ.js.map
@@ -1,5 +1,5 @@
1
1
  // src/handlers.ts
2
- import { SeverityLevel, ComponentStruct } from "@metamask/snaps-sdk";
2
+ import { ComponentOrElementStruct, SeverityLevel } from "@metamask/snaps-sdk";
3
3
  import {
4
4
  assign,
5
5
  literal,
@@ -77,7 +77,7 @@ var SNAP_EXPORTS = {
77
77
  },
78
78
  ["onUserInput" /* OnUserInput */]: {
79
79
  type: "onUserInput" /* OnUserInput */,
80
- required: true,
80
+ required: false,
81
81
  validator: (snapExport) => {
82
82
  return typeof snapExport === "function";
83
83
  }
@@ -95,7 +95,7 @@ var OnTransactionResponseWithIdStruct = assign(
95
95
  var OnTransactionResponseWithContentStruct = assign(
96
96
  OnTransactionSeverityResponseStruct,
97
97
  object({
98
- content: ComponentStruct
98
+ content: ComponentOrElementStruct
99
99
  })
100
100
  );
101
101
  var OnTransactionResponseStruct = nullable(
@@ -106,7 +106,7 @@ var OnTransactionResponseStruct = nullable(
106
106
  );
107
107
  var OnSignatureResponseStruct = OnTransactionResponseStruct;
108
108
  var OnHomePageResponseWithContentStruct = object({
109
- content: ComponentStruct
109
+ content: ComponentOrElementStruct
110
110
  });
111
111
  var OnHomePageResponseWithIdStruct = object({
112
112
  id: string()
@@ -149,4 +149,4 @@ export {
149
149
  DomainResolutionResponseStruct,
150
150
  OnNameLookupResponseStruct
151
151
  };
152
- //# sourceMappingURL=chunk-I77AVJKV.mjs.map
152
+ //# sourceMappingURL=chunk-L4DKSTK6.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/handlers.ts"],"sourcesContent":["import type {\n OnCronjobHandler,\n OnHomePageHandler,\n OnInstallHandler,\n OnKeyringRequestHandler,\n OnNameLookupHandler,\n OnRpcRequestHandler,\n OnSignatureHandler,\n OnTransactionHandler,\n OnUpdateHandler,\n OnUserInputHandler,\n} from '@metamask/snaps-sdk';\nimport { ComponentOrElementStruct, SeverityLevel } from '@metamask/snaps-sdk';\nimport {\n assign,\n literal,\n nullable,\n object,\n optional,\n string,\n array,\n size,\n union,\n} from 'superstruct';\n\nimport type { SnapHandler } from './handler-types';\nimport { HandlerType } from './handler-types';\n\nexport type SnapRpcHookArgs = {\n origin: string;\n handler: HandlerType;\n request: Record<string, unknown>;\n};\n\nexport const SNAP_EXPORTS = {\n [HandlerType.OnRpcRequest]: {\n type: HandlerType.OnRpcRequest,\n required: true,\n validator: (snapExport: unknown): snapExport is OnRpcRequestHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnTransaction]: {\n type: HandlerType.OnTransaction,\n required: true,\n validator: (snapExport: unknown): snapExport is OnTransactionHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnCronjob]: {\n type: HandlerType.OnCronjob,\n required: true,\n validator: (snapExport: unknown): snapExport is OnCronjobHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnNameLookup]: {\n type: HandlerType.OnNameLookup,\n required: true,\n validator: (snapExport: unknown): snapExport is OnNameLookupHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnInstall]: {\n type: HandlerType.OnInstall,\n required: false,\n validator: (snapExport: unknown): snapExport is OnInstallHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnUpdate]: {\n type: HandlerType.OnUpdate,\n required: false,\n validator: (snapExport: unknown): snapExport is OnUpdateHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnKeyringRequest]: {\n type: HandlerType.OnKeyringRequest,\n required: true,\n validator: (snapExport: unknown): snapExport is OnKeyringRequestHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnHomePage]: {\n type: HandlerType.OnHomePage,\n required: true,\n validator: (snapExport: unknown): snapExport is OnHomePageHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnSignature]: {\n type: HandlerType.OnSignature,\n required: true,\n validator: (snapExport: unknown): snapExport is OnSignatureHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnUserInput]: {\n type: HandlerType.OnUserInput,\n required: false,\n validator: (snapExport: unknown): snapExport is OnUserInputHandler => {\n return typeof snapExport === 'function';\n },\n },\n} as const;\n\nexport const OnTransactionSeverityResponseStruct = object({\n severity: optional(literal(SeverityLevel.Critical)),\n});\n\nexport const OnTransactionResponseWithIdStruct = assign(\n OnTransactionSeverityResponseStruct,\n object({\n id: string(),\n }),\n);\n\nexport const OnTransactionResponseWithContentStruct = assign(\n OnTransactionSeverityResponseStruct,\n object({\n content: ComponentOrElementStruct,\n }),\n);\n\nexport const OnTransactionResponseStruct = nullable(\n union([\n OnTransactionResponseWithContentStruct,\n OnTransactionResponseWithIdStruct,\n ]),\n);\n\nexport const OnSignatureResponseStruct = OnTransactionResponseStruct;\n\nexport const OnHomePageResponseWithContentStruct = object({\n content: ComponentOrElementStruct,\n});\n\nexport const OnHomePageResponseWithIdStruct = object({\n id: string(),\n});\n\nexport const OnHomePageResponseStruct = union([\n OnHomePageResponseWithContentStruct,\n OnHomePageResponseWithIdStruct,\n]);\n\nexport const AddressResolutionStruct = object({\n protocol: string(),\n resolvedDomain: string(),\n});\n\nexport const DomainResolutionStruct = object({\n protocol: string(),\n resolvedAddress: string(),\n});\n\nexport const AddressResolutionResponseStruct = object({\n resolvedDomains: size(array(AddressResolutionStruct), 1, Infinity),\n});\n\nexport const DomainResolutionResponseStruct = object({\n resolvedAddresses: size(array(DomainResolutionStruct), 1, Infinity),\n});\n\nexport const OnNameLookupResponseStruct = nullable(\n union([AddressResolutionResponseStruct, DomainResolutionResponseStruct]),\n);\n\n/**\n * Utility type for getting the handler function type from a handler type.\n */\nexport type HandlerFunction<Type extends SnapHandler> =\n Type['validator'] extends (snapExport: unknown) => snapExport is infer Handler\n ? Handler\n : never;\n\n/**\n * All the function-based handlers that a snap can implement.\n */\nexport type SnapFunctionExports = {\n [Key in keyof typeof SNAP_EXPORTS]?: HandlerFunction<\n (typeof SNAP_EXPORTS)[Key]\n >;\n};\n\n/**\n * All handlers that a snap can implement.\n */\nexport type SnapExports = SnapFunctionExports;\n"],"mappings":";AAYA,SAAS,0BAA0B,qBAAqB;AACxD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAWA,IAAM,eAAe;AAAA,EAC1B,kCAAyB,GAAG;AAAA,IAC1B;AAAA,IACA,UAAU;AAAA,IACV,WAAW,CAAC,eAA2D;AACrE,aAAO,OAAO,eAAe;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,oCAA0B,GAAG;AAAA,IAC3B;AAAA,IACA,UAAU;AAAA,IACV,WAAW,CAAC,eAA4D;AACtE,aAAO,OAAO,eAAe;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,4BAAsB,GAAG;AAAA,IACvB;AAAA,IACA,UAAU;AAAA,IACV,WAAW,CAAC,eAAwD;AAClE,aAAO,OAAO,eAAe;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,kCAAyB,GAAG;AAAA,IAC1B;AAAA,IACA,UAAU;AAAA,IACV,WAAW,CAAC,eAA2D;AACrE,aAAO,OAAO,eAAe;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,4BAAsB,GAAG;AAAA,IACvB;AAAA,IACA,UAAU;AAAA,IACV,WAAW,CAAC,eAAwD;AAClE,aAAO,OAAO,eAAe;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,0BAAqB,GAAG;AAAA,IACtB;AAAA,IACA,UAAU;AAAA,IACV,WAAW,CAAC,eAAuD;AACjE,aAAO,OAAO,eAAe;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,0CAA6B,GAAG;AAAA,IAC9B;AAAA,IACA,UAAU;AAAA,IACV,WAAW,CAAC,eAA+D;AACzE,aAAO,OAAO,eAAe;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,8BAAuB,GAAG;A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AO;AAAA,EACnD,mBAAmB,KAAK,MAAM,sBAAsB,GAAG,GAAG,QAAQ;AACpE,CAAC;AAEM,IAAM,6BAA6B;AAAA,EACxC,MAAM,CAAC,iCAAiC,8BAA8B,CAAC;AACzE;","names":[]}
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-IGMAXVPP.mjs";
4
4
  import {
5
5
  parseJson
6
- } from "./chunk-6CHO3LOX.mjs";
6
+ } from "./chunk-UNNEBOL4.mjs";
7
7
 
8
8
  // src/fs.ts
9
9
  import { promises as fs } from "fs";
@@ -100,4 +100,4 @@ export {
100
100
  validateDirPath,
101
101
  useTemporaryFile
102
102
  };
103
- //# sourceMappingURL=chunk-7Y3WSN44.mjs.map
103
+ //# sourceMappingURL=chunk-LBCPJOAV.mjs.map
@@ -9,7 +9,7 @@ import {
9
9
  } from "./chunk-MNCFAD4E.mjs";
10
10
  import {
11
11
  validateSnapManifestLocalizations
12
- } from "./chunk-WEOCOYES.mjs";
12
+ } from "./chunk-WZ457PEQ.mjs";
13
13
 
14
14
  // src/validation.ts
15
15
  async function validateFetchedSnap(files) {
@@ -27,4 +27,4 @@ async function validateFetchedSnap(files) {
27
27
  export {
28
28
  validateFetchedSnap
29
29
  };
30
- //# sourceMappingURL=chunk-E6U344HR.mjs.map
30
+ //# sourceMappingURL=chunk-PBSBESZZ.mjs.map
@@ -7,7 +7,7 @@ const getDirname = () => path.dirname(getFilename())
7
7
  export const __dirname = /* @__PURE__ */ getDirname()
8
8
  import {
9
9
  validateFilePath
10
- } from "./chunk-7Y3WSN44.mjs";
10
+ } from "./chunk-LBCPJOAV.mjs";
11
11
 
12
12
  // src/eval.ts
13
13
  import { assert } from "@metamask/utils";
@@ -60,4 +60,4 @@ export {
60
60
  SnapEvalError,
61
61
  evalBundle
62
62
  };
63
- //# sourceMappingURL=chunk-LSPOXPJJ.mjs.map
63
+ //# sourceMappingURL=chunk-PWLXPALG.mjs.map
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkHF7HUZ5Zjs = require('./chunk-HF7HUZ5Z.js');
3
+ var _chunk7VJ2BOVUjs = require('./chunk-7VJ2BOVU.js');
4
4
 
5
5
  // src/localization.ts
6
6
  var _snapssdk = require('@metamask/snaps-sdk');
@@ -26,7 +26,7 @@ var LocalizationFileStruct = _superstruct.object.call(void 0, {
26
26
  function getValidatedLocalizationFiles(localizationFiles) {
27
27
  for (const file of localizationFiles) {
28
28
  try {
29
- file.result = _superstruct.create.call(void 0, _chunkHF7HUZ5Zjs.parseJson.call(void 0, file.toString()), LocalizationFileStruct);
29
+ file.result = _superstruct.create.call(void 0, _chunk7VJ2BOVUjs.parseJson.call(void 0, file.toString()), LocalizationFileStruct);
30
30
  } catch (error) {
31
31
  if (error instanceof _superstruct.StructError) {
32
32
  throw new Error(
@@ -106,4 +106,4 @@ function validateSnapManifestLocalizations(snapManifest, localizationFiles) {
106
106
 
107
107
 
108
108
  exports.LOCALIZABLE_FIELDS = LOCALIZABLE_FIELDS; exports.LocalizationFileStruct = LocalizationFileStruct; exports.getValidatedLocalizationFiles = getValidatedLocalizationFiles; exports.getLocalizationFile = getLocalizationFile; exports.TRANSLATION_REGEX = TRANSLATION_REGEX; exports.translate = translate; exports.getLocalizedSnapManifest = getLocalizedSnapManifest; exports.validateSnapManifestLocalizations = validateSnapManifestLocalizations;
109
- //# sourceMappingURL=chunk-AFQY2CNY.js.map
109
+ //# sourceMappingURL=chunk-R5DO7T2D.js.map
@@ -0,0 +1,15 @@
1
+ // src/json.ts
2
+ import { getSafeJson } from "@metamask/utils";
3
+ function parseJson(json) {
4
+ return getSafeJson(JSON.parse(json));
5
+ }
6
+ function getJsonSizeUnsafe(value) {
7
+ const json = JSON.stringify(value);
8
+ return new TextEncoder().encode(json).byteLength;
9
+ }
10
+
11
+ export {
12
+ parseJson,
13
+ getJsonSizeUnsafe
14
+ };
15
+ //# sourceMappingURL=chunk-UNNEBOL4.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/json.ts"],"sourcesContent":["import type { Json } from '@metamask/utils';\nimport { getSafeJson } from '@metamask/utils';\n\n// TODO: Upstream this to @metamask/utils\n\n/**\n * Parse JSON safely.\n *\n * Does multiple kinds of validation and strips unwanted properties like\n * `__proto__` and `constructor`.\n *\n * @param json - A JSON string to be parsed.\n * @returns The parsed JSON object.\n * @template Type - The type of the JSON object. The type is not actually\n * checked, but it is used to infer the return type.\n */\nexport function parseJson<Type extends Json = Json>(json: string) {\n return getSafeJson<Type>(JSON.parse(json));\n}\n\n/**\n * Get the size of a JSON blob without validating that is valid JSON.\n *\n * This may sometimes be preferred over `getJsonSize` for performance reasons.\n *\n * @param value - The JSON value to get the size of.\n * @returns The size of the JSON value in bytes.\n */\nexport function getJsonSizeUnsafe(value: Json): number {\n const json = JSON.stringify(value);\n return new TextEncoder().encode(json).byteLength;\n}\n"],"mappings":";AACA,SAAS,mBAAmB;AAerB,SAAS,UAAoC,MAAc;AAChE,SAAO,YAAkB,KAAK,MAAM,IAAI,CAAC;AAC3C;AAUO,SAAS,kBAAkB,OAAqB;AACrD,QAAM,OAAO,KAAK,UAAU,KAAK;AACjC,SAAO,IAAI,YAAY,EAAE,OAAO,IAAI,EAAE;AACxC;","names":[]}
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkX44Z53IGjs = require('./chunk-X44Z53IG.js');
3
+ var _chunkG6BVXNNZjs = require('./chunk-G6BVXNNZ.js');
4
4
 
5
5
  // src/eval.ts
6
6
  var _utils = require('@metamask/utils');
@@ -14,7 +14,7 @@ var SnapEvalError = class extends Error {
14
14
  }
15
15
  };
16
16
  async function evalBundle(bundlePath) {
17
- await _chunkX44Z53IGjs.validateFilePath.call(void 0, bundlePath);
17
+ await _chunkG6BVXNNZjs.validateFilePath.call(void 0, bundlePath);
18
18
  return new Promise((resolve, reject) => {
19
19
  const worker = _child_process.fork.call(void 0, _path.join.call(void 0, __dirname, "eval-worker.js"), [bundlePath], {
20
20
  // To avoid printing the output of the worker to the console, we set
@@ -53,4 +53,4 @@ async function evalBundle(bundlePath) {
53
53
 
54
54
 
55
55
  exports.SnapEvalError = SnapEvalError; exports.evalBundle = evalBundle;
56
- //# sourceMappingURL=chunk-LBRWAC27.js.map
56
+ //# sourceMappingURL=chunk-VDJTPG6F.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  parseJson
3
- } from "./chunk-6CHO3LOX.mjs";
3
+ } from "./chunk-UNNEBOL4.mjs";
4
4
 
5
5
  // src/localization.ts
6
6
  import { getErrorMessage } from "@metamask/snaps-sdk";
@@ -106,4 +106,4 @@ export {
106
106
  getLocalizedSnapManifest,
107
107
  validateSnapManifestLocalizations
108
108
  };
109
- //# sourceMappingURL=chunk-WEOCOYES.mjs.map
109
+ //# sourceMappingURL=chunk-WZ457PEQ.mjs.map
@@ -0,0 +1,262 @@
1
+ // src/ui.tsx
2
+ import { NodeType } from "@metamask/snaps-sdk";
3
+ import {
4
+ Italic,
5
+ Link,
6
+ Bold,
7
+ Row,
8
+ Text,
9
+ Field,
10
+ Image,
11
+ Input,
12
+ Heading,
13
+ Form,
14
+ Divider,
15
+ Spinner,
16
+ Copyable,
17
+ Box,
18
+ Button,
19
+ Address
20
+ } from "@metamask/snaps-sdk/jsx";
21
+ import {
22
+ assert,
23
+ assertExhaustive,
24
+ AssertionError,
25
+ hasProperty,
26
+ isPlainObject
27
+ } from "@metamask/utils";
28
+ import { lexer, walkTokens } from "marked";
29
+ import { jsx } from "@metamask/snaps-sdk/jsx-runtime";
30
+ var MAX_TEXT_LENGTH = 5e4;
31
+ var ALLOWED_PROTOCOLS = ["https:", "mailto:"];
32
+ function getButtonVariant(variant) {
33
+ switch (variant) {
34
+ case "primary":
35
+ return "primary";
36
+ case "secondary":
37
+ return "destructive";
38
+ default:
39
+ return void 0;
40
+ }
41
+ }
42
+ function getChildren(elements) {
43
+ if (elements.length === 1) {
44
+ return elements[0];
45
+ }
46
+ return elements;
47
+ }
48
+ function getLinkText(token) {
49
+ if (token.tokens && token.tokens.length > 0) {
50
+ return getChildren(token.tokens.flatMap(getTextChildFromToken));
51
+ }
52
+ return token.href;
53
+ }
54
+ function getTextChildFromTokens(tokens) {
55
+ return getChildren(tokens.flatMap(getTextChildFromToken));
56
+ }
57
+ function getTextChildFromToken(token) {
58
+ switch (token.type) {
59
+ case "link": {
60
+ return /* @__PURE__ */ jsx(Link, { href: token.href, children: getLinkText(token) });
61
+ }
62
+ case "text":
63
+ return token.text;
64
+ case "strong":
65
+ return /* @__PURE__ */ jsx(Bold, { children: getTextChildFromTokens(
66
+ // Due to the way `marked` is typed, `token.tokens` can be
67
+ // `undefined`, but it's a required field of `Tokens.Bold`, so we
68
+ // can safely cast it to `Token[]`.
69
+ token.tokens
70
+ ) });
71
+ case "em":
72
+ return /* @__PURE__ */ jsx(Italic, { children: getTextChildFromTokens(
73
+ // Due to the way `marked` is typed, `token.tokens` can be
74
+ // `undefined`, but it's a required field of `Tokens.Bold`, so we
75
+ // can safely cast it to `Token[]`.
76
+ token.tokens
77
+ ) });
78
+ default:
79
+ return null;
80
+ }
81
+ }
82
+ function getTextChildren(value) {
83
+ const rootTokens = lexer(value, { gfm: false });
84
+ const children = [];
85
+ walkTokens(rootTokens, (token) => {
86
+ if (token.type === "paragraph") {
87
+ if (children.length > 0) {
88
+ children.push("\n\n");
89
+ }
90
+ const { tokens } = token;
91
+ children.push(...tokens.flatMap(getTextChildFromToken));
92
+ }
93
+ });
94
+ return children.filter((child) => child !== null);
95
+ }
96
+ function validateComponentTextSize(component) {
97
+ const textSize = getTotalTextLength(component);
98
+ assert(
99
+ textSize <= MAX_TEXT_LENGTH,
100
+ `The text in a Snap UI may not be larger than ${MAX_TEXT_LENGTH / 1e3} kB.`
101
+ );
102
+ }
103
+ function getJsxElementFromComponent(legacyComponent) {
104
+ validateComponentTextSize(legacyComponent);
105
+ function getElement(component) {
106
+ switch (component.type) {
107
+ case NodeType.Address:
108
+ return /* @__PURE__ */ jsx(Address, { address: component.value });
109
+ case NodeType.Button:
110
+ return /* @__PURE__ */ jsx(
111
+ Button,
112
+ {
113
+ name: component.name,
114
+ variant: getButtonVariant(component.variant),
115
+ type: component.buttonType,
116
+ children: component.value
117
+ }
118
+ );
119
+ case NodeType.Copyable:
120
+ return /* @__PURE__ */ jsx(Copyable, { value: component.value, sensitive: component.sensitive });
121
+ case NodeType.Divider:
122
+ return /* @__PURE__ */ jsx(Divider, {});
123
+ case NodeType.Form:
124
+ return /* @__PURE__ */ jsx(Form, { name: component.name, children: getChildren(component.children.map(getElement)) });
125
+ case NodeType.Heading:
126
+ return /* @__PURE__ */ jsx(Heading, { children: component.value });
127
+ case NodeType.Image:
128
+ return /* @__PURE__ */ jsx(Image, { src: component.value });
129
+ case NodeType.Input:
130
+ return /* @__PURE__ */ jsx(Field, { label: component.label, error: component.error, children: /* @__PURE__ */ jsx(
131
+ Input,
132
+ {
133
+ name: component.name,
134
+ type: component.inputType,
135
+ value: component.value,
136
+ placeholder: component.placeholder
137
+ }
138
+ ) });
139
+ case NodeType.Panel:
140
+ return /* @__PURE__ */ jsx(Box, { children: getChildren(component.children.map(getElement)) });
141
+ case NodeType.Row:
142
+ return /* @__PURE__ */ jsx(Row, { label: component.label, children: getElement(component.value) });
143
+ case NodeType.Spinner:
144
+ return /* @__PURE__ */ jsx(Spinner, {});
145
+ case NodeType.Text:
146
+ return /* @__PURE__ */ jsx(Text, { children: getChildren(getTextChildren(component.value)) });
147
+ default:
148
+ return assertExhaustive(component);
149
+ }
150
+ }
151
+ return getElement(legacyComponent);
152
+ }
153
+ function getMarkdownLinks(text) {
154
+ const tokens = lexer(text, { gfm: false });
155
+ const links = [];
156
+ walkTokens(tokens, (token) => {
157
+ if (token.type === "link") {
158
+ links.push(token);
159
+ }
160
+ });
161
+ return links;
162
+ }
163
+ function validateLink(link, isOnPhishingList) {
164
+ try {
165
+ const url = new URL(link);
166
+ assert(
167
+ ALLOWED_PROTOCOLS.includes(url.protocol),
168
+ `Protocol must be one of: ${ALLOWED_PROTOCOLS.join(", ")}.`
169
+ );
170
+ const hostname = url.protocol === "mailto:" ? url.pathname.split("@")[1] : url.hostname;
171
+ assert(!isOnPhishingList(hostname), "The specified URL is not allowed.");
172
+ } catch (error) {
173
+ throw new Error(
174
+ `Invalid URL: ${error instanceof AssertionError ? error.message : "Unable to parse URL."}`
175
+ );
176
+ }
177
+ }
178
+ function validateTextLinks(text, isOnPhishingList) {
179
+ const links = getMarkdownLinks(text);
180
+ for (const link of links) {
181
+ validateLink(link.href, isOnPhishingList);
182
+ }
183
+ }
184
+ function validateJsxLinks(node, isOnPhishingList) {
185
+ walkJsx(node, (childNode) => {
186
+ if (childNode.type !== "Link") {
187
+ return;
188
+ }
189
+ validateLink(childNode.props.href, isOnPhishingList);
190
+ });
191
+ }
192
+ function getTotalTextLength(component) {
193
+ const { type } = component;
194
+ switch (type) {
195
+ case NodeType.Panel:
196
+ return component.children.reduce(
197
+ // This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313
198
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
199
+ (sum, node) => sum + getTotalTextLength(node),
200
+ 0
201
+ );
202
+ case NodeType.Row:
203
+ return getTotalTextLength(component.value);
204
+ case NodeType.Text:
205
+ return component.value.length;
206
+ default:
207
+ return 0;
208
+ }
209
+ }
210
+ function hasChildren(element) {
211
+ return hasProperty(element.props, "children");
212
+ }
213
+ function getJsxChildren(element) {
214
+ if (hasChildren(element)) {
215
+ if (Array.isArray(element.props.children)) {
216
+ return element.props.children.filter(Boolean);
217
+ }
218
+ if (element.props.children) {
219
+ return [element.props.children];
220
+ }
221
+ }
222
+ return [];
223
+ }
224
+ function walkJsx(node, callback) {
225
+ if (Array.isArray(node)) {
226
+ for (const child of node) {
227
+ const childResult = walkJsx(child, callback);
228
+ if (childResult !== void 0) {
229
+ return childResult;
230
+ }
231
+ }
232
+ return void 0;
233
+ }
234
+ const result = callback(node);
235
+ if (result !== void 0) {
236
+ return result;
237
+ }
238
+ if (hasProperty(node, "props") && isPlainObject(node.props) && hasProperty(node.props, "children")) {
239
+ const children = getJsxChildren(node);
240
+ for (const child of children) {
241
+ if (isPlainObject(child)) {
242
+ const childResult = walkJsx(child, callback);
243
+ if (childResult !== void 0) {
244
+ return childResult;
245
+ }
246
+ }
247
+ }
248
+ }
249
+ return void 0;
250
+ }
251
+
252
+ export {
253
+ getTextChildren,
254
+ getJsxElementFromComponent,
255
+ validateTextLinks,
256
+ validateJsxLinks,
257
+ getTotalTextLength,
258
+ hasChildren,
259
+ getJsxChildren,
260
+ walkJsx
261
+ };
262
+ //# sourceMappingURL=chunk-YACE3IOJ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/ui.tsx"],"sourcesContent":["import type { Component } from '@metamask/snaps-sdk';\nimport { NodeType } from '@metamask/snaps-sdk';\nimport type {\n BoldChildren,\n FieldElement,\n ItalicChildren,\n JSXElement,\n MaybeArray,\n RowChildren,\n TextChildren,\n} from '@metamask/snaps-sdk/jsx';\nimport {\n Italic,\n Link,\n Bold,\n Row,\n Text,\n Field,\n Image,\n Input,\n Heading,\n Form,\n Divider,\n Spinner,\n Copyable,\n Box,\n Button,\n Address,\n} from '@metamask/snaps-sdk/jsx';\nimport {\n assert,\n assertExhaustive,\n AssertionError,\n hasProperty,\n isPlainObject,\n} from '@metamask/utils';\nimport { lexer, walkTokens } from 'marked';\nimport type { Token, Tokens } from 'marked';\n\nconst MAX_TEXT_LENGTH = 50_000; // 50 kb\nconst ALLOWED_PROTOCOLS = ['https:', 'mailto:'];\n\n/**\n * Get the button variant from a legacy button component variant.\n *\n * @param variant - The legacy button component variant.\n * @returns The button variant.\n */\nfunction getButtonVariant(variant?: 'primary' | 'secondary' | undefined) {\n switch (variant) {\n case 'primary':\n return 'primary';\n case 'secondary':\n return 'destructive';\n default:\n return undefined;\n }\n}\n\n/**\n * Get the children of a JSX element. If there is only one child, the child is\n * returned directly. Otherwise, the children are returned as an array.\n *\n * @param elements - The JSX elements.\n * @returns The child or children.\n */\nfunction getChildren<Type>(elements: Type[]) {\n if (elements.length === 1) {\n return elements[0];\n }\n\n return elements;\n}\n\n/**\n * Get the text of a link token.\n *\n * @param token - The link token.\n * @returns The text of the link token.\n */\nfunction getLinkText(token: Tokens.Link | Tokens.Generic) {\n if (token.tokens && token.tokens.length > 0) {\n return getChildren(token.tokens.flatMap(getTextChildFromToken));\n }\n\n return token.href;\n}\n\n/**\n * Get the text child from a list of markdown tokens.\n *\n * @param tokens - The markdown tokens.\n * @returns The text child.\n */\nfunction getTextChildFromTokens(tokens: Token[]) {\n return getChildren(tokens.flatMap(getTextChildFromToken));\n}\n\n/**\n * Get the text child from a markdown token.\n *\n * @param token - The markdown token.\n * @returns The text child.\n */\nfunction getTextChildFromToken(token: Token): TextChildren {\n switch (token.type) {\n case 'link': {\n return <Link href={token.href} children={getLinkText(token)} />;\n }\n\n case 'text':\n return token.text;\n\n case 'strong':\n return (\n <Bold>\n {\n getTextChildFromTokens(\n // Due to the way `marked` is typed, `token.tokens` can be\n // `undefined`, but it's a required field of `Tokens.Bold`, so we\n // can safely cast it to `Token[]`.\n token.tokens as Token[],\n ) as BoldChildren\n }\n </Bold>\n );\n\n case 'em':\n return (\n <Italic>\n {\n getTextChildFromTokens(\n // Due to the way `marked` is typed, `token.tokens` can be\n // `undefined`, but it's a required field of `Tokens.Bold`, so we\n // can safely cast it to `Token[]`.\n token.tokens as Token[],\n ) as ItalicChildren\n }\n </Italic>\n );\n\n default:\n return null;\n }\n}\n\n/**\n * Get all text children from a markdown string.\n *\n * @param value - The markdown string.\n * @returns The text children.\n */\nexport function getTextChildren(value: string) {\n const rootTokens = lexer(value, { gfm: false });\n const children: TextChildren = [];\n\n walkTokens(rootTokens, (token) => {\n if (token.type === 'paragraph') {\n if (children.length > 0) {\n children.push('\\n\\n');\n }\n\n const { tokens } = token as Tokens.Paragraph;\n children.push(...tokens.flatMap(getTextChildFromToken));\n }\n });\n\n return children.filter((child) => child !== null);\n}\n\n/**\n * Validate the text size of a component. The text size is the total length of\n * all text in the component.\n *\n * @param component - The component to validate.\n * @throws An error if the text size exceeds the maximum allowed size.\n */\nfunction validateComponentTextSize(component: Component) {\n const textSize = getTotalTextLength(component);\n assert(\n textSize <= MAX_TEXT_LENGTH,\n `The text in a Snap UI may not be larger than ${\n MAX_TEXT_LENGTH / 1000\n } kB.`,\n );\n}\n\n/**\n * Get a JSX element from a legacy UI component. This supports all legacy UI\n * components, and maps them to their JSX equivalents where possible.\n *\n * This function validates the text size of the component, but does not validate\n * the total size. The total size of the component should be validated before\n * calling this function.\n *\n * @param legacyComponent - The legacy UI component.\n * @returns The JSX element.\n */\nexport function getJsxElementFromComponent(\n legacyComponent: Component,\n): JSXElement {\n validateComponentTextSize(legacyComponent);\n\n /**\n * Get the JSX element for a component. This function is recursive and will\n * call itself for child components.\n *\n * @param component - The component to convert to a JSX element.\n * @returns The JSX element.\n */\n function getElement(component: Component) {\n switch (component.type) {\n case NodeType.Address:\n return <Address address={component.value} />;\n\n case NodeType.Button:\n return (\n <Button\n name={component.name}\n variant={getButtonVariant(component.variant)}\n type={component.buttonType}\n >\n {component.value}\n </Button>\n );\n\n case NodeType.Copyable:\n return (\n <Copyable value={component.value} sensitive={component.sensitive} />\n );\n\n case NodeType.Divider:\n return <Divider />;\n\n case NodeType.Form:\n return (\n <Form name={component.name}>\n {getChildren(component.children.map(getElement)) as FieldElement[]}\n </Form>\n );\n\n case NodeType.Heading:\n return <Heading children={component.value} />;\n\n case NodeType.Image:\n // `Image` supports `alt`, but the legacy `Image` component does not.\n return <Image src={component.value} />;\n\n case NodeType.Input:\n return (\n <Field label={component.label} error={component.error}>\n <Input\n name={component.name}\n type={component.inputType}\n value={component.value}\n placeholder={component.placeholder}\n />\n </Field>\n );\n\n case NodeType.Panel:\n // `Panel` is renamed to `Box` in JSX.\n return (\n <Box children={getChildren(component.children.map(getElement))} />\n );\n\n case NodeType.Row:\n return (\n <Row label={component.label}>\n {getElement(component.value) as RowChildren}\n </Row>\n );\n\n case NodeType.Spinner:\n return <Spinner />;\n\n case NodeType.Text:\n return <Text>{getChildren(getTextChildren(component.value))}</Text>;\n\n /* istanbul ignore next 2 */\n default:\n return assertExhaustive(component);\n }\n }\n\n return getElement(legacyComponent);\n}\n\n/**\n * Extract all links from a Markdown text string using the `marked` lexer.\n *\n * @param text - The markdown text string.\n * @returns A list of URLs linked to in the string.\n */\nfunction getMarkdownLinks(text: string) {\n const tokens = lexer(text, { gfm: false });\n const links: Tokens.Link[] = [];\n\n // Walk the lexed tokens and collect all link tokens\n walkTokens(tokens, (token) => {\n if (token.type === 'link') {\n links.push(token as Tokens.Link);\n }\n });\n\n return links;\n}\n\n/**\n * Validate a link against the phishing list.\n *\n * @param link - The link to validate.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n */\nfunction validateLink(\n link: string,\n isOnPhishingList: (url: string) => boolean,\n) {\n try {\n const url = new URL(link);\n assert(\n ALLOWED_PROTOCOLS.includes(url.protocol),\n `Protocol must be one of: ${ALLOWED_PROTOCOLS.join(', ')}.`,\n );\n\n const hostname =\n url.protocol === 'mailto:' ? url.pathname.split('@')[1] : url.hostname;\n\n assert(!isOnPhishingList(hostname), 'The specified URL is not allowed.');\n } catch (error) {\n throw new Error(\n `Invalid URL: ${\n error instanceof AssertionError ? error.message : 'Unable to parse URL.'\n }`,\n );\n }\n}\n\n/**\n * Search for Markdown links in a string and checks them against the phishing\n * list.\n *\n * @param text - The text to verify.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n * @throws If the text contains a link that is not allowed.\n */\nexport function validateTextLinks(\n text: string,\n isOnPhishingList: (url: string) => boolean,\n) {\n const links = getMarkdownLinks(text);\n\n for (const link of links) {\n validateLink(link.href, isOnPhishingList);\n }\n}\n\n/**\n * Walk a JSX tree and validate each {@link LinkElement} node against the\n * phishing list.\n *\n * @param node - The JSX node to walk.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n */\nexport function validateJsxLinks(\n node: JSXElement,\n isOnPhishingList: (url: string) => boolean,\n) {\n walkJsx(node, (childNode) => {\n if (childNode.type !== 'Link') {\n return;\n }\n\n validateLink(childNode.props.href, isOnPhishingList);\n });\n}\n\n/**\n * Calculate the total length of all text in the component.\n *\n * @param component - A custom UI component.\n * @returns The total length of all text components in the component.\n */\nexport function getTotalTextLength(component: Component): number {\n const { type } = component;\n\n switch (type) {\n case NodeType.Panel:\n return component.children.reduce<number>(\n // This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n (sum, node) => sum + getTotalTextLength(node),\n 0,\n );\n\n case NodeType.Row:\n return getTotalTextLength(component.value);\n\n case NodeType.Text:\n return component.value.length;\n\n default:\n return 0;\n }\n}\n\n/**\n * Check if a JSX element has children.\n *\n * @param element - A JSX element.\n * @returns `true` if the element has children, `false` otherwise.\n */\nexport function hasChildren<Element extends JSXElement>(\n element: Element,\n): element is Element & {\n props: { children: MaybeArray<JSXElement | string> };\n} {\n return hasProperty(element.props, 'children');\n}\n\n/**\n * Get the children of a JSX element as an array. If the element has only one\n * child, the child is returned as an array.\n *\n * @param element - A JSX element.\n * @returns The children of the element.\n */\nexport function getJsxChildren(element: JSXElement): (JSXElement | string)[] {\n if (hasChildren(element)) {\n if (Array.isArray(element.props.children)) {\n // @ts-expect-error - Each member of the union type has signatures, but\n // none of those signatures are compatible with each other.\n return element.props.children.filter(Boolean);\n }\n\n if (element.props.children) {\n return [element.props.children];\n }\n }\n\n return [];\n}\n\n/**\n * Walk a JSX tree and call a callback on each node.\n *\n * @param node - The JSX node to walk.\n * @param callback - The callback to call on each node.\n * @returns The result of the callback, if any.\n */\nexport function walkJsx<Value>(\n node: JSXElement | JSXElement[],\n callback: (node: JSXElement) => Value | undefined,\n): Value | undefined {\n if (Array.isArray(node)) {\n for (const child of node) {\n const childResult = walkJsx(child as JSXElement, callback);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n\n return undefined;\n }\n\n const result = callback(node);\n if (result !== undefined) {\n return result;\n }\n\n if (\n hasProperty(node, 'props') &&\n isPlainObject(node.props) &&\n hasProperty(node.props, 'children')\n ) {\n const children = getJsxChildren(node);\n for (const child of children) {\n if (isPlainObject(child)) {\n const childResult = walkJsx(child, callback);\n if (childResult !== undefined) {\n return childResult;\n }\n }\n }\n }\n\n return undefined;\n}\n"],"mappings":";AACA,SAAS,gBAAgB;AAUzB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,OAAO,kBAAkB;AAuErB;AApEb,IAAM,kBAAkB;AACxB,IAAM,oBAAoB,CAAC,UAAU,SAAS;AAQ9C,SAAS,iBAAiB,SAA+C;AACvE,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT;AACE,aAAO;AAAA,EACX;AACF;AASA,SAAS,YAAkB,UAAkB;AAC3C,MAAI,SAAS,WAAW,GAAG;AACzB,WAAO,SAAS,CAAC;AAAA,EACnB;AAEA,SAAO;AACT;AAQA,SAAS,YAAY,OAAqC;AACxD,MAAI,MAAM,UAAU,MAAM,OAAO,SAAS,GAAG;AAC3C,WAAO,YAAY,MAAM,OAAO,QAAQ,qBAAqB,CAAC;AAAA,EAChE;AAEA,SAAO,MAAM;AACf;AAQA,SAAS,uBAAuB,QAAiB;AAC/C,SAAO,YAAY,OAAO,QAAQ,qBAAqB,CAAC;AAC1D;AAQA,SAAS,sBAAsB,OAA4B;AACzD,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK,QAAQ;AACX,aAAO,oBAAC,QA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