@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
141 lines • 5.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.nameLookupCaveatSpecifications = exports.getLookupMatchersCaveat = exports.getChainIdsCaveat = exports.getNameLookupCaveatMapper = exports.nameLookupEndowmentBuilder = 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.NameLookup;
/**
* `endowment:name-lookup` returns nothing; it is intended to be used as a flag
* by the extension to detect whether the snap has the capability to resolve a domain/address.
*
* @param _builderOptions - Optional specification builder options.
* @returns The specification for the name-lookup endowment.
*/
const specificationBuilder = (_builderOptions) => {
return {
permissionType: permission_controller_1.PermissionType.Endowment,
targetName: permissionName,
allowedCaveats: [
snaps_utils_1.SnapCaveatType.ChainIds,
snaps_utils_1.SnapCaveatType.LookupMatchers,
snaps_utils_1.SnapCaveatType.MaxRequestTime,
],
endowmentGetter: (_getterOptions) => null,
validator: (0, caveats_1.createGenericPermissionValidator)([
{ type: snaps_utils_1.SnapCaveatType.ChainIds, optional: true },
{ type: snaps_utils_1.SnapCaveatType.LookupMatchers, optional: true },
{ type: snaps_utils_1.SnapCaveatType.MaxRequestTime, optional: true },
]),
subjectTypes: [permission_controller_1.SubjectType.Snap],
};
};
exports.nameLookupEndowmentBuilder = Object.freeze({
targetName: permissionName,
specificationBuilder,
});
/**
* 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;
switch (caveat.type) {
case snaps_utils_1.SnapCaveatType.ChainIds:
(0, utils_1.assertStruct)(value, snaps_utils_1.ChainIdsStruct);
break;
case snaps_utils_1.SnapCaveatType.LookupMatchers:
(0, utils_1.assertStruct)(value, snaps_utils_1.LookupMatchersStruct);
break;
default:
throw rpc_errors_1.rpcErrors.invalidParams({
message: 'Invalid caveat type, must be one of the following: "chainIds", "matchers".',
});
}
}
/**
* 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 getNameLookupCaveatMapper(value) {
if (!value || !(0, utils_1.isObject)(value) || Object.keys(value).length === 0) {
return { caveats: null };
}
const caveats = [];
if (value.chains) {
caveats.push({
type: snaps_utils_1.SnapCaveatType.ChainIds,
value: value.chains,
});
}
if (value.matchers) {
caveats.push({
type: snaps_utils_1.SnapCaveatType.LookupMatchers,
value: value.matchers,
});
}
(0, utils_1.assert)(caveats.length > 0);
return { caveats: caveats };
}
exports.getNameLookupCaveatMapper = getNameLookupCaveatMapper;
/**
* Getter function to get the chainIds caveat from a permission.
*
* This does basic validation of the caveat, but does not validate the type or
* value of the namespaces object itself, as this is handled by the
* `PermissionsController` when the permission is requested.
*
* @param permission - The permission to get the `chainIds` caveat from.
* @returns An array of `chainIds` that the snap supports.
*/
function getChainIdsCaveat(permission) {
if (!permission?.caveats) {
return null;
}
const caveat = permission.caveats.find((permCaveat) => permCaveat.type === snaps_utils_1.SnapCaveatType.ChainIds);
return caveat ? caveat.value : null;
}
exports.getChainIdsCaveat = getChainIdsCaveat;
/**
* Getter function to get the matchers caveat from a permission.
*
* This does basic validation of the caveat, but does not validate the type or
* value of the namespaces object itself, as this is handled by the
* `PermissionsController` when the permission is requested.
*
* @param permission - The permission to get the `matchers` caveat from.
* @returns A `matchers` object that defines the input that the snap supports.
*/
function getLookupMatchersCaveat(permission) {
if (!permission?.caveats) {
return null;
}
const caveat = permission.caveats.find((permCaveat) => permCaveat.type === snaps_utils_1.SnapCaveatType.LookupMatchers);
return caveat ? caveat.value : null;
}
exports.getLookupMatchersCaveat = getLookupMatchersCaveat;
exports.nameLookupCaveatSpecifications = {
[snaps_utils_1.SnapCaveatType.ChainIds]: Object.freeze({
type: snaps_utils_1.SnapCaveatType.ChainIds,
validator: (caveat) => validateCaveat(caveat),
}),
[snaps_utils_1.SnapCaveatType.LookupMatchers]: Object.freeze({
type: snaps_utils_1.SnapCaveatType.LookupMatchers,
validator: (caveat) => validateCaveat(caveat),
}),
};
//# sourceMappingURL=name-lookup.cjs.map