UNPKG

@metamask/snaps-rpc-methods

Version:
128 lines 4.95 kB
import { PermissionType, SubjectType } from "@metamask/permission-controller"; import { rpcErrors } from "@metamask/rpc-errors"; import { assertIsKeyringCapabilities, assertIsKeyringOrigins, SnapCaveatType } from "@metamask/snaps-utils"; import { hasProperty, isObject, isPlainObject } from "@metamask/utils"; import { createGenericPermissionValidator } from "./caveats/index.mjs"; import { SnapEndowments } from "./enum.mjs"; const permissionName = SnapEndowments.Keyring; /** * `endowment:keyring` returns nothing; it is intended to be used as a flag * by the client to detect whether the snap has keyring capabilities. * * @param _builderOptions - Optional specification builder options. * @returns The specification for the keyring endowment. */ const specificationBuilder = (_builderOptions) => { return { permissionType: PermissionType.Endowment, targetName: permissionName, allowedCaveats: [ SnapCaveatType.KeyringOrigin, SnapCaveatType.KeyringCapabilities, SnapCaveatType.MaxRequestTime, ], endowmentGetter: (_getterOptions) => null, validator: createGenericPermissionValidator([ { type: SnapCaveatType.KeyringOrigin, optional: true }, { type: SnapCaveatType.KeyringCapabilities, optional: true }, { type: SnapCaveatType.MaxRequestTime, optional: true }, ]), subjectTypes: [SubjectType.Snap], }; }; export const keyringEndowmentBuilder = Object.freeze({ targetName: permissionName, specificationBuilder, }); /** * Validate the value of a keyring origins caveat. This does not validate the * type of the caveat itself, only the value of the caveat. * * @param caveat - The caveat to validate. * @throws If the caveat value is invalid. */ function validateCaveatOrigins(caveat) { if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) { throw rpcErrors.invalidParams({ message: 'Invalid keyring origins: Expected a plain object.', }); } const { value } = caveat; assertIsKeyringOrigins(value, rpcErrors.invalidParams); } /** * Validate the value of a keyring capabilities caveat. This does not validate * the type of the caveat itself, only the value of the caveat. * * @param caveat - The caveat to validate. * @throws If the caveat value is invalid. */ function validateCaveatCapabilities(caveat) { if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) { throw rpcErrors.invalidParams({ message: 'Invalid keyring capabilities: Expected a plain object.', }); } const { value } = caveat; assertIsKeyringCapabilities(value, rpcErrors.invalidParams); } /** * Map a raw value from the `initialPermissions` to a caveat specification. * Note that this function does not do any validation, that's handled by the * PermissionsController when the permission is requested. * * @param value - The raw value from the `initialPermissions`. * @returns The caveat specification. */ export function getKeyringCaveatMapper(value) { if (!value || !isObject(value) || Object.keys(value).length === 0) { return { caveats: null }; } const caveats = [ { type: SnapCaveatType.KeyringOrigin, value: { allowedOrigins: value.allowedOrigins }, }, ]; if (value.capabilities) { caveats.push({ type: SnapCaveatType.KeyringCapabilities, value: { capabilities: value.capabilities }, }); } return { caveats: caveats }; } /** * Getter function to get the {@link KeyringOrigins} caveat value from a * permission. * * @param permission - The permission to get the caveat value from. * @returns The caveat value. */ export function getKeyringCaveatOrigins(permission) { const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === SnapCaveatType.KeyringOrigin); return caveat?.value ?? { allowedOrigins: [] }; } /** * Getter function to get the {@link KeyringCapabilities} caveat value from a * permission. * * @param permission - The permission to get the caveat value from. * @returns The caveat value, or `null` if the permission does not have a * {@link KeyringCapabilities} caveat. */ export function getKeyringCaveatCapabilities(permission) { const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === SnapCaveatType.KeyringCapabilities); return caveat?.value ?? null; } export const keyringCaveatSpecifications = { [SnapCaveatType.KeyringOrigin]: Object.freeze({ type: SnapCaveatType.KeyringOrigin, validator: (caveat) => validateCaveatOrigins(caveat), }), [SnapCaveatType.KeyringCapabilities]: Object.freeze({ type: SnapCaveatType.KeyringCapabilities, validator: (caveat) => validateCaveatCapabilities(caveat), }), }; //# sourceMappingURL=keyring.mjs.map