@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
70 lines • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFileHandler = exports.GetFileArgsStruct = void 0;
const rpc_errors_1 = require("@metamask/rpc-errors");
const snaps_sdk_1 = require("@metamask/snaps-sdk");
const superstruct_1 = require("@metamask/superstruct");
const utils_1 = require("@metamask/utils");
exports.GetFileArgsStruct = (0, superstruct_1.object)({
path: (0, superstruct_1.string)(),
encoding: (0, superstruct_1.optional)((0, superstruct_1.union)([
(0, snaps_sdk_1.enumValue)(snaps_sdk_1.AuxiliaryFileEncoding.Base64),
(0, snaps_sdk_1.enumValue)(snaps_sdk_1.AuxiliaryFileEncoding.Hex),
(0, snaps_sdk_1.enumValue)(snaps_sdk_1.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)
* ```
*/
exports.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;
(0, utils_1.assertStruct)(params, exports.GetFileArgsStruct, 'Invalid "snap_getFile" parameters', rpc_errors_1.rpcErrors.invalidParams);
try {
res.result = await messenger.call('SnapController:getSnapFile', origin, params.path, params.encoding ??
snaps_sdk_1.AuxiliaryFileEncoding.Base64);
}
catch (error) {
return end(error);
}
return end();
}
//# sourceMappingURL=getFile.cjs.map