@metamask/snaps-controllers 0.29.0 → 0.31.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 (46) hide show
  1. package/dist/cronjob/CronjobController.js +11 -8
  2. package/dist/cronjob/CronjobController.js.map +1 -1
  3. package/dist/fsm.js +5 -5
  4. package/dist/fsm.js.map +1 -1
  5. package/dist/multichain/MultiChainController.js +12 -20
  6. package/dist/multichain/MultiChainController.js.map +1 -1
  7. package/dist/multichain/matching.js +3 -4
  8. package/dist/multichain/matching.js.map +1 -1
  9. package/dist/services/iframe/IframeExecutionService.js +1 -2
  10. package/dist/services/iframe/IframeExecutionService.js.map +1 -1
  11. package/dist/snaps/RequestQueue.js +3 -6
  12. package/dist/snaps/RequestQueue.js.map +1 -1
  13. package/dist/snaps/SnapController.d.ts +7 -5
  14. package/dist/snaps/SnapController.js +253 -171
  15. package/dist/snaps/SnapController.js.map +1 -1
  16. package/dist/snaps/endowments/cronjob.js +2 -3
  17. package/dist/snaps/endowments/cronjob.js.map +1 -1
  18. package/dist/snaps/endowments/enum.d.ts +2 -1
  19. package/dist/snaps/endowments/enum.js +1 -0
  20. package/dist/snaps/endowments/enum.js.map +1 -1
  21. package/dist/snaps/endowments/index.d.ts +9 -0
  22. package/dist/snaps/endowments/index.js +8 -1
  23. package/dist/snaps/endowments/index.js.map +1 -1
  24. package/dist/snaps/endowments/keyring.js +3 -4
  25. package/dist/snaps/endowments/keyring.js.map +1 -1
  26. package/dist/snaps/endowments/rpc.js +2 -2
  27. package/dist/snaps/endowments/rpc.js.map +1 -1
  28. package/dist/snaps/endowments/transaction-insight.js +4 -5
  29. package/dist/snaps/endowments/transaction-insight.js.map +1 -1
  30. package/dist/snaps/endowments/web-assembly.d.ts +13 -0
  31. package/dist/snaps/endowments/web-assembly.js +30 -0
  32. package/dist/snaps/endowments/web-assembly.js.map +1 -0
  33. package/dist/snaps/location/http.js +9 -3
  34. package/dist/snaps/location/http.js.map +1 -1
  35. package/dist/snaps/location/local.js +1 -1
  36. package/dist/snaps/location/local.js.map +1 -1
  37. package/dist/snaps/location/location.js +2 -3
  38. package/dist/snaps/location/location.js.map +1 -1
  39. package/dist/snaps/location/npm.js +11 -8
  40. package/dist/snaps/location/npm.js.map +1 -1
  41. package/dist/snaps/registry/json.js +5 -7
  42. package/dist/snaps/registry/json.js.map +1 -1
  43. package/package.json +35 -15
  44. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.d.ts +0 -3
  45. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js +0 -34
  46. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js.map +0 -1
@@ -56,14 +56,13 @@ exports.getCronjobCaveatMapper = getCronjobCaveatMapper;
56
56
  * cronjob caveat.
57
57
  */
58
58
  function getCronjobCaveatJobs(permission) {
59
- var _a, _b;
60
- if (!(permission === null || permission === void 0 ? void 0 : permission.caveats)) {
59
+ if (!permission?.caveats) {
61
60
  return null;
62
61
  }
63
62
  (0, utils_1.assert)(permission.caveats.length === 1);
64
63
  (0, utils_1.assert)(permission.caveats[0].type === snaps_utils_1.SnapCaveatType.SnapCronjob);
65
64
  const caveat = permission.caveats[0];
66
- return (_b = (_a = caveat.value) === null || _a === void 0 ? void 0 : _a.jobs) !== null && _b !== void 0 ? _b : null;
65
+ return caveat.value?.jobs ?? null;
67
66
  }
68
67
  exports.getCronjobCaveatJobs = getCronjobCaveatJobs;
69
68
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"cronjob.js","sourceRoot":"","sources":["../../../src/snaps/endowments/cronjob.ts"],"names":[],"mappings":";;;AAAA,2EAQyC;AACzC,uDAI+B;AAC/B,2CAMyB;AACzB,mDAA2C;AAE3C,iCAAwC;AAExC,MAAM,cAAc,GAAG,qBAAc,CAAC,OAAO,CAAC;AAS9C;;;;;GAKG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAqB,EAAE,EAAE;IAC5B,OAAO;QACL,cAAc,EAAE,sCAAc,CAA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SACpC,CAAC,CAAC;KACJ;IAED,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IAEzB,IAAI,CAAC,IAAA,mBAAW,EAAC,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,IAAA,qBAAa,EAAC,KAAK,CAAC,EAAE;QACxD,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;YAChC,OAAO,EAAE,0BAA0B;SACpC,CAAC,CAAC;KACJ;IAED,IAAI,CAAC,IAAA,yCAA2B,EAAC,KAAK,CAAC,IAAI,CAAC,EAAE;QAC5C,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;YAChC,OAAO,EAAE,+CAA+C;SACzD,CAAC,CAAC;KACJ;AACH,CAAC;AApBD,sDAoBC;AAED;;GAEG;AACU,QAAA,2BAA2B,GAGpC;IACF,CAAC,4BAAc,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC1C,IAAI,EAAE,4BAAc,CAAC,WAAW;QAChC,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KACrD,CAAC;CACH,CAAC","sourcesContent":["import {\n PermissionSpecificationBuilder,\n PermissionType,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionConstraint,\n Caveat,\n CaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport {\n SnapCaveatType,\n CronjobSpecification,\n isCronjobSpecificationArray,\n} from '@metamask/snaps-utils';\nimport {\n assert,\n hasProperty,\n isPlainObject,\n Json,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Cronjob;\n\ntype CronjobEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof permissionName;\n endowmentGetter: (_options?: any) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the cronjob endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n CronjobEndowmentSpecification\n> = (_builderOptions?: any) => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey: permissionName,\n allowedCaveats: [SnapCaveatType.SnapCronjob],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n };\n};\n\nexport const cronjobEndowmentBuilder = Object.freeze({\n targetKey: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getCronjobCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapCronjob,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the cronjobs from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The cronjobs, or `null` if the permission does not have a\n * cronjob caveat.\n */\nexport function getCronjobCaveatJobs(\n permission?: PermissionConstraint,\n): CronjobSpecification[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);\n\n const caveat = permission.caveats[0] as Caveat<string, { jobs: Json[] }>;\n\n return (caveat.value?.jobs as CronjobSpecification[]) ?? null;\n}\n\n/**\n * Validate the cronjob specification values associated with a caveat.\n * This validates that the value is a non-empty array with valid\n * cronjob expression and request object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateCronjobCaveat(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n if (!isCronjobSpecificationArray(value.jobs)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a valid cronjob specification array.',\n });\n }\n}\n\n/**\n * Caveat specification for the Cronjob.\n */\nexport const cronjobCaveatSpecifications: Record<\n SnapCaveatType.SnapCronjob,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapCronjob]: Object.freeze({\n type: SnapCaveatType.SnapCronjob,\n validator: (caveat) => validateCronjobCaveat(caveat),\n }),\n};\n"]}
1
+ {"version":3,"file":"cronjob.js","sourceRoot":"","sources":["../../../src/snaps/endowments/cronjob.ts"],"names":[],"mappings":";;;AAAA,2EAQyC;AACzC,uDAI+B;AAC/B,2CAMyB;AACzB,mDAA2C;AAE3C,iCAAwC;AAExC,MAAM,cAAc,GAAG,qBAAc,CAAC,OAAO,CAAC;AAS9C;;;;;GAKG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAqB,EAAE,EAAE;IAC5B,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,SAAS,EAAE,cAAc;QACzB,cAAc,EAAE,CAAC,4BAAc,CAAC,WAAW,CAAC;QAC5C,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,SAAS;KACvE,CAAC;AACJ,CAAC,CAAC;AAEW,QAAA,uBAAuB,GAAG,MAAM,CAAC,MAAM,CAAC;IACnD,SAAS,EAAE,cAAc;IACzB,oBAAoB;CACZ,CAAC,CAAC;AAEZ;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAAW;IAEX,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,4BAAc,CAAC,WAAW;gBAChC,KAAK;aACN;S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AoBC;AAED;;GAEG;AACU,QAAA,2BAA2B,GAGpC;IACF,CAAC,4BAAc,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC1C,IAAI,EAAE,4BAAc,CAAC,WAAW;QAChC,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KACrD,CAAC;CACH,CAAC","sourcesContent":["import {\n PermissionSpecificationBuilder,\n PermissionType,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionConstraint,\n Caveat,\n CaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport {\n SnapCaveatType,\n CronjobSpecification,\n isCronjobSpecificationArray,\n} from '@metamask/snaps-utils';\nimport {\n assert,\n hasProperty,\n isPlainObject,\n Json,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Cronjob;\n\ntype CronjobEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof permissionName;\n endowmentGetter: (_options?: any) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the cronjob endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n CronjobEndowmentSpecification\n> = (_builderOptions?: any) => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey: permissionName,\n allowedCaveats: [SnapCaveatType.SnapCronjob],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n };\n};\n\nexport const cronjobEndowmentBuilder = Object.freeze({\n targetKey: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getCronjobCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapCronjob,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the cronjobs from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The cronjobs, or `null` if the permission does not have a\n * cronjob caveat.\n */\nexport function getCronjobCaveatJobs(\n permission?: PermissionConstraint,\n): CronjobSpecification[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);\n\n const caveat = permission.caveats[0] as Caveat<string, { jobs: Json[] }>;\n\n return (caveat.value?.jobs as CronjobSpecification[]) ?? null;\n}\n\n/**\n * Validate the cronjob specification values associated with a caveat.\n * This validates that the value is a non-empty array with valid\n * cronjob expression and request object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateCronjobCaveat(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n if (!isCronjobSpecificationArray(value.jobs)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a valid cronjob specification array.',\n });\n }\n}\n\n/**\n * Caveat specification for the Cronjob.\n */\nexport const cronjobCaveatSpecifications: Record<\n SnapCaveatType.SnapCronjob,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapCronjob]: Object.freeze({\n type: SnapCaveatType.SnapCronjob,\n validator: (caveat) => validateCronjobCaveat(caveat),\n }),\n};\n"]}
@@ -5,5 +5,6 @@ export declare enum SnapEndowments {
5
5
  Keyring = "endowment:keyring",
6
6
  Cronjob = "endowment:cronjob",
7
7
  EthereumProvider = "endowment:ethereum-provider",
8
- Rpc = "endowment:rpc"
8
+ Rpc = "endowment:rpc",
9
+ WebAssemblyAccess = "endowment:webassembly"
9
10
  }
@@ -10,5 +10,6 @@ var SnapEndowments;
10
10
  SnapEndowments["Cronjob"] = "endowment:cronjob";
11
11
  SnapEndowments["EthereumProvider"] = "endowment:ethereum-provider";
12
12
  SnapEndowments["Rpc"] = "endowment:rpc";
13
+ SnapEndowments["WebAssemblyAccess"] = "endowment:webassembly";
13
14
  })(SnapEndowments = exports.SnapEndowments || (exports.SnapEndowments = {}));
14
15
  //# sourceMappingURL=enum.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"enum.js","sourceRoot":"","sources":["../../../src/snaps/endowments/enum.ts"],"names":[],"mappings":";;;AAAA,IAAY,cAQX;AARD,WAAY,cAAc;IACxB,4DAA0C,CAAA;IAC1C,wDAAsC,CAAA;IACtC,sEAAoD,CAAA;IACpD,+CAA6B,CAAA;IAC7B,+CAA6B,CAAA;IAC7B,kEAAgD,CAAA;IAChD,uCAAqB,CAAA;AACvB,CAAC,EARW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAQzB","sourcesContent":["export enum SnapEndowments {\n NetworkAccess = 'endowment:network-access',\n LongRunning = 'endowment:long-running',\n TransactionInsight = 'endowment:transaction-insight',\n Keyring = 'endowment:keyring',\n Cronjob = 'endowment:cronjob',\n EthereumProvider = 'endowment:ethereum-provider',\n Rpc = 'endowment:rpc',\n}\n"]}
1
+ {"version":3,"file":"enum.js","sourceRoot":"","sources":["../../../src/snaps/endowments/enum.ts"],"names":[],"mappings":";;;AAAA,IAAY,cASX;AATD,WAAY,cAAc;IACxB,4DAA0C,CAAA;IAC1C,wDAAsC,CAAA;IACtC,sEAAoD,CAAA;IACpD,+CAA6B,CAAA;IAC7B,+CAA6B,CAAA;IAC7B,kEAAgD,CAAA;IAChD,uCAAqB,CAAA;IACrB,6DAA2C,CAAA;AAC7C,CAAC,EATW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QASzB","sourcesContent":["export enum SnapEndowments {\n NetworkAccess = 'endowment:network-access',\n LongRunning = 'endowment:long-running',\n TransactionInsight = 'endowment:transaction-insight',\n Keyring = 'endowment:keyring',\n Cronjob = 'endowment:cronjob',\n EthereumProvider = 'endowment:ethereum-provider',\n Rpc = 'endowment:rpc',\n WebAssemblyAccess = 'endowment:webassembly',\n}\n"]}
@@ -68,6 +68,15 @@ export declare const endowmentPermissionBuilders: {
68
68
  validator: import("@metamask/permission-controller").PermissionValidatorConstraint;
69
69
  }>;
70
70
  }>;
71
+ readonly "endowment:webassembly": Readonly<{
72
+ readonly targetKey: import("./enum").SnapEndowments.WebAssemblyAccess;
73
+ readonly specificationBuilder: import("@metamask/permission-controller").PermissionSpecificationBuilder<import("@metamask/permission-controller").PermissionType.Endowment, any, {
74
+ permissionType: import("@metamask/permission-controller").PermissionType.Endowment;
75
+ targetKey: import("./enum").SnapEndowments.WebAssemblyAccess;
76
+ endowmentGetter: (_options?: any) => ["WebAssembly"];
77
+ allowedCaveats: null;
78
+ }>;
79
+ }>;
71
80
  };
72
81
  export declare const endowmentCaveatSpecifications: {
73
82
  rpcOrigin: import("@metamask/permission-controller").CaveatSpecificationConstraint;
@@ -23,6 +23,7 @@ const long_running_1 = require("./long-running");
23
23
  const network_access_1 = require("./network-access");
24
24
  const rpc_1 = require("./rpc");
25
25
  const transaction_insight_1 = require("./transaction-insight");
26
+ const web_assembly_1 = require("./web-assembly");
26
27
  exports.endowmentPermissionBuilders = {
27
28
  [network_access_1.networkAccessEndowmentBuilder.targetKey]: network_access_1.networkAccessEndowmentBuilder,
28
29
  [long_running_1.longRunningEndowmentBuilder.targetKey]: long_running_1.longRunningEndowmentBuilder,
@@ -31,8 +32,14 @@ exports.endowmentPermissionBuilders = {
31
32
  [cronjob_1.cronjobEndowmentBuilder.targetKey]: cronjob_1.cronjobEndowmentBuilder,
32
33
  [ethereum_provider_1.ethereumProviderEndowmentBuilder.targetKey]: ethereum_provider_1.ethereumProviderEndowmentBuilder,
33
34
  [rpc_1.rpcEndowmentBuilder.targetKey]: rpc_1.rpcEndowmentBuilder,
35
+ [web_assembly_1.webAssemblyEndowmentBuilder.targetKey]: web_assembly_1.webAssemblyEndowmentBuilder,
36
+ };
37
+ exports.endowmentCaveatSpecifications = {
38
+ ...keyring_1.keyringCaveatSpecifications,
39
+ ...cronjob_1.cronjobCaveatSpecifications,
40
+ ...transaction_insight_1.transactionInsightCaveatSpecifications,
41
+ ...rpc_1.rpcCaveatSpecifications,
34
42
  };
35
- exports.endowmentCaveatSpecifications = Object.assign(Object.assign(Object.assign(Object.assign({}, keyring_1.keyringCaveatSpecifications), cronjob_1.cronjobCaveatSpecifications), transaction_insight_1.transactionInsightCaveatSpecifications), rpc_1.rpcCaveatSpecifications);
36
43
  exports.endowmentCaveatMappers = {
37
44
  [keyring_1.keyringEndowmentBuilder.targetKey]: keyring_1.getKeyringCaveatMapper,
38
45
  [cronjob_1.cronjobEndowmentBuilder.targetKey]: cronjob_1.getCronjobCaveatMapper,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/snaps/endowments/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,uDAAoD;AAGpD,uCAImB;AACnB,2DAAuE;AACvE,uCAImB;AACnB,iDAA6D;AAC7D,qDAAiE;AACjE,+BAIe;AACf,+DAI+B;AAElB,QAAA,2BAA2B,GAAG;IACzC,CAAC,8CAA6B,CAAC,SAAS,CAAC,EAAE,8CAA6B;IACxE,CAAC,0CAA2B,CAAC,SAAS,CAAC,EAAE,0CAA2B;IACpE,CAAC,wDAAkC,CAAC,SAAS,CAAC,EAC5C,wDAAkC;IACpC,CAAC,iCAAuB,CAAC,SAAS,CAAC,EAAE,iCAAuB;IAC5D,CAAC,iCAAuB,CAAC,SAAS,CAAC,EAAE,iCAAuB;IAC5D,CAAC,oDAAgC,CAAC,SAAS,CAAC,EAC1C,oDAAgC;IAClC,CAAC,yBAAmB,CAAC,SAAS,CAAC,EAAE,yBAAmB;CAC5C,CAAC;AAEE,QAAA,6BAA6B,+DACrC,qCAA2B,GAC3B,qCAA2B,GAC3B,4DAAsC,GACtC,6BAAuB,EAC1B;AAEW,QAAA,sBAAsB,GAG/B;IACF,CAAC,iCAAuB,CAAC,SAAS,CAAC,EAAE,gCAAsB;IAC3D,CAAC,iCAAuB,CAAC,SAAS,CAAC,EAAE,gCAAsB;IAC3D,CAAC,wDAAkC,CAAC,SAAS,CAAC,EAC5C,uDAAiC;IACnC,CAAC,yBAAmB,CAAC,SAAS,CAAC,EAAE,wBAAkB;CACpD,CAAC;AAEW,QAAA,iBAAiB,GAAgC;IAC5D,CAAC,yBAAW,CAAC,YAAY,CAAC,EAAE,yBAAmB,CAAC,SAAS;IACzD,CAAC,yBAAW,CAAC,WAAW,CAAC,EAAE,iCAAuB,CAAC,SAAS;IAC5D,CAAC,yBAAW,CAAC,aAAa,CAAC,EAAE,wDAAkC,CAAC,SAAS;IACzE,CAAC,yBAAW,CAAC,SAAS,CAAC,EAAE,iCAAuB,CAAC,SAAS;CAC3D,CAAC;AAEF,yCAAuB;AACvB,6DAAmE;AAA1D,iIAAA,0BAA0B,OAAA","sourcesContent":["import { PermissionConstraint } from '@metamask/permission-controller';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { Json } from '@metamask/utils';\n\nimport {\n cronjobCaveatSpecifications,\n cronjobEndowmentBuilder,\n getCronjobCaveatMapper,\n} from './cronjob';\nimport { ethereumProviderEndowmentBuilder } from './ethereum-provider';\nimport {\n keyringEndowmentBuilder,\n keyringCaveatSpecifications,\n getKeyringCaveatMapper,\n} from './keyring';\nimport { longRunningEndowmentBuilder } from './long-running';\nimport { networkAccessEndowmentBuilder } from './network-access';\nimport {\n getRpcCaveatMapper,\n rpcCaveatSpecifications,\n rpcEndowmentBuilder,\n} from './rpc';\nimport {\n getTransactionInsightCaveatMapper,\n transactionInsightCaveatSpecifications,\n transactionInsightEndowmentBuilder,\n} from './transaction-insight';\n\nexport const endowmentPermissionBuilders = {\n [networkAccessEndowmentBuilder.targetKey]: networkAccessEndowmentBuilder,\n [longRunningEndowmentBuilder.targetKey]: longRunningEndowmentBuilder,\n [transactionInsightEndowmentBuilder.targetKey]:\n transactionInsightEndowmentBuilder,\n [keyringEndowmentBuilder.targetKey]: keyringEndowmentBuilder,\n [cronjobEndowmentBuilder.targetKey]: cronjobEndowmentBuilder,\n [ethereumProviderEndowmentBuilder.targetKey]:\n ethereumProviderEndowmentBuilder,\n [rpcEndowmentBuilder.targetKey]: rpcEndowmentBuilder,\n} as const;\n\nexport const endowmentCaveatSpecifications = {\n ...keyringCaveatSpecifications,\n ...cronjobCaveatSpecifications,\n ...transactionInsightCaveatSpecifications,\n ...rpcCaveatSpecifications,\n};\n\nexport const endowmentCaveatMappers: Record<\n string,\n (value: Json) => Pick<PermissionConstraint, 'caveats'>\n> = {\n [keyringEndowmentBuilder.targetKey]: getKeyringCaveatMapper,\n [cronjobEndowmentBuilder.targetKey]: getCronjobCaveatMapper,\n [transactionInsightEndowmentBuilder.targetKey]:\n getTransactionInsightCaveatMapper,\n [rpcEndowmentBuilder.targetKey]: getRpcCaveatMapper,\n};\n\nexport const handlerEndowments: Record<HandlerType, string> = {\n [HandlerType.OnRpcRequest]: rpcEndowmentBuilder.targetKey,\n [HandlerType.SnapKeyring]: keyringEndowmentBuilder.targetKey,\n [HandlerType.OnTransaction]: transactionInsightEndowmentBuilder.targetKey,\n [HandlerType.OnCronjob]: cronjobEndowmentBuilder.targetKey,\n};\n\nexport * from './enum';\nexport { getTransactionOriginCaveat } from './transaction-insight';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/snaps/endowments/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,uDAAoD;AAGpD,u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,SAAS;CAC3D,CAAC;AAEF,yCAAuB;AACvB,6DAAmE;AAA1D,iIAAA,0BAA0B,OAAA","sourcesContent":["import { PermissionConstraint } from '@metamask/permission-controller';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { Json } from '@metamask/utils';\n\nimport {\n cronjobCaveatSpecifications,\n cronjobEndowmentBuilder,\n getCronjobCaveatMapper,\n} from './cronjob';\nimport { ethereumProviderEndowmentBuilder } from './ethereum-provider';\nimport {\n keyringEndowmentBuilder,\n keyringCaveatSpecifications,\n getKeyringCaveatMapper,\n} from './keyring';\nimport { longRunningEndowmentBuilder } from './long-running';\nimport { networkAccessEndowmentBuilder } from './network-access';\nimport {\n getRpcCaveatMapper,\n rpcCaveatSpecifications,\n rpcEndowmentBuilder,\n} from './rpc';\nimport {\n getTransactionInsightCaveatMapper,\n transactionInsightCaveatSpecifications,\n transactionInsightEndowmentBuilder,\n} from './transaction-insight';\nimport { webAssemblyEndowmentBuilder } from './web-assembly';\n\nexport const endowmentPermissionBuilders = {\n [networkAccessEndowmentBuilder.targetKey]: networkAccessEndowmentBuilder,\n [longRunningEndowmentBuilder.targetKey]: longRunningEndowmentBuilder,\n [transactionInsightEndowmentBuilder.targetKey]:\n transactionInsightEndowmentBuilder,\n [keyringEndowmentBuilder.targetKey]: keyringEndowmentBuilder,\n [cronjobEndowmentBuilder.targetKey]: cronjobEndowmentBuilder,\n [ethereumProviderEndowmentBuilder.targetKey]:\n ethereumProviderEndowmentBuilder,\n [rpcEndowmentBuilder.targetKey]: rpcEndowmentBuilder,\n [webAssemblyEndowmentBuilder.targetKey]: webAssemblyEndowmentBuilder,\n} as const;\n\nexport const endowmentCaveatSpecifications = {\n ...keyringCaveatSpecifications,\n ...cronjobCaveatSpecifications,\n ...transactionInsightCaveatSpecifications,\n ...rpcCaveatSpecifications,\n};\n\nexport const endowmentCaveatMappers: Record<\n string,\n (value: Json) => Pick<PermissionConstraint, 'caveats'>\n> = {\n [keyringEndowmentBuilder.targetKey]: getKeyringCaveatMapper,\n [cronjobEndowmentBuilder.targetKey]: getCronjobCaveatMapper,\n [transactionInsightEndowmentBuilder.targetKey]:\n getTransactionInsightCaveatMapper,\n [rpcEndowmentBuilder.targetKey]: getRpcCaveatMapper,\n};\n\nexport const handlerEndowments: Record<HandlerType, string> = {\n [HandlerType.OnRpcRequest]: rpcEndowmentBuilder.targetKey,\n [HandlerType.SnapKeyring]: keyringEndowmentBuilder.targetKey,\n [HandlerType.OnTransaction]: transactionInsightEndowmentBuilder.targetKey,\n [HandlerType.OnCronjob]: cronjobEndowmentBuilder.targetKey,\n};\n\nexport * from './enum';\nexport { getTransactionOriginCaveat } from './transaction-insight';\n"]}
@@ -19,7 +19,7 @@ const specificationBuilder = () => {
19
19
  allowedCaveats: [snaps_utils_1.SnapCaveatType.SnapKeyring],
20
20
  endowmentGetter: (_getterOptions) => undefined,
21
21
  validator: ({ caveats }) => {
22
- if ((caveats === null || caveats === void 0 ? void 0 : caveats.length) !== 1 ||
22
+ if (caveats?.length !== 1 ||
23
23
  caveats[0].type !== snaps_utils_1.SnapCaveatType.SnapKeyring) {
24
24
  throw eth_rpc_errors_1.ethErrors.rpc.invalidParams({
25
25
  message: `Expected a single "${snaps_utils_1.SnapCaveatType.SnapKeyring}" caveat.`,
@@ -84,14 +84,13 @@ exports.getKeyringCaveatMapper = getKeyringCaveatMapper;
84
84
  * keyring caveat.
85
85
  */
86
86
  function getKeyringCaveatNamespaces(permission) {
87
- var _a, _b;
88
- if (!(permission === null || permission === void 0 ? void 0 : permission.caveats)) {
87
+ if (!permission?.caveats) {
89
88
  return null;
90
89
  }
91
90
  (0, utils_1.assert)(permission.caveats.length === 1);
92
91
  (0, utils_1.assert)(permission.caveats[0].type === snaps_utils_1.SnapCaveatType.SnapKeyring);
93
92
  const caveat = permission.caveats[0];
94
- return (_b = (_a = caveat.value) === null || _a === void 0 ? void 0 : _a.namespaces) !== null && _b !== void 0 ? _b : null;
93
+ return caveat.value?.namespaces ?? null;
95
94
  }
96
95
  exports.getKeyringCaveatNamespaces = getKeyringCaveatNamespaces;
97
96
  exports.keyringCaveatSpecifications = {
@@ -1 +1 @@
1
- {"version":3,"file":"keyring.js","sourceRoot":"","sources":["../../../src/snaps/endowments/keyring.ts"],"names":[],"mappings":";;;AAAA,2EASyC;AACzC,uDAI+B;AAC/B,2CAMyB;AACzB,mDAA2C;AAE3C,iCAAwC;AAExC,MAAM,SAAS,GAAG,qBAAc,CAAC,OAAO,CAAC;AAczC;;;;GAIG;AACH,MAAM,oBAAoB,GAItB,GAAyB,EAAE;IAC7B,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,SAAS;QACT,cAAc,EAAE,CAAC,4BAAc,CAAC,WAAW,CAAC;QAC5C,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,SAAS;QACtE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,MAAK,CAAC;gBACrB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,WAAW,EAC9C;gBACA,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;oBAChC,OAAO,EAAE,sBAAsB,4BAAc,CAAC,WAAW,WAAW;iBACrE,CAAC,CAAC;aACJ;QACH,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAEW,QAAA,uBAAuB,GAAG,MAAM,CAAC,MAAM,CAAC;IACnD,SAAS;IACT,oBAAoB;CACZ,CAAC,CAAC;AAEZ;;;;;;GAMG;AACH,SAAS,uBAAuB,CAAC,MAA2B;IAC1D,IAAI,CAAC,IAAA,mBAAW,EAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,IAAA,qBAAa,EAAC,MAAM,CAAC,KAAK,CAAC,EAAE;QACjE,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;YAChC,OAAO,EAAE,0BAA0B;SACpC,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EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,uBAAuB,CAAC,MAAM,CAAC;KAC5E,CAAC;CACH,CAAC","sourcesContent":["import {\n Caveat,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionType,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport {\n assertIsNamespacesObject,\n Namespaces,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport {\n hasProperty,\n isPlainObject,\n Json,\n NonEmptyArray,\n assert,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst targetKey = SnapEndowments.Keyring;\n\ntype KeyringSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof targetKey;\n endowmentGetter: (_options?: any) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\ntype KeyringSpecificationBuilderOptions = {\n // Empty for now.\n};\n\n/**\n * The specification builder for the keyring endowment permission.\n *\n * @returns The specification for the keyring endowment permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n KeyringSpecificationBuilderOptions,\n KeyringSpecification\n> = (): KeyringSpecification => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey,\n allowedCaveats: [SnapCaveatType.SnapKeyring],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.SnapKeyring\n ) {\n throw ethErrors.rpc.invalidParams({\n message: `Expected a single \"${SnapCaveatType.SnapKeyring}\" caveat.`,\n });\n }\n },\n };\n};\n\nexport const keyringEndowmentBuilder = Object.freeze({\n targetKey,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the namespaces value of a caveat. This does not validate the type or\n * value of the caveat itself, only the value of the namespaces object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatNamespace(caveat: Caveat<string, any>): void {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n if (!hasProperty(value, 'namespaces') || !isPlainObject(value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n assertIsNamespacesObject(value.namespaces, ethErrors.rpc.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getKeyringCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapKeyring,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the keyring namespaces from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The keyring namespaces, or `null` if the permission does not have a\n * keyring caveat.\n */\nexport function getKeyringCaveatNamespaces(\n permission?: PermissionConstraint,\n): Namespaces | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.SnapKeyring);\n\n const caveat = permission.caveats[0] as Caveat<\n string,\n { namespaces: Namespaces }\n >;\n\n return caveat.value?.namespaces ?? null;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.SnapKeyring,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapKeyring]: Object.freeze({\n type: SnapCaveatType.SnapKeyring,\n validator: (caveat: Caveat<string, any>) => validateCaveatNamespace(caveat),\n }),\n};\n"]}
1
+ {"version":3,"file":"keyring.js","sourceR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AAa,EAAC,KAAK,CAAC,EAAE;QAC9D,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;YAChC,OAAO,EAAE,0BAA0B;SACpC,CAAC,CAAC;KACJ;IAED,IAAA,sCAAwB,EAAC,KAAK,CAAC,UAAU,EAAE,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AAC1E,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAAW;IAEX,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,4BAAc,CAAC,WAAW;gBAChC,KAAK;aACN;SACF;KACF,CAAC;AACJ,CAAC;AAXD,wDAWC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,0BAA0B,CACxC,UAAiC;IAEjC,IAAI,CAAC,UAAU,EAAE,OAAO,EAAE;QACxB,OAAO,IAAI,CAAC;KACb;IAED,IAAA,cAAM,EAAC,UAAU,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;IACxC,IAAA,cAAM,EAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,WAAW,CAAC,CAAC;IAElE,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,CAGlC,CAAC;IAEF,OAAO,MAAM,CAAC,KAAK,EAAE,UAAU,IAAI,IAAI,CAAC;AAC1C,CAAC;AAhBD,gEAgBC;AAEY,QAAA,2BAA2B,GAGpC;IACF,CAAC,4BAAc,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC1C,IAAI,EAAE,4BAAc,CAAC,WAAW;QAChC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,uBAAuB,CAAC,MAAM,CAAC;KAC5E,CAAC;CACH,CAAC","sourcesContent":["import {\n Caveat,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionType,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport {\n assertIsNamespacesObject,\n Namespaces,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport {\n hasProperty,\n isPlainObject,\n Json,\n NonEmptyArray,\n assert,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst targetKey = SnapEndowments.Keyring;\n\ntype KeyringSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof targetKey;\n endowmentGetter: (_options?: any) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\ntype KeyringSpecificationBuilderOptions = {\n // Empty for now.\n};\n\n/**\n * The specification builder for the keyring endowment permission.\n *\n * @returns The specification for the keyring endowment permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n KeyringSpecificationBuilderOptions,\n KeyringSpecification\n> = (): KeyringSpecification => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey,\n allowedCaveats: [SnapCaveatType.SnapKeyring],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.SnapKeyring\n ) {\n throw ethErrors.rpc.invalidParams({\n message: `Expected a single \"${SnapCaveatType.SnapKeyring}\" caveat.`,\n });\n }\n },\n };\n};\n\nexport const keyringEndowmentBuilder = Object.freeze({\n targetKey,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the namespaces value of a caveat. This does not validate the type or\n * value of the caveat itself, only the value of the namespaces object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatNamespace(caveat: Caveat<string, any>): void {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n if (!hasProperty(value, 'namespaces') || !isPlainObject(value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n assertIsNamespacesObject(value.namespaces, ethErrors.rpc.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getKeyringCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapKeyring,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the keyring namespaces from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The keyring namespaces, or `null` if the permission does not have a\n * keyring caveat.\n */\nexport function getKeyringCaveatNamespaces(\n permission?: PermissionConstraint,\n): Namespaces | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.SnapKeyring);\n\n const caveat = permission.caveats[0] as Caveat<\n string,\n { namespaces: Namespaces }\n >;\n\n return caveat.value?.namespaces ?? null;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.SnapKeyring,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapKeyring]: Object.freeze({\n type: SnapCaveatType.SnapKeyring,\n validator: (caveat: Caveat<string, any>) => validateCaveatNamespace(caveat),\n }),\n};\n"]}
@@ -19,7 +19,7 @@ const specificationBuilder = () => {
19
19
  allowedCaveats: [snaps_utils_1.SnapCaveatType.RpcOrigin],
20
20
  endowmentGetter: (_getterOptions) => undefined,
21
21
  validator: ({ caveats }) => {
22
- if ((caveats === null || caveats === void 0 ? void 0 : caveats.length) !== 1 ||
22
+ if (caveats?.length !== 1 ||
23
23
  caveats[0].type !== snaps_utils_1.SnapCaveatType.RpcOrigin) {
24
24
  throw eth_rpc_errors_1.ethErrors.rpc.invalidParams({
25
25
  message: `Expected a single "${snaps_utils_1.SnapCaveatType.RpcOrigin}" caveat.`,
@@ -75,7 +75,7 @@ exports.getRpcCaveatMapper = getRpcCaveatMapper;
75
75
  * @throws If the permission does not have a valid {@link RpcOrigins} caveat.
76
76
  */
77
77
  function getRpcCaveatOrigins(permission) {
78
- (0, utils_1.assert)(permission === null || permission === void 0 ? void 0 : permission.caveats);
78
+ (0, utils_1.assert)(permission?.caveats);
79
79
  (0, utils_1.assert)(permission.caveats.length === 1);
80
80
  (0, utils_1.assert)(permission.caveats[0].type === snaps_utils_1.SnapCaveatType.RpcOrigin);
81
81
  const caveat = permission.caveats[0];
@@ -1 +1 @@
1
- {"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../../src/snaps/endowments/rpc.ts"],"names":[],"mappings":";;;AAAA,2EASyC;AACzC,uDAI+B;AAC/B,2CAMyB;AACzB,mDAA2C;AAE3C,iCAAwC;AAExC,MAAM,SAAS,GAAG,qBAAc,CAAC,GAAG,CAAC;AAcrC;;;;GAIG;AACH,MAAM,oBAAoB,GAItB,GAAqB,EAAE;IACzB,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,SAAS;QACT,cAAc,EAAE,CAAC,4BAAc,CAAC,SAAS,CAAC;QAC1C,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,SAAS;QACtE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,MAAK,CAAC;gBACrB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,SAAS,EAC5C;gBACA,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;oBAChC,OAAO,EAAE,sBAAsB,4BAAc,CAAC,SAAS,WAAW;iBACnE,CAAC,CA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Ac,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QACxC,IAAI,EAAE,4BAAc,CAAC,SAAS;QAC9B,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KAC1E,CAAC;CACH,CAAC","sourcesContent":["import {\n Caveat,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionType,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport {\n assertIsRpcOrigins,\n RpcOrigins,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport {\n hasProperty,\n isPlainObject,\n Json,\n NonEmptyArray,\n assert,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst targetKey = SnapEndowments.Rpc;\n\ntype RpcSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof targetKey;\n endowmentGetter: (_options?: any) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\ntype RpcSpecificationBuilderOptions = {\n // Empty for now.\n};\n\n/**\n * The specification builder for the JSON-RPC endowment permission.\n *\n * @returns The specification for the JSON-RPC endowment permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n RpcSpecificationBuilderOptions,\n RpcSpecification\n> = (): RpcSpecification => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey,\n allowedCaveats: [SnapCaveatType.RpcOrigin],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.RpcOrigin\n ) {\n throw ethErrors.rpc.invalidParams({\n message: `Expected a single \"${SnapCaveatType.RpcOrigin}\" caveat.`,\n });\n }\n },\n };\n};\n\nexport const rpcEndowmentBuilder = Object.freeze({\n targetKey,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a caveat. This does not validate the type of the\n * caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Invalid JSON-RPC origins: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsRpcOrigins(value, ethErrors.rpc.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getRpcCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.RpcOrigin,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the {@link RpcOrigins} caveat value from a permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n * @throws If the permission does not have a valid {@link RpcOrigins} caveat.\n */\nexport function getRpcCaveatOrigins(\n permission?: PermissionConstraint,\n): RpcOrigins | null {\n assert(permission?.caveats);\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.RpcOrigin);\n\n const caveat = permission.caveats[0] as Caveat<string, RpcOrigins>;\n return caveat.value;\n}\n\nexport const rpcCaveatSpecifications: Record<\n SnapCaveatType.RpcOrigin,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.RpcOrigin]: Object.freeze({\n type: SnapCaveatType.RpcOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n};\n"]}
1
+ {"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../../src/snaps/endowments/rpc.ts"],"names":[],"mappings":";;;AAAA,2EASyC;AACzC,uDAI+B;AAC/B,2CAMyB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P;gBACE,IAAI,EAAE,4BAAc,CAAC,SAAS;gBAC9B,KAAK;aACN;SACF;KACF,CAAC;AACJ,CAAC;AAXD,gDAWC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,UAAiC;IAEjC,IAAA,cAAM,EAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAC5B,IAAA,cAAM,EAAC,UAAU,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;IACxC,IAAA,cAAM,EAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,SAAS,CAAC,CAAC;IAEhE,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,CAA+B,CAAC;IACnE,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB,CAAC;AATD,kDASC;AAEY,QAAA,uBAAuB,GAGhC;IACF,CAAC,4BAAc,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QACxC,IAAI,EAAE,4BAAc,CAAC,SAAS;QAC9B,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KAC1E,CAAC;CACH,CAAC","sourcesContent":["import {\n Caveat,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionType,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport {\n assertIsRpcOrigins,\n RpcOrigins,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport {\n hasProperty,\n isPlainObject,\n Json,\n NonEmptyArray,\n assert,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst targetKey = SnapEndowments.Rpc;\n\ntype RpcSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof targetKey;\n endowmentGetter: (_options?: any) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\ntype RpcSpecificationBuilderOptions = {\n // Empty for now.\n};\n\n/**\n * The specification builder for the JSON-RPC endowment permission.\n *\n * @returns The specification for the JSON-RPC endowment permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n RpcSpecificationBuilderOptions,\n RpcSpecification\n> = (): RpcSpecification => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey,\n allowedCaveats: [SnapCaveatType.RpcOrigin],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.RpcOrigin\n ) {\n throw ethErrors.rpc.invalidParams({\n message: `Expected a single \"${SnapCaveatType.RpcOrigin}\" caveat.`,\n });\n }\n },\n };\n};\n\nexport const rpcEndowmentBuilder = Object.freeze({\n targetKey,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a caveat. This does not validate the type of the\n * caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Invalid JSON-RPC origins: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsRpcOrigins(value, ethErrors.rpc.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getRpcCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.RpcOrigin,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the {@link RpcOrigins} caveat value from a permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n * @throws If the permission does not have a valid {@link RpcOrigins} caveat.\n */\nexport function getRpcCaveatOrigins(\n permission?: PermissionConstraint,\n): RpcOrigins | null {\n assert(permission?.caveats);\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.RpcOrigin);\n\n const caveat = permission.caveats[0] as Caveat<string, RpcOrigins>;\n return caveat.value;\n}\n\nexport const rpcCaveatSpecifications: Record<\n SnapCaveatType.RpcOrigin,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.RpcOrigin]: Object.freeze({\n type: SnapCaveatType.RpcOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n};\n"]}
@@ -21,8 +21,8 @@ const specificationBuilder = (_builderOptions) => {
21
21
  allowedCaveats: [snaps_utils_1.SnapCaveatType.TransactionOrigin],
22
22
  endowmentGetter: (_getterOptions) => undefined,
23
23
  validator: ({ caveats }) => {
24
- if ((caveats !== null && (caveats === null || caveats === void 0 ? void 0 : caveats.length) > 1) ||
25
- ((caveats === null || caveats === void 0 ? void 0 : caveats.length) === 1 &&
24
+ if ((caveats !== null && caveats?.length > 1) ||
25
+ (caveats?.length === 1 &&
26
26
  caveats[0].type !== snaps_utils_1.SnapCaveatType.TransactionOrigin)) {
27
27
  throw eth_rpc_errors_1.ethErrors.rpc.invalidParams({
28
28
  message: `Expected a single "${snaps_utils_1.SnapCaveatType.TransactionOrigin}" caveat.`,
@@ -87,14 +87,13 @@ exports.getTransactionInsightCaveatMapper = getTransactionInsightCaveatMapper;
87
87
  * transaction origin caveat.
88
88
  */
89
89
  function getTransactionOriginCaveat(permission) {
90
- var _a;
91
- if (!(permission === null || permission === void 0 ? void 0 : permission.caveats)) {
90
+ if (!permission?.caveats) {
92
91
  return null;
93
92
  }
94
93
  (0, utils_1.assert)(permission.caveats.length === 1);
95
94
  (0, utils_1.assert)(permission.caveats[0].type === snaps_utils_1.SnapCaveatType.TransactionOrigin);
96
95
  const caveat = permission.caveats[0];
97
- return (_a = caveat.value) !== null && _a !== void 0 ? _a : null;
96
+ return caveat.value ?? null;
98
97
  }
99
98
  exports.getTransactionOriginCaveat = getTransactionOriginCaveat;
100
99
  exports.transactionInsightCaveatSpecifications = {
@@ -1 +1 @@
1
- {"version":3,"file":"transaction-insight.js","sourceRoot":"","sources":["../../../src/snaps/endowments/transaction-insight.ts"],"names":[],"mappings":";;;AAAA,2EASyC;AACzC,uDAAuD;AACvD,2CAOyB;AACzB,mDAA2C;AAE3C,iCAAwC;AAExC,MAAM,cAAc,GAAG,qBAAc,CAAC,kBAAkB,CAAC;AAUzD;;;;;;GAMG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAyB,EAAE,EAAE;IAChC,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,SAAS,EAAE,cAAc;QACzB,cAAc,EAAE,CAAC,4BAAc,CAAC,iBAAiB,CAAC;QAClD,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,SAAS;QACtE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,CAAC,OAAO,KAAK,IAAI,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,IAAG,CAAC,CAAC;gBACzC,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,MAAK,CAAC;oBACpB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,iBAAiB,CAAC,EACvD;gBACA,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;oBAChC,OAAO,EAAE,sBAAsB,4BAAc,CAAC,iBAAiB,WAAW;iBAC3E,CAAC,CAAC;aACJ;QACH,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAEW,QAAA,kCAAkC,GAAG,MAAM,CAAC,MAAM,CAAC;IAC9D,SAAS,EAAE,cAAc;IACzB,oBAAoB;CACZ,CAAC,CAAC;AAE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AAC,iBAAiB,CAAC,CAAC;IAExE,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,CAA4B,CAAC;IAEhE,OAAO,MAAA,MAAM,CAAC,KAAK,mCAAI,IAAI,CAAC;AAC9B,CAAC;AAbD,gEAaC;AAEY,QAAA,sCAAsC,GAG/C;IACF,CAAC,4BAAc,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAChD,IAAI,EAAE,4BAAc,CAAC,iBAAiB;QACtC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,cAAc,CAAC,MAAM,CAAC;KACnE,CAAC;CACH,CAAC","sourcesContent":["import {\n PermissionSpecificationBuilder,\n PermissionType,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionConstraint,\n CaveatSpecificationConstraint,\n Caveat,\n} from '@metamask/permission-controller';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport {\n assert,\n hasProperty,\n isObject,\n isPlainObject,\n Json,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.TransactionInsight;\n\ntype TransactionInsightEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * `endowment:transaction-insight` returns nothing; it is intended to be used as a flag\n * by the extension to detect whether the snap has the capability to show information on the transaction confirmation screen.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the transaction-insight endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n TransactionInsightEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey: permissionName,\n allowedCaveats: [SnapCaveatType.TransactionOrigin],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: ({ caveats }) => {\n if (\n (caveats !== null && caveats?.length > 1) ||\n (caveats?.length === 1 &&\n caveats[0].type !== SnapCaveatType.TransactionOrigin)\n ) {\n throw ethErrors.rpc.invalidParams({\n message: `Expected a single \"${SnapCaveatType.TransactionOrigin}\" caveat.`,\n });\n }\n },\n };\n};\n\nexport const transactionInsightEndowmentBuilder = Object.freeze({\n targetKey: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validates the type of the caveat value.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveat(caveat: Caveat<string, any>): void {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n assert(\n typeof value === 'boolean',\n 'Expected caveat value to have type \"boolean\"',\n );\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getTransactionInsightCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (\n !value ||\n !isObject(value) ||\n (isObject(value) && Object.keys(value).length === 0)\n ) {\n return { caveats: null };\n }\n return {\n caveats: [\n {\n type: SnapCaveatType.TransactionOrigin,\n value:\n hasProperty(value, 'allowTransactionOrigin') &&\n value.allowTransactionOrigin,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the transaction origin caveat from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the transaction origin caveat from.\n * @returns The transaction origin, or `null` if the permission does not have a\n * transaction origin caveat.\n */\nexport function getTransactionOriginCaveat(\n permission?: PermissionConstraint,\n): boolean | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.TransactionOrigin);\n\n const caveat = permission.caveats[0] as Caveat<string, boolean>;\n\n return caveat.value ?? null;\n}\n\nexport const transactionInsightCaveatSpecifications: Record<\n SnapCaveatType.TransactionOrigin,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.TransactionOrigin]: Object.freeze({\n type: SnapCaveatType.TransactionOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),\n }),\n};\n"]}
1
+ {"version":3,"file":"transaction-insight.js","sourceRoot":"","sources":["../../../src/snaps/endowments/transaction-insight.ts"],"names":[],"mappings":";;;AAAA,2EASyC;AACzC,uDAAuD;AACvD,2CAOyB;AACzB,mDAA2C;AAE3C,iCAAwC;AAExC,MAAM,cAAc,GAAG,qBAAc,CAAC,kBAAkB,CAAC;AAUzD;;;;;;GAMG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAyB,EAAE,EAAE;IAChC,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,SAAS,EAAE,cAAc;QACzB,cAAc,EAAE,CAAC,4BAAc,CAAC,iBAAiB,CAAC;QAClD,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,SAAS;QACtE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,CAAC,OAAO,KAAK,IAAI,IAAI,OAAO,EAAE,MAAM,GAAG,CAAC,CAAC;gBACzC,CAAC,OAAO,EAAE,MAAM,KAAK,CAAC;oBACpB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,iBAAiB,CAAC,EACvD;gBACA,MAAM,0BAAS,CAAC,GAAG,CAAC,aAAa,CAAC;oBAChC,OAAO,EAAE,sBAAsB,4BAAc,CAAC,iBAAiB,WAAW;iBAC3E,CAAC,CAAC;aACJ;QACH,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;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AAM,EAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,iBAAiB,CAAC,CAAC;IAExE,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,CAA4B,CAAC;IAEhE,OAAO,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC;AAC9B,CAAC;AAbD,gEAaC;AAEY,QAAA,sCAAsC,GAG/C;IACF,CAAC,4BAAc,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAChD,IAAI,EAAE,4BAAc,CAAC,iBAAiB;QACtC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,cAAc,CAAC,MAAM,CAAC;KACnE,CAAC;CACH,CAAC","sourcesContent":["import {\n PermissionSpecificationBuilder,\n PermissionType,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionConstraint,\n CaveatSpecificationConstraint,\n Caveat,\n} from '@metamask/permission-controller';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport {\n assert,\n hasProperty,\n isObject,\n isPlainObject,\n Json,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { ethErrors } from 'eth-rpc-errors';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.TransactionInsight;\n\ntype TransactionInsightEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => undefined;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * `endowment:transaction-insight` returns nothing; it is intended to be used as a flag\n * by the extension to detect whether the snap has the capability to show information on the transaction confirmation screen.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the transaction-insight endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n TransactionInsightEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey: permissionName,\n allowedCaveats: [SnapCaveatType.TransactionOrigin],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,\n validator: ({ caveats }) => {\n if (\n (caveats !== null && caveats?.length > 1) ||\n (caveats?.length === 1 &&\n caveats[0].type !== SnapCaveatType.TransactionOrigin)\n ) {\n throw ethErrors.rpc.invalidParams({\n message: `Expected a single \"${SnapCaveatType.TransactionOrigin}\" caveat.`,\n });\n }\n },\n };\n};\n\nexport const transactionInsightEndowmentBuilder = Object.freeze({\n targetKey: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validates the type of the caveat value.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveat(caveat: Caveat<string, any>): void {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat)) {\n throw ethErrors.rpc.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n assert(\n typeof value === 'boolean',\n 'Expected caveat value to have type \"boolean\"',\n );\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getTransactionInsightCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (\n !value ||\n !isObject(value) ||\n (isObject(value) && Object.keys(value).length === 0)\n ) {\n return { caveats: null };\n }\n return {\n caveats: [\n {\n type: SnapCaveatType.TransactionOrigin,\n value:\n hasProperty(value, 'allowTransactionOrigin') &&\n (value.allowTransactionOrigin as boolean),\n },\n ],\n };\n}\n\n/**\n * Getter function to get the transaction origin caveat from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the transaction origin caveat from.\n * @returns The transaction origin, or `null` if the permission does not have a\n * transaction origin caveat.\n */\nexport function getTransactionOriginCaveat(\n permission?: PermissionConstraint,\n): boolean | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.TransactionOrigin);\n\n const caveat = permission.caveats[0] as Caveat<string, boolean>;\n\n return caveat.value ?? null;\n}\n\nexport const transactionInsightCaveatSpecifications: Record<\n SnapCaveatType.TransactionOrigin,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.TransactionOrigin]: Object.freeze({\n type: SnapCaveatType.TransactionOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),\n }),\n};\n"]}
@@ -0,0 +1,13 @@
1
+ import { PermissionSpecificationBuilder, PermissionType } from '@metamask/permission-controller';
2
+ import { SnapEndowments } from './enum';
3
+ declare const permissionName = SnapEndowments.WebAssemblyAccess;
4
+ export declare const webAssemblyEndowmentBuilder: Readonly<{
5
+ readonly targetKey: SnapEndowments.WebAssemblyAccess;
6
+ readonly specificationBuilder: PermissionSpecificationBuilder<PermissionType.Endowment, any, {
7
+ permissionType: PermissionType.Endowment;
8
+ targetKey: typeof permissionName;
9
+ endowmentGetter: (_options?: any) => ['WebAssembly'];
10
+ allowedCaveats: null;
11
+ }>;
12
+ }>;
13
+ export {};
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.webAssemblyEndowmentBuilder = void 0;
4
+ const permission_controller_1 = require("@metamask/permission-controller");
5
+ const enum_1 = require("./enum");
6
+ const permissionName = enum_1.SnapEndowments.WebAssemblyAccess;
7
+ /**
8
+ * `endowment:webassembly` returns the name of global browser API(s) that
9
+ * enable access to the WebAssembly API.
10
+ * This is intended to populate the endowments of the SES Compartment
11
+ * in which a Snap executes.
12
+ *
13
+ * @param _builderOptions - Optional specification builder options.
14
+ * @returns The specification for the WebAssembly endowment.
15
+ */
16
+ const specificationBuilder = (_builderOptions) => {
17
+ return {
18
+ permissionType: permission_controller_1.PermissionType.Endowment,
19
+ targetKey: permissionName,
20
+ allowedCaveats: null,
21
+ endowmentGetter: (_getterOptions) => {
22
+ return ['WebAssembly'];
23
+ },
24
+ };
25
+ };
26
+ exports.webAssemblyEndowmentBuilder = Object.freeze({
27
+ targetKey: permissionName,
28
+ specificationBuilder,
29
+ });
30
+ //# sourceMappingURL=web-assembly.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-assembly.js","sourceRoot":"","sources":["../../../src/snaps/endowments/web-assembly.ts"],"names":[],"mappings":";;;AAAA,2EAKyC;AAEzC,iCAAwC;AAExC,MAAM,cAAc,GAAG,qBAAc,CAAC,iBAAiB,CAAC;AASxD;;;;;;;;GAQG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAqB,EAAE,EAAE;IAC5B,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,SAAS,EAAE,cAAc;QACzB,cAAc,EAAE,IAAI;QACpB,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE;YAC1D,OAAO,CAAC,aAAa,CAAC,CAAC;QACzB,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAEW,QAAA,2BAA2B,GAAG,MAAM,CAAC,MAAM,CAAC;IACvD,SAAS,EAAE,cAAc;IACzB,oBAAoB;CACZ,CAAC,CAAC","sourcesContent":["import {\n EndowmentGetterParams,\n PermissionSpecificationBuilder,\n PermissionType,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\n\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.WebAssemblyAccess;\n\ntype WebAssemblyEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetKey: typeof permissionName;\n endowmentGetter: (_options?: any) => ['WebAssembly'];\n allowedCaveats: null;\n}>;\n\n/**\n * `endowment:webassembly` returns the name of global browser API(s) that\n * enable access to the WebAssembly API.\n * This is intended to populate the endowments of the SES Compartment\n * in which a Snap executes.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the WebAssembly endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n WebAssemblyEndowmentSpecification\n> = (_builderOptions?: any) => {\n return {\n permissionType: PermissionType.Endowment,\n targetKey: permissionName,\n allowedCaveats: null,\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => {\n return ['WebAssembly'];\n },\n };\n};\n\nexport const webAssemblyEndowmentBuilder = Object.freeze({\n targetKey: permissionName,\n specificationBuilder,\n} as const);\n"]}
@@ -5,7 +5,6 @@ const snaps_utils_1 = require("@metamask/snaps-utils");
5
5
  const utils_1 = require("@metamask/utils");
6
6
  class HttpLocation {
7
7
  constructor(url, opts = {}) {
8
- var _a;
9
8
  // We keep contents separate because then we can use only one Blob in cache,
10
9
  // which we convert to Uint8Array when actually returning the file.
11
10
  //
@@ -14,7 +13,7 @@ class HttpLocation {
14
13
  // in most often case we'll only have one file contents in common case.
15
14
  this.cache = new Map();
16
15
  (0, utils_1.assertStruct)(url.toString(), snaps_utils_1.HttpSnapIdStruct, 'Invalid Snap Id: ');
17
- this.fetchFn = (_a = opts.fetch) !== null && _a !== void 0 ? _a : globalThis.fetch.bind(globalThis);
16
+ this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
18
17
  this.fetchOptions = opts.fetchOptions;
19
18
  this.url = url;
20
19
  }
@@ -24,7 +23,11 @@ class HttpLocation {
24
23
  }
25
24
  // jest-fetch-mock doesn't handle new URL(), we need to convert .toString()
26
25
  const canonicalPath = new URL(snaps_utils_1.NpmSnapFileNames.Manifest, this.url).toString();
27
- const contents = await (await this.fetchFn(canonicalPath, this.fetchOptions)).text();
26
+ const response = await this.fetchFn(canonicalPath, this.fetchOptions);
27
+ if (!response.ok) {
28
+ throw new Error(`Failed to fetch "${canonicalPath}". Status code: ${response.status}.`);
29
+ }
30
+ const contents = await response.text();
28
31
  const manifest = JSON.parse(contents);
29
32
  const vfile = new snaps_utils_1.VirtualFile({
30
33
  value: contents,
@@ -47,6 +50,9 @@ class HttpLocation {
47
50
  }
48
51
  const canonicalPath = this.toCanonical(relativePath).toString();
49
52
  const response = await this.fetchFn(canonicalPath, this.fetchOptions);
53
+ if (!response.ok) {
54
+ throw new Error(`Failed to fetch "${canonicalPath}". Status code: ${response.status}.`);
55
+ }
50
56
  const vfile = new snaps_utils_1.VirtualFile({
51
57
  value: '',
52
58
  path: relativePath,
@@ -1 +1 @@
1
- {"version":3,"file":"http.js","sourceRoot":"","sources":["../../../src/snaps/location/http.ts"],"names":[],"mappings":";;;AAAA,uDAO+B;AAC/B,2CAAuD;AAYvD,MAAa,YAAY;IAoBvB,YAAY,GAAQ,EAAE,OAAoB,EAAE;;QAnB5C,4EAA4E;QAC5E,mEAAmE;QACnE,EAAE;QACF,yCAAyC;QACzC,8DAA8D;QAC9D,uEAAuE;QACtD,UAAK,GAAG,IAAI,GAAG,EAG7B,CAAC;QAWF,IAAA,oBAAY,EAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,8BAAgB,EAAE,mBAAmB,CAAC,CAAC;QACpE,IAAI,CAAC,OAAO,GAAG,MAAA,IAAI,CAAC,KAAK,mCAAI,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC/D,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC;QACtC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,IAAI,IAAI,CAAC,iBAAiB,EAAE;YAC1B,OAAO,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC;SACvC;QAED,2EAA2E;QAC3E,MAAM,aAAa,GAAG,IAAI,GAAG,CAC3B,8BAAgB,CAAC,QAAQ,EACzB,IAAI,CAAC,GAAG,CACT,CAAC,QAAQ,EAAE,CAAC;QAEb,MAAM,QAAQ,GAAG,MAAM,CACrB,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,IAAI,CA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CAAC,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnC,IAAA,cAAM,EACJ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,CAAC,EAC7B,iDAAiD,CAClD,CAAC;QACF,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;QAE9D,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAClC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAEO,WAAW,CAAC,IAAY;QAC9B,IAAA,cAAM,EAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,+BAA+B,CAAC,CAAC;QAC/D,OAAO,IAAI,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;CACF;AAzFD,oCAyFC","sourcesContent":["import {\n SnapManifest,\n VirtualFile,\n HttpSnapIdStruct,\n NpmSnapFileNames,\n createSnapManifest,\n normalizeRelative,\n} from '@metamask/snaps-utils';\nimport { assert, assertStruct } from '@metamask/utils';\n\nimport { SnapLocation } from './location';\n\nexport interface HttpOptions {\n /**\n * @default fetch\n */\n fetch?: typeof fetch;\n fetchOptions?: RequestInit;\n}\n\nexport class HttpLocation implements SnapLocation {\n // We keep contents separate because then we can use only one Blob in cache,\n // which we convert to Uint8Array when actually returning the file.\n //\n // That avoids deepCloning file contents.\n // I imagine ArrayBuffers are copy-on-write optimized, meaning\n // in most often case we'll only have one file contents in common case.\n private readonly cache = new Map<\n string,\n { file: VirtualFile; contents: Blob }\n >();\n\n private validatedManifest?: VirtualFile<SnapManifest>;\n\n private readonly url: URL;\n\n private readonly fetchFn: typeof fetch;\n\n private readonly fetchOptions?: RequestInit;\n\n constructor(url: URL, opts: HttpOptions = {}) {\n assertStruct(url.toString(), HttpSnapIdStruct, 'Invalid Snap Id: ');\n this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);\n this.fetchOptions = opts.fetchOptions;\n this.url = url;\n }\n\n async manifest(): Promise<VirtualFile<SnapManifest>> {\n if (this.validatedManifest) {\n return this.validatedManifest.clone();\n }\n\n // jest-fetch-mock doesn't handle new URL(), we need to convert .toString()\n const canonicalPath = new URL(\n NpmSnapFileNames.Manifest,\n this.url,\n ).toString();\n\n const contents = await (\n await this.fetchFn(canonicalPath, this.fetchOptions)\n ).text();\n const manifest = JSON.parse(contents);\n const vfile = new VirtualFile<SnapManifest>({\n value: contents,\n result: createSnapManifest(manifest),\n path: NpmSnapFileNames.Manifest,\n data: { canonicalPath },\n });\n this.validatedManifest = vfile;\n\n return this.manifest();\n }\n\n async fetch(path: string): Promise<VirtualFile> {\n const relativePath = normalizeRelative(path);\n const cached = this.cache.get(relativePath);\n if (cached !== undefined) {\n const { file, contents } = cached;\n const value = new Uint8Array(await contents.arrayBuffer());\n const vfile = file.clone();\n vfile.value = value;\n return vfile;\n }\n\n const canonicalPath = this.toCanonical(relativePath).toString();\n const response = await this.fetchFn(canonicalPath, this.fetchOptions);\n const vfile = new VirtualFile({\n value: '',\n path: relativePath,\n data: { canonicalPath },\n });\n const blob = await response.blob();\n assert(\n !this.cache.has(relativePath),\n 'Corrupted cache, multiple files with same path.',\n );\n this.cache.set(relativePath, { file: vfile, contents: blob });\n\n return this.fetch(relativePath);\n }\n\n get root(): URL {\n return new URL(this.url);\n }\n\n private toCanonical(path: string): URL {\n assert(!path.startsWith('/'), 'Tried to parse absolute path.');\n return new URL(path, this.url);\n }\n}\n"]}
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../../src/snaps/location/http.ts"],"names":[],"mappings":";;;AAAA,uDAO+B;AAC/B,2CAAuD;AAYvD,MAAa,YAAY;IAoBvB,YAAY,GAAQ,EAAE,OAAoB,EAA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Y,CAAC,EAC7B,iDAAiD,CAClD,CAAC;QACF,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;QAE9D,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAClC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAEO,WAAW,CAAC,IAAY;QAC9B,IAAA,cAAM,EAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,+BAA+B,CAAC,CAAC;QAC/D,OAAO,IAAI,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;CACF;AAlGD,oCAkGC","sourcesContent":["import {\n SnapManifest,\n VirtualFile,\n HttpSnapIdStruct,\n NpmSnapFileNames,\n createSnapManifest,\n normalizeRelative,\n} from '@metamask/snaps-utils';\nimport { assert, assertStruct } from '@metamask/utils';\n\nimport { SnapLocation } from './location';\n\nexport interface HttpOptions {\n /**\n * @default fetch\n */\n fetch?: typeof fetch;\n fetchOptions?: RequestInit;\n}\n\nexport class HttpLocation implements SnapLocation {\n // We keep contents separate because then we can use only one Blob in cache,\n // which we convert to Uint8Array when actually returning the file.\n //\n // That avoids deepCloning file contents.\n // I imagine ArrayBuffers are copy-on-write optimized, meaning\n // in most often case we'll only have one file contents in common case.\n private readonly cache = new Map<\n string,\n { file: VirtualFile; contents: Blob }\n >();\n\n private validatedManifest?: VirtualFile<SnapManifest>;\n\n private readonly url: URL;\n\n private readonly fetchFn: typeof fetch;\n\n private readonly fetchOptions?: RequestInit;\n\n constructor(url: URL, opts: HttpOptions = {}) {\n assertStruct(url.toString(), HttpSnapIdStruct, 'Invalid Snap Id: ');\n this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);\n this.fetchOptions = opts.fetchOptions;\n this.url = url;\n }\n\n async manifest(): Promise<VirtualFile<SnapManifest>> {\n if (this.validatedManifest) {\n return this.validatedManifest.clone();\n }\n\n // jest-fetch-mock doesn't handle new URL(), we need to convert .toString()\n const canonicalPath = new URL(\n NpmSnapFileNames.Manifest,\n this.url,\n ).toString();\n\n const response = await this.fetchFn(canonicalPath, this.fetchOptions);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch \"${canonicalPath}\". Status code: ${response.status}.`,\n );\n }\n const contents = await response.text();\n const manifest = JSON.parse(contents);\n const vfile = new VirtualFile<SnapManifest>({\n value: contents,\n result: createSnapManifest(manifest),\n path: NpmSnapFileNames.Manifest,\n data: { canonicalPath },\n });\n this.validatedManifest = vfile;\n\n return this.manifest();\n }\n\n async fetch(path: string): Promise<VirtualFile> {\n const relativePath = normalizeRelative(path);\n const cached = this.cache.get(relativePath);\n if (cached !== undefined) {\n const { file, contents } = cached;\n const value = new Uint8Array(await contents.arrayBuffer());\n const vfile = file.clone();\n vfile.value = value;\n return vfile;\n }\n\n const canonicalPath = this.toCanonical(relativePath).toString();\n const response = await this.fetchFn(canonicalPath, this.fetchOptions);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch \"${canonicalPath}\". Status code: ${response.status}.`,\n );\n }\n const vfile = new VirtualFile({\n value: '',\n path: relativePath,\n data: { canonicalPath },\n });\n const blob = await response.blob();\n assert(\n !this.cache.has(relativePath),\n 'Corrupted cache, multiple files with same path.',\n );\n this.cache.set(relativePath, { file: vfile, contents: blob });\n\n return this.fetch(relativePath);\n }\n\n get root(): URL {\n return new URL(this.url);\n }\n\n private toCanonical(path: string): URL {\n assert(!path.startsWith('/'), 'Tried to parse absolute path.');\n return new URL(path, this.url);\n }\n}\n"]}
@@ -22,7 +22,7 @@ class LocalLocation {
22
22
  (0, utils_1.assertStruct)(url.toString(), snaps_utils_1.LocalSnapIdStruct, 'Invalid Snap Id');
23
23
  // TODO(ritave): Write deepMerge() which merges fetchOptions.
24
24
  (0, utils_1.assert)(opts.fetchOptions === undefined, 'Currently adding fetch options to local: is unsupported.');
25
- __classPrivateFieldSet(this, _LocalLocation_http, new http_1.HttpLocation(new URL(url.toString().slice(snaps_utils_1.SnapIdPrefixes.local.length)), Object.assign(Object.assign({}, opts), { fetchOptions: { cache: 'no-cache' } })), "f");
25
+ __classPrivateFieldSet(this, _LocalLocation_http, new http_1.HttpLocation(new URL(url.toString().slice(snaps_utils_1.SnapIdPrefixes.local.length)), { ...opts, fetchOptions: { cache: 'no-cache' } }), "f");
26
26
  }
27
27
  async manifest() {
28
28
  const vfile = await __classPrivateFieldGet(this, _LocalLocation_http, "f").manifest();
@@ -1 +1 @@
1
- {"version":3,"file":"local.js","sourceRoot":"","sources":["../../../src/snaps/location/local.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uDAK+B;AAC/B,2CAAuD;AAEvD,iCAAmD;AAGnD,MAAa,aAAa;IAGxB,YAAY,GAAQ,EAAE,OAAoB,EAAE;QAF5C,sCAA6B;QAG3B,IAAA,oBAAY,EAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,+BAAiB,EAAE,iBAAiB,CAAC,CAAC;QACnE,6DAA6D;QAC7D,IAAA,cAAM,EACJ,IAAI,CAAC,YAAY,KAAK,SAAS,EAC/B,0DAA0D,CAC3D,CAAC;QAEF,uBAAA,IAAI,uBAAS,IAAI,mBAAY,CAC3B,IAAI,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,4BAAc,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,kCACrD,IAAI,KAAE,YAAY,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,IAC/C,MAAA,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,MAAM,KAAK,GAAG,MAAM,uBAAA,IAAI,2BAAM,CAAC,QAAQ,EAAE,CAAC;QAE1C,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAAY;QACtB,OAAO,gBAAgB,CAAC,MAAM,uBAAA,IAAI,2BAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,IAAI,kBAAkB;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA9BD,sCA8BC;;AAED;;;;;GAKG;AACH,SAAS,gBAAgB,CACvB,KAA0B;IAE1B,IAAA,cAAM,EAAC,KAAK,CAAC,IAAI,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC;IAC/C,KAAK,CAAC,IAAI,CAAC,aAAa,GAAG,SAAS,KAAK,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;IAC/D,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n LocalSnapIdStruct,\n SnapIdPrefixes,\n SnapManifest,\n VirtualFile,\n} from '@metamask/snaps-utils';\nimport { assert, assertStruct } from '@metamask/utils';\n\nimport { HttpLocation, HttpOptions } from './http';\nimport { SnapLocation } from './location';\n\nexport class LocalLocation implements SnapLocation {\n readonly #http: HttpLocation;\n\n constructor(url: URL, opts: HttpOptions = {}) {\n assertStruct(url.toString(), LocalSnapIdStruct, 'Invalid Snap Id');\n // TODO(ritave): Write deepMerge() which merges fetchOptions.\n assert(\n opts.fetchOptions === undefined,\n 'Currently adding fetch options to local: is unsupported.',\n );\n\n this.#http = new HttpLocation(\n new URL(url.toString().slice(SnapIdPrefixes.local.length)),\n { ...opts, fetchOptions: { cache: 'no-cache' } },\n );\n }\n\n async manifest(): Promise<VirtualFile<SnapManifest>> {\n const vfile = await this.#http.manifest();\n\n return convertCanonical(vfile);\n }\n\n async fetch(path: string): Promise<VirtualFile> {\n return convertCanonical(await this.#http.fetch(path));\n }\n\n get shouldAlwaysReload() {\n return true;\n }\n}\n\n/**\n * Converts vfiles with canonical `http:` paths into `local:` paths.\n *\n * @param vfile - The {@link VirtualFile} to convert.\n * @returns The same object with updated `.data.canonicalPath`.\n */\nfunction convertCanonical<Result>(\n vfile: VirtualFile<Result>,\n): VirtualFile<Result> {\n assert(vfile.data.canonicalPath !== undefined);\n vfile.data.canonicalPath = `local:${vfile.data.canonicalPath}`;\n return vfile;\n}\n"]}
1
+ {"version":3,"file":"local.js","sourceRoot":"","sources":["../../../src/snaps/location/local.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uDAK+B;AAC/B,2CAAuD;AAEvD,iCAAmD;AAGnD,MAAa,aAAa;IAGxB,YAAY,GAAQ,EAAE,OAAoB,EAAE;QAF5C,sCAA6B;QAG3B,IAAA,oBAAY,EAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,+BAAiB,EAAE,iBAAiB,CAAC,CAAC;QACnE,6DAA6D;QAC7D,IAAA,cAAM,EACJ,IAAI,CAAC,YAAY,KAAK,SAAS,EAC/B,0DAA0D,CAC3D,CAAC;QAEF,uBAAA,IAAI,uBAAS,IAAI,mBAAY,CAC3B,IAAI,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,4BAAc,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EAC1D,EAAE,GAAG,IAAI,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE,CACjD,MAAA,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,MAAM,KAAK,GAAG,MAAM,uBAAA,IAAI,2BAAM,CAAC,QAAQ,EAAE,CAAC;QAE1C,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAAY;QACtB,OAAO,gBAAgB,CAAC,MAAM,uBAAA,IAAI,2BAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,IAAI,kBAAkB;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA9BD,sCA8BC;;AAED;;;;;GAKG;AACH,SAAS,gBAAgB,CACvB,KAA0B;IAE1B,IAAA,cAAM,EAAC,KAAK,CAAC,IAAI,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC;IAC/C,KAAK,CAAC,IAAI,CAAC,aAAa,GAAG,SAAS,KAAK,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;IAC/D,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n LocalSnapIdStruct,\n SnapIdPrefixes,\n SnapManifest,\n VirtualFile,\n} from '@metamask/snaps-utils';\nimport { assert, assertStruct } from '@metamask/utils';\n\nimport { HttpLocation, HttpOptions } from './http';\nimport { SnapLocation } from './location';\n\nexport class LocalLocation implements SnapLocation {\n readonly #http: HttpLocation;\n\n constructor(url: URL, opts: HttpOptions = {}) {\n assertStruct(url.toString(), LocalSnapIdStruct, 'Invalid Snap Id');\n // TODO(ritave): Write deepMerge() which merges fetchOptions.\n assert(\n opts.fetchOptions === undefined,\n 'Currently adding fetch options to local: is unsupported.',\n );\n\n this.#http = new HttpLocation(\n new URL(url.toString().slice(SnapIdPrefixes.local.length)),\n { ...opts, fetchOptions: { cache: 'no-cache' } },\n );\n }\n\n async manifest(): Promise<VirtualFile<SnapManifest>> {\n const vfile = await this.#http.manifest();\n\n return convertCanonical(vfile);\n }\n\n async fetch(path: string): Promise<VirtualFile> {\n return convertCanonical(await this.#http.fetch(path));\n }\n\n get shouldAlwaysReload() {\n return true;\n }\n}\n\n/**\n * Converts vfiles with canonical `http:` paths into `local:` paths.\n *\n * @param vfile - The {@link VirtualFile} to convert.\n * @returns The same object with updated `.data.canonicalPath`.\n */\nfunction convertCanonical<Result>(\n vfile: VirtualFile<Result>,\n): VirtualFile<Result> {\n assert(vfile.data.canonicalPath !== undefined);\n vfile.data.canonicalPath = `local:${vfile.data.canonicalPath}`;\n return vfile;\n}\n"]}
@@ -13,9 +13,8 @@ const npm_1 = require("./npm");
13
13
  * @returns SnapLocation based on url.
14
14
  */
15
15
  function detectSnapLocation(location, opts) {
16
- var _a, _b;
17
- const allowHttp = (_a = opts === null || opts === void 0 ? void 0 : opts.allowHttp) !== null && _a !== void 0 ? _a : false;
18
- const allowLocal = (_b = opts === null || opts === void 0 ? void 0 : opts.allowLocal) !== null && _b !== void 0 ? _b : false;
16
+ const allowHttp = opts?.allowHttp ?? false;
17
+ const allowLocal = opts?.allowLocal ?? false;
19
18
  const root = new URL(location);
20
19
  switch (root.protocol) {
21
20
  case 'npm:':
@@ -1 +1 @@
1
- {"version":3,"file":"location.js","sourceRoot":"","sources":["../../../src/snaps/location/location.ts"],"names":[],"mappings":";;;AACA,2CAAyC;AAEzC,iCAAsC;AACtC,mCAAwC;AACxC,+BAAgD;AAsChD;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,QAAsB,EACtB,IAAgC;;IAEhC,MAAM,SAAS,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,SAAS,mCAAI,KAAK,CAAC;IAC3C,MAAM,UAAU,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,UAAU,mCAAI,KAAK,CAAC;IAC7C,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC/B,QAAQ,IAAI,CAAC,QAAQ,EAAE;QACrB,KAAK,MAAM;YACT,OAAO,IAAI,iBAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACrC,KAAK,QAAQ;YACX,IAAA,cAAM,EAAC,UAAU,EAAE,IAAI,SAAS,CAAC,mCAAmC,CAAC,CAAC,CAAC;YACvE,OAAO,IAAI,qBAAa,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACvC,KAAK,OAAO,CAAC;QACb,KAAK,QAAQ;YACX,IAAA,cAAM,EACJ,SAAS,EACT,IAAI,SAAS,CAAC,gDAAgD,CAAC,CAChE,CAAC;YACF,OAAO,IAAI,mBAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACtC;YACE,MAAM,IAAI,SAAS,CACjB,iBAAiB,IAAI,CAAC,QAAQ,2BAA2B,CAC1D,CAAC;KACL;AACH,CAAC;AAzBD,gDAyBC","sourcesContent":["import { SnapManifest, VirtualFile } from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\n\nimport { HttpLocation } from './http';\nimport { LocalLocation } from './local';\nimport { NpmLocation, NpmOptions } from './npm';\n\ndeclare module '@metamask/snaps-utils' {\n interface DataMap {\n /**\n * Fully qualified, canonical path for the file in {@link https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-8.md SIP-8 } URI format.\n */\n canonicalPath: string;\n }\n}\n\nexport interface SnapLocation {\n /**\n * All files are relative to the manifest, except the manifest itself.\n */\n manifest(): Promise<VirtualFile<SnapManifest>>;\n fetch(path: string): Promise<VirtualFile>;\n\n readonly shouldAlwaysReload?: boolean;\n}\n\nexport type DetectSnapLocationOptions = NpmOptions & {\n /**\n * The function used to fetch data.\n *\n * @default globalThis.fetch\n */\n fetch?: typeof fetch;\n /**\n * @default false\n */\n allowHttp?: boolean;\n /**\n * @default false\n */\n allowLocal?: boolean;\n};\n\n/**\n * Auto-magically detects which SnapLocation object to create based on the provided {@link location}.\n *\n * @param location - A {@link https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-8.md SIP-8} uri.\n * @param opts - NPM options and feature flags.\n * @returns SnapLocation based on url.\n */\nexport function detectSnapLocation(\n location: string | URL,\n opts?: DetectSnapLocationOptions,\n): SnapLocation {\n const allowHttp = opts?.allowHttp ?? false;\n const allowLocal = opts?.allowLocal ?? false;\n const root = new URL(location);\n switch (root.protocol) {\n case 'npm:':\n return new NpmLocation(root, opts);\n case 'local:':\n assert(allowLocal, new TypeError('Fetching local snaps is disabled.'));\n return new LocalLocation(root, opts);\n case 'http:':\n case 'https:':\n assert(\n allowHttp,\n new TypeError('Fetching snaps through http/https is disabled.'),\n );\n return new HttpLocation(root, opts);\n default:\n throw new TypeError(\n `Unrecognized \"${root.protocol}\" snap location protocol.`,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"location.js","sourceRoot":"","sources":["../../../src/snaps/location/location.ts"],"names":[],"mappings":";;;AACA,2CAAyC;AAEzC,iCAAsC;AACtC,mCAAwC;AACxC,+BAAgD;AAsChD;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,QAAsB,EACtB,IAAgC;IAEhC,MAAM,SAAS,GAAG,IAAI,EAAE,SAAS,IAAI,KAAK,CAAC;IAC3C,MAAM,UAAU,GAAG,IAAI,EAAE,UAAU,IAAI,KAAK,CAAC;IAC7C,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC/B,QAAQ,IAAI,CAAC,QAAQ,EAAE;QACrB,KAAK,MAAM;YACT,OAAO,IAAI,iBAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACrC,KAAK,QAAQ;YACX,IAAA,cAAM,EAAC,UAAU,EAAE,IAAI,SAAS,CAAC,mCAAmC,CAAC,CAAC,CAAC;YACvE,OAAO,IAAI,qBAAa,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACvC,KAAK,OAAO,CAAC;QACb,KAAK,QAAQ;YACX,IAAA,cAAM,EACJ,SAAS,EACT,IAAI,SAAS,CAAC,gDAAgD,CAAC,CAChE,CAAC;YACF,OAAO,IAAI,mBAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACtC;YACE,MAAM,IAAI,SAAS,CACjB,iBAAiB,IAAI,CAAC,QAAQ,2BAA2B,CAC1D,CAAC;KACL;AACH,CAAC;AAzBD,gDAyBC","sourcesContent":["import { SnapManifest, VirtualFile } from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\n\nimport { HttpLocation } from './http';\nimport { LocalLocation } from './local';\nimport { NpmLocation, NpmOptions } from './npm';\n\ndeclare module '@metamask/snaps-utils' {\n interface DataMap {\n /**\n * Fully qualified, canonical path for the file in {@link https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-8.md SIP-8 } URI format.\n */\n canonicalPath: string;\n }\n}\n\nexport interface SnapLocation {\n /**\n * All files are relative to the manifest, except the manifest itself.\n */\n manifest(): Promise<VirtualFile<SnapManifest>>;\n fetch(path: string): Promise<VirtualFile>;\n\n readonly shouldAlwaysReload?: boolean;\n}\n\nexport type DetectSnapLocationOptions = NpmOptions & {\n /**\n * The function used to fetch data.\n *\n * @default globalThis.fetch\n */\n fetch?: typeof fetch;\n /**\n * @default false\n */\n allowHttp?: boolean;\n /**\n * @default false\n */\n allowLocal?: boolean;\n};\n\n/**\n * Auto-magically detects which SnapLocation object to create based on the provided {@link location}.\n *\n * @param location - A {@link https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-8.md SIP-8} uri.\n * @param opts - NPM options and feature flags.\n * @returns SnapLocation based on url.\n */\nexport function detectSnapLocation(\n location: string | URL,\n opts?: DetectSnapLocationOptions,\n): SnapLocation {\n const allowHttp = opts?.allowHttp ?? false;\n const allowLocal = opts?.allowLocal ?? false;\n const root = new URL(location);\n switch (root.protocol) {\n case 'npm:':\n return new NpmLocation(root, opts);\n case 'local:':\n assert(allowLocal, new TypeError('Fetching local snaps is disabled.'));\n return new LocalLocation(root, opts);\n case 'http:':\n case 'https:':\n assert(\n allowHttp,\n new TypeError('Fetching snaps through http/https is disabled.'),\n );\n return new HttpLocation(root, opts);\n default:\n throw new TypeError(\n `Unrecognized \"${root.protocol}\" snap location protocol.`,\n );\n }\n}\n"]}
@@ -20,11 +20,10 @@ const tar_stream_1 = require("tar-stream");
20
20
  const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org';
21
21
  class NpmLocation {
22
22
  constructor(url, opts = {}) {
23
- var _a, _b, _c;
24
23
  _NpmLocation_instances.add(this);
25
- const allowCustomRegistries = (_a = opts.allowCustomRegistries) !== null && _a !== void 0 ? _a : false;
26
- const fetchFunction = (_b = opts.fetch) !== null && _b !== void 0 ? _b : globalThis.fetch.bind(globalThis);
27
- const requestedRange = (_c = opts.versionRange) !== null && _c !== void 0 ? _c : snaps_utils_1.DEFAULT_REQUESTED_SNAP_VERSION;
24
+ const allowCustomRegistries = opts.allowCustomRegistries ?? false;
25
+ const fetchFunction = opts.fetch ?? globalThis.fetch.bind(globalThis);
26
+ const requestedRange = opts.versionRange ?? snaps_utils_1.DEFAULT_REQUESTED_SNAP_VERSION;
28
27
  (0, utils_1.assertStruct)(url.toString(), snaps_utils_1.NpmSnapIdStruct, 'Invalid Snap Id: ');
29
28
  let registry;
30
29
  if (url.host === '' &&
@@ -138,12 +137,15 @@ const TARBALL_SIZE_SAFETY_LIMIT = 262144000;
138
137
  * actual version of the package.
139
138
  */
140
139
  async function fetchNpmTarball(packageName, versionRange, registryUrl, fetchFunction) {
141
- var _a, _b, _c, _d;
142
- const packageMetadata = await (await fetchFunction(new URL(packageName, registryUrl).toString())).json();
140
+ const packageResponse = await fetchFunction(new URL(packageName, registryUrl).toString());
141
+ if (!packageResponse.ok) {
142
+ throw new Error(`Failed to fetch NPM registry entry. Status code: ${packageResponse.status}.`);
143
+ }
144
+ const packageMetadata = await packageResponse.json();
143
145
  if (!(0, utils_1.isObject)(packageMetadata)) {
144
146
  throw new Error(`Failed to fetch package "${packageName}" metadata from npm.`);
145
147
  }
146
- const versions = Object.keys((_a = packageMetadata === null || packageMetadata === void 0 ? void 0 : packageMetadata.versions) !== null && _a !== void 0 ? _a : {}).map((version) => {
148
+ const versions = Object.keys(packageMetadata?.versions ?? {}).map((version) => {
147
149
  (0, utils_1.assertIsSemVerVersion)(version);
148
150
  return version;
149
151
  });
@@ -151,7 +153,8 @@ async function fetchNpmTarball(packageName, versionRange, registryUrl, fetchFunc
151
153
  if (targetVersion === null) {
152
154
  throw new Error(`Failed to find a matching version in npm metadata for package "${packageName}" and requested semver range "${versionRange}".`);
153
155
  }
154
- const tarballUrlString = (_d = (_c = (_b = packageMetadata === null || packageMetadata === void 0 ? void 0 : packageMetadata.versions) === null || _b === void 0 ? void 0 : _b[targetVersion]) === null || _c === void 0 ? void 0 : _c.dist) === null || _d === void 0 ? void 0 : _d.tarball;
156
+ const tarballUrlString = packageMetadata?.versions?.[targetVersion]
157
+ ?.dist?.tarball;
155
158
  if (!(0, snaps_utils_1.isValidUrl)(tarballUrlString) ||
156
159
  !tarballUrlString.toString().endsWith('.tgz')) {
157
160
  throw new Error(`Failed to find valid tarball URL in NPM metadata for package "${packageName}".`);