@metamask/snaps-execution-environments 6.8.0 → 6.9.1
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 +17 -1
- package/dist/browserify/iframe/bundle.js +1 -1
- package/dist/browserify/node-process/bundle.js +1 -1
- package/dist/browserify/node-thread/bundle.js +1 -1
- package/dist/browserify/webview/index.html +1 -1
- package/dist/browserify/worker-executor/bundle.js +1 -1
- package/dist/browserify/worker-pool/bundle.js +1 -1
- package/dist/common/BaseSnapExecutor.cjs +58 -58
- package/dist/common/BaseSnapExecutor.cjs.map +1 -1
- package/dist/common/BaseSnapExecutor.mjs +58 -58
- package/dist/common/BaseSnapExecutor.mjs.map +1 -1
- package/dist/common/endowments/network.cjs +52 -39
- package/dist/common/endowments/network.cjs.map +1 -1
- package/dist/common/endowments/network.mjs +52 -39
- package/dist/common/endowments/network.mjs.map +1 -1
- package/dist/common/utils.cjs +0 -1
- package/dist/common/utils.cjs.map +1 -1
- package/dist/common/utils.d.cts.map +1 -1
- package/dist/common/utils.d.mts.map +1 -1
- package/dist/common/utils.mjs +0 -1
- package/dist/common/utils.mjs.map +1 -1
- package/dist/proxy/ProxySnapExecutor.cjs +69 -72
- package/dist/proxy/ProxySnapExecutor.cjs.map +1 -1
- package/dist/proxy/ProxySnapExecutor.mjs +68 -71
- package/dist/proxy/ProxySnapExecutor.mjs.map +1 -1
- package/dist/webview/WebViewExecutorStream.cjs +22 -9
- package/dist/webview/WebViewExecutorStream.cjs.map +1 -1
- package/dist/webview/WebViewExecutorStream.mjs +22 -9
- package/dist/webview/WebViewExecutorStream.mjs.map +1 -1
- package/dist/webworker/pool/WebWorkerPool.cjs +130 -133
- package/dist/webworker/pool/WebWorkerPool.cjs.map +1 -1
- package/dist/webworker/pool/WebWorkerPool.mjs +129 -132
- package/dist/webworker/pool/WebWorkerPool.mjs.map +1 -1
- package/package.json +3 -3
package/dist/common/utils.cjs
CHANGED
|
@@ -70,7 +70,6 @@ exports.BLOCKED_RPC_METHODS = Object.freeze([
|
|
|
70
70
|
'wallet_revokePermissions',
|
|
71
71
|
// We disallow all of these confirmations for now, since the screens are not ready for Snaps.
|
|
72
72
|
'eth_sendTransaction',
|
|
73
|
-
'eth_sign',
|
|
74
73
|
'eth_signTypedData',
|
|
75
74
|
'eth_signTypedData_v1',
|
|
76
75
|
'eth_signTypedData_v3',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":";;;AACA,qDAAiD;AACjD,2CAOyB;AAEzB,4CAAiC;AAEjC,4EAA4E;AAC5E,yDAAyD;AACzD,MAAM,sBAAsB,GAAG,QAAU,CAAC;AAE1C;;;;;;;;;GASG;AACI,KAAK,UAAU,YAAY,CAChC,eAA8B,EAC9B,WAAqC;IAErC,MAAM,UAAU,GAAG,WAAW,CAAC,YAAY,CAAC;IAC5C,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,eAAe;aACZ,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE;YACd,IAAI,WAAW,CAAC,YAAY,KAAK,UAAU,EAAE,CAAC;gBAC5C,OAAO,CAAC,KAAK,CAAC,CAAC;YACjB,CAAC;iBAAM,CAAC;gBACN,IAAA,aAAG,EACD,+EAA+E,CAChF,CAAC;YACJ,CAAC;QACH,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE;YAChB,IAAI,WAAW,CAAC,YAAY,KAAK,UAAU,EAAE,CAAC;gBAC5C,MAAM,CAAC,MAAM,CAAC,CAAC;YACjB,CAAC;iBAAM,CAAC;gBACN,IAAA,aAAG,EACD,+EAA+E,CAChF,CAAC;YACJ,CAAC;QACH,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACL,CAAC;AA1BD,oCA0BC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAC,OAAg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AC,CAAC,CAAC;IAC/C,OAAO,IAAA,mBAAW,EAAC,IAAI,CAAqB,CAAC;AAC/C,CAAC;AALD,4DAKC;AAED;;;;;GAKG;AACH,SAAgB,eAAe,CAAC,QAAiC;IAC/D,IAAI,CAAC,IAAA,gBAAQ,EAAC,QAAQ,CAAC,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,CAAC;QACH,qEAAqE;QACrE,MAAM,IAAI,GAAG,IAAA,mBAAW,EAAC,QAAQ,CAAC,CAAC;QACnC,OAAO,IAAI,GAAG,sBAAsB,CAAC;IACvC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAZD,0CAYC","sourcesContent":["import type { StreamProvider, RequestArguments } from '@metamask/providers';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n assert,\n assertStruct,\n getJsonSize,\n getSafeJson,\n isObject,\n JsonStruct,\n} from '@metamask/utils';\n\nimport { log } from '../logging';\n\n// 64 MB - we chose this number because it is the size limit for postMessage\n// between the extension and the dapp enforced by Chrome.\nconst MAX_RESPONSE_JSON_SIZE = 64_000_000;\n\n/**\n * Make proxy for Promise and handle the teardown process properly.\n * If the teardown is called in the meanwhile, Promise result will not be\n * exposed to the snap anymore and warning will be logged to the console.\n *\n * @param originalPromise - Original promise.\n * @param teardownRef - Reference containing teardown count.\n * @param teardownRef.lastTeardown - Number of the last teardown.\n * @returns New proxy promise.\n */\nexport async function withTeardown<Type>(\n originalPromise: Promise<Type>,\n teardownRef: { lastTeardown: number },\n): Promise<Type> {\n const myTeardown = teardownRef.lastTeardown;\n return new Promise<Type>((resolve, reject) => {\n originalPromise\n .then((value) => {\n if (teardownRef.lastTeardown === myTeardown) {\n resolve(value);\n } else {\n log(\n 'Late promise received after Snap finished execution. Promise will be dropped.',\n );\n }\n })\n .catch((reason) => {\n if (teardownRef.lastTeardown === myTeardown) {\n reject(reason);\n } else {\n log(\n 'Late promise received after Snap finished execution. Promise will be dropped.',\n );\n }\n });\n });\n}\n\n/**\n * Returns a Proxy that only allows access to a `request` function.\n * This is useful for replacing StreamProvider with an attenuated version.\n *\n * @param request - Custom attenuated request function.\n * @returns Proxy that mimics a StreamProvider instance.\n */\nexport function proxyStreamProvider(request: unknown): StreamProvider {\n // Proxy target is intentionally set to be an empty object, to ensure\n // that access to the prototype chain is not possible.\n const proxy = new Proxy(\n {},\n {\n has(_target: object, prop: string | symbol) {\n return typeof prop === 'string' && ['request'].includes(prop);\n },\n get(_target, prop: keyof StreamProvider) {\n if (prop === 'request') {\n return request;\n }\n\n return undefined;\n },\n },\n );\n\n return proxy as StreamProvider;\n}\n\n// We're blocking these RPC methods for v1, will revisit later.\nexport const BLOCKED_RPC_METHODS = Object.freeze([\n 'wallet_requestPermissions',\n 'wallet_revokePermissions',\n // We disallow all of these confirmations for now, since the screens are not ready for Snaps.\n 'eth_sendTransaction',\n 'eth_signTypedData',\n 'eth_signTypedData_v1',\n 'eth_signTypedData_v3',\n 'eth_signTypedData_v4',\n 'eth_decrypt',\n 'eth_getEncryptionPublicKey',\n 'wallet_addEthereumChain',\n 'wallet_switchEthereumChain',\n 'wallet_watchAsset',\n 'wallet_registerOnboarding',\n 'wallet_scanQRCode',\n]);\n\n/**\n * Asserts the validity of request arguments for a snap outbound request using the `snap.request` API.\n *\n * @param args - The arguments to validate.\n */\nexport function assertSnapOutboundRequest(args: RequestArguments) {\n // Disallow any non `wallet_` or `snap_` methods for separation of concerns.\n assert(\n String.prototype.startsWith.call(args.method, 'wallet_') ||\n String.prototype.startsWith.call(args.method, 'snap_'),\n 'The global Snap API only allows RPC methods starting with `wallet_*` and `snap_*`.',\n rpcErrors.methodNotSupported,\n );\n assert(\n !BLOCKED_RPC_METHODS.includes(args.method),\n rpcErrors.methodNotFound({\n data: {\n method: args.method,\n },\n }),\n );\n assertStruct(\n args,\n JsonStruct,\n 'Provided value is not JSON-RPC compatible',\n rpcErrors.invalidParams,\n );\n}\n\n/**\n * Asserts the validity of request arguments for an ethereum outbound request using the `ethereum.request` API.\n *\n * @param args - The arguments to validate.\n */\nexport function assertEthereumOutboundRequest(args: RequestArguments) {\n // Disallow snaps methods for separation of concerns.\n assert(\n !String.prototype.startsWith.call(args.method, 'snap_'),\n rpcErrors.methodNotFound({\n data: {\n method: args.method,\n },\n }),\n );\n assert(\n !BLOCKED_RPC_METHODS.includes(args.method),\n rpcErrors.methodNotFound({\n data: {\n method: args.method,\n },\n }),\n );\n assertStruct(\n args,\n JsonStruct,\n 'Provided value is not JSON-RPC compatible',\n rpcErrors.invalidParams,\n );\n}\n\n/**\n * Gets a sanitized value to be used for passing to the underlying MetaMask provider.\n *\n * @param value - An unsanitized value from a snap.\n * @returns A sanitized value ready to be passed to a MetaMask provider.\n */\nexport function sanitizeRequestArguments(value: unknown): RequestArguments {\n // Before passing to getSafeJson we run the value through JSON serialization.\n // This lets request arguments contain undefined which is normally disallowed.\n const json = JSON.parse(JSON.stringify(value));\n return getSafeJson(json) as RequestArguments;\n}\n\n/**\n * Check if the input is a valid response.\n *\n * @param response - The response.\n * @returns True if the response is valid, otherwise false.\n */\nexport function isValidResponse(response: Record<string, unknown>) {\n if (!isObject(response)) {\n return false;\n }\n\n try {\n // If the JSON is invalid this will throw and we should return false.\n const size = getJsonSize(response);\n return size < MAX_RESPONSE_JSON_SIZE;\n } catch {\n return false;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,4BAA4B;AAiB5E;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,IAAI,EACrC,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,EAC9B,WAAW,EAAE;IAAE,YAAY,EAAE,MAAM,CAAA;CAAE,GACpC,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,CAoBpE;AAGD,eAAO,MAAM,mBAAmB,
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,4BAA4B;AAiB5E;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,IAAI,EACrC,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,EAC9B,WAAW,EAAE;IAAE,YAAY,EAAE,MAAM,CAAA;CAAE,GACpC,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,CAoBpE;AAGD,eAAO,MAAM,mBAAmB,mBAgB9B,CAAC;AAEH;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,gBAAgB,QAsB/D;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,gBAAgB,QAwBnE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,OAAO,GAAG,gBAAgB,CAKzE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WAYhE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,4BAA4B;AAiB5E;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,IAAI,EACrC,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,EAC9B,WAAW,EAAE;IAAE,YAAY,EAAE,MAAM,CAAA;CAAE,GACpC,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,CAoBpE;AAGD,eAAO,MAAM,mBAAmB,
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,4BAA4B;AAiB5E;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,IAAI,EACrC,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,EAC9B,WAAW,EAAE;IAAE,YAAY,EAAE,MAAM,CAAA;CAAE,GACpC,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,CAoBpE;AAGD,eAAO,MAAM,mBAAmB,mBAgB9B,CAAC;AAEH;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,gBAAgB,QAsB/D;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,gBAAgB,QAwBnE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,OAAO,GAAG,gBAAgB,CAKzE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WAYhE"}
|
package/dist/common/utils.mjs
CHANGED
|
@@ -65,7 +65,6 @@ export const BLOCKED_RPC_METHODS = Object.freeze([
|
|
|
65
65
|
'wallet_revokePermissions',
|
|
66
66
|
// We disallow all of these confirmations for now, since the screens are not ready for Snaps.
|
|
67
67
|
'eth_sendTransaction',
|
|
68
|
-
'eth_sign',
|
|
69
68
|
'eth_signTypedData',
|
|
70
69
|
'eth_signTypedData_v1',
|
|
71
70
|
'eth_signTypedData_v3',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../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CAAC,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,CAAC;QACH,qEAAqE;QACrE,MAAM,IAAI,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;QACnC,OAAO,IAAI,GAAG,sBAAsB,CAAC;IACvC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC","sourcesContent":["import type { StreamProvider, RequestArguments } from '@metamask/providers';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n assert,\n assertStruct,\n getJsonSize,\n getSafeJson,\n isObject,\n JsonStruct,\n} from '@metamask/utils';\n\nimport { log } from '../logging';\n\n// 64 MB - we chose this number because it is the size limit for postMessage\n// between the extension and the dapp enforced by Chrome.\nconst MAX_RESPONSE_JSON_SIZE = 64_000_000;\n\n/**\n * Make proxy for Promise and handle the teardown process properly.\n * If the teardown is called in the meanwhile, Promise result will not be\n * exposed to the snap anymore and warning will be logged to the console.\n *\n * @param originalPromise - Original promise.\n * @param teardownRef - Reference containing teardown count.\n * @param teardownRef.lastTeardown - Number of the last teardown.\n * @returns New proxy promise.\n */\nexport async function withTeardown<Type>(\n originalPromise: Promise<Type>,\n teardownRef: { lastTeardown: number },\n): Promise<Type> {\n const myTeardown = teardownRef.lastTeardown;\n return new Promise<Type>((resolve, reject) => {\n originalPromise\n .then((value) => {\n if (teardownRef.lastTeardown === myTeardown) {\n resolve(value);\n } else {\n log(\n 'Late promise received after Snap finished execution. Promise will be dropped.',\n );\n }\n })\n .catch((reason) => {\n if (teardownRef.lastTeardown === myTeardown) {\n reject(reason);\n } else {\n log(\n 'Late promise received after Snap finished execution. Promise will be dropped.',\n );\n }\n });\n });\n}\n\n/**\n * Returns a Proxy that only allows access to a `request` function.\n * This is useful for replacing StreamProvider with an attenuated version.\n *\n * @param request - Custom attenuated request function.\n * @returns Proxy that mimics a StreamProvider instance.\n */\nexport function proxyStreamProvider(request: unknown): StreamProvider {\n // Proxy target is intentionally set to be an empty object, to ensure\n // that access to the prototype chain is not possible.\n const proxy = new Proxy(\n {},\n {\n has(_target: object, prop: string | symbol) {\n return typeof prop === 'string' && ['request'].includes(prop);\n },\n get(_target, prop: keyof StreamProvider) {\n if (prop === 'request') {\n return request;\n }\n\n return undefined;\n },\n },\n );\n\n return proxy as StreamProvider;\n}\n\n// We're blocking these RPC methods for v1, will revisit later.\nexport const BLOCKED_RPC_METHODS = Object.freeze([\n 'wallet_requestPermissions',\n 'wallet_revokePermissions',\n // We disallow all of these confirmations for now, since the screens are not ready for Snaps.\n 'eth_sendTransaction',\n 'eth_signTypedData',\n 'eth_signTypedData_v1',\n 'eth_signTypedData_v3',\n 'eth_signTypedData_v4',\n 'eth_decrypt',\n 'eth_getEncryptionPublicKey',\n 'wallet_addEthereumChain',\n 'wallet_switchEthereumChain',\n 'wallet_watchAsset',\n 'wallet_registerOnboarding',\n 'wallet_scanQRCode',\n]);\n\n/**\n * Asserts the validity of request arguments for a snap outbound request using the `snap.request` API.\n *\n * @param args - The arguments to validate.\n */\nexport function assertSnapOutboundRequest(args: RequestArguments) {\n // Disallow any non `wallet_` or `snap_` methods for separation of concerns.\n assert(\n String.prototype.startsWith.call(args.method, 'wallet_') ||\n String.prototype.startsWith.call(args.method, 'snap_'),\n 'The global Snap API only allows RPC methods starting with `wallet_*` and `snap_*`.',\n rpcErrors.methodNotSupported,\n );\n assert(\n !BLOCKED_RPC_METHODS.includes(args.method),\n rpcErrors.methodNotFound({\n data: {\n method: args.method,\n },\n }),\n );\n assertStruct(\n args,\n JsonStruct,\n 'Provided value is not JSON-RPC compatible',\n rpcErrors.invalidParams,\n );\n}\n\n/**\n * Asserts the validity of request arguments for an ethereum outbound request using the `ethereum.request` API.\n *\n * @param args - The arguments to validate.\n */\nexport function assertEthereumOutboundRequest(args: RequestArguments) {\n // Disallow snaps methods for separation of concerns.\n assert(\n !String.prototype.startsWith.call(args.method, 'snap_'),\n rpcErrors.methodNotFound({\n data: {\n method: args.method,\n },\n }),\n );\n assert(\n !BLOCKED_RPC_METHODS.includes(args.method),\n rpcErrors.methodNotFound({\n data: {\n method: args.method,\n },\n }),\n );\n assertStruct(\n args,\n JsonStruct,\n 'Provided value is not JSON-RPC compatible',\n rpcErrors.invalidParams,\n );\n}\n\n/**\n * Gets a sanitized value to be used for passing to the underlying MetaMask provider.\n *\n * @param value - An unsanitized value from a snap.\n * @returns A sanitized value ready to be passed to a MetaMask provider.\n */\nexport function sanitizeRequestArguments(value: unknown): RequestArguments {\n // Before passing to getSafeJson we run the value through JSON serialization.\n // This lets request arguments contain undefined which is normally disallowed.\n const json = JSON.parse(JSON.stringify(value));\n return getSafeJson(json) as RequestArguments;\n}\n\n/**\n * Check if the input is a valid response.\n *\n * @param response - The response.\n * @returns True if the response is valid, otherwise false.\n */\nexport function isValidResponse(response: Record<string, unknown>) {\n if (!isObject(response)) {\n return false;\n }\n\n try {\n // If the JSON is invalid this will throw and we should return false.\n const size = getJsonSize(response);\n return size < MAX_RESPONSE_JSON_SIZE;\n } catch {\n return false;\n }\n}\n"]}
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
2
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
15
|
};
|
|
16
|
+
var _ProxySnapExecutor_instances, _ProxySnapExecutor_stream, _ProxySnapExecutor_frameUrl, _ProxySnapExecutor_onData, _ProxySnapExecutor_initializeJob, _ProxySnapExecutor_terminateJob;
|
|
5
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
18
|
exports.ProxySnapExecutor = void 0;
|
|
7
19
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
@@ -24,9 +36,6 @@ const IFRAME_URL = `https://execution.metamask.io/iframe/${package_json_1.defaul
|
|
|
24
36
|
* acts as a proxy between the client and the iframe execution environment.
|
|
25
37
|
*/
|
|
26
38
|
class ProxySnapExecutor {
|
|
27
|
-
#stream;
|
|
28
|
-
#frameUrl;
|
|
29
|
-
jobs = {};
|
|
30
39
|
/**
|
|
31
40
|
* Initialize the executor with the given stream. This is a wrapper around the
|
|
32
41
|
* constructor.
|
|
@@ -39,77 +48,65 @@ class ProxySnapExecutor {
|
|
|
39
48
|
return new ProxySnapExecutor(stream, frameUrl);
|
|
40
49
|
}
|
|
41
50
|
constructor(stream, frameUrl) {
|
|
42
|
-
this
|
|
43
|
-
|
|
44
|
-
this
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* request in the `data` property.
|
|
50
|
-
*
|
|
51
|
-
* @param data - The message data.
|
|
52
|
-
* @param data.data - The JSON-RPC request.
|
|
53
|
-
* @param data.jobId - The job ID.
|
|
54
|
-
*/
|
|
55
|
-
#onData(data) {
|
|
56
|
-
const { jobId, data: request } = data;
|
|
57
|
-
if (!this.jobs[jobId]) {
|
|
58
|
-
// This ensures that a job is initialized before it is used. To avoid
|
|
59
|
-
// code duplication, we call the `#onData` method again, which will
|
|
60
|
-
// run the rest of the logic after initialization.
|
|
61
|
-
this.#initializeJob(jobId)
|
|
62
|
-
.then(() => {
|
|
63
|
-
this.#onData(data);
|
|
64
|
-
})
|
|
65
|
-
.catch((error) => {
|
|
66
|
-
(0, snaps_utils_1.logError)('[Worker] Error initializing job:', error);
|
|
67
|
-
});
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
71
|
-
// itself does not have access to the iframes directly.
|
|
72
|
-
if (request.method === 'terminateJob') {
|
|
73
|
-
this.#terminateJob(jobId);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
this.jobs[jobId].stream.write(request);
|
|
51
|
+
_ProxySnapExecutor_instances.add(this);
|
|
52
|
+
_ProxySnapExecutor_stream.set(this, void 0);
|
|
53
|
+
_ProxySnapExecutor_frameUrl.set(this, void 0);
|
|
54
|
+
this.jobs = {};
|
|
55
|
+
__classPrivateFieldSet(this, _ProxySnapExecutor_stream, stream, "f");
|
|
56
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").on('data', __classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).bind(this));
|
|
57
|
+
__classPrivateFieldSet(this, _ProxySnapExecutor_frameUrl, frameUrl, "f");
|
|
77
58
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
93
|
-
jobStream.on('data', (data) => {
|
|
94
|
-
this.#stream.write({ data, jobId });
|
|
59
|
+
}
|
|
60
|
+
exports.ProxySnapExecutor = ProxySnapExecutor;
|
|
61
|
+
_ProxySnapExecutor_stream = new WeakMap(), _ProxySnapExecutor_frameUrl = new WeakMap(), _ProxySnapExecutor_instances = new WeakSet(), _ProxySnapExecutor_onData = function _ProxySnapExecutor_onData(data) {
|
|
62
|
+
const { jobId, data: request } = data;
|
|
63
|
+
if (!this.jobs[jobId]) {
|
|
64
|
+
// This ensures that a job is initialized before it is used. To avoid
|
|
65
|
+
// code duplication, we call the `#onData` method again, which will
|
|
66
|
+
// run the rest of the logic after initialization.
|
|
67
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_initializeJob).call(this, jobId)
|
|
68
|
+
.then(() => {
|
|
69
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).call(this, data);
|
|
70
|
+
})
|
|
71
|
+
.catch((error) => {
|
|
72
|
+
(0, snaps_utils_1.logError)('[Worker] Error initializing job:', error);
|
|
95
73
|
});
|
|
96
|
-
|
|
97
|
-
return this.jobs[jobId];
|
|
74
|
+
return;
|
|
98
75
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
*/
|
|
105
|
-
#terminateJob(jobId) {
|
|
106
|
-
(0, utils_1.assert)(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
107
|
-
const iframe = document.getElementById(jobId);
|
|
108
|
-
(0, utils_1.assert)(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
109
|
-
iframe.remove();
|
|
110
|
-
this.jobs[jobId].stream.destroy();
|
|
111
|
-
delete this.jobs[jobId];
|
|
76
|
+
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
77
|
+
// itself does not have access to the iframes directly.
|
|
78
|
+
if (request.method === 'terminateJob') {
|
|
79
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_terminateJob).call(this, jobId);
|
|
80
|
+
return;
|
|
112
81
|
}
|
|
113
|
-
|
|
114
|
-
|
|
82
|
+
this.jobs[jobId].stream.write(request);
|
|
83
|
+
}, _ProxySnapExecutor_initializeJob =
|
|
84
|
+
/**
|
|
85
|
+
* Create a new iframe and set up a stream to communicate with it.
|
|
86
|
+
*
|
|
87
|
+
* @param jobId - The job ID.
|
|
88
|
+
* @returns The executor job object.
|
|
89
|
+
*/
|
|
90
|
+
async function _ProxySnapExecutor_initializeJob(jobId) {
|
|
91
|
+
const window = await (0, snaps_utils_1.createWindow)({ uri: __classPrivateFieldGet(this, _ProxySnapExecutor_frameUrl, "f"), id: jobId });
|
|
92
|
+
const jobStream = new post_message_stream_1.WindowPostMessageStream({
|
|
93
|
+
name: 'parent',
|
|
94
|
+
target: 'child',
|
|
95
|
+
targetWindow: window, // iframe's internal window
|
|
96
|
+
targetOrigin: '*',
|
|
97
|
+
});
|
|
98
|
+
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
99
|
+
jobStream.on('data', (data) => {
|
|
100
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").write({ data, jobId });
|
|
101
|
+
});
|
|
102
|
+
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
103
|
+
return this.jobs[jobId];
|
|
104
|
+
}, _ProxySnapExecutor_terminateJob = function _ProxySnapExecutor_terminateJob(jobId) {
|
|
105
|
+
(0, utils_1.assert)(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
106
|
+
const iframe = document.getElementById(jobId);
|
|
107
|
+
(0, utils_1.assert)(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
108
|
+
iframe.remove();
|
|
109
|
+
this.jobs[jobId].stream.destroy();
|
|
110
|
+
delete this.jobs[jobId];
|
|
111
|
+
};
|
|
115
112
|
//# sourceMappingURL=ProxySnapExecutor.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxySnapExecutor.cjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ProxySnapExecutor.cjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts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K,cAAc,CAAC,CAAC;IAEvD,MAAM,CAAC,MAAM,EAAE,CAAC;IAChB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAClC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport packageJson from '@metamask/snaps-execution-environments/package.json';\nimport { createWindow, logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\ntype ExecutorJob = {\n id: string;\n window: Window;\n stream: WindowPostMessageStream;\n};\n\nconst IFRAME_URL = `https://execution.metamask.io/iframe/${packageJson.version}/index.html`;\n\n/**\n * A \"proxy\" snap executor that uses a level of indirection to execute snaps.\n *\n * Useful for multiple execution environments.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates an iframe window for each snap execution, and sends the\n * snap execution request to the iframe window. The iframe window is responsible\n * for executing the snap.\n *\n * This executor is persisted between snap executions. The executor essentially\n * acts as a proxy between the client and the iframe execution environment.\n */\nexport class ProxySnapExecutor {\n readonly #stream: BasePostMessageStream;\n\n readonly #frameUrl: string;\n\n readonly jobs: Record<string, ExecutorJob> = {};\n\n /**\n * Initialize the executor with the given stream. This is a wrapper around the\n * constructor.\n *\n * @param stream - The stream to use for communication.\n * @param frameUrl - An optional URL for the iframe to use.\n * @returns The initialized executor.\n */\n static initialize(stream: BasePostMessageStream, frameUrl = IFRAME_URL) {\n return new ProxySnapExecutor(stream, frameUrl);\n }\n\n constructor(stream: BasePostMessageStream, frameUrl: string) {\n this.#stream = stream;\n this.#stream.on('data', this.#onData.bind(this));\n this.#frameUrl = frameUrl;\n }\n\n /**\n * Handle an incoming message from a `ProxyExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n if (!this.jobs[jobId]) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error);\n });\n\n return;\n }\n\n // This is a method specific to the `OffscreenSnapExecutor`, as the service\n // itself does not have access to the iframes directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n this.jobs[jobId].stream.write(request);\n }\n\n /**\n * Create a new iframe and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The executor job object.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const window = await createWindow({ uri: this.#frameUrl, id: jobId });\n const jobStream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: window, // iframe's internal window\n targetOrigin: '*',\n });\n\n // Write messages from the iframe to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n this.jobs[jobId] = { id: jobId, window, stream: jobStream };\n return this.jobs[jobId];\n }\n\n /**\n * Terminate the job with the given ID. This will close the iframe and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n assert(this.jobs[jobId], `Job \"${jobId}\" not found.`);\n\n const iframe = document.getElementById(jobId);\n assert(iframe, `Iframe with ID \"${jobId}\" not found.`);\n\n iframe.remove();\n this.jobs[jobId].stream.destroy();\n delete this.jobs[jobId];\n }\n}\n"]}
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _ProxySnapExecutor_instances, _ProxySnapExecutor_stream, _ProxySnapExecutor_frameUrl, _ProxySnapExecutor_onData, _ProxySnapExecutor_initializeJob, _ProxySnapExecutor_terminateJob;
|
|
1
13
|
import $metamaskpostmessagestream from "@metamask/post-message-stream";
|
|
2
14
|
const { WindowPostMessageStream } = $metamaskpostmessagestream;
|
|
3
15
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
@@ -19,9 +31,6 @@ const IFRAME_URL = `https://execution.metamask.io/iframe/${packageJson.version}/
|
|
|
19
31
|
* acts as a proxy between the client and the iframe execution environment.
|
|
20
32
|
*/
|
|
21
33
|
export class ProxySnapExecutor {
|
|
22
|
-
#stream;
|
|
23
|
-
#frameUrl;
|
|
24
|
-
jobs = {};
|
|
25
34
|
/**
|
|
26
35
|
* Initialize the executor with the given stream. This is a wrapper around the
|
|
27
36
|
* constructor.
|
|
@@ -34,76 +43,64 @@ export class ProxySnapExecutor {
|
|
|
34
43
|
return new ProxySnapExecutor(stream, frameUrl);
|
|
35
44
|
}
|
|
36
45
|
constructor(stream, frameUrl) {
|
|
37
|
-
this
|
|
38
|
-
|
|
39
|
-
this
|
|
46
|
+
_ProxySnapExecutor_instances.add(this);
|
|
47
|
+
_ProxySnapExecutor_stream.set(this, void 0);
|
|
48
|
+
_ProxySnapExecutor_frameUrl.set(this, void 0);
|
|
49
|
+
this.jobs = {};
|
|
50
|
+
__classPrivateFieldSet(this, _ProxySnapExecutor_stream, stream, "f");
|
|
51
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").on('data', __classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).bind(this));
|
|
52
|
+
__classPrivateFieldSet(this, _ProxySnapExecutor_frameUrl, frameUrl, "f");
|
|
40
53
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// code duplication, we call the `#onData` method again, which will
|
|
55
|
-
// run the rest of the logic after initialization.
|
|
56
|
-
this.#initializeJob(jobId)
|
|
57
|
-
.then(() => {
|
|
58
|
-
this.#onData(data);
|
|
59
|
-
})
|
|
60
|
-
.catch((error) => {
|
|
61
|
-
logError('[Worker] Error initializing job:', error);
|
|
62
|
-
});
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
66
|
-
// itself does not have access to the iframes directly.
|
|
67
|
-
if (request.method === 'terminateJob') {
|
|
68
|
-
this.#terminateJob(jobId);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
this.jobs[jobId].stream.write(request);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Create a new iframe and set up a stream to communicate with it.
|
|
75
|
-
*
|
|
76
|
-
* @param jobId - The job ID.
|
|
77
|
-
* @returns The executor job object.
|
|
78
|
-
*/
|
|
79
|
-
async #initializeJob(jobId) {
|
|
80
|
-
const window = await createWindow(this.#frameUrl, jobId);
|
|
81
|
-
const jobStream = new WindowPostMessageStream({
|
|
82
|
-
name: 'parent',
|
|
83
|
-
target: 'child',
|
|
84
|
-
targetWindow: window, // iframe's internal window
|
|
85
|
-
targetOrigin: '*',
|
|
86
|
-
});
|
|
87
|
-
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
88
|
-
jobStream.on('data', (data) => {
|
|
89
|
-
this.#stream.write({ data, jobId });
|
|
54
|
+
}
|
|
55
|
+
_ProxySnapExecutor_stream = new WeakMap(), _ProxySnapExecutor_frameUrl = new WeakMap(), _ProxySnapExecutor_instances = new WeakSet(), _ProxySnapExecutor_onData = function _ProxySnapExecutor_onData(data) {
|
|
56
|
+
const { jobId, data: request } = data;
|
|
57
|
+
if (!this.jobs[jobId]) {
|
|
58
|
+
// This ensures that a job is initialized before it is used. To avoid
|
|
59
|
+
// code duplication, we call the `#onData` method again, which will
|
|
60
|
+
// run the rest of the logic after initialization.
|
|
61
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_initializeJob).call(this, jobId)
|
|
62
|
+
.then(() => {
|
|
63
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).call(this, data);
|
|
64
|
+
})
|
|
65
|
+
.catch((error) => {
|
|
66
|
+
logError('[Worker] Error initializing job:', error);
|
|
90
67
|
});
|
|
91
|
-
|
|
92
|
-
return this.jobs[jobId];
|
|
68
|
+
return;
|
|
93
69
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*/
|
|
100
|
-
#terminateJob(jobId) {
|
|
101
|
-
assert(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
102
|
-
const iframe = document.getElementById(jobId);
|
|
103
|
-
assert(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
104
|
-
iframe.remove();
|
|
105
|
-
this.jobs[jobId].stream.destroy();
|
|
106
|
-
delete this.jobs[jobId];
|
|
70
|
+
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
71
|
+
// itself does not have access to the iframes directly.
|
|
72
|
+
if (request.method === 'terminateJob') {
|
|
73
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_terminateJob).call(this, jobId);
|
|
74
|
+
return;
|
|
107
75
|
}
|
|
108
|
-
|
|
76
|
+
this.jobs[jobId].stream.write(request);
|
|
77
|
+
}, _ProxySnapExecutor_initializeJob =
|
|
78
|
+
/**
|
|
79
|
+
* Create a new iframe and set up a stream to communicate with it.
|
|
80
|
+
*
|
|
81
|
+
* @param jobId - The job ID.
|
|
82
|
+
* @returns The executor job object.
|
|
83
|
+
*/
|
|
84
|
+
async function _ProxySnapExecutor_initializeJob(jobId) {
|
|
85
|
+
const window = await createWindow({ uri: __classPrivateFieldGet(this, _ProxySnapExecutor_frameUrl, "f"), id: jobId });
|
|
86
|
+
const jobStream = new WindowPostMessageStream({
|
|
87
|
+
name: 'parent',
|
|
88
|
+
target: 'child',
|
|
89
|
+
targetWindow: window, // iframe's internal window
|
|
90
|
+
targetOrigin: '*',
|
|
91
|
+
});
|
|
92
|
+
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
93
|
+
jobStream.on('data', (data) => {
|
|
94
|
+
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").write({ data, jobId });
|
|
95
|
+
});
|
|
96
|
+
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
97
|
+
return this.jobs[jobId];
|
|
98
|
+
}, _ProxySnapExecutor_terminateJob = function _ProxySnapExecutor_terminateJob(jobId) {
|
|
99
|
+
assert(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
100
|
+
const iframe = document.getElementById(jobId);
|
|
101
|
+
assert(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
102
|
+
iframe.remove();
|
|
103
|
+
this.jobs[jobId].stream.destroy();
|
|
104
|
+
delete this.jobs[jobId];
|
|
105
|
+
};
|
|
109
106
|
//# sourceMappingURL=ProxySnapExecutor.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxySnapExecutor.mjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ProxySnapExecutor.mjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,6DAA6D;AAC7D,OAAO,WAAW,kFAA4D;AAC9E,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,8BAA8B;AAE/D,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAQzC,MAAM,UAAU,GAAG,wCAAwC,WAAW,CAAC,OAAO,aAAa,CAAC;AAE5F;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,iBAAiB;IAO5B;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,MAA6B,EAAE,QAAQ,GAAG,UAAU;QACpE,OAAO,IAAI,iBAAiB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAED,YAAY,MAA6B,EAAE,QAAgB;;QAlBlD,4CAA+B;QAE/B,8CAAkB;QAElB,SAAI,GAAgC,EAAE,CAAC;QAe9C,uBAAA,IAAI,6BAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,iCAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,uBAAA,IAAI,+DAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACjD,uBAAA,IAAI,+BAAa,QAAQ,MAAA,CAAC;IAC5B,CAAC;CA+EF;qMApES,IAA6C;IACnD,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAEtC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACtB,qEAAqE;QACrE,mEAAmE;QACnE,kDAAkD;QAClD,uBAAA,IAAI,sEAAe,MAAnB,IAAI,EAAgB,KAAK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AC;AAC1B,CAAC,6EAQa,KAAa;IACzB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,QAAQ,KAAK,cAAc,CAAC,CAAC;IAEtD,MAAM,MAAM,GAAG,QAAQ,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAC9C,MAAM,CAAC,MAAM,EAAE,mBAAmB,KAAK,cAAc,CAAC,CAAC;IAEvD,MAAM,CAAC,MAAM,EAAE,CAAC;IAChB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAClC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport packageJson from '@metamask/snaps-execution-environments/package.json';\nimport { createWindow, logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\ntype ExecutorJob = {\n id: string;\n window: Window;\n stream: WindowPostMessageStream;\n};\n\nconst IFRAME_URL = `https://execution.metamask.io/iframe/${packageJson.version}/index.html`;\n\n/**\n * A \"proxy\" snap executor that uses a level of indirection to execute snaps.\n *\n * Useful for multiple execution environments.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates an iframe window for each snap execution, and sends the\n * snap execution request to the iframe window. The iframe window is responsible\n * for executing the snap.\n *\n * This executor is persisted between snap executions. The executor essentially\n * acts as a proxy between the client and the iframe execution environment.\n */\nexport class ProxySnapExecutor {\n readonly #stream: BasePostMessageStream;\n\n readonly #frameUrl: string;\n\n readonly jobs: Record<string, ExecutorJob> = {};\n\n /**\n * Initialize the executor with the given stream. This is a wrapper around the\n * constructor.\n *\n * @param stream - The stream to use for communication.\n * @param frameUrl - An optional URL for the iframe to use.\n * @returns The initialized executor.\n */\n static initialize(stream: BasePostMessageStream, frameUrl = IFRAME_URL) {\n return new ProxySnapExecutor(stream, frameUrl);\n }\n\n constructor(stream: BasePostMessageStream, frameUrl: string) {\n this.#stream = stream;\n this.#stream.on('data', this.#onData.bind(this));\n this.#frameUrl = frameUrl;\n }\n\n /**\n * Handle an incoming message from a `ProxyExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n if (!this.jobs[jobId]) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error);\n });\n\n return;\n }\n\n // This is a method specific to the `OffscreenSnapExecutor`, as the service\n // itself does not have access to the iframes directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n this.jobs[jobId].stream.write(request);\n }\n\n /**\n * Create a new iframe and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The executor job object.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const window = await createWindow({ uri: this.#frameUrl, id: jobId });\n const jobStream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: window, // iframe's internal window\n targetOrigin: '*',\n });\n\n // Write messages from the iframe to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n this.jobs[jobId] = { id: jobId, window, stream: jobStream };\n return this.jobs[jobId];\n }\n\n /**\n * Terminate the job with the given ID. This will close the iframe and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n assert(this.jobs[jobId], `Job \"${jobId}\" not found.`);\n\n const iframe = document.getElementById(jobId);\n assert(iframe, `Iframe with ID \"${jobId}\" not found.`);\n\n iframe.remove();\n this.jobs[jobId].stream.destroy();\n delete this.jobs[jobId];\n }\n}\n"]}
|
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _WebViewExecutorStream_name, _WebViewExecutorStream_target, _WebViewExecutorStream_targetWindow;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.WebViewExecutorStream = void 0;
|
|
4
16
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
5
17
|
const utils_1 = require("@metamask/post-message-stream/dist/utils.js");
|
|
6
18
|
const utils_2 = require("@metamask/utils");
|
|
7
19
|
class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream {
|
|
8
|
-
#name;
|
|
9
|
-
#target;
|
|
10
|
-
#targetWindow;
|
|
11
20
|
/**
|
|
12
21
|
* A special post-message-stream to be used by the WebView executor.
|
|
13
22
|
*
|
|
@@ -24,9 +33,12 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
24
33
|
*/
|
|
25
34
|
constructor({ name, target, targetWindow }) {
|
|
26
35
|
super();
|
|
27
|
-
this
|
|
28
|
-
this
|
|
29
|
-
this
|
|
36
|
+
_WebViewExecutorStream_name.set(this, void 0);
|
|
37
|
+
_WebViewExecutorStream_target.set(this, void 0);
|
|
38
|
+
_WebViewExecutorStream_targetWindow.set(this, void 0);
|
|
39
|
+
__classPrivateFieldSet(this, _WebViewExecutorStream_name, name, "f");
|
|
40
|
+
__classPrivateFieldSet(this, _WebViewExecutorStream_target, target, "f");
|
|
41
|
+
__classPrivateFieldSet(this, _WebViewExecutorStream_targetWindow, targetWindow, "f");
|
|
30
42
|
this._onMessage = this._onMessage.bind(this);
|
|
31
43
|
// This method is already bound.
|
|
32
44
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
@@ -38,8 +50,8 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
38
50
|
* Reference: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md?plain=1#L471
|
|
39
51
|
*/
|
|
40
52
|
_postMessage(data) {
|
|
41
|
-
this
|
|
42
|
-
target: this
|
|
53
|
+
__classPrivateFieldGet(this, _WebViewExecutorStream_targetWindow, "f").postMessage(JSON.stringify({
|
|
54
|
+
target: __classPrivateFieldGet(this, _WebViewExecutorStream_target, "f"),
|
|
43
55
|
data,
|
|
44
56
|
}));
|
|
45
57
|
}
|
|
@@ -51,7 +63,7 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
51
63
|
const message = JSON.parse((0, utils_2.bytesToString)(bytes));
|
|
52
64
|
// Notice that we don't check targetWindow or targetOrigin here.
|
|
53
65
|
// This doesn't seem possible to do in RN.
|
|
54
|
-
if (!(0, utils_1.isValidStreamMessage)(message) || message.target !== this
|
|
66
|
+
if (!(0, utils_1.isValidStreamMessage)(message) || message.target !== __classPrivateFieldGet(this, _WebViewExecutorStream_name, "f")) {
|
|
55
67
|
return;
|
|
56
68
|
}
|
|
57
69
|
this._onData(message.data);
|
|
@@ -63,4 +75,5 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
63
75
|
}
|
|
64
76
|
}
|
|
65
77
|
exports.WebViewExecutorStream = WebViewExecutorStream;
|
|
78
|
+
_WebViewExecutorStream_name = new WeakMap(), _WebViewExecutorStream_target = new WeakMap(), _WebViewExecutorStream_targetWindow = new WeakMap();
|
|
66
79
|
//# sourceMappingURL=WebViewExecutorStream.cjs.map
|