@metamask/snaps-utils 3.2.0 → 3.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 (51) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/dist/cjs/handler-types.js +1 -0
  3. package/dist/cjs/handler-types.js.map +1 -1
  4. package/dist/cjs/handlers.js +13 -0
  5. package/dist/cjs/handlers.js.map +1 -1
  6. package/dist/cjs/index.browser.js +1 -0
  7. package/dist/cjs/index.browser.js.map +1 -1
  8. package/dist/cjs/index.js +1 -7
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/localization.js +113 -0
  11. package/dist/cjs/localization.js.map +1 -0
  12. package/dist/cjs/manifest/manifest.js +23 -9
  13. package/dist/cjs/manifest/manifest.js.map +1 -1
  14. package/dist/cjs/manifest/validation.js +3 -2
  15. package/dist/cjs/manifest/validation.js.map +1 -1
  16. package/dist/cjs/npm.js +16 -3
  17. package/dist/cjs/npm.js.map +1 -1
  18. package/dist/cjs/snaps.js.map +1 -1
  19. package/dist/cjs/types.js.map +1 -1
  20. package/dist/cjs/validation.js +2 -0
  21. package/dist/cjs/validation.js.map +1 -1
  22. package/dist/esm/handler-types.js +1 -0
  23. package/dist/esm/handler-types.js.map +1 -1
  24. package/dist/esm/handlers.js +10 -0
  25. package/dist/esm/handlers.js.map +1 -1
  26. package/dist/esm/index.browser.js +1 -0
  27. package/dist/esm/index.browser.js.map +1 -1
  28. package/dist/esm/index.js +1 -1
  29. package/dist/esm/index.js.map +1 -1
  30. package/dist/esm/localization.js +115 -0
  31. package/dist/esm/localization.js.map +1 -0
  32. package/dist/esm/manifest/manifest.js +30 -12
  33. package/dist/esm/manifest/manifest.js.map +1 -1
  34. package/dist/esm/manifest/validation.js +4 -3
  35. package/dist/esm/manifest/validation.js.map +1 -1
  36. package/dist/esm/npm.js +16 -3
  37. package/dist/esm/npm.js.map +1 -1
  38. package/dist/esm/snaps.js.map +1 -1
  39. package/dist/esm/types.js.map +1 -1
  40. package/dist/esm/validation.js +2 -0
  41. package/dist/esm/validation.js.map +1 -1
  42. package/dist/types/handler-types.d.ts +2 -1
  43. package/dist/types/handlers.d.ts +46 -0
  44. package/dist/types/index.browser.d.ts +1 -0
  45. package/dist/types/index.d.ts +1 -1
  46. package/dist/types/localization.d.ts +143 -0
  47. package/dist/types/manifest/manifest.d.ts +15 -6
  48. package/dist/types/manifest/validation.d.ts +3 -0
  49. package/dist/types/snaps.d.ts +5 -0
  50. package/dist/types/types.d.ts +4 -1
  51. package/package.json +1 -1
@@ -0,0 +1,143 @@
1
+ import type { Infer } from 'superstruct';
2
+ import type { SnapManifest } from './manifest';
3
+ import type { VirtualFile } from './virtual-file';
4
+ export declare const LOCALIZABLE_FIELDS: readonly ["description", "proposedName"];
5
+ export declare const LocalizationFileStruct: import("superstruct").Struct<{
6
+ locale: string;
7
+ messages: Record<string, {
8
+ message: string;
9
+ description?: string | undefined;
10
+ }>;
11
+ }, {
12
+ locale: import("superstruct").Struct<string, null>;
13
+ messages: import("superstruct").Struct<Record<string, {
14
+ message: string;
15
+ description?: string | undefined;
16
+ }>, null>;
17
+ }>;
18
+ export declare type LocalizationFile = Infer<typeof LocalizationFileStruct>;
19
+ /**
20
+ * Validate a list of localization files.
21
+ *
22
+ * @param localizationFiles - The localization files to validate.
23
+ * @returns The validated localization files.
24
+ * @throws If any of the files are considered invalid.
25
+ */
26
+ export declare function getValidatedLocalizationFiles(localizationFiles: VirtualFile[]): VirtualFile<LocalizationFile>[];
27
+ /**
28
+ * Get the localization file for a given locale. If the locale is not found,
29
+ * the English localization file will be returned.
30
+ *
31
+ * @param locale - The locale to use.
32
+ * @param localizationFiles - The localization files to use.
33
+ * @returns The localization file, or `undefined` if no localization file was
34
+ * found.
35
+ */
36
+ export declare function getLocalizationFile(locale: string, localizationFiles: LocalizationFile[]): {
37
+ locale: string;
38
+ messages: Record<string, {
39
+ message: string;
40
+ description?: string | undefined;
41
+ }>;
42
+ } | undefined;
43
+ export declare const TRANSLATION_REGEX: RegExp;
44
+ /**
45
+ * Translate a string using a localization file. This will replace all instances
46
+ * of `{{key}}` with the localized version of `key`.
47
+ *
48
+ * @param value - The string to translate.
49
+ * @param file - The localization file to use, or `undefined` if no localization
50
+ * file was found.
51
+ * @returns The translated string.
52
+ * @throws If the string contains a key that is not present in the localization
53
+ * file, or if no localization file was found.
54
+ */
55
+ export declare function translate(value: string, file: LocalizationFile | undefined): string;
56
+ /**
57
+ * Get the localized Snap manifest for a given locale. This will replace all
58
+ * localized strings in the manifest with the localized version.
59
+ *
60
+ * @param snapManifest - The Snap manifest to localize.
61
+ * @param locale - The locale to use.
62
+ * @param localizationFiles - The localization files to use.
63
+ * @returns The localized Snap manifest.
64
+ */
65
+ export declare function getLocalizedSnapManifest(snapManifest: SnapManifest, locale: string, localizationFiles: LocalizationFile[]): {
66
+ description: string;
67
+ source: {
68
+ location: {
69
+ npm: {
70
+ registry: "https://registry.npmjs.org" | "https://registry.npmjs.org/";
71
+ filePath: string;
72
+ packageName: string;
73
+ iconPath?: string | undefined;
74
+ };
75
+ };
76
+ shasum: string;
77
+ files?: string[] | undefined;
78
+ locales?: string[] | undefined;
79
+ };
80
+ version: import("@metamask/utils").SemVerVersion;
81
+ proposedName: string;
82
+ initialPermissions: {
83
+ 'endowment:network-access'?: {} | undefined;
84
+ 'endowment:webassembly'?: {} | undefined;
85
+ 'endowment:transaction-insight'?: {
86
+ allowTransactionOrigin?: boolean | undefined;
87
+ } | undefined;
88
+ 'endowment:cronjob'?: {
89
+ jobs: {
90
+ request: {
91
+ method: string;
92
+ params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
93
+ id?: string | number | null | undefined;
94
+ jsonrpc?: "2.0" | undefined;
95
+ };
96
+ expression: string;
97
+ }[];
98
+ } | undefined;
99
+ 'endowment:rpc'?: {
100
+ dapps?: boolean | undefined;
101
+ snaps?: boolean | undefined;
102
+ allowedOrigins?: string[] | undefined;
103
+ } | undefined;
104
+ 'endowment:name-lookup'?: string[] | undefined;
105
+ 'endowment:keyring'?: {
106
+ allowedOrigins?: string[] | undefined;
107
+ } | undefined;
108
+ snap_dialog?: {} | undefined;
109
+ snap_confirm?: {} | undefined;
110
+ snap_manageState?: {} | undefined;
111
+ snap_manageAccounts?: {} | undefined;
112
+ snap_notify?: {} | undefined;
113
+ snap_getBip32Entropy?: {
114
+ path: string[];
115
+ curve: "ed25519" | "secp256k1";
116
+ }[] | undefined;
117
+ snap_getBip32PublicKey?: {
118
+ path: string[];
119
+ curve: "ed25519" | "secp256k1";
120
+ }[] | undefined;
121
+ snap_getBip44Entropy?: {
122
+ coinType: number;
123
+ }[] | undefined;
124
+ snap_getEntropy?: {} | undefined;
125
+ wallet_snap?: Record<string, {
126
+ version?: string | undefined;
127
+ }> | undefined;
128
+ };
129
+ manifestVersion: "0.1";
130
+ repository?: {
131
+ type: string;
132
+ url: string;
133
+ } | undefined;
134
+ $schema?: string | undefined;
135
+ };
136
+ /**
137
+ * Validate the localization files for a Snap manifest.
138
+ *
139
+ * @param snapManifest - The Snap manifest to validate.
140
+ * @param localizationFiles - The localization files to validate.
141
+ * @throws If the manifest cannot be localized.
142
+ */
143
+ export declare function validateSnapManifestLocalizations(snapManifest: SnapManifest, localizationFiles: LocalizationFile[]): void;
@@ -67,14 +67,22 @@ export declare function getSnapSourceCode(basePath: string, manifest: Json, sour
67
67
  */
68
68
  export declare function getSnapIcon(basePath: string, manifest: Json): Promise<VirtualFile | undefined>;
69
69
  /**
70
- * Given an unvalidated Snap manifest, attempts to extract the auxiliary files
71
- * and read them.
70
+ * Get an array of paths from an unvalidated Snap manifest.
72
71
  *
73
- * @param basePath - The path to the folder with the manifest files.
74
72
  * @param manifest - The unvalidated Snap manifest file contents.
73
+ * @param selector - A function that returns the paths to the files.
74
+ * @returns The paths to the files, if any.
75
+ */
76
+ export declare function getSnapFilePaths(manifest: Json, selector: (manifest: Partial<SnapManifest>) => string[] | undefined): string[] | undefined;
77
+ /**
78
+ * Given an unvalidated Snap manifest, attempts to extract the files with the
79
+ * given paths and read them.
80
+ *
81
+ * @param basePath - The path to the folder with the manifest files.
82
+ * @param paths - The paths to the files.
75
83
  * @returns A list of auxiliary files and their contents, if any.
76
84
  */
77
- export declare function getSnapAuxiliaryFiles(basePath: string, manifest: Json): Promise<VirtualFile[] | undefined>;
85
+ export declare function getSnapFiles(basePath: string, paths: string[] | undefined): Promise<VirtualFile[] | undefined>;
78
86
  /**
79
87
  * Sorts the given manifest in our preferred sort order and removes the
80
88
  * `repository` field if it is falsy (it may be `null`).
@@ -84,7 +92,7 @@ export declare function getSnapAuxiliaryFiles(basePath: string, manifest: Json):
84
92
  */
85
93
  export declare function getWritableManifest(manifest: SnapManifest): SnapManifest;
86
94
  /**
87
- * Validates the fields of an npm Snap manifest that has already passed JSON
95
+ * Validates the fields of an NPM Snap manifest that has already passed JSON
88
96
  * Schema validation.
89
97
  *
90
98
  * @param snapFiles - The relevant snap files to validate.
@@ -93,5 +101,6 @@ export declare function getWritableManifest(manifest: SnapManifest): SnapManifes
93
101
  * @param snapFiles.sourceCode - The Snap's source code.
94
102
  * @param snapFiles.svgIcon - The Snap's optional icon.
95
103
  * @param snapFiles.auxiliaryFiles - Any auxiliary files required by the snap at runtime.
104
+ * @param snapFiles.localizationFiles - The Snap's localization files.
96
105
  */
97
- export declare function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, auxiliaryFiles, }: SnapFiles): void;
106
+ export declare function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, auxiliaryFiles, localizationFiles, }: SnapFiles): void;
@@ -204,6 +204,7 @@ export declare const SnapManifestStruct: Struct<{
204
204
  };
205
205
  shasum: string;
206
206
  files?: string[] | undefined;
207
+ locales?: string[] | undefined;
207
208
  };
208
209
  version: import("@metamask/utils").SemVerVersion;
209
210
  proposedName: string;
@@ -282,6 +283,7 @@ export declare const SnapManifestStruct: Struct<{
282
283
  };
283
284
  shasum: string;
284
285
  files?: string[] | undefined;
286
+ locales?: string[] | undefined;
285
287
  }, {
286
288
  shasum: Struct<string, null>;
287
289
  location: Struct<{
@@ -305,6 +307,7 @@ export declare const SnapManifestStruct: Struct<{
305
307
  }>;
306
308
  }>;
307
309
  files: Struct<string[] | undefined, Struct<string, null>>;
310
+ locales: Struct<string[] | undefined, Struct<string, null>>;
308
311
  }>;
309
312
  initialPermissions: Struct<{
310
313
  'endowment:network-access'?: {} | undefined;
@@ -2,6 +2,7 @@ import type { SubjectPermissions, PermissionConstraint } from '@metamask/permiss
2
2
  import type { BlockReason } from '@metamask/snaps-registry';
3
3
  import type { Json, JsonRpcError, Opaque, SemVerVersion } from '@metamask/utils';
4
4
  import type { Struct } from 'superstruct';
5
+ import type { LocalizationFile } from './localization';
5
6
  import type { SnapManifest, SnapPermissions } from './manifest/validation';
6
7
  import type { FetchedSnapFiles, SnapId, SnapsPermissionRequest } from './types';
7
8
  import { SnapIdPrefixes, SnapValidationFailureReason } from './types';
@@ -98,6 +99,10 @@ export declare type Snap = {
98
99
  * Static auxiliary files that can be loaded at runtime.
99
100
  */
100
101
  auxiliaryFiles?: SnapAuxilaryFile[];
102
+ /**
103
+ * Localization files which are used to translate the manifest.
104
+ */
105
+ localizationFiles?: LocalizationFile[];
101
106
  };
102
107
  export declare type TruncatedSnapFields = 'id' | 'initialPermissions' | 'version' | 'enabled' | 'blocked';
103
108
  /**
@@ -2,6 +2,7 @@ import type { Json } from '@metamask/utils';
2
2
  import type { Infer, Struct } from 'superstruct';
3
3
  import type { SnapCaveatType } from './caveats';
4
4
  import type { SnapFunctionExports, SnapRpcHookArgs } from './handlers';
5
+ import type { LocalizationFile } from './localization';
5
6
  import type { SnapManifest } from './manifest';
6
7
  import type { VirtualFile } from './virtual-file';
7
8
  export declare enum NpmSnapFileNames {
@@ -53,6 +54,7 @@ export declare type UnvalidatedSnapFiles = {
53
54
  sourceCode?: VirtualFile;
54
55
  svgIcon?: VirtualFile;
55
56
  auxiliaryFiles: VirtualFile[];
57
+ localizationFiles: VirtualFile[];
56
58
  };
57
59
  /**
58
60
  * An object for storing the contents of Snap files that have passed JSON
@@ -64,11 +66,12 @@ export declare type SnapFiles = {
64
66
  sourceCode: VirtualFile;
65
67
  svgIcon?: VirtualFile;
66
68
  auxiliaryFiles: VirtualFile[];
69
+ localizationFiles: VirtualFile<LocalizationFile>[];
67
70
  };
68
71
  /**
69
72
  * A subset of snap files extracted from a fetched snap.
70
73
  */
71
- export declare type FetchedSnapFiles = Pick<SnapFiles, 'manifest' | 'sourceCode' | 'svgIcon' | 'auxiliaryFiles'>;
74
+ export declare type FetchedSnapFiles = Pick<SnapFiles, 'manifest' | 'sourceCode' | 'svgIcon' | 'auxiliaryFiles' | 'localizationFiles'>;
72
75
  /**
73
76
  * The possible prefixes for snap ids.
74
77
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-utils",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/MetaMask/snaps.git"