UNPKG

@metamask/snaps-rpc-methods

Version:
67 lines 2.08 kB
import { rpcErrors } from "@metamask/rpc-errors"; import { AuxiliaryFileEncoding, enumValue } from "@metamask/snaps-sdk"; import { object, optional, string, union } from "@metamask/superstruct"; import { assertStruct } from "@metamask/utils"; export const GetFileArgsStruct = object({ path: string(), encoding: optional(union([ enumValue(AuxiliaryFileEncoding.Base64), enumValue(AuxiliaryFileEncoding.Hex), enumValue(AuxiliaryFileEncoding.Utf8), ])), }); /** * Gets a static file's content in UTF-8, Base64, or hexadecimal. * * The file must be specified in [the Snap's manifest file](https://docs.metamask.io/snaps/features/static-files/). * * @example * ```json name="Manifest" * { * "source": { * "files": ["./files/my-file.bin"] * } * } * ``` * ```ts name="Usage" * const contents = await snap.request({ * method: 'snap_getFile', * params: { * path: './files/myfile.bin', * encoding: 'hex', * }, * }) * * // '0x...' * console.log(contents) * ``` */ export const getFileHandler = { implementation, actionNames: ['SnapController:getSnapFile'], }; /** * The `snap_getFile` 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. Not used by this function. * @param messenger - The messenger used to call controller actions. * @returns Nothing. */ async function implementation(req, res, _next, end, _hooks, messenger) { const { params, origin } = req; assertStruct(params, GetFileArgsStruct, 'Invalid "snap_getFile" parameters', rpcErrors.invalidParams); try { res.result = await messenger.call('SnapController:getSnapFile', origin, params.path, params.encoding ?? AuxiliaryFileEncoding.Base64); } catch (error) { return end(error); } return end(); } //# sourceMappingURL=getFile.mjs.map