@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
62 lines • 2.01 kB
JavaScript
import { rpcErrors } from "@metamask/rpc-errors";
import { StructError, create, object, string } from "@metamask/superstruct";
import { JsonStruct } from "@metamask/utils";
const hookNames = {
resolveInterface: true,
};
export const resolveInterfaceHandler = {
methodNames: ['snap_resolveInterface'],
implementation: getResolveInterfaceImplementation,
hookNames,
};
const ResolveInterfaceParametersStruct = object({
id: string(),
value: JsonStruct,
});
/**
* The `snap_resolveInterface` method implementation.
*
* @param req - The JSON-RPC request object.
* @param res - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
* function.
* @param end - The `json-rpc-engine` "end" callback.
* @param hooks - The RPC method hooks.
* @param hooks.resolveInterface - The function to resolve the interface.
* @returns Nothing.
*/
async function getResolveInterfaceImplementation(req, res, _next, end, { resolveInterface }) {
const { params } = req;
try {
const validatedParams = getValidatedParams(params);
const { id, value } = validatedParams;
await resolveInterface(id, value);
res.result = null;
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validate the resolveInterface method `params` and returns them cast to the correct
* type. Throws if validation fails.
*
* @param params - The unvalidated params object from the method request.
* @returns The validated resolveInterface method parameter object.
*/
function getValidatedParams(params) {
try {
return create(params, ResolveInterfaceParametersStruct);
}
catch (error) {
if (error instanceof StructError) {
throw rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpcErrors.internal();
}
}
//# sourceMappingURL=resolveInterface.mjs.map