@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
93 lines • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.protocolCaveatSpecifications = exports.getProtocolCaveatScopes = exports.getProtocolCaveatMapper = exports.protocolEndowmentBuilder = void 0;
const permission_controller_1 = require("@metamask/permission-controller");
const rpc_errors_1 = require("@metamask/rpc-errors");
const snaps_utils_1 = require("@metamask/snaps-utils");
const utils_1 = require("@metamask/utils");
const caveats_1 = require("./caveats/index.cjs");
const enum_1 = require("./enum.cjs");
const permissionName = enum_1.SnapEndowments.Protocol;
/**
* `endowment:protocol` returns nothing; it is intended to be used as a flag
* by the client to detect whether the Snap supports the Protocol API.
*
* @param _builderOptions - Optional specification builder options.
* @returns The specification for the accounts chain endowment.
*/
const specificationBuilder = (_builderOptions) => {
return {
permissionType: permission_controller_1.PermissionType.Endowment,
targetName: permissionName,
allowedCaveats: [
snaps_utils_1.SnapCaveatType.ProtocolSnapScopes,
snaps_utils_1.SnapCaveatType.MaxRequestTime,
],
endowmentGetter: (_getterOptions) => null,
validator: (0, caveats_1.createGenericPermissionValidator)([
{ type: snaps_utils_1.SnapCaveatType.ProtocolSnapScopes },
{ type: snaps_utils_1.SnapCaveatType.MaxRequestTime, optional: true },
]),
subjectTypes: [permission_controller_1.SubjectType.Snap],
};
};
exports.protocolEndowmentBuilder = Object.freeze({
targetName: permissionName,
specificationBuilder,
});
/**
* 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.
*/
function getProtocolCaveatMapper(value) {
if (!value || !(0, utils_1.isObject)(value) || Object.keys(value).length === 0) {
return { caveats: null };
}
const caveats = [];
if (value.scopes) {
caveats.push({
type: snaps_utils_1.SnapCaveatType.ProtocolSnapScopes,
value: value.scopes,
});
}
return { caveats: caveats };
}
exports.getProtocolCaveatMapper = getProtocolCaveatMapper;
/**
* Getter function to get the {@link ProtocolSnapScopes} caveat value from a
* permission.
*
* @param permission - The permission to get the caveat value from.
* @returns The caveat value.
*/
function getProtocolCaveatScopes(permission) {
const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === snaps_utils_1.SnapCaveatType.ProtocolSnapScopes);
return caveat ? caveat.value : null;
}
exports.getProtocolCaveatScopes = getProtocolCaveatScopes;
/**
* Validates the type of the caveat value.
*
* @param caveat - The caveat to validate.
* @throws If the caveat value is invalid.
*/
function validateCaveat(caveat) {
if (!(0, utils_1.hasProperty)(caveat, 'value') || !(0, utils_1.isPlainObject)(caveat)) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: 'Expected a plain object.',
});
}
const { value } = caveat;
(0, utils_1.assertStruct)(value, snaps_utils_1.ProtocolScopesStruct, 'Invalid scopes specified', rpc_errors_1.rpcErrors.invalidParams);
}
exports.protocolCaveatSpecifications = {
[snaps_utils_1.SnapCaveatType.ProtocolSnapScopes]: Object.freeze({
type: snaps_utils_1.SnapCaveatType.ProtocolSnapScopes,
validator: (caveat) => validateCaveat(caveat),
}),
};
//# sourceMappingURL=protocol.cjs.map