@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
97 lines • 3.42 kB
JavaScript
import { rpcErrors } from "@metamask/rpc-errors";
import { MaxRequestTimeStruct, SnapCaveatType } from "@metamask/snaps-utils";
import { assertStruct, hasProperty, isObject } from "@metamask/utils";
/**
* Asserts that the given value is a valid `maxRequestTime` value.
*
* @param value - The value to assert.
* @param ErrorWrapper - An optional error wrapper to use. Defaults to
* {@link AssertionError}.
* @throws If the value is not a valid `maxRequestTime` value.
*/
function assertIsMaxRequestTime(value, ErrorWrapper) {
assertStruct(value, MaxRequestTimeStruct, 'Invalid maxRequestTime', ErrorWrapper);
}
/**
* Validate the value of a caveat. This does not validate the type of the
* caveat itself, only the value of the caveat.
*
* @param caveat - The caveat to validate.
* @throws If the caveat value is invalid.
*/
function validateMaxRequestTimeCaveat(caveat) {
if (!hasProperty(caveat, 'value')) {
throw rpcErrors.invalidParams({
message: 'Invalid maxRequestTime caveat.',
});
}
const { value } = caveat;
assertIsMaxRequestTime(value, rpcErrors.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 getMaxRequestTimeCaveatMapper(value) {
if (!value ||
!isObject(value) ||
(isObject(value) && !hasProperty(value, 'maxRequestTime'))) {
return { caveats: null };
}
return {
caveats: [
{
type: SnapCaveatType.MaxRequestTime,
value: value.maxRequestTime,
},
],
};
}
/**
* Creates a wrapping caveat mapper that creates the `maxRequestTime` caveat
* and merges it with any other caveats created by the mapper function.
*
* @param mapper - Another caveat mapper function.
* @returns The caveat specification.
*/
export function createMaxRequestTimeMapper(mapper) {
return function (value) {
// We assume this to be used only with caveats of this type
const { maxRequestTime, ...rest } = value;
const mapperResult = mapper(rest);
if (!maxRequestTime) {
return mapperResult;
}
return {
...mapperResult,
caveats: [
...(mapperResult.caveats ?? []),
{
type: SnapCaveatType.MaxRequestTime,
value: maxRequestTime,
},
],
};
};
}
/**
* Getter function to get the {@link MaxRequestTime} caveat value from a permission if specified.
*
* @param permission - The permission to get the caveat value from.
* @returns The caveat value if present, otherwise null.
*/
export function getMaxRequestTimeCaveat(permission) {
const foundCaveat = permission?.caveats?.find((caveat) => caveat.type === SnapCaveatType.MaxRequestTime);
return foundCaveat?.value ?? null;
}
export const maxRequestTimeCaveatSpecifications = {
[SnapCaveatType.MaxRequestTime]: Object.freeze({
type: SnapCaveatType.MaxRequestTime,
validator: (caveat) => validateMaxRequestTimeCaveat(caveat),
}),
};
//# sourceMappingURL=requestTime.mjs.map