-
Notifications
You must be signed in to change notification settings - Fork 542
/
snapInstallation.ts
41 lines (37 loc) · 1.48 KB
/
snapInstallation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { RequestedPermissions } from '@metamask/permission-controller';
import { InstallSnapsResult } from '@metamask/snaps-utils';
import { isObject } from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';
export { InstallSnapsResult } from '@metamask/snaps-utils';
export type InstallSnapsHook = (
requestedSnaps: RequestedPermissions,
) => Promise<InstallSnapsResult>;
/**
* Typechecks the requested snaps and passes them to the permissions
* controller for installation.
*
* @param requestedSnaps - An object containing the requested snaps to be installed. The key of the
* object is the snap id and the value is potential extra data, i.e. version.
* @param installSnaps - A function that tries to install a given snap, prompting the user if
* necessary.
* @returns An object containing the installed snaps.
* @throws If the params are invalid or the snap installation fails.
*/
export async function handleInstallSnaps(
requestedSnaps: RequestedPermissions,
installSnaps: InstallSnapsHook,
): Promise<InstallSnapsResult> {
if (!isObject(requestedSnaps)) {
throw ethErrors.rpc.invalidParams({
message: `Invalid snap installation params.`,
data: { requestedSnaps },
});
} else if (Object.keys(requestedSnaps).length === 0) {
throw ethErrors.rpc.invalidParams({
message: `Must specify at least one snap to install.`,
data: { requestedSnaps },
});
}
// installSnaps is bound to the origin
return await installSnaps(requestedSnaps);
}