@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
134 lines • 5.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.keyringCaveatSpecifications = exports.getKeyringCaveatCapabilities = exports.getKeyringCaveatOrigins = exports.getKeyringCaveatMapper = exports.keyringEndowmentBuilder = 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.Keyring;
/**
* `endowment:keyring` returns nothing; it is intended to be used as a flag
* by the client to detect whether the snap has keyring capabilities.
*
* @param _builderOptions - Optional specification builder options.
* @returns The specification for the keyring endowment.
*/
const specificationBuilder = (_builderOptions) => {
return {
permissionType: permission_controller_1.PermissionType.Endowment,
targetName: permissionName,
allowedCaveats: [
snaps_utils_1.SnapCaveatType.KeyringOrigin,
snaps_utils_1.SnapCaveatType.KeyringCapabilities,
snaps_utils_1.SnapCaveatType.MaxRequestTime,
],
endowmentGetter: (_getterOptions) => null,
validator: (0, caveats_1.createGenericPermissionValidator)([
{ type: snaps_utils_1.SnapCaveatType.KeyringOrigin, optional: true },
{ type: snaps_utils_1.SnapCaveatType.KeyringCapabilities, optional: true },
{ type: snaps_utils_1.SnapCaveatType.MaxRequestTime, optional: true },
]),
subjectTypes: [permission_controller_1.SubjectType.Snap],
};
};
exports.keyringEndowmentBuilder = Object.freeze({
targetName: permissionName,
specificationBuilder,
});
/**
* Validate the value of a keyring origins 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 validateCaveatOrigins(caveat) {
if (!(0, utils_1.hasProperty)(caveat, 'value') || !(0, utils_1.isPlainObject)(caveat.value)) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: 'Invalid keyring origins: Expected a plain object.',
});
}
const { value } = caveat;
(0, snaps_utils_1.assertIsKeyringOrigins)(value, rpc_errors_1.rpcErrors.invalidParams);
}
/**
* Validate the value of a keyring capabilities 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 validateCaveatCapabilities(caveat) {
if (!(0, utils_1.hasProperty)(caveat, 'value') || !(0, utils_1.isPlainObject)(caveat.value)) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: 'Invalid keyring capabilities: Expected a plain object.',
});
}
const { value } = caveat;
(0, snaps_utils_1.assertIsKeyringCapabilities)(value, rpc_errors_1.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.
*/
function getKeyringCaveatMapper(value) {
if (!value || !(0, utils_1.isObject)(value) || Object.keys(value).length === 0) {
return { caveats: null };
}
const caveats = [
{
type: snaps_utils_1.SnapCaveatType.KeyringOrigin,
value: { allowedOrigins: value.allowedOrigins },
},
];
if (value.capabilities) {
caveats.push({
type: snaps_utils_1.SnapCaveatType.KeyringCapabilities,
value: { capabilities: value.capabilities },
});
}
return { caveats: caveats };
}
exports.getKeyringCaveatMapper = getKeyringCaveatMapper;
/**
* Getter function to get the {@link KeyringOrigins} caveat value from a
* permission.
*
* @param permission - The permission to get the caveat value from.
* @returns The caveat value.
*/
function getKeyringCaveatOrigins(permission) {
const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === snaps_utils_1.SnapCaveatType.KeyringOrigin);
return caveat?.value ?? { allowedOrigins: [] };
}
exports.getKeyringCaveatOrigins = getKeyringCaveatOrigins;
/**
* Getter function to get the {@link KeyringCapabilities} caveat value from a
* permission.
*
* @param permission - The permission to get the caveat value from.
* @returns The caveat value, or `null` if the permission does not have a
* {@link KeyringCapabilities} caveat.
*/
function getKeyringCaveatCapabilities(permission) {
const caveat = permission?.caveats?.find((permCaveat) => permCaveat.type === snaps_utils_1.SnapCaveatType.KeyringCapabilities);
return caveat?.value ?? null;
}
exports.getKeyringCaveatCapabilities = getKeyringCaveatCapabilities;
exports.keyringCaveatSpecifications = {
[snaps_utils_1.SnapCaveatType.KeyringOrigin]: Object.freeze({
type: snaps_utils_1.SnapCaveatType.KeyringOrigin,
validator: (caveat) => validateCaveatOrigins(caveat),
}),
[snaps_utils_1.SnapCaveatType.KeyringCapabilities]: Object.freeze({
type: snaps_utils_1.SnapCaveatType.KeyringCapabilities,
validator: (caveat) => validateCaveatCapabilities(caveat),
}),
};
//# sourceMappingURL=keyring.cjs.map