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