-
Notifications
You must be signed in to change notification settings - Fork 542
/
cronjob.ts
147 lines (132 loc) · 4.09 KB
/
cronjob.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
import {
PermissionSpecificationBuilder,
PermissionType,
EndowmentGetterParams,
ValidPermissionSpecification,
PermissionConstraint,
Caveat,
CaveatSpecificationConstraint,
} from '@metamask/permission-controller';
import {
SnapCaveatType,
CronjobSpecification,
isCronjobSpecificationArray,
} from '@metamask/snaps-utils';
import {
assert,
hasProperty,
isPlainObject,
Json,
NonEmptyArray,
} from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';
import { SnapEndowments } from './enum';
const permissionName = SnapEndowments.Cronjob;
type CronjobEndowmentSpecification = ValidPermissionSpecification<{
permissionType: PermissionType.Endowment;
targetKey: typeof permissionName;
endowmentGetter: (_options?: any) => undefined;
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
}>;
/**
* `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.
*
* @param _builderOptions - Optional specification builder options.
* @returns The specification for the cronjob endowment.
*/
const specificationBuilder: PermissionSpecificationBuilder<
PermissionType.Endowment,
any,
CronjobEndowmentSpecification
> = (_builderOptions?: any) => {
return {
permissionType: PermissionType.Endowment,
targetKey: permissionName,
allowedCaveats: [SnapCaveatType.SnapCronjob],
endowmentGetter: (_getterOptions?: EndowmentGetterParams) => undefined,
};
};
export const cronjobEndowmentBuilder = Object.freeze({
targetKey: permissionName,
specificationBuilder,
} as const);
/**
* 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 getCronjobCaveatMapper(
value: Json,
): Pick<PermissionConstraint, 'caveats'> {
return {
caveats: [
{
type: SnapCaveatType.SnapCronjob,
value,
},
],
};
}
/**
* Getter function to get the cronjobs 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 cronjobs, or `null` if the permission does not have a
* cronjob caveat.
*/
export function getCronjobCaveatJobs(
permission?: PermissionConstraint,
): CronjobSpecification[] | null {
if (!permission?.caveats) {
return null;
}
assert(permission.caveats.length === 1);
assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);
const caveat = permission.caveats[0] as Caveat<string, { jobs: Json[] }>;
return (caveat.value?.jobs as CronjobSpecification[]) ?? null;
}
/**
* Validate the cronjob specification values associated with a caveat.
* This validates that the value is a non-empty array with valid
* cronjob expression and request object.
*
* @param caveat - The caveat to validate.
* @throws If the value is invalid.
*/
export function validateCronjobCaveat(caveat: Caveat<string, any>) {
if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {
throw ethErrors.rpc.invalidParams({
message: 'Expected a plain object.',
});
}
const { value } = caveat;
if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {
throw ethErrors.rpc.invalidParams({
message: 'Expected a plain object.',
});
}
if (!isCronjobSpecificationArray(value.jobs)) {
throw ethErrors.rpc.invalidParams({
message: 'Expected a valid cronjob specification array.',
});
}
}
/**
* Caveat specification for the Cronjob.
*/
export const cronjobCaveatSpecifications: Record<
SnapCaveatType.SnapCronjob,
CaveatSpecificationConstraint
> = {
[SnapCaveatType.SnapCronjob]: Object.freeze({
type: SnapCaveatType.SnapCronjob,
validator: (caveat) => validateCronjobCaveat(caveat),
}),
};