-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidation.ts
More file actions
91 lines (79 loc) · 2.64 KB
/
Copy pathvalidation.ts
File metadata and controls
91 lines (79 loc) · 2.64 KB
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
import type { PermissionRequest } from '@metamask/7715-permissions-shared/types';
import { extractZodError } from '@metamask/7715-permissions-shared/utils';
import { validateHexInteger } from '../validation';
import type {
Erc20TokenStreamPermission,
Erc20TokenStreamPermissionRequest,
} from './types';
import { zErc20TokenStreamPermission } from './types';
/**
* Validates a permission object data specific to the permission type.
* @param permission - The ERC20 token stream permission object to validate.
* @param expiry - The expiry time of permission request.
* @returns True if the permission data is valid, throws an error otherwise.
* @throws {Error} If any validation check fails.
*/
function validatePermissionData(
permission: Erc20TokenStreamPermission,
expiry: number,
): true {
const { initialAmount, maxAmount, amountPerSecond, startTime } =
permission.data;
validateHexInteger({
name: 'maxAmount',
value: maxAmount,
required: false,
allowZero: false,
});
validateHexInteger({
name: 'initialAmount',
value: initialAmount,
required: false,
allowZero: true,
});
validateHexInteger({
name: 'amountPerSecond',
value: amountPerSecond,
required: true,
allowZero: false,
});
if (initialAmount && maxAmount && BigInt(maxAmount) < BigInt(initialAmount)) {
throw new Error('Invalid maxAmount: must be greater than initialAmount');
}
// If startTime is not provided it default to Date.now(), expiry is always in the future so no need to check.
if (startTime && startTime >= expiry) {
throw new Error('Invalid startTime: must be before expiry');
}
return true;
}
/**
* Parses and validates a permission request for ERC20 token streaming.
* @param permissionRequest - The permission request object to validate.
* @returns A validated permission request object.
* @throws {Error} If the permission request is invalid.
*/
export function parseAndValidatePermission(
permissionRequest: PermissionRequest,
): Erc20TokenStreamPermissionRequest {
const {
data: validationResult,
error: validationError,
success,
} = zErc20TokenStreamPermission.safeParse(permissionRequest.permission);
if (!success) {
throw new Error(extractZodError(validationError.errors));
}
validatePermissionData(validationResult, permissionRequest.expiry);
return {
...permissionRequest,
isAdjustmentAllowed: permissionRequest.isAdjustmentAllowed ?? true,
permission: {
...validationResult,
data: {
...validationResult.data,
initialAmount: validationResult.data.initialAmount,
maxAmount: validationResult.data.maxAmount,
},
},
};
}