-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24319a4
commit 54a9165
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
packages/snaps-controllers/src/devices/DeviceController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import type { | ||
RestrictedControllerMessenger, | ||
ControllerGetStateAction, | ||
ControllerStateChangeEvent, | ||
} from '@metamask/base-controller'; | ||
import { BaseController } from '@metamask/base-controller'; | ||
import type { GetPermissions } from '@metamask/permission-controller'; | ||
|
||
import type { DeleteInterface } from '../interface'; | ||
import type { GetAllSnaps, HandleSnapRequest } from '../snaps'; | ||
import type { | ||
TransactionControllerUnapprovedTransactionAddedEvent, | ||
SignatureStateChange, | ||
TransactionControllerTransactionStatusUpdatedEvent, | ||
} from '../types'; | ||
|
||
const controllerName = 'DeviceController'; | ||
|
||
export type DeviceControllerAllowedActions = | ||
| HandleSnapRequest | ||
| GetAllSnaps | ||
| GetPermissions | ||
| DeleteInterface; | ||
|
||
export type DeviceControllerGetStateAction = ControllerGetStateAction< | ||
typeof controllerName, | ||
DeviceControllerState | ||
>; | ||
|
||
export type DeviceControllerActions = DeviceControllerGetStateAction; | ||
|
||
export type DeviceControllerStateChangeEvent = ControllerStateChangeEvent< | ||
typeof controllerName, | ||
DeviceControllerState | ||
>; | ||
|
||
export type DeviceControllerEvents = DeviceControllerStateChangeEvent; | ||
|
||
export type DeviceControllerAllowedEvents = | ||
| TransactionControllerUnapprovedTransactionAddedEvent | ||
| TransactionControllerTransactionStatusUpdatedEvent | ||
| SignatureStateChange; | ||
|
||
export type DeviceControllerMessenger = RestrictedControllerMessenger< | ||
typeof controllerName, | ||
DeviceControllerActions | DeviceControllerAllowedActions, | ||
DeviceControllerEvents | DeviceControllerAllowedEvents, | ||
DeviceControllerAllowedActions['type'], | ||
DeviceControllerAllowedEvents['type'] | ||
>; | ||
|
||
export type Device = {}; | ||
|
||
export type DeviceControllerState = { | ||
devices: Record<string, Device>; | ||
}; | ||
|
||
export type DeviceControllerArgs = { | ||
messenger: DeviceControllerMessenger; | ||
state?: DeviceControllerState; | ||
}; | ||
/** | ||
* Controller for managing access to devices for Snaps. | ||
*/ | ||
export class DeviceController extends BaseController< | ||
typeof controllerName, | ||
DeviceControllerState, | ||
DeviceControllerMessenger | ||
> { | ||
constructor({ messenger, state }: DeviceControllerArgs) { | ||
super({ | ||
messenger, | ||
metadata: { | ||
devices: { persist: true, anonymous: false }, | ||
}, | ||
name: controllerName, | ||
state: { ...state, devices: {} }, | ||
}); | ||
} | ||
|
||
async requestDevices() { | ||
const devices = await (navigator as any).hid.requestDevice({ filters: [] }); | ||
return devices; | ||
} | ||
|
||
async getDevices() { | ||
const devices = await (navigator as any).hid.getDevices(); | ||
return devices; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './DeviceController'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine'; | ||
import type { PermittedHandlerExport } from '@metamask/permission-controller'; | ||
import type { | ||
JsonRpcRequest, | ||
ProviderRequestParams, | ||
ProviderRequestResult, | ||
} from '@metamask/snaps-sdk'; | ||
import { type InferMatching } from '@metamask/snaps-utils'; | ||
import { object, optional, string, type } from '@metamask/superstruct'; | ||
import { | ||
type PendingJsonRpcResponse, | ||
CaipChainIdStruct, | ||
JsonRpcParamsStruct, | ||
} from '@metamask/utils'; | ||
|
||
import type { MethodHooksObject } from '../utils'; | ||
|
||
const hookNames: MethodHooksObject<ProviderRequestMethodHooks> = { | ||
requestDevices: true, | ||
}; | ||
|
||
export type ProviderRequestMethodHooks = { | ||
requestDevices: () => any; | ||
}; | ||
|
||
export const providerRequestHandler: PermittedHandlerExport< | ||
ProviderRequestMethodHooks, | ||
ProviderRequestParameters, | ||
ProviderRequestResult | ||
> = { | ||
methodNames: ['snap_requestDevice'], | ||
implementation: providerRequestImplementation, | ||
hookNames, | ||
}; | ||
|
||
const ProviderRequestParametersStruct = object({ | ||
chainId: CaipChainIdStruct, | ||
request: type({ | ||
method: string(), | ||
params: optional(JsonRpcParamsStruct), | ||
}), | ||
}); | ||
|
||
export type ProviderRequestParameters = InferMatching< | ||
typeof ProviderRequestParametersStruct, | ||
ProviderRequestParams | ||
>; | ||
|
||
async function providerRequestImplementation( | ||
req: JsonRpcRequest<ProviderRequestParams>, | ||
res: PendingJsonRpcResponse<ProviderRequestResult>, | ||
_next: unknown, | ||
end: JsonRpcEngineEndCallback, | ||
{ requestDevices }: ProviderRequestMethodHooks, | ||
): Promise<void> { | ||
const { params } = req; | ||
|
||
try { | ||
res.result = await requestDevices(); | ||
} catch (error) { | ||
return end(error); | ||
} | ||
|
||
return end(); | ||
} |