@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
69 lines • 2.61 kB
JavaScript
import { SubjectType, PermissionType } from "@metamask/permission-controller";
import { assert, string, object, union, array, record } from "@metamask/superstruct";
import { JsonStruct } from "@metamask/utils";
const SnapMessageStruct = union([
object({
method: string(),
}),
object({
method: string(),
params: union([array(JsonStruct), record(string(), JsonStruct)]),
}),
]);
export const 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.
*/
export const specificationBuilder = ({ allowedCaveats = null, methodHooks, }) => {
return {
permissionType: PermissionType.RestrictedMethod,
targetName: methodName,
allowedCaveats,
methodImplementation: manageAccountsImplementation(methodHooks),
subjectTypes: [SubjectType.Snap],
};
};
/**
* 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.
*/
export function manageAccountsImplementation({ getSnapKeyring, getUnlockPromise, }) {
return async function manageAccounts(options) {
const { context: { origin }, params, } = options;
assert(params, SnapMessageStruct);
await getUnlockPromise(true);
const keyring = await getSnapKeyring(origin);
return await keyring.handleKeyringSnapMessage(origin, params);
};
}
/**
* Manage account management Snap accounts. This method is organized into
* multiple sub-methods which each take their own parameters:
*
* - `createAccount`
* - `updateAccount`
* - `deleteAccount`
* - `listAccounts`
* - `submitResponse`
*/
export const manageAccountsBuilder = Object.freeze({
targetName: methodName,
specificationBuilder,
methodHooks: {
getSnapKeyring: true,
getUnlockPromise: true,
},
});
//# sourceMappingURL=manageAccounts.mjs.map