@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
1 lines • 6.34 kB
Source Map (JSON)
{"version":3,"file":"keyring.mjs","sourceRoot":"","sources":["../../src/endowments/keyring.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,8BAA8B;AAE/E,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB;AAErE,OAAO,EAAE,gCAAgC,EAAE,4BAAkB;AAC7D,OAAO,EAAE,cAAc,EAAE,mBAAe;AAExC,MAAM,cAAc,GAAG,cAAc,CAAC,OAAO,CAAC;AAW9C;;;;;;GAMG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAyB,EAAE,EAAE;IAChC,OAAO;QACL,cAAc,EAAE,cAAc,CAAC,SAAS;QACxC,UAAU,EAAE,cAAc;QAC1B,cAAc,EAAE;YACd,cAAc,CAAC,aAAa;YAC5B,cAAc,CAAC,cAAc;SAC9B;QACD,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,IAAI;QACjE,SAAS,EAAE,gCAAgC,CAAC;YAC1C,EAAE,IAAI,EAAE,cAAc,CAAC,aAAa,EAAE;YACtC,EAAE,IAAI,EAAE,cAAc,CAAC,cAAc,EAAE,QAAQ,EAAE,IAAI,EAAE;SACxD,CAAC;QACF,YAAY,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC;KACjC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC,MAAM,CAAC;IACnD,UAAU,EAAE,cAAc;IAC1B,oBAAoB;CACZ,CAAC,CAAC;AAEZ;;;;;;GAMG;AACH,SAAS,qBAAqB,CAAC,MAA2B;IACxD,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QAClE,MAAM,SAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mDAAmD;SAC7D,CAAC,CAAC;IACL,CAAC;IAED,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IACzB,sBAAsB,CAAC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CACpC,KAAW;IAEX,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,cAAc,CAAC,aAAa;gBAClC,KAAK;aACN;SACF;KACF,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,uBAAuB,CACrC,UAAiC;IAEjC,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAC5B,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;IACxC,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,cAAc,CAAC,aAAa,CAAC,CAAC;IAEpE,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,CAAmC,CAAC;IACvE,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB,CAAC;AAED,MAAM,CAAC,MAAM,2BAA2B,GAGpC;IACF,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC5C,IAAI,EAAE,cAAc,CAAC,aAAa;QAClC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KAC1E,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { KeyringOrigins } from '@metamask/snaps-utils';\nimport { assertIsKeyringOrigins, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Keyring;\n\ntype KeyringEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n subjectTypes: readonly SubjectType[];\n}>;\n\n/**\n * `endowment:keyring` returns nothing; it is intended to be used as a flag\n * by the client to detect whether the snap has keyring capabilities.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the keyring endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n KeyringEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [\n SnapCaveatType.KeyringOrigin,\n SnapCaveatType.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.KeyringOrigin },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const keyringEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a caveat. This does not validate the type of the\n * caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Invalid keyring origins: Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertIsKeyringOrigins(value, rpcErrors.invalidParams);\n}\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getKeyringCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n return {\n caveats: [\n {\n type: SnapCaveatType.KeyringOrigin,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the {@link KeyringOrigins} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n * @throws If the permission does not have a valid {@link KeyringOrigins}\n * caveat.\n */\nexport function getKeyringCaveatOrigins(\n permission?: PermissionConstraint,\n): KeyringOrigins | null {\n assert(permission?.caveats);\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.KeyringOrigin);\n\n const caveat = permission.caveats[0] as Caveat<string, KeyringOrigins>;\n return caveat.value;\n}\n\nexport const keyringCaveatSpecifications: Record<\n SnapCaveatType.KeyringOrigin,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.KeyringOrigin]: Object.freeze({\n type: SnapCaveatType.KeyringOrigin,\n validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n }),\n};\n"]}