UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 4.95 kB
{"version":3,"file":"permittedDerivationPaths.mjs","sourceRoot":"","sources":["../../../src/restricted/caveats/permittedDerivationPaths.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAEjE,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,OAAO,EACR,8BAA8B;AAC/B,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,8BAA8B;AAE1D,OAAO,EAAE,YAAY,EAAE,wBAAwB;AAE/C;;;;;;;GAOG;AACH,MAAM,UAAU,oCAAoC,CAClD,KAAW;IAEX,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,cAAc,CAAC,wBAAwB;gBAC7C,KAAK;aACN;SACF;KACF,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAC/B,KAAc;IAEd,YAAY,CACV,KAAK,EACL,kBAAkB,EAClB,wCAAwC,EACxC,SAAS,CAAC,aAAa,CACxB,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,wBAAwB,CACtC,MAA2B;IAE3B,YAAY,CACV,MAAM,EACN,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,EAC7D,+BAA+B,EAC/B,SAAS,CAAC,QAAQ,CACnB,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,2CAA2C,GAGpD;IACF,CAAC,cAAc,CAAC,wBAAwB,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QACvD,IAAI,EAAE,cAAc,CAAC,wBAAwB;QAC7C,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE;YAC5B,OAAO,KAAK,EAAE,IAAI,EAAE,EAAE;gBACpB,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;gBACxB,iBAAiB,CAAC,MAAM,CAAC,CAAC;gBAE1B,MAAM,IAAI,GAAI,MAAM,CAAC,KAAwB,CAAC,IAAI,CAChD,CAAC,UAAU,EAAE,EAAE,CACb,OAAO,CACL,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,EAC5C,UAAU,CAAC,IAAI,CAChB,IAAI,UAAU,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,CACzC,CAAC;gBAEF,IAAI,CAAC,IAAI,EAAE,CAAC;oBACV,MAAM,cAAc,CAAC,YAAY,CAAC;wBAChC,OAAO,EACL,4FAA4F;qBAC/F,CAAC,CAAC;gBACL,CAAC;gBAED,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC;YAC5B,CAAC,CAAC;QACJ,CAAC;QACD,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,wBAAwB,CAAC,MAAM,CAAC;KACxD,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n PermissionConstraint,\n RestrictedMethodCaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { Bip32Entropy } from '@metamask/snaps-utils';\nimport {\n SnapCaveatType,\n Bip32EntropyStruct,\n isEqual,\n} from '@metamask/snaps-utils';\nimport { array, size, type } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function permittedDerivationPathsCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.PermittedDerivationPaths,\n value,\n },\n ],\n };\n}\n\n/**\n * Validate a caveat path object. The object must consist of a `path` array and\n * a `curve` string. Paths must start with `m`, and must contain at\n * least two indices. If `ed25519` is used, this checks if all the path indices\n * are hardened.\n *\n * @param value - The value to validate.\n * @throws If the value is invalid.\n */\nexport function validateBIP32Path(\n value: unknown,\n): asserts value is Bip32Entropy {\n assertStruct(\n value,\n Bip32EntropyStruct,\n 'Invalid BIP-32 entropy path definition',\n rpcErrors.invalidParams,\n );\n}\n\n/**\n * Validate the path values associated with a caveat. This validates that the\n * value is a non-empty array with valid derivation paths and curves.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateBIP32CaveatPaths(\n caveat: Caveat<string, any>,\n): asserts caveat is Caveat<string, Bip32Entropy[]> {\n assertStruct(\n caveat,\n type({ value: size(array(Bip32EntropyStruct), 1, Infinity) }),\n 'Invalid BIP-32 entropy caveat',\n rpcErrors.internal,\n );\n}\n\nexport const PermittedDerivationPathsCaveatSpecification: Record<\n SnapCaveatType.PermittedDerivationPaths,\n RestrictedMethodCaveatSpecificationConstraint\n> = {\n [SnapCaveatType.PermittedDerivationPaths]: Object.freeze({\n type: SnapCaveatType.PermittedDerivationPaths,\n decorator: (method, caveat) => {\n return async (args) => {\n const { params } = args;\n validateBIP32Path(params);\n\n const path = (caveat.value as Bip32Entropy[]).find(\n (caveatPath) =>\n isEqual(\n params.path.slice(0, caveatPath.path.length),\n caveatPath.path,\n ) && caveatPath.curve === params.curve,\n );\n\n if (!path) {\n throw providerErrors.unauthorized({\n message:\n 'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',\n });\n }\n\n return await method(args);\n };\n },\n validator: (caveat) => validateBIP32CaveatPaths(caveat),\n }),\n};\n"]}