@metamask/multichain-api-middleware 1.2.7 → 3.0.0

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +45 -3
  2. package/dist/handlers/index.cjs +20 -0
  3. package/dist/handlers/index.cjs.map +1 -0
  4. package/dist/handlers/index.d.cts +25 -0
  5. package/dist/handlers/index.d.cts.map +1 -0
  6. package/dist/handlers/index.d.mts +25 -0
  7. package/dist/handlers/index.d.mts.map +1 -0
  8. package/dist/handlers/index.mjs +17 -0
  9. package/dist/handlers/index.mjs.map +1 -0
  10. package/dist/handlers/types.cjs +1 -0
  11. package/dist/handlers/types.cjs.map +1 -1
  12. package/dist/handlers/types.d.cts +13 -9
  13. package/dist/handlers/types.d.cts.map +1 -1
  14. package/dist/handlers/types.d.mts +13 -9
  15. package/dist/handlers/types.d.mts.map +1 -1
  16. package/dist/handlers/types.mjs +1 -0
  17. package/dist/handlers/types.mjs.map +1 -1
  18. package/dist/handlers/wallet-createSession.cjs +8 -6
  19. package/dist/handlers/wallet-createSession.cjs.map +1 -1
  20. package/dist/handlers/wallet-createSession.d.cts +36 -30
  21. package/dist/handlers/wallet-createSession.d.cts.map +1 -1
  22. package/dist/handlers/wallet-createSession.d.mts +36 -30
  23. package/dist/handlers/wallet-createSession.d.mts.map +1 -1
  24. package/dist/handlers/wallet-createSession.mjs +7 -5
  25. package/dist/handlers/wallet-createSession.mjs.map +1 -1
  26. package/dist/handlers/wallet-getSession.cjs +7 -5
  27. package/dist/handlers/wallet-getSession.cjs.map +1 -1
  28. package/dist/handlers/wallet-getSession.d.cts +19 -16
  29. package/dist/handlers/wallet-getSession.d.cts.map +1 -1
  30. package/dist/handlers/wallet-getSession.d.mts +19 -16
  31. package/dist/handlers/wallet-getSession.d.mts.map +1 -1
  32. package/dist/handlers/wallet-getSession.mjs +6 -4
  33. package/dist/handlers/wallet-getSession.mjs.map +1 -1
  34. package/dist/handlers/wallet-invokeMethod.cjs +10 -5
  35. package/dist/handlers/wallet-invokeMethod.cjs.map +1 -1
  36. package/dist/handlers/wallet-invokeMethod.d.cts +33 -29
  37. package/dist/handlers/wallet-invokeMethod.d.cts.map +1 -1
  38. package/dist/handlers/wallet-invokeMethod.d.mts +33 -29
  39. package/dist/handlers/wallet-invokeMethod.d.mts.map +1 -1
  40. package/dist/handlers/wallet-invokeMethod.mjs +10 -5
  41. package/dist/handlers/wallet-invokeMethod.mjs.map +1 -1
  42. package/dist/handlers/wallet-revokeSession.cjs +9 -6
  43. package/dist/handlers/wallet-revokeSession.cjs.map +1 -1
  44. package/dist/handlers/wallet-revokeSession.d.cts +22 -14
  45. package/dist/handlers/wallet-revokeSession.d.cts.map +1 -1
  46. package/dist/handlers/wallet-revokeSession.d.mts +22 -14
  47. package/dist/handlers/wallet-revokeSession.d.mts.map +1 -1
  48. package/dist/handlers/wallet-revokeSession.mjs +8 -5
  49. package/dist/handlers/wallet-revokeSession.mjs.map +1 -1
  50. package/dist/index.cjs +3 -9
  51. package/dist/index.cjs.map +1 -1
  52. package/dist/index.d.cts +2 -4
  53. package/dist/index.d.cts.map +1 -1
  54. package/dist/index.d.mts +2 -4
  55. package/dist/index.d.mts.map +1 -1
  56. package/dist/index.mjs +1 -4
  57. package/dist/index.mjs.map +1 -1
  58. package/dist/middlewares/MultichainSubscriptionManager.cjs.map +1 -1
  59. package/dist/middlewares/MultichainSubscriptionManager.d.cts.map +1 -1
  60. package/dist/middlewares/MultichainSubscriptionManager.d.mts.map +1 -1
  61. package/dist/middlewares/MultichainSubscriptionManager.mjs.map +1 -1
  62. package/package.json +22 -21
@@ -1,14 +1,24 @@
1
- import type { Caip25CaveatValue, ExternalScopeString } from "@metamask/chain-agnostic-permission";
2
- import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
3
- import type { NetworkClientId } from "@metamask/network-controller";
4
- import type { Caveat } from "@metamask/permission-controller";
5
- import type { CaipAccountId, CaipChainId, Hex, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
6
- export type WalletInvokeMethodRequest = JsonRpcRequest & {
1
+ import type { ExternalScopeString } from "@metamask/chain-agnostic-permission";
2
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from "@metamask/json-rpc-engine";
3
+ import type { NetworkClientId, NetworkController } from "@metamask/network-controller";
4
+ import type { MultichainRoutingService } from "@metamask/snaps-controllers";
5
+ import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
6
+ import type { GetCaveatForOriginHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from "./types.cjs";
7
+ export type WalletInvokeMethodParams = {
8
+ scope: ExternalScopeString;
9
+ request: Pick<JsonRpcRequest, 'method' | 'params'>;
10
+ };
11
+ export type WalletInvokeMethodRequest = JsonRpcRequest<WalletInvokeMethodParams> & {
7
12
  origin: string;
8
- params: {
9
- scope: ExternalScopeString;
10
- request: Pick<JsonRpcRequest, 'method' | 'params'>;
11
- };
13
+ };
14
+ export type WalletInvokeMethodHooks = GetCaveatForOriginHook & GetNonEvmSupportedMethodsHook & SortAccountIdsByLastSelectedHook & {
15
+ findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];
16
+ getSelectedNetworkClientId: () => NetworkClientId;
17
+ handleNonEvmRequestForOrigin: (params: {
18
+ connectedAddresses: CaipAccountId[];
19
+ scope: CaipChainId;
20
+ request: JsonRpcRequest;
21
+ }) => ReturnType<MultichainRoutingService['handleRequest']>;
12
22
  };
13
23
  /**
14
24
  * Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
@@ -24,29 +34,23 @@ export type WalletInvokeMethodRequest = JsonRpcRequest & {
24
34
  * @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.
25
35
  * @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.
26
36
  * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
37
+ * @param hooks.sortAccountIdsByLastSelected - A function that sorts accounts by their last selected order.
27
38
  * @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.
28
39
  * @returns Nothing.
29
40
  */
30
- declare function walletInvokeMethodHandler(request: WalletInvokeMethodRequest, response: PendingJsonRpcResponse, next: () => void, end: (error?: Error) => void, hooks: {
31
- getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
32
- findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;
33
- getSelectedNetworkClientId: () => NetworkClientId;
34
- getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
35
- handleNonEvmRequestForOrigin: (params: {
36
- connectedAddresses: CaipAccountId[];
37
- scope: CaipChainId;
38
- request: JsonRpcRequest;
39
- }) => Promise<Json>;
40
- }): Promise<void>;
41
- export declare const walletInvokeMethod: {
42
- methodNames: string[];
43
- implementation: typeof walletInvokeMethodHandler;
41
+ declare function handleWalletInvokeMethod(request: WalletInvokeMethodRequest, response: PendingJsonRpcResponse<Json>, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletInvokeMethodHooks): Promise<void>;
42
+ export type WalletInvokeMethodHandler = MethodHandler<WalletInvokeMethodHooks, never, WalletInvokeMethodParams, Json, {
43
+ origin: string;
44
+ }>;
45
+ export declare const walletInvokeMethodHandler: {
46
+ implementation: typeof handleWalletInvokeMethod;
44
47
  hookNames: {
45
- getCaveatForOrigin: boolean;
46
- findNetworkClientIdByChainId: boolean;
47
- getSelectedNetworkClientId: boolean;
48
- getNonEvmSupportedMethods: boolean;
49
- handleNonEvmRequestForOrigin: boolean;
48
+ getCaveatForOrigin: true;
49
+ findNetworkClientIdByChainId: true;
50
+ getSelectedNetworkClientId: true;
51
+ getNonEvmSupportedMethods: true;
52
+ sortAccountIdsByLastSelected: true;
53
+ handleNonEvmRequestForOrigin: true;
50
54
  };
51
55
  };
52
56
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-invokeMethod.d.cts","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EACpB,4CAA4C;AAC7C,OAAO,EACL,gBAAgB,EAKjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,eAAe,EAAE,qCAAqC;AACpE,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAE9D,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,GAAG,EACH,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAGzB,MAAM,MAAM,yBAAyB,GAAG,cAAc,GAAG;IACvD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE;QACN,KAAK,EAAE,mBAAmB,CAAC;QAC3B,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,GAAG,QAAQ,CAAC,CAAC;KACpD,CAAC;CACH,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,yBAAyB,CACtC,OAAO,EAAE,yBAAyB,EAClC,QAAQ,EAAE,sBAAsB,EAChC,IAAI,EAAE,MAAM,IAAI,EAChB,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,EAC5B,KAAK,EAAE;IACL,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;IACxD,4BAA4B,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,eAAe,GAAG,SAAS,CAAC;IAC5E,0BAA0B,EAAE,MAAM,eAAe,CAAC;IAClD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;IAC5D,4BAA4B,EAAE,CAAC,MAAM,EAAE;QACrC,kBAAkB,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,EAAE,cAAc,CAAC;KACzB,KAAK,UAAU,CAAC,wBAAwB,CAAC,eAAe,CAAC,CAAC,CAAC;CAC7D,CAAC;AAEJ;;;;;;;;;;;;;;;;;GAiBG;AACH,iBAAe,wBAAwB,CACrC,OAAO,EAAE,yBAAyB,EAClC,QAAQ,EAAE,sBAAsB,CAAC,IAAI,CAAC,EACtC,IAAI,EAAE,yBAAyB,EAC/B,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,uBAAuB,iBAoF/B;AAED,MAAM,MAAM,yBAAyB,GAAG,aAAa,CACnD,uBAAuB,EACvB,KAAK,EACL,wBAAwB,EACxB,IAAI,EACJ;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CACnB,CAAC;AAEF,eAAO,MAAM,yBAAyB;;;;;;;;;;CAUD,CAAC"}
@@ -1,14 +1,24 @@
1
- import type { Caip25CaveatValue, ExternalScopeString } from "@metamask/chain-agnostic-permission";
2
- import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
3
- import type { NetworkClientId } from "@metamask/network-controller";
4
- import type { Caveat } from "@metamask/permission-controller";
5
- import type { CaipAccountId, CaipChainId, Hex, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
6
- export type WalletInvokeMethodRequest = JsonRpcRequest & {
1
+ import type { ExternalScopeString } from "@metamask/chain-agnostic-permission";
2
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from "@metamask/json-rpc-engine";
3
+ import type { NetworkClientId, NetworkController } from "@metamask/network-controller";
4
+ import type { MultichainRoutingService } from "@metamask/snaps-controllers";
5
+ import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
6
+ import type { GetCaveatForOriginHook, GetNonEvmSupportedMethodsHook, SortAccountIdsByLastSelectedHook } from "./types.mjs";
7
+ export type WalletInvokeMethodParams = {
8
+ scope: ExternalScopeString;
9
+ request: Pick<JsonRpcRequest, 'method' | 'params'>;
10
+ };
11
+ export type WalletInvokeMethodRequest = JsonRpcRequest<WalletInvokeMethodParams> & {
7
12
  origin: string;
8
- params: {
9
- scope: ExternalScopeString;
10
- request: Pick<JsonRpcRequest, 'method' | 'params'>;
11
- };
13
+ };
14
+ export type WalletInvokeMethodHooks = GetCaveatForOriginHook & GetNonEvmSupportedMethodsHook & SortAccountIdsByLastSelectedHook & {
15
+ findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];
16
+ getSelectedNetworkClientId: () => NetworkClientId;
17
+ handleNonEvmRequestForOrigin: (params: {
18
+ connectedAddresses: CaipAccountId[];
19
+ scope: CaipChainId;
20
+ request: JsonRpcRequest;
21
+ }) => ReturnType<MultichainRoutingService['handleRequest']>;
12
22
  };
13
23
  /**
14
24
  * Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
@@ -24,29 +34,23 @@ export type WalletInvokeMethodRequest = JsonRpcRequest & {
24
34
  * @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.
25
35
  * @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.
26
36
  * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
37
+ * @param hooks.sortAccountIdsByLastSelected - A function that sorts accounts by their last selected order.
27
38
  * @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.
28
39
  * @returns Nothing.
29
40
  */
30
- declare function walletInvokeMethodHandler(request: WalletInvokeMethodRequest, response: PendingJsonRpcResponse, next: () => void, end: (error?: Error) => void, hooks: {
31
- getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
32
- findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId | undefined;
33
- getSelectedNetworkClientId: () => NetworkClientId;
34
- getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
35
- handleNonEvmRequestForOrigin: (params: {
36
- connectedAddresses: CaipAccountId[];
37
- scope: CaipChainId;
38
- request: JsonRpcRequest;
39
- }) => Promise<Json>;
40
- }): Promise<void>;
41
- export declare const walletInvokeMethod: {
42
- methodNames: string[];
43
- implementation: typeof walletInvokeMethodHandler;
41
+ declare function handleWalletInvokeMethod(request: WalletInvokeMethodRequest, response: PendingJsonRpcResponse<Json>, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletInvokeMethodHooks): Promise<void>;
42
+ export type WalletInvokeMethodHandler = MethodHandler<WalletInvokeMethodHooks, never, WalletInvokeMethodParams, Json, {
43
+ origin: string;
44
+ }>;
45
+ export declare const walletInvokeMethodHandler: {
46
+ implementation: typeof handleWalletInvokeMethod;
44
47
  hookNames: {
45
- getCaveatForOrigin: boolean;
46
- findNetworkClientIdByChainId: boolean;
47
- getSelectedNetworkClientId: boolean;
48
- getNonEvmSupportedMethods: boolean;
49
- handleNonEvmRequestForOrigin: boolean;
48
+ getCaveatForOrigin: true;
49
+ findNetworkClientIdByChainId: true;
50
+ getSelectedNetworkClientId: true;
51
+ getNonEvmSupportedMethods: true;
52
+ sortAccountIdsByLastSelected: true;
53
+ handleNonEvmRequestForOrigin: true;
50
54
  };
51
55
  };
52
56
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-invokeMethod.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EACpB,4CAA4C;AAC7C,OAAO,EACL,gBAAgB,EAKjB,4CAA4C;AAC7C,OAAO,KAAK,EAAE,eAAe,EAAE,qCAAqC;AACpE,OAAO,KAAK,EAAE,MAAM,EAAE,wCAAwC;AAE9D,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,GAAG,EACH,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAGzB,MAAM,MAAM,yBAAyB,GAAG,cAAc,GAAG;IACvD,MAAM,EAAE,MAAM,CAAC;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"}
@@ -1,6 +1,6 @@
1
1
  import { Caip25CaveatType, Caip25EndowmentPermissionName, assertIsInternalScopeString, getSessionScopes, parseScopeString } from "@metamask/chain-agnostic-permission";
2
2
  import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
3
- import { KnownCaipNamespace, numberToHex } from "@metamask/utils";
3
+ import { isObject, KnownCaipNamespace, numberToHex } from "@metamask/utils";
4
4
  /**
5
5
  * Handler for the `wallet_invokeMethod` RPC method as specified by [CAIP-27](https://chainagnostic.org/CAIPs/caip-27).
6
6
  * The implementation below deviates from the linked spec in that it ignores the `sessionId` param
@@ -15,10 +15,14 @@ import { KnownCaipNamespace, numberToHex } from "@metamask/utils";
15
15
  * @param hooks.findNetworkClientIdByChainId - the hook for finding the networkClientId for a chainId.
16
16
  * @param hooks.getSelectedNetworkClientId - the hook for getting the current globally selected networkClientId.
17
17
  * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
18
+ * @param hooks.sortAccountIdsByLastSelected - A function that sorts accounts by their last selected order.
18
19
  * @param hooks.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.
19
20
  * @returns Nothing.
20
21
  */
21
- async function walletInvokeMethodHandler(request, response, next, end, hooks) {
22
+ async function handleWalletInvokeMethod(request, response, next, end, hooks) {
23
+ if (!isObject(request.params)) {
24
+ return end(rpcErrors.invalidParams({ data: { request } }));
25
+ }
22
26
  const { scope, request: wrappedRequest } = request.params;
23
27
  assertIsInternalScopeString(scope);
24
28
  let caveat;
@@ -33,6 +37,7 @@ async function walletInvokeMethodHandler(request, response, next, end, hooks) {
33
37
  }
34
38
  const scopeObject = getSessionScopes(caveat.value, {
35
39
  getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,
40
+ sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,
36
41
  })[scope];
37
42
  if (!scopeObject?.methods?.includes(wrappedRequest.method)) {
38
43
  return end(providerErrors.unauthorized());
@@ -81,14 +86,14 @@ async function walletInvokeMethodHandler(request, response, next, end, hooks) {
81
86
  }
82
87
  return end();
83
88
  }
84
- export const walletInvokeMethod = {
85
- methodNames: ['wallet_invokeMethod'],
86
- implementation: walletInvokeMethodHandler,
89
+ export const walletInvokeMethodHandler = {
90
+ implementation: handleWalletInvokeMethod,
87
91
  hookNames: {
88
92
  getCaveatForOrigin: true,
89
93
  findNetworkClientIdByChainId: true,
90
94
  getSelectedNetworkClientId: true,
91
95
  getNonEvmSupportedMethods: true,
96
+ sortAccountIdsByLastSelected: true,
92
97
  handleNonEvmRequestForOrigin: true,
93
98
  },
94
99
  };
@@ -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;AAG7C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AASjE,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,wBAAwB;AAUlE;;;;;;;;;;;;;;;;GAgBG;AACH,KAAK,UAAU,yBAAyB,CACtC,OAAkC,EAClC,QAAgC,EAChC,IAAgB,EAChB,GAA4B,EAC5B,KAaC;IAED,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAE1D,2BAA2B,CAAC,KAAK,CAAC,CAAC;IAEnC,IAAI,MAAM,CAAC;IACX,IAAI,CAAC;QACH,MAAM,GAAG,KAAK,CAAC,kBAAkB,CAC/B,6BAA6B,EAC7B,gBAAgB,CACjB,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;IACT,CAAC;IACD,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,WAAW,GAAG,g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,CAAC;QACnC,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE;YACrB,GAAG,gBAAgB;YACnB,eAAe;SAChB,CAAC,CAAC;QACH,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC;IAED,IAAI,CAAC;QACH,QAAQ,CAAC,MAAM,GAAG,MAAM,KAAK,CAAC,4BAA4B,CAAC;YACzD,kBAAkB,EAAE,WAAW,CAAC,QAAQ;YACxC,uEAAuE;YACvE,kCAAkC;YAClC,KAAK,EAAE,KAAoB;YAC3B,OAAO,EAAE,gBAAgB;SAC1B,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC,GAAY,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AACD,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,WAAW,EAAE,CAAC,qBAAqB,CAAC;IACpC,cAAc,EAAE,yBAAyB;IACzC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,4BAA4B,EAAE,IAAI;QAClC,0BAA0B,EAAE,IAAI;QAChC,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;KACnC;CACF,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 { NetworkClientId } from '@metamask/network-controller';\nimport type { Caveat } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { KnownCaipNamespace, numberToHex } from '@metamask/utils';\n\nexport type WalletInvokeMethodRequest = JsonRpcRequest & {\n origin: string;\n params: {\n scope: ExternalScopeString;\n request: Pick<JsonRpcRequest, 'method' | 'params'>;\n };\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.handleNonEvmRequestForOrigin - A function that sends a request to the MultichainRouter for processing.\n * @returns Nothing.\n */\nasync function walletInvokeMethodHandler(\n request: WalletInvokeMethodRequest,\n response: PendingJsonRpcResponse,\n next: () => void,\n end: (error?: Error) => void,\n hooks: {\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 handleNonEvmRequestForOrigin: (params: {\n connectedAddresses: CaipAccountId[];\n scope: CaipChainId;\n request: JsonRpcRequest;\n }) => Promise<Json>;\n },\n) {\n const { scope, request: wrappedRequest } = request.params;\n\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 })[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}\nexport const walletInvokeMethod = {\n methodNames: ['wallet_invokeMethod'],\n implementation: walletInvokeMethodHandler,\n hookNames: {\n getCaveatForOrigin: true,\n findNetworkClientIdByChainId: true,\n getSelectedNetworkClientId: true,\n getNonEvmSupportedMethods: true,\n handleNonEvmRequestForOrigin: true,\n },\n};\n"]}
1
+ {"version":3,"file":"wallet-invokeMethod.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-invokeMethod.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,2BAA2B,EAC3B,gBAAgB,EAChB,gBAAgB,EACjB,4CAA4C;AAU7C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAEjE,OAAO,EAAE,QAAQ,EAAE,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,CAAC;QACb,OAAO,GAAG,CAAC,GAAY,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,GAAG,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 { ExternalScopeString } 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 {\n NetworkClientId,\n NetworkController,\n} from '@metamask/network-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type { MultichainRoutingService } from '@metamask/snaps-controllers';\nimport { isObject, KnownCaipNamespace, numberToHex } from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {\n Caip25Caveat,\n GetCaveatForOriginHook,\n GetNonEvmSupportedMethodsHook,\n SortAccountIdsByLastSelectedHook,\n} from './types';\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 = GetCaveatForOriginHook &\n GetNonEvmSupportedMethodsHook &\n SortAccountIdsByLastSelectedHook & {\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n getSelectedNetworkClientId: () => NetworkClientId;\n handleNonEvmRequestForOrigin: (params: {\n connectedAddresses: CaipAccountId[];\n scope: CaipChainId;\n request: JsonRpcRequest;\n }) => ReturnType<MultichainRoutingService['handleRequest']>;\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: Caip25Caveat | undefined;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ) as Caip25Caveat | undefined;\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.walletRevokeSession = void 0;
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");
@@ -33,7 +33,11 @@ function isPermissionError(error) {
33
33
  * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
34
34
  */
35
35
  function partialRevokePermissions(scopes, hooks) {
36
- let updatedCaveatValue = hooks.getCaveatForOrigin(chain_agnostic_permission_1.Caip25EndowmentPermissionName, chain_agnostic_permission_1.Caip25CaveatType).value;
36
+ const caveat = hooks.getCaveatForOrigin(chain_agnostic_permission_1.Caip25EndowmentPermissionName, chain_agnostic_permission_1.Caip25CaveatType);
37
+ if (!caveat) {
38
+ return;
39
+ }
40
+ let updatedCaveatValue = caveat.value;
37
41
  for (const scopeString of scopes) {
38
42
  const result = chain_agnostic_permission_1.Caip25CaveatMutators[chain_agnostic_permission_1.Caip25CaveatType].removeScope(updatedCaveatValue, scopeString);
39
43
  // If operation is a Noop, it means a scope was passed that was not present in the permission, so we proceed with the loop
@@ -73,7 +77,7 @@ function partialRevokePermissions(scopes, hooks) {
73
77
  * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
74
78
  * @returns Nothing.
75
79
  */
76
- async function walletRevokeSessionHandler(request, response, _next, end, hooks) {
80
+ async function handleWalletRevokeSession(request, response, _next, end, hooks) {
77
81
  try {
78
82
  if (request.params?.scopes?.length) {
79
83
  partialRevokePermissions(request.params.scopes, hooks);
@@ -91,9 +95,8 @@ async function walletRevokeSessionHandler(request, response, _next, end, hooks)
91
95
  response.result = true;
92
96
  return end();
93
97
  }
94
- exports.walletRevokeSession = {
95
- methodNames: ['wallet_revokeSession'],
96
- implementation: walletRevokeSessionHandler,
98
+ exports.walletRevokeSessionHandler = {
99
+ implementation: handleWalletRevokeSession,
97
100
  hookNames: {
98
101
  revokePermissionForOrigin: true,
99
102
  updateCaveat: true,
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-revokeSession.cjs","sourceRoot":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AA6B,CAAC,CAAC;IACjE,CAAC;SAAM,CAAC;QACN,KAAK,CAAC,YAAY,CAChB,yDAA6B,EAC7B,4CAAgB,EAChB,kBAAkB,CACnB,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,KAAK,UAAU,0BAA0B,CACvC,OAGC,EACD,QAAwB,EACxB,KAAgC,EAChC,GAA6B,EAC7B,KAA+B;IAE/B,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;YACnC,wBAAwB,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,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;AACY,QAAA,mBAAmB,GAAG;IACjC,WAAW,EAAE,CAAC,sBAAsB,CAAC;IACrC,cAAc,EAAE,0BAA0B;IAC1C,SAAS,EAAE;QACT,yBAAyB,EAAE,IAAI;QAC/B,YAAY,EAAE,IAAI;QAClB,kBAAkB,EAAE,IAAI;KACzB;CACF,CAAC","sourcesContent":["import {\n Caip25CaveatMutators,\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getCaipAccountIdsFromCaip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n} from '@metamask/json-rpc-engine';\nimport {\n CaveatMutatorOperation,\n PermissionDoesNotExistError,\n UnrecognizedSubjectError,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isObject } from '@metamask/utils';\nimport type { JsonRpcSuccess, JsonRpcRequest } from '@metamask/utils';\n\nimport type { WalletRevokeSessionHooks } from './types';\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 walletRevokeSessionHandler(\n request: JsonRpcRequest & {\n origin: string;\n params: { scopes?: string[] };\n },\n response: JsonRpcSuccess,\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}\nexport const walletRevokeSession = {\n methodNames: ['wallet_revokeSession'],\n implementation: walletRevokeSessionHandler,\n hookNames: {\n revokePermissionForOrigin: true,\n updateCaveat: true,\n getCaveatForOrigin: true,\n },\n};\n"]}
1
+ {"version":3,"file":"wallet-revokeSession.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":";;;AAAA,mFAK6C;AAO7C,2EAKyC;AACzC,qDAAiD;AACjD,2CAA2C;AAsB3C;;;;;GAKG;AACH,SAAS,iBAAiB,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K,CAAC,YAAY,CAChB,yDAA6B,EAC7B,4CAAgB,EAChB,kBAAkB,CACnB,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,KAAK,UAAU,yBAAyB,CACtC,OAAuE,EACvE,QAAsC,EACtC,KAAgC,EAChC,GAA6B,EAC7B,KAA+B;IAE/B,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;YACnC,wBAAwB,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,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 GenericPermissionController,\n PermissionDoesNotExistError,\n UnrecognizedSubjectError,\n} 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\nimport type { Caip25Caveat, GetCaveatForOriginHook } from './types';\n\nexport type WalletRevokeSessionHooks = GetCaveatForOriginHook & {\n revokePermissionForOrigin: (\n permissionName: string,\n ) => ReturnType<GenericPermissionController['revokePermissions']>;\n updateCaveat: (\n target: string,\n caveatType: string,\n caveatValue: Caip25CaveatValue,\n ) => ReturnType<GenericPermissionController['updateCaveat']>;\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 const caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ) as Caip25Caveat | undefined;\n if (!caveat) {\n return;\n }\n\n let updatedCaveatValue = caveat.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,15 @@
1
- import type { JsonRpcEngineNextCallback, JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
2
- import type { JsonRpcSuccess, JsonRpcRequest } from "@metamask/utils";
3
- import type { WalletRevokeSessionHooks } from "./types.cjs";
1
+ import type { Caip25CaveatValue } from "@metamask/chain-agnostic-permission";
2
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from "@metamask/json-rpc-engine";
3
+ import { GenericPermissionController } from "@metamask/permission-controller";
4
+ import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
5
+ import type { GetCaveatForOriginHook } from "./types.cjs";
6
+ export type WalletRevokeSessionHooks = GetCaveatForOriginHook & {
7
+ revokePermissionForOrigin: (permissionName: string) => ReturnType<GenericPermissionController['revokePermissions']>;
8
+ updateCaveat: (target: string, caveatType: string, caveatValue: Caip25CaveatValue) => ReturnType<GenericPermissionController['updateCaveat']>;
9
+ };
10
+ type WalletRevokeSessionParams = {
11
+ scopes?: string[];
12
+ };
4
13
  /**
5
14
  * Handler for the `wallet_revokeSession` RPC method as specified by [CAIP-285](https://chainagnostic.org/CAIPs/caip-285).
6
15
  * The implementation below deviates from the linked spec in that it ignores the `sessionId` param
@@ -18,19 +27,18 @@ import type { WalletRevokeSessionHooks } from "./types.cjs";
18
27
  * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
19
28
  * @returns Nothing.
20
29
  */
21
- declare function walletRevokeSessionHandler(request: JsonRpcRequest & {
30
+ declare function handleWalletRevokeSession(request: JsonRpcRequest<WalletRevokeSessionParams> & {
22
31
  origin: string;
23
- params: {
24
- scopes?: string[];
25
- };
26
- }, response: JsonRpcSuccess, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletRevokeSessionHooks): Promise<void>;
27
- export declare const walletRevokeSession: {
28
- methodNames: string[];
29
- implementation: typeof walletRevokeSessionHandler;
32
+ }, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletRevokeSessionHooks): Promise<void>;
33
+ export type WalletRevokeSessionHandler = MethodHandler<WalletRevokeSessionHooks, never, WalletRevokeSessionParams, Json, {
34
+ origin: string;
35
+ }>;
36
+ export declare const walletRevokeSessionHandler: {
37
+ implementation: typeof handleWalletRevokeSession;
30
38
  hookNames: {
31
- revokePermissionForOrigin: boolean;
32
- updateCaveat: boolean;
33
- getCaveatForOrigin: boolean;
39
+ revokePermissionForOrigin: true;
40
+ updateCaveat: true;
41
+ getCaveatForOrigin: true;
34
42
  };
35
43
  };
36
44
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-revokeSession.d.cts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,yBAAyB,EACzB,wBAAwB,EACzB,kCAAkC;AAQnC,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,wBAAwB;AAEtE,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAgB;AA4ExD;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,0BAA0B,CACvC,OAAO,EAAE,cAAc,GAAG;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;CAC/B,EACD,QAAQ,EAAE,cAAc,EACxB,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,wBAAwB,iBAiBhC;AACD,eAAO,MAAM,mBAAmB;;;;;;;;CAQ/B,CAAC"}
1
+ {"version":3,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+1,15 @@
1
- import type { JsonRpcEngineNextCallback, JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
2
- import type { JsonRpcSuccess, JsonRpcRequest } from "@metamask/utils";
3
- import type { WalletRevokeSessionHooks } from "./types.mjs";
1
+ import type { Caip25CaveatValue } from "@metamask/chain-agnostic-permission";
2
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback, MethodHandler } from "@metamask/json-rpc-engine";
3
+ import { GenericPermissionController } from "@metamask/permission-controller";
4
+ import type { Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
5
+ import type { GetCaveatForOriginHook } from "./types.mjs";
6
+ export type WalletRevokeSessionHooks = GetCaveatForOriginHook & {
7
+ revokePermissionForOrigin: (permissionName: string) => ReturnType<GenericPermissionController['revokePermissions']>;
8
+ updateCaveat: (target: string, caveatType: string, caveatValue: Caip25CaveatValue) => ReturnType<GenericPermissionController['updateCaveat']>;
9
+ };
10
+ type WalletRevokeSessionParams = {
11
+ scopes?: string[];
12
+ };
4
13
  /**
5
14
  * Handler for the `wallet_revokeSession` RPC method as specified by [CAIP-285](https://chainagnostic.org/CAIPs/caip-285).
6
15
  * The implementation below deviates from the linked spec in that it ignores the `sessionId` param
@@ -18,19 +27,18 @@ import type { WalletRevokeSessionHooks } from "./types.mjs";
18
27
  * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
19
28
  * @returns Nothing.
20
29
  */
21
- declare function walletRevokeSessionHandler(request: JsonRpcRequest & {
30
+ declare function handleWalletRevokeSession(request: JsonRpcRequest<WalletRevokeSessionParams> & {
22
31
  origin: string;
23
- params: {
24
- scopes?: string[];
25
- };
26
- }, response: JsonRpcSuccess, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletRevokeSessionHooks): Promise<void>;
27
- export declare const walletRevokeSession: {
28
- methodNames: string[];
29
- implementation: typeof walletRevokeSessionHandler;
32
+ }, response: PendingJsonRpcResponse<Json>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletRevokeSessionHooks): Promise<void>;
33
+ export type WalletRevokeSessionHandler = MethodHandler<WalletRevokeSessionHooks, never, WalletRevokeSessionParams, Json, {
34
+ origin: string;
35
+ }>;
36
+ export declare const walletRevokeSessionHandler: {
37
+ implementation: typeof handleWalletRevokeSession;
30
38
  hookNames: {
31
- revokePermissionForOrigin: boolean;
32
- updateCaveat: boolean;
33
- getCaveatForOrigin: boolean;
39
+ revokePermissionForOrigin: true;
40
+ updateCaveat: true;
41
+ getCaveatForOrigin: true;
34
42
  };
35
43
  };
36
44
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-revokeSession.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,yBAAyB,EACzB,wBAAwB,EACzB,kCAAkC;AAQnC,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,wBAAwB;AAEtE,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAgB;AA4ExD;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAe,0BAA0B,CACvC,OAAO,EAAE,cAAc,GAAG;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;CAC/B,EACD,QAAQ,EAAE,cAAc,EACxB,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,wBAAwB,iBAiBhC;AACD,eAAO,MAAM,mBAAmB;;;;;;;;CAQ/B,CAAC"}
1
+ {"version":3,"file":"wallet-revokeSession.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-revokeSession.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,4CAA4C;AAC7E,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACd,kCAAkC;AACnC,OAAO,EAEL,2BAA2B,EAG5B,wCAAwC;AAGzC,OAAO,KAAK,EACV,IAAI,EACJ,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB,OAAO,KAAK,EAAgB,sBAAsB,EAAE,oBAAgB;AAEpE,MAAM,MAAM,wBAAwB,GAAG,sBAAsB,GAAG;IAC9D,yBAAyB,EAAE,CACzB,cAAc,EAAE,MAAM,KACnB,UAAU,CAAC,2BAA2B,CAAC,mBAAmB,CAAC,CAAC,CAAC;IAClE,YAAY,EAAE,CACZ,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,iBAAiB,KAC3B,UAAU,CAAC,2BAA2B,CAAC,cAAc,CAAC,CAAC,CAAC;CAC9D,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAiFvD;;;;;;;;;;;;;;;;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"}
@@ -30,7 +30,11 @@ function isPermissionError(error) {
30
30
  * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
31
31
  */
32
32
  function partialRevokePermissions(scopes, hooks) {
33
- let updatedCaveatValue = hooks.getCaveatForOrigin(Caip25EndowmentPermissionName, Caip25CaveatType).value;
33
+ const caveat = hooks.getCaveatForOrigin(Caip25EndowmentPermissionName, Caip25CaveatType);
34
+ if (!caveat) {
35
+ return;
36
+ }
37
+ let updatedCaveatValue = caveat.value;
34
38
  for (const scopeString of scopes) {
35
39
  const result = Caip25CaveatMutators[Caip25CaveatType].removeScope(updatedCaveatValue, scopeString);
36
40
  // If operation is a Noop, it means a scope was passed that was not present in the permission, so we proceed with the loop
@@ -70,7 +74,7 @@ function partialRevokePermissions(scopes, hooks) {
70
74
  * @param hooks.getCaveatForOrigin - The hook to fetch an existing caveat for the origin of the request.
71
75
  * @returns Nothing.
72
76
  */
73
- async function walletRevokeSessionHandler(request, response, _next, end, hooks) {
77
+ async function handleWalletRevokeSession(request, response, _next, end, hooks) {
74
78
  try {
75
79
  if (request.params?.scopes?.length) {
76
80
  partialRevokePermissions(request.params.scopes, hooks);
@@ -88,9 +92,8 @@ async function walletRevokeSessionHandler(request, response, _next, end, hooks)
88
92
  response.result = true;
89
93
  return end();
90
94
  }
91
- export const walletRevokeSession = {
92
- methodNames: ['wallet_revokeSession'],
93
- implementation: walletRevokeSessionHandler,
95
+ export const walletRevokeSessionHandler = {
96
+ implementation: handleWalletRevokeSession,
94
97
  hookNames: {
95
98
  revokePermissionForOrigin: true,
96
99
  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;AAK7C,OAAO,EACL,sBAAsB,EACtB,2BAA2B,EAC3B,wBAAwB,EACzB,wCAAwC;AACzC,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAK3C;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,KAAc;IACvC,IACE,CAAC,QAAQ,CAAC,KAAK,CAAC;QAChB,CAAC,CAAC,MAAM,IAAI,KAAK,CAA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C7B,KAA+B;IAE/B,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;YACnC,wBAAwB,CAAC,OAAO,CAAC,MAAM,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;AACD,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,WAAW,EAAE,CAAC,sBAAsB,CAAC;IACrC,cAAc,EAAE,0BAA0B;IAC1C,SAAS,EAAE;QACT,yBAAyB,EAAE,IAAI;QAC/B,YAAY,EAAE,IAAI;QAClB,kBAAkB,EAAE,IAAI;KACzB;CACF,CAAC","sourcesContent":["import {\n Caip25CaveatMutators,\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getCaipAccountIdsFromCaip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n} from '@metamask/json-rpc-engine';\nimport {\n CaveatMutatorOperation,\n PermissionDoesNotExistError,\n UnrecognizedSubjectError,\n} from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isObject } from '@metamask/utils';\nimport type { JsonRpcSuccess, JsonRpcRequest } from '@metamask/utils';\n\nimport type { WalletRevokeSessionHooks } from './types';\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 walletRevokeSessionHandler(\n request: JsonRpcRequest & {\n origin: string;\n params: { scopes?: string[] };\n },\n response: JsonRpcSuccess,\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}\nexport const walletRevokeSession = {\n methodNames: ['wallet_revokeSession'],\n implementation: walletRevokeSessionHandler,\n hookNames: {\n revokePermissionForOrigin: true,\n updateCaveat: true,\n getCaveatForOrigin: true,\n },\n};\n"]}
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,EAEtB,2BAA2B,EAC3B,wBAAwB,EACzB,wCAAwC;AACzC,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,CA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;AACH,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,KAAK,UAAU,yBAAyB,CACtC,OAAuE,EACvE,QAAsC,EACtC,KAAgC,EAChC,GAA6B,EAC7B,KAA+B;IAE/B,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;YACnC,wBAAwB,CAAC,OAAO,CAAC,MAAM,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 GenericPermissionController,\n PermissionDoesNotExistError,\n UnrecognizedSubjectError,\n} 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\nimport type { Caip25Caveat, GetCaveatForOriginHook } from './types';\n\nexport type WalletRevokeSessionHooks = GetCaveatForOriginHook & {\n revokePermissionForOrigin: (\n permissionName: string,\n ) => ReturnType<GenericPermissionController['revokePermissions']>;\n updateCaveat: (\n target: string,\n caveatType: string,\n caveatValue: Caip25CaveatValue,\n ) => ReturnType<GenericPermissionController['updateCaveat']>;\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 const caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n ) as Caip25Caveat | undefined;\n if (!caveat) {\n return;\n }\n\n let updatedCaveatValue = caveat.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.walletRevokeSession = exports.walletInvokeMethod = exports.walletGetSession = exports.walletCreateSession = void 0;
4
- var wallet_createSession_1 = require("./handlers/wallet-createSession.cjs");
5
- Object.defineProperty(exports, "walletCreateSession", { enumerable: true, get: function () { return wallet_createSession_1.walletCreateSession; } });
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");
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4EAAsE;AAA7D,2HAAA,mBAAmB,OAAA;AAC5B,sEAAgE;AAAvD,qHAAA,gBAAgB,OAAA;AACzB,0EAAoE;AAA3D,yHAAA,kBAAkB,OAAA;AAC3B,4EAAsE;AAA7D,2HAAA,mBAAmB,OAAA;AAE5B,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 { walletCreateSession } from './handlers/wallet-createSession';\nexport { walletGetSession } from './handlers/wallet-getSession';\nexport { walletInvokeMethod } from './handlers/wallet-invokeMethod';\nexport { walletRevokeSession } from './handlers/wallet-revokeSession';\n\nexport { multichainMethodCallValidatorMiddleware } from './middlewares/multichainMethodCallValidatorMiddleware';\nexport { MultichainMiddlewareManager } from './middlewares/MultichainMiddlewareManager';\nexport { MultichainSubscriptionManager } from './middlewares/MultichainSubscriptionManager';\nexport { MultichainApiNotifications } from './handlers/types';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAA4C;AAAnC,0GAAA,cAAc,OAAA;AAGvB,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';\nexport type { MethodHandlerHooks } 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,5 @@
1
- export { walletCreateSession } from "./handlers/wallet-createSession.cjs";
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";
2
+ export type { MethodHandlerHooks } from "./handlers/index.cjs";
5
3
  export { multichainMethodCallValidatorMiddleware } from "./middlewares/multichainMethodCallValidatorMiddleware.cjs";
6
4
  export { MultichainMiddlewareManager } from "./middlewares/MultichainMiddlewareManager.cjs";
7
5
  export { MultichainSubscriptionManager } from "./middlewares/MultichainSubscriptionManager.cjs";