@metamask/snaps-rpc-methods 9.1.4 → 10.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/dist/{chunk-KIXPIIY5.mjs → chunk-2RDYC42U.mjs} +2 -2
  3. package/dist/chunk-2RDYC42U.mjs.map +1 -0
  4. package/dist/{chunk-4BPNRCZJ.js → chunk-4PWFZPJC.js} +2 -2
  5. package/dist/chunk-4PWFZPJC.js.map +1 -0
  6. package/dist/{chunk-B53MDMPL.js → chunk-4TUORIA6.js} +6 -6
  7. package/dist/{chunk-IC5BIIRG.js → chunk-6VJVURH5.js} +11 -11
  8. package/dist/{chunk-QKZO7EUA.js → chunk-756OQVOC.js} +2 -2
  9. package/dist/chunk-756OQVOC.js.map +1 -0
  10. package/dist/{chunk-4W7U4OH6.js → chunk-7CV677MM.js} +2 -2
  11. package/dist/chunk-7CV677MM.js.map +1 -0
  12. package/dist/{chunk-3XL562LA.mjs → chunk-7P6TF6CE.mjs} +10 -6
  13. package/dist/chunk-7P6TF6CE.mjs.map +1 -0
  14. package/dist/{chunk-P74WXDY4.js → chunk-A6H6I4VH.js} +9 -2
  15. package/dist/{chunk-P74WXDY4.js.map → chunk-A6H6I4VH.js.map} +1 -1
  16. package/dist/{chunk-QSVAU6X4.js → chunk-ATBKAX2S.js} +3 -3
  17. package/dist/{chunk-43UTAOW7.mjs → chunk-BSAXPL2T.mjs} +3 -3
  18. package/dist/{chunk-I7DR6BFO.js → chunk-ELCOEVKA.js} +65 -20
  19. package/dist/chunk-ELCOEVKA.js.map +1 -0
  20. package/dist/{chunk-NP3KGMVC.js → chunk-EOXTZF5U.js} +2 -2
  21. package/dist/chunk-EOXTZF5U.js.map +1 -0
  22. package/dist/{chunk-R4PZNFIL.mjs → chunk-FJ7COFRJ.mjs} +2 -2
  23. package/dist/chunk-FJ7COFRJ.mjs.map +1 -0
  24. package/dist/{chunk-PCIEMFOB.mjs → chunk-FNUO7MQ4.mjs} +6 -6
  25. package/dist/{chunk-I6FU5MGE.mjs → chunk-GPV4ETUH.mjs} +2 -2
  26. package/dist/chunk-GPV4ETUH.mjs.map +1 -0
  27. package/dist/{chunk-NE55M45Y.js → chunk-H6X3CWWQ.js} +2 -2
  28. package/dist/chunk-H6X3CWWQ.js.map +1 -0
  29. package/dist/{chunk-O2VSIMDY.js → chunk-HZQ34FQT.js} +14 -14
  30. package/dist/{chunk-O2VSIMDY.js.map → chunk-HZQ34FQT.js.map} +1 -1
  31. package/dist/{chunk-OHEJDJNV.mjs → chunk-I2P65KBQ.mjs} +2 -2
  32. package/dist/chunk-I2P65KBQ.mjs.map +1 -0
  33. package/dist/chunk-MC2Z4NF6.mjs +45 -0
  34. package/dist/chunk-MC2Z4NF6.mjs.map +1 -0
  35. package/dist/{chunk-PPR53SPJ.js → chunk-OI33OL7K.js} +2 -2
  36. package/dist/chunk-OI33OL7K.js.map +1 -0
  37. package/dist/{chunk-I3QG3QZL.mjs → chunk-SYB5TG5O.mjs} +10 -10
  38. package/dist/{chunk-I3QG3QZL.mjs.map → chunk-SYB5TG5O.mjs.map} +1 -1
  39. package/dist/{chunk-X247BMMW.mjs → chunk-TQCUJQHY.mjs} +64 -19
  40. package/dist/chunk-TQCUJQHY.mjs.map +1 -0
  41. package/dist/{chunk-TJYMYQAB.mjs → chunk-TVC3E5LI.mjs} +2 -2
  42. package/dist/chunk-TVC3E5LI.mjs.map +1 -0
  43. package/dist/{chunk-D5WQBUZB.js → chunk-U6TJQLNV.js} +2 -2
  44. package/dist/chunk-U6TJQLNV.js.map +1 -0
  45. package/dist/chunk-UCWANXBZ.js +45 -0
  46. package/dist/chunk-UCWANXBZ.js.map +1 -0
  47. package/dist/{chunk-F3VP6B2X.js → chunk-UF3UJTY3.js} +13 -9
  48. package/dist/chunk-UF3UJTY3.js.map +1 -0
  49. package/dist/{chunk-GVJLWGSI.mjs → chunk-UY4DUF53.mjs} +2 -2
  50. package/dist/chunk-UY4DUF53.mjs.map +1 -0
  51. package/dist/{chunk-R3DRYUNH.mjs → chunk-VYII7C3J.mjs} +2 -2
  52. package/dist/chunk-VYII7C3J.mjs.map +1 -0
  53. package/dist/{chunk-WEUKWKAQ.mjs → chunk-XGMYBPQR.mjs} +2 -2
  54. package/dist/chunk-XGMYBPQR.mjs.map +1 -0
  55. package/dist/{chunk-E6WUMFQT.mjs → chunk-Z6YFGWHN.mjs} +9 -2
  56. package/dist/chunk-Z6YFGWHN.mjs.map +1 -0
  57. package/dist/{chunk-IVD2ORDS.js → chunk-ZKISN6YP.js} +2 -2
  58. package/dist/chunk-ZKISN6YP.js.map +1 -0
  59. package/dist/{chunk-T3O2PFSR.mjs → chunk-ZXDCQWMJ.mjs} +2 -2
  60. package/dist/index.js +21 -17
  61. package/dist/index.js.map +1 -1
  62. package/dist/index.mjs +20 -16
  63. package/dist/index.mjs.map +1 -1
  64. package/dist/permissions.js +11 -11
  65. package/dist/permissions.mjs +10 -10
  66. package/dist/permitted/createInterface.js +2 -2
  67. package/dist/permitted/createInterface.mjs +1 -1
  68. package/dist/permitted/getFile.js +2 -2
  69. package/dist/permitted/getFile.mjs +1 -1
  70. package/dist/permitted/getInterfaceState.js +2 -2
  71. package/dist/permitted/getInterfaceState.mjs +1 -1
  72. package/dist/permitted/handlers.js +7 -6
  73. package/dist/permitted/handlers.mjs +6 -5
  74. package/dist/permitted/index.js +8 -7
  75. package/dist/permitted/index.mjs +7 -6
  76. package/dist/permitted/middleware.js +8 -7
  77. package/dist/permitted/middleware.mjs +7 -6
  78. package/dist/permitted/resolveInterface.js +7 -0
  79. package/dist/permitted/resolveInterface.js.map +1 -0
  80. package/dist/permitted/resolveInterface.mjs +7 -0
  81. package/dist/permitted/resolveInterface.mjs.map +1 -0
  82. package/dist/permitted/updateInterface.js +2 -2
  83. package/dist/permitted/updateInterface.mjs +1 -1
  84. package/dist/restricted/caveats/index.js +5 -5
  85. package/dist/restricted/caveats/index.mjs +4 -4
  86. package/dist/restricted/caveats/permittedDerivationPaths.js +2 -2
  87. package/dist/restricted/caveats/permittedDerivationPaths.mjs +1 -1
  88. package/dist/restricted/caveats/snapIds.js +2 -2
  89. package/dist/restricted/caveats/snapIds.mjs +1 -1
  90. package/dist/restricted/dialog.js +4 -2
  91. package/dist/restricted/dialog.mjs +3 -1
  92. package/dist/restricted/getBip32PublicKey.js +2 -2
  93. package/dist/restricted/getBip32PublicKey.mjs +1 -1
  94. package/dist/restricted/getEntropy.js +2 -2
  95. package/dist/restricted/getEntropy.mjs +1 -1
  96. package/dist/restricted/index.js +13 -10
  97. package/dist/restricted/index.mjs +12 -9
  98. package/dist/restricted/manageAccounts.js +2 -2
  99. package/dist/restricted/manageAccounts.mjs +1 -1
  100. package/dist/tsconfig.build.tsbuildinfo +1 -1
  101. package/dist/types/permitted/createInterface.d.ts +4 -4
  102. package/dist/types/permitted/getFile.d.ts +3 -3
  103. package/dist/types/permitted/getInterfaceState.d.ts +2 -2
  104. package/dist/types/permitted/handlers.d.ts +2 -1
  105. package/dist/types/permitted/index.d.ts +2 -1
  106. package/dist/types/permitted/resolveInterface.d.ts +21 -0
  107. package/dist/types/permitted/updateInterface.d.ts +4 -4
  108. package/dist/types/restricted/dialog.d.ts +353 -24
  109. package/dist/types/restricted/getBip32PublicKey.d.ts +4 -4
  110. package/dist/types/restricted/getEntropy.d.ts +4 -4
  111. package/dist/types/restricted/index.d.ts +3 -1
  112. package/dist/types/restricted/manageAccounts.d.ts +1 -1
  113. package/package.json +10 -10
  114. package/dist/chunk-3XL562LA.mjs.map +0 -1
  115. package/dist/chunk-4BPNRCZJ.js.map +0 -1
  116. package/dist/chunk-4W7U4OH6.js.map +0 -1
  117. package/dist/chunk-D5WQBUZB.js.map +0 -1
  118. package/dist/chunk-E6WUMFQT.mjs.map +0 -1
  119. package/dist/chunk-F3VP6B2X.js.map +0 -1
  120. package/dist/chunk-GVJLWGSI.mjs.map +0 -1
  121. package/dist/chunk-I6FU5MGE.mjs.map +0 -1
  122. package/dist/chunk-I7DR6BFO.js.map +0 -1
  123. package/dist/chunk-IVD2ORDS.js.map +0 -1
  124. package/dist/chunk-KIXPIIY5.mjs.map +0 -1
  125. package/dist/chunk-NE55M45Y.js.map +0 -1
  126. package/dist/chunk-NP3KGMVC.js.map +0 -1
  127. package/dist/chunk-OHEJDJNV.mjs.map +0 -1
  128. package/dist/chunk-PPR53SPJ.js.map +0 -1
  129. package/dist/chunk-QKZO7EUA.js.map +0 -1
  130. package/dist/chunk-R3DRYUNH.mjs.map +0 -1
  131. package/dist/chunk-R4PZNFIL.mjs.map +0 -1
  132. package/dist/chunk-TJYMYQAB.mjs.map +0 -1
  133. package/dist/chunk-WEUKWKAQ.mjs.map +0 -1
  134. package/dist/chunk-X247BMMW.mjs.map +0 -1
  135. /package/dist/{chunk-B53MDMPL.js.map → chunk-4TUORIA6.js.map} +0 -0
  136. /package/dist/{chunk-IC5BIIRG.js.map → chunk-6VJVURH5.js.map} +0 -0
  137. /package/dist/{chunk-QSVAU6X4.js.map → chunk-ATBKAX2S.js.map} +0 -0
  138. /package/dist/{chunk-43UTAOW7.mjs.map → chunk-BSAXPL2T.mjs.map} +0 -0
  139. /package/dist/{chunk-PCIEMFOB.mjs.map → chunk-FNUO7MQ4.mjs.map} +0 -0
  140. /package/dist/{chunk-T3O2PFSR.mjs.map → chunk-ZXDCQWMJ.mjs.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/restricted/caveats/snapIds.ts"],"sourcesContent":["import type {\n Caveat,\n RestrictedMethodOptions,\n RestrictedMethodParameters,\n RestrictedMethodCaveatSpecificationConstraint,\n PermissionConstraint,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { SnapIds } from '@metamask/snaps-utils';\nimport { SnapCaveatType, SnapIdsStruct } from '@metamask/snaps-utils';\nimport { type } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { hasProperty, assertStruct } from '@metamask/utils';\n\nimport type { InvokeSnapParams } from '../invokeSnap';\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 snapIdsCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapIds,\n value,\n },\n ],\n };\n}\n\n/**\n * Validates that the caveat value exists and is a non-empty object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat is invalid.\n */\nexport function validateSnapIdsCaveat(\n caveat: Caveat<string, any>,\n): asserts caveat is Caveat<string, SnapIds> {\n assertStruct(\n caveat,\n type({\n value: SnapIdsStruct,\n }),\n 'Expected caveat to have a value property of a non-empty object of snap IDs.',\n rpcErrors.invalidParams,\n );\n}\n\nexport const SnapIdsCaveatSpecification: Record<\n SnapCaveatType.SnapIds,\n RestrictedMethodCaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapIds]: Object.freeze({\n type: SnapCaveatType.SnapIds,\n validator: (caveat) => validateSnapIdsCaveat(caveat),\n decorator: (method, caveat) => {\n return async (args) => {\n const {\n params,\n context: { origin },\n }: RestrictedMethodOptions<RestrictedMethodParameters> = args;\n const snapIds = caveat.value as SnapIds;\n const { snapId } = params as InvokeSnapParams;\n if (!hasProperty(snapIds, snapId)) {\n throw new Error(\n `${origin} does not have permission to invoke ${snapId} snap.`,\n );\n }\n return await method(args);\n };\n },\n }),\n};\n"],"mappings":";AAOA,SAAS,iBAAiB;AAE1B,SAAS,gBAAgB,qBAAqB;AAC9C,SAAS,YAAY;AAErB,SAAS,aAAa,oBAAoB;AAYnC,SAAS,oBACd,OACuC;AACvC,SAAO;AAAA,IACL,SAAS;AAAA,MACP;AAAA,QACE,MAAM,eAAe;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAQO,SAAS,sBACd,QAC2C;AAC3C;AAAA,IACE;AAAA,IACA,KAAK;AAAA,MACH,OAAO;AAAA,IACT,CAAC;AAAA,IACD;AAAA,IACA,UAAU;AAAA,EACZ;AACF;AAEO,IAAM,6BAGT;AAAA,EACF,CAAC,eAAe,OAAO,GAAG,OAAO,OAAO;AAAA,IACtC,MAAM,eAAe;AAAA,IACrB,WAAW,CAAC,WAAW,sBAAsB,MAAM;AAAA,IACnD,WAAW,CAAC,QAAQ,WAAW;AAC7B,aAAO,OAAO,SAAS;AACrB,cAAM;AAAA,UACJ;AAAA,UACA,SAAS,EAAE,OAAO;AAAA,QACpB,IAAyD;AACzD,cAAM,UAAU,OAAO;AACvB,cAAM,EAAE,OAAO,IAAI;AACnB,YAAI,CAAC,YAAY,SAAS,MAAM,GAAG;AACjC,gBAAM,IAAI;AAAA,YACR,GAAG,MAAM,uCAAuC,MAAM;AAAA,UACxD;AAAA,QACF;AACA,eAAO,MAAM,OAAO,IAAI;AAAA,MAC1B;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -5,20 +5,20 @@ import {
5
5
  import {
6
6
  PermittedDerivationPathsCaveatSpecification,
7
7
  permittedDerivationPathsCaveatMapper
8
- } from "./chunk-OHEJDJNV.mjs";
8
+ } from "./chunk-I2P65KBQ.mjs";
9
9
  import {
10
10
  SnapIdsCaveatSpecification,
11
11
  snapIdsCaveatMapper
12
- } from "./chunk-R4PZNFIL.mjs";
12
+ } from "./chunk-FJ7COFRJ.mjs";
13
+ import {
14
+ getBip32PublicKeyBuilder
15
+ } from "./chunk-VYII7C3J.mjs";
13
16
  import {
14
17
  getBip44EntropyBuilder
15
18
  } from "./chunk-LR7UR4YU.mjs";
16
19
  import {
17
20
  getBip32EntropyBuilder
18
21
  } from "./chunk-LXJBBRQ4.mjs";
19
- import {
20
- getBip32PublicKeyBuilder
21
- } from "./chunk-R3DRYUNH.mjs";
22
22
  import {
23
23
  invokeSnapBuilder
24
24
  } from "./chunk-VVBTXSID.mjs";
@@ -40,4 +40,4 @@ export {
40
40
  caveatSpecifications,
41
41
  caveatMappers
42
42
  };
43
- //# sourceMappingURL=chunk-PCIEMFOB.mjs.map
43
+ //# sourceMappingURL=chunk-FNUO7MQ4.mjs.map
@@ -1,8 +1,8 @@
1
1
  // src/permitted/getFile.ts
2
2
  import { rpcErrors } from "@metamask/rpc-errors";
3
3
  import { AuxiliaryFileEncoding, enumValue } from "@metamask/snaps-sdk";
4
+ import { object, optional, string, union } from "@metamask/superstruct";
4
5
  import { assertStruct } from "@metamask/utils";
5
- import { object, optional, string, union } from "superstruct";
6
6
  var GetFileArgsStruct = object({
7
7
  path: string(),
8
8
  encoding: optional(
@@ -44,4 +44,4 @@ export {
44
44
  GetFileArgsStruct,
45
45
  getFileHandler
46
46
  };
47
- //# sourceMappingURL=chunk-I6FU5MGE.mjs.map
47
+ //# sourceMappingURL=chunk-GPV4ETUH.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/permitted/getFile.ts"],"sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { GetFileParams, GetFileResult } from '@metamask/snaps-sdk';\nimport { AuxiliaryFileEncoding, enumValue } from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport { object, optional, string, union } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse, JsonRpcRequest } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nexport const GetFileArgsStruct = object({\n path: string(),\n encoding: optional(\n union([\n enumValue(AuxiliaryFileEncoding.Base64),\n enumValue(AuxiliaryFileEncoding.Hex),\n enumValue(AuxiliaryFileEncoding.Utf8),\n ]),\n ),\n});\n\nexport type InferredGetFileParams = InferMatching<\n typeof GetFileArgsStruct,\n GetFileParams\n>;\n\nconst hookNames: MethodHooksObject<GetFileHooks> = {\n getSnapFile: true,\n};\n\nexport const getFileHandler: PermittedHandlerExport<\n GetFileHooks,\n InferredGetFileParams,\n string\n> = {\n methodNames: ['snap_getFile'],\n implementation,\n hookNames,\n};\n\nexport type GetFileHooks = {\n getSnapFile: (\n path: InferredGetFileParams['path'],\n encoding: InferredGetFileParams['encoding'],\n ) => Promise<string>;\n};\n\n/**\n * The `snap_getFile` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getSnapFile - The function to load a static snap file.\n * @returns Nothing.\n */\nasync function implementation(\n req: JsonRpcRequest<InferredGetFileParams>,\n res: PendingJsonRpcResponse<GetFileResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getSnapFile }: GetFileHooks,\n): Promise<void> {\n const { params } = req;\n\n assertStruct(\n params,\n GetFileArgsStruct,\n 'Invalid \"snap_getFile\" parameters',\n rpcErrors.invalidParams,\n );\n\n try {\n res.result = await getSnapFile(\n params.path,\n params.encoding ?? AuxiliaryFileEncoding.Base64,\n );\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n"],"mappings":";AAEA,SAAS,iBAAiB;AAE1B,SAAS,uBAAuB,iBAAiB;AAEjD,SAAS,QAAQ,UAAU,QAAQ,aAAa;AAEhD,SAAS,oBAAoB;AAItB,IAAM,oBAAoB,OAAO;AAAA,EACtC,MAAM,OAAO;AAAA,EACb,UAAU;AAAA,IACR,MAAM;AAAA,MACJ,UAAU,sBAAsB,MAAM;AAAA,MACtC,UAAU,sBAAsB,GAAG;AAAA,MACnC,UAAU,sBAAsB,IAAI;AAAA,IACtC,CAAC;AAAA,EACH;AACF,CAAC;AAOD,IAAM,YAA6C;AAAA,EACjD,aAAa;AACf;AAEO,IAAM,iBAIT;AAAA,EACF,aAAa,CAAC,cAAc;AAAA,EAC5B;AAAA,EACA;AACF;AAqBA,eAAe,eACb,KACA,KACA,OACA,KACA,EAAE,YAAY,GACC;AACf,QAAM,EAAE,OAAO,IAAI;AAEnB;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA,UAAU;AAAA,EACZ;AAEA,MAAI;AACF,QAAI,SAAS,MAAM;AAAA,MACjB,OAAO;AAAA,MACP,OAAO,YAAY,sBAAsB;AAAA,IAC3C;AAAA,EACF,SAAS,OAAO;AACd,WAAO,IAAI,KAAK;AAAA,EAClB;AAEA,SAAO,IAAI;AACb;","names":[]}
@@ -1,8 +1,8 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/restricted/caveats/snapIds.ts
2
2
  var _rpcerrors = require('@metamask/rpc-errors');
3
3
  var _snapsutils = require('@metamask/snaps-utils');
4
+ var _superstruct = require('@metamask/superstruct');
4
5
  var _utils = require('@metamask/utils');
5
- var _superstruct = require('superstruct');
6
6
  function snapIdsCaveatMapper(value) {
7
7
  return {
8
8
  caveats: [
@@ -51,4 +51,4 @@ var SnapIdsCaveatSpecification = {
51
51
 
52
52
 
53
53
  exports.snapIdsCaveatMapper = snapIdsCaveatMapper; exports.validateSnapIdsCaveat = validateSnapIdsCaveat; exports.SnapIdsCaveatSpecification = SnapIdsCaveatSpecification;
54
- //# sourceMappingURL=chunk-NE55M45Y.js.map
54
+ //# sourceMappingURL=chunk-H6X3CWWQ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/restricted/caveats/snapIds.ts"],"names":[],"mappings":";AAOA,SAAS,iBAAiB;AAE1B,SAAS,gBAAgB,qBAAqB;AAC9C,SAAS,YAAY;AAErB,SAAS,aAAa,oBAAoB;AAYnC,SAAS,oBACd,OACuC;AACvC,SAAO;AAAA,IACL,SAAS;AAAA,MACP;AAAA,QACE,MAAM,eAAe;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAQO,SAAS,sBACd,QAC2C;AAC3C;AAAA,IACE;AAAA,IACA,KAAK;AAAA,MACH,OAAO;AAAA,IACT,CAAC;AAAA,IACD;AAAA,IACA,UAAU;AAAA,EACZ;AACF;AAEO,IAAM,6BAGT;AAAA,EACF,CAAC,eAAe,OAAO,GAAG,OAAO,OAAO;AAAA,IACtC,MAAM,eAAe;AAAA,IACrB,WAAW,CAAC,WAAW,sBAAsB,MAAM;AAAA,IACnD,WAAW,CAAC,QAAQ,WAAW;AAC7B,aAAO,OAAO,SAAS;AACrB,cAAM;AAAA,UACJ;AAAA,UACA,SAAS,EAAE,OAAO;AAAA,QACpB,IAAyD;AACzD,cAAM,UAAU,OAAO;AACvB,cAAM,EAAE,OAAO,IAAI;AACnB,YAAI,CAAC,YAAY,SAAS,MAAM,GAAG;AACjC,gBAAM,IAAI;AAAA,YACR,GAAG,MAAM,uCAAuC,MAAM;AAAA,UACxD;AAAA,QACF;AACA,eAAO,MAAM,OAAO,IAAI;AAAA,MAC1B;AAAA,IACF;AAAA,EACF,CAAC;AACH","sourcesContent":["import type {\n Caveat,\n RestrictedMethodOptions,\n RestrictedMethodParameters,\n RestrictedMethodCaveatSpecificationConstraint,\n PermissionConstraint,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { SnapIds } from '@metamask/snaps-utils';\nimport { SnapCaveatType, SnapIdsStruct } from '@metamask/snaps-utils';\nimport { type } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { hasProperty, assertStruct } from '@metamask/utils';\n\nimport type { InvokeSnapParams } from '../invokeSnap';\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 snapIdsCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapIds,\n value,\n },\n ],\n };\n}\n\n/**\n * Validates that the caveat value exists and is a non-empty object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat is invalid.\n */\nexport function validateSnapIdsCaveat(\n caveat: Caveat<string, any>,\n): asserts caveat is Caveat<string, SnapIds> {\n assertStruct(\n caveat,\n type({\n value: SnapIdsStruct,\n }),\n 'Expected caveat to have a value property of a non-empty object of snap IDs.',\n rpcErrors.invalidParams,\n );\n}\n\nexport const SnapIdsCaveatSpecification: Record<\n SnapCaveatType.SnapIds,\n RestrictedMethodCaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapIds]: Object.freeze({\n type: SnapCaveatType.SnapIds,\n validator: (caveat) => validateSnapIdsCaveat(caveat),\n decorator: (method, caveat) => {\n return async (args) => {\n const {\n params,\n context: { origin },\n }: RestrictedMethodOptions<RestrictedMethodParameters> = args;\n const snapIds = caveat.value as SnapIds;\n const { snapId } = params as InvokeSnapParams;\n if (!hasProperty(snapIds, snapId)) {\n throw new Error(\n `${origin} does not have permission to invoke ${snapId} snap.`,\n );\n }\n return await method(args);\n };\n },\n }),\n};\n"]}
@@ -1,49 +1,49 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkVUA6ICJOjs = require('./chunk-VUA6ICJO.js');
3
+ var _chunkQDZAWH6Fjs = require('./chunk-QDZAWH6F.js');
4
4
 
5
5
 
6
- var _chunkIVD2ORDSjs = require('./chunk-IVD2ORDS.js');
6
+ var _chunk4PWFZPJCjs = require('./chunk-4PWFZPJC.js');
7
7
 
8
8
 
9
- var _chunkSYUGPLEPjs = require('./chunk-SYUGPLEP.js');
9
+ var _chunkVUA6ICJOjs = require('./chunk-VUA6ICJO.js');
10
10
 
11
11
 
12
- var _chunkP74WXDY4js = require('./chunk-P74WXDY4.js');
12
+ var _chunkZKISN6YPjs = require('./chunk-ZKISN6YP.js');
13
13
 
14
14
 
15
- var _chunkM2JQCKR7js = require('./chunk-M2JQCKR7.js');
15
+ var _chunkSYUGPLEPjs = require('./chunk-SYUGPLEP.js');
16
16
 
17
17
 
18
- var _chunkQDZAWH6Fjs = require('./chunk-QDZAWH6F.js');
18
+ var _chunkA6H6I4VHjs = require('./chunk-A6H6I4VH.js');
19
19
 
20
20
 
21
- var _chunkI7DR6BFOjs = require('./chunk-I7DR6BFO.js');
21
+ var _chunkM2JQCKR7js = require('./chunk-M2JQCKR7.js');
22
22
 
23
23
 
24
- var _chunkIE6EHYEGjs = require('./chunk-IE6EHYEG.js');
24
+ var _chunkELCOEVKAjs = require('./chunk-ELCOEVKA.js');
25
25
 
26
26
 
27
- var _chunk4BPNRCZJjs = require('./chunk-4BPNRCZJ.js');
27
+ var _chunkIE6EHYEGjs = require('./chunk-IE6EHYEG.js');
28
28
 
29
29
 
30
30
  var _chunkFFHVA6PPjs = require('./chunk-FFHVA6PP.js');
31
31
 
32
32
  // src/restricted/index.ts
33
33
  var restrictedMethodPermissionBuilders = {
34
- [_chunkI7DR6BFOjs.dialogBuilder.targetName]: _chunkI7DR6BFOjs.dialogBuilder,
34
+ [_chunkELCOEVKAjs.dialogBuilder.targetName]: _chunkELCOEVKAjs.dialogBuilder,
35
35
  [_chunkIE6EHYEGjs.getBip32EntropyBuilder.targetName]: _chunkIE6EHYEGjs.getBip32EntropyBuilder,
36
- [_chunk4BPNRCZJjs.getBip32PublicKeyBuilder.targetName]: _chunk4BPNRCZJjs.getBip32PublicKeyBuilder,
36
+ [_chunk4PWFZPJCjs.getBip32PublicKeyBuilder.targetName]: _chunk4PWFZPJCjs.getBip32PublicKeyBuilder,
37
37
  [_chunkVUA6ICJOjs.getBip44EntropyBuilder.targetName]: _chunkVUA6ICJOjs.getBip44EntropyBuilder,
38
- [_chunkIVD2ORDSjs.getEntropyBuilder.targetName]: _chunkIVD2ORDSjs.getEntropyBuilder,
38
+ [_chunkZKISN6YPjs.getEntropyBuilder.targetName]: _chunkZKISN6YPjs.getEntropyBuilder,
39
39
  [_chunkFFHVA6PPjs.invokeSnapBuilder.targetName]: _chunkFFHVA6PPjs.invokeSnapBuilder,
40
40
  [_chunkM2JQCKR7js.manageStateBuilder.targetName]: _chunkM2JQCKR7js.manageStateBuilder,
41
41
  [_chunkQDZAWH6Fjs.notifyBuilder.targetName]: _chunkQDZAWH6Fjs.notifyBuilder,
42
- [_chunkP74WXDY4js.manageAccountsBuilder.targetName]: _chunkP74WXDY4js.manageAccountsBuilder,
42
+ [_chunkA6H6I4VHjs.manageAccountsBuilder.targetName]: _chunkA6H6I4VHjs.manageAccountsBuilder,
43
43
  [_chunkSYUGPLEPjs.getLocaleBuilder.targetName]: _chunkSYUGPLEPjs.getLocaleBuilder
44
44
  };
45
45
 
46
46
 
47
47
 
48
48
  exports.restrictedMethodPermissionBuilders = restrictedMethodPermissionBuilders;
49
- //# sourceMappingURL=chunk-O2VSIMDY.js.map
49
+ //# sourceMappingURL=chunk-HZQ34FQT.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/restricted/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCO,IAAM,qCAAqC;AAAA,EAChD,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,yBAAyB,UAAU,GAAG;AAAA,EACvC,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,mBAAmB,UAAU,GAAG;AAAA,EACjC,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,sBAAsB,UAAU,GAAG;AAAA,EACpC,CAAC,iBAAiB,UAAU,GAAG;AACjC","sourcesContent":["import type { DialogMethodHooks } from './dialog';\nimport { dialogBuilder } from './dialog';\nimport type { GetBip32EntropyMethodHooks } from './getBip32Entropy';\nimport { getBip32EntropyBuilder } from './getBip32Entropy';\nimport type { GetBip32PublicKeyMethodHooks } from './getBip32PublicKey';\nimport { getBip32PublicKeyBuilder } from './getBip32PublicKey';\nimport type { GetBip44EntropyMethodHooks } from './getBip44Entropy';\nimport { getBip44EntropyBuilder } from './getBip44Entropy';\nimport type { GetEntropyHooks } from './getEntropy';\nimport { getEntropyBuilder } from './getEntropy';\nimport type { GetLocaleMethodHooks } from './getLocale';\nimport { getLocaleBuilder } from './getLocale';\nimport type { InvokeSnapMethodHooks } from './invokeSnap';\nimport { invokeSnapBuilder } from './invokeSnap';\nimport type { ManageAccountsMethodHooks } from './manageAccounts';\nimport { manageAccountsBuilder } from './manageAccounts';\nimport type { ManageStateMethodHooks } from './manageState';\nimport { manageStateBuilder } from './manageState';\nimport type { NotifyMethodHooks } from './notify';\nimport { notifyBuilder } from './notify';\n\nexport { WALLET_SNAP_PERMISSION_KEY } from './invokeSnap';\nexport { getEncryptionEntropy } from './manageState';\n\nexport type RestrictedMethodHooks = DialogMethodHooks &\n GetBip32EntropyMethodHooks &\n GetBip32PublicKeyMethodHooks &\n GetBip44EntropyMethodHooks &\n GetEntropyHooks &\n InvokeSnapMethodHooks &\n ManageStateMethodHooks &\n NotifyMethodHooks &\n ManageAccountsMethodHooks &\n GetLocaleMethodHooks;\n\nexport const restrictedMethodPermissionBuilders = {\n [dialogBuilder.targetName]: dialogBuilder,\n [getBip32EntropyBuilder.targetName]: getBip32EntropyBuilder,\n [getBip32PublicKeyBuilder.targetName]: getBip32PublicKeyBuilder,\n [getBip44EntropyBuilder.targetName]: getBip44EntropyBuilder,\n [getEntropyBuilder.targetName]: getEntropyBuilder,\n [invokeSnapBuilder.targetName]: invokeSnapBuilder,\n [manageStateBuilder.targetName]: manageStateBuilder,\n [notifyBuilder.targetName]: notifyBuilder,\n [manageAccountsBuilder.targetName]: manageAccountsBuilder,\n [getLocaleBuilder.targetName]: getLocaleBuilder,\n} as const;\n\nexport * from './caveats';\n"]}
1
+ {"version":3,"sources":["../src/restricted/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCO,IAAM,qCAAqC;AAAA,EAChD,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,yBAAyB,UAAU,GAAG;AAAA,EACvC,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,mBAAmB,UAAU,GAAG;AAAA,EACjC,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,sBAAsB,UAAU,GAAG;AAAA,EACpC,CAAC,iBAAiB,UAAU,GAAG;AACjC","sourcesContent":["import type { DialogMethodHooks } from './dialog';\nimport { dialogBuilder } from './dialog';\nimport type { GetBip32EntropyMethodHooks } from './getBip32Entropy';\nimport { getBip32EntropyBuilder } from './getBip32Entropy';\nimport type { GetBip32PublicKeyMethodHooks } from './getBip32PublicKey';\nimport { getBip32PublicKeyBuilder } from './getBip32PublicKey';\nimport type { GetBip44EntropyMethodHooks } from './getBip44Entropy';\nimport { getBip44EntropyBuilder } from './getBip44Entropy';\nimport type { GetEntropyHooks } from './getEntropy';\nimport { getEntropyBuilder } from './getEntropy';\nimport type { GetLocaleMethodHooks } from './getLocale';\nimport { getLocaleBuilder } from './getLocale';\nimport type { InvokeSnapMethodHooks } from './invokeSnap';\nimport { invokeSnapBuilder } from './invokeSnap';\nimport type { ManageAccountsMethodHooks } from './manageAccounts';\nimport { manageAccountsBuilder } from './manageAccounts';\nimport type { ManageStateMethodHooks } from './manageState';\nimport { manageStateBuilder } from './manageState';\nimport type { NotifyMethodHooks } from './notify';\nimport { notifyBuilder } from './notify';\n\nexport { WALLET_SNAP_PERMISSION_KEY } from './invokeSnap';\nexport { getEncryptionEntropy } from './manageState';\n\nexport type RestrictedMethodHooks = DialogMethodHooks &\n GetBip32EntropyMethodHooks &\n GetBip32PublicKeyMethodHooks &\n GetBip44EntropyMethodHooks &\n GetEntropyHooks &\n InvokeSnapMethodHooks &\n ManageStateMethodHooks &\n NotifyMethodHooks &\n ManageAccountsMethodHooks &\n GetLocaleMethodHooks;\n\nexport const restrictedMethodPermissionBuilders = {\n [dialogBuilder.targetName]: dialogBuilder,\n [getBip32EntropyBuilder.targetName]: getBip32EntropyBuilder,\n [getBip32PublicKeyBuilder.targetName]: getBip32PublicKeyBuilder,\n [getBip44EntropyBuilder.targetName]: getBip44EntropyBuilder,\n [getEntropyBuilder.targetName]: getEntropyBuilder,\n [invokeSnapBuilder.targetName]: invokeSnapBuilder,\n [manageStateBuilder.targetName]: manageStateBuilder,\n [notifyBuilder.targetName]: notifyBuilder,\n [manageAccountsBuilder.targetName]: manageAccountsBuilder,\n [getLocaleBuilder.targetName]: getLocaleBuilder,\n} as const;\n\nexport * from './caveats';\nexport type { DialogApprovalTypes } from './dialog';\nexport { DIALOG_APPROVAL_TYPES } from './dialog';\n"]}
@@ -5,8 +5,8 @@ import {
5
5
  Bip32EntropyStruct,
6
6
  isEqual
7
7
  } from "@metamask/snaps-utils";
8
+ import { array, size, type } from "@metamask/superstruct";
8
9
  import { assertStruct } from "@metamask/utils";
9
- import { array, size, type } from "superstruct";
10
10
  function permittedDerivationPathsCaveatMapper(value) {
11
11
  return {
12
12
  caveats: [
@@ -64,4 +64,4 @@ export {
64
64
  validateBIP32CaveatPaths,
65
65
  PermittedDerivationPathsCaveatSpecification
66
66
  };
67
- //# sourceMappingURL=chunk-OHEJDJNV.mjs.map
67
+ //# sourceMappingURL=chunk-I2P65KBQ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/restricted/caveats/permittedDerivationPaths.ts"],"sourcesContent":["import type {\n Caveat,\n PermissionConstraint,\n RestrictedMethodCaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Bip32Entropy } from '@metamask/snaps-utils';\nimport {\n SnapCaveatType,\n Bip32EntropyStruct,\n isEqual,\n} from '@metamask/snaps-utils';\nimport { array, size, type } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\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 permittedDerivationPathsCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.PermittedDerivationPaths,\n value,\n },\n ],\n };\n}\n\n/**\n * Validate a caveat path object. The object must consist of a `path` array and\n * a `curve` string. Paths must start with `m`, and must contain at\n * least two indices. If `ed25519` is used, this checks if all the path indices\n * are hardened.\n *\n * @param value - The value to validate.\n * @throws If the value is invalid.\n */\nexport function validateBIP32Path(\n value: unknown,\n): asserts value is Bip32Entropy {\n assertStruct(\n value,\n Bip32EntropyStruct,\n 'Invalid BIP-32 entropy path definition',\n rpcErrors.invalidParams,\n );\n}\n\n/**\n * Validate the path values associated with a caveat. This validates that the\n * value is a non-empty array with valid derivation paths and curves.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateBIP32CaveatPaths(\n caveat: Caveat<string, any>,\n): asserts caveat is Caveat<string, Bip32Entropy[]> {\n assertStruct(\n caveat,\n type({ value: size(array(Bip32EntropyStruct), 1, Infinity) }),\n 'Invalid BIP-32 entropy caveat',\n rpcErrors.internal,\n );\n}\n\nexport const PermittedDerivationPathsCaveatSpecification: Record<\n SnapCaveatType.PermittedDerivationPaths,\n RestrictedMethodCaveatSpecificationConstraint\n> = {\n [SnapCaveatType.PermittedDerivationPaths]: Object.freeze({\n type: SnapCaveatType.PermittedDerivationPaths,\n decorator: (method, caveat) => {\n return async (args) => {\n const { params } = args;\n validateBIP32Path(params);\n\n const path = (caveat.value as Bip32Entropy[]).find(\n (caveatPath) =>\n isEqual(\n params.path.slice(0, caveatPath.path.length),\n caveatPath.path,\n ) && caveatPath.curve === params.curve,\n );\n\n if (!path) {\n throw providerErrors.unauthorized({\n message:\n 'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',\n });\n }\n\n return await method(args);\n };\n },\n validator: (caveat) => validateBIP32CaveatPaths(caveat),\n }),\n};\n"],"mappings":";AAKA,SAAS,gBAAgB,iBAAiB;AAE1C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,OAAO,MAAM,YAAY;AAElC,SAAS,oBAAoB;AAUtB,SAAS,qCACd,OACuC;AACvC,SAAO;AAAA,IACL,SAAS;AAAA,MACP;AAAA,QACE,MAAM,eAAe;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAWO,SAAS,kBACd,OAC+B;AAC/B;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA,UAAU;AAAA,EACZ;AACF;AASO,SAAS,yBACd,QACkD;AAClD;AAAA,IACE;AAAA,IACA,KAAK,EAAE,OAAO,KAAK,MAAM,kBAAkB,GAAG,GAAG,QAAQ,EAAE,CAAC;AAAA,IAC5D;AAAA,IACA,UAAU;AAAA,EACZ;AACF;AAEO,IAAM,8CAGT;AAAA,EACF,CAAC,eAAe,wBAAwB,GAAG,OAAO,OAAO;AAAA,IACvD,MAAM,eAAe;AAAA,IACrB,WAAW,CAAC,QAAQ,WAAW;AAC7B,aAAO,OAAO,SAAS;AACrB,cAAM,EAAE,OAAO,IAAI;AACnB,0BAAkB,MAAM;AAExB,cAAM,OAAQ,OAAO,MAAyB;AAAA,UAC5C,CAAC,eACC;AAAA,YACE,OAAO,KAAK,MAAM,GAAG,WAAW,KAAK,MAAM;AAAA,YAC3C,WAAW;AAAA,UACb,KAAK,WAAW,UAAU,OAAO;AAAA,QACrC;AAEA,YAAI,CAAC,MAAM;AACT,gBAAM,eAAe,aAAa;AAAA,YAChC,SACE;AAAA,UACJ,CAAC;AAAA,QACH;AAEA,eAAO,MAAM,OAAO,IAAI;AAAA,MAC1B;AAAA,IACF;AAAA,IACA,WAAW,CAAC,WAAW,yBAAyB,MAAM;AAAA,EACxD,CAAC;AACH;","names":[]}
@@ -0,0 +1,45 @@
1
+ // src/permitted/resolveInterface.ts
2
+ import { rpcErrors } from "@metamask/rpc-errors";
3
+ import { StructError, create, object, string } from "@metamask/superstruct";
4
+ import { JsonStruct } from "@metamask/utils";
5
+ var hookNames = {
6
+ resolveInterface: true
7
+ };
8
+ var resolveInterfaceHandler = {
9
+ methodNames: ["snap_resolveInterface"],
10
+ implementation: getResolveInterfaceImplementation,
11
+ hookNames
12
+ };
13
+ var ResolveInterfaceParametersStruct = object({
14
+ id: string(),
15
+ value: JsonStruct
16
+ });
17
+ async function getResolveInterfaceImplementation(req, res, _next, end, { resolveInterface }) {
18
+ const { params } = req;
19
+ try {
20
+ const validatedParams = getValidatedParams(params);
21
+ const { id, value } = validatedParams;
22
+ await resolveInterface(id, value);
23
+ res.result = null;
24
+ } catch (error) {
25
+ return end(error);
26
+ }
27
+ return end();
28
+ }
29
+ function getValidatedParams(params) {
30
+ try {
31
+ return create(params, ResolveInterfaceParametersStruct);
32
+ } catch (error) {
33
+ if (error instanceof StructError) {
34
+ throw rpcErrors.invalidParams({
35
+ message: `Invalid params: ${error.message}.`
36
+ });
37
+ }
38
+ throw rpcErrors.internal();
39
+ }
40
+ }
41
+
42
+ export {
43
+ resolveInterfaceHandler
44
+ };
45
+ //# sourceMappingURL=chunk-MC2Z4NF6.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/permitted/resolveInterface.ts"],"sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n JsonRpcRequest,\n ResolveInterfaceParams,\n ResolveInterfaceResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\nimport { JsonStruct, type Json } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<ResolveInterfaceMethodHooks> = {\n resolveInterface: true,\n};\n\nexport type ResolveInterfaceMethodHooks = {\n /**\n * @param id - The interface id.\n * @param value - The value to resolve the interface with.\n */\n resolveInterface: (id: string, value: Json) => Promise<void>;\n};\n\nexport const resolveInterfaceHandler: PermittedHandlerExport<\n ResolveInterfaceMethodHooks,\n ResolveInterfaceParameters,\n ResolveInterfaceResult\n> = {\n methodNames: ['snap_resolveInterface'],\n implementation: getResolveInterfaceImplementation,\n hookNames,\n};\n\nconst ResolveInterfaceParametersStruct = object({\n id: string(),\n value: JsonStruct,\n});\n\nexport type ResolveInterfaceParameters = InferMatching<\n typeof ResolveInterfaceParametersStruct,\n ResolveInterfaceParams\n>;\n\n/**\n * The `snap_resolveInterface` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.resolveInterface - The function to resolve the interface.\n * @returns Nothing.\n */\nasync function getResolveInterfaceImplementation(\n req: JsonRpcRequest<ResolveInterfaceParameters>,\n res: PendingJsonRpcResponse<ResolveInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { resolveInterface }: ResolveInterfaceMethodHooks,\n): Promise<void> {\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id, value } = validatedParams;\n\n await resolveInterface(id, value);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the resolveInterface method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated resolveInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): ResolveInterfaceParameters {\n try {\n return create(params, ResolveInterfaceParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"],"mappings":";AAEA,SAAS,iBAAiB;AAO1B,SAAS,aAAa,QAAQ,QAAQ,cAAc;AAEpD,SAAS,kBAA6B;AAItC,IAAM,YAA4D;AAAA,EAChE,kBAAkB;AACpB;AAUO,IAAM,0BAIT;AAAA,EACF,aAAa,CAAC,uBAAuB;AAAA,EACrC,gBAAgB;AAAA,EAChB;AACF;AAEA,IAAM,mCAAmC,OAAO;AAAA,EAC9C,IAAI,OAAO;AAAA,EACX,OAAO;AACT,CAAC;AAmBD,eAAe,kCACb,KACA,KACA,OACA,KACA,EAAE,iBAAiB,GACJ;AACf,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI;AACF,UAAM,kBAAkB,mBAAmB,MAAM;AAEjD,UAAM,EAAE,IAAI,MAAM,IAAI;AAEtB,UAAM,iBAAiB,IAAI,KAAK;AAChC,QAAI,SAAS;AAAA,EACf,SAAS,OAAO;AACd,WAAO,IAAI,KAAK;AAAA,EAClB;AAEA,SAAO,IAAI;AACb;AASA,SAAS,mBAAmB,QAA6C;AACvE,MAAI;AACF,WAAO,OAAO,QAAQ,gCAAgC;AAAA,EACxD,SAAS,OAAO;AACd,QAAI,iBAAiB,aAAa;AAChC,YAAM,UAAU,cAAc;AAAA,QAC5B,SAAS,mBAAmB,MAAM,OAAO;AAAA,MAC3C,CAAC;AAAA,IACH;AAEA,UAAM,UAAU,SAAS;AAAA,EAC3B;AACF;","names":[]}
@@ -4,7 +4,7 @@ var _rpcerrors = require('@metamask/rpc-errors');
4
4
 
5
5
 
6
6
  var _snapssdk = require('@metamask/snaps-sdk');
7
- var _superstruct = require('superstruct');
7
+ var _superstruct = require('@metamask/superstruct');
8
8
  var hookNames = {
9
9
  createInterface: true
10
10
  };
@@ -44,4 +44,4 @@ function getValidatedParams(params) {
44
44
 
45
45
 
46
46
  exports.createInterfaceHandler = createInterfaceHandler;
47
- //# sourceMappingURL=chunk-PPR53SPJ.js.map
47
+ //# sourceMappingURL=chunk-OI33OL7K.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/permitted/createInterface.ts"],"names":[],"mappings":";AAEA,SAAS,iBAAiB;AAQ1B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP,SAAS,aAAa,QAAQ,QAAQ,gBAAgB;AAKtD,IAAM,YAA2D;AAAA,EAC/D,iBAAiB;AACnB;AAaO,IAAM,yBAIT;AAAA,EACF,aAAa,CAAC,sBAAsB;AAAA,EACpC,gBAAgB;AAAA,EAChB;AACF;AAEA,IAAM,kCAAkC,OAAO;AAAA,EAC7C,IAAI;AAAA,EACJ,SAAS,SAAS,sBAAsB;AAC1C,CAAC;AAmBD,eAAe,iCACb,KACA,KACA,OACA,KACA,EAAE,gBAAgB,GACH;AACf,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI;AACF,UAAM,kBAAkB,mBAAmB,MAAM;AAEjD,UAAM,EAAE,IAAI,QAAQ,IAAI;AAExB,QAAI,SAAS,MAAM,gBAAgB,IAAI,OAAO;AAAA,EAChD,SAAS,OAAO;AACd,WAAO,IAAI,KAAK;AAAA,EAClB;AAEA,SAAO,IAAI;AACb;AASA,SAAS,mBAAmB,QAA4C;AACtE,MAAI;AACF,WAAO,OAAO,QAAQ,+BAA+B;AAAA,EACvD,SAAS,OAAO;AACd,QAAI,iBAAiB,aAAa;AAChC,YAAM,UAAU,cAAc;AAAA,QAC5B,SAAS,mBAAmB,MAAM,OAAO;AAAA,MAC3C,CAAC;AAAA,IACH;AAEA,UAAM,UAAU,SAAS;AAAA,EAC3B;AACF","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n CreateInterfaceParams,\n CreateInterfaceResult,\n JsonRpcRequest,\n ComponentOrElement,\n InterfaceContext,\n} from '@metamask/snaps-sdk';\nimport {\n ComponentOrElementStruct,\n InterfaceContextStruct,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, optional } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<CreateInterfaceMethodHooks> = {\n createInterface: true,\n};\n\nexport type CreateInterfaceMethodHooks = {\n /**\n * @param ui - The UI components.\n * @returns The unique identifier of the interface.\n */\n createInterface: (\n ui: ComponentOrElement,\n context?: InterfaceContext,\n ) => Promise<string>;\n};\n\nexport const createInterfaceHandler: PermittedHandlerExport<\n CreateInterfaceMethodHooks,\n CreateInterfaceParameters,\n CreateInterfaceResult\n> = {\n methodNames: ['snap_createInterface'],\n implementation: getCreateInterfaceImplementation,\n hookNames,\n};\n\nconst CreateInterfaceParametersStruct = object({\n ui: ComponentOrElementStruct,\n context: optional(InterfaceContextStruct),\n});\n\nexport type CreateInterfaceParameters = InferMatching<\n typeof CreateInterfaceParametersStruct,\n CreateInterfaceParams\n>;\n\n/**\n * The `snap_createInterface` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.createInterface - The function to create the interface.\n * @returns Nothing.\n */\nasync function getCreateInterfaceImplementation(\n req: JsonRpcRequest<CreateInterfaceParameters>,\n res: PendingJsonRpcResponse<CreateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { createInterface }: CreateInterfaceMethodHooks,\n): Promise<void> {\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { ui, context } = validatedParams;\n\n res.result = await createInterface(ui, context);\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the createInterface method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated createInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): CreateInterfaceParameters {\n try {\n return create(params, CreateInterfaceParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
@@ -1,30 +1,30 @@
1
+ import {
2
+ notifyBuilder
3
+ } from "./chunk-JWDQUKKU.mjs";
4
+ import {
5
+ getBip32PublicKeyBuilder
6
+ } from "./chunk-VYII7C3J.mjs";
1
7
  import {
2
8
  getBip44EntropyBuilder
3
9
  } from "./chunk-LR7UR4YU.mjs";
4
10
  import {
5
11
  getEntropyBuilder
6
- } from "./chunk-KIXPIIY5.mjs";
12
+ } from "./chunk-2RDYC42U.mjs";
7
13
  import {
8
14
  getLocaleBuilder
9
15
  } from "./chunk-IZYMBIBD.mjs";
10
16
  import {
11
17
  manageAccountsBuilder
12
- } from "./chunk-E6WUMFQT.mjs";
18
+ } from "./chunk-Z6YFGWHN.mjs";
13
19
  import {
14
20
  manageStateBuilder
15
21
  } from "./chunk-TE4W25CA.mjs";
16
- import {
17
- notifyBuilder
18
- } from "./chunk-JWDQUKKU.mjs";
19
22
  import {
20
23
  dialogBuilder
21
- } from "./chunk-X247BMMW.mjs";
24
+ } from "./chunk-TQCUJQHY.mjs";
22
25
  import {
23
26
  getBip32EntropyBuilder
24
27
  } from "./chunk-LXJBBRQ4.mjs";
25
- import {
26
- getBip32PublicKeyBuilder
27
- } from "./chunk-R3DRYUNH.mjs";
28
28
  import {
29
29
  invokeSnapBuilder
30
30
  } from "./chunk-VVBTXSID.mjs";
@@ -46,4 +46,4 @@ var restrictedMethodPermissionBuilders = {
46
46
  export {
47
47
  restrictedMethodPermissionBuilders
48
48
  };
49
- //# sourceMappingURL=chunk-I3QG3QZL.mjs.map
49
+ //# sourceMappingURL=chunk-SYB5TG5O.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/restricted/index.ts"],"sourcesContent":["import type { DialogMethodHooks } from './dialog';\nimport { dialogBuilder } from './dialog';\nimport type { GetBip32EntropyMethodHooks } from './getBip32Entropy';\nimport { getBip32EntropyBuilder } from './getBip32Entropy';\nimport type { GetBip32PublicKeyMethodHooks } from './getBip32PublicKey';\nimport { getBip32PublicKeyBuilder } from './getBip32PublicKey';\nimport type { GetBip44EntropyMethodHooks } from './getBip44Entropy';\nimport { getBip44EntropyBuilder } from './getBip44Entropy';\nimport type { GetEntropyHooks } from './getEntropy';\nimport { getEntropyBuilder } from './getEntropy';\nimport type { GetLocaleMethodHooks } from './getLocale';\nimport { getLocaleBuilder } from './getLocale';\nimport type { InvokeSnapMethodHooks } from './invokeSnap';\nimport { invokeSnapBuilder } from './invokeSnap';\nimport type { ManageAccountsMethodHooks } from './manageAccounts';\nimport { manageAccountsBuilder } from './manageAccounts';\nimport type { ManageStateMethodHooks } from './manageState';\nimport { manageStateBuilder } from './manageState';\nimport type { NotifyMethodHooks } from './notify';\nimport { notifyBuilder } from './notify';\n\nexport { WALLET_SNAP_PERMISSION_KEY } from './invokeSnap';\nexport { getEncryptionEntropy } from './manageState';\n\nexport type RestrictedMethodHooks = DialogMethodHooks &\n GetBip32EntropyMethodHooks &\n GetBip32PublicKeyMethodHooks &\n GetBip44EntropyMethodHooks &\n GetEntropyHooks &\n InvokeSnapMethodHooks &\n ManageStateMethodHooks &\n NotifyMethodHooks &\n ManageAccountsMethodHooks &\n GetLocaleMethodHooks;\n\nexport const restrictedMethodPermissionBuilders = {\n [dialogBuilder.targetName]: dialogBuilder,\n [getBip32EntropyBuilder.targetName]: getBip32EntropyBuilder,\n [getBip32PublicKeyBuilder.targetName]: getBip32PublicKeyBuilder,\n [getBip44EntropyBuilder.targetName]: getBip44EntropyBuilder,\n [getEntropyBuilder.targetName]: getEntropyBuilder,\n [invokeSnapBuilder.targetName]: invokeSnapBuilder,\n [manageStateBuilder.targetName]: manageStateBuilder,\n [notifyBuilder.targetName]: notifyBuilder,\n [manageAccountsBuilder.targetName]: manageAccountsBuilder,\n [getLocaleBuilder.targetName]: getLocaleBuilder,\n} as const;\n\nexport * from './caveats';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCO,IAAM,qCAAqC;AAAA,EAChD,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,yBAAyB,UAAU,GAAG;AAAA,EACvC,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,mBAAmB,UAAU,GAAG;AAAA,EACjC,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,sBAAsB,UAAU,GAAG;AAAA,EACpC,CAAC,iBAAiB,UAAU,GAAG;AACjC;","names":[]}
1
+ {"version":3,"sources":["../src/restricted/index.ts"],"sourcesContent":["import type { DialogMethodHooks } from './dialog';\nimport { dialogBuilder } from './dialog';\nimport type { GetBip32EntropyMethodHooks } from './getBip32Entropy';\nimport { getBip32EntropyBuilder } from './getBip32Entropy';\nimport type { GetBip32PublicKeyMethodHooks } from './getBip32PublicKey';\nimport { getBip32PublicKeyBuilder } from './getBip32PublicKey';\nimport type { GetBip44EntropyMethodHooks } from './getBip44Entropy';\nimport { getBip44EntropyBuilder } from './getBip44Entropy';\nimport type { GetEntropyHooks } from './getEntropy';\nimport { getEntropyBuilder } from './getEntropy';\nimport type { GetLocaleMethodHooks } from './getLocale';\nimport { getLocaleBuilder } from './getLocale';\nimport type { InvokeSnapMethodHooks } from './invokeSnap';\nimport { invokeSnapBuilder } from './invokeSnap';\nimport type { ManageAccountsMethodHooks } from './manageAccounts';\nimport { manageAccountsBuilder } from './manageAccounts';\nimport type { ManageStateMethodHooks } from './manageState';\nimport { manageStateBuilder } from './manageState';\nimport type { NotifyMethodHooks } from './notify';\nimport { notifyBuilder } from './notify';\n\nexport { WALLET_SNAP_PERMISSION_KEY } from './invokeSnap';\nexport { getEncryptionEntropy } from './manageState';\n\nexport type RestrictedMethodHooks = DialogMethodHooks &\n GetBip32EntropyMethodHooks &\n GetBip32PublicKeyMethodHooks &\n GetBip44EntropyMethodHooks &\n GetEntropyHooks &\n InvokeSnapMethodHooks &\n ManageStateMethodHooks &\n NotifyMethodHooks &\n ManageAccountsMethodHooks &\n GetLocaleMethodHooks;\n\nexport const restrictedMethodPermissionBuilders = {\n [dialogBuilder.targetName]: dialogBuilder,\n [getBip32EntropyBuilder.targetName]: getBip32EntropyBuilder,\n [getBip32PublicKeyBuilder.targetName]: getBip32PublicKeyBuilder,\n [getBip44EntropyBuilder.targetName]: getBip44EntropyBuilder,\n [getEntropyBuilder.targetName]: getEntropyBuilder,\n [invokeSnapBuilder.targetName]: invokeSnapBuilder,\n [manageStateBuilder.targetName]: manageStateBuilder,\n [notifyBuilder.targetName]: notifyBuilder,\n [manageAccountsBuilder.targetName]: manageAccountsBuilder,\n [getLocaleBuilder.targetName]: getLocaleBuilder,\n} as const;\n\nexport * from './caveats';\nexport type { DialogApprovalTypes } from './dialog';\nexport { DIALOG_APPROVAL_TYPES } from './dialog';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCO,IAAM,qCAAqC;AAAA,EAChD,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,yBAAyB,UAAU,GAAG;AAAA,EACvC,CAAC,uBAAuB,UAAU,GAAG;AAAA,EACrC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,kBAAkB,UAAU,GAAG;AAAA,EAChC,CAAC,mBAAmB,UAAU,GAAG;AAAA,EACjC,CAAC,cAAc,UAAU,GAAG;AAAA,EAC5B,CAAC,sBAAsB,UAAU,GAAG;AAAA,EACpC,CAAC,iBAAiB,UAAU,GAAG;AACjC;","names":[]}
@@ -8,7 +8,6 @@ import {
8
8
  ComponentOrElementStruct
9
9
  } from "@metamask/snaps-sdk";
10
10
  import { createUnion } from "@metamask/snaps-utils";
11
- import { hasProperty } from "@metamask/utils";
12
11
  import {
13
12
  create,
14
13
  enums,
@@ -17,8 +16,15 @@ import {
17
16
  size,
18
17
  string,
19
18
  type
20
- } from "superstruct";
19
+ } from "@metamask/superstruct";
20
+ import { hasProperty, isObject } from "@metamask/utils";
21
21
  var methodName = "snap_dialog";
22
+ var DIALOG_APPROVAL_TYPES = {
23
+ [DialogType.Alert]: `${methodName}:alert`,
24
+ [DialogType.Confirmation]: `${methodName}:confirmation`,
25
+ [DialogType.Prompt]: `${methodName}:prompt`,
26
+ default: methodName
27
+ };
22
28
  var PlaceholderStruct = optional(size(string(), 1, 40));
23
29
  var specificationBuilder = ({
24
30
  allowedCaveats = null,
@@ -33,7 +39,7 @@ var specificationBuilder = ({
33
39
  };
34
40
  };
35
41
  var methodHooks = {
36
- showDialog: true,
42
+ requestUserApproval: true,
37
43
  createInterface: true,
38
44
  getInterface: true
39
45
  };
@@ -43,7 +49,9 @@ var dialogBuilder = Object.freeze({
43
49
  methodHooks
44
50
  });
45
51
  var BaseParamsStruct = type({
46
- type: enums([DialogType.Alert, DialogType.Confirmation, DialogType.Prompt])
52
+ type: optional(
53
+ enums([DialogType.Alert, DialogType.Confirmation, DialogType.Prompt])
54
+ )
47
55
  });
48
56
  var AlertParametersWithContentStruct = object({
49
57
  type: enumValue(DialogType.Alert),
@@ -83,10 +91,21 @@ var PromptParametersStruct = union([
83
91
  PromptParametersWithContentStruct,
84
92
  PromptParametersWithIdStruct
85
93
  ]);
94
+ var DefaultParametersWithContentStruct = object({
95
+ content: ComponentOrElementStruct
96
+ });
97
+ var DefaultParametersWithIdStruct = object({
98
+ id: string()
99
+ });
100
+ var DefaultParametersStruct = union([
101
+ DefaultParametersWithContentStruct,
102
+ DefaultParametersWithIdStruct
103
+ ]);
86
104
  var DialogParametersStruct = union([
87
105
  AlertParametersStruct,
88
106
  ConfirmationParametersStruct,
89
- PromptParametersStruct
107
+ PromptParametersStruct,
108
+ DefaultParametersStruct
90
109
  ]);
91
110
  var structs = {
92
111
  [DialogType.Alert]: AlertParametersStruct,
@@ -94,7 +113,7 @@ var structs = {
94
113
  [DialogType.Prompt]: PromptParametersStruct
95
114
  };
96
115
  function getDialogImplementation({
97
- showDialog,
116
+ requestUserApproval,
98
117
  createInterface,
99
118
  getInterface
100
119
  }) {
@@ -103,26 +122,48 @@ function getDialogImplementation({
103
122
  params,
104
123
  context: { origin }
105
124
  } = args;
125
+ if (!isObject(params)) {
126
+ throw rpcErrors.invalidParams({
127
+ message: "Invalid params: Expected params to be a single object."
128
+ });
129
+ }
106
130
  const validatedType = getValidatedType(params);
107
- const validatedParams = getValidatedParams(params, structs[validatedType]);
108
- const placeholder = validatedParams.type === DialogType.Prompt ? validatedParams.placeholder : void 0;
131
+ const approvalType = validatedType ? DIALOG_APPROVAL_TYPES[validatedType] : DIALOG_APPROVAL_TYPES.default;
132
+ const validatedParams = getValidatedParams(params, validatedType);
133
+ const placeholder = isPromptDialog(validatedParams) ? validatedParams.placeholder : void 0;
109
134
  if (hasProperty(validatedParams, "content")) {
110
135
  const id = await createInterface(
111
136
  origin,
112
137
  validatedParams.content
113
138
  );
114
- return showDialog(origin, validatedType, id, placeholder);
115
- }
116
- try {
117
- getInterface(origin, validatedParams.id);
118
- } catch (error) {
119
- throw rpcErrors.invalidParams({
120
- message: `Invalid params: ${error.message}`
139
+ return requestUserApproval({
140
+ id: validatedType ? void 0 : id,
141
+ origin,
142
+ type: approvalType,
143
+ requestData: { id, placeholder }
121
144
  });
122
145
  }
123
- return showDialog(origin, validatedType, validatedParams.id, placeholder);
146
+ validateInterface(origin, validatedParams.id, getInterface);
147
+ return requestUserApproval({
148
+ id: validatedType ? void 0 : validatedParams.id,
149
+ origin,
150
+ type: approvalType,
151
+ requestData: { id: validatedParams.id, placeholder }
152
+ });
124
153
  };
125
154
  }
155
+ function validateInterface(origin, id, getInterface) {
156
+ try {
157
+ getInterface(origin, id);
158
+ } catch (error) {
159
+ throw rpcErrors.invalidParams({
160
+ message: `Invalid params: ${error.message}`
161
+ });
162
+ }
163
+ }
164
+ function getDialogType(params) {
165
+ return hasProperty(params, "type") ? params.type : void 0;
166
+ }
126
167
  function getValidatedType(params) {
127
168
  try {
128
169
  return create(params, BaseParamsStruct).type;
@@ -134,9 +175,12 @@ function getValidatedType(params) {
134
175
  });
135
176
  }
136
177
  }
137
- function getValidatedParams(params, struct) {
178
+ function isPromptDialog(params) {
179
+ return getDialogType(params) === DialogType.Prompt;
180
+ }
181
+ function getValidatedParams(params, validatedType) {
138
182
  try {
139
- return createUnion(params, struct, "type");
183
+ return validatedType ? createUnion(params, structs[validatedType], "type") : create(params, DefaultParametersStruct);
140
184
  } catch (error) {
141
185
  throw rpcErrors.invalidParams({
142
186
  message: `Invalid params: ${error.message}`
@@ -145,7 +189,8 @@ function getValidatedParams(params, struct) {
145
189
  }
146
190
 
147
191
  export {
192
+ DIALOG_APPROVAL_TYPES,
148
193
  dialogBuilder,
149
194
  getDialogImplementation
150
195
  };
151
- //# sourceMappingURL=chunk-X247BMMW.mjs.map
196
+ //# sourceMappingURL=chunk-TQCUJQHY.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/restricted/dialog.ts"],"sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport {\n DialogType,\n enumValue,\n union,\n ComponentOrElementStruct,\n} from '@metamask/snaps-sdk';\nimport type {\n DialogParams,\n Component,\n InterfaceState,\n SnapId,\n PromptDialog,\n ComponentOrElement,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport { createUnion } from '@metamask/snaps-utils';\nimport type { Infer, Struct } from '@metamask/superstruct';\nimport {\n create,\n enums,\n object,\n optional,\n size,\n string,\n type,\n} from '@metamask/superstruct';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_dialog';\n\nexport type DialogApprovalTypes = Record<DialogType, string> & {\n default: string;\n};\n\nexport const DIALOG_APPROVAL_TYPES = {\n [DialogType.Alert]: `${methodName}:alert`,\n [DialogType.Confirmation]: `${methodName}:confirmation`,\n [DialogType.Prompt]: `${methodName}:prompt`,\n default: methodName,\n};\n\nconst PlaceholderStruct = optional(size(string(), 1, 40));\n\nexport type Placeholder = Infer<typeof PlaceholderStruct>;\n\ntype RequestUserApprovalOptions = {\n id?: string;\n origin: string;\n type: string;\n requestData: {\n id: string;\n placeholder?: string;\n };\n};\n\ntype RequestUserApproval = (\n opts: RequestUserApprovalOptions,\n) => Promise<boolean | null | string | Json>;\n\ntype CreateInterface = (\n snapId: string,\n content: ComponentOrElement,\n) => Promise<string>;\n\ntype GetInterface = (\n snapId: string,\n id: string,\n) => { content: ComponentOrElement; snapId: SnapId; state: InterfaceState };\n\nexport type DialogMethodHooks = {\n /**\n * @param opts - The `requestUserApproval` options.\n * @param opts.id - The approval ID. If not provided, a new approval ID will be generated.\n * @param opts.origin - The origin of the request. In this case, the Snap ID.\n * @param opts.type - The type of the approval request.\n * @param opts.requestData - The data of the approval request.\n * @param opts.requestData.id - The ID of the interface.\n * @param opts.requestData.placeholder - The placeholder of the `Prompt` dialog.\n */\n requestUserApproval: RequestUserApproval;\n\n /**\n * @param snapId - The Snap ID creating the interface.\n * @param content - The content of the interface.\n */\n createInterface: CreateInterface;\n /**\n * @param snapId - The SnapId requesting the interface.\n * @param id - The interface ID.\n */\n getInterface: GetInterface;\n};\n\ntype DialogSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: DialogMethodHooks;\n};\n\ntype DialogSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getDialogImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_dialog` permission. `snap_dialog`\n * lets the Snap display one of the following dialogs to the user:\n * - An alert, for displaying information.\n * - A confirmation, for accepting or rejecting some action.\n * - A prompt, for inputting some information.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the\n * permission.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_dialog` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n DialogSpecificationBuilderOptions,\n DialogSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: DialogSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getDialogImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<DialogMethodHooks> = {\n requestUserApproval: true,\n createInterface: true,\n getInterface: true,\n};\n\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n// Note: We use `type` here instead of `object` because `type` does not validate\n// the keys of the object, which is what we want.\nconst BaseParamsStruct = type({\n type: optional(\n enums([DialogType.Alert, DialogType.Confirmation, DialogType.Prompt]),\n ),\n});\n\nconst AlertParametersWithContentStruct = object({\n type: enumValue(DialogType.Alert),\n content: ComponentOrElementStruct,\n});\nconst AlertParametersWithIdStruct = object({\n type: enumValue(DialogType.Alert),\n id: string(),\n});\n\nconst AlertParametersStruct = union([\n AlertParametersWithContentStruct,\n AlertParametersWithIdStruct,\n]);\n\nconst ConfirmationParametersWithContentStruct = object({\n type: enumValue(DialogType.Confirmation),\n content: ComponentOrElementStruct,\n});\n\nconst ConfirmationParametersWithIdStruct = object({\n type: enumValue(DialogType.Confirmation),\n id: string(),\n});\n\nconst ConfirmationParametersStruct = union([\n ConfirmationParametersWithContentStruct,\n ConfirmationParametersWithIdStruct,\n]);\n\nconst PromptParametersWithContentStruct = object({\n type: enumValue(DialogType.Prompt),\n content: ComponentOrElementStruct,\n placeholder: PlaceholderStruct,\n});\n\nconst PromptParametersWithIdStruct = object({\n type: enumValue(DialogType.Prompt),\n id: string(),\n placeholder: PlaceholderStruct,\n});\n\nconst PromptParametersStruct = union([\n PromptParametersWithContentStruct,\n PromptParametersWithIdStruct,\n]);\n\nconst DefaultParametersWithContentStruct = object({\n content: ComponentOrElementStruct,\n});\n\nconst DefaultParametersWithIdStruct = object({\n id: string(),\n});\n\nconst DefaultParametersStruct = union([\n DefaultParametersWithContentStruct,\n DefaultParametersWithIdStruct,\n]);\n\nconst DialogParametersStruct = union([\n AlertParametersStruct,\n ConfirmationParametersStruct,\n PromptParametersStruct,\n DefaultParametersStruct,\n]);\n\nexport type DialogParameters = InferMatching<\n typeof DialogParametersStruct,\n DialogParams\n>;\n\nconst structs: Record<DialogType, Struct<any, any>> = {\n [DialogType.Alert]: AlertParametersStruct,\n [DialogType.Confirmation]: ConfirmationParametersStruct,\n [DialogType.Prompt]: PromptParametersStruct,\n};\n\n/**\n * Builds the method implementation for `snap_dialog`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.requestUserApproval - A function that creates a new Approval in the ApprovalController.\n * This function should return a Promise that resolves with the appropriate value when the user has approved or rejected the request.\n * @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.\n * @param hooks.getInterface - A function that gets an interface from SnapInterfaceController.\n * @returns The method implementation which return value depends on the dialog\n * type, valid return types are: string, boolean, null.\n */\nexport function getDialogImplementation({\n requestUserApproval,\n createInterface,\n getInterface,\n}: DialogMethodHooks) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<boolean | null | string | Json> {\n const {\n params,\n context: { origin },\n } = args;\n\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid params: Expected params to be a single object.',\n });\n }\n\n const validatedType = getValidatedType(params);\n\n const approvalType = validatedType\n ? DIALOG_APPROVAL_TYPES[validatedType]\n : DIALOG_APPROVAL_TYPES.default;\n\n const validatedParams = getValidatedParams(params, validatedType);\n const placeholder = isPromptDialog(validatedParams)\n ? validatedParams.placeholder\n : undefined;\n\n if (hasProperty(validatedParams, 'content')) {\n const id = await createInterface(\n origin,\n validatedParams.content as Component,\n );\n\n return requestUserApproval({\n id: validatedType ? undefined : id,\n origin,\n type: approvalType,\n requestData: { id, placeholder },\n });\n }\n\n validateInterface(origin, validatedParams.id, getInterface);\n\n return requestUserApproval({\n id: validatedType ? undefined : validatedParams.id,\n origin,\n type: approvalType,\n requestData: { id: validatedParams.id, placeholder },\n });\n };\n}\n/**\n * Validate that the interface ID is valid.\n *\n * @param origin - The origin of the request.\n * @param id - The interface ID.\n * @param getInterface - The function to get the interface.\n */\nfunction validateInterface(\n origin: string,\n id: string,\n getInterface: GetInterface,\n) {\n try {\n getInterface(origin, id);\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}`,\n });\n }\n}\n\n/**\n * Gets the dialog type from the dialog parameters.\n *\n * @param params - The dialog parameters.\n * @returns The dialog type.\n */\nfunction getDialogType(params: DialogParameters): DialogType | undefined {\n return hasProperty(params, 'type') ? (params.type as DialogType) : undefined;\n}\n\n/**\n * Get the validated type of the dialog parameters. Throws an error if the type\n * is invalid.\n *\n * @param params - The parameters to validate.\n * @returns The validated type of the dialog parameters.\n */\nfunction getValidatedType(params: unknown): DialogType | undefined {\n try {\n return create(params, BaseParamsStruct).type;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `The \"type\" property must be one of: ${Object.values(\n DialogType,\n ).join(', ')}.`,\n });\n }\n}\n\n/**\n * Checks if the dialog parameters are for a prompt dialog.\n *\n * @param params - The dialog parameters.\n * @returns `true` if the dialog parameters are for a prompt dialog, `false` otherwise.\n */\nfunction isPromptDialog(params: DialogParameters): params is PromptDialog {\n return getDialogType(params) === DialogType.Prompt;\n}\n\n/**\n * Validates the confirm method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @param validatedType - The validated dialog type.\n * @returns The validated confirm method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n validatedType: DialogType | undefined,\n): DialogParameters {\n try {\n return validatedType\n ? createUnion(params, structs[validatedType], 'type')\n : create(params, DefaultParametersStruct);\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}`,\n });\n }\n}\n"],"mappings":"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","names":["methodHooks"]}
@@ -1,6 +1,6 @@
1
1
  // src/permitted/getInterfaceState.ts
2
2
  import { rpcErrors } from "@metamask/rpc-errors";
3
- import { StructError, create, object, string } from "superstruct";
3
+ import { StructError, create, object, string } from "@metamask/superstruct";
4
4
  var hookNames = {
5
5
  getInterfaceState: true
6
6
  };
@@ -39,4 +39,4 @@ function getValidatedParams(params) {
39
39
  export {
40
40
  getInterfaceStateHandler
41
41
  };
42
- //# sourceMappingURL=chunk-TJYMYQAB.mjs.map
42
+ //# sourceMappingURL=chunk-TVC3E5LI.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/permitted/getInterfaceState.ts"],"sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetInterfaceStateParams,\n GetInterfaceStateResult,\n InterfaceState,\n JsonRpcRequest,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<GetInterfaceStateMethodHooks> = {\n getInterfaceState: true,\n};\n\nexport type GetInterfaceStateMethodHooks = {\n /**\n * @param id - The interface ID.\n * @returns The interface state.\n */\n getInterfaceState: (id: string) => InterfaceState;\n};\n\nexport const getInterfaceStateHandler: PermittedHandlerExport<\n GetInterfaceStateMethodHooks,\n GetInterfaceStateParameters,\n GetInterfaceStateResult\n> = {\n methodNames: ['snap_getInterfaceState'],\n implementation: getGetInterfaceStateImplementation,\n hookNames,\n};\n\nconst GetInterfaceStateParametersStruct = object({\n id: string(),\n});\n\nexport type GetInterfaceStateParameters = InferMatching<\n typeof GetInterfaceStateParametersStruct,\n GetInterfaceStateParams\n>;\n\n/**\n * The `snap_getInterfaceState` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getInterfaceState - The function to get the interface state.\n * @returns Noting.\n */\nfunction getGetInterfaceStateImplementation(\n req: JsonRpcRequest<GetInterfaceStateParameters>,\n res: PendingJsonRpcResponse<GetInterfaceStateResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getInterfaceState }: GetInterfaceStateMethodHooks,\n): void {\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id } = validatedParams;\n\n res.result = getInterfaceState(id);\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the getInterfaceState method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated getInterfaceState method parameter object.\n */\nfunction getValidatedParams(params: unknown): GetInterfaceStateParameters {\n try {\n return create(params, GetInterfaceStateParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"],"mappings":";AAEA,SAAS,iBAAiB;AAQ1B,SAAS,aAAa,QAAQ,QAAQ,cAAc;AAKpD,IAAM,YAA6D;AAAA,EACjE,mBAAmB;AACrB;AAUO,IAAM,2BAIT;AAAA,EACF,aAAa,CAAC,wBAAwB;AAAA,EACtC,gBAAgB;AAAA,EAChB;AACF;AAEA,IAAM,oCAAoC,OAAO;AAAA,EAC/C,IAAI,OAAO;AACb,CAAC;AAmBD,SAAS,mCACP,KACA,KACA,OACA,KACA,EAAE,kBAAkB,GACd;AACN,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI;AACF,UAAM,kBAAkB,mBAAmB,MAAM;AAEjD,UAAM,EAAE,GAAG,IAAI;AAEf,QAAI,SAAS,kBAAkB,EAAE;AAAA,EACnC,SAAS,OAAO;AACd,WAAO,IAAI,KAAK;AAAA,EAClB;AAEA,SAAO,IAAI;AACb;AASA,SAAS,mBAAmB,QAA8C;AACxE,MAAI;AACF,WAAO,OAAO,QAAQ,iCAAiC;AAAA,EACzD,SAAS,OAAO;AACd,QAAI,iBAAiB,aAAa;AAChC,YAAM,UAAU,cAAc;AAAA,QAC5B,SAAS,mBAAmB,MAAM,OAAO;AAAA,MAC3C,CAAC;AAAA,IACH;AAEA,UAAM,UAAU,SAAS;AAAA,EAC3B;AACF;","names":[]}
@@ -1,7 +1,7 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/permitted/updateInterface.ts
2
2
  var _rpcerrors = require('@metamask/rpc-errors');
3
3
  var _snapssdk = require('@metamask/snaps-sdk');
4
- var _superstruct = require('superstruct');
4
+ var _superstruct = require('@metamask/superstruct');
5
5
  var hookNames = {
6
6
  updateInterface: true
7
7
  };
@@ -42,4 +42,4 @@ function getValidatedParams(params) {
42
42
 
43
43
 
44
44
  exports.updateInterfaceHandler = updateInterfaceHandler;
45
- //# sourceMappingURL=chunk-D5WQBUZB.js.map
45
+ //# sourceMappingURL=chunk-U6TJQLNV.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/permitted/updateInterface.ts"],"names":[],"mappings":";AAEA,SAAS,iBAAiB;AAO1B,SAAS,gCAAgC;AAEzC,SAAS,aAAa,QAAQ,QAAQ,cAAc;AAKpD,IAAM,YAA2D;AAAA,EAC/D,iBAAiB;AACnB;AAUO,IAAM,yBAIT;AAAA,EACF,aAAa,CAAC,sBAAsB;AAAA,EACpC,gBAAgB;AAAA,EAChB;AACF;AAEA,IAAM,kCAAkC,OAAO;AAAA,EAC7C,IAAI,OAAO;AAAA,EACX,IAAI;AACN,CAAC;AAmBD,eAAe,iCACb,KACA,KACA,OACA,KACA,EAAE,gBAAgB,GACH;AACf,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI;AACF,UAAM,kBAAkB,mBAAmB,MAAM;AAEjD,UAAM,EAAE,IAAI,GAAG,IAAI;AAEnB,UAAM,gBAAgB,IAAI,EAAE;AAC5B,QAAI,SAAS;AAAA,EACf,SAAS,OAAO;AACd,WAAO,IAAI,KAAK;AAAA,EAClB;AAEA,SAAO,IAAI;AACb;AASA,SAAS,mBAAmB,QAA4C;AACtE,MAAI;AACF,WAAO,OAAO,QAAQ,+BAA+B;AAAA,EACvD,SAAS,OAAO;AACd,QAAI,iBAAiB,aAAa;AAChC,YAAM,UAAU,cAAc;AAAA,QAC5B,SAAS,mBAAmB,MAAM,OAAO;AAAA,MAC3C,CAAC;AAAA,IACH;AAEA,UAAM,UAAU,SAAS;AAAA,EAC3B;AACF","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n UpdateInterfaceParams,\n UpdateInterfaceResult,\n JsonRpcRequest,\n ComponentOrElement,\n} from '@metamask/snaps-sdk';\nimport { ComponentOrElementStruct } from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<UpdateInterfaceMethodHooks> = {\n updateInterface: true,\n};\n\nexport type UpdateInterfaceMethodHooks = {\n /**\n * @param id - The interface ID.\n * @param ui - The UI components.\n */\n updateInterface: (id: string, ui: ComponentOrElement) => Promise<void>;\n};\n\nexport const updateInterfaceHandler: PermittedHandlerExport<\n UpdateInterfaceMethodHooks,\n UpdateInterfaceParameters,\n UpdateInterfaceResult\n> = {\n methodNames: ['snap_updateInterface'],\n implementation: getUpdateInterfaceImplementation,\n hookNames,\n};\n\nconst UpdateInterfaceParametersStruct = object({\n id: string(),\n ui: ComponentOrElementStruct,\n});\n\nexport type UpdateInterfaceParameters = InferMatching<\n typeof UpdateInterfaceParametersStruct,\n UpdateInterfaceParams\n>;\n\n/**\n * The `snap_updateInterface` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.updateInterface - The function to update the interface.\n * @returns Nothing.\n */\nasync function getUpdateInterfaceImplementation(\n req: JsonRpcRequest<UpdateInterfaceParameters>,\n res: PendingJsonRpcResponse<UpdateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { updateInterface }: UpdateInterfaceMethodHooks,\n): Promise<void> {\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { id, ui } = validatedParams;\n\n await updateInterface(id, ui);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the updateInterface method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated updateInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): UpdateInterfaceParameters {\n try {\n return create(params, UpdateInterfaceParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}