@metamask/snaps-utils 0.27.1 → 0.29.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.
- package/dist/caveats.d.ts +5 -1
- package/dist/caveats.js +4 -0
- package/dist/caveats.js.map +1 -1
- package/dist/checksum.d.ts +21 -0
- package/dist/checksum.js +42 -0
- package/dist/checksum.js.map +1 -0
- package/dist/eval-worker.js +1 -0
- package/dist/eval-worker.js.map +1 -1
- package/dist/fs.d.ts +2 -11
- package/dist/fs.js +8 -17
- package/dist/fs.js.map +1 -1
- package/dist/handlers.d.ts +0 -7
- package/dist/handlers.js.map +1 -1
- package/dist/iframe.d.ts +10 -0
- package/dist/iframe.js +57 -0
- package/dist/iframe.js.map +1 -0
- package/dist/index.browser.d.ts +4 -0
- package/dist/index.browser.js +4 -0
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/logging.d.ts +49 -0
- package/dist/logging.js +67 -0
- package/dist/logging.js.map +1 -0
- package/dist/manifest/manifest.d.ts +16 -6
- package/dist/manifest/manifest.js +85 -48
- package/dist/manifest/manifest.js.map +1 -1
- package/dist/manifest/validation.d.ts +6 -29
- package/dist/manifest/validation.js +5 -38
- package/dist/manifest/validation.js.map +1 -1
- package/dist/mock.js +1 -2
- package/dist/mock.js.map +1 -1
- package/dist/namespace.d.ts +3 -3
- package/dist/npm.js +5 -4
- package/dist/npm.js.map +1 -1
- package/dist/post-process.js +2 -1
- package/dist/post-process.js.map +1 -1
- package/dist/snaps.d.ts +28 -29
- package/dist/snaps.js +60 -24
- package/dist/snaps.js.map +1 -1
- package/dist/types.d.ts +23 -14
- package/dist/types.js +4 -3
- package/dist/types.js.map +1 -1
- package/dist/versions.d.ts +1 -101
- package/dist/versions.js +2 -90
- package/dist/versions.js.map +1 -1
- package/dist/virtual-file/toVirtualFile.d.ts +1 -1
- package/dist/virtual-file/toVirtualFile.js +3 -6
- package/dist/virtual-file/toVirtualFile.js.map +1 -1
- package/package.json +11 -5
|
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.validateNpmSnapManifest = exports.getWritableManifest = exports.getSnapSourceCode = exports.fixManifest = exports.checkManifest = void 0;
|
|
17
|
+
exports.validateNpmSnapManifest = exports.getWritableManifest = exports.getSnapIcon = exports.getSnapSourceCode = exports.fixManifest = exports.checkManifest = void 0;
|
|
18
18
|
const utils_1 = require("@metamask/utils");
|
|
19
19
|
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
20
20
|
const fs_1 = require("fs");
|
|
@@ -24,6 +24,7 @@ const fs_2 = require("../fs");
|
|
|
24
24
|
const npm_1 = require("../npm");
|
|
25
25
|
const snaps_1 = require("../snaps");
|
|
26
26
|
const types_1 = require("../types");
|
|
27
|
+
const virtual_file_1 = require("../virtual-file");
|
|
27
28
|
const MANIFEST_SORT_ORDER = {
|
|
28
29
|
version: 1,
|
|
29
30
|
description: 2,
|
|
@@ -45,22 +46,18 @@ const MANIFEST_SORT_ORDER = {
|
|
|
45
46
|
* were encountered during processing of the manifest files.
|
|
46
47
|
*/
|
|
47
48
|
async function checkManifest(basePath, writeManifest = true, sourceCode) {
|
|
48
|
-
var _a, _b, _c;
|
|
49
49
|
const warnings = [];
|
|
50
50
|
const errors = [];
|
|
51
51
|
let updated = false;
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
: /* istanbul ignore next */
|
|
57
|
-
undefined;
|
|
52
|
+
const manifestPath = path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest);
|
|
53
|
+
const manifestFile = await (0, fs_2.readJsonFile)(manifestPath);
|
|
54
|
+
const unvalidatedManifest = manifestFile.result;
|
|
55
|
+
const packageFile = await (0, fs_2.readJsonFile)(path_1.default.join(basePath, types_1.NpmSnapFileNames.PackageJson));
|
|
58
56
|
const snapFiles = {
|
|
59
|
-
manifest:
|
|
60
|
-
packageJson:
|
|
61
|
-
sourceCode:
|
|
62
|
-
svgIcon:
|
|
63
|
-
(await fs_1.promises.readFile(path_1.default.join(basePath, iconPath), 'utf8')),
|
|
57
|
+
manifest: manifestFile,
|
|
58
|
+
packageJson: packageFile,
|
|
59
|
+
sourceCode: await getSnapSourceCode(basePath, unvalidatedManifest, sourceCode),
|
|
60
|
+
svgIcon: await getSnapIcon(basePath, unvalidatedManifest),
|
|
64
61
|
};
|
|
65
62
|
let manifest;
|
|
66
63
|
try {
|
|
@@ -101,9 +98,10 @@ async function checkManifest(basePath, writeManifest = true, sourceCode) {
|
|
|
101
98
|
throw error;
|
|
102
99
|
}
|
|
103
100
|
}
|
|
104
|
-
// TypeScript
|
|
105
|
-
//
|
|
106
|
-
|
|
101
|
+
// TypeScript assumes `manifest` can still be undefined, that is not the case.
|
|
102
|
+
// But we assert to keep TypeScript happy.
|
|
103
|
+
(0, utils_1.assert)(manifest);
|
|
104
|
+
const validatedManifest = manifest.result;
|
|
107
105
|
// Check presence of recommended keys
|
|
108
106
|
const recommendedFields = ['repository'];
|
|
109
107
|
const missingRecommendedFields = recommendedFields.filter((key) => !validatedManifest[key]);
|
|
@@ -114,7 +112,10 @@ async function checkManifest(basePath, writeManifest = true, sourceCode) {
|
|
|
114
112
|
}
|
|
115
113
|
if (writeManifest) {
|
|
116
114
|
try {
|
|
117
|
-
|
|
115
|
+
const newManifest = `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\n`;
|
|
116
|
+
if (updated || newManifest !== manifestFile.value) {
|
|
117
|
+
await fs_1.promises.writeFile(path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest), newManifest);
|
|
118
|
+
}
|
|
118
119
|
}
|
|
119
120
|
catch (error) {
|
|
120
121
|
// Note: This error isn't pushed to the errors array, because it's not an
|
|
@@ -135,28 +136,31 @@ exports.checkManifest = checkManifest;
|
|
|
135
136
|
* @returns A copy of the manifest file where the cause of the error is fixed.
|
|
136
137
|
*/
|
|
137
138
|
function fixManifest(snapFiles, error) {
|
|
138
|
-
const { manifest, packageJson
|
|
139
|
-
const
|
|
139
|
+
const { manifest, packageJson } = snapFiles;
|
|
140
|
+
const clonedFile = manifest.clone();
|
|
141
|
+
const manifestCopy = clonedFile.result;
|
|
140
142
|
switch (error.reason) {
|
|
141
143
|
case types_1.SnapValidationFailureReason.NameMismatch:
|
|
142
|
-
manifestCopy.source.location.npm.packageName = packageJson.name;
|
|
144
|
+
manifestCopy.source.location.npm.packageName = packageJson.result.name;
|
|
143
145
|
break;
|
|
144
146
|
case types_1.SnapValidationFailureReason.VersionMismatch:
|
|
145
|
-
manifestCopy.version = packageJson.version;
|
|
147
|
+
manifestCopy.version = packageJson.result.version;
|
|
146
148
|
break;
|
|
147
149
|
case types_1.SnapValidationFailureReason.RepositoryMismatch:
|
|
148
|
-
manifestCopy.repository = packageJson.repository
|
|
149
|
-
? (0, deep_clone_1.deepClone)(packageJson.repository)
|
|
150
|
+
manifestCopy.repository = packageJson.result.repository
|
|
151
|
+
? (0, deep_clone_1.deepClone)(packageJson.result.repository)
|
|
150
152
|
: undefined;
|
|
151
153
|
break;
|
|
152
154
|
case types_1.SnapValidationFailureReason.ShasumMismatch:
|
|
153
|
-
manifestCopy.source.shasum = (0, snaps_1.
|
|
155
|
+
manifestCopy.source.shasum = (0, snaps_1.getSnapChecksum)(snapFiles);
|
|
154
156
|
break;
|
|
155
157
|
/* istanbul ignore next */
|
|
156
158
|
default:
|
|
157
159
|
(0, utils_1.assertExhaustive)(error.reason);
|
|
158
160
|
}
|
|
159
|
-
|
|
161
|
+
clonedFile.result = manifestCopy;
|
|
162
|
+
clonedFile.value = JSON.stringify(manifestCopy);
|
|
163
|
+
return clonedFile;
|
|
160
164
|
}
|
|
161
165
|
exports.fixManifest = fixManifest;
|
|
162
166
|
/**
|
|
@@ -165,24 +169,59 @@ exports.fixManifest = fixManifest;
|
|
|
165
169
|
*
|
|
166
170
|
* @param basePath - The path to the folder with the manifest files.
|
|
167
171
|
* @param manifest - The unvalidated Snap manifest file contents.
|
|
172
|
+
* @param sourceCode - Override source code for plugins.
|
|
168
173
|
* @returns The contents of the bundle file, if any.
|
|
169
174
|
*/
|
|
170
|
-
async function getSnapSourceCode(basePath, manifest) {
|
|
175
|
+
async function getSnapSourceCode(basePath, manifest, sourceCode) {
|
|
171
176
|
var _a, _b, _c;
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
177
|
+
if (!(0, utils_1.isPlainObject)(manifest)) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
const sourceFilePath = (_c = (_b = (_a = manifest.source) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.npm) === null || _c === void 0 ? void 0 : _c.filePath;
|
|
181
|
+
if (!sourceFilePath) {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
if (sourceCode) {
|
|
185
|
+
return new virtual_file_1.VirtualFile({
|
|
186
|
+
path: path_1.default.join(basePath, sourceFilePath),
|
|
187
|
+
value: sourceCode,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, sourceFilePath), 'utf8');
|
|
192
|
+
return virtualFile;
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
throw new Error(`Failed to read Snap bundle file: ${error.message}`);
|
|
182
196
|
}
|
|
183
|
-
return undefined;
|
|
184
197
|
}
|
|
185
198
|
exports.getSnapSourceCode = getSnapSourceCode;
|
|
199
|
+
/**
|
|
200
|
+
* Given an unvalidated Snap manifest, attempts to extract the location of the
|
|
201
|
+
* icon and read the file.
|
|
202
|
+
*
|
|
203
|
+
* @param basePath - The path to the folder with the manifest files.
|
|
204
|
+
* @param manifest - The unvalidated Snap manifest file contents.
|
|
205
|
+
* @returns The contents of the icon, if any.
|
|
206
|
+
*/
|
|
207
|
+
async function getSnapIcon(basePath, manifest) {
|
|
208
|
+
var _a, _b, _c;
|
|
209
|
+
if (!(0, utils_1.isPlainObject)(manifest)) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
const iconPath = (_c = (_b = (_a = manifest.source) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.npm) === null || _c === void 0 ? void 0 : _c.iconPath;
|
|
213
|
+
if (!iconPath) {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, iconPath), 'utf8');
|
|
218
|
+
return virtualFile;
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
throw new Error(`Failed to read Snap icon file: ${error.message}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
exports.getSnapIcon = getSnapIcon;
|
|
186
225
|
/**
|
|
187
226
|
* Sorts the given manifest in our preferred sort order and removes the
|
|
188
227
|
* `repository` field if it is falsy (it may be `null`).
|
|
@@ -207,16 +246,15 @@ exports.getWritableManifest = getWritableManifest;
|
|
|
207
246
|
* @param snapFiles.manifest - The npm Snap manifest to validate.
|
|
208
247
|
* @param snapFiles.packageJson - The npm Snap's `package.json`.
|
|
209
248
|
* @param snapFiles.sourceCode - The Snap's source code.
|
|
210
|
-
* @
|
|
211
|
-
* and `package.json`.
|
|
249
|
+
* @param snapFiles.svgIcon - The Snap's optional icon.
|
|
212
250
|
*/
|
|
213
|
-
function validateNpmSnapManifest({ manifest, packageJson, sourceCode, }) {
|
|
214
|
-
const packageJsonName = packageJson.name;
|
|
215
|
-
const packageJsonVersion = packageJson.version;
|
|
216
|
-
const packageJsonRepository = packageJson.repository;
|
|
217
|
-
const manifestPackageName = manifest.source.location.npm.packageName;
|
|
218
|
-
const manifestPackageVersion = manifest.version;
|
|
219
|
-
const manifestRepository = manifest.repository;
|
|
251
|
+
function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, }) {
|
|
252
|
+
const packageJsonName = packageJson.result.name;
|
|
253
|
+
const packageJsonVersion = packageJson.result.version;
|
|
254
|
+
const packageJsonRepository = packageJson.result.repository;
|
|
255
|
+
const manifestPackageName = manifest.result.source.location.npm.packageName;
|
|
256
|
+
const manifestPackageVersion = manifest.result.version;
|
|
257
|
+
const manifestRepository = manifest.result.repository;
|
|
220
258
|
if (packageJsonName !== manifestPackageName) {
|
|
221
259
|
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);
|
|
222
260
|
}
|
|
@@ -230,8 +268,7 @@ function validateNpmSnapManifest({ manifest, packageJson, sourceCode, }) {
|
|
|
230
268
|
!(0, fast_deep_equal_1.default)(packageJsonRepository, manifestRepository)) {
|
|
231
269
|
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);
|
|
232
270
|
}
|
|
233
|
-
(0, snaps_1.validateSnapShasum)(manifest, sourceCode, `"${types_1.NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
|
|
234
|
-
return [manifest, sourceCode, packageJson];
|
|
271
|
+
(0, snaps_1.validateSnapShasum)({ manifest, sourceCode, svgIcon }, `"${types_1.NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
|
|
235
272
|
}
|
|
236
273
|
exports.validateNpmSnapManifest = validateNpmSnapManifest;
|
|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C;IAErD,MAAM,mBAAmB,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC;IACrE,MAAM,sBAAsB,GAAG,QAAQ,CAAC,OAAO,CAAC;IAChD,MAAM,kBAAkB,GAAG,QAAQ,CAAC,UAAU,CAAC;IAE/C,IAAI,eAAe,KAAK,mBAAmB,EAAE;QAC3C,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,wBAAwB,mBAAmB,0BAA0B,wBAAgB,CAAC,WAAW,oBAAoB,eAAe,KAAK,EACtK,mCAA2B,CAAC,YAAY,CACzC,CAAC;KACH;IAED,IAAI,kBAAkB,KAAK,sBAAsB,EAAE;QACjD,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,2BAA2B,sBAAsB,0BAA0B,wBAAgB,CAAC,WAAW,uBAAuB,kBAAkB,KAAK,EAClL,mCAA2B,CAAC,eAAe,CAC5C,CAAC;KACH;IAED;IACE,4EAA4E;IAC5E,wDAAwD;IACxD,CAAC,qBAAqB,IAAI,kBAAkB,CAAC;QAC7C,CAAC,IAAA,yBAAS,EAAC,qBAAqB,EAAE,kBAAkB,CAAC,EACrD;QACA,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,4CAA4C,wBAAgB,CAAC,WAAW,uBAAuB,EAC5H,mCAA2B,CAAC,kBAAkB,CAC/C,CAAC;KACH;IAED,IAAA,0BAAkB,EAChB,QAAQ,EACR,UAAU,EACV,IAAI,wBAAgB,CAAC,QAAQ,kDAAkD,CAChF,CAAC;IACF,OAAO,CAAC,QAAQ,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC;AAC7C,CAAC;AA7CD,0DA6CC","sourcesContent":["import { Json, assertExhaustive } 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 { readSnapJsonFile } from '../fs';\nimport { validateNpmSnap } from '../npm';\nimport {\n getSnapSourceShasum,\n ProgrammaticallyFixableSnapError,\n validateSnapShasum,\n} from '../snaps';\nimport {\n NpmSnapFileNames,\n NpmSnapPackageJson,\n SnapFiles,\n SnapValidationFailureReason,\n UnvalidatedSnapFiles,\n} from '../types';\nimport { SnapManifest } from './validation';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n version: 1,\n description: 2,\n proposedName: 3,\n repository: 4,\n source: 5,\n initialPermissions: 6,\n manifestVersion: 7,\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 unvalidatedManifest = await readSnapJsonFile(\n basePath,\n NpmSnapFileNames.Manifest,\n );\n\n const iconPath =\n unvalidatedManifest && typeof unvalidatedManifest === 'object'\n ? /* istanbul ignore next */\n (unvalidatedManifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath\n : /* istanbul ignore next */\n undefined;\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: unvalidatedManifest,\n packageJson: await readSnapJsonFile(basePath, NpmSnapFileNames.PackageJson),\n sourceCode:\n sourceCode ?? (await getSnapSourceCode(basePath, unvalidatedManifest)),\n svgIcon:\n iconPath &&\n (await fs.readFile(pathUtils.join(basePath, iconPath), 'utf8')),\n };\n\n let manifest: 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 doesn't see that the 'manifest' variable must be of type\n // SnapManifest at this point, so we cast it.\n const validatedManifest = manifest as SnapManifest;\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 await fs.writeFile(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\\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): SnapManifest {\n const { manifest, packageJson, sourceCode } = snapFiles;\n const manifestCopy = deepClone(manifest);\n\n switch (error.reason) {\n case SnapValidationFailureReason.NameMismatch:\n manifestCopy.source.location.npm.packageName = packageJson.name;\n break;\n\n case SnapValidationFailureReason.VersionMismatch:\n manifestCopy.version = packageJson.version;\n break;\n\n case SnapValidationFailureReason.RepositoryMismatch:\n manifestCopy.repository = packageJson.repository\n ? deepClone(packageJson.repository)\n : undefined;\n break;\n\n case SnapValidationFailureReason.ShasumMismatch:\n manifestCopy.source.shasum = getSnapSourceShasum(sourceCode);\n break;\n\n /* istanbul ignore next */\n default:\n assertExhaustive(error.reason);\n }\n\n return manifestCopy;\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 * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n): Promise<string | undefined> {\n if (manifest && typeof manifest === 'object' && !Array.isArray(manifest)) {\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n try {\n return sourceFilePath\n ? await fs.readFile(pathUtils.join(basePath, sourceFilePath), 'utf8')\n : undefined;\n } catch (error) {\n throw new Error(`Failed to read Snap bundle file: ${error.message}`);\n }\n }\n\n return undefined;\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 * @returns A tuple containing the validated snap manifest, snap source code,\n * and `package.json`.\n */\nexport function validateNpmSnapManifest({\n manifest,\n packageJson,\n sourceCode,\n}: SnapFiles): [SnapManifest, string, NpmSnapPackageJson] {\n const packageJsonName = packageJson.name;\n const packageJsonVersion = packageJson.version;\n const packageJsonRepository = packageJson.repository;\n\n const manifestPackageName = manifest.source.location.npm.packageName;\n const manifestPackageVersion = manifest.version;\n const manifestRepository = manifest.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,\n sourceCode,\n `\"${NpmSnapFileNames.Manifest}\" \"shasum\" field does not match computed shasum.`,\n );\n return [manifest, sourceCode, packageJson];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"manifest.js","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAgF;AAChF,sEAAwC;AACxC,2BAAoC;AACpC,gDAA6B;AAE7B,8CAA0C;AAC1C,8BAAqC;AACrC,gCAAyC;AACzC,oCAIkB;AAClB,oCAKkB;AAClB,kDAA+D;AAG/D,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC;IACT,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;CACnB,CAAC;AAuBF;;;;;;;;;;GAUG;AACI,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,aAAa,GAAG,IAAI,EACpB,UAAmB;IAEnB,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,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,4CAA4C,wBAAgB,CAAC,WAAW,uBAAuB,EAC5H,mCAA2B,CAAC,kBAAkB,CAC/C,CAAC;KACH;IAED,IAAA,0BAAkB,EAChB,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,EACjC,IAAI,wBAAgB,CAAC,QAAQ,kDAAkD,CAChF,CAAC;AACJ,CAAC;AA5CD,0DA4CC","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 version: 1,\n description: 2,\n proposedName: 3,\n repository: 4,\n source: 5,\n initialPermissions: 6,\n manifestVersion: 7,\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"]}
|
|
@@ -1,27 +1,4 @@
|
|
|
1
1
|
import { Infer, Struct } from 'superstruct';
|
|
2
|
-
export declare type Base64Opts = {
|
|
3
|
-
/**
|
|
4
|
-
* Is the `=` padding at the end required or not.
|
|
5
|
-
*
|
|
6
|
-
* @default false
|
|
7
|
-
*/
|
|
8
|
-
paddingRequired?: boolean;
|
|
9
|
-
/**
|
|
10
|
-
* Which character set should be used.
|
|
11
|
-
* The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.
|
|
12
|
-
*
|
|
13
|
-
* @default 'base64'
|
|
14
|
-
*/
|
|
15
|
-
characterSet?: 'base64' | 'base64url';
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Ensure that a provided string-based struct is valid base64.
|
|
19
|
-
*
|
|
20
|
-
* @param struct - The string based struct.
|
|
21
|
-
* @param opts - Optional options to specialize base64 validation. See {@link Base64Opts} documentation.
|
|
22
|
-
* @returns A superstruct validating base64.
|
|
23
|
-
*/
|
|
24
|
-
export declare const base64: <T extends string, S>(struct: Struct<T, S>, opts?: Base64Opts) => Struct<T, S>;
|
|
25
2
|
export declare const Bip32PathStruct: Struct<string[], Struct<string, null>>;
|
|
26
3
|
export declare const bip32entropy: <T extends {
|
|
27
4
|
path: string[];
|
|
@@ -213,10 +190,11 @@ export declare const PermissionsStruct: Struct<{
|
|
|
213
190
|
}>;
|
|
214
191
|
export declare type SnapPermissions = Infer<typeof PermissionsStruct>;
|
|
215
192
|
export declare const SnapManifestStruct: Struct<{
|
|
216
|
-
version: import("
|
|
193
|
+
version: import("@metamask/utils").SemVerVersion;
|
|
217
194
|
description: string;
|
|
218
195
|
proposedName: string;
|
|
219
196
|
source: {
|
|
197
|
+
shasum: string;
|
|
220
198
|
location: {
|
|
221
199
|
npm: {
|
|
222
200
|
filePath: string;
|
|
@@ -225,7 +203,6 @@ export declare const SnapManifestStruct: Struct<{
|
|
|
225
203
|
iconPath?: string | undefined;
|
|
226
204
|
};
|
|
227
205
|
};
|
|
228
|
-
shasum: string;
|
|
229
206
|
};
|
|
230
207
|
initialPermissions: {
|
|
231
208
|
'endowment:long-running'?: {} | undefined;
|
|
@@ -280,7 +257,7 @@ export declare const SnapManifestStruct: Struct<{
|
|
|
280
257
|
url: string;
|
|
281
258
|
} | undefined;
|
|
282
259
|
}, {
|
|
283
|
-
version: Struct<import("
|
|
260
|
+
version: Struct<import("@metamask/utils").SemVerVersion, null>;
|
|
284
261
|
description: Struct<string, null>;
|
|
285
262
|
proposedName: Struct<string, null>;
|
|
286
263
|
repository: Struct<{
|
|
@@ -291,6 +268,7 @@ export declare const SnapManifestStruct: Struct<{
|
|
|
291
268
|
url: Struct<string, null>;
|
|
292
269
|
}>;
|
|
293
270
|
source: Struct<{
|
|
271
|
+
shasum: string;
|
|
294
272
|
location: {
|
|
295
273
|
npm: {
|
|
296
274
|
filePath: string;
|
|
@@ -299,7 +277,6 @@ export declare const SnapManifestStruct: Struct<{
|
|
|
299
277
|
iconPath?: string | undefined;
|
|
300
278
|
};
|
|
301
279
|
};
|
|
302
|
-
shasum: string;
|
|
303
280
|
}, {
|
|
304
281
|
shasum: Struct<string, null>;
|
|
305
282
|
location: Struct<{
|
|
@@ -316,8 +293,8 @@ export declare const SnapManifestStruct: Struct<{
|
|
|
316
293
|
registry: "https://registry.npmjs.org" | "https://registry.npmjs.org/";
|
|
317
294
|
iconPath?: string | undefined;
|
|
318
295
|
}, {
|
|
319
|
-
filePath: Struct<string,
|
|
320
|
-
iconPath: Struct<string | undefined,
|
|
296
|
+
filePath: Struct<string, null>;
|
|
297
|
+
iconPath: Struct<string | undefined, null>;
|
|
321
298
|
packageName: Struct<string, null>;
|
|
322
299
|
registry: Struct<"https://registry.npmjs.org" | "https://registry.npmjs.org/", null>;
|
|
323
300
|
}>;
|
|
@@ -1,50 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createSnapManifest = exports.assertIsSnapManifest = exports.isSnapManifest = exports.SnapManifestStruct = exports.PermissionsStruct = exports.SnapGetBip32EntropyPermissionsStruct = exports.Bip32EntropyStruct = exports.bip32entropy = exports.Bip32PathStruct =
|
|
3
|
+
exports.createSnapManifest = exports.assertIsSnapManifest = exports.isSnapManifest = exports.SnapManifestStruct = exports.PermissionsStruct = exports.SnapGetBip32EntropyPermissionsStruct = exports.Bip32EntropyStruct = exports.bip32entropy = exports.Bip32PathStruct = void 0;
|
|
4
4
|
const utils_1 = require("@metamask/utils");
|
|
5
5
|
const superstruct_1 = require("superstruct");
|
|
6
6
|
const cronjob_1 = require("../cronjob");
|
|
7
7
|
const entropy_1 = require("../entropy");
|
|
8
8
|
const json_rpc_1 = require("../json-rpc");
|
|
9
9
|
const namespace_1 = require("../namespace");
|
|
10
|
-
const path_1 = require("../path");
|
|
11
10
|
const types_1 = require("../types");
|
|
12
|
-
const versions_1 = require("../versions");
|
|
13
11
|
// BIP-43 purposes that cannot be used for entropy derivation. These are in the
|
|
14
12
|
// string form, ending with `'`.
|
|
15
13
|
const FORBIDDEN_PURPOSES = [
|
|
16
14
|
entropy_1.SIP_6_MAGIC_VALUE,
|
|
17
15
|
entropy_1.STATE_ENCRYPTION_MAGIC_VALUE,
|
|
18
16
|
];
|
|
19
|
-
/**
|
|
20
|
-
* Ensure that a provided string-based struct is valid base64.
|
|
21
|
-
*
|
|
22
|
-
* @param struct - The string based struct.
|
|
23
|
-
* @param opts - Optional options to specialize base64 validation. See {@link Base64Opts} documentation.
|
|
24
|
-
* @returns A superstruct validating base64.
|
|
25
|
-
*/
|
|
26
|
-
const base64 = (struct, opts = {}) => {
|
|
27
|
-
var _a, _b;
|
|
28
|
-
const paddingRequired = (_a = opts.paddingRequired) !== null && _a !== void 0 ? _a : false;
|
|
29
|
-
const characterSet = (_b = opts.characterSet) !== null && _b !== void 0 ? _b : 'base64';
|
|
30
|
-
let letters;
|
|
31
|
-
if (characterSet === 'base64') {
|
|
32
|
-
letters = String.raw `[A-Za-z0-9+\/]`;
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
(0, utils_1.assert)(characterSet === 'base64url');
|
|
36
|
-
letters = String.raw `[-_A-Za-z0-9]`;
|
|
37
|
-
}
|
|
38
|
-
let re;
|
|
39
|
-
if (paddingRequired) {
|
|
40
|
-
re = new RegExp(`^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`, 'u');
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
re = new RegExp(`^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`, 'u');
|
|
44
|
-
}
|
|
45
|
-
return (0, superstruct_1.pattern)(struct, re);
|
|
46
|
-
};
|
|
47
|
-
exports.base64 = base64;
|
|
48
17
|
const BIP32_INDEX_REGEX = /^\d+'?$/u;
|
|
49
18
|
exports.Bip32PathStruct = (0, superstruct_1.refine)((0, superstruct_1.array)((0, superstruct_1.string)()), 'BIP-32 path', (path) => {
|
|
50
19
|
if (path.length === 0) {
|
|
@@ -98,10 +67,8 @@ exports.PermissionsStruct = (0, superstruct_1.type)({
|
|
|
98
67
|
namespaces: namespace_1.NamespacesStruct,
|
|
99
68
|
})),
|
|
100
69
|
});
|
|
101
|
-
/* eslint-enable @typescript-eslint/naming-convention */
|
|
102
|
-
const relativePath = (struct) => (0, superstruct_1.coerce)(struct, struct, (value) => (0, path_1.normalizeRelative)(value));
|
|
103
70
|
exports.SnapManifestStruct = (0, superstruct_1.object)({
|
|
104
|
-
version:
|
|
71
|
+
version: utils_1.VersionStruct,
|
|
105
72
|
description: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, 280),
|
|
106
73
|
proposedName: (0, superstruct_1.size)((0, superstruct_1.pattern)((0, superstruct_1.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),
|
|
107
74
|
repository: (0, superstruct_1.optional)((0, superstruct_1.object)({
|
|
@@ -109,11 +76,11 @@ exports.SnapManifestStruct = (0, superstruct_1.object)({
|
|
|
109
76
|
url: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
|
|
110
77
|
})),
|
|
111
78
|
source: (0, superstruct_1.object)({
|
|
112
|
-
shasum:
|
|
79
|
+
shasum: utils_1.ChecksumStruct,
|
|
113
80
|
location: (0, superstruct_1.object)({
|
|
114
81
|
npm: (0, superstruct_1.object)({
|
|
115
|
-
filePath:
|
|
116
|
-
iconPath: (0, superstruct_1.optional)(
|
|
82
|
+
filePath: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
|
|
83
|
+
iconPath: (0, superstruct_1.optional)((0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity)),
|
|
117
84
|
packageName: types_1.NameStruct,
|
|
118
85
|
registry: (0, superstruct_1.union)([
|
|
119
86
|
(0, superstruct_1.literal)('https://registry.npmjs.org'),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/manifest/validation.ts"],"names":[],"mappings":";;;AAAA,2CAAuD;AACvD,6CAmBqB;AAErB,wCAA6D;AAC7D,wCAA6E;AAC7E,0CAA+C;AAC/C,4CAAgD;AAChD,kC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AAE,IAAA,sBAAQ,EAAC,YAAY,CAAC,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;gBAC7D,WAAW,EAAE,kBAAU;gBACvB,QAAQ,EAAE,IAAA,mBAAK,EAAC;oBACd,IAAA,qBAAO,EAAC,4BAA4B,CAAC;oBACrC,IAAA,qBAAO,EAAC,6BAA6B,CAAC;iBACvC,CAAC;aACH,CAAC;SACH,CAAC;KACH,CAAC;IACF,kBAAkB,EAAE,yBAAiB;IACrC,eAAe,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;CAChC,CAAC,CAAC;AAIH;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AACvC,CAAC;AAFD,wCAEC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAc;IAEd,IAAA,oBAAY,EACV,KAAK,EACL,0BAAkB,EAClB,IAAI,wBAAgB,CAAC,QAAQ,cAAc,CAC5C,CAAC;AACJ,CAAC;AARD,oDAQC;AAED;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAAC,KAAc;IAC/C,qEAAqE;IACrE,OAAO,IAAA,oBAAM,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AAC3C,CAAC;AAHD,gDAGC","sourcesContent":["import { assert, assertStruct } from '@metamask/utils';\nimport {\n array,\n boolean,\n coerce,\n create,\n enums,\n Infer,\n integer,\n is,\n literal,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n Struct,\n type,\n union,\n} from 'superstruct';\n\nimport { CronjobSpecificationArrayStruct } from '../cronjob';\nimport { SIP_6_MAGIC_VALUE, STATE_ENCRYPTION_MAGIC_VALUE } from '../entropy';\nimport { RpcOriginsStruct } from '../json-rpc';\nimport { NamespacesStruct } from '../namespace';\nimport { normalizeRelative } from '../path';\nimport { NameStruct, NpmSnapFileNames } from '../types';\nimport { VersionStruct } from '../versions';\n\n// BIP-43 purposes that cannot be used for entropy derivation. These are in the\n// string form, ending with `'`.\nconst FORBIDDEN_PURPOSES: string[] = [\n SIP_6_MAGIC_VALUE,\n STATE_ENCRYPTION_MAGIC_VALUE,\n];\n\nexport type Base64Opts = {\n /**\n * Is the `=` padding at the end required or not.\n *\n * @default false\n */\n // Padding is optional in RFC 4648, that's why the default value is false\n paddingRequired?: boolean;\n /**\n * Which character set should be used.\n * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.\n *\n * @default 'base64'\n */\n characterSet?: 'base64' | 'base64url';\n};\n\n/**\n * Ensure that a provided string-based struct is valid base64.\n *\n * @param struct - The string based struct.\n * @param opts - Optional options to specialize base64 validation. See {@link Base64Opts} documentation.\n * @returns A superstruct validating base64.\n */\nexport const base64 = <T extends string, S>(\n struct: Struct<T, S>,\n opts: Base64Opts = {},\n) => {\n const paddingRequired = opts.paddingRequired ?? false;\n const characterSet = opts.characterSet ?? 'base64';\n\n let letters: string;\n if (characterSet === 'base64') {\n letters = String.raw`[A-Za-z0-9+\\/]`;\n } else {\n assert(characterSet === 'base64url');\n letters = String.raw`[-_A-Za-z0-9]`;\n }\n\n let re: RegExp;\n if (paddingRequired) {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n } else {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n }\n\n return pattern(struct, re);\n};\n\nconst BIP32_INDEX_REGEX = /^\\d+'?$/u;\nexport const Bip32PathStruct = refine(\n array(string()),\n 'BIP-32 path',\n (path) => {\n if (path.length === 0) {\n return 'Path must be a non-empty BIP-32 derivation path array';\n }\n\n if (path[0] !== 'm') {\n return 'Path must start with \"m\".';\n }\n\n if (path.length < 3) {\n return 'Paths must have a length of at least three.';\n }\n\n if (path.slice(1).some((part) => !BIP32_INDEX_REGEX.test(part))) {\n return 'Path must be a valid BIP-32 derivation path array.';\n }\n\n if (FORBIDDEN_PURPOSES.includes(path[1])) {\n return `The purpose \"${path[1]}\" is not allowed for entropy derivation.`;\n }\n\n return true;\n },\n);\n\nexport const bip32entropy = <T extends { path: string[]; curve: string }, S>(\n struct: Struct<T, S>,\n) =>\n refine(struct, 'BIP-32 entropy', (value) => {\n if (\n value.curve === 'ed25519' &&\n value.path.slice(1).some((part) => !part.endsWith(\"'\"))\n ) {\n return 'Ed25519 does not support unhardened paths.';\n }\n\n return true;\n });\n\n// Used outside @metamask/snap-utils\nexport const Bip32EntropyStruct = bip32entropy(\n type({\n path: Bip32PathStruct,\n curve: enums(['ed25519', 'secp256k1']),\n }),\n);\n\nexport type Bip32Entropy = Infer<typeof Bip32EntropyStruct>;\n\nexport const SnapGetBip32EntropyPermissionsStruct = size(\n array(Bip32EntropyStruct),\n 1,\n Infinity,\n);\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport const PermissionsStruct = type({\n 'endowment:long-running': optional(object({})),\n 'endowment:network-access': optional(object({})),\n 'endowment:transaction-insight': optional(\n object({\n allowTransactionOrigin: optional(boolean()),\n }),\n ),\n 'endowment:cronjob': optional(\n object({ jobs: CronjobSpecificationArrayStruct }),\n ),\n 'endowment:rpc': optional(RpcOriginsStruct),\n snap_confirm: optional(object({})),\n snap_manageState: optional(object({})),\n snap_notify: optional(object({})),\n snap_getBip32Entropy: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip32PublicKey: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip44Entropy: optional(\n size(\n array(object({ coinType: size(integer(), 0, 2 ** 32 - 1) })),\n 1,\n Infinity,\n ),\n ),\n snap_getEntropy: optional(object({})),\n 'endowment:keyring': optional(\n object({\n namespaces: NamespacesStruct,\n }),\n ),\n});\n/* eslint-enable @typescript-eslint/naming-convention */\n\nconst relativePath = <Type extends string>(struct: Struct<Type>) =>\n coerce(struct, struct, (value) => normalizeRelative(value));\n\nexport type SnapPermissions = Infer<typeof PermissionsStruct>;\n\nexport const SnapManifestStruct = object({\n version: VersionStruct,\n description: size(string(), 1, 280),\n proposedName: size(\n pattern(\n string(),\n /^(?:[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,\n ),\n 1,\n 214,\n ),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n source: object({\n shasum: size(base64(string(), { paddingRequired: true }), 44, 44),\n location: object({\n npm: object({\n filePath: relativePath(size(string(), 1, Infinity)),\n iconPath: optional(relativePath(size(string(), 1, Infinity))),\n packageName: NameStruct,\n registry: union([\n literal('https://registry.npmjs.org'),\n literal('https://registry.npmjs.org/'),\n ]),\n }),\n }),\n }),\n initialPermissions: PermissionsStruct,\n manifestVersion: literal('0.1'),\n});\n\nexport type SnapManifest = Infer<typeof SnapManifestStruct>;\n\n/**\n * Check if the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link SnapManifest} object.\n */\nexport function isSnapManifest(value: unknown): value is SnapManifest {\n return is(value, SnapManifestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link SnapManifest} object.\n */\nexport function assertIsSnapManifest(\n value: unknown,\n): asserts value is SnapManifest {\n assertStruct(\n value,\n SnapManifestStruct,\n `\"${NpmSnapFileNames.Manifest}\" is invalid`,\n );\n}\n\n/**\n * Creates a {@link SnapManifest} object from JSON.\n *\n *\n * @param value - The value to check.\n * @throws If the value cannot be coerced to a {@link SnapManifest} object.\n * @returns The created {@link SnapManifest} object.\n */\nexport function createSnapManifest(value: unknown): SnapManifest {\n // TODO: Add a utility to prefix these errors similar to assertStruct\n return create(value, SnapManifestStruct);\n}\n"]}
|
|
1
|
+
{"version":3,"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;QACtB,QAAQ,EAAE,IAAA,oBAAM,EAAC;YACf,GAAG,EAAE,IAAA,oBAAM,EAAC;gBACV,QAAQ,EAAE,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC;gBACrC,QAAQ,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;gBAC/C,WAAW,EAAE,kBAAU;gBACvB,QAAQ,EAAE,IAAA,mBAAK,EAAC;oBACd,IAAA,qBAAO,EAAC,4BAA4B,CAAC;oBACrC,IAAA,qBAAO,EAAC,6BAA6B,CAAC;iBACvC,CAAC;aACH,CAAC;SACH,CAAC;KACH,CAAC;IACF,kBAAkB,EAAE,yBAAiB;IACrC,eAAe,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;CAChC,CAAC,CAAC;AAIH;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AACvC,CAAC;AAFD,wCAEC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAc;IAEd,IAAA,oBAAY,EACV,KAAK,EACL,0BAAkB,EAClB,IAAI,wBAAgB,CAAC,QAAQ,cAAc,CAC5C,CAAC;AACJ,CAAC;AARD,oDAQC;AAED;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAAC,KAAc;IAC/C,qEAAqE;IACrE,OAAO,IAAA,oBAAM,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AAC3C,CAAC;AAHD,gDAGC","sourcesContent":["import { assertStruct, ChecksumStruct, VersionStruct } from '@metamask/utils';\nimport {\n array,\n boolean,\n create,\n enums,\n Infer,\n integer,\n is,\n literal,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n Struct,\n type,\n union,\n} from 'superstruct';\n\nimport { CronjobSpecificationArrayStruct } from '../cronjob';\nimport { SIP_6_MAGIC_VALUE, STATE_ENCRYPTION_MAGIC_VALUE } from '../entropy';\nimport { RpcOriginsStruct } from '../json-rpc';\nimport { NamespacesStruct } from '../namespace';\nimport { NameStruct, NpmSnapFileNames } from '../types';\n\n// BIP-43 purposes that cannot be used for entropy derivation. These are in the\n// string form, ending with `'`.\nconst FORBIDDEN_PURPOSES: string[] = [\n SIP_6_MAGIC_VALUE,\n STATE_ENCRYPTION_MAGIC_VALUE,\n];\n\nconst BIP32_INDEX_REGEX = /^\\d+'?$/u;\nexport const Bip32PathStruct = refine(\n array(string()),\n 'BIP-32 path',\n (path) => {\n if (path.length === 0) {\n return 'Path must be a non-empty BIP-32 derivation path array';\n }\n\n if (path[0] !== 'm') {\n return 'Path must start with \"m\".';\n }\n\n if (path.length < 3) {\n return 'Paths must have a length of at least three.';\n }\n\n if (path.slice(1).some((part) => !BIP32_INDEX_REGEX.test(part))) {\n return 'Path must be a valid BIP-32 derivation path array.';\n }\n\n if (FORBIDDEN_PURPOSES.includes(path[1])) {\n return `The purpose \"${path[1]}\" is not allowed for entropy derivation.`;\n }\n\n return true;\n },\n);\n\nexport const bip32entropy = <T extends { path: string[]; curve: string }, S>(\n struct: Struct<T, S>,\n) =>\n refine(struct, 'BIP-32 entropy', (value) => {\n if (\n value.curve === 'ed25519' &&\n value.path.slice(1).some((part) => !part.endsWith(\"'\"))\n ) {\n return 'Ed25519 does not support unhardened paths.';\n }\n\n return true;\n });\n\n// Used outside @metamask/snap-utils\nexport const Bip32EntropyStruct = bip32entropy(\n type({\n path: Bip32PathStruct,\n curve: enums(['ed25519', 'secp256k1']),\n }),\n);\n\nexport type Bip32Entropy = Infer<typeof Bip32EntropyStruct>;\n\nexport const SnapGetBip32EntropyPermissionsStruct = size(\n array(Bip32EntropyStruct),\n 1,\n Infinity,\n);\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport const PermissionsStruct = type({\n 'endowment:long-running': optional(object({})),\n 'endowment:network-access': optional(object({})),\n 'endowment:transaction-insight': optional(\n object({\n allowTransactionOrigin: optional(boolean()),\n }),\n ),\n 'endowment:cronjob': optional(\n object({ jobs: CronjobSpecificationArrayStruct }),\n ),\n 'endowment:rpc': optional(RpcOriginsStruct),\n snap_confirm: optional(object({})),\n snap_manageState: optional(object({})),\n snap_notify: optional(object({})),\n snap_getBip32Entropy: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip32PublicKey: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip44Entropy: optional(\n size(\n array(object({ coinType: size(integer(), 0, 2 ** 32 - 1) })),\n 1,\n Infinity,\n ),\n ),\n snap_getEntropy: optional(object({})),\n 'endowment:keyring': optional(\n object({\n namespaces: NamespacesStruct,\n }),\n ),\n});\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport type SnapPermissions = Infer<typeof PermissionsStruct>;\n\nexport const SnapManifestStruct = object({\n version: VersionStruct,\n description: size(string(), 1, 280),\n proposedName: size(\n pattern(\n string(),\n /^(?:[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,\n ),\n 1,\n 214,\n ),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n source: object({\n shasum: ChecksumStruct,\n location: object({\n npm: object({\n filePath: size(string(), 1, Infinity),\n iconPath: optional(size(string(), 1, Infinity)),\n packageName: NameStruct,\n registry: union([\n literal('https://registry.npmjs.org'),\n literal('https://registry.npmjs.org/'),\n ]),\n }),\n }),\n }),\n initialPermissions: PermissionsStruct,\n manifestVersion: literal('0.1'),\n});\n\nexport type SnapManifest = Infer<typeof SnapManifestStruct>;\n\n/**\n * Check if the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link SnapManifest} object.\n */\nexport function isSnapManifest(value: unknown): value is SnapManifest {\n return is(value, SnapManifestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link SnapManifest} object.\n */\nexport function assertIsSnapManifest(\n value: unknown,\n): asserts value is SnapManifest {\n assertStruct(\n value,\n SnapManifestStruct,\n `\"${NpmSnapFileNames.Manifest}\" is invalid`,\n );\n}\n\n/**\n * Creates a {@link SnapManifest} object from JSON.\n *\n *\n * @param value - The value to check.\n * @throws If the value cannot be coerced to a {@link SnapManifest} object.\n * @returns The created {@link SnapManifest} object.\n */\nexport function createSnapManifest(value: unknown): SnapManifest {\n // TODO: Add a utility to prefix these errors similar to assertStruct\n return create(value, SnapManifestStruct);\n}\n"]}
|
package/dist/mock.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.generateMockEndowments = exports.isConstructor = exports.ALL_APIS = void
|
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
const events_1 = __importDefault(require("events"));
|
|
9
9
|
const default_endowments_1 = require("./default-endowments");
|
|
10
|
-
const NETWORK_APIS = ['fetch'
|
|
10
|
+
const NETWORK_APIS = ['fetch'];
|
|
11
11
|
exports.ALL_APIS = [...default_endowments_1.DEFAULT_ENDOWMENTS, ...NETWORK_APIS];
|
|
12
12
|
/**
|
|
13
13
|
* Get a mock snap API, that always returns `true` for requests.
|
|
@@ -67,7 +67,6 @@ const generateMockClass = (value) => {
|
|
|
67
67
|
// Things not currently auto-mocked because of NodeJS, by adding them here we
|
|
68
68
|
// have types for them and can use that to generate mocks if needed.
|
|
69
69
|
const mockWindow = {
|
|
70
|
-
WebSocket: MockClass,
|
|
71
70
|
crypto: crypto_1.default,
|
|
72
71
|
SubtleCrypto: MockClass,
|
|
73
72
|
};
|
package/dist/mock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../src/mock.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,oDAAkC;AAElC,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AAE,CACnE,CAAC;AACJ,CAAC,CAAC;AALW,QAAA,sBAAsB,0BAKjC","sourcesContent":["import crypto from 'crypto';\nimport EventEmitter from 'events';\n\nimport { DEFAULT_ENDOWMENTS } from './default-endowments';\n\nconst NETWORK_APIS = ['fetch'];\n\nexport const ALL_APIS: string[] = [...DEFAULT_ENDOWMENTS, ...NETWORK_APIS];\n\ntype MockSnapGlobal = {\n request: () => Promise<any>;\n};\n\ntype MockEthereumProvider = EventEmitter & {\n request: () => Promise<any>;\n};\n\n/**\n * Get a mock snap API, that always returns `true` for requests.\n *\n * @returns A mocked snap provider.\n */\nfunction getMockSnapGlobal(): MockSnapGlobal {\n // eslint-disable-next-line @typescript-eslint/require-await\n return { request: async () => true };\n}\n\n/**\n * Get a mock Ethereum provider, that always returns `true` for requests.\n *\n * @returns A mocked ethereum provider.\n */\nfunction getMockEthereumProvider(): MockEthereumProvider {\n const mockProvider = new EventEmitter() as Partial<MockEthereumProvider>;\n // eslint-disable-next-line @typescript-eslint/require-await\n mockProvider.request = async () => true;\n return mockProvider as MockEthereumProvider;\n}\n\n/**\n * Check if a value is a constructor.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nexport const isConstructor = (value: any) =>\n Boolean(typeof value?.prototype?.constructor?.name === 'string');\n\n/**\n * A function that always returns `true`.\n *\n * @returns `true`.\n */\nconst mockFunction = () => true;\nclass MockClass {}\n\nconst handler = {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n construct(Target: any, args: any[]): any {\n return new Proxy(new Target(...args), handler);\n },\n get(_target: any, _prop: any) {\n return mockFunction;\n },\n};\n\n/**\n * Generate a mock class for a given value. The value is wrapped in a Proxy, and\n * all methods are replaced with a mock function.\n *\n * @param value - The value to mock.\n * @returns A mock class.\n */\nconst generateMockClass = (value: any) => {\n return new Proxy(value, handler);\n};\n\n// Things not currently auto-mocked because of NodeJS, by adding them here we\n// have types for them and can use that to generate mocks if needed.\nconst mockWindow: Record<string, unknown> = {\n crypto,\n SubtleCrypto: MockClass,\n};\n\n/**\n * Generate a mock endowment for a certain class or function on the `globalThis`\n * object.\n *\n * @param key - The key to generate the mock endowment for.\n * @returns A mocked class or function. If the key is part of the default\n * endowments, the original value is returned.\n */\nconst generateMockEndowment = (key: string) => {\n const globalValue = (globalThis as any)[key];\n\n // Default exposed APIs don't need to be mocked\n if (globalValue && DEFAULT_ENDOWMENTS.includes(key)) {\n return globalValue;\n }\n\n // Fall back to mockWindow for certain APIs not exposed in global in Node.JS\n const globalOrMocked = globalValue ?? mockWindow[key];\n\n const type = typeof globalOrMocked;\n const isFunction = type === 'function';\n if (isFunction && isConstructor(globalOrMocked)) {\n return generateMockClass(globalOrMocked);\n } else if (isFunction || !globalOrMocked) {\n // Fall back to function mock for now\n return mockFunction;\n }\n return globalOrMocked;\n};\n\n/**\n * Generate mock endowments for all the APIs as defined in {@link ALL_APIS}.\n *\n * @returns A map of endowments.\n */\nexport const generateMockEndowments = () => {\n return ALL_APIS.reduce<Record<string, any>>(\n (acc, cur) => ({ ...acc, [cur]: generateMockEndowment(cur) }),\n { snap: getMockSnapGlobal(), ethereum: getMockEthereumProvider() },\n );\n};\n"]}
|
package/dist/namespace.d.ts
CHANGED
|
@@ -94,8 +94,8 @@ export declare const RequestNamespaceStruct: import("superstruct").Struct<{
|
|
|
94
94
|
}>;
|
|
95
95
|
export declare type RequestNamespace = Infer<typeof RequestNamespaceStruct>;
|
|
96
96
|
export declare const SessionNamespaceStruct: import("superstruct").Struct<{
|
|
97
|
-
accounts: string[];
|
|
98
97
|
chains: string[];
|
|
98
|
+
accounts: string[];
|
|
99
99
|
methods?: string[] | undefined;
|
|
100
100
|
events?: string[] | undefined;
|
|
101
101
|
}, {
|
|
@@ -124,15 +124,15 @@ export declare const NamespacesStruct: import("superstruct").Struct<Record<strin
|
|
|
124
124
|
export declare type Namespaces = Infer<typeof NamespacesStruct>;
|
|
125
125
|
export declare const SessionStruct: import("superstruct").Struct<{
|
|
126
126
|
namespaces: Record<string, {
|
|
127
|
-
accounts: string[];
|
|
128
127
|
chains: string[];
|
|
128
|
+
accounts: string[];
|
|
129
129
|
methods?: string[] | undefined;
|
|
130
130
|
events?: string[] | undefined;
|
|
131
131
|
}>;
|
|
132
132
|
}, {
|
|
133
133
|
namespaces: import("superstruct").Struct<Record<string, {
|
|
134
|
-
accounts: string[];
|
|
135
134
|
chains: string[];
|
|
135
|
+
accounts: string[];
|
|
136
136
|
methods?: string[] | undefined;
|
|
137
137
|
events?: string[] | undefined;
|
|
138
138
|
}>, null>;
|