@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
61 lines • 2.07 kB
JavaScript
import { rpcErrors } from "@metamask/rpc-errors";
import { ComponentOrElementStruct, InterfaceContextStruct } from "@metamask/snaps-sdk";
import { StructError, create, object, optional } from "@metamask/superstruct";
const hookNames = {
createInterface: true,
};
export const createInterfaceHandler = {
methodNames: ['snap_createInterface'],
implementation: getCreateInterfaceImplementation,
hookNames,
};
const CreateInterfaceParametersStruct = object({
ui: ComponentOrElementStruct,
context: optional(InterfaceContextStruct),
});
/**
* The `snap_createInterface` 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.createInterface - The function to create the interface.
* @returns Nothing.
*/
async function getCreateInterfaceImplementation(req, res, _next, end, { createInterface }) {
const { params } = req;
try {
const validatedParams = getValidatedParams(params);
const { ui, context } = validatedParams;
res.result = await createInterface(ui, context);
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validate the createInterface 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 createInterface method parameter object.
*/
function getValidatedParams(params) {
try {
return create(params, CreateInterfaceParametersStruct);
}
catch (error) {
if (error instanceof StructError) {
throw rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpcErrors.internal();
}
}
//# sourceMappingURL=createInterface.mjs.map