@metamask/keyring-snap-sdk
Version:
MetaMask Keyring Snap SDK
185 lines • 8.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MethodNotSupportedError = void 0;
exports.handleKeyringRequest = handleKeyringRequest;
const keyring_api_1 = require("@metamask/keyring-api");
const keyring_utils_1 = require("@metamask/keyring-utils");
const snaps_sdk_1 = require("@metamask/snaps-sdk");
const superstruct_1 = require("@metamask/superstruct");
const errors_1 = require("./errors.cjs");
// ESLint does not like our custom error classes in this repo for some reason, they do extend Error, so unsure why.
/* eslint-disable @typescript-eslint/only-throw-error */
/**
* Error thrown when a keyring JSON-RPC method is not supported.
*/
class MethodNotSupportedError extends snaps_sdk_1.MethodNotSupportedError {
constructor(method) {
super(`Method not supported: ${method}`);
}
}
exports.MethodNotSupportedError = MethodNotSupportedError;
/**
* Inner function that dispatches JSON-RPC request to the associated Keyring
* methods.
*
* @param keyring - Keyring instance.
* @param request - Keyring JSON-RPC request.
* @returns A promise that resolves to the keyring response.
*/
async function dispatchRequest(keyring, request) {
// We first have to make sure that the request is a valid JSON-RPC request so
// we can check its method name.
(0, superstruct_1.assert)(request, keyring_utils_1.JsonRpcRequestStruct);
switch (request.method) {
case `${keyring_api_1.KeyringRpcMethod.ListAccounts}`: {
(0, superstruct_1.assert)(request, keyring_api_1.ListAccountsRequestStruct);
return keyring.listAccounts();
}
case `${keyring_api_1.KeyringRpcMethod.GetAccount}`: {
(0, superstruct_1.assert)(request, keyring_api_1.GetAccountRequestStruct);
return keyring.getAccount(request.params.id);
}
case `${keyring_api_1.KeyringRpcMethod.CreateAccount}`: {
(0, superstruct_1.assert)(request, keyring_api_1.CreateAccountRequestStruct);
return keyring.createAccount(request.params.options);
}
case `${keyring_api_1.KeyringRpcMethod.CreateAccounts}`: {
if (keyring.createAccounts === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.CreateAccountsRequestStruct);
return keyring.createAccounts(request.params.options);
}
case `${keyring_api_1.KeyringRpcMethod.DiscoverAccounts}`: {
if (keyring.discoverAccounts === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.DiscoverAccountsRequestStruct);
return keyring.discoverAccounts(request.params.scopes, request.params.entropySource, request.params.groupIndex);
}
case `${keyring_api_1.KeyringRpcMethod.ListAccountTransactions}`: {
if (keyring.listAccountTransactions === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.ListAccountTransactionsRequestStruct);
return keyring.listAccountTransactions(request.params.id, request.params.pagination);
}
case `${keyring_api_1.KeyringRpcMethod.ListAccountAssets}`: {
if (keyring.listAccountAssets === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.ListAccountAssetsRequestStruct);
return keyring.listAccountAssets(request.params.id);
}
case `${keyring_api_1.KeyringRpcMethod.GetAccountBalances}`: {
if (keyring.getAccountBalances === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.GetAccountBalancesRequestStruct);
return keyring.getAccountBalances(request.params.id, request.params.assets);
}
case `${keyring_api_1.KeyringRpcMethod.ResolveAccountAddress}`: {
if (keyring.resolveAccountAddress === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.ResolveAccountAddressRequestStruct);
return keyring.resolveAccountAddress(request.params.scope, request.params.request);
}
case `${keyring_api_1.KeyringRpcMethod.SetSelectedAccounts}`: {
if (keyring.setSelectedAccounts === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.SetSelectedAccountsRequestStruct);
return keyring.setSelectedAccounts(request.params.accounts);
}
case `${keyring_api_1.KeyringRpcMethod.FilterAccountChains}`: {
(0, superstruct_1.assert)(request, keyring_api_1.FilterAccountChainsStruct);
return keyring.filterAccountChains(request.params.id, request.params.chains);
}
case `${keyring_api_1.KeyringRpcMethod.UpdateAccount}`: {
(0, superstruct_1.assert)(request, keyring_api_1.UpdateAccountRequestStruct);
return keyring.updateAccount(request.params.account);
}
case `${keyring_api_1.KeyringRpcMethod.DeleteAccount}`: {
(0, superstruct_1.assert)(request, keyring_api_1.DeleteAccountRequestStruct);
return keyring.deleteAccount(request.params.id);
}
case `${keyring_api_1.KeyringRpcMethod.ExportAccount}`: {
if (keyring.exportAccount === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.ExportAccountRequestStruct);
return keyring.exportAccount(request.params.id);
}
case `${keyring_api_1.KeyringRpcMethod.ListRequests}`: {
if (keyring.listRequests === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.ListRequestsRequestStruct);
return keyring.listRequests();
}
case `${keyring_api_1.KeyringRpcMethod.GetRequest}`: {
if (keyring.getRequest === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.GetRequestRequestStruct);
return keyring.getRequest(request.params.id);
}
case `${keyring_api_1.KeyringRpcMethod.SubmitRequest}`: {
(0, superstruct_1.assert)(request, keyring_api_1.SubmitRequestRequestStruct);
return keyring.submitRequest(request.params);
}
case `${keyring_api_1.KeyringRpcMethod.ApproveRequest}`: {
if (keyring.approveRequest === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.ApproveRequestRequestStruct);
return keyring.approveRequest(request.params.id, request.params.data);
}
case `${keyring_api_1.KeyringRpcMethod.RejectRequest}`: {
if (keyring.rejectRequest === undefined) {
throw new MethodNotSupportedError(request.method);
}
(0, superstruct_1.assert)(request, keyring_api_1.RejectRequestRequestStruct);
return keyring.rejectRequest(request.params.id);
}
default: {
throw new MethodNotSupportedError(request.method);
}
}
}
/**
* Handles a keyring JSON-RPC request.
*
* This function is meant to be used as a handler for Keyring JSON-RPC requests
* in an Accounts Snap.
*
* @param keyring - Keyring instance.
* @param request - Keyring JSON-RPC request.
* @returns A promise that resolves to the keyring response.
* @example
* ```ts
* export const onKeyringRequest: OnKeyringRequestHandler = async ({
* origin,
* request,
* }) => {
* return await handleKeyringRequest(keyring, request);
* };
* ```
*/
async function handleKeyringRequest(keyring, request) {
try {
return await dispatchRequest(keyring, request);
}
catch (error) {
if ((0, errors_1.isSnapError)(error)) {
throw error;
}
// Treat other errors as unexpected, this will crash the Snap.
const message = error instanceof Error && typeof error.message === 'string'
? error.message
: 'An unknown error occurred while handling the keyring request';
throw new Error(message);
}
}
//# sourceMappingURL=rpc-handler.cjs.map