@metamask/snaps-rpc-methods 11.6.0 → 11.8.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 (88) hide show
  1. package/CHANGELOG.md +17 -1
  2. package/dist/endowments/enum.cjs +1 -0
  3. package/dist/endowments/enum.cjs.map +1 -1
  4. package/dist/endowments/enum.d.cts +2 -1
  5. package/dist/endowments/enum.d.cts.map +1 -1
  6. package/dist/endowments/enum.d.mts +2 -1
  7. package/dist/endowments/enum.d.mts.map +1 -1
  8. package/dist/endowments/enum.mjs +1 -0
  9. package/dist/endowments/enum.mjs.map +1 -1
  10. package/dist/endowments/index.cjs +4 -0
  11. package/dist/endowments/index.cjs.map +1 -1
  12. package/dist/endowments/index.d.cts +9 -0
  13. package/dist/endowments/index.d.cts.map +1 -1
  14. package/dist/endowments/index.d.mts +9 -0
  15. package/dist/endowments/index.d.mts.map +1 -1
  16. package/dist/endowments/index.mjs +4 -0
  17. package/dist/endowments/index.mjs.map +1 -1
  18. package/dist/endowments/settings-page.cjs +28 -0
  19. package/dist/endowments/settings-page.cjs.map +1 -0
  20. package/dist/endowments/settings-page.d.cts +16 -0
  21. package/dist/endowments/settings-page.d.cts.map +1 -0
  22. package/dist/endowments/settings-page.d.mts +16 -0
  23. package/dist/endowments/settings-page.d.mts.map +1 -0
  24. package/dist/endowments/settings-page.mjs +25 -0
  25. package/dist/endowments/settings-page.mjs.map +1 -0
  26. package/dist/permitted/createInterface.d.cts +2 -2
  27. package/dist/permitted/createInterface.d.mts +2 -2
  28. package/dist/permitted/updateInterface.d.cts +2 -2
  29. package/dist/permitted/updateInterface.d.mts +2 -2
  30. package/dist/restricted/dialog.d.cts +4 -4
  31. package/dist/restricted/dialog.d.mts +4 -4
  32. package/dist/restricted/getBip32Entropy.cjs +5 -1
  33. package/dist/restricted/getBip32Entropy.cjs.map +1 -1
  34. package/dist/restricted/getBip32Entropy.d.cts +12 -1
  35. package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
  36. package/dist/restricted/getBip32Entropy.d.mts +12 -1
  37. package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
  38. package/dist/restricted/getBip32Entropy.mjs +5 -1
  39. package/dist/restricted/getBip32Entropy.mjs.map +1 -1
  40. package/dist/restricted/getBip32PublicKey.cjs +5 -1
  41. package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
  42. package/dist/restricted/getBip32PublicKey.d.cts +12 -1
  43. package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
  44. package/dist/restricted/getBip32PublicKey.d.mts +12 -1
  45. package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
  46. package/dist/restricted/getBip32PublicKey.mjs +5 -1
  47. package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
  48. package/dist/restricted/getBip44Entropy.cjs +5 -6
  49. package/dist/restricted/getBip44Entropy.cjs.map +1 -1
  50. package/dist/restricted/getBip44Entropy.d.cts +12 -1
  51. package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
  52. package/dist/restricted/getBip44Entropy.d.mts +12 -1
  53. package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
  54. package/dist/restricted/getBip44Entropy.mjs +5 -6
  55. package/dist/restricted/getBip44Entropy.mjs.map +1 -1
  56. package/dist/restricted/getEntropy.cjs +5 -1
  57. package/dist/restricted/getEntropy.cjs.map +1 -1
  58. package/dist/restricted/getEntropy.d.cts +12 -1
  59. package/dist/restricted/getEntropy.d.cts.map +1 -1
  60. package/dist/restricted/getEntropy.d.mts +12 -1
  61. package/dist/restricted/getEntropy.d.mts.map +1 -1
  62. package/dist/restricted/getEntropy.mjs +5 -1
  63. package/dist/restricted/getEntropy.mjs.map +1 -1
  64. package/dist/restricted/manageState.cjs +4 -1
  65. package/dist/restricted/manageState.cjs.map +1 -1
  66. package/dist/restricted/manageState.d.cts +5 -1
  67. package/dist/restricted/manageState.d.cts.map +1 -1
  68. package/dist/restricted/manageState.d.mts +5 -1
  69. package/dist/restricted/manageState.d.mts.map +1 -1
  70. package/dist/restricted/manageState.mjs +4 -1
  71. package/dist/restricted/manageState.mjs.map +1 -1
  72. package/dist/restricted/notify.cjs +49 -5
  73. package/dist/restricted/notify.cjs.map +1 -1
  74. package/dist/restricted/notify.d.cts +311 -13
  75. package/dist/restricted/notify.d.cts.map +1 -1
  76. package/dist/restricted/notify.d.mts +311 -13
  77. package/dist/restricted/notify.d.mts.map +1 -1
  78. package/dist/restricted/notify.mjs +52 -8
  79. package/dist/restricted/notify.mjs.map +1 -1
  80. package/dist/utils.cjs +8 -4
  81. package/dist/utils.cjs.map +1 -1
  82. package/dist/utils.d.cts +12 -3
  83. package/dist/utils.d.cts.map +1 -1
  84. package/dist/utils.d.mts +12 -3
  85. package/dist/utils.d.mts.map +1 -1
  86. package/dist/utils.mjs +8 -4
  87. package/dist/utils.mjs.map +1 -1
  88. package/package.json +5 -5
@@ -1 +1 @@
1
- {"version":3,"file":"getBip32PublicKey.d.mts","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,uBAAuB,EACvB,uBAAuB,EACxB,4BAA4B;AAQ7B,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAGrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAGlD,QAAA,MAAM,UAAU,2BAA2B,CAAC;AAE5C,MAAM,MAAM,4BAA4B,GAAG;IACzC;;OAEG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IAEvC;;;;OAIG;IACH,gBAAgB,EAAE,CAAC,uBAAuB,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACvE,CAAC;AAEF,KAAK,4CAA4C,GAAG;IAClD,WAAW,EAAE,4BAA4B,CA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wB;;;;;;;;EAMpC,CAAC;AAyCF,eAAO,MAAM,wBAAwB;;;wBAtDnB,eAAe,gBAAgB;oBACnC,iBAAiB;8BACP,WAAW,sCAAsC,CAAC;wBACxD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;;;EAsD/B,CAAC;AAEZ;;;;;;;;;;;GAWG;AACH,wBAAgB,+BAA+B,CAAC,EAC9C,WAAW,EACX,gBAAgB,EAChB,qBAAqB,GACtB,EAAE,4BAA4B,UAErB,wBAAwB,uBAAuB,CAAC,KACrD,QAAQ,uBAAuB,CAAC,CAwBpC"}
@@ -39,6 +39,7 @@ const specificationBuilder = ({ methodHooks }) => {
39
39
  const methodHooks = {
40
40
  getMnemonic: true,
41
41
  getUnlockPromise: true,
42
+ getClientCryptography: true,
42
43
  };
43
44
  export const getBip32PublicKeyBuilder = Object.freeze({
44
45
  targetName,
@@ -52,10 +53,12 @@ export const getBip32PublicKeyBuilder = Object.freeze({
52
53
  * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
53
54
  * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
54
55
  * and prompts the user to unlock their MetaMask if it is locked.
56
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
57
+ * functions to use for the client.
55
58
  * @returns The method implementation which returns a public key.
56
59
  * @throws If the params are invalid.
57
60
  */
58
- export function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, }) {
61
+ export function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
59
62
  return async function getBip32PublicKey(args) {
60
63
  await getUnlockPromise(true);
61
64
  assertStruct(args.params, Bip32PublicKeyArgsStruct, 'Invalid BIP-32 public key params', rpcErrors.invalidParams);
@@ -64,6 +67,7 @@ export function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise,
64
67
  curve: params.curve,
65
68
  path: params.path,
66
69
  secretRecoveryPhrase: await getMnemonic(),
70
+ cryptographicFunctions: getClientCryptography(),
67
71
  });
68
72
  if (params.compressed) {
69
73
  return node.compressedPublicKey;
@@ -1 +1 @@
1
- {"version":3,"file":"getBip32PublicKey.mjs","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EACL,YAAY,EACZ,eAAe,EACf,WAAW,EACX,cAAc,EACf,8BAA8B;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,8BAA8B;AAElE,OAAO,EAAE,YAAY,EAAE,wBAAwB;AAG/C,OAAO,EAAE,OAAO,EAAE,qBAAiB;AAEnC,MAAM,UAAU,GAAG,wBAAwB,CAAC;AA4B5C,MAAM,CAAC,MAAM,wBAAwB,GAAG,YAAY,CAClD,MAAM,CAAC;IACL,IAAI,EAAE,eAAe;IACrB,KAAK,EAAE,WAAW;IAClB,UAAU,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;CAChC,CAAC,CACH,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,oBAAoB,GAItB,CAAC,EAAE,WAAW,EAAgD,EAAE,EAAE;IACpE,OAAO;QACL,cAAc,EAAE,cAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc,EAAE,CAAC,cAAc,CAAC,wBAAwB,CAAC;QACzD,oBAAoB,EAAE,+BAA+B,CAAC,WAAW,CAAC;QAClE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,OAAO,EAAE,MAAM,KAAK,CAAC;gBACrB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,cAAc,CAAC,wBAAwB,EAC3D,CAAC;gBACD,MAAM,SAAS,CAAC,aAAa,CAAC;oBAC5B,OAAO,EAAE,sBAAsB,cAAc,CAAC,wBAAwB,WAAW;iBAClF,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,YAAY,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAoD;IACnE,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,IAAI;CACvB,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC,MAAM,CAAC;IACpD,UAAU;IACV,oBAAoB;IACpB,WAAW;CACH,CAAC,CAAC;AAEZ;;;;;;;;;GASG;AACH,MAAM,UAAU,+BAA+B,CAAC,EAC9C,WAAW,EACX,gBAAgB,GACa;IAC7B,OAAO,KAAK,UAAU,iBAAiB,CACrC,IAAsD;QAEtD,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAE7B,YAAY,CACV,IAAI,CAAC,MAAM,EACX,wBAAwB,EACxB,kCAAkC,EAClC,SAAS,CAAC,aAAa,CACxB,CAAC;QAEF,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QACxB,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC;YACzB,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,oBAAoB,EAAE,MAAM,WAAW,EAAE;SAC1C,CAAC,CAAC;QAEH,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,mBAAmB,CAAC;QAClC,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32PublicKeyParams,\n GetBip32PublicKeyResult,\n} from '@metamask/snaps-sdk';\nimport {\n bip32entropy,\n Bip32PathStruct,\n CurveStruct,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport { boolean, object, optional } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { getNode } from '../utils';\n\nconst targetName = 'snap_getBip32PublicKey';\n\nexport type GetBip32PublicKeyMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n};\n\ntype GetBip32PublicKeySpecificationBuilderOptions = {\n methodHooks: GetBip32PublicKeyMethodHooks;\n};\n\ntype GetBip32PublicKeySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32PublicKeyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\nexport const Bip32PublicKeyArgsStruct = bip32entropy(\n object({\n path: Bip32PathStruct,\n curve: CurveStruct,\n compressed: optional(boolean()),\n }),\n);\n\n/**\n * The specification builder for the `snap_getBip32PublicKey` permission.\n * `snap_getBip32PublicKey` lets the Snap retrieve public keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32PublicKey` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32PublicKeySpecificationBuilderOptions,\n GetBip32PublicKeySpecification\n> = ({ methodHooks }: GetBip32PublicKeySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32PublicKeyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32PublicKeyMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n};\n\nexport const getBip32PublicKeyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32PublicKey`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @returns The method implementation which returns a public key.\n * @throws If the params are invalid.\n */\nexport function getBip32PublicKeyImplementation({\n getMnemonic,\n getUnlockPromise,\n}: GetBip32PublicKeyMethodHooks) {\n return async function getBip32PublicKey(\n args: RestrictedMethodOptions<GetBip32PublicKeyParams>,\n ): Promise<GetBip32PublicKeyResult> {\n await getUnlockPromise(true);\n\n assertStruct(\n args.params,\n Bip32PublicKeyArgsStruct,\n 'Invalid BIP-32 public key params',\n rpcErrors.invalidParams,\n );\n\n const { params } = args;\n const node = await getNode({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase: await getMnemonic(),\n });\n\n if (params.compressed) {\n return node.compressedPublicKey;\n }\n\n return node.publicKey;\n };\n}\n"]}
1
+ {"version":3,"file":"getBip32PublicKey.mjs","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EACL,YAAY,EACZ,eAAe,EACf,WAAW,EACX,cAAc,EACf,8BAA8B;AAC/B,OAAO,EAAE,OAAO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H,MAAM,UAAU,+BAA+B,CAAC,EAC9C,WAAW,EACX,gBAAgB,EAChB,qBAAqB,GACQ;IAC7B,OAAO,KAAK,UAAU,iBAAiB,CACrC,IAAsD;QAEtD,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAE7B,YAAY,CACV,IAAI,CAAC,MAAM,EACX,wBAAwB,EACxB,kCAAkC,EAClC,SAAS,CAAC,aAAa,CACxB,CAAC;QAEF,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QACxB,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC;YACzB,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,oBAAoB,EAAE,MAAM,WAAW,EAAE;YACzC,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;QAEH,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,mBAAmB,CAAC;QAClC,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32PublicKeyParams,\n GetBip32PublicKeyResult,\n} from '@metamask/snaps-sdk';\nimport {\n bip32entropy,\n Bip32PathStruct,\n CurveStruct,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport { boolean, object, optional } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { getNode } from '../utils';\n\nconst targetName = 'snap_getBip32PublicKey';\n\nexport type GetBip32PublicKeyMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip32PublicKeySpecificationBuilderOptions = {\n methodHooks: GetBip32PublicKeyMethodHooks;\n};\n\ntype GetBip32PublicKeySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32PublicKeyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\nexport const Bip32PublicKeyArgsStruct = bip32entropy(\n object({\n path: Bip32PathStruct,\n curve: CurveStruct,\n compressed: optional(boolean()),\n }),\n);\n\n/**\n * The specification builder for the `snap_getBip32PublicKey` permission.\n * `snap_getBip32PublicKey` lets the Snap retrieve public keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32PublicKey` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32PublicKeySpecificationBuilderOptions,\n GetBip32PublicKeySpecification\n> = ({ methodHooks }: GetBip32PublicKeySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32PublicKeyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32PublicKeyMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getBip32PublicKeyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32PublicKey`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a public key.\n * @throws If the params are invalid.\n */\nexport function getBip32PublicKeyImplementation({\n getMnemonic,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip32PublicKeyMethodHooks) {\n return async function getBip32PublicKey(\n args: RestrictedMethodOptions<GetBip32PublicKeyParams>,\n ): Promise<GetBip32PublicKeyResult> {\n await getUnlockPromise(true);\n\n assertStruct(\n args.params,\n Bip32PublicKeyArgsStruct,\n 'Invalid BIP-32 public key params',\n rpcErrors.invalidParams,\n );\n\n const { params } = args;\n const node = await getNode({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase: await getMnemonic(),\n cryptographicFunctions: getClientCryptography(),\n });\n\n if (params.compressed) {\n return node.compressedPublicKey;\n }\n\n return node.publicKey;\n };\n}\n"]}
@@ -36,6 +36,7 @@ const specificationBuilder = ({ methodHooks }) => {
36
36
  const methodHooks = {
37
37
  getMnemonic: true,
38
38
  getUnlockPromise: true,
39
+ getClientCryptography: true,
39
40
  };
40
41
  exports.getBip44EntropyBuilder = Object.freeze({
41
42
  targetName,
@@ -51,19 +52,17 @@ exports.getBip44EntropyBuilder = Object.freeze({
51
52
  * @param hooks.getUnlockPromise - A function that resolves once the MetaMask
52
53
  * extension is unlocked and prompts the user to unlock their MetaMask if it is
53
54
  * locked.
55
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
56
+ * functions to use for the client.
54
57
  * @returns The method implementation which returns a `BIP44CoinTypeNode`.
55
58
  * @throws If the params are invalid.
56
59
  */
57
- function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, }) {
60
+ function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
58
61
  return async function getBip44Entropy(args) {
59
62
  await getUnlockPromise(true);
60
63
  // `args.params` is validated by the decorator, so it's safe to assert here.
61
64
  const params = args.params;
62
- const node = await key_tree_1.BIP44CoinTypeNode.fromDerivationPath([
63
- await getMnemonic(),
64
- `bip32:44'`,
65
- `bip32:${params.coinType}'`,
66
- ]);
65
+ const node = await key_tree_1.BIP44CoinTypeNode.fromDerivationPath([await getMnemonic(), `bip32:44'`, `bip32:${params.coinType}'`], 'mainnet', getClientCryptography());
67
66
  return node.toJSON();
68
67
  };
69
68
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getBip44Entropy.cjs","sourceRoot":"","sources":["../../src/restricted/getBip44Entropy.ts"],"names":[],"mappings":";;;AAAA,iDAAuD;AAOvD,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAAuD;AAKvD,MAAM,UAAU,GAAG,sBAAsB,CAAC;AA4B1C;;;;;;;;;GASG;AACH,MAAM,oBAAoB,GAItB,CAAC,EAAE,WAAW,EAA8C,EAAE,EAAE;IAClE,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc,EAAE,CAAC,4BAAc,CAAC,kBAAkB,CAAC;QACnD,oBAAoB,EAAE,6BAA6B,CAAC,WAAW,CAAC;QAChE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,OAAO,EAAE,MAAM,KAAK,CAAC;gBACrB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,kBAAkB,EACrD,CAAC;gBACD,MAAM,sBAAS,CAAC,aAAa,CAAC;oBAC5B,OAAO,EAAE,sBAAsB,4BAAc,CAAC,kBAAkB,WAAW;iBAC5E,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAkD;IACjE,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,IAAI;CACvB,CAAC;AAEW,QAAA,sBAAsB,GAAG,MAAM,CAAC,MAAM,CAAC;IAClD,UAAU;IACV,oBAAoB;IACpB,WAAW;CACH,CAAC,CAAC;AAEZ;;;;;;;;;;;GAWG;AACH,SAAgB,6BAA6B,CAAC,EAC5C,WAAW,EACX,gBAAgB,GACW;IAC3B,OAAO,KAAK,UAAU,eAAe,CACnC,IAAoD;QAEpD,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAE7B,4EAA4E;QAC5E,MAAM,MAAM,GAAG,IAAI,CAAC,MAA+B,CAAC;QAEpD,MAAM,IAAI,GAAG,MAAM,4BAAiB,CAAC,kBAAkB,CAAC;YACtD,MAAM,WAAW,EAAE;YACnB,WAAW;YACX,SAAS,MAAM,CAAC,QAAQ,GAAG;SAC5B,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AApBD,sEAoBC","sourcesContent":["import { BIP44CoinTypeNode } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip44EntropyParams,\n GetBip44EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst targetName = 'snap_getBip44Entropy';\n\nexport type GetBip44EntropyMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n};\n\ntype GetBip44EntropySpecificationBuilderOptions = {\n methodHooks: GetBip44EntropyMethodHooks;\n};\n\ntype GetBip44EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip44EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip44Entropy` permission.\n * `snap_getBip44Entropy_*` lets the Snap control private keys for a particular\n * BIP-32 coin type.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_getBip44Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip44EntropySpecificationBuilderOptions,\n GetBip44EntropySpecification\n> = ({ methodHooks }: GetBip44EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedCoinTypes],\n methodImplementation: getBip44EntropyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedCoinTypes\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedCoinTypes}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip44EntropyMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n};\n\nexport const getBip44EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip44Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase\n * of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @returns The method implementation which returns a `BIP44CoinTypeNode`.\n * @throws If the params are invalid.\n */\nexport function getBip44EntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n}: GetBip44EntropyMethodHooks) {\n return async function getBip44Entropy(\n args: RestrictedMethodOptions<GetBip44EntropyParams>,\n ): Promise<GetBip44EntropyResult> {\n await getUnlockPromise(true);\n\n // `args.params` is validated by the decorator, so it's safe to assert here.\n const params = args.params as GetBip44EntropyParams;\n\n const node = await BIP44CoinTypeNode.fromDerivationPath([\n await getMnemonic(),\n `bip32:44'`,\n `bip32:${params.coinType}'`,\n ]);\n\n return node.toJSON();\n };\n}\n"]}
1
+ {"version":3,"file":"getBip44Entropy.cjs","sourceRoot":"","sources":["../../src/restricted/getBip44Entropy.ts"],"names":[],"mappings":";;;AACA,iDAAuD;AAOvD,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAAuD;AAKvD,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAqC1C;;;;;;;;;GASG;AACH,MAAM,oBAAoB,GAItB,CAAC,EAAE,WAAW,EAA8C,EAAE,EAAE;IAClE,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc,EAAE,CAAC,4BAAc,CAAC,kBAAkB,CAAC;QACnD,oBAAoB,EAAE,6BAA6B,CAAC,WAAW,CAAC;QAChE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,OAAO,EAAE,MAAM,KAAK,CAAC;gBACrB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,4BAAc,CAAC,kBAAkB,EACrD,CAAC;gBACD,MAAM,sBAAS,CAAC,aAAa,CAAC;oBAC5B,OAAO,EAAE,sBAAsB,4BAAc,CAAC,kBAAkB,WAAW;iBAC5E,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAkD;IACjE,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,IAAI;IACtB,qBAAqB,EAAE,IAAI;CAC5B,CAAC;AAEW,QAAA,sBAAsB,GAAG,MAAM,CAAC,MAAM,CAAC;IAClD,UAAU;IACV,oBAAoB;IACpB,WAAW;CACH,CAAC,CAAC;AAEZ;;;;;;;;;;;;;GAaG;AACH,SAAgB,6BAA6B,CAAC,EAC5C,WAAW,EACX,gBAAgB,EAChB,qBAAqB,GACM;IAC3B,OAAO,KAAK,UAAU,eAAe,CACnC,IAAoD;QAEpD,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAE7B,4EAA4E;QAC5E,MAAM,MAAM,GAAG,IAAI,CAAC,MAA+B,CAAC;QAEpD,MAAM,IAAI,GAAG,MAAM,4BAAiB,CAAC,kBAAkB,CACrD,CAAC,MAAM,WAAW,EAAE,EAAE,WAAW,EAAE,SAAS,MAAM,CAAC,QAAQ,GAAG,CAAC,EAC/D,SAAS,EACT,qBAAqB,EAAE,CACxB,CAAC;QAEF,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AArBD,sEAqBC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport { BIP44CoinTypeNode } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip44EntropyParams,\n GetBip44EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst targetName = 'snap_getBip44Entropy';\n\nexport type GetBip44EntropyMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip44EntropySpecificationBuilderOptions = {\n methodHooks: GetBip44EntropyMethodHooks;\n};\n\ntype GetBip44EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip44EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip44Entropy` permission.\n * `snap_getBip44Entropy_*` lets the Snap control private keys for a particular\n * BIP-32 coin type.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_getBip44Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip44EntropySpecificationBuilderOptions,\n GetBip44EntropySpecification\n> = ({ methodHooks }: GetBip44EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedCoinTypes],\n methodImplementation: getBip44EntropyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedCoinTypes\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedCoinTypes}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip44EntropyMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getBip44EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip44Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase\n * of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a `BIP44CoinTypeNode`.\n * @throws If the params are invalid.\n */\nexport function getBip44EntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip44EntropyMethodHooks) {\n return async function getBip44Entropy(\n args: RestrictedMethodOptions<GetBip44EntropyParams>,\n ): Promise<GetBip44EntropyResult> {\n await getUnlockPromise(true);\n\n // `args.params` is validated by the decorator, so it's safe to assert here.\n const params = args.params as GetBip44EntropyParams;\n\n const node = await BIP44CoinTypeNode.fromDerivationPath(\n [await getMnemonic(), `bip32:44'`, `bip32:${params.coinType}'`],\n 'mainnet',\n getClientCryptography(),\n );\n\n return node.toJSON();\n };\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import type { CryptographicFunctions } from "@metamask/key-tree";
1
2
  import type { PermissionSpecificationBuilder, PermissionValidatorConstraint, RestrictedMethodOptions } from "@metamask/permission-controller";
2
3
  import { PermissionType } from "@metamask/permission-controller";
3
4
  import type { GetBip44EntropyParams, GetBip44EntropyResult } from "@metamask/snaps-sdk";
@@ -15,6 +16,14 @@ export type GetBip44EntropyMethodHooks = {
15
16
  * @returns A promise that resolves once the extension is unlocked.
16
17
  */
17
18
  getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
19
+ /**
20
+ * Get the cryptographic functions to use for the client. This may return an
21
+ * empty object or `undefined` to fall back to the default cryptographic
22
+ * functions.
23
+ *
24
+ * @returns The cryptographic functions to use for the client.
25
+ */
26
+ getClientCryptography: () => CryptographicFunctions | undefined;
18
27
  };
19
28
  type GetBip44EntropySpecificationBuilderOptions = {
20
29
  methodHooks: GetBip44EntropyMethodHooks;
@@ -39,9 +48,11 @@ export declare const getBip44EntropyBuilder: Readonly<{
39
48
  * @param hooks.getUnlockPromise - A function that resolves once the MetaMask
40
49
  * extension is unlocked and prompts the user to unlock their MetaMask if it is
41
50
  * locked.
51
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
52
+ * functions to use for the client.
42
53
  * @returns The method implementation which returns a `BIP44CoinTypeNode`.
43
54
  * @throws If the params are invalid.
44
55
  */
45
- export declare function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, }: GetBip44EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip44EntropyParams>) => Promise<GetBip44EntropyResult>;
56
+ export declare function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetBip44EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip44EntropyParams>) => Promise<GetBip44EntropyResult>;
46
57
  export {};
47
58
  //# sourceMappingURL=getBip44Entropy.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getBip44Entropy.d.cts","sourceRoot":"","sources":["../../src/restricted/getBip44Entropy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACtB,4BAA4B;AAE7B,OAAO,KAAK,E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,WAAW,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IAEvC;;;;OAIG;IACH,gBAAgB,EAAE,CAAC,uBAAuB,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtE;;;;;;OAMG;IACH,qBAAqB,EAAE,MAAM,sBAAsB,GAAG,SAAS,CAAC;CACjE,CAAC;AAEF,KAAK,0CAA0C,GAAG;IAChD,WAAW,EAAE,0BAA0B,CAAC;CACzC,CAAC;AAkDF,eAAO,MAAM,sBAAsB;;;wBA/CjB,eAAe,gBAAgB;oBACnC,iBAAiB;8BACP,WAAW,oCAAoC,CAAC;wBACtD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;;;EA+C/B,CAAC;AAEZ;;;;;;;;;;;;;GAaG;AACH,wBAAgB,6BAA6B,CAAC,EAC5C,WAAW,EACX,gBAAgB,EAChB,qBAAqB,GACtB,EAAE,0BAA0B,UAEnB,wBAAwB,qBAAqB,CAAC,KACnD,QAAQ,qBAAqB,CAAC,CAclC"}
@@ -1,3 +1,4 @@
1
+ import type { CryptographicFunctions } from "@metamask/key-tree";
1
2
  import type { PermissionSpecificationBuilder, PermissionValidatorConstraint, RestrictedMethodOptions } from "@metamask/permission-controller";
2
3
  import { PermissionType } from "@metamask/permission-controller";
3
4
  import type { GetBip44EntropyParams, GetBip44EntropyResult } from "@metamask/snaps-sdk";
@@ -15,6 +16,14 @@ export type GetBip44EntropyMethodHooks = {
15
16
  * @returns A promise that resolves once the extension is unlocked.
16
17
  */
17
18
  getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
19
+ /**
20
+ * Get the cryptographic functions to use for the client. This may return an
21
+ * empty object or `undefined` to fall back to the default cryptographic
22
+ * functions.
23
+ *
24
+ * @returns The cryptographic functions to use for the client.
25
+ */
26
+ getClientCryptography: () => CryptographicFunctions | undefined;
18
27
  };
19
28
  type GetBip44EntropySpecificationBuilderOptions = {
20
29
  methodHooks: GetBip44EntropyMethodHooks;
@@ -39,9 +48,11 @@ export declare const getBip44EntropyBuilder: Readonly<{
39
48
  * @param hooks.getUnlockPromise - A function that resolves once the MetaMask
40
49
  * extension is unlocked and prompts the user to unlock their MetaMask if it is
41
50
  * locked.
51
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
52
+ * functions to use for the client.
42
53
  * @returns The method implementation which returns a `BIP44CoinTypeNode`.
43
54
  * @throws If the params are invalid.
44
55
  */
45
- export declare function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, }: GetBip44EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip44EntropyParams>) => Promise<GetBip44EntropyResult>;
56
+ export declare function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetBip44EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip44EntropyParams>) => Promise<GetBip44EntropyResult>;
46
57
  export {};
47
58
  //# sourceMappingURL=getBip44Entropy.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getBip44Entropy.d.mts","sourceRoot":"","sources":["../../src/restricted/getBip44Entropy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACtB,4BAA4B;AAE7B,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAErD,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAElD,QAAA,MAAM,UAAU,yBAAyB,CAAC;AAE1C,MAAM,MAAM,0BAA0B,GAAG;IACvC;;OAEG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IAEvC;;;;OAIG;IACH,gBAAgB,EAAE,CAAC,uBAAuB,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACvE,CAAC;AAEF,KAAK,0CAA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jB,eAAe,gBAAgB;oBACnC,iBAAiB;8BACP,WAAW,oCAAoC,CAAC;wBACtD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;;;EA+C/B,CAAC;AAEZ;;;;;;;;;;;;;GAaG;AACH,wBAAgB,6BAA6B,CAAC,EAC5C,WAAW,EACX,gBAAgB,EAChB,qBAAqB,GACtB,EAAE,0BAA0B,UAEnB,wBAAwB,qBAAqB,CAAC,KACnD,QAAQ,qBAAqB,CAAC,CAclC"}
@@ -33,6 +33,7 @@ const specificationBuilder = ({ methodHooks }) => {
33
33
  const methodHooks = {
34
34
  getMnemonic: true,
35
35
  getUnlockPromise: true,
36
+ getClientCryptography: true,
36
37
  };
37
38
  export const getBip44EntropyBuilder = Object.freeze({
38
39
  targetName,
@@ -48,19 +49,17 @@ export const getBip44EntropyBuilder = Object.freeze({
48
49
  * @param hooks.getUnlockPromise - A function that resolves once the MetaMask
49
50
  * extension is unlocked and prompts the user to unlock their MetaMask if it is
50
51
  * locked.
52
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
53
+ * functions to use for the client.
51
54
  * @returns The method implementation which returns a `BIP44CoinTypeNode`.
52
55
  * @throws If the params are invalid.
53
56
  */
54
- export function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, }) {
57
+ export function getBip44EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
55
58
  return async function getBip44Entropy(args) {
56
59
  await getUnlockPromise(true);
57
60
  // `args.params` is validated by the decorator, so it's safe to assert here.
58
61
  const params = args.params;
59
- const node = await BIP44CoinTypeNode.fromDerivationPath([
60
- await getMnemonic(),
61
- `bip32:44'`,
62
- `bip32:${params.coinType}'`,
63
- ]);
62
+ const node = await BIP44CoinTypeNode.fromDerivationPath([await getMnemonic(), `bip32:44'`, `bip32:${params.coinType}'`], 'mainnet', getClientCryptography());
64
63
  return node.toJSON();
65
64
  };
66
65
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getB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e,CACnC,IAAoD;QAEpD,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAE7B,4EAA4E;QAC5E,MAAM,MAAM,GAAG,IAAI,CAAC,MAA+B,CAAC;QAEpD,MAAM,IAAI,GAAG,MAAM,iBAAiB,CAAC,kBAAkB,CAAC;YACtD,MAAM,WAAW,EAAE;YACnB,WAAW;YACX,SAAS,MAAM,CAAC,QAAQ,GAAG;SAC5B,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { BIP44CoinTypeNode } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip44EntropyParams,\n GetBip44EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst targetName = 'snap_getBip44Entropy';\n\nexport type GetBip44EntropyMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n};\n\ntype GetBip44EntropySpecificationBuilderOptions = {\n methodHooks: GetBip44EntropyMethodHooks;\n};\n\ntype GetBip44EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip44EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip44Entropy` permission.\n * `snap_getBip44Entropy_*` lets the Snap control private keys for a particular\n * BIP-32 coin type.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_getBip44Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip44EntropySpecificationBuilderOptions,\n GetBip44EntropySpecification\n> = ({ methodHooks }: GetBip44EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedCoinTypes],\n methodImplementation: getBip44EntropyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedCoinTypes\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedCoinTypes}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip44EntropyMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n};\n\nexport const getBip44EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip44Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase\n * of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @returns The method implementation which returns a `BIP44CoinTypeNode`.\n * @throws If the params are invalid.\n */\nexport function getBip44EntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n}: GetBip44EntropyMethodHooks) {\n return async function getBip44Entropy(\n args: RestrictedMethodOptions<GetBip44EntropyParams>,\n ): Promise<GetBip44EntropyResult> {\n await getUnlockPromise(true);\n\n // `args.params` is validated by the decorator, so it's safe to assert here.\n const params = args.params as GetBip44EntropyParams;\n\n const node = await BIP44CoinTypeNode.fromDerivationPath([\n await getMnemonic(),\n `bip32:44'`,\n `bip32:${params.coinType}'`,\n ]);\n\n return node.toJSON();\n };\n}\n"]}
1
+ {"version":3,"file":"getBip44Entropy.mjs","sourceRoot":"","sources":["../../src/restricted/getBip44Entropy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,2BAA2B;AAOvD,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EAAE,cAAc,EAAE,8BAA8B;AAKvD,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAqC1C;;;;;;;;;GASG;AACH,MAAM,oBAAoB,GAItB,CAAC,EAAE,WAAW,EAA8C,EAAE,EAAE;IAClE,OAAO;QACL,cAAc,EAAE,cAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc,EAAE,CAAC,cAAc,CAAC,kBAAkB,CAAC;QACnD,oBAAoB,EAAE,6BAA6B,CAAC,WAAW,CAAC;QAChE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACzB,IACE,OAAO,EAAE,MAAM,KAAK,CAAC;gBACrB,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,cAAc,CAAC,kBAAkB,EACrD,CAAC;gBACD,MAAM,SAAS,CAAC,aAAa,CAAC;oBAC5B,OAAO,EAAE,sBAAsB,cAAc,CAAC,kBAAkB,WAAW;iBAC5E,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,YAAY,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAkD;IACjE,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,IAAI;IACtB,qBAAqB,EAAE,IAAI;CAC5B,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC,MAAM,CAAC;IAClD,UAAU;IACV,oBAAoB;IACpB,WAAW;CACH,CAAC,CAAC;AAEZ;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,6BAA6B,CAAC,EAC5C,WAAW,EACX,gBAAgB,EAChB,qBAAqB,GACM;IAC3B,OAAO,KAAK,UAAU,eAAe,CACnC,IAAoD;QAEpD,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAE7B,4EAA4E;QAC5E,MAAM,MAAM,GAAG,IAAI,CAAC,MAA+B,CAAC;QAEpD,MAAM,IAAI,GAAG,MAAM,iBAAiB,CAAC,kBAAkB,CACrD,CAAC,MAAM,WAAW,EAAE,EAAE,WAAW,EAAE,SAAS,MAAM,CAAC,QAAQ,GAAG,CAAC,EAC/D,SAAS,EACT,qBAAqB,EAAE,CACxB,CAAC;QAEF,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport { BIP44CoinTypeNode } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip44EntropyParams,\n GetBip44EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst targetName = 'snap_getBip44Entropy';\n\nexport type GetBip44EntropyMethodHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip44EntropySpecificationBuilderOptions = {\n methodHooks: GetBip44EntropyMethodHooks;\n};\n\ntype GetBip44EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip44EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip44Entropy` permission.\n * `snap_getBip44Entropy_*` lets the Snap control private keys for a particular\n * BIP-32 coin type.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_getBip44Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip44EntropySpecificationBuilderOptions,\n GetBip44EntropySpecification\n> = ({ methodHooks }: GetBip44EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedCoinTypes],\n methodImplementation: getBip44EntropyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedCoinTypes\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedCoinTypes}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip44EntropyMethodHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getBip44EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip44Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase\n * of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a `BIP44CoinTypeNode`.\n * @throws If the params are invalid.\n */\nexport function getBip44EntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip44EntropyMethodHooks) {\n return async function getBip44Entropy(\n args: RestrictedMethodOptions<GetBip44EntropyParams>,\n ): Promise<GetBip44EntropyResult> {\n await getUnlockPromise(true);\n\n // `args.params` is validated by the decorator, so it's safe to assert here.\n const params = args.params as GetBip44EntropyParams;\n\n const node = await BIP44CoinTypeNode.fromDerivationPath(\n [await getMnemonic(), `bip32:44'`, `bip32:${params.coinType}'`],\n 'mainnet',\n getClientCryptography(),\n );\n\n return node.toJSON();\n };\n}\n"]}
@@ -24,6 +24,7 @@ const specificationBuilder = ({ allowedCaveats = null, methodHooks, }) => {
24
24
  const methodHooks = {
25
25
  getMnemonic: true,
26
26
  getUnlockPromise: true,
27
+ getClientCryptography: true,
27
28
  };
28
29
  exports.getEntropyBuilder = Object.freeze({
29
30
  targetName,
@@ -40,9 +41,11 @@ exports.getEntropyBuilder = Object.freeze({
40
41
  * primary keyring.
41
42
  * @param hooks.getUnlockPromise - The method to get a promise that resolves
42
43
  * once the extension is unlocked.
44
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
45
+ * functions to use for the client.
43
46
  * @returns The method implementation.
44
47
  */
45
- function getEntropyImplementation({ getMnemonic, getUnlockPromise, }) {
48
+ function getEntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
46
49
  return async function getEntropy(options) {
47
50
  const { params, context: { origin }, } = options;
48
51
  (0, utils_1.assertStruct)(params, exports.GetEntropyArgsStruct, 'Invalid "snap_getEntropy" parameters', rpc_errors_1.rpcErrors.invalidParams);
@@ -53,6 +56,7 @@ function getEntropyImplementation({ getMnemonic, getUnlockPromise, }) {
53
56
  salt: params.salt,
54
57
  mnemonicPhrase,
55
58
  magic: snaps_utils_1.SIP_6_MAGIC_VALUE,
59
+ cryptographicFunctions: getClientCryptography(),
56
60
  });
57
61
  };
58
62
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getEntropy.cjs","sourceRoot":"","sources":["../../src/restricted/getEntropy.ts"],"names":[],"mappings":";;;AAKA,2EAA8E;AAC9E,qDAAiD;AAEjD,uDAA0D;AAE1D,uDAA0E;AAE1E,2CAA+C;AAG/C,wCAAyC;AAEzC,MAAM,UAAU,GAAG,iBAAiB,CAAC;AAcxB,QAAA,oBAAoB,GAAG,IAAA,oBAAM,EAAC;IACzC,OAAO,EAAE,IAAA,qBAAO,EAAC,CAAC,CAAC;IACnB,IAAI,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;CACzB,CAAC,CAAC;AAUH,MAAM,oBAAoB,GAItB,CAAC,EACH,cAAc,GAAG,IAAI,EACrB,WAAW,GAC2B,EAAE,EAAE;IAC1C,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc;QACd,oBAAoB,EAAE,wBAAwB,CAAC,WAAW,CAAC;QAC3D,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,WAAW,GAAuC;IACtD,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,IAAI;CACvB,CAAC;AAEW,QAAA,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7C,UAAU;IACV,oBAAoB;IACpB,WAAW;CACH,CAAC,CAAC;AAgBZ;;;;;;;;;;;GAWG;AACH,SAAS,wBAAwB,CAAC,EAChC,WAAW,EACX,gBAAgB,GACA;IAChB,OAAO,KAAK,UAAU,UAAU,CAC9B,OAAkD;QAElD,MAAM,EACJ,MAAM,EACN,OAAO,EAAE,EAAE,MAAM,EAAE,GACpB,GAAG,OAAO,CAAC;QAEZ,IAAA,oBAAY,EACV,MAAM,EACN,4BAAoB,EACpB,sCAAsC,EACtC,sBAAS,CAAC,aAAa,CACxB,CAAC;QAEF,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAC7B,MAAM,cAAc,GAAG,MAAM,WAAW,EAAE,CAAC;QAE3C,OAAO,IAAA,qBAAa,EAAC;YACnB,KAAK,EAAE,MAAM;YACb,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,cAAc;YACd,KAAK,EAAE,+BAAiB;SACzB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","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 type { GetEntropyParams, GetEntropyResult } from '@metamask/snaps-sdk';\nimport { SIP_6_MAGIC_VALUE } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { literal, object, optional, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\nconst targetName = 'snap_getEntropy';\n\ntype GetEntropySpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: GetEntropyHooks;\n};\n\ntype GetEntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getEntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\nexport const GetEntropyArgsStruct = object({\n version: literal(1),\n salt: optional(string()),\n});\n\n/**\n * @property version - The version of the `snap_getEntropy` method. This must be\n * the numeric literal `1`.\n * @property salt - A string to use as the salt when deriving the entropy. If\n * omitted, the salt will be an empty string.\n */\nexport type GetEntropyArgs = Infer<typeof GetEntropyArgsStruct>;\n\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetEntropySpecificationBuilderOptions,\n GetEntropySpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: GetEntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats,\n methodImplementation: getEntropyImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetEntropyHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n};\n\nexport const getEntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport type GetEntropyHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n};\n\n/**\n * Builds the method implementation for `snap_getEntropy`. The implementation\n * is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - The method to get the mnemonic of the user's\n * primary keyring.\n * @param hooks.getUnlockPromise - The method to get a promise that resolves\n * once the extension is unlocked.\n * @returns The method implementation.\n */\nfunction getEntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n}: GetEntropyHooks) {\n return async function getEntropy(\n options: RestrictedMethodOptions<GetEntropyParams>,\n ): Promise<GetEntropyResult> {\n const {\n params,\n context: { origin },\n } = options;\n\n assertStruct(\n params,\n GetEntropyArgsStruct,\n 'Invalid \"snap_getEntropy\" parameters',\n rpcErrors.invalidParams,\n );\n\n await getUnlockPromise(true);\n const mnemonicPhrase = await getMnemonic();\n\n return deriveEntropy({\n input: origin,\n salt: params.salt,\n mnemonicPhrase,\n magic: SIP_6_MAGIC_VALUE,\n });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,CAAC;QAEF,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAC7B,MAAM,cAAc,GAAG,MAAM,WAAW,EAAE,CAAC;QAE3C,OAAO,IAAA,qBAAa,EAAC;YACnB,KAAK,EAAE,MAAM;YACb,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,cAAc;YACd,KAAK,EAAE,+BAAiB;YACxB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport 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 type { GetEntropyParams, GetEntropyResult } from '@metamask/snaps-sdk';\nimport { SIP_6_MAGIC_VALUE } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { literal, object, optional, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\nconst targetName = 'snap_getEntropy';\n\ntype GetEntropySpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: GetEntropyHooks;\n};\n\ntype GetEntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getEntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\nexport const GetEntropyArgsStruct = object({\n version: literal(1),\n salt: optional(string()),\n});\n\n/**\n * @property version - The version of the `snap_getEntropy` method. This must be\n * the numeric literal `1`.\n * @property salt - A string to use as the salt when deriving the entropy. If\n * omitted, the salt will be an empty string.\n */\nexport type GetEntropyArgs = Infer<typeof GetEntropyArgsStruct>;\n\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetEntropySpecificationBuilderOptions,\n GetEntropySpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: GetEntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats,\n methodImplementation: getEntropyImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetEntropyHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getEntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport type GetEntropyHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\n/**\n * Builds the method implementation for `snap_getEntropy`. The implementation\n * is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - The method to get the mnemonic of the user's\n * primary keyring.\n * @param hooks.getUnlockPromise - The method to get a promise that resolves\n * once the extension is unlocked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation.\n */\nfunction getEntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n getClientCryptography,\n}: GetEntropyHooks) {\n return async function getEntropy(\n options: RestrictedMethodOptions<GetEntropyParams>,\n ): Promise<GetEntropyResult> {\n const {\n params,\n context: { origin },\n } = options;\n\n assertStruct(\n params,\n GetEntropyArgsStruct,\n 'Invalid \"snap_getEntropy\" parameters',\n rpcErrors.invalidParams,\n );\n\n await getUnlockPromise(true);\n const mnemonicPhrase = await getMnemonic();\n\n return deriveEntropy({\n input: origin,\n salt: params.salt,\n mnemonicPhrase,\n magic: SIP_6_MAGIC_VALUE,\n cryptographicFunctions: getClientCryptography(),\n });\n };\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import type { CryptographicFunctions } from "@metamask/key-tree";
1
2
  import type { PermissionSpecificationBuilder, RestrictedMethodOptions } from "@metamask/permission-controller";
2
3
  import { PermissionType } from "@metamask/permission-controller";
3
4
  import type { GetEntropyParams, GetEntropyResult } from "@metamask/snaps-sdk";
@@ -44,6 +45,14 @@ export type GetEntropyHooks = {
44
45
  * @returns A promise that resolves once the extension is unlocked.
45
46
  */
46
47
  getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
48
+ /**
49
+ * Get the cryptographic functions to use for the client. This may return an
50
+ * empty object or `undefined` to fall back to the default cryptographic
51
+ * functions.
52
+ *
53
+ * @returns The cryptographic functions to use for the client.
54
+ */
55
+ getClientCryptography: () => CryptographicFunctions | undefined;
47
56
  };
48
57
  /**
49
58
  * Builds the method implementation for `snap_getEntropy`. The implementation
@@ -55,8 +64,10 @@ export type GetEntropyHooks = {
55
64
  * primary keyring.
56
65
  * @param hooks.getUnlockPromise - The method to get a promise that resolves
57
66
  * once the extension is unlocked.
67
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
68
+ * functions to use for the client.
58
69
  * @returns The method implementation.
59
70
  */
60
- declare function getEntropyImplementation({ getMnemonic, getUnlockPromise, }: GetEntropyHooks): (options: RestrictedMethodOptions<GetEntropyParams>) => Promise<GetEntropyResult>;
71
+ declare function getEntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetEntropyHooks): (options: RestrictedMethodOptions<GetEntropyParams>) => Promise<GetEntropyResult>;
61
72
  export {};
62
73
  //# sourceMappingURL=getEntropy.d.cts.map
@@ -1 +1 @@
1
- {"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iBAAS,wBAAwB,CAAC,EAChC,WAAW,EACX,gBAAgB,EAChB,qBAAqB,GACtB,EAAE,eAAe,aAEL,wBAAwB,gBAAgB,CAAC,KACjD,QAAQ,gBAAgB,CAAC,CAwB7B"}
@@ -1,3 +1,4 @@
1
+ import type { CryptographicFunctions } from "@metamask/key-tree";
1
2
  import type { PermissionSpecificationBuilder, RestrictedMethodOptions } from "@metamask/permission-controller";
2
3
  import { PermissionType } from "@metamask/permission-controller";
3
4
  import type { GetEntropyParams, GetEntropyResult } from "@metamask/snaps-sdk";
@@ -44,6 +45,14 @@ export type GetEntropyHooks = {
44
45
  * @returns A promise that resolves once the extension is unlocked.
45
46
  */
46
47
  getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
48
+ /**
49
+ * Get the cryptographic functions to use for the client. This may return an
50
+ * empty object or `undefined` to fall back to the default cryptographic
51
+ * functions.
52
+ *
53
+ * @returns The cryptographic functions to use for the client.
54
+ */
55
+ getClientCryptography: () => CryptographicFunctions | undefined;
47
56
  };
48
57
  /**
49
58
  * Builds the method implementation for `snap_getEntropy`. The implementation
@@ -55,8 +64,10 @@ export type GetEntropyHooks = {
55
64
  * primary keyring.
56
65
  * @param hooks.getUnlockPromise - The method to get a promise that resolves
57
66
  * once the extension is unlocked.
67
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
68
+ * functions to use for the client.
58
69
  * @returns The method implementation.
59
70
  */
60
- declare function getEntropyImplementation({ getMnemonic, getUnlockPromise, }: GetEntropyHooks): (options: RestrictedMethodOptions<GetEntropyParams>) => Promise<GetEntropyResult>;
71
+ declare function getEntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetEntropyHooks): (options: RestrictedMethodOptions<GetEntropyParams>) => Promise<GetEntropyResult>;
61
72
  export {};
62
73
  //# sourceMappingURL=getEntropy.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getEntropy.d.mts","sourceRoot":"","sources":["../../src/restricted/getEntropy.ts"],"names":[],"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EAAE,eAAe,aAEL,wBAAwB,gBAAgB,CAAC,KACjD,QAAQ,gBAAgB,CAAC,CAwB7B"}
@@ -21,6 +21,7 @@ const specificationBuilder = ({ allowedCaveats = null, methodHooks, }) => {
21
21
  const methodHooks = {
22
22
  getMnemonic: true,
23
23
  getUnlockPromise: true,
24
+ getClientCryptography: true,
24
25
  };
25
26
  export const getEntropyBuilder = Object.freeze({
26
27
  targetName,
@@ -37,9 +38,11 @@ export const getEntropyBuilder = Object.freeze({
37
38
  * primary keyring.
38
39
  * @param hooks.getUnlockPromise - The method to get a promise that resolves
39
40
  * once the extension is unlocked.
41
+ * @param hooks.getClientCryptography - A function to retrieve the cryptographic
42
+ * functions to use for the client.
40
43
  * @returns The method implementation.
41
44
  */
42
- function getEntropyImplementation({ getMnemonic, getUnlockPromise, }) {
45
+ function getEntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
43
46
  return async function getEntropy(options) {
44
47
  const { params, context: { origin }, } = options;
45
48
  assertStruct(params, GetEntropyArgsStruct, 'Invalid "snap_getEntropy" parameters', rpcErrors.invalidParams);
@@ -50,6 +53,7 @@ function getEntropyImplementation({ getMnemonic, getUnlockPromise, }) {
50
53
  salt: params.salt,
51
54
  mnemonicPhrase,
52
55
  magic: SIP_6_MAGIC_VALUE,
56
+ cryptographicFunctions: getClientCryptography(),
53
57
  });
54
58
  };
55
59
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getEntropy.mjs","sourceRoot":"","sources":["../../src/restricted/getEntropy.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAE1D,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,8BAA8B;AAE1E,OAAO,EAAE,YAAY,EAAE,wBAAwB;AAG/C,OAAO,EAAE,aAAa,EAAE,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IACL,CAAC,CAAC;AACJ,CAAC","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 type { GetEntropyParams, GetEntropyResult } from '@metamask/snaps-sdk';\nimport { SIP_6_MAGIC_VALUE } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { literal, object, optional, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\nconst targetName = 'snap_getEntropy';\n\ntype GetEntropySpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: GetEntropyHooks;\n};\n\ntype GetEntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getEntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\nexport const GetEntropyArgsStruct = object({\n version: literal(1),\n salt: optional(string()),\n});\n\n/**\n * @property version - The version of the `snap_getEntropy` method. This must be\n * the numeric literal `1`.\n * @property salt - A string to use as the salt when deriving the entropy. If\n * omitted, the salt will be an empty string.\n */\nexport type GetEntropyArgs = Infer<typeof GetEntropyArgsStruct>;\n\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetEntropySpecificationBuilderOptions,\n GetEntropySpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: GetEntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats,\n methodImplementation: getEntropyImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetEntropyHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n};\n\nexport const getEntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport type GetEntropyHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n};\n\n/**\n * Builds the method implementation for `snap_getEntropy`. The implementation\n * is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - The method to get the mnemonic of the user's\n * primary keyring.\n * @param hooks.getUnlockPromise - The method to get a promise that resolves\n * once the extension is unlocked.\n * @returns The method implementation.\n */\nfunction getEntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n}: GetEntropyHooks) {\n return async function getEntropy(\n options: RestrictedMethodOptions<GetEntropyParams>,\n ): Promise<GetEntropyResult> {\n const {\n params,\n context: { origin },\n } = options;\n\n assertStruct(\n params,\n GetEntropyArgsStruct,\n 'Invalid \"snap_getEntropy\" parameters',\n rpcErrors.invalidParams,\n );\n\n await getUnlockPromise(true);\n const mnemonicPhrase = await getMnemonic();\n\n return deriveEntropy({\n input: origin,\n salt: params.salt,\n mnemonicPhrase,\n magic: SIP_6_MAGIC_VALUE,\n });\n };\n}\n"]}
1
+ {"version":3,"file":"getEntropy.mjs","sourceRoot":"","sources":["../../src/restricted/getEntropy.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAE1D,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,8BAA8B;AAE1E,OAAO,EAAE,YAAY,EAAE,wBAAwB;AAG/C,O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CAAC,IAAI;YACjB,cAAc;YACd,KAAK,EAAE,iBAAiB;YACxB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport 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 type { GetEntropyParams, GetEntropyResult } from '@metamask/snaps-sdk';\nimport { SIP_6_MAGIC_VALUE } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport { literal, object, optional, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\nconst targetName = 'snap_getEntropy';\n\ntype GetEntropySpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: GetEntropyHooks;\n};\n\ntype GetEntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getEntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\nexport const GetEntropyArgsStruct = object({\n version: literal(1),\n salt: optional(string()),\n});\n\n/**\n * @property version - The version of the `snap_getEntropy` method. This must be\n * the numeric literal `1`.\n * @property salt - A string to use as the salt when deriving the entropy. If\n * omitted, the salt will be an empty string.\n */\nexport type GetEntropyArgs = Infer<typeof GetEntropyArgsStruct>;\n\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetEntropySpecificationBuilderOptions,\n GetEntropySpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: GetEntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats,\n methodImplementation: getEntropyImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetEntropyHooks> = {\n getMnemonic: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getEntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport type GetEntropyHooks = {\n /**\n * @returns The mnemonic of the user's primary keyring.\n */\n getMnemonic: () => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\n/**\n * Builds the method implementation for `snap_getEntropy`. The implementation\n * is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - The method to get the mnemonic of the user's\n * primary keyring.\n * @param hooks.getUnlockPromise - The method to get a promise that resolves\n * once the extension is unlocked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation.\n */\nfunction getEntropyImplementation({\n getMnemonic,\n getUnlockPromise,\n getClientCryptography,\n}: GetEntropyHooks) {\n return async function getEntropy(\n options: RestrictedMethodOptions<GetEntropyParams>,\n ): Promise<GetEntropyResult> {\n const {\n params,\n context: { origin },\n } = options;\n\n assertStruct(\n params,\n GetEntropyArgsStruct,\n 'Invalid \"snap_getEntropy\" parameters',\n rpcErrors.invalidParams,\n );\n\n await getUnlockPromise(true);\n const mnemonicPhrase = await getMnemonic();\n\n return deriveEntropy({\n input: origin,\n salt: params.salt,\n mnemonicPhrase,\n magic: SIP_6_MAGIC_VALUE,\n cryptographicFunctions: getClientCryptography(),\n });\n };\n}\n"]}
@@ -53,14 +53,17 @@ exports.STORAGE_SIZE_LIMIT = 104857600; // In bytes (100MB)
53
53
  * @param args.snapId - The ID of the snap to get the encryption key for.
54
54
  * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key
55
55
  * from.
56
+ * @param args.cryptographicFunctions - The cryptographic functions to use for
57
+ * the client.
56
58
  * @returns The state encryption key.
57
59
  */
58
- async function getEncryptionEntropy({ mnemonicPhrase, snapId, }) {
60
+ async function getEncryptionEntropy({ mnemonicPhrase, snapId, cryptographicFunctions, }) {
59
61
  return await (0, utils_2.deriveEntropy)({
60
62
  mnemonicPhrase,
61
63
  input: snapId,
62
64
  salt: exports.STATE_ENCRYPTION_SALT,
63
65
  magic: snaps_utils_1.STATE_ENCRYPTION_MAGIC_VALUE,
66
+ cryptographicFunctions,
64
67
  });
65
68
  }
66
69
  exports.getEncryptionEntropy = getEncryptionEntropy;
@@ -1 +1 @@
1
- {"version":3,"file":"manageState.cjs","sourceRoot":"","sources":["../../src/restricted/manageState.ts"],"names":[],"mappings":";;;AAKA,2EAA8E;AAC9E,qDAAiD;AAEjD,mDAA2D;AAC3D,uDAAqE;AAErE,2CAAwD;AAGxD,wCAAyC;AAEzC,oDAAoD;AACvC,QAAA,qBAAqB,GAAG,6BAA6B,CAAC;AAEnE,MAAM,UAAU,GAAG,kBAAkB,CAAC;AAiDtC;;;;;;;;;GASG;AACI,MAAM,oBAAoB,GAI7B,CAAC,EACH,cAAc,GAAG,IAAI,EACrB,WAAW,GAC4B,EAAE,EAAE;IAC3C,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU,EAAE,UAAU;QACtB,cAAc;QACd,oBAAoB,EAAE,4BAA4B,CAAC,WAAW,CAAC;QAC/D,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAfW,QAAA,oBAAoB,wBAe/B;AAEF,MAAM,WA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AAI,SAAS,KAAK,gCAAoB,CAAC,WAAW,EAAE,CAAC;QACnD,IAAI,CAAC,IAAA,gBAAQ,EAAC,QAAQ,CAAC,EAAE,CAAC;YACxB,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,WAAW,MAAM,iEAAiE;gBAC3F,IAAI,EAAE;oBACJ,gBAAgB,EACd,OAAO,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ;iBAC3D;aACF,CAAC,CAAC;QACL,CAAC;QAED,IAAI,IAAI,CAAC;QACT,IAAI,CAAC;YACH,kEAAkE;YAClE,IAAI,GAAG,IAAA,mBAAW,EAAC,QAAQ,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,WAAW,MAAM,oEAAoE;gBAC9F,IAAI,EAAE;oBACJ,gBAAgB,EACd,OAAO,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ;iBAC3D;aACF,CAAC,CAAC;QACL,CAAC;QAED,IAAI,IAAI,GAAG,gBAAgB,EAAE,CAAC;YAC5B,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,WAAW,MAAM,2DAA2D,gBAAgB,iBAAiB;gBACtH,IAAI,EAAE;oBACJ,gBAAgB,EACd,OAAO,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ;iBAC3D;aACF,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,OAAO,MAA2B,CAAC;AACrC,CAAC;AApED,gDAoEC","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 type { ManageStateParams, ManageStateResult } from '@metamask/snaps-sdk';\nimport { ManageStateOperation } from '@metamask/snaps-sdk';\nimport { STATE_ENCRYPTION_MAGIC_VALUE } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { isObject, getJsonSize } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\n// The salt used for SIP-6-based entropy derivation.\nexport const STATE_ENCRYPTION_SALT = 'snap_manageState encryption';\n\nconst methodName = 'snap_manageState';\n\nexport type ManageStateMethodHooks = {\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * A function that clears the state of the requesting Snap.\n */\n clearSnapState: (snapId: string, encrypted: boolean) => void;\n\n /**\n * A function that gets the encrypted state of the requesting Snap.\n *\n * @returns The current state of the Snap.\n */\n getSnapState: (\n snapId: string,\n encrypted: boolean,\n ) => Promise<Record<string, Json>>;\n\n /**\n * A function that updates the state of the requesting Snap.\n *\n * @param newState - The new state of the Snap.\n */\n updateSnapState: (\n snapId: string,\n newState: Record<string, Json>,\n encrypted: boolean,\n ) => Promise<void>;\n};\n\ntype ManageStateSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: ManageStateMethodHooks;\n};\n\ntype ManageStateSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getManageStateImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_manageState` permission.\n * `snap_manageState` lets the Snap store and manage some of its state on\n * your device.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_manageState` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n ManageStateSpecificationBuilderOptions,\n ManageStateSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: ManageStateSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getManageStateImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<ManageStateMethodHooks> = {\n getUnlockPromise: true,\n clearSnapState: true,\n getSnapState: true,\n updateSnapState: true,\n};\n\nexport const manageStateBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport const STORAGE_SIZE_LIMIT = 104857600; // In bytes (100MB)\n\ntype GetEncryptionKeyArgs = {\n snapId: string;\n mnemonicPhrase: Uint8Array;\n};\n\n/**\n * Get a deterministic encryption key to use for encrypting and decrypting the\n * state.\n *\n * This key should only be used for state encryption using `snap_manageState`.\n * To get other encryption keys, a different salt can be used.\n *\n * @param args - The encryption key args.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @returns The state encryption key.\n */\nexport async function getEncryptionEntropy({\n mnemonicPhrase,\n snapId,\n}: GetEncryptionKeyArgs) {\n return await deriveEntropy({\n mnemonicPhrase,\n input: snapId,\n salt: STATE_ENCRYPTION_SALT,\n magic: STATE_ENCRYPTION_MAGIC_VALUE,\n });\n}\n\n/**\n * Builds the method implementation for `snap_manageState`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.clearSnapState - A function that clears the state stored for a\n * snap.\n * @param hooks.getSnapState - A function that fetches the persisted decrypted\n * state for a snap.\n * @param hooks.updateSnapState - A function that updates the state stored for a\n * snap.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @returns The method implementation which either returns `null` for a\n * successful state update/deletion or returns the decrypted state.\n * @throws If the params are invalid.\n */\nexport function getManageStateImplementation({\n getUnlockPromise,\n clearSnapState,\n getSnapState,\n updateSnapState,\n}: ManageStateMethodHooks) {\n return async function manageState(\n options: RestrictedMethodOptions<ManageStateParams>,\n ): Promise<ManageStateResult> {\n const {\n params = {},\n method,\n context: { origin },\n } = options;\n const validatedParams = getValidatedParams(params, method);\n\n // If the encrypted param is undefined or null we default to true.\n const shouldEncrypt = validatedParams.encrypted ?? true;\n\n // We only need to prompt the user when the mnemonic is needed\n // which it isn't for the clear operation or unencrypted storage.\n if (\n shouldEncrypt &&\n validatedParams.operation !== ManageStateOperation.ClearState\n ) {\n await getUnlockPromise(true);\n }\n\n switch (validatedParams.operation) {\n case ManageStateOperation.ClearState:\n clearSnapState(origin, shouldEncrypt);\n return null;\n\n case ManageStateOperation.GetState: {\n return await getSnapState(origin, shouldEncrypt);\n }\n\n case ManageStateOperation.UpdateState: {\n await updateSnapState(origin, validatedParams.newState, shouldEncrypt);\n return null;\n }\n\n default:\n throw rpcErrors.invalidParams(\n `Invalid ${method} operation: \"${\n validatedParams.operation as string\n }\"`,\n );\n }\n };\n}\n\n/**\n * Validates the manageState 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 method - RPC method name used for debugging errors.\n * @param storageSizeLimit - Maximum allowed size (in bytes) of a new state object.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(\n params: unknown,\n method: string,\n storageSizeLimit = STORAGE_SIZE_LIMIT,\n): ManageStateParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { operation, newState, encrypted } = params;\n\n if (\n !operation ||\n typeof operation !== 'string' ||\n !Object.values(ManageStateOperation).includes(\n operation as ManageStateOperation,\n )\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid manage state \"operation\".',\n });\n }\n\n if (encrypted !== undefined && typeof encrypted !== 'boolean') {\n throw rpcErrors.invalidParams({\n message: '\"encrypted\" parameter must be a boolean if specified.',\n });\n }\n\n if (operation === ManageStateOperation.UpdateState) {\n if (!isObject(newState)) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be a plain object.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n let size;\n try {\n // `getJsonSize` will throw if the state is not JSON serializable.\n size = getJsonSize(newState);\n } catch {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be JSON serializable.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n if (size > storageSizeLimit) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must not exceed ${storageSizeLimit} bytes in size.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n }\n\n return params as ManageStateParams;\n}\n"]}
1
+ {"version":3,"file":"manageState.cjs","sourceRoot":"","sources":["../../src/restricted/manageState.ts"],"names":[],"mappings":";;;AAMA,2EAA8E;AAC9E,qDAAiD;AAEjD,mDAA2D;AAC3D,uDAAqE;AAErE,2CAAwD;AAGxD,wCAAyC;AAEzC,oDAAoD;AACvC,QAAA,qBAAqB,GAAG,6BAA6B,CAAC;AAEnE,MAAM,UAAU,GAAG,kBAAkB,CAAC;AAiDtC;;;;;;;;;GASG;AACI,MAAM,oBAAoB,GAI7B,CAAC,EACH,cAAc,GAAG,IAAI,EACrB,WAAW,GAC4B,EAAE,EAAE;IAC3C,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU,EAAE,UAAU;QACtB,cAAc;QACd,oBAAoB,EAAE,4BAA4B,CAAC,WAAW,CAAC;QAC/D,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAfW,QAAA,oBAAoB,wBAe/B;AAEF,MAAM,WAAW,GAA8C;I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;IACL,CAAC;IAED,IAAI,SAAS,KAAK,gCAAoB,CAAC,WAAW,EAAE,CAAC;QACnD,IAAI,CAAC,IAAA,gBAAQ,EAAC,QAAQ,CAAC,EAAE,CAAC;YACxB,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,WAAW,MAAM,iEAAiE;gBAC3F,IAAI,EAAE;oBACJ,gBAAgB,EACd,OAAO,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ;iBAC3D;aACF,CAAC,CAAC;QACL,CAAC;QAED,IAAI,IAAI,CAAC;QACT,IAAI,CAAC;YACH,kEAAkE;YAClE,IAAI,GAAG,IAAA,mBAAW,EAAC,QAAQ,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,WAAW,MAAM,oEAAoE;gBAC9F,IAAI,EAAE;oBACJ,gBAAgB,EACd,OAAO,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ;iBAC3D;aACF,CAAC,CAAC;QACL,CAAC;QAED,IAAI,IAAI,GAAG,gBAAgB,EAAE,CAAC;YAC5B,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,WAAW,MAAM,2DAA2D,gBAAgB,iBAAiB;gBACtH,IAAI,EAAE;oBACJ,gBAAgB,EACd,OAAO,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ;iBAC3D;aACF,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,OAAO,MAA2B,CAAC;AACrC,CAAC;AApED,gDAoEC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport 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 type { ManageStateParams, ManageStateResult } from '@metamask/snaps-sdk';\nimport { ManageStateOperation } from '@metamask/snaps-sdk';\nimport { STATE_ENCRYPTION_MAGIC_VALUE } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { isObject, getJsonSize } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport { deriveEntropy } from '../utils';\n\n// The salt used for SIP-6-based entropy derivation.\nexport const STATE_ENCRYPTION_SALT = 'snap_manageState encryption';\n\nconst methodName = 'snap_manageState';\n\nexport type ManageStateMethodHooks = {\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * A function that clears the state of the requesting Snap.\n */\n clearSnapState: (snapId: string, encrypted: boolean) => void;\n\n /**\n * A function that gets the encrypted state of the requesting Snap.\n *\n * @returns The current state of the Snap.\n */\n getSnapState: (\n snapId: string,\n encrypted: boolean,\n ) => Promise<Record<string, Json>>;\n\n /**\n * A function that updates the state of the requesting Snap.\n *\n * @param newState - The new state of the Snap.\n */\n updateSnapState: (\n snapId: string,\n newState: Record<string, Json>,\n encrypted: boolean,\n ) => Promise<void>;\n};\n\ntype ManageStateSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: ManageStateMethodHooks;\n};\n\ntype ManageStateSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getManageStateImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_manageState` permission.\n * `snap_manageState` lets the Snap store and manage some of its state on\n * your device.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_manageState` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n ManageStateSpecificationBuilderOptions,\n ManageStateSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: ManageStateSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getManageStateImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<ManageStateMethodHooks> = {\n getUnlockPromise: true,\n clearSnapState: true,\n getSnapState: true,\n updateSnapState: true,\n};\n\nexport const manageStateBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\nexport const STORAGE_SIZE_LIMIT = 104857600; // In bytes (100MB)\n\ntype GetEncryptionKeyArgs = {\n snapId: string;\n mnemonicPhrase: Uint8Array;\n cryptographicFunctions?: CryptographicFunctions | undefined;\n};\n\n/**\n * Get a deterministic encryption key to use for encrypting and decrypting the\n * state.\n *\n * This key should only be used for state encryption using `snap_manageState`.\n * To get other encryption keys, a different salt can be used.\n *\n * @param args - The encryption key args.\n * @param args.snapId - The ID of the snap to get the encryption key for.\n * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key\n * from.\n * @param args.cryptographicFunctions - The cryptographic functions to use for\n * the client.\n * @returns The state encryption key.\n */\nexport async function getEncryptionEntropy({\n mnemonicPhrase,\n snapId,\n cryptographicFunctions,\n}: GetEncryptionKeyArgs) {\n return await deriveEntropy({\n mnemonicPhrase,\n input: snapId,\n salt: STATE_ENCRYPTION_SALT,\n magic: STATE_ENCRYPTION_MAGIC_VALUE,\n cryptographicFunctions,\n });\n}\n\n/**\n * Builds the method implementation for `snap_manageState`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.clearSnapState - A function that clears the state stored for a\n * snap.\n * @param hooks.getSnapState - A function that fetches the persisted decrypted\n * state for a snap.\n * @param hooks.updateSnapState - A function that updates the state stored for a\n * snap.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask\n * extension is unlocked and prompts the user to unlock their MetaMask if it is\n * locked.\n * @returns The method implementation which either returns `null` for a\n * successful state update/deletion or returns the decrypted state.\n * @throws If the params are invalid.\n */\nexport function getManageStateImplementation({\n getUnlockPromise,\n clearSnapState,\n getSnapState,\n updateSnapState,\n}: ManageStateMethodHooks) {\n return async function manageState(\n options: RestrictedMethodOptions<ManageStateParams>,\n ): Promise<ManageStateResult> {\n const {\n params = {},\n method,\n context: { origin },\n } = options;\n const validatedParams = getValidatedParams(params, method);\n\n // If the encrypted param is undefined or null we default to true.\n const shouldEncrypt = validatedParams.encrypted ?? true;\n\n // We only need to prompt the user when the mnemonic is needed\n // which it isn't for the clear operation or unencrypted storage.\n if (\n shouldEncrypt &&\n validatedParams.operation !== ManageStateOperation.ClearState\n ) {\n await getUnlockPromise(true);\n }\n\n switch (validatedParams.operation) {\n case ManageStateOperation.ClearState:\n clearSnapState(origin, shouldEncrypt);\n return null;\n\n case ManageStateOperation.GetState: {\n return await getSnapState(origin, shouldEncrypt);\n }\n\n case ManageStateOperation.UpdateState: {\n await updateSnapState(origin, validatedParams.newState, shouldEncrypt);\n return null;\n }\n\n default:\n throw rpcErrors.invalidParams(\n `Invalid ${method} operation: \"${\n validatedParams.operation as string\n }\"`,\n );\n }\n };\n}\n\n/**\n * Validates the manageState 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 method - RPC method name used for debugging errors.\n * @param storageSizeLimit - Maximum allowed size (in bytes) of a new state object.\n * @returns The validated method parameter object.\n */\nexport function getValidatedParams(\n params: unknown,\n method: string,\n storageSizeLimit = STORAGE_SIZE_LIMIT,\n): ManageStateParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { operation, newState, encrypted } = params;\n\n if (\n !operation ||\n typeof operation !== 'string' ||\n !Object.values(ManageStateOperation).includes(\n operation as ManageStateOperation,\n )\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid manage state \"operation\".',\n });\n }\n\n if (encrypted !== undefined && typeof encrypted !== 'boolean') {\n throw rpcErrors.invalidParams({\n message: '\"encrypted\" parameter must be a boolean if specified.',\n });\n }\n\n if (operation === ManageStateOperation.UpdateState) {\n if (!isObject(newState)) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be a plain object.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n let size;\n try {\n // `getJsonSize` will throw if the state is not JSON serializable.\n size = getJsonSize(newState);\n } catch {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must be JSON serializable.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n\n if (size > storageSizeLimit) {\n throw rpcErrors.invalidParams({\n message: `Invalid ${method} \"updateState\" parameter: The new state must not exceed ${storageSizeLimit} bytes in size.`,\n data: {\n receivedNewState:\n typeof newState === 'undefined' ? 'undefined' : newState,\n },\n });\n }\n }\n\n return params as ManageStateParams;\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import type { CryptographicFunctions } from "@metamask/key-tree";
1
2
  import type { PermissionSpecificationBuilder, RestrictedMethodOptions, ValidPermissionSpecification } from "@metamask/permission-controller";
2
3
  import { PermissionType } from "@metamask/permission-controller";
3
4
  import type { ManageStateParams, ManageStateResult } from "@metamask/snaps-sdk";
@@ -64,6 +65,7 @@ export declare const STORAGE_SIZE_LIMIT = 104857600;
64
65
  type GetEncryptionKeyArgs = {
65
66
  snapId: string;
66
67
  mnemonicPhrase: Uint8Array;
68
+ cryptographicFunctions?: CryptographicFunctions | undefined;
67
69
  };
68
70
  /**
69
71
  * Get a deterministic encryption key to use for encrypting and decrypting the
@@ -76,9 +78,11 @@ type GetEncryptionKeyArgs = {
76
78
  * @param args.snapId - The ID of the snap to get the encryption key for.
77
79
  * @param args.mnemonicPhrase - The mnemonic phrase to derive the encryption key
78
80
  * from.
81
+ * @param args.cryptographicFunctions - The cryptographic functions to use for
82
+ * the client.
79
83
  * @returns The state encryption key.
80
84
  */
81
- export declare function getEncryptionEntropy({ mnemonicPhrase, snapId, }: GetEncryptionKeyArgs): Promise<`0x${string}`>;
85
+ export declare function getEncryptionEntropy({ mnemonicPhrase, snapId, cryptographicFunctions, }: GetEncryptionKeyArgs): Promise<`0x${string}`>;
82
86
  /**
83
87
  * Builds the method implementation for `snap_manageState`.
84
88
  *