UNPKG

@metamask/snaps-rpc-methods

Version:
91 lines 3.3 kB
import { PermissionType, SubjectType } from "@metamask/permission-controller"; import { rpcErrors } from "@metamask/rpc-errors"; import { assertIsKeyringOrigins, SnapCaveatType } from "@metamask/snaps-utils"; import { assert, hasProperty, 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.MaxRequestTime, ], endowmentGetter: (_getterOptions) => null, validator: createGenericPermissionValidator([ { type: SnapCaveatType.KeyringOrigin }, { type: SnapCaveatType.MaxRequestTime, optional: true }, ]), subjectTypes: [SubjectType.Snap], }; }; export const keyringEndowmentBuilder = Object.freeze({ targetName: permissionName, specificationBuilder, }); /** * Validate the value of a 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); } /** * 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) { return { caveats: [ { type: SnapCaveatType.KeyringOrigin, value, }, ], }; } /** * 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. * @throws If the permission does not have a valid {@link KeyringOrigins} * caveat. */ export function getKeyringCaveatOrigins(permission) { assert(permission?.caveats); assert(permission.caveats.length === 1); assert(permission.caveats[0].type === SnapCaveatType.KeyringOrigin); const caveat = permission.caveats[0]; return caveat.value; } export const keyringCaveatSpecifications = { [SnapCaveatType.KeyringOrigin]: Object.freeze({ type: SnapCaveatType.KeyringOrigin, validator: (caveat) => validateCaveatOrigins(caveat), }), }; //# sourceMappingURL=keyring.mjs.map