@metamask/snaps-utils 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -218
- package/dist/cjs/array.js +23 -0
- package/dist/cjs/array.js.map +1 -0
- package/dist/cjs/caveats.js +36 -0
- package/dist/cjs/caveats.js.map +1 -0
- package/dist/cjs/checksum.js +38 -0
- package/dist/cjs/checksum.js.map +1 -0
- package/dist/cjs/cronjob.js +84 -0
- package/dist/cjs/cronjob.js.map +1 -0
- package/dist/cjs/deep-clone.js +22 -0
- package/dist/cjs/deep-clone.js.map +1 -0
- package/dist/cjs/default-endowments.js +49 -0
- package/dist/cjs/default-endowments.js.map +1 -0
- package/dist/cjs/entropy.js +23 -0
- package/dist/cjs/entropy.js.map +1 -0
- package/dist/cjs/enum.js +16 -0
- package/dist/cjs/enum.js.map +1 -0
- package/dist/cjs/errors.js +19 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/{eval-worker.js → cjs/eval-worker.js} +22 -21
- package/dist/cjs/eval-worker.js.map +1 -0
- package/dist/cjs/eval.js +77 -0
- package/dist/cjs/eval.js.map +1 -0
- package/dist/cjs/fs.js +126 -0
- package/dist/cjs/fs.js.map +1 -0
- package/dist/cjs/handlers.js +80 -0
- package/dist/cjs/handlers.js.map +1 -0
- package/dist/cjs/icon.js +37 -0
- package/dist/cjs/icon.js.map +1 -0
- package/dist/cjs/iframe.js +59 -0
- package/dist/cjs/iframe.js.map +1 -0
- package/dist/cjs/index.browser.js +43 -0
- package/dist/cjs/index.browser.js.map +1 -0
- package/dist/cjs/index.executionenv.js +24 -0
- package/dist/cjs/index.executionenv.js.map +1 -0
- package/dist/cjs/index.js +48 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/json-rpc.js +46 -0
- package/dist/cjs/json-rpc.js.map +1 -0
- package/dist/cjs/json.js +16 -0
- package/dist/cjs/json.js.map +1 -0
- package/dist/cjs/logging.js +40 -0
- package/dist/cjs/logging.js.map +1 -0
- package/dist/cjs/manifest/index.browser.js +20 -0
- package/dist/cjs/manifest/index.browser.js.map +1 -0
- package/dist/cjs/manifest/index.js +21 -0
- package/dist/cjs/manifest/index.js.map +1 -0
- package/dist/cjs/manifest/manifest.js +239 -0
- package/dist/cjs/manifest/manifest.js.map +1 -0
- package/dist/cjs/manifest/validation.js +189 -0
- package/dist/cjs/manifest/validation.js.map +1 -0
- package/dist/cjs/mock.js +128 -0
- package/dist/cjs/mock.js.map +1 -0
- package/dist/cjs/namespace.js +132 -0
- package/dist/cjs/namespace.js.map +1 -0
- package/dist/cjs/npm.js +81 -0
- package/dist/cjs/npm.js.map +1 -0
- package/dist/cjs/path.js +21 -0
- package/dist/cjs/path.js.map +1 -0
- package/dist/cjs/post-process.js +328 -0
- package/dist/cjs/post-process.js.map +1 -0
- package/dist/cjs/snaps.js +230 -0
- package/dist/cjs/snaps.js.map +1 -0
- package/dist/cjs/strings.js +21 -0
- package/dist/cjs/strings.js.map +1 -0
- package/dist/cjs/structs.js +163 -0
- package/dist/cjs/structs.js.map +1 -0
- package/dist/cjs/types.js +109 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/validation.js +22 -0
- package/dist/cjs/validation.js.map +1 -0
- package/dist/cjs/versions.js +47 -0
- package/dist/cjs/versions.js.map +1 -0
- package/dist/cjs/virtual-file/VirtualFile.js +85 -0
- package/dist/cjs/virtual-file/VirtualFile.js.map +1 -0
- package/dist/cjs/virtual-file/index.browser.js +20 -0
- package/dist/cjs/virtual-file/index.browser.js.map +1 -0
- package/dist/cjs/virtual-file/index.js +21 -0
- package/dist/cjs/virtual-file/index.js.map +1 -0
- package/dist/cjs/virtual-file/toVirtualFile.js +33 -0
- package/dist/cjs/virtual-file/toVirtualFile.js.map +1 -0
- package/dist/{array.js → esm/array.js} +3 -7
- package/dist/esm/array.js.map +1 -0
- package/dist/esm/caveats.js +26 -0
- package/dist/esm/caveats.js.map +1 -0
- package/dist/esm/checksum.js +36 -0
- package/dist/esm/checksum.js.map +1 -0
- package/dist/esm/cronjob.js +66 -0
- package/dist/esm/cronjob.js.map +1 -0
- package/dist/esm/deep-clone.js +7 -0
- package/dist/esm/deep-clone.js.map +1 -0
- package/dist/{default-endowments.js → esm/default-endowments.js} +3 -6
- package/dist/esm/default-endowments.js.map +1 -0
- package/dist/esm/entropy.js +6 -0
- package/dist/esm/entropy.js.map +1 -0
- package/dist/esm/enum.js +12 -0
- package/dist/esm/enum.js.map +1 -0
- package/dist/esm/errors.js +17 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/eval-worker.js +47 -0
- package/dist/esm/eval-worker.js.map +1 -0
- package/dist/esm/eval.js +65 -0
- package/dist/esm/eval.js.map +1 -0
- package/dist/{fs.js → esm/fs.js} +33 -56
- package/dist/esm/fs.js.map +1 -0
- package/dist/esm/handlers.js +59 -0
- package/dist/esm/handlers.js.map +1 -0
- package/dist/esm/icon.js +11 -0
- package/dist/esm/icon.js.map +1 -0
- package/dist/{iframe.js → esm/iframe.js} +16 -18
- package/dist/esm/iframe.js.map +1 -0
- package/dist/esm/index.browser.js +26 -0
- package/dist/esm/index.browser.js.map +1 -0
- package/dist/esm/index.executionenv.js +7 -0
- package/dist/esm/index.executionenv.js.map +1 -0
- package/dist/esm/index.js +31 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/json-rpc.js +39 -0
- package/dist/esm/json-rpc.js.map +1 -0
- package/dist/esm/json.js +17 -0
- package/dist/esm/json.js.map +1 -0
- package/dist/{logging.js → esm/logging.js} +6 -14
- package/dist/esm/logging.js.map +1 -0
- package/dist/esm/manifest/index.browser.js +3 -0
- package/dist/esm/manifest/index.browser.js.map +1 -0
- package/dist/esm/manifest/index.js +4 -0
- package/dist/esm/manifest/index.js.map +1 -0
- package/dist/{manifest → esm/manifest}/manifest.js +101 -115
- package/dist/esm/manifest/manifest.js.map +1 -0
- package/dist/esm/manifest/validation.js +155 -0
- package/dist/esm/manifest/validation.js.map +1 -0
- package/dist/{mock.js → esm/mock.js} +42 -40
- package/dist/esm/mock.js.map +1 -0
- package/dist/esm/namespace.js +112 -0
- package/dist/esm/namespace.js.map +1 -0
- package/dist/esm/npm.js +70 -0
- package/dist/esm/npm.js.map +1 -0
- package/dist/esm/path.js +17 -0
- package/dist/esm/path.js.map +1 -0
- package/dist/{post-process.js → esm/post-process.js} +110 -99
- package/dist/esm/post-process.js.map +1 -0
- package/dist/esm/snaps.js +215 -0
- package/dist/esm/snaps.js.map +1 -0
- package/dist/esm/strings.js +11 -0
- package/dist/esm/strings.js.map +1 -0
- package/dist/esm/structs.js +230 -0
- package/dist/esm/structs.js.map +1 -0
- package/dist/esm/types.js +80 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/validation.js +17 -0
- package/dist/esm/validation.js.map +1 -0
- package/dist/{versions.js → esm/versions.js} +15 -18
- package/dist/esm/versions.js.map +1 -0
- package/dist/{virtual-file → esm/virtual-file}/VirtualFile.js +47 -33
- package/dist/esm/virtual-file/VirtualFile.js.map +1 -0
- package/dist/esm/virtual-file/index.browser.js +3 -0
- package/dist/esm/virtual-file/index.browser.js.map +1 -0
- package/dist/esm/virtual-file/index.js +4 -0
- package/dist/esm/virtual-file/index.js.map +1 -0
- package/dist/esm/virtual-file/toVirtualFile.js +26 -0
- package/dist/esm/virtual-file/toVirtualFile.js.map +1 -0
- package/dist/{caveats.d.ts → types/caveats.d.ts} +5 -5
- package/dist/{cronjob.d.ts → types/cronjob.d.ts} +1 -1
- package/dist/types/enum.d.ts +30 -0
- package/dist/types/errors.d.ts +10 -0
- package/dist/{eval.d.ts → types/eval.d.ts} +9 -1
- package/dist/{fs.d.ts → types/fs.d.ts} +2 -2
- package/dist/types/handlers.d.ts +196 -0
- package/dist/types/icon.d.ts +4 -0
- package/dist/{iframe.d.ts → types/iframe.d.ts} +3 -2
- package/dist/{index.browser.d.ts → types/index.browser.d.ts} +5 -1
- package/dist/{index.d.ts → types/index.d.ts} +5 -1
- package/dist/{json-rpc.d.ts → types/json-rpc.d.ts} +2 -2
- package/dist/types/json.d.ts +13 -0
- package/dist/{manifest → types/manifest}/manifest.d.ts +6 -4
- package/dist/{manifest → types/manifest}/validation.d.ts +40 -76
- package/dist/types/namespace.d.ts +128 -0
- package/dist/{npm.d.ts → types/npm.d.ts} +2 -3
- package/dist/{snaps.d.ts → types/snaps.d.ts} +17 -9
- package/dist/types/strings.d.ts +8 -0
- package/dist/types/structs.d.ts +158 -0
- package/dist/{types.d.ts → types/types.d.ts} +15 -22
- package/dist/types/validation.d.ts +8 -0
- package/dist/{versions.d.ts → types/versions.d.ts} +1 -1
- package/package.json +66 -46
- package/dist/array.js.map +0 -1
- package/dist/caveats.js +0 -35
- package/dist/caveats.js.map +0 -1
- package/dist/checksum.js +0 -42
- package/dist/checksum.js.map +0 -1
- package/dist/cronjob.js +0 -71
- package/dist/cronjob.js.map +0 -1
- package/dist/deep-clone.js +0 -9
- package/dist/deep-clone.js.map +0 -1
- package/dist/default-endowments.js.map +0 -1
- package/dist/entropy.js +0 -8
- package/dist/entropy.js.map +0 -1
- package/dist/eval-worker.js.map +0 -1
- package/dist/eval.js +0 -27
- package/dist/eval.js.map +0 -1
- package/dist/fs.js.map +0 -1
- package/dist/handlers.d.ts +0 -105
- package/dist/handlers.js +0 -3
- package/dist/handlers.js.map +0 -1
- package/dist/iframe.js.map +0 -1
- package/dist/iframe.test.browser.js +0 -15
- package/dist/iframe.test.browser.js.map +0 -1
- package/dist/index.browser.js +0 -37
- package/dist/index.browser.js.map +0 -1
- package/dist/index.executionenv.js +0 -22
- package/dist/index.executionenv.js.map +0 -1
- package/dist/index.js +0 -42
- package/dist/index.js.map +0 -1
- package/dist/json-rpc.js +0 -46
- package/dist/json-rpc.js.map +0 -1
- package/dist/json.d.ts +0 -9
- package/dist/json.js +0 -18
- package/dist/json.js.map +0 -1
- package/dist/logging.js.map +0 -1
- package/dist/manifest/index.browser.js +0 -18
- package/dist/manifest/index.browser.js.map +0 -1
- package/dist/manifest/index.js +0 -19
- package/dist/manifest/index.js.map +0 -1
- package/dist/manifest/manifest.js.map +0 -1
- package/dist/manifest/validation.js +0 -141
- package/dist/manifest/validation.js.map +0 -1
- package/dist/mock.js.map +0 -1
- package/dist/namespace.d.ts +0 -275
- package/dist/namespace.js +0 -225
- package/dist/namespace.js.map +0 -1
- package/dist/notification.d.ts +0 -66
- package/dist/notification.js +0 -58
- package/dist/notification.js.map +0 -1
- package/dist/npm.js +0 -74
- package/dist/npm.js.map +0 -1
- package/dist/path.js +0 -21
- package/dist/path.js.map +0 -1
- package/dist/post-process.js.map +0 -1
- package/dist/snaps.js +0 -202
- package/dist/snaps.js.map +0 -1
- package/dist/types.js +0 -103
- package/dist/types.js.map +0 -1
- package/dist/versions.js.map +0 -1
- package/dist/virtual-file/VirtualFile.js.map +0 -1
- package/dist/virtual-file/index.browser.js +0 -18
- package/dist/virtual-file/index.browser.js.map +0 -1
- package/dist/virtual-file/index.js +0 -19
- package/dist/virtual-file/index.js.map +0 -1
- package/dist/virtual-file/toVirtualFile.js +0 -30
- package/dist/virtual-file/toVirtualFile.js.map +0 -1
- /package/dist/{array.d.ts → types/array.d.ts} +0 -0
- /package/dist/{checksum.d.ts → types/checksum.d.ts} +0 -0
- /package/dist/{deep-clone.d.ts → types/deep-clone.d.ts} +0 -0
- /package/dist/{default-endowments.d.ts → types/default-endowments.d.ts} +0 -0
- /package/dist/{entropy.d.ts → types/entropy.d.ts} +0 -0
- /package/dist/{eval-worker.d.ts → types/eval-worker.d.ts} +0 -0
- /package/dist/{iframe.test.browser.d.ts → types/iframe.test.browser.d.ts} +0 -0
- /package/dist/{index.executionenv.d.ts → types/index.executionenv.d.ts} +0 -0
- /package/dist/{logging.d.ts → types/logging.d.ts} +0 -0
- /package/dist/{manifest → types/manifest}/index.browser.d.ts +0 -0
- /package/dist/{manifest → types/manifest}/index.d.ts +0 -0
- /package/dist/{mock.d.ts → types/mock.d.ts} +0 -0
- /package/dist/{path.d.ts → types/path.d.ts} +0 -0
- /package/dist/{post-process.d.ts → types/post-process.d.ts} +0 -0
- /package/dist/{virtual-file → types/virtual-file}/VirtualFile.d.ts +0 -0
- /package/dist/{virtual-file → types/virtual-file}/index.browser.d.ts +0 -0
- /package/dist/{virtual-file → types/virtual-file}/index.d.ts +0 -0
- /package/dist/{virtual-file → types/virtual-file}/toVirtualFile.d.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/validation.ts"],"sourcesContent":["import { assertIsSnapIcon } from './icon';\nimport { assertIsSnapManifest } from './manifest/validation';\nimport { validateSnapShasum } from './snaps';\nimport type { SnapFiles } from './types';\n\n/**\n * Validates the files contained in a fetched snap.\n *\n * @param files - All potentially included files in a fetched snap.\n * @throws If any of the files are considered invalid.\n */\nexport function validateFetchedSnap(\n files: Pick<SnapFiles, 'manifest' | 'sourceCode' | 'svgIcon'>,\n): void {\n assertIsSnapManifest(files.manifest.result);\n validateSnapShasum(files);\n\n if (files.svgIcon) {\n assertIsSnapIcon(files.svgIcon);\n }\n}\n"],"names":["assertIsSnapIcon","assertIsSnapManifest","validateSnapShasum","validateFetchedSnap","files","manifest","result","svgIcon"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,SAAS;AAC1C,SAASC,oBAAoB,QAAQ,wBAAwB;AAC7D,SAASC,kBAAkB,QAAQ,UAAU;AAG7C;;;;;CAKC,GACD,OAAO,SAASC,oBACdC,KAA6D;IAE7DH,qBAAqBG,MAAMC,QAAQ,CAACC,MAAM;IAC1CJ,mBAAmBE;IAEnB,IAAIA,MAAMG,OAAO,EAAE;QACjBP,iBAAiBI,MAAMG,OAAO;IAChC;AACF"}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const semver_1 = require("semver");
|
|
6
|
-
const superstruct_1 = require("superstruct");
|
|
7
|
-
exports.DEFAULT_REQUESTED_SNAP_VERSION = '*';
|
|
1
|
+
import { VersionRangeStruct } from '@metamask/utils';
|
|
2
|
+
import { maxSatisfying as maxSatisfyingSemver } from 'semver';
|
|
3
|
+
import { validate } from 'superstruct';
|
|
4
|
+
export const DEFAULT_REQUESTED_SNAP_VERSION = '*';
|
|
8
5
|
/**
|
|
9
6
|
* Return the highest version in the list that satisfies the range, or `null` if
|
|
10
7
|
* none of them do. For the satisfaction check, pre-release versions will only
|
|
@@ -14,31 +11,31 @@ exports.DEFAULT_REQUESTED_SNAP_VERSION = '*';
|
|
|
14
11
|
* @param versionRange - The SemVer version range to check against.
|
|
15
12
|
* @returns The highest version in the list that satisfies the range,
|
|
16
13
|
* or `null` if none of them do.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
const maxSatisfyingNonPreRelease = (0, semver_1.maxSatisfying)(versions, versionRange);
|
|
14
|
+
*/ export function getTargetVersion(versions, versionRange) {
|
|
15
|
+
const maxSatisfyingNonPreRelease = maxSatisfyingSemver(versions, versionRange);
|
|
20
16
|
// By default don't use pre-release versions
|
|
21
17
|
if (maxSatisfyingNonPreRelease) {
|
|
22
18
|
return maxSatisfyingNonPreRelease;
|
|
23
19
|
}
|
|
24
20
|
// If no satisfying release version is found by default, try pre-release versions
|
|
25
|
-
return (
|
|
26
|
-
includePrerelease: true
|
|
21
|
+
return maxSatisfyingSemver(versions, versionRange, {
|
|
22
|
+
includePrerelease: true
|
|
27
23
|
});
|
|
28
24
|
}
|
|
29
|
-
exports.getTargetVersion = getTargetVersion;
|
|
30
25
|
/**
|
|
31
26
|
* Parse a version received by some subject attempting to access a snap.
|
|
32
27
|
*
|
|
33
28
|
* @param version - The received version value.
|
|
34
29
|
* @returns `*` if the version is `undefined` or `latest", otherwise returns
|
|
35
30
|
* the specified version.
|
|
36
|
-
*/
|
|
37
|
-
function resolveVersionRange(version) {
|
|
31
|
+
*/ export function resolveVersionRange(version) {
|
|
38
32
|
if (version === undefined || version === 'latest') {
|
|
39
|
-
return [
|
|
33
|
+
return [
|
|
34
|
+
undefined,
|
|
35
|
+
DEFAULT_REQUESTED_SNAP_VERSION
|
|
36
|
+
];
|
|
40
37
|
}
|
|
41
|
-
return
|
|
38
|
+
return validate(version, VersionRangeStruct);
|
|
42
39
|
}
|
|
43
|
-
|
|
40
|
+
|
|
44
41
|
//# sourceMappingURL=versions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/versions.ts"],"sourcesContent":["import type { Json, SemVerVersion, SemVerRange } from '@metamask/utils';\nimport { VersionRangeStruct } from '@metamask/utils';\nimport { maxSatisfying as maxSatisfyingSemver } from 'semver';\nimport { validate } from 'superstruct';\n\nexport const DEFAULT_REQUESTED_SNAP_VERSION = '*' as SemVerRange;\n\n/**\n * Return the highest version in the list that satisfies the range, or `null` if\n * none of them do. For the satisfaction check, pre-release versions will only\n * be checked if no satisfactory non-prerelease version is found first.\n *\n * @param versions - The list of version to check.\n * @param versionRange - The SemVer version range to check against.\n * @returns The highest version in the list that satisfies the range,\n * or `null` if none of them do.\n */\nexport function getTargetVersion(\n versions: SemVerVersion[],\n versionRange: SemVerRange,\n): SemVerVersion | null {\n const maxSatisfyingNonPreRelease = maxSatisfyingSemver(\n versions,\n versionRange,\n );\n\n // By default don't use pre-release versions\n if (maxSatisfyingNonPreRelease) {\n return maxSatisfyingNonPreRelease;\n }\n\n // If no satisfying release version is found by default, try pre-release versions\n return maxSatisfyingSemver(versions, versionRange, {\n includePrerelease: true,\n });\n}\n\n/**\n * Parse a version received by some subject attempting to access a snap.\n *\n * @param version - The received version value.\n * @returns `*` if the version is `undefined` or `latest\", otherwise returns\n * the specified version.\n */\nexport function resolveVersionRange(\n version?: Json,\n): [error: undefined, range: SemVerRange] | [error: Error, range: undefined] {\n if (version === undefined || version === 'latest') {\n return [undefined, DEFAULT_REQUESTED_SNAP_VERSION];\n }\n return validate(version, VersionRangeStruct);\n}\n"],"names":["VersionRangeStruct","maxSatisfying","maxSatisfyingSemver","validate","DEFAULT_REQUESTED_SNAP_VERSION","getTargetVersion","versions","versionRange","maxSatisfyingNonPreRelease","includePrerelease","resolveVersionRange","version","undefined"],"mappings":"AACA,SAASA,kBAAkB,QAAQ,kBAAkB;AACrD,SAASC,iBAAiBC,mBAAmB,QAAQ,SAAS;AAC9D,SAASC,QAAQ,QAAQ,cAAc;AAEvC,OAAO,MAAMC,iCAAiC,IAAmB;AAEjE;;;;;;;;;CASC,GACD,OAAO,SAASC,iBACdC,QAAyB,EACzBC,YAAyB;IAEzB,MAAMC,6BAA6BN,oBACjCI,UACAC;IAGF,4CAA4C;IAC5C,IAAIC,4BAA4B;QAC9B,OAAOA;IACT;IAEA,iFAAiF;IACjF,OAAON,oBAAoBI,UAAUC,cAAc;QACjDE,mBAAmB;IACrB;AACF;AAEA;;;;;;CAMC,GACD,OAAO,SAASC,oBACdC,OAAc;IAEd,IAAIA,YAAYC,aAAaD,YAAY,UAAU;QACjD,OAAO;YAACC;YAAWR;SAA+B;IACpD;IACA,OAAOD,SAASQ,SAASX;AAC3B"}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.VirtualFile = void 0;
|
|
4
1
|
// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review
|
|
5
2
|
// TODO(ritave): Streaming vfile contents similar to vinyl maybe?
|
|
6
3
|
// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile
|
|
@@ -9,34 +6,25 @@ exports.VirtualFile = void 0;
|
|
|
9
6
|
// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.
|
|
10
7
|
// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because
|
|
11
8
|
// we're calculating checksums based on original path.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
this.value = options?.value ?? '';
|
|
24
|
-
// This situations happens when there's no .result used,
|
|
25
|
-
// we expect the file to have default generic in that situation:
|
|
26
|
-
// VirtualFile<unknown> which will handle undefined properly
|
|
27
|
-
//
|
|
28
|
-
// While not 100% type safe, it'll be way less frustrating to work with.
|
|
29
|
-
// The alternative would be to have VirtualFile.result be Result | undefined
|
|
30
|
-
// and that would result in needing to branch out and check in all situations.
|
|
31
|
-
//
|
|
32
|
-
// In short, optimizing for most common use case.
|
|
33
|
-
this.result = options?.result ?? undefined;
|
|
34
|
-
this.data = options?.data ?? {};
|
|
35
|
-
this.path = options?.path ?? '/';
|
|
9
|
+
function _define_property(obj, key, value) {
|
|
10
|
+
if (key in obj) {
|
|
11
|
+
Object.defineProperty(obj, key, {
|
|
12
|
+
value: value,
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
obj[key] = value;
|
|
36
19
|
}
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
import { assert } from '@metamask/utils';
|
|
23
|
+
import { deepClone } from '../deep-clone';
|
|
24
|
+
export class VirtualFile {
|
|
37
25
|
toString(encoding) {
|
|
38
26
|
if (typeof this.value === 'string') {
|
|
39
|
-
|
|
27
|
+
assert(encoding === undefined, 'Tried to encode string.');
|
|
40
28
|
return this.value;
|
|
41
29
|
}
|
|
42
30
|
const decoder = new TextDecoder(encoding);
|
|
@@ -46,16 +34,42 @@ class VirtualFile {
|
|
|
46
34
|
const vfile = new VirtualFile();
|
|
47
35
|
if (typeof this.value === 'string') {
|
|
48
36
|
vfile.value = this.value;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
37
|
+
} else {
|
|
51
38
|
// deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array
|
|
52
39
|
vfile.value = this.value.slice(0);
|
|
53
40
|
}
|
|
54
|
-
vfile.result =
|
|
55
|
-
vfile.data =
|
|
41
|
+
vfile.result = deepClone(this.result);
|
|
42
|
+
vfile.data = deepClone(this.data);
|
|
56
43
|
vfile.path = this.path;
|
|
57
44
|
return vfile;
|
|
58
45
|
}
|
|
46
|
+
constructor(value){
|
|
47
|
+
_define_property(this, "value", void 0);
|
|
48
|
+
_define_property(this, "result", void 0);
|
|
49
|
+
_define_property(this, "data", void 0);
|
|
50
|
+
_define_property(this, "path", void 0);
|
|
51
|
+
let options;
|
|
52
|
+
if (typeof value === 'string' || value instanceof Uint8Array) {
|
|
53
|
+
options = {
|
|
54
|
+
value
|
|
55
|
+
};
|
|
56
|
+
} else {
|
|
57
|
+
options = value;
|
|
58
|
+
}
|
|
59
|
+
this.value = options?.value ?? '';
|
|
60
|
+
// This situations happens when there's no .result used,
|
|
61
|
+
// we expect the file to have default generic in that situation:
|
|
62
|
+
// VirtualFile<unknown> which will handle undefined properly
|
|
63
|
+
//
|
|
64
|
+
// While not 100% type safe, it'll be way less frustrating to work with.
|
|
65
|
+
// The alternative would be to have VirtualFile.result be Result | undefined
|
|
66
|
+
// and that would result in needing to branch out and check in all situations.
|
|
67
|
+
//
|
|
68
|
+
// In short, optimizing for most common use case.
|
|
69
|
+
this.result = options?.result ?? undefined;
|
|
70
|
+
this.data = options?.data ?? {};
|
|
71
|
+
this.path = options?.path ?? '/';
|
|
72
|
+
}
|
|
59
73
|
}
|
|
60
|
-
|
|
74
|
+
|
|
61
75
|
//# sourceMappingURL=VirtualFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/virtual-file/VirtualFile.ts"],"sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert } from '@metamask/utils';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-interface\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"],"names":["assert","deepClone","VirtualFile","toString","encoding","value","undefined","decoder","TextDecoder","decode","clone","vfile","slice","result","data","path","constructor","options","Uint8Array"],"mappings":"AAAA,0HAA0H;AAC1H,iEAAiE;AACjE,2FAA2F;AAC3F,wFAAwF;AACxF,EAAE;AACF,oHAAoH;AACpH,qGAAqG;AACrG,sDAAsD;;;;;;;;;;;;;;AACtD,SAASA,MAAM,QAAQ,kBAAkB;AAEzC,SAASC,SAAS,QAAQ,gBAAgB;AA+B1C,OAAO,MAAMC;IAgCXC,SAASC,QAAiB,EAAE;QAC1B,IAAI,OAAO,IAAI,CAACC,KAAK,KAAK,UAAU;YAClCL,OAAOI,aAAaE,WAAW;YAC/B,OAAO,IAAI,CAACD,KAAK;QACnB;QACA,MAAME,UAAU,IAAIC,YAAYJ;QAChC,OAAOG,QAAQE,MAAM,CAAC,IAAI,CAACJ,KAAK;IAClC;IAEAK,QAAQ;QACN,MAAMC,QAAQ,IAAIT;QAClB,IAAI,OAAO,IAAI,CAACG,KAAK,KAAK,UAAU;YAClCM,MAAMN,KAAK,GAAG,IAAI,CAACA,KAAK;QAC1B,OAAO;YACL,mFAAmF;YACnFM,MAAMN,KAAK,GAAG,IAAI,CAACA,KAAK,CAACO,KAAK,CAAC;QACjC;QACAD,MAAME,MAAM,GAAGZ,UAAU,IAAI,CAACY,MAAM;QACpCF,MAAMG,IAAI,GAAGb,UAAU,IAAI,CAACa,IAAI;QAChCH,MAAMI,IAAI,GAAG,IAAI,CAACA,IAAI;QACtB,OAAOJ;IACT;IApDAK,YAAYX,KAA0B,CAAE;QAuBxCA,uBAAAA,SAAAA,KAAAA;QAEAQ,uBAAAA,UAAAA,KAAAA;QAEAC,uBAAAA,QAAAA,KAAAA;QAEAC,uBAAAA,QAAAA,KAAAA;QA5BE,IAAIE;QACJ,IAAI,OAAOZ,UAAU,YAAYA,iBAAiBa,YAAY;YAC5DD,UAAU;gBAAEZ;YAAM;QACpB,OAAO;YACLY,UAAUZ;QACZ;QAEA,IAAI,CAACA,KAAK,GAAGY,SAASZ,SAAS;QAC/B,wDAAwD;QACxD,gEAAgE;QAChE,4DAA4D;QAC5D,EAAE;QACF,wEAAwE;QACxE,4EAA4E;QAC5E,8EAA8E;QAC9E,EAAE;QACF,iDAAiD;QACjD,IAAI,CAACQ,MAAM,GAAGI,SAASJ,UAAWP;QAClC,IAAI,CAACQ,IAAI,GAAGG,SAASH,QAAQ,CAAC;QAC9B,IAAI,CAACC,IAAI,GAAGE,SAASF,QAAQ;IAC/B;AAgCF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/virtual-file/index.browser.ts"],"sourcesContent":["export * from './VirtualFile';\n"],"names":[],"mappings":"AAAA,cAAc,gBAAgB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/virtual-file/index.ts"],"sourcesContent":["export * from './toVirtualFile';\nexport * from './VirtualFile';\n"],"names":[],"mappings":"AAAA,cAAc,kBAAkB;AAChC,cAAc,gBAAgB"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { promises as fsPromises } from 'fs';
|
|
2
|
+
import { VirtualFile } from './VirtualFile';
|
|
3
|
+
/**
|
|
4
|
+
* Reads a file from filesystem and creates a vfile.
|
|
5
|
+
*
|
|
6
|
+
* @param path - Filesystem path to load the contents from.
|
|
7
|
+
* @param encoding - Optional encoding to pass down to fs.readFile.
|
|
8
|
+
* @returns Promise returning VFile with loaded file contents.
|
|
9
|
+
*/ export async function readVirtualFile(path, encoding = null) {
|
|
10
|
+
return new VirtualFile({
|
|
11
|
+
path,
|
|
12
|
+
value: await fsPromises.readFile(path, {
|
|
13
|
+
encoding
|
|
14
|
+
})
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Writes vfile to filesystem.
|
|
19
|
+
*
|
|
20
|
+
* @param vfile - The vfile to write.
|
|
21
|
+
* @param options - Options to pass down to fs.writeFile.
|
|
22
|
+
*/ export async function writeVirtualFile(vfile, options) {
|
|
23
|
+
return fsPromises.writeFile(vfile.path, vfile.value, options);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=toVirtualFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/virtual-file/toVirtualFile.ts"],"sourcesContent":["import { promises as fsPromises } from 'fs';\n\nimport { VirtualFile } from './VirtualFile';\n\n/**\n * Reads a file from filesystem and creates a vfile.\n *\n * @param path - Filesystem path to load the contents from.\n * @param encoding - Optional encoding to pass down to fs.readFile.\n * @returns Promise returning VFile with loaded file contents.\n */\nexport async function readVirtualFile(\n path: string,\n encoding: BufferEncoding | null = null,\n) {\n return new VirtualFile({\n path,\n value: await fsPromises.readFile(path, { encoding }),\n });\n}\n\ntype WriteVFileOptions = Exclude<\n Parameters<typeof fsPromises['writeFile']>[2],\n undefined\n>;\n\n/**\n * Writes vfile to filesystem.\n *\n * @param vfile - The vfile to write.\n * @param options - Options to pass down to fs.writeFile.\n */\nexport async function writeVirtualFile(\n vfile: VirtualFile,\n options?: WriteVFileOptions,\n) {\n return fsPromises.writeFile(vfile.path, vfile.value, options);\n}\n"],"names":["promises","fsPromises","VirtualFile","readVirtualFile","path","encoding","value","readFile","writeVirtualFile","vfile","options","writeFile"],"mappings":"AAAA,SAASA,YAAYC,UAAU,QAAQ,KAAK;AAE5C,SAASC,WAAW,QAAQ,gBAAgB;AAE5C;;;;;;CAMC,GACD,OAAO,eAAeC,gBACpBC,IAAY,EACZC,WAAkC,IAAI;IAEtC,OAAO,IAAIH,YAAY;QACrBE;QACAE,OAAO,MAAML,WAAWM,QAAQ,CAACH,MAAM;YAAEC;QAAS;IACpD;AACF;AAOA;;;;;CAKC,GACD,OAAO,eAAeG,iBACpBC,KAAkB,EAClBC,OAA2B;IAE3B,OAAOT,WAAWU,SAAS,CAACF,MAAML,IAAI,EAAEK,MAAMH,KAAK,EAAEI;AACvD"}
|
|
@@ -7,10 +7,6 @@ export declare enum SnapCaveatType {
|
|
|
7
7
|
* Permitted coin types, used by `snap_getBip44Entropy`.
|
|
8
8
|
*/
|
|
9
9
|
PermittedCoinTypes = "permittedCoinTypes",
|
|
10
|
-
/**
|
|
11
|
-
* Permission to use the Snap keyring API.
|
|
12
|
-
*/
|
|
13
|
-
SnapKeyring = "snapKeyring",
|
|
14
10
|
/**
|
|
15
11
|
* Caveat specifying a snap cronjob.
|
|
16
12
|
*/
|
|
@@ -26,5 +22,9 @@ export declare enum SnapCaveatType {
|
|
|
26
22
|
/**
|
|
27
23
|
* Caveat specifying the snap IDs that can be interacted with.
|
|
28
24
|
*/
|
|
29
|
-
SnapIds = "snapIds"
|
|
25
|
+
SnapIds = "snapIds",
|
|
26
|
+
/**
|
|
27
|
+
* Caveat specifying the CAIP-2 chain IDs that a snap can service, currently limited to `endowment:name-lookup`.
|
|
28
|
+
*/
|
|
29
|
+
ChainIds = "chainIds"
|
|
30
30
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Infer } from 'superstruct';
|
|
1
|
+
import type { Infer } from 'superstruct';
|
|
2
2
|
export declare const CronjobRpcRequestStruct: import("superstruct").Struct<{
|
|
3
3
|
method: string;
|
|
4
4
|
params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Struct } from 'superstruct';
|
|
2
|
+
/**
|
|
3
|
+
* Get the enum values as union type. This allows using both the enum string
|
|
4
|
+
* values and the enum itself as values.
|
|
5
|
+
*
|
|
6
|
+
* Note: This only works for string enums.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* enum Foo {
|
|
11
|
+
* Bar = 'bar',
|
|
12
|
+
* Baz = 'baz',
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* type FooValue = EnumToUnion<Foo>;
|
|
16
|
+
* // FooValue is 'bar' | 'baz'
|
|
17
|
+
*
|
|
18
|
+
* const foo: FooValue = Foo.Bar; // Works
|
|
19
|
+
* const foo: FooValue = 'bar'; // Also works
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare type EnumToUnion<Type extends string> = `${Type}`;
|
|
23
|
+
/**
|
|
24
|
+
* Superstruct struct for validating an enum value. This allows using both the
|
|
25
|
+
* enum string values and the enum itself as values.
|
|
26
|
+
*
|
|
27
|
+
* @param constant - The enum to validate against.
|
|
28
|
+
* @returns The superstruct struct.
|
|
29
|
+
*/
|
|
30
|
+
export declare function enumValue<Type extends string>(constant: Type): Struct<EnumToUnion<Type>, null>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the error message from an unknown error type.
|
|
3
|
+
*
|
|
4
|
+
* - If the error is an object with a `message` property, return the message.
|
|
5
|
+
* - Otherwise, return the error converted to a string.
|
|
6
|
+
*
|
|
7
|
+
* @param error - The error to get the message from.
|
|
8
|
+
* @returns The error message.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getErrorMessage(error: unknown): string;
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export declare type EvalOutput = {
|
|
2
|
+
stdout: string;
|
|
3
|
+
stderr: string;
|
|
4
|
+
};
|
|
5
|
+
export declare class SnapEvalError extends Error {
|
|
6
|
+
readonly output: EvalOutput;
|
|
7
|
+
constructor(message: string, output: EvalOutput);
|
|
8
|
+
}
|
|
1
9
|
/**
|
|
2
10
|
* Spawn a new process to run the provided bundle in.
|
|
3
11
|
*
|
|
@@ -5,4 +13,4 @@
|
|
|
5
13
|
* @returns `null` if the worker ran successfully.
|
|
6
14
|
* @throws If the worker failed to run successfully.
|
|
7
15
|
*/
|
|
8
|
-
export declare function evalBundle(bundlePath: string): Promise<
|
|
16
|
+
export declare function evalBundle(bundlePath: string): Promise<EvalOutput>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Json } from '@metamask/utils';
|
|
2
|
-
import { VirtualFile } from './virtual-file';
|
|
1
|
+
import type { Json } from '@metamask/utils';
|
|
2
|
+
import type { VirtualFile } from './virtual-file';
|
|
3
3
|
/**
|
|
4
4
|
* Checks whether the given path string resolves to an existing directory, and
|
|
5
5
|
* optionally creates the directory if it doesn't exist.
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { Component } from '@metamask/snaps-ui';
|
|
2
|
+
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
|
|
3
|
+
import type { EnumToUnion } from './enum';
|
|
4
|
+
import type { AccountAddress, Caip2ChainId } from './namespace';
|
|
5
|
+
export declare enum HandlerType {
|
|
6
|
+
OnRpcRequest = "onRpcRequest",
|
|
7
|
+
OnTransaction = "onTransaction",
|
|
8
|
+
OnCronjob = "onCronjob",
|
|
9
|
+
OnInstall = "onInstall",
|
|
10
|
+
OnUpdate = "onUpdate",
|
|
11
|
+
OnNameLookup = "onNameLookup"
|
|
12
|
+
}
|
|
13
|
+
declare type SnapHandler = {
|
|
14
|
+
/**
|
|
15
|
+
* The type of handler.
|
|
16
|
+
*/
|
|
17
|
+
type: HandlerType;
|
|
18
|
+
/**
|
|
19
|
+
* Whether the handler is required, i.e., whether the request will fail if the
|
|
20
|
+
* handler is called, but the snap does not export it.
|
|
21
|
+
*
|
|
22
|
+
* This is primarily used for the lifecycle handlers, which are optional.
|
|
23
|
+
*/
|
|
24
|
+
required: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Validate the given snap export. This should return a type guard for the
|
|
27
|
+
* handler type.
|
|
28
|
+
*
|
|
29
|
+
* @param snapExport - The export to validate.
|
|
30
|
+
* @returns Whether the export is valid.
|
|
31
|
+
*/
|
|
32
|
+
validator: (snapExport: unknown) => boolean;
|
|
33
|
+
};
|
|
34
|
+
export declare const SNAP_EXPORTS: {
|
|
35
|
+
readonly onRpcRequest: {
|
|
36
|
+
readonly type: HandlerType.OnRpcRequest;
|
|
37
|
+
readonly required: true;
|
|
38
|
+
readonly validator: (snapExport: unknown) => snapExport is OnRpcRequestHandler<JsonRpcParams>;
|
|
39
|
+
};
|
|
40
|
+
readonly onTransaction: {
|
|
41
|
+
readonly type: HandlerType.OnTransaction;
|
|
42
|
+
readonly required: true;
|
|
43
|
+
readonly validator: (snapExport: unknown) => snapExport is OnTransactionHandler;
|
|
44
|
+
};
|
|
45
|
+
readonly onCronjob: {
|
|
46
|
+
readonly type: HandlerType.OnCronjob;
|
|
47
|
+
readonly required: true;
|
|
48
|
+
readonly validator: (snapExport: unknown) => snapExport is OnCronjobHandler<JsonRpcParams>;
|
|
49
|
+
};
|
|
50
|
+
readonly onNameLookup: {
|
|
51
|
+
readonly type: HandlerType.OnNameLookup;
|
|
52
|
+
readonly required: true;
|
|
53
|
+
readonly validator: (snapExport: unknown) => snapExport is OnNameLookupHandler;
|
|
54
|
+
};
|
|
55
|
+
readonly onInstall: {
|
|
56
|
+
readonly type: HandlerType.OnInstall;
|
|
57
|
+
readonly required: false;
|
|
58
|
+
readonly validator: (snapExport: unknown) => snapExport is LifecycleEventHandler;
|
|
59
|
+
};
|
|
60
|
+
readonly onUpdate: {
|
|
61
|
+
readonly type: HandlerType.OnUpdate;
|
|
62
|
+
readonly required: false;
|
|
63
|
+
readonly validator: (snapExport: unknown) => snapExport is LifecycleEventHandler;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* The `onRpcRequest` handler. This is called whenever a JSON-RPC request is
|
|
68
|
+
* made to the snap.
|
|
69
|
+
*
|
|
70
|
+
* @param args - The request arguments.
|
|
71
|
+
* @param args.origin - The origin of the request. This can be the ID of another
|
|
72
|
+
* snap, or the URL of a dapp.
|
|
73
|
+
* @param args.request - The JSON-RPC request sent to the snap.
|
|
74
|
+
* @returns The JSON-RPC response. This must be a JSON-serializable value.
|
|
75
|
+
*/
|
|
76
|
+
export declare type OnRpcRequestHandler<Params extends JsonRpcParams = JsonRpcParams> = (args: {
|
|
77
|
+
origin: string;
|
|
78
|
+
request: JsonRpcRequest<Params>;
|
|
79
|
+
}) => Promise<unknown>;
|
|
80
|
+
/**
|
|
81
|
+
* Enum used to specify the severity level of content being returned from a transaction insight.
|
|
82
|
+
*/
|
|
83
|
+
export declare enum SeverityLevel {
|
|
84
|
+
Critical = "critical"
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The response from a snap's `onTransaction` handler.
|
|
88
|
+
*
|
|
89
|
+
* @property content - A custom UI component, that will be shown in MetaMask. Can be created using `@metamask/snaps-ui`.
|
|
90
|
+
*
|
|
91
|
+
* If the snap has no insights about the transaction, this should be `null`.
|
|
92
|
+
*/
|
|
93
|
+
export declare type OnTransactionResponse = {
|
|
94
|
+
content: Component | null;
|
|
95
|
+
severity?: EnumToUnion<SeverityLevel>;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* The `onTransaction` handler. This is called whenever a transaction is
|
|
99
|
+
* submitted to the snap. It can return insights about the transaction, which
|
|
100
|
+
* will be displayed to the user.
|
|
101
|
+
*
|
|
102
|
+
* @param args - The request arguments.
|
|
103
|
+
* @param args.transaction - The transaction object.
|
|
104
|
+
* @param args.chainId - The CAIP-2 chain ID of the network the transaction is
|
|
105
|
+
* being submitted to.
|
|
106
|
+
* @param args.transactionOrigin - The origin of the transaction. This is the
|
|
107
|
+
* URL of the dapp that submitted the transaction.
|
|
108
|
+
* @returns Insights about the transaction. See {@link OnTransactionResponse}.
|
|
109
|
+
*/
|
|
110
|
+
export declare type OnTransactionHandler = (args: {
|
|
111
|
+
transaction: {
|
|
112
|
+
[key: string]: Json;
|
|
113
|
+
};
|
|
114
|
+
chainId: string;
|
|
115
|
+
transactionOrigin?: string;
|
|
116
|
+
}) => Promise<OnTransactionResponse>;
|
|
117
|
+
/**
|
|
118
|
+
* The `onCronjob` handler. This is called on a regular interval, as defined by
|
|
119
|
+
* the snap's manifest.
|
|
120
|
+
*
|
|
121
|
+
* @param args - The request arguments.
|
|
122
|
+
* @param args.request - The JSON-RPC request sent to the snap.
|
|
123
|
+
*/
|
|
124
|
+
export declare type OnCronjobHandler<Params extends JsonRpcParams = JsonRpcParams> = (args: {
|
|
125
|
+
request: JsonRpcRequest<Params>;
|
|
126
|
+
}) => Promise<unknown>;
|
|
127
|
+
/**
|
|
128
|
+
* A handler that can be used for the lifecycle hooks.
|
|
129
|
+
*/
|
|
130
|
+
export declare type LifecycleEventHandler = (args: {
|
|
131
|
+
request: JsonRpcRequest;
|
|
132
|
+
}) => Promise<unknown>;
|
|
133
|
+
/**
|
|
134
|
+
* The `onInstall` handler. This is called after the snap is installed.
|
|
135
|
+
*
|
|
136
|
+
* This type is an alias for {@link LifecycleEventHandler}.
|
|
137
|
+
*/
|
|
138
|
+
export declare type OnInstallHandler = LifecycleEventHandler;
|
|
139
|
+
/**
|
|
140
|
+
* The `onUpdate` handler. This is called after the snap is updated.
|
|
141
|
+
*
|
|
142
|
+
* This type is an alias for {@link LifecycleEventHandler}.
|
|
143
|
+
*/
|
|
144
|
+
export declare type OnUpdateHandler = LifecycleEventHandler;
|
|
145
|
+
/**
|
|
146
|
+
* Utility type for getting the handler function type from a handler type.
|
|
147
|
+
*/
|
|
148
|
+
export declare type HandlerFunction<Type extends SnapHandler> = Type['validator'] extends (snapExport: unknown) => snapExport is infer Handler ? Handler : never;
|
|
149
|
+
/**
|
|
150
|
+
* The response from a snap's `onNameLookup` handler.
|
|
151
|
+
*
|
|
152
|
+
* @property resolvedAddress - The resolved address for a given domain.
|
|
153
|
+
* @property resolvedDomain - The resolved domain for a given address.
|
|
154
|
+
*
|
|
155
|
+
*
|
|
156
|
+
* If the snap has no resolved address/domain from its lookup, this should be `null`.
|
|
157
|
+
*/
|
|
158
|
+
export declare type OnNameLookupResponse = {
|
|
159
|
+
resolvedAddress: AccountAddress;
|
|
160
|
+
resolvedDomain?: never;
|
|
161
|
+
} | {
|
|
162
|
+
resolvedDomain: string;
|
|
163
|
+
resolvedAddress?: never;
|
|
164
|
+
} | null;
|
|
165
|
+
export declare type OnNameLookupArgs = {
|
|
166
|
+
chainId: Caip2ChainId;
|
|
167
|
+
} & ({
|
|
168
|
+
domain: string;
|
|
169
|
+
address?: never;
|
|
170
|
+
} | {
|
|
171
|
+
address: string;
|
|
172
|
+
domain?: never;
|
|
173
|
+
});
|
|
174
|
+
/**
|
|
175
|
+
* The `onNameLookup` handler. This is called whenever content is entered
|
|
176
|
+
* into the send to field for sending assets to an EOA address.
|
|
177
|
+
*
|
|
178
|
+
* @param args - The request arguments.
|
|
179
|
+
* @param args.domain - The human-readable address that is to be resolved.
|
|
180
|
+
* @param args.chainId - The CAIP-2 chain ID of the network the transaction is
|
|
181
|
+
* being submitted to.
|
|
182
|
+
* @param args.address - The address that is to be resolved.
|
|
183
|
+
* @returns Resolved address/domain from the lookup. See {@link OnNameLookupResponse}.
|
|
184
|
+
*/
|
|
185
|
+
export declare type OnNameLookupHandler = (args: OnNameLookupArgs) => Promise<OnNameLookupResponse>;
|
|
186
|
+
/**
|
|
187
|
+
* All the function-based handlers that a snap can implement.
|
|
188
|
+
*/
|
|
189
|
+
export declare type SnapFunctionExports = {
|
|
190
|
+
[Key in keyof typeof SNAP_EXPORTS]?: HandlerFunction<typeof SNAP_EXPORTS[Key]>;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* All handlers that a snap can implement.
|
|
194
|
+
*/
|
|
195
|
+
export declare type SnapExports = SnapFunctionExports;
|
|
196
|
+
export {};
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* an initialization timeout in the SnapController.
|
|
5
5
|
*
|
|
6
6
|
* @param uri - The iframe URI.
|
|
7
|
-
* @param
|
|
7
|
+
* @param id - The ID to assign to the iframe.
|
|
8
|
+
* @param sandbox - Whether to enable the sandbox attribute.
|
|
8
9
|
* @returns A promise that resolves to the contentWindow of the iframe.
|
|
9
10
|
*/
|
|
10
|
-
export declare function createWindow(uri: string,
|
|
11
|
+
export declare function createWindow(uri: string, id: string, sandbox?: boolean): Promise<Window>;
|
|
@@ -5,6 +5,8 @@ export * from './cronjob';
|
|
|
5
5
|
export * from './deep-clone';
|
|
6
6
|
export * from './default-endowments';
|
|
7
7
|
export * from './entropy';
|
|
8
|
+
export * from './enum';
|
|
9
|
+
export * from './errors';
|
|
8
10
|
export * from './handlers';
|
|
9
11
|
export * from './iframe';
|
|
10
12
|
export * from './json';
|
|
@@ -12,9 +14,11 @@ export * from './json-rpc';
|
|
|
12
14
|
export * from './logging';
|
|
13
15
|
export * from './manifest/index.browser';
|
|
14
16
|
export * from './namespace';
|
|
15
|
-
export * from './notification';
|
|
16
17
|
export * from './path';
|
|
17
18
|
export * from './snaps';
|
|
19
|
+
export * from './strings';
|
|
20
|
+
export * from './structs';
|
|
18
21
|
export * from './types';
|
|
22
|
+
export * from './validation';
|
|
19
23
|
export * from './versions';
|
|
20
24
|
export * from './virtual-file/index.browser';
|
|
@@ -5,7 +5,9 @@ export * from './checksum';
|
|
|
5
5
|
export * from './deep-clone';
|
|
6
6
|
export * from './default-endowments';
|
|
7
7
|
export * from './entropy';
|
|
8
|
+
export * from './enum';
|
|
8
9
|
export * from './eval';
|
|
10
|
+
export * from './errors';
|
|
9
11
|
export * from './fs';
|
|
10
12
|
export * from './handlers';
|
|
11
13
|
export * from './iframe';
|
|
@@ -15,11 +17,13 @@ export * from './logging';
|
|
|
15
17
|
export * from './manifest';
|
|
16
18
|
export * from './mock';
|
|
17
19
|
export * from './namespace';
|
|
18
|
-
export * from './notification';
|
|
19
20
|
export * from './npm';
|
|
20
21
|
export * from './path';
|
|
21
22
|
export * from './post-process';
|
|
22
23
|
export * from './snaps';
|
|
24
|
+
export * from './strings';
|
|
25
|
+
export * from './structs';
|
|
23
26
|
export * from './types';
|
|
27
|
+
export * from './validation';
|
|
24
28
|
export * from './versions';
|
|
25
29
|
export * from './virtual-file';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Json, JsonRpcSuccess, AssertionErrorConstructor } from '@metamask/utils';
|
|
2
|
-
import { Infer } from 'superstruct';
|
|
1
|
+
import type { Json, JsonRpcSuccess, AssertionErrorConstructor } from '@metamask/utils';
|
|
2
|
+
import type { Infer } from 'superstruct';
|
|
3
3
|
export declare const RpcOriginsStruct: import("superstruct").Struct<{
|
|
4
4
|
dapps?: boolean | undefined;
|
|
5
5
|
snaps?: boolean | undefined;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Json } from '@metamask/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Parse JSON safely.
|
|
4
|
+
*
|
|
5
|
+
* Does multiple kinds of validation and strips unwanted properties like
|
|
6
|
+
* `__proto__` and `constructor`.
|
|
7
|
+
*
|
|
8
|
+
* @param json - A JSON string to be parsed.
|
|
9
|
+
* @returns The parsed JSON object.
|
|
10
|
+
* @template Type - The type of the JSON object. The type is not actually
|
|
11
|
+
* checked, but it is used to infer the return type.
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseJson<Type extends Json = Json>(json: string): Type;
|