@metamask/snaps-utils 1.0.2 → 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 -223
- 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,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
readVirtualFile: function() {
|
|
13
|
+
return readVirtualFile;
|
|
14
|
+
},
|
|
15
|
+
writeVirtualFile: function() {
|
|
16
|
+
return writeVirtualFile;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _fs = require("fs");
|
|
20
|
+
const _VirtualFile = require("./VirtualFile");
|
|
21
|
+
async function readVirtualFile(path, encoding = null) {
|
|
22
|
+
return new _VirtualFile.VirtualFile({
|
|
23
|
+
path,
|
|
24
|
+
value: await _fs.promises.readFile(path, {
|
|
25
|
+
encoding
|
|
26
|
+
})
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async function writeVirtualFile(vfile, options) {
|
|
30
|
+
return _fs.promises.writeFile(vfile.path, vfile.value, options);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//# 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":["readVirtualFile","writeVirtualFile","path","encoding","VirtualFile","value","fsPromises","readFile","vfile","options","writeFile"],"mappings":";;;;;;;;;;;IAWsBA,eAAe;eAAfA;;IAqBAC,gBAAgB;eAAhBA;;;oBAhCiB;6BAEX;AASrB,eAAeD,gBACpBE,IAAY,EACZC,WAAkC,IAAI;IAEtC,OAAO,IAAIC,wBAAW,CAAC;QACrBF;QACAG,OAAO,MAAMC,YAAU,CAACC,QAAQ,CAACL,MAAM;YAAEC;QAAS;IACpD;AACF;AAaO,eAAeF,iBACpBO,KAAkB,EAClBC,OAA2B;IAE3B,OAAOH,YAAU,CAACI,SAAS,CAACF,MAAMN,IAAI,EAAEM,MAAMH,KAAK,EAAEI;AACvD"}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEqual = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Checks if array `a` is equal to array `b`. Note that this does not do a deep
|
|
6
3
|
* equality check. It only checks if the arrays are the same length and if each
|
|
@@ -9,9 +6,8 @@ exports.isEqual = void 0;
|
|
|
9
6
|
* @param a - The first array to compare.
|
|
10
7
|
* @param b - The second array to compare.
|
|
11
8
|
* @returns `true` if the arrays are equal, `false` otherwise.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
9
|
+
*/ export function isEqual(a, b) {
|
|
10
|
+
return a.length === b.length && a.every((value, index)=>value === b[index]);
|
|
15
11
|
}
|
|
16
|
-
|
|
12
|
+
|
|
17
13
|
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/array.ts"],"sourcesContent":["/**\n * Checks if array `a` is equal to array `b`. Note that this does not do a deep\n * equality check. It only checks if the arrays are the same length and if each\n * element in `a` is equal to (`===`) the corresponding element in `b`.\n *\n * @param a - The first array to compare.\n * @param b - The second array to compare.\n * @returns `true` if the arrays are equal, `false` otherwise.\n */\nexport function isEqual(a: unknown[], b: unknown[]): boolean {\n return a.length === b.length && a.every((value, index) => value === b[index]);\n}\n"],"names":["isEqual","a","b","length","every","value","index"],"mappings":"AAAA;;;;;;;;CAQC,GACD,OAAO,SAASA,QAAQC,CAAY,EAAEC,CAAY;IAChD,OAAOD,EAAEE,MAAM,KAAKD,EAAEC,MAAM,IAAIF,EAAEG,KAAK,CAAC,CAACC,OAAOC,QAAUD,UAAUH,CAAC,CAACI,MAAM;AAC9E"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export var SnapCaveatType;
|
|
2
|
+
(function(SnapCaveatType) {
|
|
3
|
+
SnapCaveatType[/**
|
|
4
|
+
* Permitted derivation paths, used by `snap_getBip32Entropy`.
|
|
5
|
+
*/ "PermittedDerivationPaths"] = 'permittedDerivationPaths';
|
|
6
|
+
SnapCaveatType[/**
|
|
7
|
+
* Permitted coin types, used by `snap_getBip44Entropy`.
|
|
8
|
+
*/ "PermittedCoinTypes"] = 'permittedCoinTypes';
|
|
9
|
+
SnapCaveatType[/**
|
|
10
|
+
* Caveat specifying a snap cronjob.
|
|
11
|
+
*/ "SnapCronjob"] = 'snapCronjob';
|
|
12
|
+
SnapCaveatType[/**
|
|
13
|
+
* Caveat specifying access to the transaction origin, used by `endowment:transaction-insight`.
|
|
14
|
+
*/ "TransactionOrigin"] = 'transactionOrigin';
|
|
15
|
+
SnapCaveatType[/**
|
|
16
|
+
* The origins that a Snap can receive JSON-RPC messages from.
|
|
17
|
+
*/ "RpcOrigin"] = 'rpcOrigin';
|
|
18
|
+
SnapCaveatType[/**
|
|
19
|
+
* Caveat specifying the snap IDs that can be interacted with.
|
|
20
|
+
*/ "SnapIds"] = 'snapIds';
|
|
21
|
+
SnapCaveatType[/**
|
|
22
|
+
* Caveat specifying the CAIP-2 chain IDs that a snap can service, currently limited to `endowment:name-lookup`.
|
|
23
|
+
*/ "ChainIds"] = 'chainIds';
|
|
24
|
+
})(SnapCaveatType || (SnapCaveatType = {}));
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=caveats.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/caveats.ts"],"sourcesContent":["export enum SnapCaveatType {\n /**\n * Permitted derivation paths, used by `snap_getBip32Entropy`.\n */\n PermittedDerivationPaths = 'permittedDerivationPaths',\n\n /**\n * Permitted coin types, used by `snap_getBip44Entropy`.\n */\n PermittedCoinTypes = 'permittedCoinTypes',\n\n /**\n * Caveat specifying a snap cronjob.\n */\n SnapCronjob = 'snapCronjob',\n\n /**\n * Caveat specifying access to the transaction origin, used by `endowment:transaction-insight`.\n */\n TransactionOrigin = 'transactionOrigin',\n\n /**\n * The origins that a Snap can receive JSON-RPC messages from.\n */\n RpcOrigin = 'rpcOrigin',\n\n /**\n * Caveat specifying the snap IDs that can be interacted with.\n */\n SnapIds = 'snapIds',\n\n /**\n * Caveat specifying the CAIP-2 chain IDs that a snap can service, currently limited to `endowment:name-lookup`.\n */\n ChainIds = 'chainIds',\n}\n"],"names":["SnapCaveatType","PermittedDerivationPaths","PermittedCoinTypes","SnapCronjob","TransactionOrigin","RpcOrigin","SnapIds","ChainIds"],"mappings":"WAAO;UAAKA,cAAc;IAAdA,eACV;;GAEC,GACDC,8BAA2B;IAJjBD,eAMV;;GAEC,GACDE,wBAAqB;IATXF,eAWV;;GAEC,GACDG,iBAAc;IAdJH,eAgBV;;GAEC,GACDI,uBAAoB;IAnBVJ,eAqBV;;GAEC,GACDK,eAAY;IAxBFL,eA0BV;;GAEC,GACDM,aAAU;IA7BAN,eA+BV;;GAEC,GACDO,cAAW;GAlCDP,mBAAAA"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { assert, concatBytes } from '@metamask/utils';
|
|
2
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
3
|
+
import { VirtualFile } from './virtual-file/VirtualFile';
|
|
4
|
+
/**
|
|
5
|
+
* Calculates checksum for a single byte array.
|
|
6
|
+
*
|
|
7
|
+
* @param bytes - The byte array to calculate the checksum for.
|
|
8
|
+
* @returns A single sha-256 checksum.
|
|
9
|
+
*/ export function checksum(bytes) {
|
|
10
|
+
const value = bytes instanceof VirtualFile ? bytes.value : bytes;
|
|
11
|
+
return sha256(value);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Calculates checksum over multiple files in a reproducible way.
|
|
15
|
+
*
|
|
16
|
+
* 1. Sort all the files by their paths.
|
|
17
|
+
* 2. Calculate sha-256 checksum of each file separately.
|
|
18
|
+
* 3. Concatenate all the checksums into one buffer and sha-256 that buffer.
|
|
19
|
+
*
|
|
20
|
+
* The sorting of paths is done using {@link https://tc39.es/ecma262/#sec-islessthan UTF-16 Code Units}.
|
|
21
|
+
*
|
|
22
|
+
* @param files - The files over which to calculate the checksum.
|
|
23
|
+
* @returns A single sha-256 checksum.
|
|
24
|
+
*/ export function checksumFiles(files) {
|
|
25
|
+
return checksum(concatBytes([
|
|
26
|
+
...files
|
|
27
|
+
].sort((a, b)=>{
|
|
28
|
+
assert(a.path !== b.path, 'Tried to sort files with non-unique paths.');
|
|
29
|
+
if (a.path < b.path) {
|
|
30
|
+
return -1;
|
|
31
|
+
}
|
|
32
|
+
return 1;
|
|
33
|
+
}).map((file)=>checksum(file))));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=checksum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/checksum.ts"],"sourcesContent":["import { assert, concatBytes } from '@metamask/utils';\nimport { sha256 } from '@noble/hashes/sha256';\n\nimport { VirtualFile } from './virtual-file/VirtualFile';\n\n/**\n * Calculates checksum for a single byte array.\n *\n * @param bytes - The byte array to calculate the checksum for.\n * @returns A single sha-256 checksum.\n */\nexport function checksum(bytes: VirtualFile | Uint8Array | string): Uint8Array {\n const value = bytes instanceof VirtualFile ? bytes.value : bytes;\n return sha256(value);\n}\n\n/**\n * Calculates checksum over multiple files in a reproducible way.\n *\n * 1. Sort all the files by their paths.\n * 2. Calculate sha-256 checksum of each file separately.\n * 3. Concatenate all the checksums into one buffer and sha-256 that buffer.\n *\n * The sorting of paths is done using {@link https://tc39.es/ecma262/#sec-islessthan UTF-16 Code Units}.\n *\n * @param files - The files over which to calculate the checksum.\n * @returns A single sha-256 checksum.\n */\nexport function checksumFiles(files: VirtualFile[]) {\n return checksum(\n concatBytes(\n [...files]\n .sort((a, b) => {\n assert(\n a.path !== b.path,\n 'Tried to sort files with non-unique paths.',\n );\n if (a.path < b.path) {\n return -1;\n }\n return 1;\n })\n .map((file) => checksum(file)),\n ),\n );\n}\n"],"names":["assert","concatBytes","sha256","VirtualFile","checksum","bytes","value","checksumFiles","files","sort","a","b","path","map","file"],"mappings":"AAAA,SAASA,MAAM,EAAEC,WAAW,QAAQ,kBAAkB;AACtD,SAASC,MAAM,QAAQ,uBAAuB;AAE9C,SAASC,WAAW,QAAQ,6BAA6B;AAEzD;;;;;CAKC,GACD,OAAO,SAASC,SAASC,KAAwC;IAC/D,MAAMC,QAAQD,iBAAiBF,cAAcE,MAAMC,KAAK,GAAGD;IAC3D,OAAOH,OAAOI;AAChB;AAEA;;;;;;;;;;;CAWC,GACD,OAAO,SAASC,cAAcC,KAAoB;IAChD,OAAOJ,SACLH,YACE;WAAIO;KAAM,CACPC,IAAI,CAAC,CAACC,GAAGC;QACRX,OACEU,EAAEE,IAAI,KAAKD,EAAEC,IAAI,EACjB;QAEF,IAAIF,EAAEE,IAAI,GAAGD,EAAEC,IAAI,EAAE;YACnB,OAAO,CAAC;QACV;QACA,OAAO;IACT,GACCC,GAAG,CAAC,CAACC,OAASV,SAASU;AAGhC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { JsonRpcRequestStruct } from '@metamask/utils';
|
|
2
|
+
import { parseExpression } from 'cron-parser';
|
|
3
|
+
import { array, assign, coerce, create, object, omit, optional, partial, pick, refine, string } from 'superstruct';
|
|
4
|
+
export const CronjobRpcRequestStruct = assign(partial(pick(JsonRpcRequestStruct, [
|
|
5
|
+
'id',
|
|
6
|
+
'jsonrpc'
|
|
7
|
+
])), omit(JsonRpcRequestStruct, [
|
|
8
|
+
'id',
|
|
9
|
+
'jsonrpc'
|
|
10
|
+
]));
|
|
11
|
+
export const CronExpressionStruct = refine(coerce(string(), object({
|
|
12
|
+
minute: optional(string()),
|
|
13
|
+
hour: optional(string()),
|
|
14
|
+
dayOfMonth: optional(string()),
|
|
15
|
+
month: optional(string()),
|
|
16
|
+
dayOfWeek: optional(string())
|
|
17
|
+
}), (value)=>`${value.minute ?? '*'} ${value.hour ?? '*'} ${value.dayOfMonth ?? '*'} ${value.month ?? '*'} ${value.dayOfWeek ?? '*'}`), 'CronExpression', (value)=>{
|
|
18
|
+
try {
|
|
19
|
+
parseExpression(value);
|
|
20
|
+
return true;
|
|
21
|
+
} catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Parses a cron expression.
|
|
27
|
+
*
|
|
28
|
+
* @param expression - Expression to parse.
|
|
29
|
+
* @returns A CronExpression class instance.
|
|
30
|
+
*/ export function parseCronExpression(expression) {
|
|
31
|
+
const ensureStringExpression = create(expression, CronExpressionStruct);
|
|
32
|
+
return parseExpression(ensureStringExpression);
|
|
33
|
+
}
|
|
34
|
+
export const CronjobSpecificationStruct = object({
|
|
35
|
+
expression: CronExpressionStruct,
|
|
36
|
+
request: CronjobRpcRequestStruct
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Check if the given value is a {@link CronjobSpecification} object.
|
|
40
|
+
*
|
|
41
|
+
* @param value - The value to check.
|
|
42
|
+
* @returns Whether the value is a valid {@link CronjobSpecification} object.
|
|
43
|
+
*/ export function isCronjobSpecification(value) {
|
|
44
|
+
try {
|
|
45
|
+
create(value, CronjobSpecificationStruct);
|
|
46
|
+
return true;
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export const CronjobSpecificationArrayStruct = array(CronjobSpecificationStruct);
|
|
52
|
+
/**
|
|
53
|
+
* Check if the given value is an array of {@link CronjobSpecification} objects.
|
|
54
|
+
*
|
|
55
|
+
* @param value - The value to check.
|
|
56
|
+
* @returns Whether the value is a valid array of {@link CronjobSpecification} objects.
|
|
57
|
+
*/ export function isCronjobSpecificationArray(value) {
|
|
58
|
+
try {
|
|
59
|
+
create(value, CronjobSpecificationArrayStruct);
|
|
60
|
+
return true;
|
|
61
|
+
} catch {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=cronjob.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/cronjob.ts"],"sourcesContent":["import { JsonRpcRequestStruct } from '@metamask/utils';\nimport { parseExpression } from 'cron-parser';\nimport type { Infer } from 'superstruct';\nimport {\n array,\n assign,\n coerce,\n create,\n object,\n omit,\n optional,\n partial,\n pick,\n refine,\n string,\n} from 'superstruct';\n\nexport const CronjobRpcRequestStruct = assign(\n partial(pick(JsonRpcRequestStruct, ['id', 'jsonrpc'])),\n omit(JsonRpcRequestStruct, ['id', 'jsonrpc']),\n);\nexport type CronjobRpcRequest = Infer<typeof CronjobRpcRequestStruct>;\n\nexport const CronExpressionStruct = refine(\n coerce(\n string(),\n object({\n minute: optional(string()),\n hour: optional(string()),\n dayOfMonth: optional(string()),\n month: optional(string()),\n dayOfWeek: optional(string()),\n }),\n (value) =>\n `${value.minute ?? '*'} ${value.hour ?? '*'} ${value.dayOfMonth ?? '*'} ${\n value.month ?? '*'\n } ${value.dayOfWeek ?? '*'}`,\n ),\n 'CronExpression',\n (value) => {\n try {\n parseExpression(value);\n return true;\n } catch {\n return false;\n }\n },\n);\n\nexport type CronExpression = Infer<typeof CronExpressionStruct>;\n\n/**\n * Parses a cron expression.\n *\n * @param expression - Expression to parse.\n * @returns A CronExpression class instance.\n */\nexport function parseCronExpression(expression: string | object) {\n const ensureStringExpression = create(expression, CronExpressionStruct);\n return parseExpression(ensureStringExpression);\n}\n\nexport const CronjobSpecificationStruct = object({\n expression: CronExpressionStruct,\n request: CronjobRpcRequestStruct,\n});\nexport type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;\n\n/**\n * Check if the given value is a {@link CronjobSpecification} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link CronjobSpecification} object.\n */\nexport function isCronjobSpecification(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationStruct);\n return true;\n } catch {\n return false;\n }\n}\n\nexport const CronjobSpecificationArrayStruct = array(\n CronjobSpecificationStruct,\n);\n\n/**\n * Check if the given value is an array of {@link CronjobSpecification} objects.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid array of {@link CronjobSpecification} objects.\n */\nexport function isCronjobSpecificationArray(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationArrayStruct);\n return true;\n } catch {\n return false;\n }\n}\n"],"names":["JsonRpcRequestStruct","parseExpression","array","assign","coerce","create","object","omit","optional","partial","pick","refine","string","CronjobRpcRequestStruct","CronExpressionStruct","minute","hour","dayOfMonth","month","dayOfWeek","value","parseCronExpression","expression","ensureStringExpression","CronjobSpecificationStruct","request","isCronjobSpecification","CronjobSpecificationArrayStruct","isCronjobSpecificationArray"],"mappings":"AAAA,SAASA,oBAAoB,QAAQ,kBAAkB;AACvD,SAASC,eAAe,QAAQ,cAAc;AAE9C,SACEC,KAAK,EACLC,MAAM,EACNC,MAAM,EACNC,MAAM,EACNC,MAAM,EACNC,IAAI,EACJC,QAAQ,EACRC,OAAO,EACPC,IAAI,EACJC,MAAM,EACNC,MAAM,QACD,cAAc;AAErB,OAAO,MAAMC,0BAA0BV,OACrCM,QAAQC,KAAKV,sBAAsB;IAAC;IAAM;CAAU,IACpDO,KAAKP,sBAAsB;IAAC;IAAM;CAAU,GAC5C;AAGF,OAAO,MAAMc,uBAAuBH,OAClCP,OACEQ,UACAN,OAAO;IACLS,QAAQP,SAASI;IACjBI,MAAMR,SAASI;IACfK,YAAYT,SAASI;IACrBM,OAAOV,SAASI;IAChBO,WAAWX,SAASI;AACtB,IACA,CAACQ,QACC,CAAC,EAAEA,MAAML,MAAM,IAAI,IAAI,CAAC,EAAEK,MAAMJ,IAAI,IAAI,IAAI,CAAC,EAAEI,MAAMH,UAAU,IAAI,IAAI,CAAC,EACtEG,MAAMF,KAAK,IAAI,IAChB,CAAC,EAAEE,MAAMD,SAAS,IAAI,IAAI,CAAC,GAEhC,kBACA,CAACC;IACC,IAAI;QACFnB,gBAAgBmB;QAChB,OAAO;IACT,EAAE,OAAM;QACN,OAAO;IACT;AACF,GACA;AAIF;;;;;CAKC,GACD,OAAO,SAASC,oBAAoBC,UAA2B;IAC7D,MAAMC,yBAAyBlB,OAAOiB,YAAYR;IAClD,OAAOb,gBAAgBsB;AACzB;AAEA,OAAO,MAAMC,6BAA6BlB,OAAO;IAC/CgB,YAAYR;IACZW,SAASZ;AACX,GAAG;AAGH;;;;;CAKC,GACD,OAAO,SAASa,uBAAuBN,KAAc;IACnD,IAAI;QACFf,OAAOe,OAAOI;QACd,OAAO;IACT,EAAE,OAAM;QACN,OAAO;IACT;AACF;AAEA,OAAO,MAAMG,kCAAkCzB,MAC7CsB,4BACA;AAEF;;;;;CAKC,GACD,OAAO,SAASI,4BAA4BR,KAAc;IACxD,IAAI;QACFf,OAAOe,OAAOO;QACd,OAAO;IACT,EAAE,OAAM;QACN,OAAO;IACT;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/deep-clone.ts"],"sourcesContent":["import rfdc from 'rfdc';\n\nexport const deepClone = rfdc({ proto: false, circles: false });\n"],"names":["rfdc","deepClone","proto","circles"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AAExB,OAAO,MAAMC,YAAYD,KAAK;IAAEE,OAAO;IAAOC,SAAS;AAAM,GAAG"}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_ENDOWMENTS = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Global JavaScript APIs exposed by default to all snaps.
|
|
6
|
-
*/
|
|
7
|
-
exports.DEFAULT_ENDOWMENTS = Object.freeze([
|
|
3
|
+
*/ export const DEFAULT_ENDOWMENTS = Object.freeze([
|
|
8
4
|
'atob',
|
|
9
5
|
'btoa',
|
|
10
6
|
'BigInt',
|
|
@@ -37,6 +33,7 @@ exports.DEFAULT_ENDOWMENTS = Object.freeze([
|
|
|
37
33
|
// https://github.com/MetaMask/snaps-monorepo/issues/662
|
|
38
34
|
// https://github.com/MetaMask/snaps-monorepo/discussions/678
|
|
39
35
|
'AbortController',
|
|
40
|
-
'AbortSignal'
|
|
36
|
+
'AbortSignal'
|
|
41
37
|
]);
|
|
38
|
+
|
|
42
39
|
//# sourceMappingURL=default-endowments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/default-endowments.ts"],"sourcesContent":["/**\n * Global JavaScript APIs exposed by default to all snaps.\n */\nexport const DEFAULT_ENDOWMENTS: readonly string[] = Object.freeze([\n 'atob',\n 'btoa',\n 'BigInt',\n 'console',\n 'crypto',\n 'Date',\n 'Math',\n 'setTimeout',\n 'clearTimeout',\n 'SubtleCrypto',\n 'TextDecoder',\n 'TextEncoder',\n 'URL',\n 'setInterval',\n 'clearInterval',\n 'Int8Array',\n 'Uint8Array',\n 'Uint8ClampedArray',\n 'Int16Array',\n 'Uint16Array',\n 'Int32Array',\n 'Uint32Array',\n 'Float32Array',\n 'Float64Array',\n 'BigInt64Array',\n 'BigUint64Array',\n 'DataView',\n 'ArrayBuffer',\n // Used by fetch, but also as API for some packages that don't do network connections\n // https://github.com/MetaMask/snaps-monorepo/issues/662\n // https://github.com/MetaMask/snaps-monorepo/discussions/678\n 'AbortController',\n 'AbortSignal',\n]);\n"],"names":["DEFAULT_ENDOWMENTS","Object","freeze"],"mappings":"AAAA;;CAEC,GACD,OAAO,MAAMA,qBAAwCC,OAAOC,MAAM,CAAC;IACjE;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA,qFAAqF;IACrF,wDAAwD;IACxD,6DAA6D;IAC7D;IACA;CACD,EAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/entropy.ts"],"sourcesContent":["// 0xd36e6170 - 0x80000000\nexport const SIP_6_MAGIC_VALUE = `1399742832'` as `${number}'`;\n\n// `${bytesToNumber(keccak256('Snaps state encryption').slice(0, 4))}'`\nexport const STATE_ENCRYPTION_MAGIC_VALUE = `572232532'` as `${number}'`;\n\nexport type MagicValue =\n | typeof SIP_6_MAGIC_VALUE\n | typeof STATE_ENCRYPTION_MAGIC_VALUE;\n"],"names":["SIP_6_MAGIC_VALUE","STATE_ENCRYPTION_MAGIC_VALUE"],"mappings":"AAAA,0BAA0B;AAC1B,OAAO,MAAMA,oBAAoB,CAAC,WAAW,CAAC,CAAiB;AAE/D,uEAAuE;AACvE,OAAO,MAAMC,+BAA+B,CAAC,UAAU,CAAC,CAAiB"}
|
package/dist/esm/enum.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { literal } from './structs';
|
|
2
|
+
/**
|
|
3
|
+
* Superstruct struct for validating an enum value. This allows using both the
|
|
4
|
+
* enum string values and the enum itself as values.
|
|
5
|
+
*
|
|
6
|
+
* @param constant - The enum to validate against.
|
|
7
|
+
* @returns The superstruct struct.
|
|
8
|
+
*/ export function enumValue(constant) {
|
|
9
|
+
return literal(constant);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=enum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/enum.ts"],"sourcesContent":["import type { Struct } from 'superstruct';\n\nimport { literal } from './structs';\n\n/**\n * Get the enum values as union type. This allows using both the enum string\n * values and the enum itself as values.\n *\n * Note: This only works for string enums.\n *\n * @example\n * ```typescript\n * enum Foo {\n * Bar = 'bar',\n * Baz = 'baz',\n * }\n *\n * type FooValue = EnumToUnion<Foo>;\n * // FooValue is 'bar' | 'baz'\n *\n * const foo: FooValue = Foo.Bar; // Works\n * const foo: FooValue = 'bar'; // Also works\n * ```\n */\nexport type EnumToUnion<Type extends string> = `${Type}`;\n\n/**\n * Superstruct struct for validating an enum value. This allows using both the\n * enum string values and the enum itself as values.\n *\n * @param constant - The enum to validate against.\n * @returns The superstruct struct.\n */\nexport function enumValue<Type extends string>(\n constant: Type,\n): Struct<EnumToUnion<Type>, null> {\n return literal(constant as EnumToUnion<Type>);\n}\n"],"names":["literal","enumValue","constant"],"mappings":"AAEA,SAASA,OAAO,QAAQ,YAAY;AAwBpC;;;;;;CAMC,GACD,OAAO,SAASC,UACdC,QAAc;IAEd,OAAOF,QAAQE;AACjB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { hasProperty, isObject } from '@metamask/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Get the error message from an unknown error type.
|
|
4
|
+
*
|
|
5
|
+
* - If the error is an object with a `message` property, return the message.
|
|
6
|
+
* - Otherwise, return the error converted to a string.
|
|
7
|
+
*
|
|
8
|
+
* @param error - The error to get the message from.
|
|
9
|
+
* @returns The error message.
|
|
10
|
+
*/ export function getErrorMessage(error) {
|
|
11
|
+
if (isObject(error) && hasProperty(error, 'message') && typeof error.message === 'string') {
|
|
12
|
+
return error.message;
|
|
13
|
+
}
|
|
14
|
+
return String(error);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import { hasProperty, isObject } from '@metamask/utils';\n\n/**\n * Get the error message from an unknown error type.\n *\n * - If the error is an object with a `message` property, return the message.\n * - Otherwise, return the error converted to a string.\n *\n * @param error - The error to get the message from.\n * @returns The error message.\n */\nexport function getErrorMessage(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'message') &&\n typeof error.message === 'string'\n ) {\n return error.message;\n }\n\n return String(error);\n}\n"],"names":["hasProperty","isObject","getErrorMessage","error","message","String"],"mappings":"AAAA,SAASA,WAAW,EAAEC,QAAQ,QAAQ,kBAAkB;AAExD;;;;;;;;CAQC,GACD,OAAO,SAASC,gBAAgBC,KAAc;IAC5C,IACEF,SAASE,UACTH,YAAYG,OAAO,cACnB,OAAOA,MAAMC,OAAO,KAAK,UACzB;QACA,OAAOD,MAAMC,OAAO;IACtB;IAEA,OAAOC,OAAOF;AAChB"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// eslint-disable-next-line import/no-unassigned-import
|
|
2
|
+
import 'ses/lockdown';
|
|
3
|
+
import { readFileSync } from 'fs';
|
|
4
|
+
import { generateMockEndowments } from './mock';
|
|
5
|
+
import { SNAP_EXPORT_NAMES } from './types';
|
|
6
|
+
lockdown({
|
|
7
|
+
consoleTaming: 'unsafe',
|
|
8
|
+
errorTaming: 'unsafe',
|
|
9
|
+
mathTaming: 'unsafe',
|
|
10
|
+
dateTaming: 'unsafe',
|
|
11
|
+
overrideTaming: 'severe',
|
|
12
|
+
// We disable domain taming, because it does not work in certain cases when
|
|
13
|
+
// running tests. This is unlikely to be a problem in production, because
|
|
14
|
+
// Node.js domains are deprecated.
|
|
15
|
+
domainTaming: 'unsafe'
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Get mock endowments that don't do anything. This is useful for running the
|
|
19
|
+
* eval, for snaps that try to communicate with the extension on initialisation,
|
|
20
|
+
* for example.
|
|
21
|
+
*
|
|
22
|
+
* @returns The mock endowments.
|
|
23
|
+
*/ function getMockEndowments() {
|
|
24
|
+
const endowments = generateMockEndowments();
|
|
25
|
+
return {
|
|
26
|
+
...endowments,
|
|
27
|
+
window: endowments,
|
|
28
|
+
self: endowments
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const snapFilePath = process.argv[2];
|
|
32
|
+
const snapModule = {
|
|
33
|
+
exports: {}
|
|
34
|
+
};
|
|
35
|
+
new Compartment({
|
|
36
|
+
...getMockEndowments(),
|
|
37
|
+
module: snapModule,
|
|
38
|
+
exports: snapModule.exports
|
|
39
|
+
}).evaluate(readFileSync(snapFilePath, 'utf8'));
|
|
40
|
+
const invalidExports = Object.keys(snapModule.exports).filter((snapExport)=>!SNAP_EXPORT_NAMES.includes(snapExport));
|
|
41
|
+
if (invalidExports.length > 0) {
|
|
42
|
+
// eslint-disable-next-line no-console
|
|
43
|
+
console.warn(`Invalid snap exports detected:\n${invalidExports.join('\n')}`);
|
|
44
|
+
}
|
|
45
|
+
setTimeout(()=>process.exit(0), 1000); // Hack to ensure worker exits
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=eval-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eval-worker.ts"],"sourcesContent":["// eslint-disable-next-line import/no-unassigned-import\nimport 'ses/lockdown';\n\nimport { readFileSync } from 'fs';\n\nimport type { HandlerType } from './handlers';\nimport { generateMockEndowments } from './mock';\nimport { SNAP_EXPORT_NAMES } from './types';\n\ndeclare let lockdown: any, Compartment: any;\n\nlockdown({\n consoleTaming: 'unsafe',\n errorTaming: 'unsafe',\n mathTaming: 'unsafe',\n dateTaming: 'unsafe',\n overrideTaming: 'severe',\n\n // We disable domain taming, because it does not work in certain cases when\n // running tests. This is unlikely to be a problem in production, because\n // Node.js domains are deprecated.\n domainTaming: 'unsafe',\n});\n\n/**\n * Get mock endowments that don't do anything. This is useful for running the\n * eval, for snaps that try to communicate with the extension on initialisation,\n * for example.\n *\n * @returns The mock endowments.\n */\nfunction getMockEndowments() {\n const endowments = generateMockEndowments();\n return {\n ...endowments,\n window: endowments,\n self: endowments,\n };\n}\n\nconst snapFilePath = process.argv[2];\n\nconst snapModule: { exports?: any } = { exports: {} };\n\nnew Compartment({\n ...getMockEndowments(),\n module: snapModule,\n exports: snapModule.exports,\n}).evaluate(readFileSync(snapFilePath, 'utf8'));\n\nconst invalidExports = Object.keys(snapModule.exports).filter(\n (snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport as HandlerType),\n);\n\nif (invalidExports.length > 0) {\n // eslint-disable-next-line no-console\n console.warn(`Invalid snap exports detected:\\n${invalidExports.join('\\n')}`);\n}\n\nsetTimeout(() => process.exit(0), 1000); // Hack to ensure worker exits\n"],"names":["readFileSync","generateMockEndowments","SNAP_EXPORT_NAMES","lockdown","consoleTaming","errorTaming","mathTaming","dateTaming","overrideTaming","domainTaming","getMockEndowments","endowments","window","self","snapFilePath","process","argv","snapModule","exports","Compartment","module","evaluate","invalidExports","Object","keys","filter","snapExport","includes","length","console","warn","join","setTimeout","exit"],"mappings":"AAAA,uDAAuD;AACvD,OAAO,eAAe;AAEtB,SAASA,YAAY,QAAQ,KAAK;AAGlC,SAASC,sBAAsB,QAAQ,SAAS;AAChD,SAASC,iBAAiB,QAAQ,UAAU;AAI5CC,SAAS;IACPC,eAAe;IACfC,aAAa;IACbC,YAAY;IACZC,YAAY;IACZC,gBAAgB;IAEhB,2EAA2E;IAC3E,yEAAyE;IACzE,kCAAkC;IAClCC,cAAc;AAChB;AAEA;;;;;;CAMC,GACD,SAASC;IACP,MAAMC,aAAaV;IACnB,OAAO;QACL,GAAGU,UAAU;QACbC,QAAQD;QACRE,MAAMF;IACR;AACF;AAEA,MAAMG,eAAeC,QAAQC,IAAI,CAAC,EAAE;AAEpC,MAAMC,aAAgC;IAAEC,SAAS,CAAC;AAAE;AAEpD,IAAIC,YAAY;IACd,GAAGT,mBAAmB;IACtBU,QAAQH;IACRC,SAASD,WAAWC,OAAO;AAC7B,GAAGG,QAAQ,CAACrB,aAAac,cAAc;AAEvC,MAAMQ,iBAAiBC,OAAOC,IAAI,CAACP,WAAWC,OAAO,EAAEO,MAAM,CAC3D,CAACC,aAAe,CAACxB,kBAAkByB,QAAQ,CAACD;AAG9C,IAAIJ,eAAeM,MAAM,GAAG,GAAG;IAC7B,sCAAsC;IACtCC,QAAQC,IAAI,CAAC,CAAC,gCAAgC,EAAER,eAAeS,IAAI,CAAC,MAAM,CAAC;AAC7E;AAEAC,WAAW,IAAMjB,QAAQkB,IAAI,CAAC,IAAI,OAAO,8BAA8B"}
|
package/dist/esm/eval.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
import { assert } from '@metamask/utils';
|
|
15
|
+
import { fork } from 'child_process';
|
|
16
|
+
import { join } from 'path';
|
|
17
|
+
import { validateFilePath } from './fs';
|
|
18
|
+
export class SnapEvalError extends Error {
|
|
19
|
+
constructor(message, output){
|
|
20
|
+
super(message);
|
|
21
|
+
_define_property(this, "output", void 0);
|
|
22
|
+
this.name = 'SnapEvalError';
|
|
23
|
+
this.output = output;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Spawn a new process to run the provided bundle in.
|
|
28
|
+
*
|
|
29
|
+
* @param bundlePath - The path to the bundle to run.
|
|
30
|
+
* @returns `null` if the worker ran successfully.
|
|
31
|
+
* @throws If the worker failed to run successfully.
|
|
32
|
+
*/ export async function evalBundle(bundlePath) {
|
|
33
|
+
await validateFilePath(bundlePath);
|
|
34
|
+
return new Promise((resolve, reject)=>{
|
|
35
|
+
const worker = fork(join(__dirname, 'eval-worker.js'), [
|
|
36
|
+
bundlePath
|
|
37
|
+
], {
|
|
38
|
+
// To avoid printing the output of the worker to the console, we set
|
|
39
|
+
// `stdio` to `pipe` and handle the output ourselves.
|
|
40
|
+
stdio: 'pipe'
|
|
41
|
+
});
|
|
42
|
+
let stdout = '';
|
|
43
|
+
let stderr = '';
|
|
44
|
+
assert(worker.stdout, '`stdout` should be defined.');
|
|
45
|
+
assert(worker.stderr, '`stderr` should be defined.');
|
|
46
|
+
worker.stdout.on('data', (data)=>{
|
|
47
|
+
stdout += data.toString();
|
|
48
|
+
});
|
|
49
|
+
worker.stderr.on('data', (data)=>{
|
|
50
|
+
stderr += data.toString();
|
|
51
|
+
});
|
|
52
|
+
worker.on('exit', (exitCode)=>{
|
|
53
|
+
const output = {
|
|
54
|
+
stdout,
|
|
55
|
+
stderr
|
|
56
|
+
};
|
|
57
|
+
if (exitCode === 0) {
|
|
58
|
+
return resolve(output);
|
|
59
|
+
}
|
|
60
|
+
return reject(new SnapEvalError(`Process exited with non-zero exit code: ${exitCode}.`, output));
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//# sourceMappingURL=eval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eval.ts"],"sourcesContent":["import { assert } from '@metamask/utils';\nimport { fork } from 'child_process';\nimport { join } from 'path';\n\nimport { validateFilePath } from './fs';\n\nexport type EvalOutput = {\n stdout: string;\n stderr: string;\n};\n\nexport class SnapEvalError extends Error {\n readonly output: EvalOutput;\n\n constructor(message: string, output: EvalOutput) {\n super(message);\n\n this.name = 'SnapEvalError';\n this.output = output;\n }\n}\n\n/**\n * Spawn a new process to run the provided bundle in.\n *\n * @param bundlePath - The path to the bundle to run.\n * @returns `null` if the worker ran successfully.\n * @throws If the worker failed to run successfully.\n */\nexport async function evalBundle(bundlePath: string): Promise<EvalOutput> {\n await validateFilePath(bundlePath);\n\n return new Promise((resolve, reject) => {\n const worker = fork(join(__dirname, 'eval-worker.js'), [bundlePath], {\n // To avoid printing the output of the worker to the console, we set\n // `stdio` to `pipe` and handle the output ourselves.\n stdio: 'pipe',\n });\n\n let stdout = '';\n let stderr = '';\n\n assert(worker.stdout, '`stdout` should be defined.');\n assert(worker.stderr, '`stderr` should be defined.');\n\n worker.stdout.on('data', (data: Buffer) => {\n stdout += data.toString();\n });\n\n worker.stderr.on('data', (data: Buffer) => {\n stderr += data.toString();\n });\n\n worker.on('exit', (exitCode: number) => {\n const output = {\n stdout,\n stderr,\n };\n\n if (exitCode === 0) {\n return resolve(output);\n }\n\n return reject(\n new SnapEvalError(\n `Process exited with non-zero exit code: ${exitCode}.`,\n output,\n ),\n );\n });\n });\n}\n"],"names":["assert","fork","join","validateFilePath","SnapEvalError","Error","constructor","message","output","name","evalBundle","bundlePath","Promise","resolve","reject","worker","__dirname","stdio","stdout","stderr","on","data","toString","exitCode"],"mappings":";;;;;;;;;;;;;AAAA,SAASA,MAAM,QAAQ,kBAAkB;AACzC,SAASC,IAAI,QAAQ,gBAAgB;AACrC,SAASC,IAAI,QAAQ,OAAO;AAE5B,SAASC,gBAAgB,QAAQ,OAAO;AAOxC,OAAO,MAAMC,sBAAsBC;IAGjCC,YAAYC,OAAe,EAAEC,MAAkB,CAAE;QAC/C,KAAK,CAACD;QAHR,uBAASC,UAAT,KAAA;QAKE,IAAI,CAACC,IAAI,GAAG;QACZ,IAAI,CAACD,MAAM,GAAGA;IAChB;AACF;AAEA;;;;;;CAMC,GACD,OAAO,eAAeE,WAAWC,UAAkB;IACjD,MAAMR,iBAAiBQ;IAEvB,OAAO,IAAIC,QAAQ,CAACC,SAASC;QAC3B,MAAMC,SAASd,KAAKC,KAAKc,WAAW,mBAAmB;YAACL;SAAW,EAAE;YACnE,oEAAoE;YACpE,qDAAqD;YACrDM,OAAO;QACT;QAEA,IAAIC,SAAS;QACb,IAAIC,SAAS;QAEbnB,OAAOe,OAAOG,MAAM,EAAE;QACtBlB,OAAOe,OAAOI,MAAM,EAAE;QAEtBJ,OAAOG,MAAM,CAACE,EAAE,CAAC,QAAQ,CAACC;YACxBH,UAAUG,KAAKC,QAAQ;QACzB;QAEAP,OAAOI,MAAM,CAACC,EAAE,CAAC,QAAQ,CAACC;YACxBF,UAAUE,KAAKC,QAAQ;QACzB;QAEAP,OAAOK,EAAE,CAAC,QAAQ,CAACG;YACjB,MAAMf,SAAS;gBACbU;gBACAC;YACF;YAEA,IAAII,aAAa,GAAG;gBAClB,OAAOV,QAAQL;YACjB;YAEA,OAAOM,OACL,IAAIV,cACF,CAAC,wCAAwC,EAAEmB,SAAS,CAAC,CAAC,EACtDf;QAGN;IACF;AACF"}
|
package/dist/{fs.js → esm/fs.js}
RENAMED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
exports.useTemporaryFile = exports.validateDirPath = exports.validateFilePath = exports.validateOutfileName = exports.getOutfilePath = exports.readJsonFile = exports.isFile = exports.isDirectory = void 0;
|
|
7
|
-
const fs_1 = require("fs");
|
|
8
|
-
const os_1 = __importDefault(require("os"));
|
|
9
|
-
const path_1 = __importDefault(require("path"));
|
|
10
|
-
const json_1 = require("./json");
|
|
11
|
-
const virtual_file_1 = require("./virtual-file");
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import pathUtils from 'path';
|
|
4
|
+
import { parseJson } from './json';
|
|
5
|
+
import { readVirtualFile } from './virtual-file';
|
|
12
6
|
/**
|
|
13
7
|
* Checks whether the given path string resolves to an existing directory, and
|
|
14
8
|
* optionally creates the directory if it doesn't exist.
|
|
@@ -16,64 +10,57 @@ const virtual_file_1 = require("./virtual-file");
|
|
|
16
10
|
* @param pathString - The path string to check.
|
|
17
11
|
* @param createDir - Whether to create the directory if it doesn't exist.
|
|
18
12
|
* @returns Whether the given path is an existing directory.
|
|
19
|
-
*/
|
|
20
|
-
async function isDirectory(pathString, createDir) {
|
|
13
|
+
*/ export async function isDirectory(pathString, createDir) {
|
|
21
14
|
try {
|
|
22
|
-
const stats = await
|
|
15
|
+
const stats = await fs.stat(pathString);
|
|
23
16
|
return stats.isDirectory();
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
17
|
+
} catch (error) {
|
|
26
18
|
if (error.code === 'ENOENT') {
|
|
27
19
|
if (!createDir) {
|
|
28
20
|
return false;
|
|
29
21
|
}
|
|
30
|
-
await
|
|
22
|
+
await fs.mkdir(pathString, {
|
|
23
|
+
recursive: true
|
|
24
|
+
});
|
|
31
25
|
return true;
|
|
32
26
|
}
|
|
33
27
|
return false;
|
|
34
28
|
}
|
|
35
29
|
}
|
|
36
|
-
exports.isDirectory = isDirectory;
|
|
37
30
|
/**
|
|
38
31
|
* Checks whether the given path string resolves to an existing file.
|
|
39
32
|
*
|
|
40
33
|
* @param pathString - The path string to check.
|
|
41
34
|
* @returns Whether the given path is an existing file.
|
|
42
|
-
*/
|
|
43
|
-
async function isFile(pathString) {
|
|
35
|
+
*/ export async function isFile(pathString) {
|
|
44
36
|
try {
|
|
45
|
-
const stats = await
|
|
37
|
+
const stats = await fs.stat(pathString);
|
|
46
38
|
return stats.isFile();
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
39
|
+
} catch (error) {
|
|
49
40
|
return false;
|
|
50
41
|
}
|
|
51
42
|
}
|
|
52
|
-
exports.isFile = isFile;
|
|
53
43
|
/**
|
|
54
44
|
* Reads a `.json` file, parses its contents, and returns them.
|
|
55
45
|
*
|
|
56
46
|
* @param pathString - The path to the JSON file.
|
|
57
47
|
* @returns The parsed contents of the JSON file.
|
|
58
|
-
*/
|
|
59
|
-
async function readJsonFile(pathString) {
|
|
48
|
+
*/ export async function readJsonFile(pathString) {
|
|
60
49
|
if (!pathString.endsWith('.json')) {
|
|
61
50
|
throw new Error('The specified file must be a ".json" file.');
|
|
62
51
|
}
|
|
63
52
|
let file;
|
|
64
53
|
try {
|
|
65
|
-
file = await
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
54
|
+
file = await readVirtualFile(pathString, 'utf8');
|
|
55
|
+
} catch (error) {
|
|
68
56
|
if (error.code === 'ENOENT') {
|
|
69
57
|
throw new Error(`Could not find '${pathString}'. Please ensure that the file exists.`);
|
|
70
58
|
}
|
|
71
59
|
throw error;
|
|
72
60
|
}
|
|
73
|
-
file.result =
|
|
61
|
+
file.result = parseJson(file.toString());
|
|
74
62
|
return file;
|
|
75
63
|
}
|
|
76
|
-
exports.readJsonFile = readJsonFile;
|
|
77
64
|
/**
|
|
78
65
|
* Gets the complete out file path from an output file name and parent
|
|
79
66
|
* directory path.
|
|
@@ -81,11 +68,9 @@ exports.readJsonFile = readJsonFile;
|
|
|
81
68
|
* @param outDir - The path to the out file's parent directory.
|
|
82
69
|
* @param outFileName - The out file's name.
|
|
83
70
|
* @returns The complete path to the out file.
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
return path_1.default.join(outDir, outFileName || 'bundle.js');
|
|
71
|
+
*/ export function getOutfilePath(outDir, outFileName) {
|
|
72
|
+
return pathUtils.join(outDir, outFileName || 'bundle.js');
|
|
87
73
|
}
|
|
88
|
-
exports.getOutfilePath = getOutfilePath;
|
|
89
74
|
/**
|
|
90
75
|
* Ensures that the outfile name is just a `.js` file name.
|
|
91
76
|
* Throws on validation failure.
|
|
@@ -93,31 +78,25 @@ exports.getOutfilePath = getOutfilePath;
|
|
|
93
78
|
* @param filename - The file name to validate.
|
|
94
79
|
* @returns `true` if validation succeeded.
|
|
95
80
|
* @throws If the file name is invalid.
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
if (!filename.endsWith('.js') ||
|
|
99
|
-
filename === '.js' ||
|
|
100
|
-
path_1.default.basename(filename) !== filename) {
|
|
81
|
+
*/ export function validateOutfileName(filename) {
|
|
82
|
+
if (!filename.endsWith('.js') || filename === '.js' || pathUtils.basename(filename) !== filename) {
|
|
101
83
|
throw new Error(`Invalid outfile name: ${filename}. Must be a .js file`);
|
|
102
84
|
}
|
|
103
85
|
return true;
|
|
104
86
|
}
|
|
105
|
-
exports.validateOutfileName = validateOutfileName;
|
|
106
87
|
/**
|
|
107
88
|
* Validates a file path. Throws on validation failure.
|
|
108
89
|
*
|
|
109
90
|
* @param filePath - The file path to validate.
|
|
110
91
|
* @returns `true` if validation succeeded.
|
|
111
92
|
* @throws If the path does not resolve to a file.
|
|
112
|
-
*/
|
|
113
|
-
async function validateFilePath(filePath) {
|
|
93
|
+
*/ export async function validateFilePath(filePath) {
|
|
114
94
|
const exists = await isFile(filePath);
|
|
115
95
|
if (!exists) {
|
|
116
96
|
throw new Error(`Invalid params: '${filePath}' is not a file or does not exist.`);
|
|
117
97
|
}
|
|
118
98
|
return true;
|
|
119
99
|
}
|
|
120
|
-
exports.validateFilePath = validateFilePath;
|
|
121
100
|
/**
|
|
122
101
|
* Validates a directory path. Throws on validation failure.
|
|
123
102
|
*
|
|
@@ -125,15 +104,13 @@ exports.validateFilePath = validateFilePath;
|
|
|
125
104
|
* @param createDir - Whether to create the directory if it doesn't exist.
|
|
126
105
|
* @returns `true` if validation succeeded or the directory was created.
|
|
127
106
|
* @throws If the directory does not exist or could not be created.
|
|
128
|
-
*/
|
|
129
|
-
async function validateDirPath(dirPath, createDir) {
|
|
107
|
+
*/ export async function validateDirPath(dirPath, createDir) {
|
|
130
108
|
const exists = await isDirectory(dirPath, createDir);
|
|
131
109
|
if (!exists) {
|
|
132
110
|
throw new Error(`Invalid params: '${dirPath}' is not a directory or could not be created.`);
|
|
133
111
|
}
|
|
134
112
|
return true;
|
|
135
113
|
}
|
|
136
|
-
exports.validateDirPath = validateDirPath;
|
|
137
114
|
/**
|
|
138
115
|
* Creates a temporary file with a given name and content, writes it to disk and calls the provided function.
|
|
139
116
|
* This function handles deletion of the temporary file after usage.
|
|
@@ -141,19 +118,19 @@ exports.validateDirPath = validateDirPath;
|
|
|
141
118
|
* @param fileName - The name of the temporary file.
|
|
142
119
|
* @param fileContents - The content of the temporary file.
|
|
143
120
|
* @param fn - The callback function to call when the temporary file has been created.
|
|
144
|
-
*/
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
121
|
+
*/ export async function useTemporaryFile(fileName, fileContents, fn) {
|
|
122
|
+
const filePath = pathUtils.join(os.tmpdir(), fileName);
|
|
123
|
+
await fs.mkdir(pathUtils.dirname(filePath), {
|
|
124
|
+
recursive: true
|
|
125
|
+
});
|
|
126
|
+
await fs.writeFile(filePath, fileContents);
|
|
149
127
|
try {
|
|
150
128
|
await fn(filePath);
|
|
151
|
-
}
|
|
152
|
-
finally {
|
|
129
|
+
} finally{
|
|
153
130
|
if (await isFile(filePath)) {
|
|
154
|
-
await
|
|
131
|
+
await fs.unlink(filePath);
|
|
155
132
|
}
|
|
156
133
|
}
|
|
157
134
|
}
|
|
158
|
-
|
|
135
|
+
|
|
159
136
|
//# sourceMappingURL=fs.js.map
|