UNPKG

@metamask/snaps-rpc-methods

Version:
74 lines 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.manageAccountsBuilder = exports.manageAccountsImplementation = exports.specificationBuilder = exports.methodName = void 0; const permission_controller_1 = require("@metamask/permission-controller"); const superstruct_1 = require("@metamask/superstruct"); const utils_1 = require("@metamask/utils"); const SnapMessageStruct = (0, superstruct_1.union)([ (0, superstruct_1.object)({ method: (0, superstruct_1.string)(), }), (0, superstruct_1.object)({ method: (0, superstruct_1.string)(), params: (0, superstruct_1.union)([(0, superstruct_1.array)(utils_1.JsonStruct), (0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct)]), }), ]); exports.methodName = 'snap_manageAccounts'; /** * The specification builder for the `snap_manageAccounts` permission. * `snap_manageAccounts` lets the Snap manage a set of accounts via a custom keyring. * * @param options - The specification builder options. * @param options.allowedCaveats - The optional allowed caveats for the permission. * @param options.methodHooks - The RPC method hooks needed by the method implementation. * @returns The specification for the `snap_manageAccounts` permission. */ const specificationBuilder = ({ allowedCaveats = null, methodHooks, }) => { return { permissionType: permission_controller_1.PermissionType.RestrictedMethod, targetName: exports.methodName, allowedCaveats, methodImplementation: manageAccountsImplementation(methodHooks), subjectTypes: [permission_controller_1.SubjectType.Snap], }; }; exports.specificationBuilder = specificationBuilder; /** * Builds the method implementation for `snap_manageAccounts`. * * @param hooks - The RPC method hooks. * @param hooks.getSnapKeyring - A function to get the snap keyring. * @param hooks.getUnlockPromise - The function to get the unlock promise. * @returns The method implementation which either returns `null` for a * successful state update/deletion or returns the decrypted state. * @throws If the params are invalid. */ function manageAccountsImplementation({ getSnapKeyring, getUnlockPromise, }) { return async function manageAccounts(options) { const { context: { origin }, params, } = options; (0, superstruct_1.assert)(params, SnapMessageStruct); await getUnlockPromise(true); const keyring = await getSnapKeyring(origin); return await keyring.handleKeyringSnapMessage(origin, params); }; } exports.manageAccountsImplementation = manageAccountsImplementation; /** * Manage account management Snap accounts. This method is organized into * multiple sub-methods which each take their own parameters: * * - `createAccount` * - `updateAccount` * - `deleteAccount` * - `listAccounts` * - `submitResponse` */ exports.manageAccountsBuilder = Object.freeze({ targetName: exports.methodName, specificationBuilder: exports.specificationBuilder, methodHooks: { getSnapKeyring: true, getUnlockPromise: true, }, }); //# sourceMappingURL=manageAccounts.cjs.map