@metamask-previews/multichain-api-middleware 2.0.0-preview-d23d9dc44 → 2.0.0-preview-a1caffc9c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -1
- package/dist/handlers/index.cjs +20 -0
- package/dist/handlers/index.cjs.map +1 -0
- package/dist/handlers/index.d.cts +19 -0
- package/dist/handlers/index.d.cts.map +1 -0
- package/dist/handlers/index.d.mts +19 -0
- package/dist/handlers/index.d.mts.map +1 -0
- package/dist/handlers/index.mjs +17 -0
- package/dist/handlers/index.mjs.map +1 -0
- package/dist/handlers/types.cjs.map +1 -1
- package/dist/handlers/types.d.cts +1 -7
- package/dist/handlers/types.d.cts.map +1 -1
- package/dist/handlers/types.d.mts +1 -7
- package/dist/handlers/types.d.mts.map +1 -1
- package/dist/handlers/types.mjs.map +1 -1
- package/dist/handlers/wallet-createSession.cjs +5 -6
- package/dist/handlers/wallet-createSession.cjs.map +1 -1
- package/dist/handlers/wallet-createSession.d.cts +35 -30
- package/dist/handlers/wallet-createSession.d.cts.map +1 -1
- package/dist/handlers/wallet-createSession.d.mts +35 -30
- package/dist/handlers/wallet-createSession.d.mts.map +1 -1
- package/dist/handlers/wallet-createSession.mjs +4 -5
- package/dist/handlers/wallet-createSession.mjs.map +1 -1
- package/dist/handlers/wallet-getSession.cjs +4 -5
- package/dist/handlers/wallet-getSession.cjs.map +1 -1
- package/dist/handlers/wallet-getSession.d.cts +20 -16
- package/dist/handlers/wallet-getSession.d.cts.map +1 -1
- package/dist/handlers/wallet-getSession.d.mts +20 -16
- package/dist/handlers/wallet-getSession.d.mts.map +1 -1
- package/dist/handlers/wallet-getSession.mjs +3 -4
- package/dist/handlers/wallet-getSession.mjs.map +1 -1
- package/dist/handlers/wallet-invokeMethod.cjs +7 -5
- package/dist/handlers/wallet-invokeMethod.cjs.map +1 -1
- package/dist/handlers/wallet-invokeMethod.d.cts +30 -26
- package/dist/handlers/wallet-invokeMethod.d.cts.map +1 -1
- package/dist/handlers/wallet-invokeMethod.d.mts +30 -26
- package/dist/handlers/wallet-invokeMethod.d.mts.map +1 -1
- package/dist/handlers/wallet-invokeMethod.mjs +7 -5
- package/dist/handlers/wallet-invokeMethod.mjs.map +1 -1
- package/dist/handlers/wallet-revokeSession.cjs +4 -5
- package/dist/handlers/wallet-revokeSession.cjs.map +1 -1
- package/dist/handlers/wallet-revokeSession.d.cts +23 -14
- package/dist/handlers/wallet-revokeSession.d.cts.map +1 -1
- package/dist/handlers/wallet-revokeSession.d.mts +23 -14
- package/dist/handlers/wallet-revokeSession.d.mts.map +1 -1
- package/dist/handlers/wallet-revokeSession.mjs +3 -4
- package/dist/handlers/wallet-revokeSession.mjs.map +1 -1
- package/dist/index.cjs +3 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet-invokeMethod.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,2BAA2B,EAC3B,gBAAgB,EAChB,gBAAgB,EACjB,4CAA4C;
|
|
1
|
+
{"version":3,"file":"wallet-invokeMethod.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,2BAA2B,EAC3B,gBAAgB,EAChB,gBAAgB,EACjB,4CAA4C;AAQ7C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AACjE,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,WAAW,EAAE,wBAAwB;AAoC5E;;;;;;;;;;;;;;;;;GAiBG;AAC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AAG,EAAE,CAAC;AACf,CAAC;AAUD,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,cAAc,EAAE,wBAAwB;IACxC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,4BAA4B,EAAE,IAAI;QAClC,0BAA0B,EAAE,IAAI;QAChC,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,4BAA4B,EAAE,IAAI;KACnC;CACkC,CAAC","sourcesContent":["import type {\n Caip25CaveatValue,\n ExternalScopeString,\n} from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n assertIsInternalScopeString,\n getSessionScopes,\n parseScopeString,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { NetworkClientId } from '@metamask/network-controller';\nimport type { Caveat } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport { isObject, KnownCaipNamespace, numberToHex } from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nexport type WalletInvokeMethodParams = {\n scope: ExternalScopeString;\n request: Pick<JsonRpcRequest, 'method' | 'params'>;\n};\n\nexport type WalletInvokeMethodRequest =\n JsonRpcRequest<WalletInvokeMethodParams> & {\n origin: string;\n };\n\nexport type WalletInvokeMethodHooks = {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;\n getSelectedNetworkClientId: () => NetworkClientId;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];\n handleNonEvmRequestForOrigin: (params: {\n connectedAddresses: CaipAccountId[];\n scope: CaipChainId;\n request: JsonRpcRequest;\n }) => Promise<Json>;\n};\n\n/**\n * Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param\n * and instead uses the singular session for the origin if available.\n *\n * @param request - The request object.\n * @param response - The response object. Unused.\n * @param next - The next middleware function.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.getCaveatForOrigin - the hook for getting a caveat from a permission for an origin.\n * @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.\n * @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @param hooks.sortAccountIdsByLastSelected - A function that sorts accounts by their last selected order.\n * @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.\n * @returns Nothing.\n */\nasync function handleWalletInvokeMethod(\n request: WalletInvokeMethodRequest,\n response: PendingJsonRpcResponse<Json>,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletInvokeMethodHooks,\n) {\n if (!isObject(request.params)) {\n return end(rpcErrors.invalidParams({ data: { request } }));\n }\n\n const { scope, request: wrappedRequest } = request.params;\n assertIsInternalScopeString(scope);\n\n let caveat;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n );\n } catch {\n // noop\n }\n if (!caveat) {\n return end(providerErrors.unauthorized());\n }\n\n const scopeObject = getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n })[scope];\n\n if (!scopeObject?.methods?.includes(wrappedRequest.method)) {\n return end(providerErrors.unauthorized());\n }\n\n const { namespace, reference } = parseScopeString(scope);\n\n const isEvmRequest =\n (namespace === KnownCaipNamespace.Wallet &&\n (!reference || reference === KnownCaipNamespace.Eip155)) ||\n namespace === KnownCaipNamespace.Eip155;\n\n const unwrappedRequest = {\n ...request,\n scope,\n method: wrappedRequest.method,\n params: wrappedRequest.params,\n };\n\n if (isEvmRequest) {\n let networkClientId;\n if (namespace === KnownCaipNamespace.Wallet) {\n networkClientId = hooks.getSelectedNetworkClientId();\n } else if (namespace === KnownCaipNamespace.Eip155) {\n if (reference) {\n networkClientId = hooks.findNetworkClientIdByChainId(\n numberToHex(parseInt(reference, 10)),\n );\n }\n }\n\n if (!networkClientId) {\n console.error(\n 'failed to resolve network client for wallet_invokeMethod',\n request,\n );\n return end(rpcErrors.internal());\n }\n\n Object.assign(request, {\n ...unwrappedRequest,\n networkClientId,\n });\n return next();\n }\n\n try {\n response.result = await hooks.handleNonEvmRequestForOrigin({\n connectedAddresses: scopeObject.accounts,\n // Type assertion: We know that scope is not \"wallet\" by now because it\n // is already being handled above.\n scope: scope as CaipChainId,\n request: unwrappedRequest,\n });\n } catch (err) {\n return end(err as Error);\n }\n return end();\n}\n\nexport type WalletInvokeMethodHandler = MethodHandler<\n WalletInvokeMethodHooks,\n never,\n WalletInvokeMethodParams,\n Json,\n { origin: string }\n>;\n\nexport const walletInvokeMethodHandler = {\n implementation: handleWalletInvokeMethod,\n hookNames: {\n getCaveatForOrigin: true,\n findNetworkClientIdByChainId: true,\n getSelectedNetworkClientId: true,\n getNonEvmSupportedMethods: true,\n sortAccountIdsByLastSelected: true,\n handleNonEvmRequestForOrigin: true,\n },\n} satisfies WalletInvokeMethodHandler;\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.walletRevokeSessionHandler = void 0;
|
|
4
4
|
const chain_agnostic_permission_1 = require("@metamask/chain-agnostic-permission");
|
|
5
5
|
const permission_controller_1 = require("@metamask/permission-controller");
|
|
6
6
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
@@ -73,7 +73,7 @@ function partialRevokePermissions(scopes, hooks) {
|
|
|
73
73
|
* @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
|
|
74
74
|
* @returns Nothing.
|
|
75
75
|
*/
|
|
76
|
-
async function
|
|
76
|
+
async function handleWalletRevokeSession(request, response, _next, end, hooks) {
|
|
77
77
|
try {
|
|
78
78
|
if (request.params?.scopes?.length) {
|
|
79
79
|
partialRevokePermissions(request.params.scopes, hooks);
|
|
@@ -91,9 +91,8 @@ async function walletRevokeSessionHandler(request, response, _next, end, hooks)
|
|
|
91
91
|
response.result = true;
|
|
92
92
|
return end();
|
|
93
93
|
}
|
|
94
|
-
exports.
|
|
95
|
-
|
|
96
|
-
implementation: walletRevokeSessionHandler,
|
|
94
|
+
exports.walletRevokeSessionHandler = {
|
|
95
|
+
implementation: handleWalletRevokeSession,
|
|
97
96
|
hookNames: {
|
|
98
97
|
revokePermissionForOrigin: true,
|
|
99
98
|
updateCaveat: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet-revokeSession.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":";;;AAAA,mFAK6C;
|
|
1
|
+
{"version":3,"file":"wallet-revokeSession.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":";;;AAAA,mFAK6C;AAO7C,2EAIyC;AAEzC,qDAAiD;AACjD,2CAA2C;AAsB3C;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,KAAc;IACvC,IACE,CAAC,IAAA,gBAAQ,EAAC,KAAK,CAAC;QAChB,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC;QAClB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAC9B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO;QACL,gDAAwB,CAAC,IAAI;QAC7B,mDAA2B,CAAC,IAAI;K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AE,KAAK,CAAC,CAAC;QACzD,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,yBAAyB,CAAC,yDAA6B,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5B,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnB,OAAO,GAAG,CAAC,sBAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC;IACvB,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAUY,QAAA,0BAA0B,GAAG;IACxC,cAAc,EAAE,yBAAyB;IACzC,SAAS,EAAE;QACT,yBAAyB,EAAE,IAAI;QAC/B,YAAY,EAAE,IAAI;QAClB,kBAAkB,EAAE,IAAI;KACzB;CACmC,CAAC","sourcesContent":["import {\n Caip25CaveatMutators,\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getCaipAccountIdsFromCaip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport type { Caip25CaveatValue } from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport {\n CaveatMutatorOperation,\n PermissionDoesNotExistError,\n UnrecognizedSubjectError,\n} from '@metamask/permission-controller';\nimport type { Caveat } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isObject } from '@metamask/utils';\nimport type {\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nexport type WalletRevokeSessionHooks = {\n revokePermissionForOrigin: (permissionName: string) => void;\n updateCaveat: (\n target: string,\n caveatType: string,\n caveatValue: Caip25CaveatValue,\n ) => void;\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n};\n\ntype WalletRevokeSessionParams = { scopes?: string[] };\n\n/**\n * Check whether the given error is a permission error.\n *\n * @param error - The error to check.\n * @returns Whether the error is a permission error.\n */\nfunction isPermissionError(error: unknown) {\n if (\n !isObject(error) ||\n !('name' in error) ||\n typeof error.name !== 'string'\n ) {\n return false;\n }\n\n return [\n UnrecognizedSubjectError.name,\n PermissionDoesNotExistError.name,\n ].includes(error.name);\n}\n\n/**\n * Revokes specific session scopes from an existing caveat.\n * Fully revokes permission if no accounts remain permitted after iterating through scopes.\n *\n * @param scopes - Array of scope strings to remove from the caveat.\n * @param hooks - The hooks object.\n * @param hooks.revokePermissionForOrigin - The hook for revoking a permission for an origin function.\n * @param hooks.updateCaveat - The hook used to conditionally update the caveat rather than fully revoke the permission.\n * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.\n */\nfunction partialRevokePermissions(\n scopes: string[],\n hooks: WalletRevokeSessionHooks,\n) {\n let updatedCaveatValue = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ).value;\n\n for (const scopeString of scopes) {\n const result = Caip25CaveatMutators[Caip25CaveatType].removeScope(\n updatedCaveatValue,\n scopeString,\n );\n\n // If operation is a Noop, it means a scope was passed that was not present in the permission, so we proceed with the loop\n if (result.operation === CaveatMutatorOperation.Noop) {\n continue;\n }\n\n updatedCaveatValue = result?.value ?? {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: {},\n isMultichainOrigin: true,\n };\n }\n\n const caipAccountIds =\n getCaipAccountIdsFromCaip25CaveatValue(updatedCaveatValue);\n\n // We fully revoke permission if no accounts are left after scope removal loop.\n if (!caipAccountIds.length) {\n hooks.revokePermissionForOrigin(Caip25EndowmentPermissionName);\n } else {\n hooks.updateCaveat(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n updatedCaveatValue,\n );\n }\n}\n\n/**\n * Handler for the `wallet_revokeSession` RPC method as specified by [CAIP-285](https://chainagnostic.org/CAIPs/caip-285).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param\n * and instead revokes the singular session for the origin if available. Additionally,\n * the handler also does not return an error if there is currently no active session and instead\n * returns true which is the same result returned if an active session was actually revoked.\n *\n * @param request - The JSON-RPC request object. Unused.\n * @param response - The JSON-RPC response object.\n * @param _next - The next middleware function. Unused.\n * @param end - The end callback function.\n * @param hooks - The hooks object.\n * @param hooks.revokePermissionForOrigin - The hook for revoking a permission for an origin function.\n * @param hooks.updateCaveat - The hook used to conditionally update the caveat rather than fully revoke the permission.\n * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.\n * @returns Nothing.\n */\nasync function handleWalletRevokeSession(\n request: JsonRpcRequest<WalletRevokeSessionParams> & { origin: string },\n response: PendingJsonRpcResponse<Json>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletRevokeSessionHooks,\n) {\n try {\n if (request.params?.scopes?.length) {\n partialRevokePermissions(request.params.scopes, hooks);\n } else {\n hooks.revokePermissionForOrigin(Caip25EndowmentPermissionName);\n }\n } catch (err) {\n if (!isPermissionError(err)) {\n console.error(err);\n return end(rpcErrors.internal());\n }\n }\n\n response.result = true;\n return end();\n}\n\nexport type WalletRevokeSessionHandler = MethodHandler<\n WalletRevokeSessionHooks,\n never,\n WalletRevokeSessionParams,\n Json,\n { origin: string }\n>;\n\nexport const walletRevokeSessionHandler = {\n implementation: handleWalletRevokeSession,\n hookNames: {\n revokePermissionForOrigin: true,\n updateCaveat: true,\n getCaveatForOrigin: true,\n },\n} satisfies WalletRevokeSessionHandler;\n"]}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
1
|
+
import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
import type { Caip25CaveatValue } from "@metamask/chain-agnostic-permission";
|
|
3
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from "@metamask/json-rpc-engine";
|
|
4
|
+
import type { Caveat } from "@metamask/permission-controller";
|
|
5
|
+
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
6
|
+
export type WalletRevokeSessionHooks = {
|
|
7
|
+
revokePermissionForOrigin: (permissionName: string) => void;
|
|
8
|
+
updateCaveat: (target: string, caveatType: string, caveatValue: Caip25CaveatValue) => void;
|
|
9
|
+
getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
|
|
10
|
+
};
|
|
11
|
+
type WalletRevokeSessionParams = {
|
|
12
|
+
scopes?: string[];
|
|
13
|
+
};
|
|
4
14
|
/**
|
|
5
15
|
* Handler for the `wallet_revokeSession` RPC method as specified by [CAIP-285](https://chainagnostic.org/CAIPs/caip-285).
|
|
6
16
|
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param
|
|
@@ -18,19 +28,18 @@ import type { WalletRevokeSessionHooks } from "./types.cjs";
|
|
|
18
28
|
* @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
|
|
19
29
|
* @returns Nothing.
|
|
20
30
|
*/
|
|
21
|
-
declare function
|
|
31
|
+
declare function handleWalletRevokeSession(request: JsonRpcRequest<WalletRevokeSessionParams> & {
|
|
22
32
|
origin: string;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
export declare const
|
|
28
|
-
|
|
29
|
-
implementation: typeof walletRevokeSessionHandler;
|
|
33
|
+
}, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletRevokeSessionHooks): Promise<void>;
|
|
34
|
+
export type WalletRevokeSessionHandler = MethodHandler<WalletRevokeSessionHooks, never, WalletRevokeSessionParams, Json, {
|
|
35
|
+
origin: string;
|
|
36
|
+
}>;
|
|
37
|
+
export declare const walletRevokeSessionHandler: {
|
|
38
|
+
implementation: typeof handleWalletRevokeSession;
|
|
30
39
|
hookNames: {
|
|
31
|
-
revokePermissionForOrigin:
|
|
32
|
-
updateCaveat:
|
|
33
|
-
getCaveatForOrigin:
|
|
40
|
+
revokePermissionForOrigin: true;
|
|
41
|
+
updateCaveat: true;
|
|
42
|
+
getCaveatForOrigin: true;
|
|
34
43
|
};
|
|
35
44
|
};
|
|
36
45
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet-revokeSession.d.cts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"wallet-revokeSession.d.cts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAGjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,4CAA4C;AAC7E,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACd,kCAAkC;AAMnC,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAG9D,OAAO,KAAK,EACV,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB,MAAM,MAAM,wBAAwB,GAAG;IACrC,yBAAyB,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5D,YAAY,EAAE,CACZ,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,iBAAiB,KAC3B,IAAI,CAAC;IACV,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;CACzD,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AA4EvD;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,yBAAyB,CACtC,OAAO,EAAE,cAAc,CAAC,yBAAyB,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EACvE,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,wBAAwB,iBAiBhC;AAED,MAAM,MAAM,0BAA0B,GAAG,aAAa,CACpD,wBAAwB,EACxB,KAAK,EACL,yBAAyB,EACzB,IAAI,EACJ;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CACnB,CAAC;AAEF,eAAO,MAAM,0BAA0B;;;;;;;CAOD,CAAC"}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
1
|
+
import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
|
|
2
|
+
import type { Caip25CaveatValue } from "@metamask/chain-agnostic-permission";
|
|
3
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from "@metamask/json-rpc-engine";
|
|
4
|
+
import type { Caveat } from "@metamask/permission-controller";
|
|
5
|
+
import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
|
|
6
|
+
export type WalletRevokeSessionHooks = {
|
|
7
|
+
revokePermissionForOrigin: (permissionName: string) => void;
|
|
8
|
+
updateCaveat: (target: string, caveatType: string, caveatValue: Caip25CaveatValue) => void;
|
|
9
|
+
getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
|
|
10
|
+
};
|
|
11
|
+
type WalletRevokeSessionParams = {
|
|
12
|
+
scopes?: string[];
|
|
13
|
+
};
|
|
4
14
|
/**
|
|
5
15
|
* Handler for the `wallet_revokeSession` RPC method as specified by [CAIP-285](https://chainagnostic.org/CAIPs/caip-285).
|
|
6
16
|
* The implementation below deviates from the linked spec in that it ignores the `sessionId` param
|
|
@@ -18,19 +28,18 @@ import type { WalletRevokeSessionHooks } from "./types.mjs";
|
|
|
18
28
|
* @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
|
|
19
29
|
* @returns Nothing.
|
|
20
30
|
*/
|
|
21
|
-
declare function
|
|
31
|
+
declare function handleWalletRevokeSession(request: JsonRpcRequest<WalletRevokeSessionParams> & {
|
|
22
32
|
origin: string;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
export declare const
|
|
28
|
-
|
|
29
|
-
implementation: typeof walletRevokeSessionHandler;
|
|
33
|
+
}, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletRevokeSessionHooks): Promise<void>;
|
|
34
|
+
export type WalletRevokeSessionHandler = MethodHandler<WalletRevokeSessionHooks, never, WalletRevokeSessionParams, Json, {
|
|
35
|
+
origin: string;
|
|
36
|
+
}>;
|
|
37
|
+
export declare const walletRevokeSessionHandler: {
|
|
38
|
+
implementation: typeof handleWalletRevokeSession;
|
|
30
39
|
hookNames: {
|
|
31
|
-
revokePermissionForOrigin:
|
|
32
|
-
updateCaveat:
|
|
33
|
-
getCaveatForOrigin:
|
|
40
|
+
revokePermissionForOrigin: true;
|
|
41
|
+
updateCaveat: true;
|
|
42
|
+
getCaveatForOrigin: true;
|
|
34
43
|
};
|
|
35
44
|
};
|
|
36
45
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet-revokeSession.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"wallet-revokeSession.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAGjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,4CAA4C;AAC7E,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACd,kCAAkC;AAMnC,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAG9D,OAAO,KAAK,EACV,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB,MAAM,MAAM,wBAAwB,GAAG;IACrC,yBAAyB,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5D,YAAY,EAAE,CACZ,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,iBAAiB,KAC3B,IAAI,CAAC;IACV,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;CACzD,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AA4EvD;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,yBAAyB,CACtC,OAAO,EAAE,cAAc,CAAC,yBAAyB,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EACvE,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,wBAAwB,iBAiBhC;AAED,MAAM,MAAM,0BAA0B,GAAG,aAAa,CACpD,wBAAwB,EACxB,KAAK,EACL,yBAAyB,EACzB,IAAI,EACJ;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CACnB,CAAC;AAEF,eAAO,MAAM,0BAA0B;;;;;;;CAOD,CAAC"}
|
|
@@ -70,7 +70,7 @@ function partialRevokePermissions(scopes, hooks) {
|
|
|
70
70
|
* @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
|
|
71
71
|
* @returns Nothing.
|
|
72
72
|
*/
|
|
73
|
-
async function
|
|
73
|
+
async function handleWalletRevokeSession(request, response, _next, end, hooks) {
|
|
74
74
|
try {
|
|
75
75
|
if (request.params?.scopes?.length) {
|
|
76
76
|
partialRevokePermissions(request.params.scopes, hooks);
|
|
@@ -88,9 +88,8 @@ async function walletRevokeSessionHandler(request, response, _next, end, hooks)
|
|
|
88
88
|
response.result = true;
|
|
89
89
|
return end();
|
|
90
90
|
}
|
|
91
|
-
export const
|
|
92
|
-
|
|
93
|
-
implementation: walletRevokeSessionHandler,
|
|
91
|
+
export const walletRevokeSessionHandler = {
|
|
92
|
+
implementation: handleWalletRevokeSession,
|
|
94
93
|
hookNames: {
|
|
95
94
|
revokePermissionForOrigin: true,
|
|
96
95
|
updateCaveat: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet-revokeSession.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,6BAA6B,EAC7B,sCAAsC,EACvC,4CAA4C;
|
|
1
|
+
{"version":3,"file":"wallet-revokeSession.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,6BAA6B,EAC7B,sCAAsC,EACvC,4CAA4C;AAO7C,OAAO,EACL,sBAAsB,EACtB,2BAA2B,EAC3B,wBAAwB,EACzB,wCAAwC;AAEzC,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAsB3C;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,KAAc;IACvC,IACE,CAAC,QAAQ,CAAC,KAAK,CAAC;QAChB,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC;QAClB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAC9B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO;QACL,wBAAwB,CAAC,IAAI;QAC7B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M,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QACzD,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,yBAAyB,CAAC,6BAA6B,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5B,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnB,OAAO,GAAG,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC;IACvB,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAUD,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,cAAc,EAAE,yBAAyB;IACzC,SAAS,EAAE;QACT,yBAAyB,EAAE,IAAI;QAC/B,YAAY,EAAE,IAAI;QAClB,kBAAkB,EAAE,IAAI;KACzB;CACmC,CAAC","sourcesContent":["import {\n Caip25CaveatMutators,\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getCaipAccountIdsFromCaip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport type { Caip25CaveatValue } from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport {\n CaveatMutatorOperation,\n PermissionDoesNotExistError,\n UnrecognizedSubjectError,\n} from '@metamask/permission-controller';\nimport type { Caveat } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isObject } from '@metamask/utils';\nimport type {\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nexport type WalletRevokeSessionHooks = {\n revokePermissionForOrigin: (permissionName: string) => void;\n updateCaveat: (\n target: string,\n caveatType: string,\n caveatValue: Caip25CaveatValue,\n ) => void;\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n};\n\ntype WalletRevokeSessionParams = { scopes?: string[] };\n\n/**\n * Check whether the given error is a permission error.\n *\n * @param error - The error to check.\n * @returns Whether the error is a permission error.\n */\nfunction isPermissionError(error: unknown) {\n if (\n !isObject(error) ||\n !('name' in error) ||\n typeof error.name !== 'string'\n ) {\n return false;\n }\n\n return [\n UnrecognizedSubjectError.name,\n PermissionDoesNotExistError.name,\n ].includes(error.name);\n}\n\n/**\n * Revokes specific session scopes from an existing caveat.\n * Fully revokes permission if no accounts remain permitted after iterating through scopes.\n *\n * @param scopes - Array of scope strings to remove from the caveat.\n * @param hooks - The hooks object.\n * @param hooks.revokePermissionForOrigin - The hook for revoking a permission for an origin function.\n * @param hooks.updateCaveat - The hook used to conditionally update the caveat rather than fully revoke the permission.\n * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.\n */\nfunction partialRevokePermissions(\n scopes: string[],\n hooks: WalletRevokeSessionHooks,\n) {\n let updatedCaveatValue = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ).value;\n\n for (const scopeString of scopes) {\n const result = Caip25CaveatMutators[Caip25CaveatType].removeScope(\n updatedCaveatValue,\n scopeString,\n );\n\n // If operation is a Noop, it means a scope was passed that was not present in the permission, so we proceed with the loop\n if (result.operation === CaveatMutatorOperation.Noop) {\n continue;\n }\n\n updatedCaveatValue = result?.value ?? {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: {},\n isMultichainOrigin: true,\n };\n }\n\n const caipAccountIds =\n getCaipAccountIdsFromCaip25CaveatValue(updatedCaveatValue);\n\n // We fully revoke permission if no accounts are left after scope removal loop.\n if (!caipAccountIds.length) {\n hooks.revokePermissionForOrigin(Caip25EndowmentPermissionName);\n } else {\n hooks.updateCaveat(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n updatedCaveatValue,\n );\n }\n}\n\n/**\n * Handler for the `wallet_revokeSession` RPC method as specified by [CAIP-285](https://chainagnostic.org/CAIPs/caip-285).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param\n * and instead revokes the singular session for the origin if available. Additionally,\n * the handler also does not return an error if there is currently no active session and instead\n * returns true which is the same result returned if an active session was actually revoked.\n *\n * @param request - The JSON-RPC request object. Unused.\n * @param response - The JSON-RPC response object.\n * @param _next - The next middleware function. Unused.\n * @param end - The end callback function.\n * @param hooks - The hooks object.\n * @param hooks.revokePermissionForOrigin - The hook for revoking a permission for an origin function.\n * @param hooks.updateCaveat - The hook used to conditionally update the caveat rather than fully revoke the permission.\n * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.\n * @returns Nothing.\n */\nasync function handleWalletRevokeSession(\n request: JsonRpcRequest<WalletRevokeSessionParams> & { origin: string },\n response: PendingJsonRpcResponse<Json>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletRevokeSessionHooks,\n) {\n try {\n if (request.params?.scopes?.length) {\n partialRevokePermissions(request.params.scopes, hooks);\n } else {\n hooks.revokePermissionForOrigin(Caip25EndowmentPermissionName);\n }\n } catch (err) {\n if (!isPermissionError(err)) {\n console.error(err);\n return end(rpcErrors.internal());\n }\n }\n\n response.result = true;\n return end();\n}\n\nexport type WalletRevokeSessionHandler = MethodHandler<\n WalletRevokeSessionHooks,\n never,\n WalletRevokeSessionParams,\n Json,\n { origin: string }\n>;\n\nexport const walletRevokeSessionHandler = {\n implementation: handleWalletRevokeSession,\n hookNames: {\n revokePermissionForOrigin: true,\n updateCaveat: true,\n getCaveatForOrigin: true,\n },\n} satisfies WalletRevokeSessionHandler;\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MultichainApiNotifications = exports.MultichainSubscriptionManager = exports.MultichainMiddlewareManager = exports.multichainMethodCallValidatorMiddleware = exports.
|
|
4
|
-
var
|
|
5
|
-
Object.defineProperty(exports, "
|
|
6
|
-
var wallet_getSession_1 = require("./handlers/wallet-getSession.cjs");
|
|
7
|
-
Object.defineProperty(exports, "walletGetSession", { enumerable: true, get: function () { return wallet_getSession_1.walletGetSession; } });
|
|
8
|
-
var wallet_invokeMethod_1 = require("./handlers/wallet-invokeMethod.cjs");
|
|
9
|
-
Object.defineProperty(exports, "walletInvokeMethod", { enumerable: true, get: function () { return wallet_invokeMethod_1.walletInvokeMethod; } });
|
|
10
|
-
var wallet_revokeSession_1 = require("./handlers/wallet-revokeSession.cjs");
|
|
11
|
-
Object.defineProperty(exports, "walletRevokeSession", { enumerable: true, get: function () { return wallet_revokeSession_1.walletRevokeSession; } });
|
|
3
|
+
exports.MultichainApiNotifications = exports.MultichainSubscriptionManager = exports.MultichainMiddlewareManager = exports.multichainMethodCallValidatorMiddleware = exports.methodHandlers = void 0;
|
|
4
|
+
var handlers_1 = require("./handlers/index.cjs");
|
|
5
|
+
Object.defineProperty(exports, "methodHandlers", { enumerable: true, get: function () { return handlers_1.methodHandlers; } });
|
|
12
6
|
var multichainMethodCallValidatorMiddleware_1 = require("./middlewares/multichainMethodCallValidatorMiddleware.cjs");
|
|
13
7
|
Object.defineProperty(exports, "multichainMethodCallValidatorMiddleware", { enumerable: true, get: function () { return multichainMethodCallValidatorMiddleware_1.multichainMethodCallValidatorMiddleware; } });
|
|
14
8
|
var MultichainMiddlewareManager_1 = require("./middlewares/MultichainMiddlewareManager.cjs");
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAA4C;AAAnC,0GAAA,cAAc,OAAA;AAEvB,qHAAgH;AAAvG,kKAAA,uCAAuC,OAAA;AAChD,6FAAwF;AAA/E,0IAAA,2BAA2B,OAAA;AACpC,iGAA4F;AAAnF,8IAAA,6BAA6B,OAAA;AACtC,8CAA8D;AAArD,mHAAA,0BAA0B,OAAA","sourcesContent":["export { methodHandlers } from './handlers';\n\nexport { multichainMethodCallValidatorMiddleware } from './middlewares/multichainMethodCallValidatorMiddleware';\nexport { MultichainMiddlewareManager } from './middlewares/MultichainMiddlewareManager';\nexport { MultichainSubscriptionManager } from './middlewares/MultichainSubscriptionManager';\nexport { MultichainApiNotifications } from './handlers/types';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { walletGetSession } from "./handlers/wallet-getSession.cjs";
|
|
3
|
-
export { walletInvokeMethod } from "./handlers/wallet-invokeMethod.cjs";
|
|
4
|
-
export { walletRevokeSession } from "./handlers/wallet-revokeSession.cjs";
|
|
1
|
+
export { methodHandlers } from "./handlers/index.cjs";
|
|
5
2
|
export { multichainMethodCallValidatorMiddleware } from "./middlewares/multichainMethodCallValidatorMiddleware.cjs";
|
|
6
3
|
export { MultichainMiddlewareManager } from "./middlewares/MultichainMiddlewareManager.cjs";
|
|
7
4
|
export { MultichainSubscriptionManager } from "./middlewares/MultichainSubscriptionManager.cjs";
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAmB;AAE5C,OAAO,EAAE,uCAAuC,EAAE,kEAA8D;AAChH,OAAO,EAAE,2BAA2B,EAAE,sDAAkD;AACxF,OAAO,EAAE,6BAA6B,EAAE,wDAAoD;AAC5F,OAAO,EAAE,0BAA0B,EAAE,6BAAyB"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { walletGetSession } from "./handlers/wallet-getSession.mjs";
|
|
3
|
-
export { walletInvokeMethod } from "./handlers/wallet-invokeMethod.mjs";
|
|
4
|
-
export { walletRevokeSession } from "./handlers/wallet-revokeSession.mjs";
|
|
1
|
+
export { methodHandlers } from "./handlers/index.mjs";
|
|
5
2
|
export { multichainMethodCallValidatorMiddleware } from "./middlewares/multichainMethodCallValidatorMiddleware.mjs";
|
|
6
3
|
export { MultichainMiddlewareManager } from "./middlewares/MultichainMiddlewareManager.mjs";
|
|
7
4
|
export { MultichainSubscriptionManager } from "./middlewares/MultichainSubscriptionManager.mjs";
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAmB;AAE5C,OAAO,EAAE,uCAAuC,EAAE,kEAA8D;AAChH,OAAO,EAAE,2BAA2B,EAAE,sDAAkD;AACxF,OAAO,EAAE,6BAA6B,EAAE,wDAAoD;AAC5F,OAAO,EAAE,0BAA0B,EAAE,6BAAyB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { walletGetSession } from "./handlers/wallet-getSession.mjs";
|
|
3
|
-
export { walletInvokeMethod } from "./handlers/wallet-invokeMethod.mjs";
|
|
4
|
-
export { walletRevokeSession } from "./handlers/wallet-revokeSession.mjs";
|
|
1
|
+
export { methodHandlers } from "./handlers/index.mjs";
|
|
5
2
|
export { multichainMethodCallValidatorMiddleware } from "./middlewares/multichainMethodCallValidatorMiddleware.mjs";
|
|
6
3
|
export { MultichainMiddlewareManager } from "./middlewares/MultichainMiddlewareManager.mjs";
|
|
7
4
|
export { MultichainSubscriptionManager } from "./middlewares/MultichainSubscriptionManager.mjs";
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,6BAAmB;AAE5C,OAAO,EAAE,uCAAuC,EAAE,kEAA8D;AAChH,OAAO,EAAE,2BAA2B,EAAE,sDAAkD;AACxF,OAAO,EAAE,6BAA6B,EAAE,wDAAoD;AAC5F,OAAO,EAAE,0BAA0B,EAAE,6BAAyB","sourcesContent":["export { methodHandlers } from './handlers';\n\nexport { multichainMethodCallValidatorMiddleware } from './middlewares/multichainMethodCallValidatorMiddleware';\nexport { MultichainMiddlewareManager } from './middlewares/MultichainMiddlewareManager';\nexport { MultichainSubscriptionManager } from './middlewares/MultichainSubscriptionManager';\nexport { MultichainApiNotifications } from './handlers/types';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/multichain-api-middleware",
|
|
3
|
-
"version": "2.0.0-preview-
|
|
3
|
+
"version": "2.0.0-preview-a1caffc9c",
|
|
4
4
|
"description": "JSON-RPC methods and middleware to support the MetaMask Multichain API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"@metamask/chain-agnostic-permission": "^1.5.0",
|
|
56
56
|
"@metamask/controller-utils": "^11.20.0",
|
|
57
57
|
"@metamask/json-rpc-engine": "^10.2.4",
|
|
58
|
-
"@metamask/network-controller": "^30.1
|
|
58
|
+
"@metamask/network-controller": "^30.0.1",
|
|
59
59
|
"@metamask/permission-controller": "^12.3.0",
|
|
60
60
|
"@metamask/rpc-errors": "^7.0.2",
|
|
61
61
|
"@metamask/utils": "^11.9.0",
|