Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add devices #2824

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Store filters in state
FrederikBolding committed Nov 11, 2024

Verified

This commit was signed with the committer’s verified signature.
FrederikBolding Frederik Bolding
commit 8cd79edee03bb4296e7a1e3904b83db30c80f606
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ describe('DeviceController', () => {
const pairingPromise = messenger.call(
'DeviceController:requestDevice',
MOCK_SNAP_ID,
{ type: 'hid' },
);

messenger.call('DeviceController:resolvePairing', MOCK_DEVICE_ID);
24 changes: 19 additions & 5 deletions packages/snaps-controllers/src/devices/DeviceController.ts
Original file line number Diff line number Diff line change
@@ -15,9 +15,11 @@ import {
getPermittedDeviceIds,
} from '@metamask/snaps-rpc-methods';
import type {
DeviceFilter,
DeviceId,
ListDevicesParams,
ReadDeviceParams,
RequestDeviceParams,
SnapId,
WriteDeviceParams,
} from '@metamask/snaps-sdk';
@@ -117,7 +119,11 @@ export type ConnectedDevice = {

export type DeviceControllerState = {
devices: Record<string, Device>;
pairing: { snapId: string } | null;
pairing: {
snapId: string;
type: DeviceType;
filters?: DeviceFilter[];
} | null;
};

export type DeviceControllerArgs = {
@@ -189,8 +195,8 @@ export class DeviceController extends BaseController<
);
}

async requestDevice(snapId: string) {
const deviceId = await this.#requestPairing({ snapId });
async requestDevice(snapId: string, { type, filters }: RequestDeviceParams) {
const deviceId = await this.#requestPairing({ snapId, type, filters });

// await this.#syncDevices();

@@ -417,7 +423,15 @@ export class DeviceController extends BaseController<
return this.#pairing !== undefined;
}

async #requestPairing({ snapId }: { snapId: string }) {
async #requestPairing({
snapId,
type,
filters,
}: {
snapId: string;
type: DeviceType;
filters?: DeviceFilter[];
}) {
if (this.#isPairing()) {
// TODO: Potentially await existing pairing flow?
throw new Error('A pairing is already underway.');
@@ -431,7 +445,7 @@ export class DeviceController extends BaseController<
await this.#syncDevices();

this.update((draftState) => {
draftState.pairing = { snapId };
draftState.pairing = { snapId, type, filters };
});

return promise;