@metamask-previews/multichain-api-middleware 2.0.0-preview-45a82ea8e → 2.0.0-preview-d8ff44d

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 (50) hide show
  1. package/CHANGELOG.md +1 -4
  2. package/dist/handlers/wallet-createSession.cjs +4 -6
  3. package/dist/handlers/wallet-createSession.cjs.map +1 -1
  4. package/dist/handlers/wallet-createSession.d.cts +27 -44
  5. package/dist/handlers/wallet-createSession.d.cts.map +1 -1
  6. package/dist/handlers/wallet-createSession.d.mts +27 -44
  7. package/dist/handlers/wallet-createSession.d.mts.map +1 -1
  8. package/dist/handlers/wallet-createSession.mjs +3 -5
  9. package/dist/handlers/wallet-createSession.mjs.map +1 -1
  10. package/dist/handlers/wallet-getSession.cjs +4 -6
  11. package/dist/handlers/wallet-getSession.cjs.map +1 -1
  12. package/dist/handlers/wallet-getSession.d.cts +15 -26
  13. package/dist/handlers/wallet-getSession.d.cts.map +1 -1
  14. package/dist/handlers/wallet-getSession.d.mts +15 -26
  15. package/dist/handlers/wallet-getSession.d.mts.map +1 -1
  16. package/dist/handlers/wallet-getSession.mjs +3 -5
  17. package/dist/handlers/wallet-getSession.mjs.map +1 -1
  18. package/dist/handlers/wallet-invokeMethod.cjs +4 -9
  19. package/dist/handlers/wallet-invokeMethod.cjs.map +1 -1
  20. package/dist/handlers/wallet-invokeMethod.d.cts +25 -39
  21. package/dist/handlers/wallet-invokeMethod.d.cts.map +1 -1
  22. package/dist/handlers/wallet-invokeMethod.d.mts +25 -39
  23. package/dist/handlers/wallet-invokeMethod.d.mts.map +1 -1
  24. package/dist/handlers/wallet-invokeMethod.mjs +4 -9
  25. package/dist/handlers/wallet-invokeMethod.mjs.map +1 -1
  26. package/dist/handlers/wallet-revokeSession.cjs +4 -6
  27. package/dist/handlers/wallet-revokeSession.cjs.map +1 -1
  28. package/dist/handlers/wallet-revokeSession.d.cts +12 -21
  29. package/dist/handlers/wallet-revokeSession.d.cts.map +1 -1
  30. package/dist/handlers/wallet-revokeSession.d.mts +12 -21
  31. package/dist/handlers/wallet-revokeSession.d.mts.map +1 -1
  32. package/dist/handlers/wallet-revokeSession.mjs +3 -5
  33. package/dist/handlers/wallet-revokeSession.mjs.map +1 -1
  34. package/dist/index.cjs +9 -3
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.cts +4 -1
  37. package/dist/index.d.cts.map +1 -1
  38. package/dist/index.d.mts +4 -1
  39. package/dist/index.d.mts.map +1 -1
  40. package/dist/index.mjs +4 -1
  41. package/dist/index.mjs.map +1 -1
  42. package/package.json +2 -2
  43. package/dist/handlers/index.cjs +0 -14
  44. package/dist/handlers/index.cjs.map +0 -1
  45. package/dist/handlers/index.d.cts +0 -8
  46. package/dist/handlers/index.d.cts.map +0 -1
  47. package/dist/handlers/index.d.mts +0 -8
  48. package/dist/handlers/index.d.mts.map +0 -1
  49. package/dist/handlers/index.mjs +0 -11
  50. package/dist/handlers/index.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -9,11 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Changed
11
11
 
12
- - **BREAKING:** Consolidate method handlers into a single `methodHandlers` export ([#8583](https://github.com/MetaMask/core/pull/8583))
13
- - The individual handler exports have been removed. They can still be accessed as properties on the `methodHandlers` export.
14
- - The new handlers follow the format expected by `createMethodMiddleware` from `@metamask/json-rpc-engine@10.3.0`.
15
12
  - Bump `@metamask/json-rpc-engine` from `^10.2.3` to `^10.2.4` ([#8317](https://github.com/MetaMask/core/pull/8317))
16
- - Bump `@metamask/network-controller` from `^30.0.0` to `^30.0.1` ([#8317](https://github.com/MetaMask/core/pull/8317))
13
+ - Bump `@metamask/network-controller` from `^30.0.0` to `^30.1.0` ([#8317](https://github.com/MetaMask/core/pull/8317), [#8636](https://github.com/MetaMask/core/pull/8636))
17
14
  - Bump `@metamask/permission-controller` from `^12.2.1` to `^12.3.0` ([#8317](https://github.com/MetaMask/core/pull/8317))
18
15
  - Bump `@metamask/multichain-transactions-controller` from `^7.0.3` to `^7.0.4` ([#8325](https://github.com/MetaMask/core/pull/8325))
19
16
  - Bump `@metamask/controller-utils` from `^11.19.0` to `^11.20.0` ([#8344](https://github.com/MetaMask/core/pull/8344))
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.walletCreateSessionHandler = exports.walletCreateSession = void 0;
3
+ exports.walletCreateSession = void 0;
4
4
  const chain_agnostic_permission_1 = require("@metamask/chain-agnostic-permission");
5
5
  const controller_utils_1 = require("@metamask/controller-utils");
6
6
  const permission_controller_1 = require("@metamask/permission-controller");
@@ -33,7 +33,7 @@ const SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';
33
33
  * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.
34
34
  * @returns A promise with wallet_createSession handler
35
35
  */
36
- async function handleWalletCreateSession(req, res, _next, end, hooks) {
36
+ async function walletCreateSessionHandler(req, res, _next, end, hooks) {
37
37
  if (!(0, utils_1.isPlainObject)(req.params)) {
38
38
  return end((0, permission_controller_1.invalidParams)({ data: { request: req } }));
39
39
  }
@@ -160,7 +160,8 @@ async function handleWalletCreateSession(req, res, _next, end, hooks) {
160
160
  }
161
161
  }
162
162
  exports.walletCreateSession = {
163
- implementation: handleWalletCreateSession,
163
+ methodNames: ['wallet_createSession'],
164
+ implementation: walletCreateSessionHandler,
164
165
  hookNames: {
165
166
  findNetworkClientIdByChainId: true,
166
167
  listAccounts: true,
@@ -172,7 +173,4 @@ exports.walletCreateSession = {
172
173
  trackSessionCreatedEvent: true,
173
174
  },
174
175
  };
175
- exports.walletCreateSessionHandler = {
176
- wallet_createSession: exports.walletCreateSession,
177
- };
178
176
  //# sourceMappingURL=wallet-createSession.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-createSession.cjs","sourceRo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C,4BAA4B;SACjE,CAAC,CAAC;QAEH,MAAM,EAAE,iBAAiB,EAAE,yBAAyB,GAAG,EAAE,EAAE,GACzD,yBAAyB,CAAC;QAE5B,KAAK,CAAC,wBAAwB,EAAE,CAAC,yBAAyB,CAAC,CAAC;QAE5D,GAAG,CAAC,MAAM,GAAG;YACX,aAAa;YACb,iBAAiB,EAAE,yBAAyB;SAC7C,CAAC;QACF,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAUY,QAAA,mBAAmB,GAAG;IACjC,cAAc,EAAE,yBAAyB;IACzC,SAAS,EAAE;QACT,4BAA4B,EAAE,IAAI;QAClC,YAAY,EAAE,IAAI;QAClB,2BAA2B,EAAE,IAAI;QACjC,yBAAyB,EAAE,IAAI;QAC/B,sBAAsB,EAAE,IAAI;QAC5B,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,wBAAwB,EAAE,IAAI;KAC/B;CACyC,CAAC;AAEhC,QAAA,0BAA0B,GAAG;IACxC,oBAAoB,EAAE,2BAAmB;CAC1C,CAAC","sourcesContent":["import {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n bucketScopes,\n validateAndNormalizeScopes,\n getInternalScopesObject,\n getSessionScopes,\n getSupportedScopeObjects,\n isKnownSessionPropertyValue,\n getCaipAccountIdsFromScopesObjects,\n getAllScopesFromScopesObjects,\n setNonSCACaipAccountIdsInCaip25CaveatValue,\n isNamespaceInScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n Caip25Authorization,\n NormalizedScopesObject,\n Caip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type {\n MethodHandler,\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type { NetworkController } from '@metamask/network-controller';\nimport { invalidParams } from '@metamask/permission-controller';\nimport type { RequestedPermissions } from '@metamask/permission-controller';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport {\n isPlainObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { GrantedPermissions } from './types';\n\nconst SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';\n\ntype WalletCreateSessionHooks = {\n listAccounts: () => { address: string }[];\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n requestPermissionsForOrigin: (\n requestedPermissions: RequestedPermissions,\n metadata?: Record<string, Json>,\n ) => Promise<[GrantedPermissions]>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];\n sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];\n trackSessionCreatedEvent?: (\n approvedCaip25CaveatValue: Caip25CaveatValue,\n ) => void;\n};\n\ntype Params = Caip25Authorization;\n\ntype Result = {\n sessionScopes: NormalizedScopesObject;\n sessionProperties?: Record<string, Json>;\n};\n\n/**\n * Handler for the `wallet_createSession` RPC method which is responsible\n * for prompting for approval and granting a CAIP-25 permission.\n *\n * This implementation primarily deviates from the CAIP-25 handler\n * specification by treating all scopes as optional regardless of\n * if they were specified in `requiredScopes` or `optionalScopes`.\n * Additionally, provided scopes, methods, notifications, and\n * account values that are invalid/malformed are ignored rather than\n * causing an error to be returned.\n *\n * @param req - The request object.\n * @param res - The response object.\n * @param _next - The next middleware function.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.listAccounts - The hook that returns an array of the wallet's evm accounts.\n * @param hooks.findNetworkClientIdByChainId - The hook that returns the networkClientId for a chainId.\n * @param hooks.requestPermissionsForOrigin - The hook that approves and grants requested permissions.\n * @param hooks.getNonEvmSupportedMethods - The hook that returns the supported methods for a non EVM scope.\n * @param hooks.isNonEvmScopeSupported - The hook that returns true if a non EVM scope is supported.\n * @param hooks.getNonEvmAccountAddresses - The hook that returns a list of CaipAccountIds that are supported for a CaipChainId.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by last selected.\n * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.\n * @returns A promise with wallet_createSession handler\n */\nasync function handleWalletCreateSession(\n req: JsonRpcRequest<Params> & { origin: string },\n res: PendingJsonRpcResponse<Result>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletCreateSessionHooks,\n): Promise<void> {\n if (!isPlainObject(req.params)) {\n return end(invalidParams({ data: { request: req } }));\n }\n const { requiredScopes, optionalScopes, sessionProperties } = req.params;\n\n if (sessionProperties && Object.keys(sessionProperties).length === 0) {\n return end(new JsonRpcError(5302, 'Invalid sessionProperties requested'));\n }\n\n const filteredSessionProperties = Object.fromEntries(\n Object.entries(sessionProperties ?? {}).filter(([key]) =>\n isKnownSessionPropertyValue(key),\n ),\n );\n\n try {\n const { normalizedRequiredScopes, normalizedOptionalScopes } =\n validateAndNormalizeScopes(requiredScopes || {}, optionalScopes || {});\n\n const requiredScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedRequiredScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n const optionalScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedOptionalScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n\n const networkClientExistsForChainId = (chainId: Hex) => {\n try {\n hooks.findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n // if solana is a requested scope but not supported, we add a promptToCreateSolanaAccount flag to request\n const isSolanaRequested =\n isNamespaceInScopesObject(\n requiredScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n ) ||\n isNamespaceInScopesObject(\n optionalScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n );\n\n let promptToCreateSolanaAccount = false;\n if (isSolanaRequested) {\n const supportedSolanaAccounts =\n hooks.getNonEvmAccountAddresses(SOLANA_CAIP_CHAIN_ID);\n promptToCreateSolanaAccount = supportedSolanaAccounts.length === 0;\n }\n\n const { supportedScopes: supportedRequiredScopes } = bucketScopes(\n requiredScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const { supportedScopes: supportedOptionalScopes } = bucketScopes(\n optionalScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const allRequestedAccountAddresses = getCaipAccountIdsFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const allSupportedRequestedCaipChainIds = getAllScopesFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const existingEvmAddresses = hooks\n .listAccounts()\n .map((account) => account.address);\n\n const supportedRequestedAccountAddresses =\n allRequestedAccountAddresses.filter(\n (requestedAccountAddress: CaipAccountId) => {\n const {\n address,\n chain: { namespace },\n chainId: caipChainId,\n } = parseCaipAccountId(requestedAccountAddress);\n if (namespace === KnownCaipNamespace.Eip155.toString()) {\n return existingEvmAddresses.some((existingEvmAddress) => {\n return isEqualCaseInsensitive(address, existingEvmAddress);\n });\n }\n\n // If the namespace is not eip155 (EVM) we do a case sensitive check\n return hooks\n .getNonEvmAccountAddresses(caipChainId)\n .some((existingCaipAddress) => {\n return requestedAccountAddress === existingCaipAddress;\n });\n },\n );\n\n const requestedCaip25CaveatValue = {\n requiredScopes: getInternalScopesObject(supportedRequiredScopes),\n optionalScopes: getInternalScopesObject(supportedOptionalScopes),\n isMultichainOrigin: true,\n sessionProperties: filteredSessionProperties,\n };\n\n const requestedCaip25CaveatValueWithSupportedAccounts =\n setNonSCACaipAccountIdsInCaip25CaveatValue(\n requestedCaip25CaveatValue,\n supportedRequestedAccountAddresses,\n );\n\n // if `promptToCreateSolanaAccount` is true and there are no other valid scopes requested,\n // we add a `wallet` scope to the request in order to get passed the CAIP-25 caveat validator.\n // This is very hacky but is necessary because the solana opt-in flow breaks key assumptions\n // of the CAIP-25 permission specification - namely that we can have valid requests with no scopes.\n if (allSupportedRequestedCaipChainIds.length === 0) {\n if (promptToCreateSolanaAccount) {\n requestedCaip25CaveatValueWithSupportedAccounts.optionalScopes[\n KnownCaipNamespace.Wallet\n ] = {\n accounts: [],\n };\n } else {\n // if solana is not requested and there are no supported scopes, we return an error\n return end(\n new JsonRpcError(5100, 'Requested scopes are not supported'),\n );\n }\n }\n\n const [grantedPermissions] = await hooks.requestPermissionsForOrigin(\n {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: requestedCaip25CaveatValueWithSupportedAccounts,\n },\n ],\n },\n },\n {\n metadata: { promptToCreateSolanaAccount },\n },\n );\n\n const approvedCaip25Permission =\n grantedPermissions[Caip25EndowmentPermissionName];\n const approvedCaip25CaveatValue = approvedCaip25Permission?.caveats?.find(\n (caveat) => caveat.type === Caip25CaveatType,\n )?.value as Caip25CaveatValue;\n if (!approvedCaip25CaveatValue) {\n throw rpcErrors.internal();\n }\n\n const sessionScopes = getSessionScopes(approvedCaip25CaveatValue, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n });\n\n const { sessionProperties: approvedSessionProperties = {} } =\n approvedCaip25CaveatValue;\n\n hooks.trackSessionCreatedEvent?.(approvedCaip25CaveatValue);\n\n res.result = {\n sessionScopes,\n sessionProperties: approvedSessionProperties,\n };\n return end();\n } catch (err) {\n return end(err);\n }\n}\n\ntype WalletCreateSessionMethodHandler = MethodHandler<\n WalletCreateSessionHooks,\n never,\n Params,\n Result,\n { origin: string }\n>;\n\nexport const walletCreateSession = {\n implementation: handleWalletCreateSession,\n hookNames: {\n findNetworkClientIdByChainId: true,\n listAccounts: true,\n requestPermissionsForOrigin: true,\n getNonEvmSupportedMethods: true,\n isNonEvmScopeSupported: true,\n getNonEvmAccountAddresses: true,\n sortAccountIdsByLastSelected: true,\n trackSessionCreatedEvent: true,\n },\n} satisfies WalletCreateSessionMethodHandler;\n\nexport const walletCreateSessionHandler = {\n wallet_createSession: walletCreateSession,\n};\n"]}
1
+ {"version":3,"file":"wallet-createSession.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-createSession.ts"],"names":[],"mappings":";;;AAAA,mFAa6C;AAM7C,iEAAoE;AAMpE,2EAAgE;AAEhE,qDAA+D;AAC/D,2CAIyB;AAYzB,MAAM,oBAAoB,GAAG,yCAAyC,CAAC;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,KAAK,UAAU,0BAA0B,CACvC,GAA6D,EAC7D,GAGE,EACF,KAAgC,EAChC,GAA6B,EAC7B,KAgBC;IAED,IAAI,CAAC,IAAA,qBAAa,EAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QAC/B,OAAO,GAAG,CAAC,IAAA,qCAAa,EAAC,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;IACxD,CAAC;IACD,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,iBAAiB,EAAE,GAAG,GAAG,CAAC,MAA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EAAE,IAAI;QAC/B,sBAAsB,EAAE,IAAI;QAC5B,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,wBAAwB,EAAE,IAAI;KAC/B;CACF,CAAC","sourcesContent":["import {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n bucketScopes,\n validateAndNormalizeScopes,\n getInternalScopesObject,\n getSessionScopes,\n getSupportedScopeObjects,\n isKnownSessionPropertyValue,\n getCaipAccountIdsFromScopesObjects,\n getAllScopesFromScopesObjects,\n setNonSCACaipAccountIdsInCaip25CaveatValue,\n isNamespaceInScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n Caip25Authorization,\n NormalizedScopesObject,\n Caip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type { NetworkController } from '@metamask/network-controller';\nimport { invalidParams } from '@metamask/permission-controller';\nimport type { RequestedPermissions } from '@metamask/permission-controller';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport {\n isPlainObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n JsonRpcSuccess,\n} from '@metamask/utils';\n\nimport type { GrantedPermissions } from './types';\n\nconst SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';\n\n/**\n * Handler for the `wallet_createSession` RPC method which is responsible\n * for prompting for approval and granting a CAIP-25 permission.\n *\n * This implementation primarily deviates from the CAIP-25 handler\n * specification by treating all scopes as optional regardless of\n * if they were specified in `requiredScopes` or `optionalScopes`.\n * Additionally, provided scopes, methods, notifications, and\n * account values that are invalid/malformed are ignored rather than\n * causing an error to be returned.\n *\n * @param req - The request object.\n * @param res - The response object.\n * @param _next - The next middleware function.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.listAccounts - The hook that returns an array of the wallet's evm accounts.\n * @param hooks.findNetworkClientIdByChainId - The hook that returns the networkClientId for a chainId.\n * @param hooks.requestPermissionsForOrigin - The hook that approves and grants requested permissions.\n * @param hooks.getNonEvmSupportedMethods - The hook that returns the supported methods for a non EVM scope.\n * @param hooks.isNonEvmScopeSupported - The hook that returns true if a non EVM scope is supported.\n * @param hooks.getNonEvmAccountAddresses - The hook that returns a list of CaipAccountIds that are supported for a CaipChainId.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by last selected.\n * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.\n * @returns A promise with wallet_createSession handler\n */\nasync function walletCreateSessionHandler(\n req: JsonRpcRequest<Caip25Authorization> & { origin: string },\n res: JsonRpcSuccess<{\n sessionScopes: NormalizedScopesObject;\n sessionProperties?: Record<string, Json>;\n }>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: {\n listAccounts: () => { address: string }[];\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n requestPermissionsForOrigin: (\n requestedPermissions: RequestedPermissions,\n metadata?: Record<string, Json>,\n ) => Promise<[GrantedPermissions]>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];\n sortAccountIdsByLastSelected: (\n accounts: CaipAccountId[],\n ) => CaipAccountId[];\n trackSessionCreatedEvent?: (\n approvedCaip25CaveatValue: Caip25CaveatValue,\n ) => void;\n },\n) {\n if (!isPlainObject(req.params)) {\n return end(invalidParams({ data: { request: req } }));\n }\n const { requiredScopes, optionalScopes, sessionProperties } = req.params;\n\n if (sessionProperties && Object.keys(sessionProperties).length === 0) {\n return end(new JsonRpcError(5302, 'Invalid sessionProperties requested'));\n }\n\n const filteredSessionProperties = Object.fromEntries(\n Object.entries(sessionProperties ?? {}).filter(([key]) =>\n isKnownSessionPropertyValue(key),\n ),\n );\n\n try {\n const { normalizedRequiredScopes, normalizedOptionalScopes } =\n validateAndNormalizeScopes(requiredScopes || {}, optionalScopes || {});\n\n const requiredScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedRequiredScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n const optionalScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedOptionalScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n\n const networkClientExistsForChainId = (chainId: Hex) => {\n try {\n hooks.findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n // if solana is a requested scope but not supported, we add a promptToCreateSolanaAccount flag to request\n const isSolanaRequested =\n isNamespaceInScopesObject(\n requiredScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n ) ||\n isNamespaceInScopesObject(\n optionalScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n );\n\n let promptToCreateSolanaAccount = false;\n if (isSolanaRequested) {\n const supportedSolanaAccounts =\n hooks.getNonEvmAccountAddresses(SOLANA_CAIP_CHAIN_ID);\n promptToCreateSolanaAccount = supportedSolanaAccounts.length === 0;\n }\n\n const { supportedScopes: supportedRequiredScopes } = bucketScopes(\n requiredScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const { supportedScopes: supportedOptionalScopes } = bucketScopes(\n optionalScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const allRequestedAccountAddresses = getCaipAccountIdsFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const allSupportedRequestedCaipChainIds = getAllScopesFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const existingEvmAddresses = hooks\n .listAccounts()\n .map((account) => account.address);\n\n const supportedRequestedAccountAddresses =\n allRequestedAccountAddresses.filter(\n (requestedAccountAddress: CaipAccountId) => {\n const {\n address,\n chain: { namespace },\n chainId: caipChainId,\n } = parseCaipAccountId(requestedAccountAddress);\n if (namespace === KnownCaipNamespace.Eip155.toString()) {\n return existingEvmAddresses.some((existingEvmAddress) => {\n return isEqualCaseInsensitive(address, existingEvmAddress);\n });\n }\n\n // If the namespace is not eip155 (EVM) we do a case sensitive check\n return hooks\n .getNonEvmAccountAddresses(caipChainId)\n .some((existingCaipAddress) => {\n return requestedAccountAddress === existingCaipAddress;\n });\n },\n );\n\n const requestedCaip25CaveatValue = {\n requiredScopes: getInternalScopesObject(supportedRequiredScopes),\n optionalScopes: getInternalScopesObject(supportedOptionalScopes),\n isMultichainOrigin: true,\n sessionProperties: filteredSessionProperties,\n };\n\n const requestedCaip25CaveatValueWithSupportedAccounts =\n setNonSCACaipAccountIdsInCaip25CaveatValue(\n requestedCaip25CaveatValue,\n supportedRequestedAccountAddresses,\n );\n\n // if `promptToCreateSolanaAccount` is true and there are no other valid scopes requested,\n // we add a `wallet` scope to the request in order to get passed the CAIP-25 caveat validator.\n // This is very hacky but is necessary because the solana opt-in flow breaks key assumptions\n // of the CAIP-25 permission specification - namely that we can have valid requests with no scopes.\n if (allSupportedRequestedCaipChainIds.length === 0) {\n if (promptToCreateSolanaAccount) {\n requestedCaip25CaveatValueWithSupportedAccounts.optionalScopes[\n KnownCaipNamespace.Wallet\n ] = {\n accounts: [],\n };\n } else {\n // if solana is not requested and there are no supported scopes, we return an error\n return end(\n new JsonRpcError(5100, 'Requested scopes are not supported'),\n );\n }\n }\n\n const [grantedPermissions] = await hooks.requestPermissionsForOrigin(\n {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: requestedCaip25CaveatValueWithSupportedAccounts,\n },\n ],\n },\n },\n {\n metadata: { promptToCreateSolanaAccount },\n },\n );\n\n const approvedCaip25Permission =\n grantedPermissions[Caip25EndowmentPermissionName];\n const approvedCaip25CaveatValue = approvedCaip25Permission?.caveats?.find(\n (caveat) => caveat.type === Caip25CaveatType,\n )?.value as Caip25CaveatValue;\n if (!approvedCaip25CaveatValue) {\n throw rpcErrors.internal();\n }\n\n const sessionScopes = getSessionScopes(approvedCaip25CaveatValue, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n });\n\n const { sessionProperties: approvedSessionProperties = {} } =\n approvedCaip25CaveatValue;\n\n hooks.trackSessionCreatedEvent?.(approvedCaip25CaveatValue);\n\n res.result = {\n sessionScopes,\n sessionProperties: approvedSessionProperties,\n };\n return end();\n } catch (err) {\n return end(err);\n }\n}\n\nexport const walletCreateSession = {\n methodNames: ['wallet_createSession'],\n implementation: walletCreateSessionHandler,\n hookNames: {\n findNetworkClientIdByChainId: true,\n listAccounts: true,\n requestPermissionsForOrigin: true,\n getNonEvmSupportedMethods: true,\n isNonEvmScopeSupported: true,\n getNonEvmAccountAddresses: true,\n sortAccountIdsByLastSelected: true,\n trackSessionCreatedEvent: true,\n },\n};\n"]}
@@ -2,25 +2,8 @@ import type { Caip25Authorization, NormalizedScopesObject, Caip25CaveatValue } f
2
2
  import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
3
3
  import type { NetworkController } from "@metamask/network-controller";
4
4
  import type { RequestedPermissions } from "@metamask/permission-controller";
5
- import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
5
+ import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, JsonRpcSuccess } from "@metamask/utils";
6
6
  import type { GrantedPermissions } from "./types.cjs";
7
- type WalletCreateSessionHooks = {
8
- listAccounts: () => {
9
- address: string;
10
- }[];
11
- findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];
12
- requestPermissionsForOrigin: (requestedPermissions: RequestedPermissions, metadata?: Record<string, Json>) => Promise<[GrantedPermissions]>;
13
- getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
14
- isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
15
- getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];
16
- sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
17
- trackSessionCreatedEvent?: (approvedCaip25CaveatValue: Caip25CaveatValue) => void;
18
- };
19
- type Params = Caip25Authorization;
20
- type Result = {
21
- sessionScopes: NormalizedScopesObject;
22
- sessionProperties?: Record<string, Json>;
23
- };
24
7
  /**
25
8
  * Handler for the `wallet_createSession` RPC method which is responsible
26
9
  * for prompting for approval and granting a CAIP-25 permission.
@@ -47,35 +30,35 @@ type Result = {
47
30
  * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.
48
31
  * @returns A promise with wallet_createSession handler
49
32
  */
50
- declare function handleWalletCreateSession(req: JsonRpcRequest<Params> & {
33
+ declare function walletCreateSessionHandler(req: JsonRpcRequest<Caip25Authorization> & {
51
34
  origin: string;
52
- }, res: PendingJsonRpcResponse<Result>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletCreateSessionHooks): Promise<void>;
35
+ }, res: JsonRpcSuccess<{
36
+ sessionScopes: NormalizedScopesObject;
37
+ sessionProperties?: Record<string, Json>;
38
+ }>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: {
39
+ listAccounts: () => {
40
+ address: string;
41
+ }[];
42
+ findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];
43
+ requestPermissionsForOrigin: (requestedPermissions: RequestedPermissions, metadata?: Record<string, Json>) => Promise<[GrantedPermissions]>;
44
+ getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
45
+ isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
46
+ getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];
47
+ sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
48
+ trackSessionCreatedEvent?: (approvedCaip25CaveatValue: Caip25CaveatValue) => void;
49
+ }): Promise<void>;
53
50
  export declare const walletCreateSession: {
54
- implementation: typeof handleWalletCreateSession;
51
+ methodNames: string[];
52
+ implementation: typeof walletCreateSessionHandler;
55
53
  hookNames: {
56
- findNetworkClientIdByChainId: true;
57
- listAccounts: true;
58
- requestPermissionsForOrigin: true;
59
- getNonEvmSupportedMethods: true;
60
- isNonEvmScopeSupported: true;
61
- getNonEvmAccountAddresses: true;
62
- sortAccountIdsByLastSelected: true;
63
- trackSessionCreatedEvent: true;
64
- };
65
- };
66
- export declare const walletCreateSessionHandler: {
67
- wallet_createSession: {
68
- implementation: typeof handleWalletCreateSession;
69
- hookNames: {
70
- findNetworkClientIdByChainId: true;
71
- listAccounts: true;
72
- requestPermissionsForOrigin: true;
73
- getNonEvmSupportedMethods: true;
74
- isNonEvmScopeSupported: true;
75
- getNonEvmAccountAddresses: true;
76
- sortAccountIdsByLastSelected: true;
77
- trackSessionCreatedEvent: true;
78
- };
54
+ findNetworkClientIdByChainId: boolean;
55
+ listAccounts: boolean;
56
+ requestPermissionsForOrigin: boolean;
57
+ getNonEvmSupportedMethods: boolean;
58
+ isNonEvmScopeSupported: boolean;
59
+ getNonEvmAccountAddresses: boolean;
60
+ sortAccountIdsByLastSelected: boolean;
61
+ trackSessionCreatedEvent: boolean;
79
62
  };
80
63
  };
81
64
  export {};
@@ -1 +1 @@
1
- {"version":3,"file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C;CAC1C,CAAC,EACF,KAAK,EAAE,yBAAyB,EAChC,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE;IACL,YAAY,EAAE,MAAM;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC1C,4BAA4B,EAAE,iBAAiB,CAAC,8BAA8B,CAAC,CAAC;IAChF,2BAA2B,EAAE,CAC3B,oBAAoB,EAAE,oBAAoB,EAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,KAC5B,OAAO,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC;IACnC,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;IAC5D,sBAAsB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,CAAC;IACxD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,aAAa,EAAE,CAAC;IACnE,4BAA4B,EAAE,CAC5B,QAAQ,EAAE,aAAa,EAAE,KACtB,aAAa,EAAE,CAAC;IACrB,wBAAwB,CAAC,EAAE,CACzB,yBAAyB,EAAE,iBAAiB,KACzC,IAAI,CAAC;CACX,iBA6LF;AAED,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;CAa/B,CAAC"}
@@ -2,25 +2,8 @@ import type { Caip25Authorization, NormalizedScopesObject, Caip25CaveatValue } f
2
2
  import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
3
3
  import type { NetworkController } from "@metamask/network-controller";
4
4
  import type { RequestedPermissions } from "@metamask/permission-controller";
5
- import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
5
+ import type { CaipAccountId, CaipChainId, Json, JsonRpcRequest, JsonRpcSuccess } from "@metamask/utils";
6
6
  import type { GrantedPermissions } from "./types.mjs";
7
- type WalletCreateSessionHooks = {
8
- listAccounts: () => {
9
- address: string;
10
- }[];
11
- findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];
12
- requestPermissionsForOrigin: (requestedPermissions: RequestedPermissions, metadata?: Record<string, Json>) => Promise<[GrantedPermissions]>;
13
- getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
14
- isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
15
- getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];
16
- sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
17
- trackSessionCreatedEvent?: (approvedCaip25CaveatValue: Caip25CaveatValue) => void;
18
- };
19
- type Params = Caip25Authorization;
20
- type Result = {
21
- sessionScopes: NormalizedScopesObject;
22
- sessionProperties?: Record<string, Json>;
23
- };
24
7
  /**
25
8
  * Handler for the `wallet_createSession` RPC method which is responsible
26
9
  * for prompting for approval and granting a CAIP-25 permission.
@@ -47,35 +30,35 @@ type Result = {
47
30
  * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.
48
31
  * @returns A promise with wallet_createSession handler
49
32
  */
50
- declare function handleWalletCreateSession(req: JsonRpcRequest<Params> & {
33
+ declare function walletCreateSessionHandler(req: JsonRpcRequest<Caip25Authorization> & {
51
34
  origin: string;
52
- }, res: PendingJsonRpcResponse<Result>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletCreateSessionHooks): Promise<void>;
35
+ }, res: JsonRpcSuccess<{
36
+ sessionScopes: NormalizedScopesObject;
37
+ sessionProperties?: Record<string, Json>;
38
+ }>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: {
39
+ listAccounts: () => {
40
+ address: string;
41
+ }[];
42
+ findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];
43
+ requestPermissionsForOrigin: (requestedPermissions: RequestedPermissions, metadata?: Record<string, Json>) => Promise<[GrantedPermissions]>;
44
+ getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
45
+ isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
46
+ getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];
47
+ sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
48
+ trackSessionCreatedEvent?: (approvedCaip25CaveatValue: Caip25CaveatValue) => void;
49
+ }): Promise<void>;
53
50
  export declare const walletCreateSession: {
54
- implementation: typeof handleWalletCreateSession;
51
+ methodNames: string[];
52
+ implementation: typeof walletCreateSessionHandler;
55
53
  hookNames: {
56
- findNetworkClientIdByChainId: true;
57
- listAccounts: true;
58
- requestPermissionsForOrigin: true;
59
- getNonEvmSupportedMethods: true;
60
- isNonEvmScopeSupported: true;
61
- getNonEvmAccountAddresses: true;
62
- sortAccountIdsByLastSelected: true;
63
- trackSessionCreatedEvent: true;
64
- };
65
- };
66
- export declare const walletCreateSessionHandler: {
67
- wallet_createSession: {
68
- implementation: typeof handleWalletCreateSession;
69
- hookNames: {
70
- findNetworkClientIdByChainId: true;
71
- listAccounts: true;
72
- requestPermissionsForOrigin: true;
73
- getNonEvmSupportedMethods: true;
74
- isNonEvmScopeSupported: true;
75
- getNonEvmAccountAddresses: true;
76
- sortAccountIdsByLastSelected: true;
77
- trackSessionCreatedEvent: true;
78
- };
54
+ findNetworkClientIdByChainId: boolean;
55
+ listAccounts: boolean;
56
+ requestPermissionsForOrigin: boolean;
57
+ getNonEvmSupportedMethods: boolean;
58
+ isNonEvmScopeSupported: boolean;
59
+ getNonEvmAccountAddresses: boolean;
60
+ sortAccountIdsByLastSelected: boolean;
61
+ trackSessionCreatedEvent: boolean;
79
62
  };
80
63
  };
81
64
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-createSession.d.mts","sourceRoot":"","sources":["../../src/handlers/wallet-createSession.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EACV,mBAAmB,EACnB,sBAAsB,EACtB,iBAAiB,EAClB,4CAA4C;AAE7C,OAAO,KAAK,EAEV,wBAAwB,EACxB,yBAAyB,EAC1B,kCAAkC;AACn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;IAChF,2BAA2B,EAAE,CAC3B,oBAAoB,EAAE,oBAAoB,EAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,KAC5B,OAAO,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC;IACnC,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;IAC5D,sBAAsB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,CAAC;IACxD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,aAAa,EAAE,CAAC;IACnE,4BAA4B,EAAE,CAC5B,QAAQ,EAAE,aAAa,EAAE,KACtB,aAAa,EAAE,CAAC;IACrB,wBAAwB,CAAC,EAAE,CACzB,yBAAyB,EAAE,iBAAiB,KACzC,IAAI,CAAC;CACX,iBA6LF;AAED,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;CAa/B,CAAC"}
@@ -30,7 +30,7 @@ const SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';
30
30
  * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.
31
31
  * @returns A promise with wallet_createSession handler
32
32
  */
33
- async function handleWalletCreateSession(req, res, _next, end, hooks) {
33
+ async function walletCreateSessionHandler(req, res, _next, end, hooks) {
34
34
  if (!isPlainObject(req.params)) {
35
35
  return end(invalidParams({ data: { request: req } }));
36
36
  }
@@ -157,7 +157,8 @@ async function handleWalletCreateSession(req, res, _next, end, hooks) {
157
157
  }
158
158
  }
159
159
  export const walletCreateSession = {
160
- implementation: handleWalletCreateSession,
160
+ methodNames: ['wallet_createSession'],
161
+ implementation: walletCreateSessionHandler,
161
162
  hookNames: {
162
163
  findNetworkClientIdByChainId: true,
163
164
  listAccounts: true,
@@ -169,7 +170,4 @@ export const walletCreateSession = {
169
170
  trackSessionCreatedEvent: true,
170
171
  },
171
172
  };
172
- export const walletCreateSessionHandler = {
173
- wallet_createSession: walletCreateSession,
174
- };
175
173
  //# sourceMappingURL=wallet-createSession.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-createSession.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-createSession.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,YAAY,EACZ,0BAA0B,EAC1B,uBAAuB,EACvB,gBAAgB,EAChB,wBAAwB,EACxB,2BAA2B,EAC3B,kCAAkC,EAClC,6BAA6B,EAC7B,0CAA0C,EAC1C,yBAAyB,EAC1B,4CAA4C;AAM7C,OAAO,EAAE,sBAAsB,EAAE,mCAAmC;AAOpE,OAAO,EAAE,aAAa,EAAE,wCAAwC;AAEhE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,6BAA6B;AAC/D,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AAYzB,MAAM,oBAAoB,GAAG,yCAAyC,CAAC;AAyBvE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,KAAK,UAAU,yBAAyB,CACtC,GAAgD,EAChD,GAAmC,EACnC,KAAgC,EAChC,GAA6B,EAC7B,KAA+B;IAE/B,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QAC/B,OAAO,GAAG,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;IACxD,CAAC;IACD,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,iBAAiB,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;IAEzE,IAAI,iBAAiB,IAAI,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrE,OAAO,GAAG,CAAC,IAAI,YAAY,CAAC,IAAI,EAAE,qCAAqC,CAAC,CAAC,CAAC;IAC5E,CAAC;IAED,MAAM,yBAAy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IAAI,CACvE,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,gBAAgB,CAC7C,EAAE,KAA0B,CAAC;QAC9B,IAAI,CAAC,yBAAyB,EAAE,CAAC;YAC/B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;QAC7B,CAAC;QAED,MAAM,aAAa,GAAG,gBAAgB,CAAC,yBAAyB,EAAE;YAChE,yBAAyB,EAAE,KAAK,CAAC,yBAAyB;YAC1D,4BAA4B,EAAE,KAAK,CAAC,4BAA4B;SACjE,CAAC,CAAC;QAEH,MAAM,EAAE,iBAAiB,EAAE,yBAAyB,GAAG,EAAE,EAAE,GACzD,yBAAyB,CAAC;QAE5B,KAAK,CAAC,wBAAwB,EAAE,CAAC,yBAAyB,CAAC,CAAC;QAE5D,GAAG,CAAC,MAAM,GAAG;YACX,aAAa;YACb,iBAAiB,EAAE,yBAAyB;SAC7C,CAAC;QACF,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAUD,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,cAAc,EAAE,yBAAyB;IACzC,SAAS,EAAE;QACT,4BAA4B,EAAE,IAAI;QAClC,YAAY,EAAE,IAAI;QAClB,2BAA2B,EAAE,IAAI;QACjC,yBAAyB,EAAE,IAAI;QAC/B,sBAAsB,EAAE,IAAI;QAC5B,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,wBAAwB,EAAE,IAAI;KAC/B;CACyC,CAAC;AAE7C,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,oBAAoB,EAAE,mBAAmB;CAC1C,CAAC","sourcesContent":["import {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n bucketScopes,\n validateAndNormalizeScopes,\n getInternalScopesObject,\n getSessionScopes,\n getSupportedScopeObjects,\n isKnownSessionPropertyValue,\n getCaipAccountIdsFromScopesObjects,\n getAllScopesFromScopesObjects,\n setNonSCACaipAccountIdsInCaip25CaveatValue,\n isNamespaceInScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n Caip25Authorization,\n NormalizedScopesObject,\n Caip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type {\n MethodHandler,\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type { NetworkController } from '@metamask/network-controller';\nimport { invalidParams } from '@metamask/permission-controller';\nimport type { RequestedPermissions } from '@metamask/permission-controller';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport {\n isPlainObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { GrantedPermissions } from './types';\n\nconst SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';\n\ntype WalletCreateSessionHooks = {\n listAccounts: () => { address: string }[];\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n requestPermissionsForOrigin: (\n requestedPermissions: RequestedPermissions,\n metadata?: Record<string, Json>,\n ) => Promise<[GrantedPermissions]>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];\n sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];\n trackSessionCreatedEvent?: (\n approvedCaip25CaveatValue: Caip25CaveatValue,\n ) => void;\n};\n\ntype Params = Caip25Authorization;\n\ntype Result = {\n sessionScopes: NormalizedScopesObject;\n sessionProperties?: Record<string, Json>;\n};\n\n/**\n * Handler for the `wallet_createSession` RPC method which is responsible\n * for prompting for approval and granting a CAIP-25 permission.\n *\n * This implementation primarily deviates from the CAIP-25 handler\n * specification by treating all scopes as optional regardless of\n * if they were specified in `requiredScopes` or `optionalScopes`.\n * Additionally, provided scopes, methods, notifications, and\n * account values that are invalid/malformed are ignored rather than\n * causing an error to be returned.\n *\n * @param req - The request object.\n * @param res - The response object.\n * @param _next - The next middleware function.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.listAccounts - The hook that returns an array of the wallet's evm accounts.\n * @param hooks.findNetworkClientIdByChainId - The hook that returns the networkClientId for a chainId.\n * @param hooks.requestPermissionsForOrigin - The hook that approves and grants requested permissions.\n * @param hooks.getNonEvmSupportedMethods - The hook that returns the supported methods for a non EVM scope.\n * @param hooks.isNonEvmScopeSupported - The hook that returns true if a non EVM scope is supported.\n * @param hooks.getNonEvmAccountAddresses - The hook that returns a list of CaipAccountIds that are supported for a CaipChainId.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by last selected.\n * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.\n * @returns A promise with wallet_createSession handler\n */\nasync function handleWalletCreateSession(\n req: JsonRpcRequest<Params> & { origin: string },\n res: PendingJsonRpcResponse<Result>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletCreateSessionHooks,\n): Promise<void> {\n if (!isPlainObject(req.params)) {\n return end(invalidParams({ data: { request: req } }));\n }\n const { requiredScopes, optionalScopes, sessionProperties } = req.params;\n\n if (sessionProperties && Object.keys(sessionProperties).length === 0) {\n return end(new JsonRpcError(5302, 'Invalid sessionProperties requested'));\n }\n\n const filteredSessionProperties = Object.fromEntries(\n Object.entries(sessionProperties ?? {}).filter(([key]) =>\n isKnownSessionPropertyValue(key),\n ),\n );\n\n try {\n const { normalizedRequiredScopes, normalizedOptionalScopes } =\n validateAndNormalizeScopes(requiredScopes || {}, optionalScopes || {});\n\n const requiredScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedRequiredScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n const optionalScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedOptionalScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n\n const networkClientExistsForChainId = (chainId: Hex) => {\n try {\n hooks.findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n // if solana is a requested scope but not supported, we add a promptToCreateSolanaAccount flag to request\n const isSolanaRequested =\n isNamespaceInScopesObject(\n requiredScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n ) ||\n isNamespaceInScopesObject(\n optionalScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n );\n\n let promptToCreateSolanaAccount = false;\n if (isSolanaRequested) {\n const supportedSolanaAccounts =\n hooks.getNonEvmAccountAddresses(SOLANA_CAIP_CHAIN_ID);\n promptToCreateSolanaAccount = supportedSolanaAccounts.length === 0;\n }\n\n const { supportedScopes: supportedRequiredScopes } = bucketScopes(\n requiredScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const { supportedScopes: supportedOptionalScopes } = bucketScopes(\n optionalScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const allRequestedAccountAddresses = getCaipAccountIdsFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const allSupportedRequestedCaipChainIds = getAllScopesFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const existingEvmAddresses = hooks\n .listAccounts()\n .map((account) => account.address);\n\n const supportedRequestedAccountAddresses =\n allRequestedAccountAddresses.filter(\n (requestedAccountAddress: CaipAccountId) => {\n const {\n address,\n chain: { namespace },\n chainId: caipChainId,\n } = parseCaipAccountId(requestedAccountAddress);\n if (namespace === KnownCaipNamespace.Eip155.toString()) {\n return existingEvmAddresses.some((existingEvmAddress) => {\n return isEqualCaseInsensitive(address, existingEvmAddress);\n });\n }\n\n // If the namespace is not eip155 (EVM) we do a case sensitive check\n return hooks\n .getNonEvmAccountAddresses(caipChainId)\n .some((existingCaipAddress) => {\n return requestedAccountAddress === existingCaipAddress;\n });\n },\n );\n\n const requestedCaip25CaveatValue = {\n requiredScopes: getInternalScopesObject(supportedRequiredScopes),\n optionalScopes: getInternalScopesObject(supportedOptionalScopes),\n isMultichainOrigin: true,\n sessionProperties: filteredSessionProperties,\n };\n\n const requestedCaip25CaveatValueWithSupportedAccounts =\n setNonSCACaipAccountIdsInCaip25CaveatValue(\n requestedCaip25CaveatValue,\n supportedRequestedAccountAddresses,\n );\n\n // if `promptToCreateSolanaAccount` is true and there are no other valid scopes requested,\n // we add a `wallet` scope to the request in order to get passed the CAIP-25 caveat validator.\n // This is very hacky but is necessary because the solana opt-in flow breaks key assumptions\n // of the CAIP-25 permission specification - namely that we can have valid requests with no scopes.\n if (allSupportedRequestedCaipChainIds.length === 0) {\n if (promptToCreateSolanaAccount) {\n requestedCaip25CaveatValueWithSupportedAccounts.optionalScopes[\n KnownCaipNamespace.Wallet\n ] = {\n accounts: [],\n };\n } else {\n // if solana is not requested and there are no supported scopes, we return an error\n return end(\n new JsonRpcError(5100, 'Requested scopes are not supported'),\n );\n }\n }\n\n const [grantedPermissions] = await hooks.requestPermissionsForOrigin(\n {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: requestedCaip25CaveatValueWithSupportedAccounts,\n },\n ],\n },\n },\n {\n metadata: { promptToCreateSolanaAccount },\n },\n );\n\n const approvedCaip25Permission =\n grantedPermissions[Caip25EndowmentPermissionName];\n const approvedCaip25CaveatValue = approvedCaip25Permission?.caveats?.find(\n (caveat) => caveat.type === Caip25CaveatType,\n )?.value as Caip25CaveatValue;\n if (!approvedCaip25CaveatValue) {\n throw rpcErrors.internal();\n }\n\n const sessionScopes = getSessionScopes(approvedCaip25CaveatValue, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n });\n\n const { sessionProperties: approvedSessionProperties = {} } =\n approvedCaip25CaveatValue;\n\n hooks.trackSessionCreatedEvent?.(approvedCaip25CaveatValue);\n\n res.result = {\n sessionScopes,\n sessionProperties: approvedSessionProperties,\n };\n return end();\n } catch (err) {\n return end(err);\n }\n}\n\ntype WalletCreateSessionMethodHandler = MethodHandler<\n WalletCreateSessionHooks,\n never,\n Params,\n Result,\n { origin: string }\n>;\n\nexport const walletCreateSession = {\n implementation: handleWalletCreateSession,\n hookNames: {\n findNetworkClientIdByChainId: true,\n listAccounts: true,\n requestPermissionsForOrigin: true,\n getNonEvmSupportedMethods: true,\n isNonEvmScopeSupported: true,\n getNonEvmAccountAddresses: true,\n sortAccountIdsByLastSelected: true,\n trackSessionCreatedEvent: true,\n },\n} satisfies WalletCreateSessionMethodHandler;\n\nexport const walletCreateSessionHandler = {\n wallet_createSession: walletCreateSession,\n};\n"]}
1
+ {"version":3,"file":"wallet-createSession.mjs","sourceRoot":"","sources":["../../src/handlers/wallet-createSession.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC7B,YAAY,EACZ,0BAA0B,EAC1B,uBAAuB,EACvB,gBAAgB,EAChB,wBAAwB,EACxB,2BAA2B,EAC3B,kCAAkC,EAClC,6BAA6B,EAC7B,0CAA0C,EAC1C,yBAAyB,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H,MAAM,EAAE,iBAAiB,EAAE,yBAAyB,GAAG,EAAE,EAAE,GACzD,yBAAyB,CAAC;QAE5B,KAAK,CAAC,wBAAwB,EAAE,CAAC,yBAAyB,CAAC,CAAC;QAE5D,GAAG,CAAC,MAAM,GAAG;YACX,aAAa;YACb,iBAAiB,EAAE,yBAAyB;SAC7C,CAAC;QACF,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,WAAW,EAAE,CAAC,sBAAsB,CAAC;IACrC,cAAc,EAAE,0BAA0B;IAC1C,SAAS,EAAE;QACT,4BAA4B,EAAE,IAAI;QAClC,YAAY,EAAE,IAAI;QAClB,2BAA2B,EAAE,IAAI;QACjC,yBAAyB,EAAE,IAAI;QAC/B,sBAAsB,EAAE,IAAI;QAC5B,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;QAClC,wBAAwB,EAAE,IAAI;KAC/B;CACF,CAAC","sourcesContent":["import {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n bucketScopes,\n validateAndNormalizeScopes,\n getInternalScopesObject,\n getSessionScopes,\n getSupportedScopeObjects,\n isKnownSessionPropertyValue,\n getCaipAccountIdsFromScopesObjects,\n getAllScopesFromScopesObjects,\n setNonSCACaipAccountIdsInCaip25CaveatValue,\n isNamespaceInScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n Caip25Authorization,\n NormalizedScopesObject,\n Caip25CaveatValue,\n} from '@metamask/chain-agnostic-permission';\nimport { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type { NetworkController } from '@metamask/network-controller';\nimport { invalidParams } from '@metamask/permission-controller';\nimport type { RequestedPermissions } from '@metamask/permission-controller';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport {\n isPlainObject,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipChainId,\n Hex,\n Json,\n JsonRpcRequest,\n JsonRpcSuccess,\n} from '@metamask/utils';\n\nimport type { GrantedPermissions } from './types';\n\nconst SOLANA_CAIP_CHAIN_ID = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';\n\n/**\n * Handler for the `wallet_createSession` RPC method which is responsible\n * for prompting for approval and granting a CAIP-25 permission.\n *\n * This implementation primarily deviates from the CAIP-25 handler\n * specification by treating all scopes as optional regardless of\n * if they were specified in `requiredScopes` or `optionalScopes`.\n * Additionally, provided scopes, methods, notifications, and\n * account values that are invalid/malformed are ignored rather than\n * causing an error to be returned.\n *\n * @param req - The request object.\n * @param res - The response object.\n * @param _next - The next middleware function.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.listAccounts - The hook that returns an array of the wallet's evm accounts.\n * @param hooks.findNetworkClientIdByChainId - The hook that returns the networkClientId for a chainId.\n * @param hooks.requestPermissionsForOrigin - The hook that approves and grants requested permissions.\n * @param hooks.getNonEvmSupportedMethods - The hook that returns the supported methods for a non EVM scope.\n * @param hooks.isNonEvmScopeSupported - The hook that returns true if a non EVM scope is supported.\n * @param hooks.getNonEvmAccountAddresses - The hook that returns a list of CaipAccountIds that are supported for a CaipChainId.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by last selected.\n * @param hooks.trackSessionCreatedEvent - An optional hook for platform specific logic to run. Can be undefined.\n * @returns A promise with wallet_createSession handler\n */\nasync function walletCreateSessionHandler(\n req: JsonRpcRequest<Caip25Authorization> & { origin: string },\n res: JsonRpcSuccess<{\n sessionScopes: NormalizedScopesObject;\n sessionProperties?: Record<string, Json>;\n }>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: {\n listAccounts: () => { address: string }[];\n findNetworkClientIdByChainId: NetworkController['findNetworkClientIdByChainId'];\n requestPermissionsForOrigin: (\n requestedPermissions: RequestedPermissions,\n metadata?: Record<string, Json>,\n ) => Promise<[GrantedPermissions]>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => CaipAccountId[];\n sortAccountIdsByLastSelected: (\n accounts: CaipAccountId[],\n ) => CaipAccountId[];\n trackSessionCreatedEvent?: (\n approvedCaip25CaveatValue: Caip25CaveatValue,\n ) => void;\n },\n) {\n if (!isPlainObject(req.params)) {\n return end(invalidParams({ data: { request: req } }));\n }\n const { requiredScopes, optionalScopes, sessionProperties } = req.params;\n\n if (sessionProperties && Object.keys(sessionProperties).length === 0) {\n return end(new JsonRpcError(5302, 'Invalid sessionProperties requested'));\n }\n\n const filteredSessionProperties = Object.fromEntries(\n Object.entries(sessionProperties ?? {}).filter(([key]) =>\n isKnownSessionPropertyValue(key),\n ),\n );\n\n try {\n const { normalizedRequiredScopes, normalizedOptionalScopes } =\n validateAndNormalizeScopes(requiredScopes || {}, optionalScopes || {});\n\n const requiredScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedRequiredScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n const optionalScopesWithSupportedMethodsAndNotifications =\n getSupportedScopeObjects(normalizedOptionalScopes, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n });\n\n const networkClientExistsForChainId = (chainId: Hex) => {\n try {\n hooks.findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n // if solana is a requested scope but not supported, we add a promptToCreateSolanaAccount flag to request\n const isSolanaRequested =\n isNamespaceInScopesObject(\n requiredScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n ) ||\n isNamespaceInScopesObject(\n optionalScopesWithSupportedMethodsAndNotifications,\n KnownCaipNamespace.Solana,\n );\n\n let promptToCreateSolanaAccount = false;\n if (isSolanaRequested) {\n const supportedSolanaAccounts =\n hooks.getNonEvmAccountAddresses(SOLANA_CAIP_CHAIN_ID);\n promptToCreateSolanaAccount = supportedSolanaAccounts.length === 0;\n }\n\n const { supportedScopes: supportedRequiredScopes } = bucketScopes(\n requiredScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const { supportedScopes: supportedOptionalScopes } = bucketScopes(\n optionalScopesWithSupportedMethodsAndNotifications,\n {\n isEvmChainIdSupported: networkClientExistsForChainId,\n isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,\n },\n );\n\n const allRequestedAccountAddresses = getCaipAccountIdsFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const allSupportedRequestedCaipChainIds = getAllScopesFromScopesObjects([\n supportedRequiredScopes,\n supportedOptionalScopes,\n ]);\n\n const existingEvmAddresses = hooks\n .listAccounts()\n .map((account) => account.address);\n\n const supportedRequestedAccountAddresses =\n allRequestedAccountAddresses.filter(\n (requestedAccountAddress: CaipAccountId) => {\n const {\n address,\n chain: { namespace },\n chainId: caipChainId,\n } = parseCaipAccountId(requestedAccountAddress);\n if (namespace === KnownCaipNamespace.Eip155.toString()) {\n return existingEvmAddresses.some((existingEvmAddress) => {\n return isEqualCaseInsensitive(address, existingEvmAddress);\n });\n }\n\n // If the namespace is not eip155 (EVM) we do a case sensitive check\n return hooks\n .getNonEvmAccountAddresses(caipChainId)\n .some((existingCaipAddress) => {\n return requestedAccountAddress === existingCaipAddress;\n });\n },\n );\n\n const requestedCaip25CaveatValue = {\n requiredScopes: getInternalScopesObject(supportedRequiredScopes),\n optionalScopes: getInternalScopesObject(supportedOptionalScopes),\n isMultichainOrigin: true,\n sessionProperties: filteredSessionProperties,\n };\n\n const requestedCaip25CaveatValueWithSupportedAccounts =\n setNonSCACaipAccountIdsInCaip25CaveatValue(\n requestedCaip25CaveatValue,\n supportedRequestedAccountAddresses,\n );\n\n // if `promptToCreateSolanaAccount` is true and there are no other valid scopes requested,\n // we add a `wallet` scope to the request in order to get passed the CAIP-25 caveat validator.\n // This is very hacky but is necessary because the solana opt-in flow breaks key assumptions\n // of the CAIP-25 permission specification - namely that we can have valid requests with no scopes.\n if (allSupportedRequestedCaipChainIds.length === 0) {\n if (promptToCreateSolanaAccount) {\n requestedCaip25CaveatValueWithSupportedAccounts.optionalScopes[\n KnownCaipNamespace.Wallet\n ] = {\n accounts: [],\n };\n } else {\n // if solana is not requested and there are no supported scopes, we return an error\n return end(\n new JsonRpcError(5100, 'Requested scopes are not supported'),\n );\n }\n }\n\n const [grantedPermissions] = await hooks.requestPermissionsForOrigin(\n {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: requestedCaip25CaveatValueWithSupportedAccounts,\n },\n ],\n },\n },\n {\n metadata: { promptToCreateSolanaAccount },\n },\n );\n\n const approvedCaip25Permission =\n grantedPermissions[Caip25EndowmentPermissionName];\n const approvedCaip25CaveatValue = approvedCaip25Permission?.caveats?.find(\n (caveat) => caveat.type === Caip25CaveatType,\n )?.value as Caip25CaveatValue;\n if (!approvedCaip25CaveatValue) {\n throw rpcErrors.internal();\n }\n\n const sessionScopes = getSessionScopes(approvedCaip25CaveatValue, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n });\n\n const { sessionProperties: approvedSessionProperties = {} } =\n approvedCaip25CaveatValue;\n\n hooks.trackSessionCreatedEvent?.(approvedCaip25CaveatValue);\n\n res.result = {\n sessionScopes,\n sessionProperties: approvedSessionProperties,\n };\n return end();\n } catch (err) {\n return end(err);\n }\n}\n\nexport const walletCreateSession = {\n methodNames: ['wallet_createSession'],\n implementation: walletCreateSessionHandler,\n hookNames: {\n findNetworkClientIdByChainId: true,\n listAccounts: true,\n requestPermissionsForOrigin: true,\n getNonEvmSupportedMethods: true,\n isNonEvmScopeSupported: true,\n getNonEvmAccountAddresses: true,\n sortAccountIdsByLastSelected: true,\n trackSessionCreatedEvent: true,\n },\n};\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.walletGetSessionHandler = exports.walletGetSession = void 0;
3
+ exports.walletGetSession = void 0;
4
4
  const chain_agnostic_permission_1 = require("@metamask/chain-agnostic-permission");
5
5
  /**
6
6
  * Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
@@ -18,7 +18,7 @@ const chain_agnostic_permission_1 = require("@metamask/chain-agnostic-permission
18
18
  * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by corresponding last selected account in the wallet.
19
19
  * @returns Nothing.
20
20
  */
21
- async function handleWalletGetSession(_request, response, _next, end, hooks) {
21
+ async function walletGetSessionHandler(_request, response, _next, end, hooks) {
22
22
  let caveat;
23
23
  try {
24
24
  caveat = hooks.getCaveatForOrigin(chain_agnostic_permission_1.Caip25EndowmentPermissionName, chain_agnostic_permission_1.Caip25CaveatType);
@@ -39,14 +39,12 @@ async function handleWalletGetSession(_request, response, _next, end, hooks) {
39
39
  return end();
40
40
  }
41
41
  exports.walletGetSession = {
42
- implementation: handleWalletGetSession,
42
+ methodNames: ['wallet_getSession'],
43
+ implementation: walletGetSessionHandler,
43
44
  hookNames: {
44
45
  getCaveatForOrigin: true,
45
46
  getNonEvmSupportedMethods: true,
46
47
  sortAccountIdsByLastSelected: true,
47
48
  },
48
49
  };
49
- exports.walletGetSessionHandler = {
50
- wallet_getSession: exports.walletGetSession,
51
- };
52
50
  //# sourceMappingURL=wallet-getSession.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-getSession.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":";;;AAIA,mFAI6C;AA0B7C;;;;;;;;;;;;;;;GAeG;AACH,KAAK,UAAU,sBAAsB,CACnC,QAA6C,EAC7C,QAAwD,EACxD,KAAgC,EAChC,GAA6B,EAC7B,KAA4B;IAE5B,IAAI,MAAM,CAAC;IACX,IAAI,CAAC;QACH,MAAM,GAAG,KAAK,CAAC,kBAAkB,CAC/B,yDAA6B,EAC7B,4CAAgB,CACjB,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;IACT,CAAC;IAED,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,QAAQ,CAAC,MAAM,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC;QACxC,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG;QAChB,aAAa,EAAE,IAAA,4CAAgB,EAAC,MAAM,CAAC,KAAK,EAAE;YAC5C,yBAAyB,EAAE,KAAK,CAAC,yBAAyB;YAC1D,4BAA4B,EAAE,KAAK,CAAC,4BAA4B;SACjE,CAAC;KACH,CAAC;IACF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAUY,QAAA,gBAAgB,GAAG;IAC9B,cAAc,EAAE,sBAAsB;IACtC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;KACnC;CACsC,CAAC;AAE7B,QAAA,uBAAuB,GAAG;IACrC,iBAAiB,EAAE,wBAAgB;CACpC,CAAC","sourcesContent":["import type {\n Caip25CaveatValue,\n NormalizedScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getSessionScopes,\n} from '@metamask/chain-agnostic-permission';\nimport type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Caveat } from '@metamask/permission-controller';\nimport type {\n CaipAccountId,\n CaipChainId,\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\ntype WalletGetSessionResult = { sessionScopes: NormalizedScopesObject };\n\ntype WalletGetSessionHooks = {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];\n};\n\n/**\n * Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,\n * and that an empty object is returned for the `sessionScopes` result rather than throwing an error if there\n * is no active session for the origin.\n *\n * @param _request - The request object.\n * @param response - The response object.\n * @param _next - The next middleware function. Unused.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin.\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by corresponding last selected account in the wallet.\n * @returns Nothing.\n */\nasync function handleWalletGetSession(\n _request: JsonRpcRequest & { origin: string },\n response: PendingJsonRpcResponse<WalletGetSessionResult>,\n _next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: WalletGetSessionHooks,\n) {\n let caveat;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n );\n } catch {\n // noop\n }\n\n if (!caveat) {\n response.result = { sessionScopes: {} };\n return end();\n }\n\n response.result = {\n sessionScopes: getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n }),\n };\n return end();\n}\n\ntype WalletGetSessionMethodHandler = MethodHandler<\n WalletGetSessionHooks,\n never,\n JsonRpcParams,\n WalletGetSessionResult,\n { origin: string }\n>;\n\nexport const walletGetSession = {\n implementation: handleWalletGetSession,\n hookNames: {\n getCaveatForOrigin: true,\n getNonEvmSupportedMethods: true,\n sortAccountIdsByLastSelected: true,\n },\n} satisfies WalletGetSessionMethodHandler;\n\nexport const walletGetSessionHandler = {\n wallet_getSession: walletGetSession,\n};\n"]}
1
+ {"version":3,"file":"wallet-getSession.cjs","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":";;;AAIA,mFAI6C;AAS7C;;;;;;;;;;;;;;;GAeG;AACH,KAAK,UAAU,uBAAuB,CACpC,QAA6C,EAC7C,QAAmE,EACnE,KAAiB,EACjB,GAAe,EACf,KASC;IAED,IAAI,MAAM,CAAC;IACX,IAAI,CAAC;QACH,MAAM,GAAG,KAAK,CAAC,kBAAkB,CAC/B,yDAA6B,EAC7B,4CAAgB,CACjB,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;IACT,CAAC;IAED,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,QAAQ,CAAC,MAAM,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC;QACxC,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG;QAChB,aAAa,EAAE,IAAA,4CAAgB,EAAC,MAAM,CAAC,KAAK,EAAE;YAC5C,yBAAyB,EAAE,KAAK,CAAC,yBAAyB;YAC1D,4BAA4B,EAAE,KAAK,CAAC,4BAA4B;SACjE,CAAC;KACH,CAAC;IACF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAEY,QAAA,gBAAgB,GAAG;IAC9B,WAAW,EAAE,CAAC,mBAAmB,CAAC;IAClC,cAAc,EAAE,uBAAuB;IACvC,SAAS,EAAE;QACT,kBAAkB,EAAE,IAAI;QACxB,yBAAyB,EAAE,IAAI;QAC/B,4BAA4B,EAAE,IAAI;KACnC;CACF,CAAC","sourcesContent":["import type {\n Caip25CaveatValue,\n NormalizedScopesObject,\n} from '@metamask/chain-agnostic-permission';\nimport {\n Caip25CaveatType,\n Caip25EndowmentPermissionName,\n getSessionScopes,\n} from '@metamask/chain-agnostic-permission';\nimport type { Caveat } from '@metamask/permission-controller';\nimport type { CaipAccountId } from '@metamask/utils';\nimport type {\n CaipChainId,\n JsonRpcRequest,\n JsonRpcSuccess,\n} from '@metamask/utils';\n\n/**\n * Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).\n * The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,\n * and that an empty object is returned for the `sessionScopes` result rather than throwing an error if there\n * is no active session for the origin.\n *\n * @param _request - The request object.\n * @param response - The response object.\n * @param _next - The next middleware function. Unused.\n * @param end - The end function.\n * @param hooks - The hooks object.\n * @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin.\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by corresponding last selected account in the wallet.\n * @returns Nothing.\n */\nasync function walletGetSessionHandler(\n _request: JsonRpcRequest & { origin: string },\n response: JsonRpcSuccess<{ sessionScopes: NormalizedScopesObject }>,\n _next: () => void,\n end: () => void,\n hooks: {\n getCaveatForOrigin: (\n endowmentPermissionName: string,\n caveatType: string,\n ) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n sortAccountIdsByLastSelected: (\n accounts: CaipAccountId[],\n ) => CaipAccountId[];\n },\n) {\n let caveat;\n try {\n caveat = hooks.getCaveatForOrigin(\n Caip25EndowmentPermissionName,\n Caip25CaveatType,\n );\n } catch {\n // noop\n }\n\n if (!caveat) {\n response.result = { sessionScopes: {} };\n return end();\n }\n\n response.result = {\n sessionScopes: getSessionScopes(caveat.value, {\n getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,\n sortAccountIdsByLastSelected: hooks.sortAccountIdsByLastSelected,\n }),\n };\n return end();\n}\n\nexport const walletGetSession = {\n methodNames: ['wallet_getSession'],\n implementation: walletGetSessionHandler,\n hookNames: {\n getCaveatForOrigin: true,\n getNonEvmSupportedMethods: true,\n sortAccountIdsByLastSelected: true,\n },\n};\n"]}
@@ -1,16 +1,8 @@
1
1
  import type { Caip25CaveatValue, NormalizedScopesObject } from "@metamask/chain-agnostic-permission";
2
2
  import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
3
- import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
4
3
  import type { Caveat } from "@metamask/permission-controller";
5
- import type { CaipAccountId, CaipChainId, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
6
- type WalletGetSessionResult = {
7
- sessionScopes: NormalizedScopesObject;
8
- };
9
- type WalletGetSessionHooks = {
10
- getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
11
- getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
12
- sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
13
- };
4
+ import type { CaipAccountId } from "@metamask/utils";
5
+ import type { CaipChainId, JsonRpcRequest, JsonRpcSuccess } from "@metamask/utils";
14
6
  /**
15
7
  * Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
16
8
  * The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,
@@ -27,25 +19,22 @@ type WalletGetSessionHooks = {
27
19
  * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by corresponding last selected account in the wallet.
28
20
  * @returns Nothing.
29
21
  */
30
- declare function handleWalletGetSession(_request: JsonRpcRequest & {
22
+ declare function walletGetSessionHandler(_request: JsonRpcRequest & {
31
23
  origin: string;
32
- }, response: PendingJsonRpcResponse<WalletGetSessionResult>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletGetSessionHooks): Promise<void>;
24
+ }, response: JsonRpcSuccess<{
25
+ sessionScopes: NormalizedScopesObject;
26
+ }>, _next: () => void, end: () => void, hooks: {
27
+ getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
28
+ getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
29
+ sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
30
+ }): Promise<void>;
33
31
  export declare const walletGetSession: {
34
- implementation: typeof handleWalletGetSession;
32
+ methodNames: string[];
33
+ implementation: typeof walletGetSessionHandler;
35
34
  hookNames: {
36
- getCaveatForOrigin: true;
37
- getNonEvmSupportedMethods: true;
38
- sortAccountIdsByLastSelected: true;
39
- };
40
- };
41
- export declare const walletGetSessionHandler: {
42
- wallet_getSession: {
43
- implementation: typeof handleWalletGetSession;
44
- hookNames: {
45
- getCaveatForOrigin: true;
46
- getNonEvmSupportedMethods: true;
47
- sortAccountIdsByLastSelected: true;
48
- };
35
+ getCaveatForOrigin: boolean;
36
+ getNonEvmSupportedMethods: boolean;
37
+ sortAccountIdsByLastSelected: boolean;
49
38
  };
50
39
  };
51
40
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"wallet-getSession.d.cts","sourceRoot":"","sources":["../../src/handlers/wallet-getSession.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,sBAAsB,EACvB,4CAA4C;AAC7C,OAAO,EACL,gBAAgB,EAGjB,4CAA4C;AAC7C,OAAO,KAAK,EACV,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AAc,EACf,wBAAwB;AAEzB;;;;;;;;;;;;;;;GAeG;AACH,iBAAe,uBAAuB,CACpC,QAAQ,EAAE,cAAc,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7C,QAAQ,EAAE,cAAc,CAAC;IAAE,aAAa,EAAE,sBAAsB,CAAA;CAAE,CAAC,EACnE,KAAK,EAAE,MAAM,IAAI,EACjB,GAAG,EAAE,MAAM,IAAI,EACf,KAAK,EAAE;IACL,kBAAkB,EAAE,CAClB,uBAAuB,EAAE,MAAM,EAC/B,UAAU,EAAE,MAAM,KACf,MAAM,CAAC,OAAO,gBAAgB,EAAE,iBAAiB,CAAC,CAAC;IACxD,yBAAyB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,MAAM,EAAE,CAAC;IAC5D,4BAA4B,EAAE,CAC5B,QAAQ,EAAE,aAAa,EAAE,KACtB,aAAa,EAAE,CAAC;CACtB,iBAwBF;AAED,eAAO,MAAM,gBAAgB;;;;;;;;CAQ5B,CAAC"}
@@ -1,16 +1,8 @@
1
1
  import type { Caip25CaveatValue, NormalizedScopesObject } from "@metamask/chain-agnostic-permission";
2
2
  import { Caip25CaveatType } from "@metamask/chain-agnostic-permission";
3
- import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
4
3
  import type { Caveat } from "@metamask/permission-controller";
5
- import type { CaipAccountId, CaipChainId, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
6
- type WalletGetSessionResult = {
7
- sessionScopes: NormalizedScopesObject;
8
- };
9
- type WalletGetSessionHooks = {
10
- getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
11
- getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
12
- sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
13
- };
4
+ import type { CaipAccountId } from "@metamask/utils";
5
+ import type { CaipChainId, JsonRpcRequest, JsonRpcSuccess } from "@metamask/utils";
14
6
  /**
15
7
  * Handler for the `wallet_getSession` RPC method as specified by [CAIP-312](https://chainagnostic.org/CAIPs/caip-312).
16
8
  * The implementation below deviates from the linked spec in that it ignores the `sessionId` param entirely,
@@ -27,25 +19,22 @@ type WalletGetSessionHooks = {
27
19
  * @param hooks.sortAccountIdsByLastSelected - A function that accepts an array of CaipAccountId and returns an array of CaipAccountId sorted by corresponding last selected account in the wallet.
28
20
  * @returns Nothing.
29
21
  */
30
- declare function handleWalletGetSession(_request: JsonRpcRequest & {
22
+ declare function walletGetSessionHandler(_request: JsonRpcRequest & {
31
23
  origin: string;
32
- }, response: PendingJsonRpcResponse<WalletGetSessionResult>, _next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: WalletGetSessionHooks): Promise<void>;
24
+ }, response: JsonRpcSuccess<{
25
+ sessionScopes: NormalizedScopesObject;
26
+ }>, _next: () => void, end: () => void, hooks: {
27
+ getCaveatForOrigin: (endowmentPermissionName: string, caveatType: string) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
28
+ getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
29
+ sortAccountIdsByLastSelected: (accounts: CaipAccountId[]) => CaipAccountId[];
30
+ }): Promise<void>;
33
31
  export declare const walletGetSession: {
34
- implementation: typeof handleWalletGetSession;
32
+ methodNames: string[];
33
+ implementation: typeof walletGetSessionHandler;
35
34
  hookNames: {
36
- getCaveatForOrigin: true;
37
- getNonEvmSupportedMethods: true;
38
- sortAccountIdsByLastSelected: true;
39
- };
40
- };
41
- export declare const walletGetSessionHandler: {
42
- wallet_getSession: {
43
- implementation: typeof handleWalletGetSession;
44
- hookNames: {
45
- getCaveatForOrigin: true;
46
- getNonEvmSupportedMethods: true;
47
- sortAccountIdsByLastSelected: true;
48
- };
35
+ getCaveatForOrigin: boolean;
36
+ getNonEvmSupportedMethods: boolean;
37
+ sortAccountIdsByLastSelected: boolean;
49
38
  };
50
39
  };
51
40
  export {};