@metamask/snaps-controllers 4.0.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -1
- package/dist/cjs/cronjob/CronjobController.js +3 -3
- package/dist/cjs/cronjob/CronjobController.js.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/interface/SnapInterfaceController.js +166 -0
- package/dist/cjs/interface/SnapInterfaceController.js.map +1 -0
- package/dist/cjs/interface/index.js +20 -0
- package/dist/cjs/interface/index.js.map +1 -0
- package/dist/cjs/interface/utils.js +59 -0
- package/dist/cjs/interface/utils.js.map +1 -0
- package/dist/cjs/services/ProxyPostMessageStream.js +3 -10
- package/dist/cjs/services/ProxyPostMessageStream.js.map +1 -1
- package/dist/cjs/services/browser.js +1 -0
- package/dist/cjs/services/browser.js.map +1 -1
- package/dist/cjs/services/index.js +1 -0
- package/dist/cjs/services/index.js.map +1 -1
- package/dist/cjs/services/node/NodeProcessExecutionService.js +13 -1
- package/dist/cjs/services/node/NodeProcessExecutionService.js.map +1 -1
- package/dist/cjs/services/node/NodeThreadExecutionService.js +14 -1
- package/dist/cjs/services/node/NodeThreadExecutionService.js.map +1 -1
- package/dist/cjs/services/offscreen/OffscreenExecutionService.js +36 -99
- package/dist/cjs/services/offscreen/OffscreenExecutionService.js.map +1 -1
- package/dist/cjs/services/proxy/ProxyExecutionService.js +110 -0
- package/dist/cjs/services/proxy/ProxyExecutionService.js.map +1 -0
- package/dist/cjs/services/webview/WebViewExecutionService.js +99 -0
- package/dist/cjs/services/webview/WebViewExecutionService.js.map +1 -0
- package/dist/cjs/services/webview/WebViewMessageStream.js +127 -0
- package/dist/cjs/services/webview/WebViewMessageStream.js.map +1 -0
- package/dist/cjs/services/webview/index.js +20 -0
- package/dist/cjs/services/webview/index.js.map +1 -0
- package/dist/cjs/snaps/SnapController.js +303 -138
- package/dist/cjs/snaps/SnapController.js.map +1 -1
- package/dist/cjs/snaps/constants.js +25 -0
- package/dist/cjs/snaps/constants.js.map +1 -0
- package/dist/cjs/snaps/index.js +0 -2
- package/dist/cjs/snaps/index.js.map +1 -1
- package/dist/cjs/snaps/location/npm.js +13 -2
- package/dist/cjs/snaps/location/npm.js.map +1 -1
- package/dist/cjs/utils.js +32 -0
- package/dist/cjs/utils.js.map +1 -1
- package/dist/esm/cronjob/CronjobController.js +2 -2
- package/dist/esm/cronjob/CronjobController.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/interface/SnapInterfaceController.js +158 -0
- package/dist/esm/interface/SnapInterfaceController.js.map +1 -0
- package/dist/esm/interface/index.js +3 -0
- package/dist/esm/interface/index.js.map +1 -0
- package/dist/esm/interface/utils.js +62 -0
- package/dist/esm/interface/utils.js.map +1 -0
- package/dist/esm/services/ProxyPostMessageStream.js +3 -10
- package/dist/esm/services/ProxyPostMessageStream.js.map +1 -1
- package/dist/esm/services/browser.js +1 -0
- package/dist/esm/services/browser.js.map +1 -1
- package/dist/esm/services/index.js +1 -0
- package/dist/esm/services/index.js.map +1 -1
- package/dist/esm/services/node/NodeProcessExecutionService.js +13 -1
- package/dist/esm/services/node/NodeProcessExecutionService.js.map +1 -1
- package/dist/esm/services/node/NodeThreadExecutionService.js +14 -1
- package/dist/esm/services/node/NodeThreadExecutionService.js.map +1 -1
- package/dist/esm/services/offscreen/OffscreenExecutionService.js +36 -99
- package/dist/esm/services/offscreen/OffscreenExecutionService.js.map +1 -1
- package/dist/esm/services/proxy/ProxyExecutionService.js +100 -0
- package/dist/esm/services/proxy/ProxyExecutionService.js.map +1 -0
- package/dist/esm/services/webview/WebViewExecutionService.js +89 -0
- package/dist/esm/services/webview/WebViewExecutionService.js.map +1 -0
- package/dist/esm/services/webview/WebViewMessageStream.js +119 -0
- package/dist/esm/services/webview/WebViewMessageStream.js.map +1 -0
- package/dist/esm/services/webview/index.js +3 -0
- package/dist/esm/services/webview/index.js.map +1 -0
- package/dist/esm/snaps/SnapController.js +299 -134
- package/dist/esm/snaps/SnapController.js.map +1 -1
- package/dist/esm/snaps/constants.js +16 -0
- package/dist/esm/snaps/constants.js.map +1 -0
- package/dist/esm/snaps/index.js +0 -2
- package/dist/esm/snaps/index.js.map +1 -1
- package/dist/esm/snaps/location/npm.js +13 -2
- package/dist/esm/snaps/location/npm.js.map +1 -1
- package/dist/esm/utils.js +37 -0
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/interface/SnapInterfaceController.d.ts +85 -0
- package/dist/types/interface/index.d.ts +1 -0
- package/dist/types/interface/utils.d.ts +36 -0
- package/dist/types/services/ProxyPostMessageStream.d.ts +1 -2
- package/dist/types/services/browser.d.ts +1 -0
- package/dist/types/services/index.d.ts +1 -0
- package/dist/types/services/offscreen/OffscreenExecutionService.d.ts +5 -22
- package/dist/types/services/proxy/ProxyExecutionService.d.ts +39 -0
- package/dist/types/services/webview/WebViewExecutionService.d.ts +20 -0
- package/dist/types/services/webview/WebViewMessageStream.d.ts +32 -0
- package/dist/types/services/webview/index.d.ts +1 -0
- package/dist/types/snaps/SnapController.d.ts +37 -6
- package/dist/types/snaps/constants.d.ts +1 -0
- package/dist/types/snaps/index.d.ts +0 -2
- package/dist/types/utils.d.ts +119 -0
- package/package.json +14 -14
- package/dist/cjs/snaps/endowments/cronjob.js +0 -100
- package/dist/cjs/snaps/endowments/cronjob.js.map +0 -1
- package/dist/cjs/snaps/endowments/enum.js +0 -25
- package/dist/cjs/snaps/endowments/enum.js.map +0 -1
- package/dist/cjs/snaps/endowments/ethereum-provider.js +0 -43
- package/dist/cjs/snaps/endowments/ethereum-provider.js.map +0 -1
- package/dist/cjs/snaps/endowments/home-page.js +0 -37
- package/dist/cjs/snaps/endowments/home-page.js.map +0 -1
- package/dist/cjs/snaps/endowments/index.js +0 -99
- package/dist/cjs/snaps/endowments/index.js.map +0 -1
- package/dist/cjs/snaps/endowments/keyring.js +0 -100
- package/dist/cjs/snaps/endowments/keyring.js.map +0 -1
- package/dist/cjs/snaps/endowments/lifecycle-hooks.js +0 -37
- package/dist/cjs/snaps/endowments/lifecycle-hooks.js.map +0 -1
- package/dist/cjs/snaps/endowments/name-lookup.js +0 -106
- package/dist/cjs/snaps/endowments/name-lookup.js.map +0 -1
- package/dist/cjs/snaps/endowments/network-access.js +0 -44
- package/dist/cjs/snaps/endowments/network-access.js.map +0 -1
- package/dist/cjs/snaps/endowments/rpc.js +0 -99
- package/dist/cjs/snaps/endowments/rpc.js.map +0 -1
- package/dist/cjs/snaps/endowments/transaction-insight.js +0 -106
- package/dist/cjs/snaps/endowments/transaction-insight.js.map +0 -1
- package/dist/cjs/snaps/endowments/web-assembly.js +0 -42
- package/dist/cjs/snaps/endowments/web-assembly.js.map +0 -1
- package/dist/cjs/snaps/permissions.js +0 -61
- package/dist/cjs/snaps/permissions.js.map +0 -1
- package/dist/esm/snaps/endowments/cronjob.js +0 -99
- package/dist/esm/snaps/endowments/cronjob.js.map +0 -1
- package/dist/esm/snaps/endowments/enum.js +0 -15
- package/dist/esm/snaps/endowments/enum.js.map +0 -1
- package/dist/esm/snaps/endowments/ethereum-provider.js +0 -33
- package/dist/esm/snaps/endowments/ethereum-provider.js.map +0 -1
- package/dist/esm/snaps/endowments/home-page.js +0 -27
- package/dist/esm/snaps/endowments/home-page.js.map +0 -1
- package/dist/esm/snaps/endowments/index.js +0 -54
- package/dist/esm/snaps/endowments/index.js.map +0 -1
- package/dist/esm/snaps/endowments/keyring.js +0 -91
- package/dist/esm/snaps/endowments/keyring.js.map +0 -1
- package/dist/esm/snaps/endowments/lifecycle-hooks.js +0 -27
- package/dist/esm/snaps/endowments/lifecycle-hooks.js.map +0 -1
- package/dist/esm/snaps/endowments/name-lookup.js +0 -98
- package/dist/esm/snaps/endowments/name-lookup.js.map +0 -1
- package/dist/esm/snaps/endowments/network-access.js +0 -34
- package/dist/esm/snaps/endowments/network-access.js.map +0 -1
- package/dist/esm/snaps/endowments/rpc.js +0 -88
- package/dist/esm/snaps/endowments/rpc.js.map +0 -1
- package/dist/esm/snaps/endowments/transaction-insight.js +0 -99
- package/dist/esm/snaps/endowments/transaction-insight.js.map +0 -1
- package/dist/esm/snaps/endowments/web-assembly.js +0 -32
- package/dist/esm/snaps/endowments/web-assembly.js.map +0 -1
- package/dist/esm/snaps/permissions.js +0 -50
- package/dist/esm/snaps/permissions.js.map +0 -1
- package/dist/types/snaps/endowments/cronjob.d.ts +0 -51
- package/dist/types/snaps/endowments/enum.d.ts +0 -12
- package/dist/types/snaps/endowments/ethereum-provider.d.ts +0 -14
- package/dist/types/snaps/endowments/home-page.d.ts +0 -15
- package/dist/types/snaps/endowments/index.d.ts +0 -115
- package/dist/types/snaps/endowments/keyring.d.ts +0 -39
- package/dist/types/snaps/endowments/lifecycle-hooks.d.ts +0 -15
- package/dist/types/snaps/endowments/name-lookup.d.ts +0 -38
- package/dist/types/snaps/endowments/network-access.d.ts +0 -14
- package/dist/types/snaps/endowments/rpc.d.ts +0 -38
- package/dist/types/snaps/endowments/transaction-insight.d.ts +0 -39
- package/dist/types/snaps/endowments/web-assembly.d.ts +0 -14
- package/dist/types/snaps/permissions.d.ts +0 -16
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/services/node/NodeThreadExecutionService.ts"],"sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { ThreadParentMessageStream } from '@metamask/post-message-stream';\n// eslint-disable-next-line @typescript-eslint/no-shadow\nimport { Worker } from 'worker_threads';\n\nimport type { Job } from '..';\nimport { AbstractExecutionService } from '..';\n\nexport class NodeThreadExecutionService extends AbstractExecutionService<Worker> {\n protected async initEnvStream(): Promise<{\n worker: Worker;\n stream: BasePostMessageStream;\n }> {\n const worker = new Worker(\n require.resolve(\n '@metamask/snaps-execution-environments/dist/browserify/node-thread/bundle.js',\n ),\n );\n const stream = new ThreadParentMessageStream({ thread: worker });\n return Promise.resolve({ worker, stream });\n }\n\n protected async terminateJob(jobWrapper: Job<Worker>): Promise<void> {\n await jobWrapper.worker.terminate();\n }\n}\n"],"names":["NodeThreadExecutionService","AbstractExecutionService","initEnvStream","worker","Worker","require","resolve","stream","ThreadParentMessageStream","thread","Promise","terminateJob","jobWrapper","terminate"],"mappings":";;;;+BAQaA;;;eAAAA;;;mCAP6B;gCAEnB;kBAGkB;AAElC,MAAMA,mCAAmCC,0BAAwB;IACtE,MAAgBC,gBAGb;QACD,MAAMC,SAAS,IAAIC,sBAAM,CACvBC,QAAQC,OAAO,CACb;
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/node/NodeThreadExecutionService.ts"],"sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { ThreadParentMessageStream } from '@metamask/post-message-stream';\n// eslint-disable-next-line @typescript-eslint/no-shadow\nimport { Worker } from 'worker_threads';\n\nimport type { Job } from '..';\nimport { AbstractExecutionService } from '..';\n\nexport class NodeThreadExecutionService extends AbstractExecutionService<Worker> {\n protected async initEnvStream(): Promise<{\n worker: Worker;\n stream: BasePostMessageStream;\n }> {\n const worker = new Worker(\n require.resolve(\n '@metamask/snaps-execution-environments/dist/browserify/node-thread/bundle.js',\n ),\n {\n stdout: true,\n stderr: true,\n },\n );\n\n // Capturing `stdout` and `stderr` from the worker prevents the worker from\n // writing to them directly, making it easier to capture them Jest.\n worker.stdout.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.log(data.toString());\n });\n\n worker.stderr.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.error(data.toString());\n });\n\n const stream = new ThreadParentMessageStream({ thread: worker });\n return Promise.resolve({ worker, stream });\n }\n\n protected async terminateJob(jobWrapper: Job<Worker>): Promise<void> {\n await jobWrapper.worker.terminate();\n }\n}\n"],"names":["NodeThreadExecutionService","AbstractExecutionService","initEnvStream","worker","Worker","require","resolve","stdout","stderr","on","data","console","log","toString","error","stream","ThreadParentMessageStream","thread","Promise","terminateJob","jobWrapper","terminate"],"mappings":";;;;+BAQaA;;;eAAAA;;;mCAP6B;gCAEnB;kBAGkB;AAElC,MAAMA,mCAAmCC,0BAAwB;IACtE,MAAgBC,gBAGb;QACD,MAAMC,SAAS,IAAIC,sBAAM,CACvBC,QAAQC,OAAO,CACb,iFAEF;YACEC,QAAQ;YACRC,QAAQ;QACV;QAGF,2EAA2E;QAC3E,mEAAmE;QACnEL,OAAOI,MAAM,CAACE,EAAE,CAAC,QAAQ,CAACC;YACxB,sCAAsC;YACtCC,QAAQC,GAAG,CAACF,KAAKG,QAAQ;QAC3B;QAEAV,OAAOK,MAAM,CAACC,EAAE,CAAC,QAAQ,CAACC;YACxB,sCAAsC;YACtCC,QAAQG,KAAK,CAACJ,KAAKG,QAAQ;QAC7B;QAEA,MAAME,SAAS,IAAIC,4CAAyB,CAAC;YAAEC,QAAQd;QAAO;QAC9D,OAAOe,QAAQZ,OAAO,CAAC;YAAEH;YAAQY;QAAO;IAC1C;IAEA,MAAgBI,aAAaC,UAAuB,EAAiB;QACnE,MAAMA,WAAWjB,MAAM,CAACkB,SAAS;IACnC;AACF"}
|
|
@@ -9,48 +9,21 @@ Object.defineProperty(exports, "OffscreenExecutionService", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
const _postmessagestream = require("@metamask/post-message-stream");
|
|
12
|
-
const
|
|
13
|
-
const _AbstractExecutionService = require("../AbstractExecutionService");
|
|
14
|
-
const _ProxyPostMessageStream = require("../ProxyPostMessageStream");
|
|
12
|
+
const _ProxyExecutionService = require("../proxy/ProxyExecutionService");
|
|
15
13
|
function _check_private_redeclaration(obj, privateCollection) {
|
|
16
14
|
if (privateCollection.has(obj)) {
|
|
17
15
|
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
18
16
|
}
|
|
19
17
|
}
|
|
20
|
-
function
|
|
21
|
-
if (
|
|
22
|
-
|
|
18
|
+
function _class_private_method_get(receiver, privateSet, fn) {
|
|
19
|
+
if (!privateSet.has(receiver)) {
|
|
20
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
23
21
|
}
|
|
24
|
-
return
|
|
22
|
+
return fn;
|
|
25
23
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} else {
|
|
30
|
-
if (!descriptor.writable) {
|
|
31
|
-
throw new TypeError("attempted to set read only private field");
|
|
32
|
-
}
|
|
33
|
-
descriptor.value = value;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
37
|
-
if (!privateMap.has(receiver)) {
|
|
38
|
-
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
39
|
-
}
|
|
40
|
-
return privateMap.get(receiver);
|
|
41
|
-
}
|
|
42
|
-
function _class_private_field_get(receiver, privateMap) {
|
|
43
|
-
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
44
|
-
return _class_apply_descriptor_get(receiver, descriptor);
|
|
45
|
-
}
|
|
46
|
-
function _class_private_field_init(obj, privateMap, value) {
|
|
47
|
-
_check_private_redeclaration(obj, privateMap);
|
|
48
|
-
privateMap.set(obj, value);
|
|
49
|
-
}
|
|
50
|
-
function _class_private_field_set(receiver, privateMap, value) {
|
|
51
|
-
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
52
|
-
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
53
|
-
return value;
|
|
24
|
+
function _class_private_method_init(obj, privateSet) {
|
|
25
|
+
_check_private_redeclaration(obj, privateSet);
|
|
26
|
+
privateSet.add(obj);
|
|
54
27
|
}
|
|
55
28
|
function _define_property(obj, key, value) {
|
|
56
29
|
if (key in obj) {
|
|
@@ -65,24 +38,8 @@ function _define_property(obj, key, value) {
|
|
|
65
38
|
}
|
|
66
39
|
return obj;
|
|
67
40
|
}
|
|
68
|
-
var
|
|
69
|
-
class OffscreenExecutionService extends
|
|
70
|
-
/**
|
|
71
|
-
* Send a termination command to the offscreen document.
|
|
72
|
-
*
|
|
73
|
-
* @param job - The job to terminate.
|
|
74
|
-
*/ async terminateJob(job) {
|
|
75
|
-
// The `AbstractExecutionService` will have already closed the job stream,
|
|
76
|
-
// so we write to the runtime stream directly.
|
|
77
|
-
_class_private_field_get(this, _runtimeStream).write({
|
|
78
|
-
jobId: job.id,
|
|
79
|
-
data: {
|
|
80
|
-
jsonrpc: '2.0',
|
|
81
|
-
method: 'terminateJob',
|
|
82
|
-
id: (0, _nanoid.nanoid)()
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
41
|
+
var _createDocument = /*#__PURE__*/ new WeakSet();
|
|
42
|
+
class OffscreenExecutionService extends _ProxyExecutionService.ProxyExecutionService {
|
|
86
43
|
/**
|
|
87
44
|
* Create a new stream for the specified job. This wraps the runtime stream
|
|
88
45
|
* in a stream specific to the job.
|
|
@@ -90,38 +47,8 @@ class OffscreenExecutionService extends _AbstractExecutionService.AbstractExecut
|
|
|
90
47
|
* @param jobId - The job ID.
|
|
91
48
|
*/ async initEnvStream(jobId) {
|
|
92
49
|
// Lazily create the offscreen document.
|
|
93
|
-
await this.
|
|
94
|
-
|
|
95
|
-
stream: _class_private_field_get(this, _runtimeStream),
|
|
96
|
-
extra: {
|
|
97
|
-
// TODO: Rather than injecting the frame URL here, we should come up
|
|
98
|
-
// with a better way to do this. The frame URL is needed to avoid hard
|
|
99
|
-
// coding it in the offscreen execution environment.
|
|
100
|
-
frameUrl: this.frameUrl.toString()
|
|
101
|
-
},
|
|
102
|
-
jobId
|
|
103
|
-
});
|
|
104
|
-
return {
|
|
105
|
-
worker: jobId,
|
|
106
|
-
stream
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Creates the offscreen document to be used as the execution environment.
|
|
111
|
-
*
|
|
112
|
-
* If the document already exists, this does nothing.
|
|
113
|
-
*/ async createDocument() {
|
|
114
|
-
// Extensions can only have a single offscreen document.
|
|
115
|
-
if (await chrome.offscreen.hasDocument()) {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
await chrome.offscreen.createDocument({
|
|
119
|
-
justification: 'MetaMask Snaps Execution Environment',
|
|
120
|
-
reasons: [
|
|
121
|
-
'IFRAME_SCRIPTING'
|
|
122
|
-
],
|
|
123
|
-
url: this.documentUrl.toString()
|
|
124
|
-
});
|
|
50
|
+
await _class_private_method_get(this, _createDocument, createDocument).call(this);
|
|
51
|
+
return super.initEnvStream(jobId);
|
|
125
52
|
}
|
|
126
53
|
/**
|
|
127
54
|
* Create a new offscreen execution service.
|
|
@@ -130,30 +57,40 @@ class OffscreenExecutionService extends _AbstractExecutionService.AbstractExecut
|
|
|
130
57
|
* @param args.documentUrl - The URL of the offscreen document to use as the
|
|
131
58
|
* execution environment. This must be a URL relative to the location where
|
|
132
59
|
* this is called. This cannot be a public (http(s)) URL.
|
|
133
|
-
* @param args.frameUrl - The URL of the iframe to load inside the offscreen
|
|
134
|
-
* document.
|
|
135
60
|
* @param args.messenger - The messenger to use for communication with the
|
|
136
61
|
* `SnapController`.
|
|
137
62
|
* @param args.setupSnapProvider - The function to use to set up the snap
|
|
138
63
|
* provider.
|
|
139
|
-
*/ constructor({ documentUrl,
|
|
64
|
+
*/ constructor({ documentUrl, messenger, setupSnapProvider }){
|
|
140
65
|
super({
|
|
141
66
|
messenger,
|
|
142
|
-
setupSnapProvider
|
|
67
|
+
setupSnapProvider,
|
|
68
|
+
stream: new _postmessagestream.BrowserRuntimePostMessageStream({
|
|
69
|
+
name: 'parent',
|
|
70
|
+
target: 'child'
|
|
71
|
+
})
|
|
143
72
|
});
|
|
73
|
+
/**
|
|
74
|
+
* Creates the offscreen document to be used as the execution environment.
|
|
75
|
+
*
|
|
76
|
+
* If the document already exists, this does nothing.
|
|
77
|
+
*/ _class_private_method_init(this, _createDocument);
|
|
144
78
|
_define_property(this, "documentUrl", void 0);
|
|
145
|
-
_define_property(this, "frameUrl", void 0);
|
|
146
|
-
_class_private_field_init(this, _runtimeStream, {
|
|
147
|
-
writable: true,
|
|
148
|
-
value: void 0
|
|
149
|
-
});
|
|
150
79
|
this.documentUrl = documentUrl;
|
|
151
|
-
this.frameUrl = frameUrl;
|
|
152
|
-
_class_private_field_set(this, _runtimeStream, new _postmessagestream.BrowserRuntimePostMessageStream({
|
|
153
|
-
name: 'parent',
|
|
154
|
-
target: 'child'
|
|
155
|
-
}));
|
|
156
80
|
}
|
|
157
81
|
}
|
|
82
|
+
async function createDocument() {
|
|
83
|
+
// Extensions can only have a single offscreen document.
|
|
84
|
+
if (await chrome.offscreen.hasDocument()) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
await chrome.offscreen.createDocument({
|
|
88
|
+
justification: 'MetaMask Snaps Execution Environment',
|
|
89
|
+
reasons: [
|
|
90
|
+
'IFRAME_SCRIPTING'
|
|
91
|
+
],
|
|
92
|
+
url: this.documentUrl.toString()
|
|
93
|
+
});
|
|
94
|
+
}
|
|
158
95
|
|
|
159
96
|
//# sourceMappingURL=OffscreenExecutionService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/services/offscreen/OffscreenExecutionService.ts"],"sourcesContent":["import { BrowserRuntimePostMessageStream } from '@metamask/post-message-stream';\
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/offscreen/OffscreenExecutionService.ts"],"sourcesContent":["import { BrowserRuntimePostMessageStream } from '@metamask/post-message-stream';\n\nimport type { ExecutionServiceArgs } from '../AbstractExecutionService';\nimport { ProxyExecutionService } from '../proxy/ProxyExecutionService';\n\ntype OffscreenExecutionEnvironmentServiceArgs = {\n documentUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class OffscreenExecutionService extends ProxyExecutionService {\n public readonly documentUrl: URL;\n\n /**\n * Create a new offscreen execution service.\n *\n * @param args - The constructor arguments.\n * @param args.documentUrl - The URL of the offscreen document to use as the\n * execution environment. This must be a URL relative to the location where\n * this is called. This cannot be a public (http(s)) URL.\n * @param args.messenger - The messenger to use for communication with the\n * `SnapController`.\n * @param args.setupSnapProvider - The function to use to set up the snap\n * provider.\n */\n constructor({\n documentUrl,\n messenger,\n setupSnapProvider,\n }: OffscreenExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n stream: new BrowserRuntimePostMessageStream({\n name: 'parent',\n target: 'child',\n }),\n });\n\n this.documentUrl = documentUrl;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n */\n protected async initEnvStream(jobId: string) {\n // Lazily create the offscreen document.\n await this.#createDocument();\n\n return super.initEnvStream(jobId);\n }\n\n /**\n * Creates the offscreen document to be used as the execution environment.\n *\n * If the document already exists, this does nothing.\n */\n async #createDocument() {\n // Extensions can only have a single offscreen document.\n if (await chrome.offscreen.hasDocument()) {\n return;\n }\n\n await chrome.offscreen.createDocument({\n justification: 'MetaMask Snaps Execution Environment',\n reasons: ['IFRAME_SCRIPTING' as chrome.offscreen.Reason],\n url: this.documentUrl.toString(),\n });\n }\n}\n"],"names":["OffscreenExecutionService","ProxyExecutionService","initEnvStream","jobId","createDocument","constructor","documentUrl","messenger","setupSnapProvider","stream","BrowserRuntimePostMessageStream","name","target","chrome","offscreen","hasDocument","justification","reasons","url","toString"],"mappings":";;;;+BASaA;;;eAAAA;;;mCATmC;uCAGV;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAwD9B;AAlDD,MAAMA,kCAAkCC,4CAAqB;IAgClE;;;;;GAKC,GACD,MAAgBC,cAAcC,KAAa,EAAE;QAC3C,wCAAwC;QACxC,MAAM,0BAAA,IAAI,EAAEC,iBAAAA,qBAAN,IAAI;QAEV,OAAO,KAAK,CAACF,cAAcC;IAC7B;IAxCA;;;;;;;;;;;GAWC,GACDE,YAAY,EACVC,WAAW,EACXC,SAAS,EACTC,iBAAiB,EACwB,CAAE;QAC3C,KAAK,CAAC;YACJD;YACAC;YACAC,QAAQ,IAAIC,kDAA+B,CAAC;gBAC1CC,MAAM;gBACNC,QAAQ;YACV;QACF;QAkBF;;;;GAIC,GACD,iCAAM;QAjDN,uBAAgBN,eAAhB,KAAA;QA4BE,IAAI,CAACA,WAAW,GAAGA;IACrB;AAgCF;AAZE,eAAA;IACE,wDAAwD;IACxD,IAAI,MAAMO,OAAOC,SAAS,CAACC,WAAW,IAAI;QACxC;IACF;IAEA,MAAMF,OAAOC,SAAS,CAACV,cAAc,CAAC;QACpCY,eAAe;QACfC,SAAS;YAAC;SAA8C;QACxDC,KAAK,IAAI,CAACZ,WAAW,CAACa,QAAQ;IAChC;AACF"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "ProxyExecutionService", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return ProxyExecutionService;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _nanoid = require("nanoid");
|
|
12
|
+
const _AbstractExecutionService = require("../AbstractExecutionService");
|
|
13
|
+
const _ProxyPostMessageStream = require("../ProxyPostMessageStream");
|
|
14
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
15
|
+
if (privateCollection.has(obj)) {
|
|
16
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
20
|
+
if (descriptor.get) {
|
|
21
|
+
return descriptor.get.call(receiver);
|
|
22
|
+
}
|
|
23
|
+
return descriptor.value;
|
|
24
|
+
}
|
|
25
|
+
function _class_apply_descriptor_set(receiver, descriptor, value) {
|
|
26
|
+
if (descriptor.set) {
|
|
27
|
+
descriptor.set.call(receiver, value);
|
|
28
|
+
} else {
|
|
29
|
+
if (!descriptor.writable) {
|
|
30
|
+
throw new TypeError("attempted to set read only private field");
|
|
31
|
+
}
|
|
32
|
+
descriptor.value = value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
36
|
+
if (!privateMap.has(receiver)) {
|
|
37
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
38
|
+
}
|
|
39
|
+
return privateMap.get(receiver);
|
|
40
|
+
}
|
|
41
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
42
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
43
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
44
|
+
}
|
|
45
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
46
|
+
_check_private_redeclaration(obj, privateMap);
|
|
47
|
+
privateMap.set(obj, value);
|
|
48
|
+
}
|
|
49
|
+
function _class_private_field_set(receiver, privateMap, value) {
|
|
50
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
51
|
+
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
var _stream = /*#__PURE__*/ new WeakMap();
|
|
55
|
+
class ProxyExecutionService extends _AbstractExecutionService.AbstractExecutionService {
|
|
56
|
+
/**
|
|
57
|
+
* Send a termination command to the proxy stream.
|
|
58
|
+
*
|
|
59
|
+
* @param job - The job to terminate.
|
|
60
|
+
*/ async terminateJob(job) {
|
|
61
|
+
// The `AbstractExecutionService` will have already closed the job stream,
|
|
62
|
+
// so we write to the runtime stream directly.
|
|
63
|
+
_class_private_field_get(this, _stream).write({
|
|
64
|
+
jobId: job.id,
|
|
65
|
+
data: {
|
|
66
|
+
jsonrpc: '2.0',
|
|
67
|
+
method: 'terminateJob',
|
|
68
|
+
id: (0, _nanoid.nanoid)()
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Create a new stream for the specified job. This wraps the root stream
|
|
74
|
+
* in a stream specific to the job.
|
|
75
|
+
*
|
|
76
|
+
* @param jobId - The job ID.
|
|
77
|
+
*/ async initEnvStream(jobId) {
|
|
78
|
+
const stream = new _ProxyPostMessageStream.ProxyPostMessageStream({
|
|
79
|
+
stream: _class_private_field_get(this, _stream),
|
|
80
|
+
jobId
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
worker: jobId,
|
|
84
|
+
stream
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a new proxy execution service.
|
|
89
|
+
*
|
|
90
|
+
* @param args - The constructor arguments.
|
|
91
|
+
* @param args.messenger - The messenger to use for communication with the
|
|
92
|
+
* `SnapController`.
|
|
93
|
+
* @param args.setupSnapProvider - The function to use to set up the snap
|
|
94
|
+
* provider.
|
|
95
|
+
* @param args.stream - The stream to use for communicating with the proxy
|
|
96
|
+
* executor.
|
|
97
|
+
*/ constructor({ stream, messenger, setupSnapProvider }){
|
|
98
|
+
super({
|
|
99
|
+
messenger,
|
|
100
|
+
setupSnapProvider
|
|
101
|
+
});
|
|
102
|
+
_class_private_field_init(this, _stream, {
|
|
103
|
+
writable: true,
|
|
104
|
+
value: void 0
|
|
105
|
+
});
|
|
106
|
+
_class_private_field_set(this, _stream, stream);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=ProxyExecutionService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/proxy/ProxyExecutionService.ts"],"sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { nanoid } from 'nanoid';\n\nimport type { ExecutionServiceArgs, Job } from '../AbstractExecutionService';\nimport { AbstractExecutionService } from '../AbstractExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype ProxyExecutionEnvironmentServiceArgs = {\n stream: BasePostMessageStream;\n} & ExecutionServiceArgs;\n\nexport class ProxyExecutionService extends AbstractExecutionService<string> {\n readonly #stream: BasePostMessageStream;\n\n /**\n * Create a new proxy execution service.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger to use for communication with the\n * `SnapController`.\n * @param args.setupSnapProvider - The function to use to set up the snap\n * provider.\n * @param args.stream - The stream to use for communicating with the proxy\n * executor.\n */\n constructor({\n stream,\n messenger,\n setupSnapProvider,\n }: ProxyExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n\n this.#stream = stream;\n }\n\n /**\n * Send a termination command to the proxy stream.\n *\n * @param job - The job to terminate.\n */\n protected async terminateJob(job: Job<string>) {\n // The `AbstractExecutionService` will have already closed the job stream,\n // so we write to the runtime stream directly.\n this.#stream.write({\n jobId: job.id,\n data: {\n jsonrpc: '2.0',\n method: 'terminateJob',\n id: nanoid(),\n },\n });\n }\n\n /**\n * Create a new stream for the specified job. This wraps the root stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n */\n protected async initEnvStream(jobId: string) {\n const stream = new ProxyPostMessageStream({\n stream: this.#stream,\n jobId,\n });\n\n return { worker: jobId, stream };\n }\n}\n"],"names":["ProxyExecutionService","AbstractExecutionService","terminateJob","job","stream","write","jobId","id","data","jsonrpc","method","nanoid","initEnvStream","ProxyPostMessageStream","worker","constructor","messenger","setupSnapProvider"],"mappings":";;;;+BAWaA;;;eAAAA;;;wBAVU;0CAGkB;wCACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAO5B;AADJ,MAAMA,8BAA8BC,kDAAwB;IA2BjE;;;;GAIC,GACD,MAAgBC,aAAaC,GAAgB,EAAE;QAC7C,0EAA0E;QAC1E,8CAA8C;QAC9C,yBAAA,IAAI,EAAEC,SAAOC,KAAK,CAAC;YACjBC,OAAOH,IAAII,EAAE;YACbC,MAAM;gBACJC,SAAS;gBACTC,QAAQ;gBACRH,IAAII,IAAAA,cAAM;YACZ;QACF;IACF;IAEA;;;;;GAKC,GACD,MAAgBC,cAAcN,KAAa,EAAE;QAC3C,MAAMF,SAAS,IAAIS,8CAAsB,CAAC;YACxCT,MAAM,2BAAE,IAAI,EAAEA;YACdE;QACF;QAEA,OAAO;YAAEQ,QAAQR;YAAOF;QAAO;IACjC;IAvDA;;;;;;;;;;GAUC,GACDW,YAAY,EACVX,MAAM,EACNY,SAAS,EACTC,iBAAiB,EACoB,CAAE;QACvC,KAAK,CAAC;YACJD;YACAC;QACF;QArBF,gCAAS;;mBAAT,KAAA;;uCAuBQb,SAASA;IACjB;AAkCF"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "WebViewExecutionService", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return WebViewExecutionService;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _ProxyExecutionService = require("../proxy/ProxyExecutionService");
|
|
12
|
+
const _WebViewMessageStream = require("./WebViewMessageStream");
|
|
13
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
14
|
+
if (privateCollection.has(obj)) {
|
|
15
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
19
|
+
if (descriptor.get) {
|
|
20
|
+
return descriptor.get.call(receiver);
|
|
21
|
+
}
|
|
22
|
+
return descriptor.value;
|
|
23
|
+
}
|
|
24
|
+
function _class_apply_descriptor_set(receiver, descriptor, value) {
|
|
25
|
+
if (descriptor.set) {
|
|
26
|
+
descriptor.set.call(receiver, value);
|
|
27
|
+
} else {
|
|
28
|
+
if (!descriptor.writable) {
|
|
29
|
+
throw new TypeError("attempted to set read only private field");
|
|
30
|
+
}
|
|
31
|
+
descriptor.value = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
35
|
+
if (!privateMap.has(receiver)) {
|
|
36
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
37
|
+
}
|
|
38
|
+
return privateMap.get(receiver);
|
|
39
|
+
}
|
|
40
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
41
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
42
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
43
|
+
}
|
|
44
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
45
|
+
_check_private_redeclaration(obj, privateMap);
|
|
46
|
+
privateMap.set(obj, value);
|
|
47
|
+
}
|
|
48
|
+
function _class_private_field_set(receiver, privateMap, value) {
|
|
49
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
50
|
+
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
function _class_private_method_get(receiver, privateSet, fn) {
|
|
54
|
+
if (!privateSet.has(receiver)) {
|
|
55
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
56
|
+
}
|
|
57
|
+
return fn;
|
|
58
|
+
}
|
|
59
|
+
function _class_private_method_init(obj, privateSet) {
|
|
60
|
+
_check_private_redeclaration(obj, privateSet);
|
|
61
|
+
privateSet.add(obj);
|
|
62
|
+
}
|
|
63
|
+
var _getWebView = /*#__PURE__*/ new WeakMap(), _ensureWebViewLoaded = /*#__PURE__*/ new WeakSet();
|
|
64
|
+
class WebViewExecutionService extends _ProxyExecutionService.ProxyExecutionService {
|
|
65
|
+
/**
|
|
66
|
+
* Create a new stream for the specified job. This wraps the runtime stream
|
|
67
|
+
* in a stream specific to the job.
|
|
68
|
+
*
|
|
69
|
+
* @param jobId - The job ID.
|
|
70
|
+
*/ async initEnvStream(jobId) {
|
|
71
|
+
// Ensure that the WebView has been loaded before we proceed.
|
|
72
|
+
await _class_private_method_get(this, _ensureWebViewLoaded, ensureWebViewLoaded).call(this);
|
|
73
|
+
return super.initEnvStream(jobId);
|
|
74
|
+
}
|
|
75
|
+
constructor({ messenger, setupSnapProvider, getWebView }){
|
|
76
|
+
super({
|
|
77
|
+
messenger,
|
|
78
|
+
setupSnapProvider,
|
|
79
|
+
stream: new _WebViewMessageStream.WebViewMessageStream({
|
|
80
|
+
name: 'parent',
|
|
81
|
+
target: 'child',
|
|
82
|
+
getWebView
|
|
83
|
+
})
|
|
84
|
+
});
|
|
85
|
+
/**
|
|
86
|
+
* Ensure that the WebView has been loaded by awaiting the getWebView promise.
|
|
87
|
+
*/ _class_private_method_init(this, _ensureWebViewLoaded);
|
|
88
|
+
_class_private_field_init(this, _getWebView, {
|
|
89
|
+
writable: true,
|
|
90
|
+
value: void 0
|
|
91
|
+
});
|
|
92
|
+
_class_private_field_set(this, _getWebView, getWebView);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function ensureWebViewLoaded() {
|
|
96
|
+
await _class_private_field_get(this, _getWebView).call(this);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=WebViewExecutionService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/webview/WebViewExecutionService.ts"],"sourcesContent":["import type { ExecutionServiceArgs } from '../AbstractExecutionService';\nimport { ProxyExecutionService } from '../proxy/ProxyExecutionService';\nimport type { WebViewInterface } from './WebViewMessageStream';\nimport { WebViewMessageStream } from './WebViewMessageStream';\n\nexport type WebViewExecutionServiceArgs = ExecutionServiceArgs & {\n getWebView: () => Promise<WebViewInterface>;\n};\n\nexport class WebViewExecutionService extends ProxyExecutionService {\n #getWebView;\n\n constructor({\n messenger,\n setupSnapProvider,\n getWebView,\n }: WebViewExecutionServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n stream: new WebViewMessageStream({\n name: 'parent',\n target: 'child',\n getWebView,\n }),\n });\n this.#getWebView = getWebView;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n */\n protected async initEnvStream(jobId: string) {\n // Ensure that the WebView has been loaded before we proceed.\n await this.#ensureWebViewLoaded();\n\n return super.initEnvStream(jobId);\n }\n\n /**\n * Ensure that the WebView has been loaded by awaiting the getWebView promise.\n */\n async #ensureWebViewLoaded() {\n await this.#getWebView();\n }\n}\n"],"names":["WebViewExecutionService","ProxyExecutionService","initEnvStream","jobId","ensureWebViewLoaded","constructor","messenger","setupSnapProvider","getWebView","stream","WebViewMessageStream","name","target"],"mappings":";;;;+BASaA;;;eAAAA;;;uCARyB;sCAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAOnC,2CAmCM;AApCD,MAAMA,gCAAgCC,4CAAqB;IAoBhE;;;;;GAKC,GACD,MAAgBC,cAAcC,KAAa,EAAE;QAC3C,6DAA6D;QAC7D,MAAM,0BAAA,IAAI,EAAEC,sBAAAA,0BAAN,IAAI;QAEV,OAAO,KAAK,CAACF,cAAcC;IAC7B;IA5BAE,YAAY,EACVC,SAAS,EACTC,iBAAiB,EACjBC,UAAU,EACkB,CAAE;QAC9B,KAAK,CAAC;YACJF;YACAC;YACAE,QAAQ,IAAIC,0CAAoB,CAAC;gBAC/BC,MAAM;gBACNC,QAAQ;gBACRJ;YACF;QACF;QAiBF;;GAEC,GACD,iCAAM;QAnCN,gCAAA;;mBAAA,KAAA;;uCAgBQA,aAAaA;IACrB;AAqBF;AAHE,eAAA;IACE,MAAM,yBAAA,IAAI,EAAEA,kBAAN,IAAI;AACZ"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "WebViewMessageStream", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return WebViewMessageStream;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _postmessagestream = require("@metamask/post-message-stream");
|
|
12
|
+
const _utils = require("@metamask/post-message-stream/dist/utils");
|
|
13
|
+
const _snapsutils = require("@metamask/snaps-utils");
|
|
14
|
+
const _utils1 = require("@metamask/utils");
|
|
15
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
16
|
+
if (privateCollection.has(obj)) {
|
|
17
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
21
|
+
if (descriptor.get) {
|
|
22
|
+
return descriptor.get.call(receiver);
|
|
23
|
+
}
|
|
24
|
+
return descriptor.value;
|
|
25
|
+
}
|
|
26
|
+
function _class_apply_descriptor_set(receiver, descriptor, value) {
|
|
27
|
+
if (descriptor.set) {
|
|
28
|
+
descriptor.set.call(receiver, value);
|
|
29
|
+
} else {
|
|
30
|
+
if (!descriptor.writable) {
|
|
31
|
+
throw new TypeError("attempted to set read only private field");
|
|
32
|
+
}
|
|
33
|
+
descriptor.value = value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
37
|
+
if (!privateMap.has(receiver)) {
|
|
38
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
39
|
+
}
|
|
40
|
+
return privateMap.get(receiver);
|
|
41
|
+
}
|
|
42
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
43
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
44
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
45
|
+
}
|
|
46
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
47
|
+
_check_private_redeclaration(obj, privateMap);
|
|
48
|
+
privateMap.set(obj, value);
|
|
49
|
+
}
|
|
50
|
+
function _class_private_field_set(receiver, privateMap, value) {
|
|
51
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
52
|
+
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
var _name = /*#__PURE__*/ new WeakMap(), _target = /*#__PURE__*/ new WeakMap(), _webView = /*#__PURE__*/ new WeakMap();
|
|
56
|
+
class WebViewMessageStream extends _postmessagestream.BasePostMessageStream {
|
|
57
|
+
_postMessage(data) {
|
|
58
|
+
(0, _utils1.assert)(_class_private_field_get(this, _webView));
|
|
59
|
+
const json = JSON.stringify({
|
|
60
|
+
target: _class_private_field_get(this, _target),
|
|
61
|
+
data
|
|
62
|
+
});
|
|
63
|
+
// To prevent XSS, we base64 encode the message before injecting it.
|
|
64
|
+
// This adds significant performance overhead.
|
|
65
|
+
// TODO: Should we use mobile native base64 here?
|
|
66
|
+
const bytes = (0, _utils1.stringToBytes)(json);
|
|
67
|
+
const base64 = (0, _utils1.bytesToBase64)(bytes);
|
|
68
|
+
_class_private_field_get(this, _webView).injectJavaScript(`window.postMessage('${base64}')`);
|
|
69
|
+
}
|
|
70
|
+
_onMessage(event) {
|
|
71
|
+
if (typeof event.data !== 'string') {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const message = JSON.parse(event.data);
|
|
75
|
+
// Notice that we don't check targetWindow or targetOrigin here.
|
|
76
|
+
// This doesn't seem possible to do in RN.
|
|
77
|
+
if (!(0, _utils.isValidStreamMessage)(message) || message.target !== _class_private_field_get(this, _name)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this._onData(message.data);
|
|
81
|
+
}
|
|
82
|
+
_destroy() {
|
|
83
|
+
(0, _utils1.assert)(_class_private_field_get(this, _webView));
|
|
84
|
+
// This method is already bound.
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
86
|
+
_class_private_field_get(this, _webView).unregisterMessageListener(this._onMessage);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Creates a stream for communicating with other streams inside a WebView.
|
|
90
|
+
*
|
|
91
|
+
* @param args - Options bag.
|
|
92
|
+
* @param args.name - The name of the stream. Used to differentiate between
|
|
93
|
+
* multiple streams sharing the same window object.
|
|
94
|
+
* @param args.target - The name of the stream to exchange messages with.
|
|
95
|
+
* @param args.getWebView - A asynchronous getter for the webview.
|
|
96
|
+
*/ constructor({ name, target, getWebView }){
|
|
97
|
+
super();
|
|
98
|
+
_class_private_field_init(this, _name, {
|
|
99
|
+
writable: true,
|
|
100
|
+
value: void 0
|
|
101
|
+
});
|
|
102
|
+
_class_private_field_init(this, _target, {
|
|
103
|
+
writable: true,
|
|
104
|
+
value: void 0
|
|
105
|
+
});
|
|
106
|
+
_class_private_field_init(this, _webView, {
|
|
107
|
+
writable: true,
|
|
108
|
+
value: void 0
|
|
109
|
+
});
|
|
110
|
+
_class_private_field_set(this, _name, name);
|
|
111
|
+
_class_private_field_set(this, _target, target);
|
|
112
|
+
this._onMessage = this._onMessage.bind(this);
|
|
113
|
+
// This is a bit atypical from other post-message streams.
|
|
114
|
+
// We have to wait for the WebView to fully load before we can continue using the stream.
|
|
115
|
+
getWebView().then((webView)=>{
|
|
116
|
+
_class_private_field_set(this, _webView, webView);
|
|
117
|
+
// This method is already bound.
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
119
|
+
webView.registerMessageListener(this._onMessage);
|
|
120
|
+
this._handshake();
|
|
121
|
+
}).catch((error)=>{
|
|
122
|
+
(0, _snapsutils.logError)(error);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//# sourceMappingURL=WebViewMessageStream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/webview/WebViewMessageStream.ts"],"sourcesContent":["import type { PostMessageEvent } from '@metamask/post-message-stream';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { isValidStreamMessage } from '@metamask/post-message-stream/dist/utils';\nimport { logError } from '@metamask/snaps-utils';\nimport { assert, bytesToBase64, stringToBytes } from '@metamask/utils';\n\nexport type WebViewInterface = {\n injectJavaScript(js: string): void;\n registerMessageListener(listener: (event: PostMessageEvent) => void): void;\n unregisterMessageListener(listener: (event: PostMessageEvent) => void): void;\n};\n\ntype WebViewStreamArgs = {\n name: string;\n target: string;\n getWebView: () => Promise<WebViewInterface>;\n};\n\n/**\n * A special postMessage stream used to interface with a WebView.\n */\n\nexport class WebViewMessageStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #webView: WebViewInterface | undefined;\n\n /**\n * Creates a stream for communicating with other streams inside a WebView.\n *\n * @param args - Options bag.\n * @param args.name - The name of the stream. Used to differentiate between\n * multiple streams sharing the same window object.\n * @param args.target - The name of the stream to exchange messages with.\n * @param args.getWebView - A asynchronous getter for the webview.\n */\n constructor({ name, target, getWebView }: WebViewStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n\n this._onMessage = this._onMessage.bind(this);\n\n // This is a bit atypical from other post-message streams.\n // We have to wait for the WebView to fully load before we can continue using the stream.\n getWebView()\n .then((webView) => {\n this.#webView = webView;\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n webView.registerMessageListener(this._onMessage);\n this._handshake();\n })\n .catch((error) => {\n logError(error);\n });\n }\n\n protected _postMessage(data: unknown): void {\n assert(this.#webView);\n const json = JSON.stringify({\n target: this.#target,\n data,\n });\n\n // To prevent XSS, we base64 encode the message before injecting it.\n // This adds significant performance overhead.\n // TODO: Should we use mobile native base64 here?\n const bytes = stringToBytes(json);\n const base64 = bytesToBase64(bytes);\n this.#webView.injectJavaScript(`window.postMessage('${base64}')`);\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const message = JSON.parse(event.data);\n\n // Notice that we don't check targetWindow or targetOrigin here.\n // This doesn't seem possible to do in RN.\n if (!isValidStreamMessage(message) || message.target !== this.#name) {\n return;\n }\n\n this._onData(message.data);\n }\n\n _destroy() {\n assert(this.#webView);\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.unregisterMessageListener(this._onMessage);\n }\n}\n"],"names":["WebViewMessageStream","BasePostMessageStream","_postMessage","data","assert","webView","json","JSON","stringify","target","bytes","stringToBytes","base64","bytesToBase64","injectJavaScript","_onMessage","event","message","parse","isValidStreamMessage","name","_onData","_destroy","unregisterMessageListener","constructor","getWebView","bind","then","registerMessageListener","_handshake","catch","error","logError"],"mappings":";;;;+BAsBaA;;;eAAAA;;;mCArByB;uBACD;4BACZ;wBAC4B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAmBnD,qCAEA,uCAEA;AALK,MAAMA,6BAA6BC,wCAAqB;IAuCnDC,aAAaC,IAAa,EAAQ;QAC1CC,IAAAA,cAAM,2BAAC,IAAI,EAAEC;QACb,MAAMC,OAAOC,KAAKC,SAAS,CAAC;YAC1BC,MAAM,2BAAE,IAAI,EAAEA;YACdN;QACF;QAEA,oEAAoE;QACpE,8CAA8C;QAC9C,iDAAiD;QACjD,MAAMO,QAAQC,IAAAA,qBAAa,EAACL;QAC5B,MAAMM,SAASC,IAAAA,qBAAa,EAACH;QAC7B,yBAAA,IAAI,EAAEL,UAAQS,gBAAgB,CAAC,CAAC,oBAAoB,EAAEF,OAAO,EAAE,CAAC;IAClE;IAEQG,WAAWC,KAAuB,EAAQ;QAChD,IAAI,OAAOA,MAAMb,IAAI,KAAK,UAAU;YAClC;QACF;QAEA,MAAMc,UAAUV,KAAKW,KAAK,CAACF,MAAMb,IAAI;QAErC,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAACgB,IAAAA,2BAAoB,EAACF,YAAYA,QAAQR,MAAM,8BAAK,IAAI,EAAEW,QAAM;YACnE;QACF;QAEA,IAAI,CAACC,OAAO,CAACJ,QAAQd,IAAI;IAC3B;IAEAmB,WAAW;QACTlB,IAAAA,cAAM,2BAAC,IAAI,EAAEC;QACb,gCAAgC;QAChC,6DAA6D;QAC7D,yBAAA,IAAI,EAAEA,UAAQkB,yBAAyB,CAAC,IAAI,CAACR,UAAU;IACzD;IApEA;;;;;;;;GAQC,GACDS,YAAY,EAAEJ,IAAI,EAAEX,MAAM,EAAEgB,UAAU,EAAqB,CAAE;QAC3D,KAAK;QAhBP,gCAAA;;mBAAA,KAAA;;QAEA,gCAAA;;mBAAA,KAAA;;QAEA,gCAAA;;mBAAA,KAAA;;uCAcQL,OAAOA;uCACPX,SAASA;QAEf,IAAI,CAACM,UAAU,GAAG,IAAI,CAACA,UAAU,CAACW,IAAI,CAAC,IAAI;QAE3C,0DAA0D;QAC1D,yFAAyF;QACzFD,aACGE,IAAI,CAAC,CAACtB;2CACCA,UAAUA;YAChB,gCAAgC;YAChC,6DAA6D;YAC7DA,QAAQuB,uBAAuB,CAAC,IAAI,CAACb,UAAU;YAC/C,IAAI,CAACc,UAAU;QACjB,GACCC,KAAK,CAAC,CAACC;YACNC,IAAAA,oBAAQ,EAACD;QACX;IACJ;AAuCF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./WebViewExecutionService"), exports);
|
|
6
|
+
function _export_star(from, to) {
|
|
7
|
+
Object.keys(from).forEach(function(k) {
|
|
8
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
9
|
+
Object.defineProperty(to, k, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function() {
|
|
12
|
+
return from[k];
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return from;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/services/webview/index.ts"],"sourcesContent":["export * from './WebViewExecutionService';\n"],"names":[],"mappings":";;;;qBAAc"}
|