@metamask/snaps-controllers 0.27.1 → 0.29.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.
Files changed (58) hide show
  1. package/dist/cronjob/CronjobController.js +3 -3
  2. package/dist/cronjob/CronjobController.js.map +1 -1
  3. package/dist/logging.d.ts +8 -0
  4. package/dist/logging.js +13 -0
  5. package/dist/logging.js.map +1 -0
  6. package/dist/multichain/MultiChainController.js +11 -12
  7. package/dist/multichain/MultiChainController.js.map +1 -1
  8. package/dist/services/AbstractExecutionService.js +10 -9
  9. package/dist/services/AbstractExecutionService.js.map +1 -1
  10. package/dist/services/browser.d.ts +1 -0
  11. package/dist/services/browser.js +1 -0
  12. package/dist/services/browser.js.map +1 -1
  13. package/dist/services/iframe/IframeExecutionService.d.ts +0 -10
  14. package/dist/services/iframe/IframeExecutionService.js +2 -53
  15. package/dist/services/iframe/IframeExecutionService.js.map +1 -1
  16. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js +0 -40
  17. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js.map +1 -1
  18. package/dist/services/index.d.ts +1 -0
  19. package/dist/services/index.js +1 -0
  20. package/dist/services/index.js.map +1 -1
  21. package/dist/services/offscreen/OffscreenExecutionService.d.ts +49 -0
  22. package/dist/services/offscreen/OffscreenExecutionService.js +100 -0
  23. package/dist/services/offscreen/OffscreenExecutionService.js.map +1 -0
  24. package/dist/services/offscreen/OffscreenPostMessageStream.d.ts +35 -0
  25. package/dist/services/offscreen/OffscreenPostMessageStream.js +66 -0
  26. package/dist/services/offscreen/OffscreenPostMessageStream.js.map +1 -0
  27. package/dist/services/offscreen/index.d.ts +2 -0
  28. package/dist/services/offscreen/index.js +19 -0
  29. package/dist/services/offscreen/index.js.map +1 -0
  30. package/dist/snaps/SnapController.d.ts +30 -35
  31. package/dist/snaps/SnapController.js +109 -87
  32. package/dist/snaps/SnapController.js.map +1 -1
  33. package/dist/snaps/endowments/index.d.ts +1 -1
  34. package/dist/snaps/endowments/network-access.d.ts +1 -1
  35. package/dist/snaps/endowments/network-access.js +1 -1
  36. package/dist/snaps/endowments/network-access.js.map +1 -1
  37. package/dist/snaps/index.d.ts +1 -0
  38. package/dist/snaps/index.js +1 -0
  39. package/dist/snaps/index.js.map +1 -1
  40. package/dist/snaps/location/location.d.ts +4 -0
  41. package/dist/snaps/location/location.js +3 -1
  42. package/dist/snaps/location/location.js.map +1 -1
  43. package/dist/snaps/location/npm.d.ts +2 -1
  44. package/dist/snaps/location/npm.js +30 -12
  45. package/dist/snaps/location/npm.js.map +1 -1
  46. package/dist/snaps/registry/index.d.ts +2 -0
  47. package/dist/snaps/registry/index.js +19 -0
  48. package/dist/snaps/registry/index.js.map +1 -0
  49. package/dist/snaps/registry/json.d.ts +20 -0
  50. package/dist/snaps/registry/json.js +95 -0
  51. package/dist/snaps/registry/json.js.map +1 -0
  52. package/dist/snaps/registry/registry.d.ts +29 -0
  53. package/dist/snaps/registry/registry.js +11 -0
  54. package/dist/snaps/registry/registry.js.map +1 -0
  55. package/package.json +10 -9
  56. package/dist/services/iframe/test/server.d.ts +0 -11
  57. package/dist/services/iframe/test/server.js +0 -71
  58. package/dist/services/iframe/test/server.js.map +0 -1
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IframeExecutionService = void 0;
4
4
  const post_message_stream_1 = require("@metamask/post-message-stream");
5
+ const snaps_utils_1 = require("@metamask/snaps-utils");
5
6
  const AbstractExecutionService_1 = require("../AbstractExecutionService");
6
7
  class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
7
8
  constructor({ iframeUrl, messenger, setupSnapProvider, }) {
@@ -16,7 +17,7 @@ class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutio
16
17
  (_a = document.getElementById(jobWrapper.id)) === null || _a === void 0 ? void 0 : _a.remove();
17
18
  }
18
19
  async initEnvStream(jobId) {
19
- const iframeWindow = await this.createWindow(this.iframeUrl.toString(), jobId);
20
+ const iframeWindow = await (0, snaps_utils_1.createWindow)(this.iframeUrl.toString(), jobId);
20
21
  const stream = new post_message_stream_1.WindowPostMessageStream({
21
22
  name: 'parent',
22
23
  target: 'child',
@@ -25,58 +26,6 @@ class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutio
25
26
  });
26
27
  return { worker: iframeWindow, stream };
27
28
  }
28
- /**
29
- * Creates the iframe to be used as the execution environment. This may run
30
- * forever if the iframe never loads, but the promise should be wrapped in
31
- * an initialization timeout in the SnapController.
32
- *
33
- * @param uri - The iframe URI.
34
- * @param jobId - The job id.
35
- * @returns A promise that resolves to the contentWindow of the iframe.
36
- */
37
- async createWindow(uri, jobId) {
38
- return new Promise((resolve, reject) => {
39
- const iframe = document.createElement('iframe');
40
- // The order of operations appears to matter for everything except this
41
- // attribute. We may as well set it here.
42
- iframe.setAttribute('id', jobId);
43
- // In the past, we've had problems that appear to be symptomatic of the
44
- // iframe firing the `load` event before its scripts are actually loaded,
45
- // which has prevented snaps from executing properly. Therefore, we set
46
- // the `src` attribute and append the iframe to the DOM before attaching
47
- // the `load` listener.
48
- //
49
- // `load` should only fire when "all dependent resources" have been
50
- // loaded, which includes scripts.
51
- //
52
- // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
53
- // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489
54
- iframe.setAttribute('src', uri);
55
- document.body.appendChild(iframe);
56
- iframe.addEventListener('load', () => {
57
- if (iframe.contentWindow) {
58
- resolve(iframe.contentWindow);
59
- }
60
- else {
61
- // We don't know of a case when this would happen, but better to fail
62
- // fast if it does.
63
- reject(new Error(`iframe.contentWindow not present on load for job "${jobId}".`));
64
- }
65
- });
66
- // We need to set the sandbox attribute after appending the iframe to the
67
- // DOM, otherwise errors in the iframe will not be propagated via `error`
68
- // and `unhandledrejection` events, and we cannot catch and handle them.
69
- // We wish we knew why this was the case.
70
- //
71
- // We set this property after adding the `load` listener because it
72
- // appears to work dependably. ¯\_(ツ)_/¯
73
- //
74
- // We apply this property as a principle of least authority (POLA)
75
- // measure.
76
- // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox
77
- iframe.setAttribute('sandbox', 'allow-scripts');
78
- });
79
- }
80
29
  }
81
30
  exports.IframeExecutionService = IframeExecutionService;
82
31
  //# sourceMappingURL=IframeExecutionService.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"IframeExecutionService.js","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;AAEvC,0EAIqC;AAMrC,MAAa,sBAAuB,SAAQ,mDAAgC;IAG1E,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,GACqB;QACtC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAuB;;QAC5C,MAAA,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,0CAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,KAAa;QAIzC,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,YAAY,CAC1C,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,EACzB,KAAK,CACN,CAAC;QACF,MAAM,MAAM,GAAG,IAAI,6CAAuB,CAAC;YACzC,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,YAAY;YAC1B,YAAY,EAAE,GAAG;SAClB,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC;IAC1C,CAAC;IAED;;;;;;;;OAQG;IACK,KAAK,CAAC,YAAY,CAAC,GAAW,EAAE,KAAa;QACnD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;YAChD,uEAAuE;YACvE,yCAAyC;YACzC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAEjC,uEAAuE;YACvE,yEAAyE;YACzE,uEAAuE;YACvE,wEAAwE;YACxE,uBAAuB;YACvB,EAAE;YACF,mEAAmE;YACnE,kCAAkC;YAClC,EAAE;YACF,mGAAmG;YACnG,iJAAiJ;YACjJ,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;YAChC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;YAElC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACnC,IAAI,MAAM,CAAC,aAAa,EAAE;oBACxB,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;iBAC/B;qBAAM;oBACL,qEAAqE;oBACrE,mBAAmB;oBACnB,MAAM,CACJ,IAAI,KAAK,CACP,qDAAqD,KAAK,IAAI,CAC/D,CACF,CAAC;iBACH;YACH,CAAC,CAAC,CAAC;YAEH,yEAAyE;YACzE,yEAAyE;YACzE,wEAAwE;YACxE,yCAAyC;YACzC,EAAE;YACF,mEAAmE;YACnE,wCAAwC;YACxC,EAAE;YACF,kEAAkE;YAClE,WAAW;YACX,qFAAqF;YACrF,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA/FD,wDA+FC","sourcesContent":["import {\n WindowPostMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\n\nimport {\n Job,\n AbstractExecutionService,\n ExecutionServiceArgs,\n} from '../AbstractExecutionService';\n\ntype IframeExecutionEnvironmentServiceArgs = {\n iframeUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class IframeExecutionService extends AbstractExecutionService<Window> {\n public iframeUrl: URL;\n\n constructor({\n iframeUrl,\n messenger,\n setupSnapProvider,\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: Job<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(jobId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await this.createWindow(\n this.iframeUrl.toString(),\n jobId,\n );\n const stream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: iframeWindow,\n targetOrigin: '*',\n });\n\n return { worker: iframeWindow, stream };\n }\n\n /**\n * Creates the iframe to be used as the execution environment. This may run\n * forever if the iframe never loads, but the promise should be wrapped in\n * an initialization timeout in the SnapController.\n *\n * @param uri - The iframe URI.\n * @param jobId - The job id.\n * @returns A promise that resolves to the contentWindow of the iframe.\n */\n private async createWindow(uri: string, jobId: string): Promise<Window> {\n return new Promise((resolve, reject) => {\n const iframe = document.createElement('iframe');\n // The order of operations appears to matter for everything except this\n // attribute. We may as well set it here.\n iframe.setAttribute('id', jobId);\n\n // In the past, we've had problems that appear to be symptomatic of the\n // iframe firing the `load` event before its scripts are actually loaded,\n // which has prevented snaps from executing properly. Therefore, we set\n // the `src` attribute and append the iframe to the DOM before attaching\n // the `load` listener.\n //\n // `load` should only fire when \"all dependent resources\" have been\n // loaded, which includes scripts.\n //\n // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event\n // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489\n iframe.setAttribute('src', uri);\n document.body.appendChild(iframe);\n\n iframe.addEventListener('load', () => {\n if (iframe.contentWindow) {\n resolve(iframe.contentWindow);\n } else {\n // We don't know of a case when this would happen, but better to fail\n // fast if it does.\n reject(\n new Error(\n `iframe.contentWindow not present on load for job \"${jobId}\".`,\n ),\n );\n }\n });\n\n // We need to set the sandbox attribute after appending the iframe to the\n // DOM, otherwise errors in the iframe will not be propagated via `error`\n // and `unhandledrejection` events, and we cannot catch and handle them.\n // We wish we knew why this was the case.\n //\n // We set this property after adding the `load` listener because it\n // appears to work dependably. ¯\\_(ツ)_/¯\n //\n // We apply this property as a principle of least authority (POLA)\n // measure.\n // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox\n iframe.setAttribute('sandbox', 'allow-scripts');\n });\n }\n}\n"]}
1
+ {"version":3,"file":"IframeExecutionService.js","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;AACvC,uDAAqD;AAErD,0EAIqC;AAMrC,MAAa,sBAAuB,SAAQ,mDAAgC;IAG1E,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,GACqB;QACtC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAuB;;QAC5C,MAAA,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,0CAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,KAAa;QAIzC,MAAM,YAAY,GAAG,MAAM,IAAA,0BAAY,EAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,EAAE,KAAK,CAAC,CAAC;QAE1E,MAAM,MAAM,GAAG,IAAI,6CAAuB,CAAC;YACzC,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,YAAY;YAC1B,YAAY,EAAE,GAAG;SAClB,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC;IAC1C,CAAC;CACF;AAlCD,wDAkCC","sourcesContent":["import {\n WindowPostMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\n\nimport {\n Job,\n AbstractExecutionService,\n ExecutionServiceArgs,\n} from '../AbstractExecutionService';\n\ntype IframeExecutionEnvironmentServiceArgs = {\n iframeUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class IframeExecutionService extends AbstractExecutionService<Window> {\n public iframeUrl: URL;\n\n constructor({\n iframeUrl,\n messenger,\n setupSnapProvider,\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: Job<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(jobId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await createWindow(this.iframeUrl.toString(), jobId);\n\n const stream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: iframeWindow,\n targetOrigin: '*',\n });\n\n return { worker: iframeWindow, stream };\n }\n}\n"]}
@@ -2,46 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  // Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745
4
4
  const fixJSDOMPostMessageEventSource = (iframeExecutionService) => {
5
- const oldCreateWindow = iframeExecutionService.createWindow;
6
- iframeExecutionService.createWindow = async (uri, envId, timeout) => {
7
- const result = await oldCreateWindow(uri, envId, timeout);
8
- const scriptElement = result.document.createElement('script');
9
- if (!scriptElement) {
10
- return result;
11
- }
12
- // fix the inside window
13
- scriptElement.textContent = `
14
- window.addEventListener('message', (postMessageEvent) => {
15
- if (postMessageEvent.source === null && !postMessageEvent.origin) {
16
- let source;
17
- let postMessageEventOrigin;
18
- if (postMessageEvent.data.target === 'child') {
19
- source = window.parent;
20
- postMessageEventOrigin = '*';
21
- } else if (postMessageEvent.data.target === 'parent') {
22
- source = window;
23
- postMessageEventOrigin = window.location.origin;
24
- }
25
- if (postMessageEvent.data.target) {
26
- postMessageEvent.stopImmediatePropagation();
27
- const args = Object.assign({
28
- ...postMessageEvent,
29
- data: postMessageEvent.data,
30
- source,
31
- origin: postMessageEventOrigin,
32
- });
33
- const postMessageEventWithOrigin = new MessageEvent(
34
- 'message',
35
- args,
36
- );
37
- window.dispatchEvent(postMessageEventWithOrigin);
38
- }
39
- }
40
- });
41
- `;
42
- result.document.body.appendChild(scriptElement);
43
- return result;
44
- };
45
5
  const listener = (event) => {
46
6
  if (event.source === null && !event.origin) {
47
7
  let source;
@@ -1 +1 @@
1
- {"version":3,"file":"fixJSDOMPostMessageEventSource.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/fixJSDOMPostMessageEventSource.ts"],"names":[],"mappings":";;AAEA,yGAAyG;AACzG,MAAM,8BAA8B,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,eAAe,GAAI,sBAA8B,CAAC,YAAY,CAAC;IACpE,sBAA8B,CAAC,YAAY,GAAG,KAAK,EAClD,GAAW,EACX,KAAa,EACb,OAAe,EACf,EAAE;QACF,MAAM,MAAM,GAAG,MAAM,eAAe,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;QAE1D,MAAM,aAAa,GAAG,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAE9D,IAAI,CAAC,aAAa,EAAE;YAClB,OAAO,MAAM,CAAC;SACf;QAED,wBAAwB;QACxB,aAAa,CAAC,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4B7B,CAAC;QACA,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC;QAEhD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,MAAM,QAAQ,GAAG,CAAC,KAAmB,EAAE,EAAE;QACvC,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;YAC1C,IAAI,MAAM,CAAC;YACX,IAAI,MAAM,CAAC;YACX,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE;gBACjC,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;aACjC;iBAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;gBACzC,8CAA8C;gBAC9C,MAAM,EAAE,MAAM,EAAE,GAAG,sBAAsB,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;gBACrE,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,sBAAsB,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;aACtD;YAED,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;gBACrB,KAAK,CAAC,wBAAwB,EAAE,CAAC;gBACjC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,iCACrB,KAAK,KACR,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,MAAM;oBACN,MAAM,IACN,CAAC;gBACH,MAAM,eAAe,GAAiB,IAAI,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBACxE,MAAM,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;aACvC;SACF;IACH,CAAC,CAAC;IAEF,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAE7C,OAAO,GAAG,EAAE;QACV,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,8BAA8B,CAAC","sourcesContent":["import { IframeExecutionService } from '../IframeExecutionService';\n\n// Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745\nconst fixJSDOMPostMessageEventSource = (\n iframeExecutionService: IframeExecutionService,\n) => {\n const oldCreateWindow = (iframeExecutionService as any).createWindow;\n (iframeExecutionService as any).createWindow = async (\n uri: string,\n envId: string,\n timeout: number,\n ) => {\n const result = await oldCreateWindow(uri, envId, timeout);\n\n const scriptElement = result.document.createElement('script');\n\n if (!scriptElement) {\n return result;\n }\n\n // fix the inside window\n scriptElement.textContent = `\n window.addEventListener('message', (postMessageEvent) => {\n if (postMessageEvent.source === null && !postMessageEvent.origin) {\n let source;\n let postMessageEventOrigin;\n if (postMessageEvent.data.target === 'child') {\n source = window.parent;\n postMessageEventOrigin = '*';\n } else if (postMessageEvent.data.target === 'parent') {\n source = window;\n postMessageEventOrigin = window.location.origin;\n }\n if (postMessageEvent.data.target) {\n postMessageEvent.stopImmediatePropagation();\n const args = Object.assign({\n ...postMessageEvent,\n data: postMessageEvent.data,\n source,\n origin: postMessageEventOrigin,\n });\n const postMessageEventWithOrigin = new MessageEvent(\n 'message',\n args,\n );\n window.dispatchEvent(postMessageEventWithOrigin);\n }\n }\n });\n `;\n result.document.body.appendChild(scriptElement);\n\n return result;\n };\n\n const listener = (event: MessageEvent) => {\n if (event.source === null && !event.origin) {\n let source;\n let origin;\n if (event.data.target === 'child') {\n source = window;\n origin = window.location.origin;\n } else if (event.data.target === 'parent') {\n // @ts-expect-error Accessing private property\n const { worker } = iframeExecutionService.jobs.values().next().value;\n source = worker;\n origin = iframeExecutionService.iframeUrl.toString();\n }\n\n if (event.data.target) {\n event.stopImmediatePropagation();\n const args = Object.assign({\n ...event,\n data: event.data,\n source,\n origin,\n });\n const eventWithOrigin: MessageEvent = new MessageEvent('message', args);\n window.dispatchEvent(eventWithOrigin);\n }\n }\n };\n\n window.addEventListener('message', listener);\n\n return () => {\n window.removeEventListener('message', listener);\n };\n};\n\nexport default fixJSDOMPostMessageEventSource;\n"]}
1
+ {"version":3,"file":"fixJSDOMPostMessageEventSource.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/fixJSDOMPostMessageEventSource.ts"],"names":[],"mappings":";;AAEA,yGAAyG;AACzG,MAAM,8BAA8B,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,QAAQ,GAAG,CAAC,KAAmB,EAAE,EAAE;QACvC,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;YAC1C,IAAI,MAAM,CAAC;YACX,IAAI,MAAM,CAAC;YACX,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE;gBACjC,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;aACjC;iBAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;gBACzC,8CAA8C;gBAC9C,MAAM,EAAE,MAAM,EAAE,GAAG,sBAAsB,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;gBACrE,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,sBAAsB,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;aACtD;YAED,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;gBACrB,KAAK,CAAC,wBAAwB,EAAE,CAAC;gBACjC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,iCACrB,KAAK,KACR,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,MAAM;oBACN,MAAM,IACN,CAAC;gBACH,MAAM,eAAe,GAAiB,IAAI,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBACxE,MAAM,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;aACvC;SACF;IACH,CAAC,CAAC;IAEF,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAE7C,OAAO,GAAG,EAAE;QACV,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,8BAA8B,CAAC","sourcesContent":["import { IframeExecutionService } from '../IframeExecutionService';\n\n// Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745\nconst fixJSDOMPostMessageEventSource = (\n iframeExecutionService: IframeExecutionService,\n) => {\n const listener = (event: MessageEvent) => {\n if (event.source === null && !event.origin) {\n let source;\n let origin;\n if (event.data.target === 'child') {\n source = window;\n origin = window.location.origin;\n } else if (event.data.target === 'parent') {\n // @ts-expect-error Accessing private property\n const { worker } = iframeExecutionService.jobs.values().next().value;\n source = worker;\n origin = iframeExecutionService.iframeUrl.toString();\n }\n\n if (event.data.target) {\n event.stopImmediatePropagation();\n const args = Object.assign({\n ...event,\n data: event.data,\n source,\n origin,\n });\n const eventWithOrigin: MessageEvent = new MessageEvent('message', args);\n window.dispatchEvent(eventWithOrigin);\n }\n }\n };\n\n window.addEventListener('message', listener);\n\n return () => {\n window.removeEventListener('message', listener);\n };\n};\n\nexport default fixJSDOMPostMessageEventSource;\n"]}
@@ -2,3 +2,4 @@ export * from './AbstractExecutionService';
2
2
  export * from './ExecutionService';
3
3
  export * from './iframe';
4
4
  export * from './node';
5
+ export * from './offscreen';
@@ -18,4 +18,5 @@ __exportStar(require("./AbstractExecutionService"), exports);
18
18
  __exportStar(require("./ExecutionService"), exports);
19
19
  __exportStar(require("./iframe"), exports);
20
20
  __exportStar(require("./node"), exports);
21
+ __exportStar(require("./offscreen"), exports);
21
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB;AACzB,yCAAuB","sourcesContent":["export * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\nexport * from './node';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB;AACzB,yCAAuB;AACvB,8CAA4B","sourcesContent":["export * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\nexport * from './node';\nexport * from './offscreen';\n"]}
@@ -0,0 +1,49 @@
1
+ import { AbstractExecutionService, ExecutionServiceArgs, Job } from '../AbstractExecutionService';
2
+ import { OffscreenPostMessageStream } from './OffscreenPostMessageStream';
3
+ declare type OffscreenExecutionEnvironmentServiceArgs = {
4
+ documentUrl: URL;
5
+ frameUrl: URL;
6
+ } & ExecutionServiceArgs;
7
+ export declare class OffscreenExecutionService extends AbstractExecutionService<string> {
8
+ #private;
9
+ readonly documentUrl: URL;
10
+ readonly frameUrl: URL;
11
+ /**
12
+ * Create a new offscreen execution service.
13
+ *
14
+ * @param args - The constructor arguments.
15
+ * @param args.documentUrl - The URL of the offscreen document to use as the
16
+ * execution environment. This must be a URL relative to the location where
17
+ * this is called. This cannot be a public (http(s)) URL.
18
+ * @param args.frameUrl - The URL of the iframe to load inside the offscreen
19
+ * document.
20
+ * @param args.messenger - The messenger to use for communication with the
21
+ * `SnapController`.
22
+ * @param args.setupSnapProvider - The function to use to set up the snap
23
+ * provider.
24
+ */
25
+ constructor({ documentUrl, frameUrl, messenger, setupSnapProvider, }: OffscreenExecutionEnvironmentServiceArgs);
26
+ /**
27
+ * Send a termination command to the offscreen document.
28
+ *
29
+ * @param job - The job to terminate.
30
+ */
31
+ protected terminateJob(job: Job<string>): Promise<void>;
32
+ /**
33
+ * Create a new stream for the specified job. This wraps the runtime stream
34
+ * in a stream specific to the job.
35
+ *
36
+ * @param jobId - The job ID.
37
+ */
38
+ protected initEnvStream(jobId: string): Promise<{
39
+ worker: string;
40
+ stream: OffscreenPostMessageStream;
41
+ }>;
42
+ /**
43
+ * Creates the offscreen document to be used as the execution environment.
44
+ *
45
+ * If the document already exists, this does nothing.
46
+ */
47
+ private createDocument;
48
+ }
49
+ export {};
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _OffscreenExecutionService_runtimeStream;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OffscreenExecutionService = void 0;
16
+ const post_message_stream_1 = require("@metamask/post-message-stream");
17
+ const nanoid_1 = require("nanoid");
18
+ const AbstractExecutionService_1 = require("../AbstractExecutionService");
19
+ const OffscreenPostMessageStream_1 = require("./OffscreenPostMessageStream");
20
+ class OffscreenExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
21
+ /**
22
+ * Create a new offscreen execution service.
23
+ *
24
+ * @param args - The constructor arguments.
25
+ * @param args.documentUrl - The URL of the offscreen document to use as the
26
+ * execution environment. This must be a URL relative to the location where
27
+ * this is called. This cannot be a public (http(s)) URL.
28
+ * @param args.frameUrl - The URL of the iframe to load inside the offscreen
29
+ * document.
30
+ * @param args.messenger - The messenger to use for communication with the
31
+ * `SnapController`.
32
+ * @param args.setupSnapProvider - The function to use to set up the snap
33
+ * provider.
34
+ */
35
+ constructor({ documentUrl, frameUrl, messenger, setupSnapProvider, }) {
36
+ super({
37
+ messenger,
38
+ setupSnapProvider,
39
+ });
40
+ _OffscreenExecutionService_runtimeStream.set(this, void 0);
41
+ this.documentUrl = documentUrl;
42
+ this.frameUrl = frameUrl;
43
+ __classPrivateFieldSet(this, _OffscreenExecutionService_runtimeStream, new post_message_stream_1.BrowserRuntimePostMessageStream({
44
+ name: 'parent',
45
+ target: 'child',
46
+ }), "f");
47
+ }
48
+ /**
49
+ * Send a termination command to the offscreen document.
50
+ *
51
+ * @param job - The job to terminate.
52
+ */
53
+ async terminateJob(job) {
54
+ // The `AbstractExecutionService` will have already closed the job stream,
55
+ // so we write to the runtime stream directly.
56
+ __classPrivateFieldGet(this, _OffscreenExecutionService_runtimeStream, "f").write({
57
+ jobId: job.id,
58
+ data: {
59
+ jsonrpc: '2.0',
60
+ method: 'terminateJob',
61
+ id: (0, nanoid_1.nanoid)(),
62
+ },
63
+ });
64
+ }
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
+ */
71
+ async initEnvStream(jobId) {
72
+ // Lazily create the offscreen document.
73
+ await this.createDocument();
74
+ const stream = new OffscreenPostMessageStream_1.OffscreenPostMessageStream({
75
+ stream: __classPrivateFieldGet(this, _OffscreenExecutionService_runtimeStream, "f"),
76
+ frameUrl: this.frameUrl.toString(),
77
+ jobId,
78
+ });
79
+ return { worker: jobId, stream };
80
+ }
81
+ /**
82
+ * Creates the offscreen document to be used as the execution environment.
83
+ *
84
+ * If the document already exists, this does nothing.
85
+ */
86
+ async createDocument() {
87
+ // Extensions can only have a single offscreen document.
88
+ if (await chrome.offscreen.hasDocument()) {
89
+ return;
90
+ }
91
+ await chrome.offscreen.createDocument({
92
+ justification: 'MetaMask Snaps Execution Environment',
93
+ reasons: ['IFRAME_SCRIPTING'],
94
+ url: this.documentUrl.toString(),
95
+ });
96
+ }
97
+ }
98
+ exports.OffscreenExecutionService = OffscreenExecutionService;
99
+ _OffscreenExecutionService_runtimeStream = new WeakMap();
100
+ //# sourceMappingURL=OffscreenExecutionService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OffscreenExecutionService.js","sourceRoot":"","sources":["../../../src/services/offscreen/OffscreenExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uEAAgF;AAChF,mCAAgC;AAEhC,0EAIqC;AACrC,6EAA0E;AAO1E,MAAa,yBAA0B,SAAQ,mDAAgC;IAO7E;;;;;;;;;;;;;OAaG;IACH,YAAY,EACV,WAAW,EACX,QAAQ,EACR,SAAS,EACT,iBAAiB,GACwB;QACzC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QAzBL,2DAAyD;QA2BvD,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,uBAAA,IAAI,4CAAkB,IAAI,qDAA+B,CAAC;YACxD,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;SAChB,CAAC,MAAA,CAAC;IACL,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAAgB;QAC3C,0EAA0E;QAC1E,8CAA8C;QAC9C,uBAAA,IAAI,gDAAe,CAAC,KAAK,CAAC;YACxB,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,IAAI,EAAE;gBACJ,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,cAAc;gBACtB,EAAE,EAAE,IAAA,eAAM,GAAE;aACb;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,wCAAwC;QACxC,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC;QAE5B,MAAM,MAAM,GAAG,IAAI,uDAA0B,CAAC;YAC5C,MAAM,EAAE,uBAAA,IAAI,gDAAe;YAC3B,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE;YAClC,KAAK;SACN,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,cAAc;QAC1B,wDAAwD;QACxD,IAAI,MAAM,MAAM,CAAC,SAAS,CAAC,WAAW,EAAE,EAAE;YACxC,OAAO;SACR;QAED,MAAM,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;YACpC,aAAa,EAAE,sCAAsC;YACrD,OAAO,EAAE,CAAC,kBAAkB,CAAC;YAC7B,GAAG,EAAE,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE;SACjC,CAAC,CAAC;IACL,CAAC;CACF;AA9FD,8DA8FC","sourcesContent":["import { BrowserRuntimePostMessageStream } from '@metamask/post-message-stream';\nimport { nanoid } from 'nanoid';\n\nimport {\n AbstractExecutionService,\n ExecutionServiceArgs,\n Job,\n} from '../AbstractExecutionService';\nimport { OffscreenPostMessageStream } from './OffscreenPostMessageStream';\n\ntype OffscreenExecutionEnvironmentServiceArgs = {\n documentUrl: URL;\n frameUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class OffscreenExecutionService extends AbstractExecutionService<string> {\n public readonly documentUrl: URL;\n\n public readonly frameUrl: URL;\n\n readonly #runtimeStream: BrowserRuntimePostMessageStream;\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.frameUrl - The URL of the iframe to load inside the offscreen\n * document.\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 frameUrl,\n messenger,\n setupSnapProvider,\n }: OffscreenExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n\n this.documentUrl = documentUrl;\n this.frameUrl = frameUrl;\n this.#runtimeStream = new BrowserRuntimePostMessageStream({\n name: 'parent',\n target: 'child',\n });\n }\n\n /**\n * Send a termination command to the offscreen document.\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.#runtimeStream.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 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 const stream = new OffscreenPostMessageStream({\n stream: this.#runtimeStream,\n frameUrl: this.frameUrl.toString(),\n jobId,\n });\n\n return { worker: jobId, stream };\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 private 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'],\n url: this.documentUrl.toString(),\n });\n }\n}\n"]}
@@ -0,0 +1,35 @@
1
+ import { BasePostMessageStream } from '@metamask/post-message-stream';
2
+ import { JsonRpcRequest } from '@metamask/utils';
3
+ export declare type OffscreenPostMessageStreamArgs = {
4
+ stream: BasePostMessageStream;
5
+ jobId: string;
6
+ frameUrl: string;
7
+ };
8
+ export declare type OffscreenPostMessage = {
9
+ jobId: string;
10
+ data: JsonRpcRequest;
11
+ };
12
+ /**
13
+ * A post message stream that wraps messages in a job ID, before sending them
14
+ * over the underlying stream.
15
+ */
16
+ export declare class OffscreenPostMessageStream extends BasePostMessageStream {
17
+ #private;
18
+ /**
19
+ * Initializes a new `OffscreenPostMessageStream` instance.
20
+ *
21
+ * @param args - The constructor arguments.
22
+ * @param args.stream - The underlying stream to use for communication.
23
+ * @param args.jobId - The ID of the job this stream is associated with.
24
+ * @param args.frameUrl - The URL of the frame to load inside the offscreen
25
+ * document.
26
+ */
27
+ constructor({ stream, jobId, frameUrl }: OffscreenPostMessageStreamArgs);
28
+ /**
29
+ * Write data to the underlying stream. This wraps the data in an object with
30
+ * the job ID.
31
+ *
32
+ * @param data - The data to write.
33
+ */
34
+ _postMessage(data: OffscreenPostMessage): void;
35
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _OffscreenPostMessageStream_instances, _OffscreenPostMessageStream_stream, _OffscreenPostMessageStream_jobId, _OffscreenPostMessageStream_frameUrl, _OffscreenPostMessageStream_onData;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OffscreenPostMessageStream = void 0;
16
+ const post_message_stream_1 = require("@metamask/post-message-stream");
17
+ /**
18
+ * A post message stream that wraps messages in a job ID, before sending them
19
+ * over the underlying stream.
20
+ */
21
+ class OffscreenPostMessageStream extends post_message_stream_1.BasePostMessageStream {
22
+ /**
23
+ * Initializes a new `OffscreenPostMessageStream` instance.
24
+ *
25
+ * @param args - The constructor arguments.
26
+ * @param args.stream - The underlying stream to use for communication.
27
+ * @param args.jobId - The ID of the job this stream is associated with.
28
+ * @param args.frameUrl - The URL of the frame to load inside the offscreen
29
+ * document.
30
+ */
31
+ constructor({ stream, jobId, frameUrl }) {
32
+ super();
33
+ _OffscreenPostMessageStream_instances.add(this);
34
+ _OffscreenPostMessageStream_stream.set(this, void 0);
35
+ _OffscreenPostMessageStream_jobId.set(this, void 0);
36
+ _OffscreenPostMessageStream_frameUrl.set(this, void 0);
37
+ __classPrivateFieldSet(this, _OffscreenPostMessageStream_stream, stream, "f");
38
+ __classPrivateFieldSet(this, _OffscreenPostMessageStream_jobId, jobId, "f");
39
+ __classPrivateFieldSet(this, _OffscreenPostMessageStream_frameUrl, frameUrl, "f");
40
+ __classPrivateFieldGet(this, _OffscreenPostMessageStream_stream, "f").on('data', __classPrivateFieldGet(this, _OffscreenPostMessageStream_instances, "m", _OffscreenPostMessageStream_onData).bind(this));
41
+ }
42
+ /**
43
+ * Write data to the underlying stream. This wraps the data in an object with
44
+ * the job ID.
45
+ *
46
+ * @param data - The data to write.
47
+ */
48
+ _postMessage(data) {
49
+ __classPrivateFieldGet(this, _OffscreenPostMessageStream_stream, "f").write({
50
+ jobId: __classPrivateFieldGet(this, _OffscreenPostMessageStream_jobId, "f"),
51
+ // TODO: Rather than injecting the frame URL here, we should come up with
52
+ // a better way to do this. The frame URL is needed to avoid hard coding
53
+ // it in the offscreen execution environment.
54
+ frameUrl: __classPrivateFieldGet(this, _OffscreenPostMessageStream_frameUrl, "f"),
55
+ data,
56
+ });
57
+ }
58
+ }
59
+ exports.OffscreenPostMessageStream = OffscreenPostMessageStream;
60
+ _OffscreenPostMessageStream_stream = new WeakMap(), _OffscreenPostMessageStream_jobId = new WeakMap(), _OffscreenPostMessageStream_frameUrl = new WeakMap(), _OffscreenPostMessageStream_instances = new WeakSet(), _OffscreenPostMessageStream_onData = function _OffscreenPostMessageStream_onData(data) {
61
+ if (data.jobId !== __classPrivateFieldGet(this, _OffscreenPostMessageStream_jobId, "f")) {
62
+ return;
63
+ }
64
+ this.push(data.data);
65
+ };
66
+ //# sourceMappingURL=OffscreenPostMessageStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OffscreenPostMessageStream.js","sourceRoot":"","sources":["../../../src/services/offscreen/OffscreenPostMessageStream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uEAAsE;AActE;;;GAGG;AACH,MAAa,0BAA2B,SAAQ,2CAAqB;IAOnE;;;;;;;;OAQG;IACH,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAkC;QACrE,KAAK,EAAE,CAAC;;QAhBV,qDAAwC;QAExC,oDAAwB;QAExB,uDAA2B;QAczB,uBAAA,IAAI,sCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,qCAAU,KAAK,MAAA,CAAC;QACpB,uBAAA,IAAI,wCAAa,QAAQ,MAAA,CAAC;QAE1B,uBAAA,IAAI,0CAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,uBAAA,IAAI,iFAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnD,CAAC;IAgBD;;;;;OAKG;IACH,YAAY,CAAC,IAA0B;QACrC,uBAAA,IAAI,0CAAQ,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,uBAAA,IAAI,yCAAO;YAClB,yEAAyE;YACzE,wEAAwE;YACxE,6CAA6C;YAC7C,QAAQ,EAAE,uBAAA,IAAI,4CAAU;YACxB,IAAI;SACL,CAAC,CAAC;IACL,CAAC;CACF;AAxDD,gEAwDC;qSAxBS,IAA0B;IAChC,IAAI,IAAI,CAAC,KAAK,KAAK,uBAAA,IAAI,yCAAO,EAAE;QAC9B,OAAO;KACR;IAED,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { JsonRpcRequest } from '@metamask/utils';\n\nexport type OffscreenPostMessageStreamArgs = {\n stream: BasePostMessageStream;\n jobId: string;\n frameUrl: string;\n};\n\nexport type OffscreenPostMessage = {\n jobId: string;\n data: JsonRpcRequest;\n};\n\n/**\n * A post message stream that wraps messages in a job ID, before sending them\n * over the underlying stream.\n */\nexport class OffscreenPostMessageStream extends BasePostMessageStream {\n readonly #stream: BasePostMessageStream;\n\n readonly #jobId: string;\n\n readonly #frameUrl: string;\n\n /**\n * Initializes a new `OffscreenPostMessageStream` instance.\n *\n * @param args - The constructor arguments.\n * @param args.stream - The underlying stream to use for communication.\n * @param args.jobId - The ID of the job this stream is associated with.\n * @param args.frameUrl - The URL of the frame to load inside the offscreen\n * document.\n */\n constructor({ stream, jobId, frameUrl }: OffscreenPostMessageStreamArgs) {\n super();\n\n this.#stream = stream;\n this.#jobId = jobId;\n this.#frameUrl = frameUrl;\n\n this.#stream.on('data', this.#onData.bind(this));\n }\n\n /**\n * Handle incoming data from the underlying stream. This checks that the job\n * ID matches the expected job ID, and pushes the data to the stream if so.\n *\n * @param data - The data to handle.\n */\n #onData(data: OffscreenPostMessage) {\n if (data.jobId !== this.#jobId) {\n return;\n }\n\n this.push(data.data);\n }\n\n /**\n * Write data to the underlying stream. This wraps the data in an object with\n * the job ID.\n *\n * @param data - The data to write.\n */\n _postMessage(data: OffscreenPostMessage) {\n this.#stream.write({\n jobId: this.#jobId,\n // TODO: Rather than injecting the frame URL here, we should come up with\n // a better way to do this. The frame URL is needed to avoid hard coding\n // it in the offscreen execution environment.\n frameUrl: this.#frameUrl,\n data,\n });\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export * from './OffscreenExecutionService';
2
+ export * from './OffscreenPostMessageStream';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./OffscreenExecutionService"), exports);
18
+ __exportStar(require("./OffscreenPostMessageStream"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/services/offscreen/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8DAA4C;AAC5C,+DAA6C","sourcesContent":["export * from './OffscreenExecutionService';\nexport * from './OffscreenPostMessageStream';\n"]}
@@ -1,13 +1,15 @@
1
1
  import { AddApprovalRequest } from '@metamask/approval-controller';
2
2
  import { BaseControllerV2 as BaseController, RestrictedControllerMessenger } from '@metamask/base-controller';
3
- import { GetEndowments, GetPermissions, GrantPermissions, HasPermission, HasPermissions, RevokeAllPermissions, RevokePermissionForAllSubjects, RevokePermissions } from '@metamask/permission-controller';
4
- import { BlockedSnapInfo, InstallSnapsResult, PersistedSnap, RequestedSnapPermissions, Snap, SnapId, SnapRpcHook, SnapRpcHookArgs, SnapStatusEvents, StatusContext, StatusEvents, StatusStates, TruncatedSnap, ValidatedSnapId } from '@metamask/snaps-utils';
3
+ import { GetEndowments, GetPermissions, GetSubjects, GrantPermissions, HasPermission, HasPermissions, RevokeAllPermissions, RevokePermissionForAllSubjects, RevokePermissions, UpdateCaveat } from '@metamask/permission-controller';
4
+ import { BlockReason } from '@metamask/snaps-registry';
5
+ import { InstallSnapsResult, PersistedSnap, RequestedSnapPermissions, Snap, SnapId, SnapRpcHook, SnapRpcHookArgs, SnapStatusEvents, StatusContext, StatusEvents, StatusStates, TruncatedSnap, ValidatedSnapId } from '@metamask/snaps-utils';
5
6
  import { GetSubjectMetadata } from '@metamask/subject-metadata-controller';
6
7
  import { Json } from '@metamask/utils';
7
8
  import { StateMachine } from '@xstate/fsm';
8
9
  import type { Patch } from 'immer';
9
10
  import { ExecuteSnapAction, ExecutionServiceEvents, HandleRpcRequestAction, SnapErrorJson, TerminateAllSnapsAction, TerminateSnapAction } from '../services';
10
11
  import { detectSnapLocation, SnapLocation } from './location';
12
+ import { SnapsRegistry, SnapsRegistryMetadata } from './registry';
11
13
  import { Timer } from './Timer';
12
14
  export declare const controllerName = "SnapController";
13
15
  export declare const SNAP_APPROVAL_INSTALL = "wallet_installSnap";
@@ -168,7 +170,11 @@ export declare type RemoveSnapError = {
168
170
  type: `${typeof controllerName}:removeSnapError`;
169
171
  handler: SnapController['removeSnapError'];
170
172
  };
171
- export declare type SnapControllerActions = ClearSnapState | GetSnap | GetSnapState | HandleSnapRequest | HasSnap | UpdateBlockedSnaps | UpdateSnapState | EnableSnap | DisableSnap | RemoveSnap | GetPermittedSnaps | InstallSnaps | RemoveSnapError | GetAllSnaps | IncrementActiveReferences | DecrementActiveReferences;
173
+ export declare type GetRegistryMetadata = {
174
+ type: `${typeof controllerName}:getRegistryMetadata`;
175
+ handler: SnapController['getRegistryMetadata'];
176
+ };
177
+ export declare type SnapControllerActions = ClearSnapState | GetSnap | GetSnapState | HandleSnapRequest | HasSnap | UpdateBlockedSnaps | UpdateSnapState | EnableSnap | DisableSnap | RemoveSnap | GetPermittedSnaps | InstallSnaps | RemoveSnapError | GetAllSnaps | IncrementActiveReferences | DecrementActiveReferences | GetRegistryMetadata;
172
178
  export declare type SnapStateChange = {
173
179
  type: `${typeof controllerName}:stateChange`;
174
180
  payload: [SnapControllerState, Patch[]];
@@ -185,7 +191,7 @@ export declare type SnapAdded = {
185
191
  */
186
192
  export declare type SnapBlocked = {
187
193
  type: `${typeof controllerName}:snapBlocked`;
188
- payload: [snapId: string, blockedSnapInfo: BlockedSnapInfo];
194
+ payload: [snapId: string, blockedSnapInfo?: BlockReason];
189
195
  };
190
196
  /**
191
197
  * Emitted when a snap has been started after being added and authorized during
@@ -232,7 +238,7 @@ export declare type SnapTerminated = {
232
238
  payload: [snap: TruncatedSnap];
233
239
  };
234
240
  export declare type SnapControllerEvents = SnapAdded | SnapBlocked | SnapInstalled | SnapRemoved | SnapStateChange | SnapUnblocked | SnapUpdated | SnapRolledback | SnapTerminated;
235
- export declare type AllowedActions = GetEndowments | GetPermissions | GetSubjectMetadata | HasPermission | HasPermissions | RevokePermissions | RevokeAllPermissions | RevokePermissionForAllSubjects | GrantPermissions | AddApprovalRequest | HandleRpcRequestAction | ExecuteSnapAction | TerminateAllSnapsAction | TerminateSnapAction;
241
+ export declare type AllowedActions = GetEndowments | GetPermissions | GetSubjects | GetSubjectMetadata | HasPermission | HasPermissions | RevokePermissions | RevokeAllPermissions | RevokePermissionForAllSubjects | GrantPermissions | AddApprovalRequest | HandleRpcRequestAction | ExecuteSnapAction | TerminateAllSnapsAction | TerminateSnapAction | UpdateCaveat;
236
242
  export declare type AllowedEvents = ExecutionServiceEvents;
237
243
  declare type SnapControllerMessenger = RestrictedControllerMessenger<typeof controllerName, SnapControllerActions | AllowedActions, SnapControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
238
244
  declare type FeatureFlags = {
@@ -244,24 +250,9 @@ declare type FeatureFlags = {
244
250
  * @see {SnapController.processRequestedSnap}
245
251
  */
246
252
  dappsCanUpdateSnaps?: true;
253
+ requireAllowlist?: true;
254
+ allowLocalSnaps?: true;
247
255
  };
248
- declare type SemVerVersion = string;
249
- declare type SnapInfo = {
250
- version: SemVerVersion;
251
- shasum: string;
252
- };
253
- export declare type CheckSnapBlockListArg = Record<SnapId, SnapInfo>;
254
- export declare type CheckSnapBlockListResult = Record<SnapId, {
255
- blocked: true;
256
- reason?: string;
257
- infoUrl?: string;
258
- } | {
259
- blocked: false;
260
- }>;
261
- /**
262
- * Checks whether a version of a snap is blocked.
263
- */
264
- export declare type CheckSnapBlockList = (snapsToCheck: CheckSnapBlockListArg) => Promise<CheckSnapBlockListResult>;
265
256
  declare type SnapControllerArgs = {
266
257
  /**
267
258
  * A teardown function that allows the host to clean up its instrumentation
@@ -273,6 +264,10 @@ declare type SnapControllerArgs = {
273
264
  * APIs that will be added to the snap execution environment at runtime.
274
265
  */
275
266
  environmentEndowmentPermissions: string[];
267
+ /**
268
+ * Excluded permissions with its associated error message used to forbid certain permssions.
269
+ */
270
+ excludedPermissions: Record<string, string>;
276
271
  /**
277
272
  * The function that will be used by the controller fo make network requests.
278
273
  * Should be compatible with {@link fetch}.
@@ -288,9 +283,9 @@ declare type SnapControllerArgs = {
288
283
  */
289
284
  idleTimeCheckInterval?: number;
290
285
  /**
291
- * A function that checks whether the specified snap and version is blocked.
286
+ * A registry implementation used for checking for verified and blocked snaps.
292
287
  */
293
- checkBlockList: CheckSnapBlockList;
288
+ registry: SnapsRegistry;
294
289
  /**
295
290
  * The maximum amount of time that a snap may be idle.
296
291
  */
@@ -323,21 +318,13 @@ export declare class SnapController extends BaseController<string, SnapControlle
323
318
  #private;
324
319
  private readonly maxRequestTime;
325
320
  private readonly snapsRuntimeData;
326
- constructor({ closeAllConnections, messenger, state, environmentEndowmentPermissions, idleTimeCheckInterval, checkBlockList, maxIdleTime, maxRequestTime, fetchFunction, featureFlags, detectSnapLocation: detectSnapLocationFunction, }: SnapControllerArgs);
321
+ constructor({ closeAllConnections, messenger, state, environmentEndowmentPermissions, excludedPermissions, idleTimeCheckInterval, registry, maxIdleTime, maxRequestTime, fetchFunction, featureFlags, detectSnapLocation: detectSnapLocationFunction, }: SnapControllerArgs);
327
322
  /**
328
323
  * Checks all installed snaps against the block list and
329
324
  * blocks/unblocks snaps as appropriate. See {@link SnapController.blockSnap}
330
325
  * for more information.
331
326
  */
332
327
  updateBlockedSnaps(): Promise<void>;
333
- /**
334
- * Checks the block list to determine whether a version of a snap is blocked.
335
- *
336
- * @param snapId - The snap id to check.
337
- * @param snapInfo - Snap information containing version and shasum.
338
- * @returns Whether the version of the snap is blocked or not.
339
- */
340
- isBlocked(snapId: ValidatedSnapId, snapInfo: SnapInfo): Promise<boolean>;
341
328
  _onUnhandledSnapError(snapId: SnapId, error: SnapErrorJson): void;
342
329
  _onOutboundRequest(snapId: SnapId): void;
343
330
  _onOutboundResponse(snapId: SnapId): void;
@@ -548,11 +535,19 @@ export declare class SnapController extends BaseController<string, SnapControlle
548
535
  *
549
536
  * @param origin - The origin requesting the snap update.
550
537
  * @param snapId - The id of the Snap to be updated.
551
- * @param newVersionRange - A semver version range in which the maximum version will be chosen.
552
538
  * @param location - Optional location that was already used during installation flow.
539
+ * @param newVersionRange - A semver version range in which the maximum version will be chosen.
553
540
  * @returns The snap metadata if updated, `null` otherwise.
554
541
  */
555
- updateSnap(origin: string, snapId: ValidatedSnapId, newVersionRange?: string, location?: SnapLocation): Promise<TruncatedSnap | null>;
542
+ updateSnap(origin: string, snapId: ValidatedSnapId, location: SnapLocation, newVersionRange?: string): Promise<TruncatedSnap | null>;
543
+ /**
544
+ * Get metadata for the given snap ID.
545
+ *
546
+ * @param snapId - The ID of the snap to get metadata for.
547
+ * @returns The metadata for the given snap ID, or `null` if the snap is not
548
+ * verified.
549
+ */
550
+ getRegistryMetadata(snapId: SnapId): Promise<SnapsRegistryMetadata | null>;
556
551
  /**
557
552
  * Initiates a request for the given snap's initial permissions.
558
553
  * Must be called in order. See processRequestedSnap.