From 1a87b4dd3c8c6f73bc83eebf3c412c9247d4ffad Mon Sep 17 00:00:00 2001 From: Jiexi Luan Date: Wed, 15 Jan 2025 10:57:12 -0800 Subject: [PATCH] remove adapter middleware --- ...caip-permission-adapter-middleware.test.ts | 147 ------------------ .../caip-permission-adapter-middleware.ts | 80 ---------- packages/multichain/src/index.ts | 1 - 3 files changed, 228 deletions(-) delete mode 100644 packages/multichain/src/adapters/caip-permission-adapter-middleware.test.ts delete mode 100644 packages/multichain/src/adapters/caip-permission-adapter-middleware.ts diff --git a/packages/multichain/src/adapters/caip-permission-adapter-middleware.test.ts b/packages/multichain/src/adapters/caip-permission-adapter-middleware.test.ts deleted file mode 100644 index fd47f4107b..0000000000 --- a/packages/multichain/src/adapters/caip-permission-adapter-middleware.test.ts +++ /dev/null @@ -1,147 +0,0 @@ -import { providerErrors } from '@metamask/rpc-errors'; -import type { JsonRpcRequest } from '@metamask/utils'; - -import { - Caip25CaveatType, - Caip25EndowmentPermissionName, -} from '../caip25Permission'; -import { caipPermissionAdapterMiddleware } from './caip-permission-adapter-middleware'; - -const baseRequest = { - id: 1, - jsonrpc: '2.0' as const, - origin: 'http://test.com', - networkClientId: 'mainnet', - method: 'eth_call', - params: { - foo: 'bar', - }, -}; - -const createMockedHandler = () => { - const next = jest.fn(); - const end = jest.fn(); - const getCaveatForOrigin = jest.fn().mockReturnValue({ - value: { - requiredScopes: { - 'eip155:1': { - methods: ['eth_call'], - notifications: [], - accounts: [], - }, - 'eip155:5': { - methods: ['eth_chainId'], - notifications: [], - accounts: [], - }, - }, - optionalScopes: { - 'eip155:1': { - methods: ['net_version'], - notifications: [], - accounts: [], - }, - wallet: { - methods: ['wallet_watchAsset'], - notifications: [], - accounts: [], - }, - unhandled: { - methods: ['foobar'], - notifications: [], - accounts: [], - }, - }, - isMultichainOrigin: true, - }, - }); - const getNetworkConfigurationByNetworkClientId = jest - .fn() - .mockImplementation((networkClientId: string) => { - const chainId = - { - mainnet: '0x1', - goerli: '0x5', - }[networkClientId] || '0x999'; - return { - chainId, - }; - }); - const handler = ( - request: JsonRpcRequest & { - networkClientId: string; - origin: string; - }, - ) => - caipPermissionAdapterMiddleware(request, {}, next, end, { - getCaveatForOrigin, - getNetworkConfigurationByNetworkClientId, - }); - - return { - next, - end, - getCaveatForOrigin, - getNetworkConfigurationByNetworkClientId, - handler, - }; -}; - -describe('CaipPermissionAdapterMiddleware', () => { - it('gets the authorized scopes from the CAIP-25 endowment permission', async () => { - const { handler, getCaveatForOrigin } = createMockedHandler(); - await handler(baseRequest); - expect(getCaveatForOrigin).toHaveBeenCalledWith( - Caip25EndowmentPermissionName, - Caip25CaveatType, - ); - }); - - it('allows the request when there is no CAIP-25 endowment permission', async () => { - const { handler, getCaveatForOrigin, next } = createMockedHandler(); - getCaveatForOrigin.mockImplementation(() => { - throw new Error('permission not found'); - }); - await handler(baseRequest); - expect(next).toHaveBeenCalled(); - }); - - it('allows the request when the CAIP-25 endowment permission was not granted from the multichain API', async () => { - const { handler, getCaveatForOrigin, next } = createMockedHandler(); - getCaveatForOrigin.mockReturnValue({ - value: { - isMultichainOrigin: false, - }, - }); - await handler(baseRequest); - expect(next).toHaveBeenCalled(); - }); - - it('gets the chainId for the request networkClientId', async () => { - const { handler, getNetworkConfigurationByNetworkClientId } = - createMockedHandler(); - await handler(baseRequest); - expect(getNetworkConfigurationByNetworkClientId).toHaveBeenCalledWith( - 'mainnet', - ); - }); - - describe('when the CAIP-25 endowment permission was granted over the multichain API', () => { - it('throws an error if the requested method is not authorized for the scope specified in the request', async () => { - const { handler, end } = createMockedHandler(); - - await handler({ - ...baseRequest, - method: 'unauthorized_method', - }); - expect(end).toHaveBeenCalledWith(providerErrors.unauthorized()); - }); - - it('allows the request if the requested scope method is authorized in the current scope', async () => { - const { handler, next } = createMockedHandler(); - - await handler(baseRequest); - expect(next).toHaveBeenCalled(); - }); - }); -}); diff --git a/packages/multichain/src/adapters/caip-permission-adapter-middleware.ts b/packages/multichain/src/adapters/caip-permission-adapter-middleware.ts deleted file mode 100644 index 3d258a45fe..0000000000 --- a/packages/multichain/src/adapters/caip-permission-adapter-middleware.ts +++ /dev/null @@ -1,80 +0,0 @@ -import type { - NetworkConfiguration, - NetworkClientId, -} from '@metamask/network-controller'; -import type { Caveat } from '@metamask/permission-controller'; -import { providerErrors } from '@metamask/rpc-errors'; -import { hexToBigInt, type JsonRpcRequest } from '@metamask/utils'; - -import type { Caip25CaveatValue } from '../caip25Permission'; -import { - Caip25CaveatType, - Caip25EndowmentPermissionName, -} from '../caip25Permission'; -import { Eip1193OnlyMethods, KnownWalletScopeString } from '../scope/constants'; -import type { InternalScopeString } from '../scope/types'; -import { getSessionScopes } from './caip-permission-adapter-session-scopes'; - -/** - * Middleware to handle CAIP-25 permission requests. - * - * @param request - The request object. - * @param _response - The response object. Unused. - * @param next - The next middleware function. - * @param end - The end function. - * @param hooks - The hooks object. - * @param hooks.getCaveatForOrigin - Function to retrieve a caveat for the origin. - * @param hooks.getNetworkConfigurationByNetworkClientId - Function to retrieve a network configuration. - */ -export async function caipPermissionAdapterMiddleware( - request: JsonRpcRequest & { - networkClientId: NetworkClientId; - origin: string; - }, - _response: unknown, - next: () => Promise, - end: (error?: Error) => void, - hooks: { - getCaveatForOrigin: ( - ...args: unknown[] - ) => Caveat; - getNetworkConfigurationByNetworkClientId: ( - networkClientId: NetworkClientId, - ) => NetworkConfiguration; - }, -) { - const { networkClientId, method } = request; - - let caveat; - try { - caveat = hooks.getCaveatForOrigin( - Caip25EndowmentPermissionName, - Caip25CaveatType, - ); - } catch (err) { - // noop - } - if (!caveat?.value?.isMultichainOrigin) { - return next(); - } - - const { chainId } = - hooks.getNetworkConfigurationByNetworkClientId(networkClientId); - - const scope: InternalScopeString = `eip155:${hexToBigInt(chainId).toString( - 10, - )}`; - - const sessionScopes = getSessionScopes(caveat.value); - - if ( - !sessionScopes[scope]?.methods?.includes(method) && - !sessionScopes[KnownWalletScopeString.Eip155]?.methods?.includes(method) && - !sessionScopes.wallet?.methods?.includes(method) && - !Eip1193OnlyMethods.includes(method) - ) { - return end(providerErrors.unauthorized()); - } - - return next(); -} diff --git a/packages/multichain/src/index.ts b/packages/multichain/src/index.ts index 49b53a38e0..60732796b4 100644 --- a/packages/multichain/src/index.ts +++ b/packages/multichain/src/index.ts @@ -11,7 +11,6 @@ export { getInternalScopesObject, getSessionScopes, } from './adapters/caip-permission-adapter-session-scopes'; -export { caipPermissionAdapterMiddleware } from './adapters/caip-permission-adapter-middleware'; export { walletGetSession } from './handlers/wallet-getSession'; export { walletInvokeMethod } from './handlers/wallet-invokeMethod';