|
| 1 | +import type { |
| 2 | + RestrictedControllerMessenger, |
| 3 | + ControllerGetStateAction, |
| 4 | + ControllerStateChangeEvent, |
| 5 | +} from '@metamask/base-controller'; |
| 6 | +import { BaseController } from '@metamask/base-controller'; |
| 7 | +import type { GetPermissions } from '@metamask/permission-controller'; |
| 8 | + |
| 9 | +import type { DeleteInterface } from '../interface'; |
| 10 | +import type { GetAllSnaps, HandleSnapRequest } from '../snaps'; |
| 11 | +import type { |
| 12 | + TransactionControllerUnapprovedTransactionAddedEvent, |
| 13 | + SignatureStateChange, |
| 14 | + TransactionControllerTransactionStatusUpdatedEvent, |
| 15 | +} from '../types'; |
| 16 | + |
| 17 | +const controllerName = 'DeviceController'; |
| 18 | + |
| 19 | +export type DeviceControllerAllowedActions = |
| 20 | + | HandleSnapRequest |
| 21 | + | GetAllSnaps |
| 22 | + | GetPermissions |
| 23 | + | DeleteInterface; |
| 24 | + |
| 25 | +export type DeviceControllerGetStateAction = ControllerGetStateAction< |
| 26 | + typeof controllerName, |
| 27 | + DeviceControllerState |
| 28 | +>; |
| 29 | + |
| 30 | +export type DeviceControllerActions = DeviceControllerGetStateAction; |
| 31 | + |
| 32 | +export type DeviceControllerStateChangeEvent = ControllerStateChangeEvent< |
| 33 | + typeof controllerName, |
| 34 | + DeviceControllerState |
| 35 | +>; |
| 36 | + |
| 37 | +export type DeviceControllerEvents = DeviceControllerStateChangeEvent; |
| 38 | + |
| 39 | +export type DeviceControllerAllowedEvents = |
| 40 | + | TransactionControllerUnapprovedTransactionAddedEvent |
| 41 | + | TransactionControllerTransactionStatusUpdatedEvent |
| 42 | + | SignatureStateChange; |
| 43 | + |
| 44 | +export type DeviceControllerMessenger = RestrictedControllerMessenger< |
| 45 | + typeof controllerName, |
| 46 | + DeviceControllerActions | DeviceControllerAllowedActions, |
| 47 | + DeviceControllerEvents | DeviceControllerAllowedEvents, |
| 48 | + DeviceControllerAllowedActions['type'], |
| 49 | + DeviceControllerAllowedEvents['type'] |
| 50 | +>; |
| 51 | + |
| 52 | +export type Device = {}; |
| 53 | + |
| 54 | +export type DeviceControllerState = { |
| 55 | + devices: Record<string, Device>; |
| 56 | +}; |
| 57 | + |
| 58 | +export type DeviceControllerArgs = { |
| 59 | + messenger: DeviceControllerMessenger; |
| 60 | + state?: DeviceControllerState; |
| 61 | +}; |
| 62 | +/** |
| 63 | + * Controller for managing access to devices for Snaps. |
| 64 | + */ |
| 65 | +export class DeviceController extends BaseController< |
| 66 | + typeof controllerName, |
| 67 | + DeviceControllerState, |
| 68 | + DeviceControllerMessenger |
| 69 | +> { |
| 70 | + constructor({ messenger, state }: DeviceControllerArgs) { |
| 71 | + super({ |
| 72 | + messenger, |
| 73 | + metadata: { |
| 74 | + devices: { persist: true, anonymous: false }, |
| 75 | + }, |
| 76 | + name: controllerName, |
| 77 | + state: { ...state, devices: {} }, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + async requestDevices() { |
| 82 | + const devices = await (navigator as any).hid.requestDevice({ filters: [] }); |
| 83 | + return devices; |
| 84 | + } |
| 85 | + |
| 86 | + async getDevices() { |
| 87 | + const devices = await (navigator as any).hid.getDevices(); |
| 88 | + return devices; |
| 89 | + } |
| 90 | +} |
0 commit comments