@metamask/snaps-execution-environments 6.6.2 → 6.7.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 +13 -1
- package/dist/common/BaseSnapExecutor.cjs +436 -0
- package/dist/common/BaseSnapExecutor.cjs.map +1 -0
- package/dist/{types/common/BaseSnapExecutor.d.ts → common/BaseSnapExecutor.d.cts} +8 -6
- package/dist/common/BaseSnapExecutor.d.cts.map +1 -0
- package/dist/common/BaseSnapExecutor.d.mts +70 -0
- package/dist/common/BaseSnapExecutor.d.mts.map +1 -0
- package/dist/common/BaseSnapExecutor.mjs +430 -24
- package/dist/common/BaseSnapExecutor.mjs.map +1 -1
- package/dist/common/commands.cjs +95 -0
- package/dist/common/commands.cjs.map +1 -0
- package/dist/{types/common/commands.d.ts → common/commands.d.cts} +5 -4
- package/dist/common/commands.d.cts.map +1 -0
- package/dist/common/commands.d.mts +31 -0
- package/dist/common/commands.d.mts.map +1 -0
- package/dist/common/commands.mjs +89 -10
- package/dist/common/commands.mjs.map +1 -1
- package/dist/common/endowments/commonEndowmentFactory.cjs +76 -0
- package/dist/common/endowments/commonEndowmentFactory.cjs.map +1 -0
- package/dist/{types/common/endowments/commonEndowmentFactory.d.ts → common/endowments/commonEndowmentFactory.d.cts} +5 -4
- package/dist/common/endowments/commonEndowmentFactory.d.cts.map +1 -0
- package/dist/common/endowments/commonEndowmentFactory.d.mts +26 -0
- package/dist/common/endowments/commonEndowmentFactory.d.mts.map +1 -0
- package/dist/common/endowments/commonEndowmentFactory.mjs +69 -18
- package/dist/common/endowments/commonEndowmentFactory.mjs.map +1 -1
- package/dist/common/endowments/console.cjs +103 -0
- package/dist/common/endowments/console.cjs.map +1 -0
- package/dist/{types/common/endowments/console.d.ts → common/endowments/console.d.cts} +2 -1
- package/dist/common/endowments/console.d.cts.map +1 -0
- package/dist/common/endowments/console.d.mts +46 -0
- package/dist/common/endowments/console.d.mts.map +1 -0
- package/dist/common/endowments/console.mjs +98 -11
- package/dist/common/endowments/console.mjs.map +1 -1
- package/dist/common/endowments/crypto.cjs +30 -0
- package/dist/common/endowments/crypto.cjs.map +1 -0
- package/dist/{types/common/endowments/crypto.d.ts → common/endowments/crypto.d.cts} +1 -0
- package/dist/common/endowments/crypto.d.cts.map +1 -0
- package/dist/common/endowments/crypto.d.mts +13 -0
- package/dist/common/endowments/crypto.d.mts.map +1 -0
- package/dist/common/endowments/crypto.mjs +24 -9
- package/dist/common/endowments/crypto.mjs.map +1 -1
- package/dist/common/endowments/date.cjs +39 -0
- package/dist/common/endowments/date.cjs.map +1 -0
- package/dist/{types/common/endowments/date.d.ts → common/endowments/date.d.cts} +1 -0
- package/dist/common/endowments/date.d.cts.map +1 -0
- package/dist/common/endowments/date.d.mts +16 -0
- package/dist/common/endowments/date.d.mts.map +1 -0
- package/dist/common/endowments/date.mjs +35 -7
- package/dist/common/endowments/date.mjs.map +1 -1
- package/dist/common/endowments/index.cjs +93 -0
- package/dist/common/endowments/index.cjs.map +1 -0
- package/dist/{types/common/endowments/index.d.ts → common/endowments/index.d.cts} +4 -3
- package/dist/common/endowments/index.d.cts.map +1 -0
- package/dist/common/endowments/index.d.mts +29 -0
- package/dist/common/endowments/index.d.mts.map +1 -0
- package/dist/common/endowments/index.mjs +85 -20
- package/dist/common/endowments/index.mjs.map +1 -1
- package/dist/common/endowments/interval.cjs +51 -0
- package/dist/common/endowments/interval.cjs.map +1 -0
- package/dist/{types/common/endowments/interval.d.ts → common/endowments/interval.d.cts} +1 -0
- package/dist/common/endowments/interval.d.cts.map +1 -0
- package/dist/common/endowments/interval.d.mts +10 -0
- package/dist/common/endowments/interval.d.mts.map +1 -0
- package/dist/common/endowments/interval.mjs +47 -6
- package/dist/common/endowments/interval.mjs.map +1 -1
- package/dist/common/endowments/math.cjs +54 -0
- package/dist/common/endowments/math.cjs.map +1 -0
- package/dist/{types/common/endowments/math.d.ts → common/endowments/math.d.cts} +1 -0
- package/dist/common/endowments/math.d.cts.map +1 -0
- package/dist/common/endowments/math.d.mts +61 -0
- package/dist/common/endowments/math.d.mts.map +1 -0
- package/dist/common/endowments/math.mjs +50 -8
- package/dist/common/endowments/math.mjs.map +1 -1
- package/dist/common/endowments/network.cjs +255 -0
- package/dist/common/endowments/network.cjs.map +1 -0
- package/dist/{types/common/endowments/network.d.ts → common/endowments/network.d.cts} +4 -2
- package/dist/common/endowments/network.d.cts.map +1 -0
- package/dist/common/endowments/network.d.mts +25 -0
- package/dist/common/endowments/network.d.mts.map +1 -0
- package/dist/common/endowments/network.mjs +251 -8
- package/dist/common/endowments/network.mjs.map +1 -1
- package/dist/common/endowments/textDecoder.cjs +18 -0
- package/dist/common/endowments/textDecoder.cjs.map +1 -0
- package/dist/{types/common/endowments/textDecoder.d.ts → common/endowments/textDecoder.d.cts} +1 -0
- package/dist/common/endowments/textDecoder.d.cts.map +1 -0
- package/dist/common/endowments/textDecoder.d.mts +11 -0
- package/dist/common/endowments/textDecoder.d.mts.map +1 -0
- package/dist/common/endowments/textDecoder.mjs +14 -6
- package/dist/common/endowments/textDecoder.mjs.map +1 -1
- package/dist/common/endowments/textEncoder.cjs +18 -0
- package/dist/common/endowments/textEncoder.cjs.map +1 -0
- package/dist/{types/common/endowments/textEncoder.d.ts → common/endowments/textEncoder.d.cts} +1 -0
- package/dist/common/endowments/textEncoder.d.cts.map +1 -0
- package/dist/common/endowments/textEncoder.d.mts +11 -0
- package/dist/common/endowments/textEncoder.d.mts.map +1 -0
- package/dist/common/endowments/textEncoder.mjs +14 -6
- package/dist/common/endowments/textEncoder.mjs.map +1 -1
- package/dist/common/endowments/timeout.cjs +53 -0
- package/dist/common/endowments/timeout.cjs.map +1 -0
- package/dist/{types/common/endowments/timeout.d.ts → common/endowments/timeout.d.cts} +1 -0
- package/dist/common/endowments/timeout.d.cts.map +1 -0
- package/dist/common/endowments/timeout.d.mts +10 -0
- package/dist/common/endowments/timeout.d.mts.map +1 -0
- package/dist/common/endowments/timeout.mjs +49 -6
- package/dist/common/endowments/timeout.mjs.map +1 -1
- package/dist/common/globalEvents.cjs +48 -0
- package/dist/common/globalEvents.cjs.map +1 -0
- package/dist/{types/common/globalEvents.d.ts → common/globalEvents.d.cts} +1 -0
- package/dist/common/globalEvents.d.cts.map +1 -0
- package/dist/common/globalEvents.d.mts +21 -0
- package/dist/common/globalEvents.d.mts.map +1 -0
- package/dist/common/globalEvents.mjs +42 -10
- package/dist/common/globalEvents.mjs.map +1 -1
- package/dist/common/globalObject.cjs +50 -0
- package/dist/common/globalObject.cjs.map +1 -0
- package/dist/{types/common/globalObject.d.ts → common/globalObject.d.cts} +1 -0
- package/dist/common/globalObject.d.cts.map +1 -0
- package/dist/common/globalObject.d.mts +10 -0
- package/dist/common/globalObject.d.mts.map +1 -0
- package/dist/common/globalObject.mjs +45 -9
- package/dist/common/globalObject.mjs.map +1 -1
- package/dist/common/lockdown/lockdown-events.cjs +60 -0
- package/dist/common/lockdown/lockdown-events.cjs.map +1 -0
- package/dist/{types/common/lockdown/lockdown-events.d.ts → common/lockdown/lockdown-events.d.cts} +1 -0
- package/dist/common/lockdown/lockdown-events.d.cts.map +1 -0
- package/dist/common/lockdown/lockdown-events.d.mts +5 -0
- package/dist/common/lockdown/lockdown-events.d.mts.map +1 -0
- package/dist/common/lockdown/lockdown-events.mjs +55 -7
- package/dist/common/lockdown/lockdown-events.mjs.map +1 -1
- package/dist/common/lockdown/lockdown-more.cjs +89 -0
- package/dist/common/lockdown/lockdown-more.cjs.map +1 -0
- package/dist/{types/common/lockdown/lockdown-more.d.ts → common/lockdown/lockdown-more.d.cts} +1 -0
- package/dist/common/lockdown/lockdown-more.d.cts.map +1 -0
- package/dist/common/lockdown/lockdown-more.d.mts +23 -0
- package/dist/common/lockdown/lockdown-more.d.mts.map +1 -0
- package/dist/common/lockdown/lockdown-more.mjs +84 -7
- package/dist/common/lockdown/lockdown-more.mjs.map +1 -1
- package/dist/common/lockdown/lockdown.cjs +29 -0
- package/dist/common/lockdown/lockdown.cjs.map +1 -0
- package/dist/{types/common/lockdown/lockdown.d.ts → common/lockdown/lockdown.d.cts} +1 -0
- package/dist/common/lockdown/lockdown.d.cts.map +1 -0
- package/dist/common/lockdown/lockdown.d.mts +8 -0
- package/dist/common/lockdown/lockdown.d.mts.map +1 -0
- package/dist/common/lockdown/lockdown.mjs +22 -19
- package/dist/common/lockdown/lockdown.mjs.map +1 -1
- package/dist/common/sortParams.cjs +35 -0
- package/dist/common/sortParams.cjs.map +1 -0
- package/dist/{types/common/sortParams.d.ts → common/sortParams.d.cts} +2 -1
- package/dist/common/sortParams.d.cts.map +1 -0
- package/dist/common/sortParams.d.mts +17 -0
- package/dist/common/sortParams.d.mts.map +1 -0
- package/dist/common/sortParams.mjs +29 -6
- package/dist/common/sortParams.mjs.map +1 -1
- package/dist/common/utils.cjs +156 -0
- package/dist/common/utils.cjs.map +1 -0
- package/dist/{types/common/utils.d.ts → common/utils.d.cts} +2 -1
- package/dist/common/utils.d.cts.map +1 -0
- package/dist/common/utils.d.mts +50 -0
- package/dist/common/utils.d.mts.map +1 -0
- package/dist/common/utils.mjs +146 -20
- package/dist/common/utils.mjs.map +1 -1
- package/dist/common/validation.cjs +137 -0
- package/dist/common/validation.cjs.map +1 -0
- package/dist/{types/common/validation.d.ts → common/validation.d.cts} +37 -36
- package/dist/common/validation.d.cts.map +1 -0
- package/dist/common/validation.d.mts +199 -0
- package/dist/common/validation.d.mts.map +1 -0
- package/dist/common/validation.mjs +127 -37
- package/dist/common/validation.mjs.map +1 -1
- package/dist/iframe/IFrameSnapExecutor.cjs +42 -0
- package/dist/iframe/IFrameSnapExecutor.cjs.map +1 -0
- package/dist/{types/iframe/IFrameSnapExecutor.d.ts → iframe/IFrameSnapExecutor.d.cts} +3 -2
- package/dist/iframe/IFrameSnapExecutor.d.cts.map +1 -0
- package/dist/iframe/IFrameSnapExecutor.d.mts +14 -0
- package/dist/iframe/IFrameSnapExecutor.d.mts.map +1 -0
- package/dist/iframe/IFrameSnapExecutor.mjs +42 -26
- package/dist/iframe/IFrameSnapExecutor.mjs.map +1 -1
- package/dist/iframe/index.cjs +10 -0
- package/dist/iframe/index.cjs.map +1 -0
- package/dist/iframe/index.d.cts +2 -0
- package/dist/iframe/index.d.cts.map +1 -0
- package/dist/iframe/index.d.mts +2 -0
- package/dist/iframe/index.d.mts.map +1 -0
- package/dist/iframe/index.mjs +4 -31
- package/dist/iframe/index.mjs.map +1 -1
- package/dist/index.cjs +18 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1 -8
- package/dist/index.mjs.map +1 -1
- package/dist/logging.cjs +13 -0
- package/dist/logging.cjs.map +1 -0
- package/dist/{types/logging.d.ts → logging.d.cts} +1 -0
- package/dist/logging.d.cts.map +1 -0
- package/dist/logging.d.mts +9 -0
- package/dist/logging.d.mts.map +1 -0
- package/dist/logging.mjs +9 -7
- package/dist/logging.mjs.map +1 -1
- package/dist/node-process/ChildProcessSnapExecutor.cjs +30 -0
- package/dist/node-process/ChildProcessSnapExecutor.cjs.map +1 -0
- package/dist/{types/node-process/ChildProcessSnapExecutor.d.ts → node-process/ChildProcessSnapExecutor.d.cts} +2 -1
- package/dist/node-process/ChildProcessSnapExecutor.d.cts.map +1 -0
- package/dist/node-process/ChildProcessSnapExecutor.d.mts +5 -0
- package/dist/node-process/ChildProcessSnapExecutor.d.mts.map +1 -0
- package/dist/node-process/ChildProcessSnapExecutor.mjs +30 -26
- package/dist/node-process/ChildProcessSnapExecutor.mjs.map +1 -1
- package/dist/node-process/index.cjs +8 -0
- package/dist/node-process/index.cjs.map +1 -0
- package/dist/node-process/index.d.cts +2 -0
- package/dist/node-process/index.d.cts.map +1 -0
- package/dist/node-process/index.d.mts +2 -0
- package/dist/node-process/index.d.mts.map +1 -0
- package/dist/node-process/index.mjs +3 -28
- package/dist/node-process/index.mjs.map +1 -1
- package/dist/node-thread/ThreadSnapExecutor.cjs +30 -0
- package/dist/node-thread/ThreadSnapExecutor.cjs.map +1 -0
- package/dist/node-thread/ThreadSnapExecutor.d.cts +5 -0
- package/dist/node-thread/ThreadSnapExecutor.d.cts.map +1 -0
- package/dist/node-thread/ThreadSnapExecutor.d.mts +5 -0
- package/dist/node-thread/ThreadSnapExecutor.d.mts.map +1 -0
- package/dist/node-thread/ThreadSnapExecutor.mjs +30 -26
- package/dist/node-thread/ThreadSnapExecutor.mjs.map +1 -1
- package/dist/node-thread/index.cjs +8 -0
- package/dist/node-thread/index.cjs.map +1 -0
- package/dist/node-thread/index.d.cts +2 -0
- package/dist/node-thread/index.d.cts.map +1 -0
- package/dist/node-thread/index.d.mts +2 -0
- package/dist/node-thread/index.d.mts.map +1 -0
- package/dist/node-thread/index.mjs +3 -28
- package/dist/node-thread/index.mjs.map +1 -1
- package/dist/proxy/ProxySnapExecutor.cjs +112 -0
- package/dist/proxy/ProxySnapExecutor.cjs.map +1 -0
- package/dist/{types/proxy/ProxySnapExecutor.d.ts → proxy/ProxySnapExecutor.d.cts} +4 -3
- package/dist/proxy/ProxySnapExecutor.d.cts.map +1 -0
- package/dist/proxy/ProxySnapExecutor.d.mts +36 -0
- package/dist/proxy/ProxySnapExecutor.d.mts.map +1 -0
- package/dist/proxy/ProxySnapExecutor.mjs +104 -6
- package/dist/proxy/ProxySnapExecutor.mjs.map +1 -1
- package/dist/proxy/index.cjs +18 -0
- package/dist/proxy/index.cjs.map +1 -0
- package/dist/proxy/index.d.cts +2 -0
- package/dist/proxy/index.d.cts.map +1 -0
- package/dist/proxy/index.d.mts +2 -0
- package/dist/proxy/index.d.mts.map +1 -0
- package/dist/proxy/index.mjs +1 -8
- package/dist/proxy/index.mjs.map +1 -1
- package/dist/webview/WebViewExecutorStream.cjs +79 -0
- package/dist/webview/WebViewExecutorStream.cjs.map +1 -0
- package/dist/{types/webview/WebViewExecutorStream.d.ts → webview/WebViewExecutorStream.d.cts} +3 -2
- package/dist/webview/WebViewExecutorStream.d.cts.map +1 -0
- package/dist/webview/WebViewExecutorStream.d.mts +33 -0
- package/dist/webview/WebViewExecutorStream.d.mts.map +1 -0
- package/dist/webview/WebViewExecutorStream.mjs +74 -6
- package/dist/webview/WebViewExecutorStream.mjs.map +1 -1
- package/dist/webview/index.cjs +16 -0
- package/dist/webview/index.cjs.map +1 -0
- package/dist/webview/index.d.cts +2 -0
- package/dist/webview/index.d.cts.map +1 -0
- package/dist/webview/index.d.mts +2 -0
- package/dist/webview/index.d.mts.map +1 -0
- package/dist/webview/index.mjs +9 -21
- package/dist/webview/index.mjs.map +1 -1
- package/dist/webworker/executor/WebWorkerSnapExecutor.cjs +37 -0
- package/dist/webworker/executor/WebWorkerSnapExecutor.cjs.map +1 -0
- package/dist/{types/webworker/executor/WebWorkerSnapExecutor.d.ts → webworker/executor/WebWorkerSnapExecutor.d.cts} +3 -2
- package/dist/webworker/executor/WebWorkerSnapExecutor.d.cts.map +1 -0
- package/dist/webworker/executor/WebWorkerSnapExecutor.d.mts +14 -0
- package/dist/webworker/executor/WebWorkerSnapExecutor.d.mts.map +1 -0
- package/dist/webworker/executor/WebWorkerSnapExecutor.mjs +37 -26
- package/dist/webworker/executor/WebWorkerSnapExecutor.mjs.map +1 -1
- package/dist/webworker/executor/index.cjs +10 -0
- package/dist/webworker/executor/index.cjs.map +1 -0
- package/dist/webworker/executor/index.d.cts +2 -0
- package/dist/webworker/executor/index.d.cts.map +1 -0
- package/dist/webworker/executor/index.d.mts +2 -0
- package/dist/webworker/executor/index.d.mts.map +1 -0
- package/dist/webworker/executor/index.mjs +4 -31
- package/dist/webworker/executor/index.mjs.map +1 -1
- package/dist/webworker/pool/WebWorkerPool.cjs +168 -0
- package/dist/webworker/pool/WebWorkerPool.cjs.map +1 -0
- package/dist/{types/webworker/pool/WebWorkerPool.d.ts → webworker/pool/WebWorkerPool.d.cts} +4 -3
- package/dist/webworker/pool/WebWorkerPool.d.cts.map +1 -0
- package/dist/webworker/pool/WebWorkerPool.d.mts +24 -0
- package/dist/webworker/pool/WebWorkerPool.d.mts.map +1 -0
- package/dist/webworker/pool/WebWorkerPool.mjs +163 -6
- package/dist/webworker/pool/WebWorkerPool.mjs.map +1 -1
- package/dist/webworker/pool/index.cjs +10 -0
- package/dist/webworker/pool/index.cjs.map +1 -0
- package/dist/webworker/pool/index.d.cts +2 -0
- package/dist/webworker/pool/index.d.cts.map +1 -0
- package/dist/webworker/pool/index.d.mts +2 -0
- package/dist/webworker/pool/index.d.mts.map +1 -0
- package/dist/webworker/pool/index.mjs +4 -12
- package/dist/webworker/pool/index.mjs.map +1 -1
- package/package.json +21 -18
- package/dist/browserify/iframe/bundle.js +0 -9
- package/dist/browserify/iframe/index.html +0 -13394
- package/dist/browserify/node-process/bundle.js +0 -13390
- package/dist/browserify/node-thread/bundle.js +0 -13390
- package/dist/browserify/webview/index.html +0 -13402
- package/dist/browserify/worker-executor/bundle.js +0 -13392
- package/dist/browserify/worker-pool/bundle.js +0 -9
- package/dist/browserify/worker-pool/index.html +0 -13394
- package/dist/chunk-2VA6RAEE.js +0 -30
- package/dist/chunk-2VA6RAEE.js.map +0 -1
- package/dist/chunk-3JCT4MWT.js +0 -108
- package/dist/chunk-3JCT4MWT.js.map +0 -1
- package/dist/chunk-46LW37FK.js +0 -144
- package/dist/chunk-46LW37FK.js.map +0 -1
- package/dist/chunk-4COIRXF5.js +0 -86
- package/dist/chunk-4COIRXF5.js.map +0 -1
- package/dist/chunk-4XRZLEKU.js +0 -49
- package/dist/chunk-4XRZLEKU.js.map +0 -1
- package/dist/chunk-53ZULHPK.mjs +0 -16
- package/dist/chunk-53ZULHPK.mjs.map +0 -1
- package/dist/chunk-5DEV3QQU.mjs +0 -12
- package/dist/chunk-5DEV3QQU.mjs.map +0 -1
- package/dist/chunk-5LGIS4VY.js +0 -33
- package/dist/chunk-5LGIS4VY.js.map +0 -1
- package/dist/chunk-5STN3QCA.js +0 -183
- package/dist/chunk-5STN3QCA.js.map +0 -1
- package/dist/chunk-75RCH4ZX.mjs +0 -151
- package/dist/chunk-75RCH4ZX.mjs.map +0 -1
- package/dist/chunk-7AJWXSDQ.mjs +0 -73
- package/dist/chunk-7AJWXSDQ.mjs.map +0 -1
- package/dist/chunk-7MMUJJQ2.js +0 -151
- package/dist/chunk-7MMUJJQ2.js.map +0 -1
- package/dist/chunk-BCBKEUCG.mjs +0 -38
- package/dist/chunk-BCBKEUCG.mjs.map +0 -1
- package/dist/chunk-BTEAZZKP.mjs +0 -24
- package/dist/chunk-BTEAZZKP.mjs.map +0 -1
- package/dist/chunk-BVRVUIJB.mjs +0 -67
- package/dist/chunk-BVRVUIJB.mjs.map +0 -1
- package/dist/chunk-CV5LC4PP.js +0 -24
- package/dist/chunk-CV5LC4PP.js.map +0 -1
- package/dist/chunk-DAG7NWEU.mjs +0 -33
- package/dist/chunk-DAG7NWEU.mjs.map +0 -1
- package/dist/chunk-DKDTGAFK.js +0 -67
- package/dist/chunk-DKDTGAFK.js.map +0 -1
- package/dist/chunk-DLLLH5HN.js +0 -26
- package/dist/chunk-DLLLH5HN.js.map +0 -1
- package/dist/chunk-EXN2TFDJ.js +0 -38
- package/dist/chunk-EXN2TFDJ.js.map +0 -1
- package/dist/chunk-FJT65CXB.js +0 -472
- package/dist/chunk-FJT65CXB.js.map +0 -1
- package/dist/chunk-FOEAIF5U.js +0 -16
- package/dist/chunk-FOEAIF5U.js.map +0 -1
- package/dist/chunk-FUPEJT7B.js +0 -82
- package/dist/chunk-FUPEJT7B.js.map +0 -1
- package/dist/chunk-HCJZG6W7.mjs +0 -45
- package/dist/chunk-HCJZG6W7.mjs.map +0 -1
- package/dist/chunk-HGLRZZ56.js +0 -32
- package/dist/chunk-HGLRZZ56.js.map +0 -1
- package/dist/chunk-HL2TTKF3.mjs +0 -144
- package/dist/chunk-HL2TTKF3.mjs.map +0 -1
- package/dist/chunk-HWS4QSOC.mjs +0 -16
- package/dist/chunk-HWS4QSOC.mjs.map +0 -1
- package/dist/chunk-HXKBUIYS.js +0 -262
- package/dist/chunk-HXKBUIYS.js.map +0 -1
- package/dist/chunk-I5G4YFHM.js +0 -73
- package/dist/chunk-I5G4YFHM.js.map +0 -1
- package/dist/chunk-IX5S3V47.mjs +0 -26
- package/dist/chunk-IX5S3V47.mjs.map +0 -1
- package/dist/chunk-JOT5JH42.js +0 -52
- package/dist/chunk-JOT5JH42.js.map +0 -1
- package/dist/chunk-KEAB7ZVV.js +0 -50
- package/dist/chunk-KEAB7ZVV.js.map +0 -1
- package/dist/chunk-LAKWJYBO.js +0 -33
- package/dist/chunk-LAKWJYBO.js.map +0 -1
- package/dist/chunk-LO5ORVRC.js +0 -16
- package/dist/chunk-LO5ORVRC.js.map +0 -1
- package/dist/chunk-MCXWFCGV.js +0 -1
- package/dist/chunk-MCXWFCGV.js.map +0 -1
- package/dist/chunk-MIWGLICQ.mjs +0 -108
- package/dist/chunk-MIWGLICQ.mjs.map +0 -1
- package/dist/chunk-MMYM63NH.mjs +0 -82
- package/dist/chunk-MMYM63NH.mjs.map +0 -1
- package/dist/chunk-MS2LQQYH.js +0 -38
- package/dist/chunk-MS2LQQYH.js.map +0 -1
- package/dist/chunk-MXNMMTJW.mjs +0 -51
- package/dist/chunk-MXNMMTJW.mjs.map +0 -1
- package/dist/chunk-OBT45IWW.js +0 -92
- package/dist/chunk-OBT45IWW.js.map +0 -1
- package/dist/chunk-OCNPLHS2.js +0 -51
- package/dist/chunk-OCNPLHS2.js.map +0 -1
- package/dist/chunk-ODICKSGZ.mjs +0 -86
- package/dist/chunk-ODICKSGZ.mjs.map +0 -1
- package/dist/chunk-OIN7VU4G.mjs +0 -33
- package/dist/chunk-OIN7VU4G.mjs.map +0 -1
- package/dist/chunk-Q42NNJFY.mjs +0 -32
- package/dist/chunk-Q42NNJFY.mjs.map +0 -1
- package/dist/chunk-Q42WB3ZO.js +0 -40
- package/dist/chunk-Q42WB3ZO.js.map +0 -1
- package/dist/chunk-QZC2X6NG.js +0 -44
- package/dist/chunk-QZC2X6NG.js.map +0 -1
- package/dist/chunk-S5NWI5Z7.mjs +0 -50
- package/dist/chunk-S5NWI5Z7.mjs.map +0 -1
- package/dist/chunk-S7257EN3.mjs +0 -49
- package/dist/chunk-S7257EN3.mjs.map +0 -1
- package/dist/chunk-TSEAPB7L.mjs +0 -1
- package/dist/chunk-TSEAPB7L.mjs.map +0 -1
- package/dist/chunk-TZAPZE45.mjs +0 -472
- package/dist/chunk-TZAPZE45.mjs.map +0 -1
- package/dist/chunk-UOPIRTJX.mjs +0 -44
- package/dist/chunk-UOPIRTJX.mjs.map +0 -1
- package/dist/chunk-V76DI2XM.mjs +0 -183
- package/dist/chunk-V76DI2XM.mjs.map +0 -1
- package/dist/chunk-VFQ5CRZS.mjs +0 -40
- package/dist/chunk-VFQ5CRZS.mjs.map +0 -1
- package/dist/chunk-W7EDURT5.mjs +0 -92
- package/dist/chunk-W7EDURT5.mjs.map +0 -1
- package/dist/chunk-WZDVTU5C.js +0 -45
- package/dist/chunk-WZDVTU5C.js.map +0 -1
- package/dist/chunk-XPCW7TKM.mjs +0 -262
- package/dist/chunk-XPCW7TKM.mjs.map +0 -1
- package/dist/chunk-Y7AK2VVC.mjs +0 -52
- package/dist/chunk-Y7AK2VVC.mjs.map +0 -1
- package/dist/chunk-YRZVIDCF.mjs +0 -38
- package/dist/chunk-YRZVIDCF.mjs.map +0 -1
- package/dist/chunk-Z7BOREC4.js +0 -12
- package/dist/chunk-Z7BOREC4.js.map +0 -1
- package/dist/chunk-ZNTCZK7J.mjs +0 -30
- package/dist/chunk-ZNTCZK7J.mjs.map +0 -1
- package/dist/common/BaseSnapExecutor.js +0 -26
- package/dist/common/BaseSnapExecutor.js.map +0 -1
- package/dist/common/commands.js +0 -11
- package/dist/common/commands.js.map +0 -1
- package/dist/common/endowments/commonEndowmentFactory.js +0 -20
- package/dist/common/endowments/commonEndowmentFactory.js.map +0 -1
- package/dist/common/endowments/console.js +0 -13
- package/dist/common/endowments/console.js.map +0 -1
- package/dist/common/endowments/crypto.js +0 -11
- package/dist/common/endowments/crypto.js.map +0 -1
- package/dist/common/endowments/date.js +0 -9
- package/dist/common/endowments/date.js.map +0 -1
- package/dist/common/endowments/index.js +0 -21
- package/dist/common/endowments/index.js.map +0 -1
- package/dist/common/endowments/interval.js +0 -8
- package/dist/common/endowments/interval.js.map +0 -1
- package/dist/common/endowments/math.js +0 -10
- package/dist/common/endowments/math.js.map +0 -1
- package/dist/common/endowments/network.js +0 -10
- package/dist/common/endowments/network.js.map +0 -1
- package/dist/common/endowments/textDecoder.js +0 -8
- package/dist/common/endowments/textDecoder.js.map +0 -1
- package/dist/common/endowments/textEncoder.js +0 -8
- package/dist/common/endowments/textEncoder.js.map +0 -1
- package/dist/common/endowments/timeout.js +0 -8
- package/dist/common/endowments/timeout.js.map +0 -1
- package/dist/common/globalEvents.js +0 -11
- package/dist/common/globalEvents.js.map +0 -1
- package/dist/common/globalObject.js +0 -10
- package/dist/common/globalObject.js.map +0 -1
- package/dist/common/lockdown/lockdown-events.js +0 -8
- package/dist/common/lockdown/lockdown-events.js.map +0 -1
- package/dist/common/lockdown/lockdown-more.js +0 -8
- package/dist/common/lockdown/lockdown-more.js.map +0 -1
- package/dist/common/lockdown/lockdown.js +0 -22
- package/dist/common/lockdown/lockdown.js.map +0 -1
- package/dist/common/sortParams.js +0 -8
- package/dist/common/sortParams.js.map +0 -1
- package/dist/common/utils.js +0 -21
- package/dist/common/utils.js.map +0 -1
- package/dist/common/validation.js +0 -38
- package/dist/common/validation.js.map +0 -1
- package/dist/globals.d.js +0 -1
- package/dist/globals.d.js.map +0 -1
- package/dist/globals.d.mjs +0 -1
- package/dist/globals.d.mjs.map +0 -1
- package/dist/iframe/IFrameSnapExecutor.js +0 -27
- package/dist/iframe/IFrameSnapExecutor.js.map +0 -1
- package/dist/iframe/index.js +0 -35
- package/dist/iframe/index.js.map +0 -1
- package/dist/index.js +0 -9
- package/dist/index.js.map +0 -1
- package/dist/logging.js +0 -8
- package/dist/logging.js.map +0 -1
- package/dist/node-process/ChildProcessSnapExecutor.js +0 -27
- package/dist/node-process/ChildProcessSnapExecutor.js.map +0 -1
- package/dist/node-process/index.js +0 -31
- package/dist/node-process/index.js.map +0 -1
- package/dist/node-thread/ThreadSnapExecutor.js +0 -27
- package/dist/node-thread/ThreadSnapExecutor.js.map +0 -1
- package/dist/node-thread/index.js +0 -31
- package/dist/node-thread/index.js.map +0 -1
- package/dist/proxy/ProxySnapExecutor.js +0 -8
- package/dist/proxy/ProxySnapExecutor.js.map +0 -1
- package/dist/proxy/index.js +0 -9
- package/dist/proxy/index.js.map +0 -1
- package/dist/tsconfig.build.tsbuildinfo +0 -1
- package/dist/types/iframe/index.d.ts +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/node-process/index.d.ts +0 -1
- package/dist/types/node-thread/ThreadSnapExecutor.d.ts +0 -4
- package/dist/types/node-thread/index.d.ts +0 -1
- package/dist/types/proxy/index.d.ts +0 -1
- package/dist/types/vendor/global.d.js +0 -1
- package/dist/types/vendor/global.d.js.map +0 -1
- package/dist/types/vendor/global.d.mjs +0 -1
- package/dist/types/vendor/global.d.mjs.map +0 -1
- package/dist/types/vendor/providers.d.js +0 -1
- package/dist/types/vendor/providers.d.js.map +0 -1
- package/dist/types/vendor/providers.d.mjs +0 -1
- package/dist/types/vendor/providers.d.mjs.map +0 -1
- package/dist/types/vendor/readable-stream.d.js +0 -1
- package/dist/types/vendor/readable-stream.d.js.map +0 -1
- package/dist/types/vendor/readable-stream.d.mjs +0 -1
- package/dist/types/vendor/readable-stream.d.mjs.map +0 -1
- package/dist/types/webview/index.d.ts +0 -1
- package/dist/types/webworker/executor/index.d.ts +0 -1
- package/dist/types/webworker/pool/index.d.ts +0 -1
- package/dist/webview/WebViewExecutorStream.js +0 -8
- package/dist/webview/WebViewExecutorStream.js.map +0 -1
- package/dist/webview/index.js +0 -26
- package/dist/webview/index.js.map +0 -1
- package/dist/webworker/executor/WebWorkerSnapExecutor.js +0 -27
- package/dist/webworker/executor/WebWorkerSnapExecutor.js.map +0 -1
- package/dist/webworker/executor/index.js +0 -35
- package/dist/webworker/executor/index.js.map +0 -1
- package/dist/webworker/pool/WebWorkerPool.js +0 -8
- package/dist/webworker/pool/WebWorkerPool.js.map +0 -1
- package/dist/webworker/pool/index.js +0 -16
- package/dist/webworker/pool/index.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [6.7.0]
|
|
10
|
+
### Changed
|
|
11
|
+
- Unblock `wallet_requestSnaps` ([#2661](https://github.com/MetaMask/snaps/pull/2661))
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
- Fix ESM version of the package ([#2682](https://github.com/MetaMask/snaps/pull/2682))
|
|
15
|
+
- This fixes the ESM version of the package to be fully compliant with the ESM
|
|
16
|
+
standard.
|
|
17
|
+
- Bump `@metamask/utils` from `^9.1.0` to `^9.2.1` ([#2680](https://github.com/MetaMask/snaps/pull/2680))
|
|
18
|
+
- Bump other MetaMask dependencies ([#2703](https://github.com/MetaMask/snaps/pull/2703))
|
|
19
|
+
|
|
9
20
|
## [6.6.2]
|
|
10
21
|
### Changed
|
|
11
22
|
- Bump `@metamask/json-rpc-engine` from `^9.0.0` to `^9.0.2` ([#2593](https://github.com/metamask/snaps/pull/2593))
|
|
@@ -240,7 +251,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
240
251
|
- The version of the package no longer needs to match the version of all other
|
|
241
252
|
MetaMask Snaps packages.
|
|
242
253
|
|
|
243
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-execution-environments@6.
|
|
254
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-execution-environments@6.7.0...HEAD
|
|
255
|
+
[6.7.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-execution-environments@6.6.2...@metamask/snaps-execution-environments@6.7.0
|
|
244
256
|
[6.6.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-execution-environments@6.6.1...@metamask/snaps-execution-environments@6.6.2
|
|
245
257
|
[6.6.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-execution-environments@6.6.0...@metamask/snaps-execution-environments@6.6.1
|
|
246
258
|
[6.6.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-execution-environments@6.5.0...@metamask/snaps-execution-environments@6.6.0
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _BaseSnapExecutor_instances, _BaseSnapExecutor_write, _BaseSnapExecutor_notify, _BaseSnapExecutor_respond;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.BaseSnapExecutor = void 0;
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference, spaced-comment
|
|
11
|
+
/// <reference path="../../../../node_modules/ses/types.d.ts" />
|
|
12
|
+
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
13
|
+
const stream_provider_1 = require("@metamask/providers/stream-provider");
|
|
14
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
15
|
+
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
16
|
+
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
17
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
18
|
+
const utils_1 = require("@metamask/utils");
|
|
19
|
+
const logging_1 = require("../logging.cjs");
|
|
20
|
+
const commands_1 = require("./commands.cjs");
|
|
21
|
+
const endowments_1 = require("./endowments/index.cjs");
|
|
22
|
+
const globalEvents_1 = require("./globalEvents.cjs");
|
|
23
|
+
const sortParams_1 = require("./sortParams.cjs");
|
|
24
|
+
const utils_2 = require("./utils.cjs");
|
|
25
|
+
const validation_1 = require("./validation.cjs");
|
|
26
|
+
const fallbackError = {
|
|
27
|
+
code: rpc_errors_1.errorCodes.rpc.internal,
|
|
28
|
+
message: 'Execution Environment Error',
|
|
29
|
+
};
|
|
30
|
+
const unhandledError = rpc_errors_1.rpcErrors
|
|
31
|
+
.internal({
|
|
32
|
+
message: 'Unhandled Snap Error',
|
|
33
|
+
})
|
|
34
|
+
.serialize();
|
|
35
|
+
/**
|
|
36
|
+
* The supported methods in the execution environment. The validator checks the
|
|
37
|
+
* incoming JSON-RPC request, and the `params` property is used for sorting the
|
|
38
|
+
* parameters, if they are an object.
|
|
39
|
+
*/
|
|
40
|
+
const EXECUTION_ENVIRONMENT_METHODS = {
|
|
41
|
+
ping: {
|
|
42
|
+
struct: validation_1.PingRequestArgumentsStruct,
|
|
43
|
+
params: [],
|
|
44
|
+
},
|
|
45
|
+
executeSnap: {
|
|
46
|
+
struct: validation_1.ExecuteSnapRequestArgumentsStruct,
|
|
47
|
+
params: ['snapId', 'sourceCode', 'endowments'],
|
|
48
|
+
},
|
|
49
|
+
terminate: {
|
|
50
|
+
struct: validation_1.TerminateRequestArgumentsStruct,
|
|
51
|
+
params: [],
|
|
52
|
+
},
|
|
53
|
+
snapRpc: {
|
|
54
|
+
struct: validation_1.SnapRpcRequestArgumentsStruct,
|
|
55
|
+
params: ['target', 'handler', 'origin', 'request'],
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
class BaseSnapExecutor {
|
|
59
|
+
constructor(commandStream, rpcStream) {
|
|
60
|
+
_BaseSnapExecutor_instances.add(this);
|
|
61
|
+
this.lastTeardown = 0;
|
|
62
|
+
this.snapData = new Map();
|
|
63
|
+
this.commandStream = commandStream;
|
|
64
|
+
this.commandStream.on('data', (data) => {
|
|
65
|
+
this.onCommandRequest(data).catch((error) => {
|
|
66
|
+
// TODO: Decide how to handle errors.
|
|
67
|
+
(0, snaps_utils_1.logError)(error);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
this.rpcStream = rpcStream;
|
|
71
|
+
this.methods = (0, commands_1.getCommandMethodImplementations)(this.startSnap.bind(this), async (target, handlerType, args) => {
|
|
72
|
+
const data = this.snapData.get(target);
|
|
73
|
+
// We're capturing the handler in case someone modifies the data object
|
|
74
|
+
// before the call.
|
|
75
|
+
const handler = data?.exports[handlerType];
|
|
76
|
+
const { required } = snaps_utils_1.SNAP_EXPORTS[handlerType];
|
|
77
|
+
(0, utils_1.assert)(!required || handler !== undefined, `No ${handlerType} handler exported for snap "${target}`, rpc_errors_1.rpcErrors.methodNotSupported);
|
|
78
|
+
// Certain handlers are not required. If they are not exported, we
|
|
79
|
+
// return null.
|
|
80
|
+
if (!handler) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
let result = await this.executeInSnapContext(target, () =>
|
|
84
|
+
// TODO: fix handler args type cast
|
|
85
|
+
handler(args));
|
|
86
|
+
// The handler might not return anything, but undefined is not valid JSON.
|
|
87
|
+
if (result === undefined) {
|
|
88
|
+
result = null;
|
|
89
|
+
}
|
|
90
|
+
// /!\ Always return only sanitized JSON to prevent security flaws. /!\
|
|
91
|
+
try {
|
|
92
|
+
return (0, utils_1.getSafeJson)(result);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw rpc_errors_1.rpcErrors.internal(`Received non-JSON-serializable value: ${error.message.replace(/^Assertion failed: /u, '')}`);
|
|
96
|
+
}
|
|
97
|
+
}, this.onTerminate.bind(this));
|
|
98
|
+
}
|
|
99
|
+
errorHandler(error, data) {
|
|
100
|
+
const serializedError = (0, rpc_errors_1.serializeError)(error, {
|
|
101
|
+
fallbackError: unhandledError,
|
|
102
|
+
shouldIncludeStack: false,
|
|
103
|
+
});
|
|
104
|
+
const errorData = (0, snaps_sdk_1.getErrorData)(serializedError);
|
|
105
|
+
__classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_notify).call(this, {
|
|
106
|
+
method: 'UnhandledError',
|
|
107
|
+
params: {
|
|
108
|
+
error: {
|
|
109
|
+
...serializedError,
|
|
110
|
+
data: {
|
|
111
|
+
...errorData,
|
|
112
|
+
...data,
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
}).catch((notifyError) => {
|
|
117
|
+
(0, snaps_utils_1.logError)(notifyError);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
async onCommandRequest(message) {
|
|
121
|
+
if (!(0, utils_1.isJsonRpcRequest)(message)) {
|
|
122
|
+
if ((0, utils_1.hasProperty)(message, 'id') &&
|
|
123
|
+
(0, superstruct_1.is)(message.id, utils_1.JsonRpcIdStruct)) {
|
|
124
|
+
// Instead of throwing, we directly respond with an error.
|
|
125
|
+
// We can only do this if the message ID is still valid.
|
|
126
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_write).call(this, {
|
|
127
|
+
error: (0, rpc_errors_1.serializeError)(rpc_errors_1.rpcErrors.internal('JSON-RPC requests must be JSON serializable objects.')),
|
|
128
|
+
id: message.id,
|
|
129
|
+
jsonrpc: '2.0',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
(0, snaps_utils_1.logInfo)('Command stream received a non-JSON-RPC request, and was unable to respond.');
|
|
134
|
+
}
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const { id, method, params } = message;
|
|
138
|
+
if (!(0, utils_1.hasProperty)(EXECUTION_ENVIRONMENT_METHODS, method)) {
|
|
139
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_respond).call(this, id, {
|
|
140
|
+
error: rpc_errors_1.rpcErrors
|
|
141
|
+
.methodNotFound({
|
|
142
|
+
data: {
|
|
143
|
+
method,
|
|
144
|
+
},
|
|
145
|
+
})
|
|
146
|
+
.serialize(),
|
|
147
|
+
});
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const methodObject = EXECUTION_ENVIRONMENT_METHODS[method];
|
|
151
|
+
// support params by-name and by-position
|
|
152
|
+
const paramsAsArray = (0, sortParams_1.sortParamKeys)(methodObject.params, params);
|
|
153
|
+
const [error] = (0, superstruct_1.validate)(paramsAsArray, methodObject.struct);
|
|
154
|
+
if (error) {
|
|
155
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_respond).call(this, id, {
|
|
156
|
+
error: rpc_errors_1.rpcErrors
|
|
157
|
+
.invalidParams({
|
|
158
|
+
message: `Invalid parameters for method "${method}": ${error.message}.`,
|
|
159
|
+
data: {
|
|
160
|
+
method,
|
|
161
|
+
params: paramsAsArray,
|
|
162
|
+
},
|
|
163
|
+
})
|
|
164
|
+
.serialize(),
|
|
165
|
+
});
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const result = await this.methods[method](...paramsAsArray);
|
|
170
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_respond).call(this, id, { result });
|
|
171
|
+
}
|
|
172
|
+
catch (rpcError) {
|
|
173
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_respond).call(this, id, {
|
|
174
|
+
error: (0, rpc_errors_1.serializeError)(rpcError, {
|
|
175
|
+
fallbackError,
|
|
176
|
+
}),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Attempts to evaluate a snap in SES. Generates APIs for the snap. May throw
|
|
182
|
+
* on errors.
|
|
183
|
+
*
|
|
184
|
+
* @param snapId - The id of the snap.
|
|
185
|
+
* @param sourceCode - The source code of the snap, in IIFE format.
|
|
186
|
+
* @param _endowments - An array of the names of the endowments.
|
|
187
|
+
*/
|
|
188
|
+
async startSnap(snapId, sourceCode, _endowments) {
|
|
189
|
+
(0, logging_1.log)(`Starting snap '${snapId}' in worker.`);
|
|
190
|
+
if (this.snapPromiseErrorHandler) {
|
|
191
|
+
(0, globalEvents_1.removeEventListener)('unhandledrejection', this.snapPromiseErrorHandler);
|
|
192
|
+
}
|
|
193
|
+
if (this.snapErrorHandler) {
|
|
194
|
+
(0, globalEvents_1.removeEventListener)('error', this.snapErrorHandler);
|
|
195
|
+
}
|
|
196
|
+
this.snapErrorHandler = (error) => {
|
|
197
|
+
this.errorHandler(error.error, { snapId });
|
|
198
|
+
};
|
|
199
|
+
this.snapPromiseErrorHandler = (error) => {
|
|
200
|
+
this.errorHandler(error instanceof Error ? error : error.reason, {
|
|
201
|
+
snapId,
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
const provider = new stream_provider_1.StreamProvider(this.rpcStream, {
|
|
205
|
+
jsonRpcStreamName: 'metamask-provider',
|
|
206
|
+
rpcMiddleware: [(0, json_rpc_engine_1.createIdRemapMiddleware)()],
|
|
207
|
+
});
|
|
208
|
+
await provider.initialize();
|
|
209
|
+
const snap = this.createSnapGlobal(provider);
|
|
210
|
+
const ethereum = this.createEIP1193Provider(provider);
|
|
211
|
+
// We specifically use any type because the Snap can modify the object any way they want
|
|
212
|
+
const snapModule = { exports: {} };
|
|
213
|
+
try {
|
|
214
|
+
const { endowments, teardown: endowmentTeardown } = (0, endowments_1.createEndowments)({
|
|
215
|
+
snap,
|
|
216
|
+
ethereum,
|
|
217
|
+
snapId,
|
|
218
|
+
endowments: _endowments,
|
|
219
|
+
notify: __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_notify).bind(this),
|
|
220
|
+
});
|
|
221
|
+
// !!! Ensure that this is the only place the data is being set.
|
|
222
|
+
// Other methods access the object value and mutate its properties.
|
|
223
|
+
this.snapData.set(snapId, {
|
|
224
|
+
idleTeardown: endowmentTeardown,
|
|
225
|
+
runningEvaluations: new Set(),
|
|
226
|
+
exports: {},
|
|
227
|
+
});
|
|
228
|
+
(0, globalEvents_1.addEventListener)('unhandledRejection', this.snapPromiseErrorHandler);
|
|
229
|
+
(0, globalEvents_1.addEventListener)('error', this.snapErrorHandler);
|
|
230
|
+
const compartment = new Compartment({
|
|
231
|
+
...endowments,
|
|
232
|
+
module: snapModule,
|
|
233
|
+
exports: snapModule.exports,
|
|
234
|
+
});
|
|
235
|
+
// All of those are JavaScript runtime specific and self referential,
|
|
236
|
+
// but we add them for compatibility sake with external libraries.
|
|
237
|
+
//
|
|
238
|
+
// We can't do that in the injected globals object above
|
|
239
|
+
// because SES creates its own globalThis
|
|
240
|
+
compartment.globalThis.self = compartment.globalThis;
|
|
241
|
+
compartment.globalThis.global = compartment.globalThis;
|
|
242
|
+
compartment.globalThis.window = compartment.globalThis;
|
|
243
|
+
await this.executeInSnapContext(snapId, () => {
|
|
244
|
+
compartment.evaluate(sourceCode);
|
|
245
|
+
this.registerSnapExports(snapId, snapModule);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
this.removeSnap(snapId);
|
|
250
|
+
const [cause] = (0, snaps_utils_1.unwrapError)(error);
|
|
251
|
+
throw rpc_errors_1.rpcErrors.internal({
|
|
252
|
+
message: `Error while running snap '${snapId}': ${cause.message}`,
|
|
253
|
+
data: {
|
|
254
|
+
cause: cause.serialize(),
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Cancels all running evaluations of all snaps and clears all snap data.
|
|
261
|
+
* NOTE:** Should only be called in response to the `terminate` RPC command.
|
|
262
|
+
*/
|
|
263
|
+
onTerminate() {
|
|
264
|
+
// `stop()` tears down snap endowments.
|
|
265
|
+
// Teardown will also be run for each snap as soon as there are
|
|
266
|
+
// no more running evaluations for that snap.
|
|
267
|
+
this.snapData.forEach((data) => data.runningEvaluations.forEach((evaluation) => evaluation.stop()));
|
|
268
|
+
this.snapData.clear();
|
|
269
|
+
}
|
|
270
|
+
registerSnapExports(snapId, snapModule) {
|
|
271
|
+
const data = this.snapData.get(snapId);
|
|
272
|
+
// Somebody deleted the snap before we could register.
|
|
273
|
+
if (!data) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
data.exports = snaps_utils_1.SNAP_EXPORT_NAMES.reduce((acc, exportName) => {
|
|
277
|
+
const snapExport = snapModule.exports[exportName];
|
|
278
|
+
const { validator } = snaps_utils_1.SNAP_EXPORTS[exportName];
|
|
279
|
+
if (validator(snapExport)) {
|
|
280
|
+
return { ...acc, [exportName]: snapExport };
|
|
281
|
+
}
|
|
282
|
+
return acc;
|
|
283
|
+
}, {});
|
|
284
|
+
// If the Snap has no valid exports after this, fail.
|
|
285
|
+
(0, utils_1.assert)(Object.keys(data.exports).length > 0, 'Snap has no valid exports.');
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Instantiates a snap API object (i.e. `globalThis.snap`).
|
|
289
|
+
*
|
|
290
|
+
* @param provider - A StreamProvider connected to MetaMask.
|
|
291
|
+
* @returns The snap provider object.
|
|
292
|
+
*/
|
|
293
|
+
createSnapGlobal(provider) {
|
|
294
|
+
const originalRequest = provider.request.bind(provider);
|
|
295
|
+
const request = async (args) => {
|
|
296
|
+
const sanitizedArgs = (0, utils_2.sanitizeRequestArguments)(args);
|
|
297
|
+
(0, utils_2.assertSnapOutboundRequest)(sanitizedArgs);
|
|
298
|
+
return await (0, utils_2.withTeardown)((async () => {
|
|
299
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_notify).call(this, {
|
|
300
|
+
method: 'OutboundRequest',
|
|
301
|
+
params: { source: 'snap.request' },
|
|
302
|
+
});
|
|
303
|
+
try {
|
|
304
|
+
return await originalRequest(sanitizedArgs);
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_notify).call(this, {
|
|
308
|
+
method: 'OutboundResponse',
|
|
309
|
+
params: { source: 'snap.request' },
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
})(), this);
|
|
313
|
+
};
|
|
314
|
+
const snapGlobalProxy = (0, utils_2.proxyStreamProvider)(request);
|
|
315
|
+
return harden(snapGlobalProxy);
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Instantiates an EIP-1193 Ethereum provider object (i.e. `globalThis.ethereum`).
|
|
319
|
+
*
|
|
320
|
+
* @param provider - A StreamProvider connected to MetaMask.
|
|
321
|
+
* @returns The EIP-1193 Ethereum provider object.
|
|
322
|
+
*/
|
|
323
|
+
createEIP1193Provider(provider) {
|
|
324
|
+
const originalRequest = provider.request.bind(provider);
|
|
325
|
+
const request = async (args) => {
|
|
326
|
+
const sanitizedArgs = (0, utils_2.sanitizeRequestArguments)(args);
|
|
327
|
+
(0, utils_2.assertEthereumOutboundRequest)(sanitizedArgs);
|
|
328
|
+
return await (0, utils_2.withTeardown)((async () => {
|
|
329
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_notify).call(this, {
|
|
330
|
+
method: 'OutboundRequest',
|
|
331
|
+
params: { source: 'ethereum.request' },
|
|
332
|
+
});
|
|
333
|
+
try {
|
|
334
|
+
return await originalRequest(sanitizedArgs);
|
|
335
|
+
}
|
|
336
|
+
finally {
|
|
337
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_notify).call(this, {
|
|
338
|
+
method: 'OutboundResponse',
|
|
339
|
+
params: { source: 'ethereum.request' },
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
})(), this);
|
|
343
|
+
};
|
|
344
|
+
const streamProviderProxy = (0, utils_2.proxyStreamProvider)(request);
|
|
345
|
+
return harden(streamProviderProxy);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Removes the snap with the given name.
|
|
349
|
+
*
|
|
350
|
+
* @param snapId - The id of the snap to remove.
|
|
351
|
+
*/
|
|
352
|
+
removeSnap(snapId) {
|
|
353
|
+
this.snapData.delete(snapId);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Calls the specified executor function in the context of the specified snap.
|
|
357
|
+
* Essentially, this means that the operation performed by the executor is
|
|
358
|
+
* counted as an evaluation of the specified snap. When the count of running
|
|
359
|
+
* evaluations of a snap reaches zero, its endowments are torn down.
|
|
360
|
+
*
|
|
361
|
+
* @param snapId - The id of the snap whose context to execute in.
|
|
362
|
+
* @param executor - The function that will be executed in the snap's context.
|
|
363
|
+
* @returns The executor's return value.
|
|
364
|
+
* @template Result - The return value of the executor.
|
|
365
|
+
*/
|
|
366
|
+
async executeInSnapContext(snapId, executor) {
|
|
367
|
+
const data = this.snapData.get(snapId);
|
|
368
|
+
if (data === undefined) {
|
|
369
|
+
throw rpc_errors_1.rpcErrors.internal(`Tried to execute in context of unknown snap: "${snapId}".`);
|
|
370
|
+
}
|
|
371
|
+
let stop;
|
|
372
|
+
const stopPromise = new Promise((_, reject) => (stop = () => reject(
|
|
373
|
+
// TODO(rekmarks): Specify / standardize error code for this case.
|
|
374
|
+
rpc_errors_1.rpcErrors.internal(`The snap "${snapId}" has been terminated during execution.`))));
|
|
375
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
376
|
+
const evaluationData = { stop: stop };
|
|
377
|
+
try {
|
|
378
|
+
data.runningEvaluations.add(evaluationData);
|
|
379
|
+
// Notice that we have to await this executor.
|
|
380
|
+
// If we didn't, we would decrease the amount of running evaluations
|
|
381
|
+
// before the promise actually resolves
|
|
382
|
+
return await Promise.race([executor(), stopPromise]);
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
throw new snaps_utils_1.WrappedSnapError(error);
|
|
386
|
+
}
|
|
387
|
+
finally {
|
|
388
|
+
data.runningEvaluations.delete(evaluationData);
|
|
389
|
+
if (data.runningEvaluations.size === 0) {
|
|
390
|
+
this.lastTeardown += 1;
|
|
391
|
+
await data.idleTeardown();
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
exports.BaseSnapExecutor = BaseSnapExecutor;
|
|
397
|
+
_BaseSnapExecutor_instances = new WeakSet(), _BaseSnapExecutor_write =
|
|
398
|
+
// Awaitable function that writes back to the command stream
|
|
399
|
+
// To prevent snap execution from blocking writing we wrap in a promise
|
|
400
|
+
// and await it before continuing execution
|
|
401
|
+
async function _BaseSnapExecutor_write(chunk) {
|
|
402
|
+
return new Promise((resolve, reject) => {
|
|
403
|
+
this.commandStream.write(chunk, (error) => {
|
|
404
|
+
if (error) {
|
|
405
|
+
reject(error);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
resolve();
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
}, _BaseSnapExecutor_notify = async function _BaseSnapExecutor_notify(notification) {
|
|
412
|
+
if (!(0, utils_2.isValidResponse)(notification)) {
|
|
413
|
+
throw rpc_errors_1.rpcErrors.internal('JSON-RPC notifications must be JSON serializable objects smaller than 64 MB.');
|
|
414
|
+
}
|
|
415
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_write).call(this, {
|
|
416
|
+
...notification,
|
|
417
|
+
jsonrpc: '2.0',
|
|
418
|
+
});
|
|
419
|
+
}, _BaseSnapExecutor_respond = async function _BaseSnapExecutor_respond(id, response) {
|
|
420
|
+
if (!(0, utils_2.isValidResponse)(response)) {
|
|
421
|
+
// Instead of throwing, we directly respond with an error.
|
|
422
|
+
// This prevents an issue where we wouldn't respond when errors were non-serializable
|
|
423
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_write).call(this, {
|
|
424
|
+
error: (0, rpc_errors_1.serializeError)(rpc_errors_1.rpcErrors.internal('JSON-RPC responses must be JSON serializable objects smaller than 64 MB.')),
|
|
425
|
+
id,
|
|
426
|
+
jsonrpc: '2.0',
|
|
427
|
+
});
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
await __classPrivateFieldGet(this, _BaseSnapExecutor_instances, "m", _BaseSnapExecutor_write).call(this, {
|
|
431
|
+
...response,
|
|
432
|
+
id,
|
|
433
|
+
jsonrpc: '2.0',
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
//# sourceMappingURL=BaseSnapExecutor.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseSnapExecutor.cjs","sourceRoot":"","sources":["../../src/common/BaseSnapExecutor.ts"],"names":[],"mappings":";;;;;;;;;AAAA,qFAAqF;AACrF,gEAAgE;AAChE,+DAAoE;AAEpE,yEAAqE;AACrE,qDAA6E;AAE7E,mDAAmD;AAMnD,uDAO+B;AAC/B,uDAAqD;AAOrD,2CAMyB;AAGzB,4CAAiC;AAEjC,6CAA6D;AAC7D,uDAAgD;AAChD,qDAAuE;AACvE,iDAA6C;AAC7C,uCAOiB;AACjB,iDAKsB;AAYtB,MAAM,aAAa,GAAG;IACpB,IAAI,EAAE,uBAAU,CAAC,GAAG,CAAC,QAAQ;IAC7B,OAAO,EAAE,6BAA6B;CACvC,CAAC;AAEF,MAAM,cAAc,GAAG,sBAAS;KAC7B,QAAQ,CAAO;IACd,OAAO,EAAE,sBAAsB;CAChC,CAAC;KACD,SAAS,EAAE,CAAC;AAUf;;;;GAIG;AACH,MAAM,6BAA6B,GAAG;IACpC,IAAI,EAAE;QACJ,MAAM,EAAE,uCAA0B;QAClC,MAAM,EAAE,EAAE;KACX;IACD,WAAW,EAAE;QACX,MAAM,EAAE,8CAAiC;QACzC,MAAM,EAAE,CAAC,QAAQ,EAAE,YAAY,EAAE,YAAY,CAAC;KAC/C;IACD,SAAS,EAAE;QACT,MAAM,EAAE,4CAA+B;QACvC,MAAM,EAA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AAO,MAAX,IAAI,EAAQ;YAChB,KAAK,EAAE,IAAA,2BAAc,EACnB,sBAAS,CAAC,QAAQ,CAChB,0EAA0E,CAC3E,CACF;YACD,EAAE;YACF,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QACH,OAAO;IACT,CAAC;IAED,MAAM,uBAAA,IAAI,4DAAO,MAAX,IAAI,EAAQ;QAChB,GAAG,QAAQ;QACX,EAAE;QACF,OAAO,EAAE,KAAK;KACf,CAAC,CAAC;AACL,CAAC","sourcesContent":["// eslint-disable-next-line @typescript-eslint/triple-slash-reference, spaced-comment\n/// <reference path=\"../../../../node_modules/ses/types.d.ts\" />\nimport { createIdRemapMiddleware } from '@metamask/json-rpc-engine';\nimport type { RequestArguments } from '@metamask/providers';\nimport { StreamProvider } from '@metamask/providers/stream-provider';\nimport { errorCodes, rpcErrors, serializeError } from '@metamask/rpc-errors';\nimport type { SnapsProvider } from '@metamask/snaps-sdk';\nimport { getErrorData } from '@metamask/snaps-sdk';\nimport type {\n SnapExports,\n HandlerType,\n SnapExportsParameters,\n} from '@metamask/snaps-utils';\nimport {\n SNAP_EXPORT_NAMES,\n logError,\n SNAP_EXPORTS,\n WrappedSnapError,\n unwrapError,\n logInfo,\n} from '@metamask/snaps-utils';\nimport { validate, is } from '@metamask/superstruct';\nimport type {\n JsonRpcNotification,\n JsonRpcId,\n JsonRpcRequest,\n Json,\n} from '@metamask/utils';\nimport {\n assert,\n isJsonRpcRequest,\n hasProperty,\n getSafeJson,\n JsonRpcIdStruct,\n} from '@metamask/utils';\nimport type { Duplex } from 'readable-stream';\n\nimport { log } from '../logging';\nimport type { CommandMethodsMapping } from './commands';\nimport { getCommandMethodImplementations } from './commands';\nimport { createEndowments } from './endowments';\nimport { addEventListener, removeEventListener } from './globalEvents';\nimport { sortParamKeys } from './sortParams';\nimport {\n assertEthereumOutboundRequest,\n assertSnapOutboundRequest,\n sanitizeRequestArguments,\n proxyStreamProvider,\n withTeardown,\n isValidResponse,\n} from './utils';\nimport {\n ExecuteSnapRequestArgumentsStruct,\n PingRequestArgumentsStruct,\n SnapRpcRequestArgumentsStruct,\n TerminateRequestArgumentsStruct,\n} from './validation';\n\ntype EvaluationData = {\n stop: () => void;\n};\n\ntype SnapData = {\n exports: SnapExports;\n runningEvaluations: Set<EvaluationData>;\n idleTeardown: () => Promise<void>;\n};\n\nconst fallbackError = {\n code: errorCodes.rpc.internal,\n message: 'Execution Environment Error',\n};\n\nconst unhandledError = rpcErrors\n .internal<Json>({\n message: 'Unhandled Snap Error',\n })\n .serialize();\n\nexport type InvokeSnapArgs = Omit<SnapExportsParameters[0], 'chainId'>;\n\nexport type InvokeSnap = (\n target: string,\n handler: HandlerType,\n args: InvokeSnapArgs | undefined,\n) => Promise<Json>;\n\n/**\n * The supported methods in the execution environment. The validator checks the\n * incoming JSON-RPC request, and the `params` property is used for sorting the\n * parameters, if they are an object.\n */\nconst EXECUTION_ENVIRONMENT_METHODS = {\n ping: {\n struct: PingRequestArgumentsStruct,\n params: [],\n },\n executeSnap: {\n struct: ExecuteSnapRequestArgumentsStruct,\n params: ['snapId', 'sourceCode', 'endowments'],\n },\n terminate: {\n struct: TerminateRequestArgumentsStruct,\n params: [],\n },\n snapRpc: {\n struct: SnapRpcRequestArgumentsStruct,\n params: ['target', 'handler', 'origin', 'request'],\n },\n};\n\ntype Methods = typeof EXECUTION_ENVIRONMENT_METHODS;\n\nexport type NotifyFunction = (\n notification: Omit<JsonRpcNotification, 'jsonrpc'>,\n) => Promise<void>;\n\nexport class BaseSnapExecutor {\n private readonly snapData: Map<string, SnapData>;\n\n private readonly commandStream: Duplex;\n\n private readonly rpcStream: Duplex;\n\n private readonly methods: CommandMethodsMapping;\n\n private snapErrorHandler?: (event: ErrorEvent) => void;\n\n private snapPromiseErrorHandler?: (event: PromiseRejectionEvent) => void;\n\n private lastTeardown = 0;\n\n protected constructor(commandStream: Duplex, rpcStream: Duplex) {\n this.snapData = new Map();\n this.commandStream = commandStream;\n this.commandStream.on('data', (data) => {\n this.onCommandRequest(data).catch((error) => {\n // TODO: Decide how to handle errors.\n logError(error);\n });\n });\n this.rpcStream = rpcStream;\n\n this.methods = getCommandMethodImplementations(\n this.startSnap.bind(this),\n async (target, handlerType, args) => {\n const data = this.snapData.get(target);\n // We're capturing the handler in case someone modifies the data object\n // before the call.\n const handler = data?.exports[handlerType];\n const { required } = SNAP_EXPORTS[handlerType];\n\n assert(\n !required || handler !== undefined,\n `No ${handlerType} handler exported for snap \"${target}`,\n rpcErrors.methodNotSupported,\n );\n\n // Certain handlers are not required. If they are not exported, we\n // return null.\n if (!handler) {\n return null;\n }\n\n let result = await this.executeInSnapContext(target, () =>\n // TODO: fix handler args type cast\n handler(args as any),\n );\n\n // The handler might not return anything, but undefined is not valid JSON.\n if (result === undefined) {\n result = null;\n }\n\n // /!\\ Always return only sanitized JSON to prevent security flaws. /!\\\n try {\n return getSafeJson(result);\n } catch (error) {\n throw rpcErrors.internal(\n `Received non-JSON-serializable value: ${error.message.replace(\n /^Assertion failed: /u,\n '',\n )}`,\n );\n }\n },\n this.onTerminate.bind(this),\n );\n }\n\n private errorHandler(error: unknown, data: Record<string, Json>) {\n const serializedError = serializeError(error, {\n fallbackError: unhandledError,\n shouldIncludeStack: false,\n });\n\n const errorData = getErrorData(serializedError);\n\n this.#notify({\n method: 'UnhandledError',\n params: {\n error: {\n ...serializedError,\n data: {\n ...errorData,\n ...data,\n },\n },\n },\n }).catch((notifyError) => {\n logError(notifyError);\n });\n }\n\n private async onCommandRequest(message: JsonRpcRequest) {\n if (!isJsonRpcRequest(message)) {\n if (\n hasProperty(message, 'id') &&\n is((message as Pick<JsonRpcRequest, 'id'>).id, JsonRpcIdStruct)\n ) {\n // Instead of throwing, we directly respond with an error.\n // We can only do this if the message ID is still valid.\n await this.#write({\n error: serializeError(\n rpcErrors.internal(\n 'JSON-RPC requests must be JSON serializable objects.',\n ),\n ),\n id: (message as Pick<JsonRpcRequest, 'id'>).id,\n jsonrpc: '2.0',\n });\n } else {\n logInfo(\n 'Command stream received a non-JSON-RPC request, and was unable to respond.',\n );\n }\n return;\n }\n\n const { id, method, params } = message;\n\n if (!hasProperty(EXECUTION_ENVIRONMENT_METHODS, method)) {\n await this.#respond(id, {\n error: rpcErrors\n .methodNotFound({\n data: {\n method,\n },\n })\n .serialize(),\n });\n return;\n }\n\n const methodObject = EXECUTION_ENVIRONMENT_METHODS[method as keyof Methods];\n\n // support params by-name and by-position\n const paramsAsArray = sortParamKeys(methodObject.params, params);\n\n const [error] = validate<any, any>(paramsAsArray, methodObject.struct);\n if (error) {\n await this.#respond(id, {\n error: rpcErrors\n .invalidParams({\n message: `Invalid parameters for method \"${method}\": ${error.message}.`,\n data: {\n method,\n params: paramsAsArray,\n },\n })\n .serialize(),\n });\n return;\n }\n\n try {\n const result = await (this.methods as any)[method](...paramsAsArray);\n await this.#respond(id, { result });\n } catch (rpcError) {\n await this.#respond(id, {\n error: serializeError(rpcError, {\n fallbackError,\n }),\n });\n }\n }\n\n // Awaitable function that writes back to the command stream\n // To prevent snap execution from blocking writing we wrap in a promise\n // and await it before continuing execution\n async #write(chunk: Json) {\n return new Promise<void>((resolve, reject) => {\n this.commandStream.write(chunk, (error) => {\n if (error) {\n reject(error);\n return;\n }\n resolve();\n });\n });\n }\n\n async #notify(notification: Omit<JsonRpcNotification, 'jsonrpc'>) {\n if (!isValidResponse(notification)) {\n throw rpcErrors.internal(\n 'JSON-RPC notifications must be JSON serializable objects smaller than 64 MB.',\n );\n }\n\n await this.#write({\n ...notification,\n jsonrpc: '2.0',\n });\n }\n\n async #respond(id: JsonRpcId, response: Record<string, unknown>) {\n if (!isValidResponse(response)) {\n // Instead of throwing, we directly respond with an error.\n // This prevents an issue where we wouldn't respond when errors were non-serializable\n await this.#write({\n error: serializeError(\n rpcErrors.internal(\n 'JSON-RPC responses must be JSON serializable objects smaller than 64 MB.',\n ),\n ),\n id,\n jsonrpc: '2.0',\n });\n return;\n }\n\n await this.#write({\n ...response,\n id,\n jsonrpc: '2.0',\n });\n }\n\n /**\n * Attempts to evaluate a snap in SES. Generates APIs for the snap. May throw\n * on errors.\n *\n * @param snapId - The id of the snap.\n * @param sourceCode - The source code of the snap, in IIFE format.\n * @param _endowments - An array of the names of the endowments.\n */\n protected async startSnap(\n snapId: string,\n sourceCode: string,\n _endowments: string[],\n ): Promise<void> {\n log(`Starting snap '${snapId}' in worker.`);\n if (this.snapPromiseErrorHandler) {\n removeEventListener('unhandledrejection', this.snapPromiseErrorHandler);\n }\n\n if (this.snapErrorHandler) {\n removeEventListener('error', this.snapErrorHandler);\n }\n\n this.snapErrorHandler = (error: ErrorEvent) => {\n this.errorHandler(error.error, { snapId });\n };\n\n this.snapPromiseErrorHandler = (error: PromiseRejectionEvent) => {\n this.errorHandler(error instanceof Error ? error : error.reason, {\n snapId,\n });\n };\n\n const provider = new StreamProvider(this.rpcStream, {\n jsonRpcStreamName: 'metamask-provider',\n rpcMiddleware: [createIdRemapMiddleware()],\n });\n\n await provider.initialize();\n\n const snap = this.createSnapGlobal(provider);\n const ethereum = this.createEIP1193Provider(provider);\n // We specifically use any type because the Snap can modify the object any way they want\n const snapModule: any = { exports: {} };\n\n try {\n const { endowments, teardown: endowmentTeardown } = createEndowments({\n snap,\n ethereum,\n snapId,\n endowments: _endowments,\n notify: this.#notify.bind(this),\n });\n\n // !!! Ensure that this is the only place the data is being set.\n // Other methods access the object value and mutate its properties.\n this.snapData.set(snapId, {\n idleTeardown: endowmentTeardown,\n runningEvaluations: new Set(),\n exports: {},\n });\n\n addEventListener('unhandledRejection', this.snapPromiseErrorHandler);\n addEventListener('error', this.snapErrorHandler);\n\n const compartment = new Compartment({\n ...endowments,\n module: snapModule,\n exports: snapModule.exports,\n });\n\n // All of those are JavaScript runtime specific and self referential,\n // but we add them for compatibility sake with external libraries.\n //\n // We can't do that in the injected globals object above\n // because SES creates its own globalThis\n compartment.globalThis.self = compartment.globalThis;\n compartment.globalThis.global = compartment.globalThis;\n compartment.globalThis.window = compartment.globalThis;\n\n await this.executeInSnapContext(snapId, () => {\n compartment.evaluate(sourceCode);\n this.registerSnapExports(snapId, snapModule);\n });\n } catch (error) {\n this.removeSnap(snapId);\n\n const [cause] = unwrapError(error);\n throw rpcErrors.internal({\n message: `Error while running snap '${snapId}': ${cause.message}`,\n data: {\n cause: cause.serialize(),\n },\n });\n }\n }\n\n /**\n * Cancels all running evaluations of all snaps and clears all snap data.\n * NOTE:** Should only be called in response to the `terminate` RPC command.\n */\n protected onTerminate() {\n // `stop()` tears down snap endowments.\n // Teardown will also be run for each snap as soon as there are\n // no more running evaluations for that snap.\n this.snapData.forEach((data) =>\n data.runningEvaluations.forEach((evaluation) => evaluation.stop()),\n );\n this.snapData.clear();\n }\n\n private registerSnapExports(snapId: string, snapModule: any) {\n const data = this.snapData.get(snapId);\n // Somebody deleted the snap before we could register.\n if (!data) {\n return;\n }\n\n data.exports = SNAP_EXPORT_NAMES.reduce((acc, exportName) => {\n const snapExport = snapModule.exports[exportName];\n const { validator } = SNAP_EXPORTS[exportName];\n if (validator(snapExport)) {\n return { ...acc, [exportName]: snapExport };\n }\n return acc;\n }, {});\n\n // If the Snap has no valid exports after this, fail.\n assert(Object.keys(data.exports).length > 0, 'Snap has no valid exports.');\n }\n\n /**\n * Instantiates a snap API object (i.e. `globalThis.snap`).\n *\n * @param provider - A StreamProvider connected to MetaMask.\n * @returns The snap provider object.\n */\n private createSnapGlobal(provider: StreamProvider): SnapsProvider {\n const originalRequest = provider.request.bind(provider);\n\n const request = async (args: RequestArguments) => {\n const sanitizedArgs = sanitizeRequestArguments(args);\n assertSnapOutboundRequest(sanitizedArgs);\n return await withTeardown(\n (async () => {\n await this.#notify({\n method: 'OutboundRequest',\n params: { source: 'snap.request' },\n });\n try {\n return await originalRequest(sanitizedArgs);\n } finally {\n await this.#notify({\n method: 'OutboundResponse',\n params: { source: 'snap.request' },\n });\n }\n })(),\n this as any,\n );\n };\n\n const snapGlobalProxy = proxyStreamProvider(request) as SnapsProvider;\n\n return harden(snapGlobalProxy);\n }\n\n /**\n * Instantiates an EIP-1193 Ethereum provider object (i.e. `globalThis.ethereum`).\n *\n * @param provider - A StreamProvider connected to MetaMask.\n * @returns The EIP-1193 Ethereum provider object.\n */\n private createEIP1193Provider(provider: StreamProvider): StreamProvider {\n const originalRequest = provider.request.bind(provider);\n\n const request = async (args: RequestArguments) => {\n const sanitizedArgs = sanitizeRequestArguments(args);\n assertEthereumOutboundRequest(sanitizedArgs);\n return await withTeardown(\n (async () => {\n await this.#notify({\n method: 'OutboundRequest',\n params: { source: 'ethereum.request' },\n });\n try {\n return await originalRequest(sanitizedArgs);\n } finally {\n await this.#notify({\n method: 'OutboundResponse',\n params: { source: 'ethereum.request' },\n });\n }\n })(),\n this as any,\n );\n };\n\n const streamProviderProxy = proxyStreamProvider(request);\n\n return harden(streamProviderProxy);\n }\n\n /**\n * Removes the snap with the given name.\n *\n * @param snapId - The id of the snap to remove.\n */\n private removeSnap(snapId: string): void {\n this.snapData.delete(snapId);\n }\n\n /**\n * Calls the specified executor function in the context of the specified snap.\n * Essentially, this means that the operation performed by the executor is\n * counted as an evaluation of the specified snap. When the count of running\n * evaluations of a snap reaches zero, its endowments are torn down.\n *\n * @param snapId - The id of the snap whose context to execute in.\n * @param executor - The function that will be executed in the snap's context.\n * @returns The executor's return value.\n * @template Result - The return value of the executor.\n */\n private async executeInSnapContext<Result>(\n snapId: string,\n executor: () => Promise<Result> | Result,\n ): Promise<Result> {\n const data = this.snapData.get(snapId);\n if (data === undefined) {\n throw rpcErrors.internal(\n `Tried to execute in context of unknown snap: \"${snapId}\".`,\n );\n }\n\n let stop: () => void;\n const stopPromise = new Promise<never>(\n (_, reject) =>\n (stop = () =>\n reject(\n // TODO(rekmarks): Specify / standardize error code for this case.\n rpcErrors.internal(\n `The snap \"${snapId}\" has been terminated during execution.`,\n ),\n )),\n );\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const evaluationData = { stop: stop! };\n\n try {\n data.runningEvaluations.add(evaluationData);\n // Notice that we have to await this executor.\n // If we didn't, we would decrease the amount of running evaluations\n // before the promise actually resolves\n return await Promise.race([executor(), stopPromise]);\n } catch (error) {\n throw new WrappedSnapError(error);\n } finally {\n data.runningEvaluations.delete(evaluationData);\n\n if (data.runningEvaluations.size === 0) {\n this.lastTeardown += 1;\n await data.idleTeardown();\n }\n }\n }\n}\n"]}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="ses" />
|
|
3
|
-
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
export
|
|
8
|
-
export
|
|
3
|
+
/// <reference types="src/types/vendor/readable-stream" />
|
|
4
|
+
import type { HandlerType, SnapExportsParameters } from "@metamask/snaps-utils";
|
|
5
|
+
import type { JsonRpcNotification, Json } from "@metamask/utils";
|
|
6
|
+
import type { Duplex } from "readable-stream";
|
|
7
|
+
export type InvokeSnapArgs = Omit<SnapExportsParameters[0], 'chainId'>;
|
|
8
|
+
export type InvokeSnap = (target: string, handler: HandlerType, args: InvokeSnapArgs | undefined) => Promise<Json>;
|
|
9
|
+
export type NotifyFunction = (notification: Omit<JsonRpcNotification, 'jsonrpc'>) => Promise<void>;
|
|
9
10
|
export declare class BaseSnapExecutor {
|
|
10
11
|
#private;
|
|
11
12
|
private readonly snapData;
|
|
@@ -66,3 +67,4 @@ export declare class BaseSnapExecutor {
|
|
|
66
67
|
*/
|
|
67
68
|
private executeInSnapContext;
|
|
68
69
|
}
|
|
70
|
+
//# sourceMappingURL=BaseSnapExecutor.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseSnapExecutor.d.cts","sourceRoot":"","sources":["../../src/common/BaseSnapExecutor.ts"],"names":[],"mappings":";;;AAQA,OAAO,KAAK,EAEV,WAAW,EACX,qBAAqB,EACtB,8BAA8B;AAU/B,OAAO,KAAK,EACV,mBAAmB,EAGnB,IAAI,EACL,wBAAwB;AAQzB,OAAO,KA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@@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="ses" />
|
|
3
|
+
/// <reference types="src/types/vendor/readable-stream" />
|
|
4
|
+
import type { HandlerType, SnapExportsParameters } from "@metamask/snaps-utils";
|
|
5
|
+
import type { JsonRpcNotification, Json } from "@metamask/utils";
|
|
6
|
+
import type { Duplex } from "readable-stream";
|
|
7
|
+
export type InvokeSnapArgs = Omit<SnapExportsParameters[0], 'chainId'>;
|
|
8
|
+
export type InvokeSnap = (target: string, handler: HandlerType, args: InvokeSnapArgs | undefined) => Promise<Json>;
|
|
9
|
+
export type NotifyFunction = (notification: Omit<JsonRpcNotification, 'jsonrpc'>) => Promise<void>;
|
|
10
|
+
export declare class BaseSnapExecutor {
|
|
11
|
+
#private;
|
|
12
|
+
private readonly snapData;
|
|
13
|
+
private readonly commandStream;
|
|
14
|
+
private readonly rpcStream;
|
|
15
|
+
private readonly methods;
|
|
16
|
+
private snapErrorHandler?;
|
|
17
|
+
private snapPromiseErrorHandler?;
|
|
18
|
+
private lastTeardown;
|
|
19
|
+
protected constructor(commandStream: Duplex, rpcStream: Duplex);
|
|
20
|
+
private errorHandler;
|
|
21
|
+
private onCommandRequest;
|
|
22
|
+
/**
|
|
23
|
+
* Attempts to evaluate a snap in SES. Generates APIs for the snap. May throw
|
|
24
|
+
* on errors.
|
|
25
|
+
*
|
|
26
|
+
* @param snapId - The id of the snap.
|
|
27
|
+
* @param sourceCode - The source code of the snap, in IIFE format.
|
|
28
|
+
* @param _endowments - An array of the names of the endowments.
|
|
29
|
+
*/
|
|
30
|
+
protected startSnap(snapId: string, sourceCode: string, _endowments: string[]): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Cancels all running evaluations of all snaps and clears all snap data.
|
|
33
|
+
* NOTE:** Should only be called in response to the `terminate` RPC command.
|
|
34
|
+
*/
|
|
35
|
+
protected onTerminate(): void;
|
|
36
|
+
private registerSnapExports;
|
|
37
|
+
/**
|
|
38
|
+
* Instantiates a snap API object (i.e. `globalThis.snap`).
|
|
39
|
+
*
|
|
40
|
+
* @param provider - A StreamProvider connected to MetaMask.
|
|
41
|
+
* @returns The snap provider object.
|
|
42
|
+
*/
|
|
43
|
+
private createSnapGlobal;
|
|
44
|
+
/**
|
|
45
|
+
* Instantiates an EIP-1193 Ethereum provider object (i.e. `globalThis.ethereum`).
|
|
46
|
+
*
|
|
47
|
+
* @param provider - A StreamProvider connected to MetaMask.
|
|
48
|
+
* @returns The EIP-1193 Ethereum provider object.
|
|
49
|
+
*/
|
|
50
|
+
private createEIP1193Provider;
|
|
51
|
+
/**
|
|
52
|
+
* Removes the snap with the given name.
|
|
53
|
+
*
|
|
54
|
+
* @param snapId - The id of the snap to remove.
|
|
55
|
+
*/
|
|
56
|
+
private removeSnap;
|
|
57
|
+
/**
|
|
58
|
+
* Calls the specified executor function in the context of the specified snap.
|
|
59
|
+
* Essentially, this means that the operation performed by the executor is
|
|
60
|
+
* counted as an evaluation of the specified snap. When the count of running
|
|
61
|
+
* evaluations of a snap reaches zero, its endowments are torn down.
|
|
62
|
+
*
|
|
63
|
+
* @param snapId - The id of the snap whose context to execute in.
|
|
64
|
+
* @param executor - The function that will be executed in the snap's context.
|
|
65
|
+
* @returns The executor's return value.
|
|
66
|
+
* @template Result - The return value of the executor.
|
|
67
|
+
*/
|
|
68
|
+
private executeInSnapContext;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=BaseSnapExecutor.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseSnapExecutor.d.mts","sourceRoot":"","sources":["../../src/common/BaseSnapExecutor.ts"],"names":[],"mappings":";;;AAQA,OAAO,KAAK,EAEV,WAAW,EACX,qBAAqB,EACtB,8BAA8B;AAU/B,OAAO,KAAK,EACV,mBAAmB,EAGnB,IAAI,EACL,wBAAwB;AAQzB,OAAO,KAAK,EAAE,MAAM,EAAE,wBAAwB;AA4C9C,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,q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