|
| 1 | +import type { |
| 2 | + Caveat, |
| 3 | + CaveatSpecificationConstraint, |
| 4 | + EndowmentGetterParams, |
| 5 | + PermissionConstraint, |
| 6 | + PermissionSpecificationBuilder, |
| 7 | + ValidPermissionSpecification, |
| 8 | +} from '@metamask/permission-controller'; |
| 9 | +import { PermissionType, SubjectType } from '@metamask/permission-controller'; |
| 10 | +import { rpcErrors } from '@metamask/rpc-errors'; |
| 11 | +import type { DeviceSpecification } from '@metamask/snaps-utils'; |
| 12 | +import { |
| 13 | + SnapCaveatType, |
| 14 | + isDeviceSpecificationArray, |
| 15 | +} from '@metamask/snaps-utils'; |
| 16 | +import { hasProperty, isPlainObject, assert } from '@metamask/utils'; |
| 17 | + |
| 18 | +import { SnapEndowments } from './enum'; |
| 19 | + |
| 20 | +const permissionName = SnapEndowments.Devices; |
| 21 | + |
| 22 | +type DevicesEndowmentSpecification = ValidPermissionSpecification<{ |
| 23 | + permissionType: PermissionType.Endowment; |
| 24 | + targetName: typeof permissionName; |
| 25 | + endowmentGetter: (_options?: any) => null; |
| 26 | + allowedCaveats: [SnapCaveatType.DeviceIds]; |
| 27 | +}>; |
| 28 | + |
| 29 | +/** |
| 30 | + * The `endowment:devices` permission is intended to be used as a flag to |
| 31 | + * determine whether the Snap wants to access the devices API. |
| 32 | + * |
| 33 | + * @param _builderOptions - Optional specification builder options. |
| 34 | + * @returns The specification for the network endowment. |
| 35 | + */ |
| 36 | +const specificationBuilder: PermissionSpecificationBuilder< |
| 37 | + PermissionType.Endowment, |
| 38 | + any, |
| 39 | + DevicesEndowmentSpecification |
| 40 | +> = (_builderOptions?: any) => { |
| 41 | + return { |
| 42 | + permissionType: PermissionType.Endowment, |
| 43 | + targetName: permissionName, |
| 44 | + allowedCaveats: [SnapCaveatType.DeviceIds], |
| 45 | + endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null, |
| 46 | + subjectTypes: [SubjectType.Snap], |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +export const devicesEndowmentBuilder = Object.freeze({ |
| 51 | + targetName: permissionName, |
| 52 | + specificationBuilder, |
| 53 | +} as const); |
| 54 | + |
| 55 | +/** |
| 56 | + * Getter function to get the permitted device IDs from a permission |
| 57 | + * specification. |
| 58 | + * |
| 59 | + * This does basic validation of the caveat, but does not validate the type or |
| 60 | + * value of the namespaces object itself, as this is handled by the |
| 61 | + * `PermissionsController` when the permission is requested. |
| 62 | + * |
| 63 | + * @param permission - The permission to get the keyring namespaces from. |
| 64 | + * @returns The device IDs, or `null` if the permission does not have a |
| 65 | + * device IDs caveat. |
| 66 | + */ |
| 67 | +export function getPermittedDeviceIds( |
| 68 | + permission?: PermissionConstraint, |
| 69 | +): DeviceSpecification[] | null { |
| 70 | + if (!permission?.caveats) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + assert(permission.caveats.length === 1); |
| 75 | + assert(permission.caveats[0].type === SnapCaveatType.DeviceIds); |
| 76 | + |
| 77 | + const caveat = permission.caveats[0] as Caveat< |
| 78 | + string, |
| 79 | + { devices: DeviceSpecification[] } |
| 80 | + >; |
| 81 | + |
| 82 | + return caveat.value?.devices ?? null; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Validate the cronjob specification values associated with a caveat. |
| 87 | + * This validates that the value is a non-empty array with valid device |
| 88 | + * specification objects. |
| 89 | + * |
| 90 | + * @param caveat - The caveat to validate. |
| 91 | + * @throws If the value is invalid. |
| 92 | + */ |
| 93 | +export function validateDeviceIdsCaveat(caveat: Caveat<string, any>) { |
| 94 | + if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) { |
| 95 | + throw rpcErrors.invalidParams({ |
| 96 | + message: 'Expected a plain object.', |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + const { value } = caveat; |
| 101 | + |
| 102 | + if (!hasProperty(value, 'devices') || !isPlainObject(value)) { |
| 103 | + throw rpcErrors.invalidParams({ |
| 104 | + message: 'Expected a plain object.', |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + if (!isDeviceSpecificationArray(value.jobs)) { |
| 109 | + throw rpcErrors.invalidParams({ |
| 110 | + message: 'Expected a valid device specification array.', |
| 111 | + }); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Caveat specification for the device IDs caveat. |
| 117 | + */ |
| 118 | +export const deviceIdsCaveatSpecifications: Record< |
| 119 | + SnapCaveatType.DeviceIds, |
| 120 | + CaveatSpecificationConstraint |
| 121 | +> = { |
| 122 | + [SnapCaveatType.DeviceIds]: Object.freeze({ |
| 123 | + type: SnapCaveatType.DeviceIds, |
| 124 | + validator: (caveat) => validateDeviceIdsCaveat(caveat), |
| 125 | + }), |
| 126 | +}; |
0 commit comments