@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
96 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.keyringCaveatSpecifications = 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.MaxRequestTime,
],
endowmentGetter: (_getterOptions) => null,
validator: (0, caveats_1.createGenericPermissionValidator)([
{ type: snaps_utils_1.SnapCaveatType.KeyringOrigin },
{ 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 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);
}
/**
* 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) {
return {
caveats: [
{
type: snaps_utils_1.SnapCaveatType.KeyringOrigin,
value,
},
],
};
}
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.
* @throws If the permission does not have a valid {@link KeyringOrigins}
* caveat.
*/
function getKeyringCaveatOrigins(permission) {
(0, utils_1.assert)(permission?.caveats);
(0, utils_1.assert)(permission.caveats.length === 1);
(0, utils_1.assert)(permission.caveats[0].type === snaps_utils_1.SnapCaveatType.KeyringOrigin);
const caveat = permission.caveats[0];
return caveat.value;
}
exports.getKeyringCaveatOrigins = getKeyringCaveatOrigins;
exports.keyringCaveatSpecifications = {
[snaps_utils_1.SnapCaveatType.KeyringOrigin]: Object.freeze({
type: snaps_utils_1.SnapCaveatType.KeyringOrigin,
validator: (caveat) => validateCaveatOrigins(caveat),
}),
};
//# sourceMappingURL=keyring.cjs.map