-
Notifications
You must be signed in to change notification settings - Fork 542
/
keyring.ts
157 lines (141 loc) · 4.25 KB
/
keyring.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
Caveat,
CaveatSpecificationConstraint,
EndowmentGetterParams,
PermissionConstraint,
PermissionSpecificationBuilder,
PermissionType,
PermissionValidatorConstraint,
ValidPermissionSpecification,
} from '@metamask/permission-controller';
import {
assertIsNamespacesObject,
Namespaces,
SnapCaveatType,
} from '@metamask/snaps-utils';
import {
hasProperty,
isPlainObject,
Json,
NonEmptyArray,
assert,
} from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';
import { SnapEndowments } from './enum';
const targetKey = SnapEndowments.Keyring;
type KeyringSpecification = ValidPermissionSpecification<{
permissionType: PermissionType.Endowment;
targetKey: typeof targetKey;
endowmentGetter: (_options?: any) => undefined;
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
validator: PermissionValidatorConstraint;
}>;
type KeyringSpecificationBuilderOptions = {
// Empty for now.
};
/**
* The specification builder for the keyring endowment permission.
*
* @returns The specification for the keyring endowment permission.
*/
const specificationBuilder: PermissionSpecificationBuilder<
PermissionType.Endowment,
KeyringSpecificationBuilderOptions,
KeyringSpecification
> = (): KeyringSpecification => {
return {
permissionType: PermissionType.Endowment,
targetKey,
allowedCaveats: [SnapCaveatType.SnapKeyring],
endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,
validator: ({ caveats }) => {
if (
caveats?.length !== 1 ||
caveats[0].type !== SnapCaveatType.SnapKeyring
) {
throw ethErrors.rpc.invalidParams({
message: `Expected a single "${SnapCaveatType.SnapKeyring}" caveat.`,
});
}
},
};
};
export const keyringEndowmentBuilder = Object.freeze({
targetKey,
specificationBuilder,
} as const);
/**
* Validate the namespaces value of a caveat. This does not validate the type or
* value of the caveat itself, only the value of the namespaces object.
*
* @param caveat - The caveat to validate.
* @throws If the caveat value is invalid.
*/
function validateCaveatNamespace(caveat: Caveat<string, any>): void {
if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {
throw ethErrors.rpc.invalidParams({
message: 'Expected a plain object.',
});
}
const { value } = caveat;
if (!hasProperty(value, 'namespaces') || !isPlainObject(value)) {
throw ethErrors.rpc.invalidParams({
message: 'Expected a plain object.',
});
}
assertIsNamespacesObject(value.namespaces, ethErrors.rpc.invalidParams);
}
/**
* Map a raw value from the `initialPermissions` to a caveat specification.
* Note that this function does not do any validation, that's handled by the
* PermissionsController when the permission is requested.
*
* @param value - The raw value from the `initialPermissions`.
* @returns The caveat specification.
*/
export function getKeyringCaveatMapper(
value: Json,
): Pick<PermissionConstraint, 'caveats'> {
return {
caveats: [
{
type: SnapCaveatType.SnapKeyring,
value,
},
],
};
}
/**
* Getter function to get the keyring namespaces from a permission.
*
* This does basic validation of the caveat, but does not validate the type or
* value of the namespaces object itself, as this is handled by the
* `PermissionsController` when the permission is requested.
*
* @param permission - The permission to get the keyring namespaces from.
* @returns The keyring namespaces, or `null` if the permission does not have a
* keyring caveat.
*/
export function getKeyringCaveatNamespaces(
permission?: PermissionConstraint,
): Namespaces | null {
if (!permission?.caveats) {
return null;
}
assert(permission.caveats.length === 1);
assert(permission.caveats[0].type === SnapCaveatType.SnapKeyring);
const caveat = permission.caveats[0] as Caveat<
string,
{ namespaces: Namespaces }
>;
return caveat.value?.namespaces ?? null;
}
export const keyringCaveatSpecifications: Record<
SnapCaveatType.SnapKeyring,
CaveatSpecificationConstraint
> = {
[SnapCaveatType.SnapKeyring]: Object.freeze({
type: SnapCaveatType.SnapKeyring,
validator: (caveat: Caveat<string, any>) => validateCaveatNamespace(caveat),
}),
};