@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
1 lines • 5.39 kB
Source Map (JSON)
{"version":3,"file":"preinstalled-snaps.cjs","sourceRoot":"","sources":["../../src/middleware/preinstalled-snaps.ts"],"names":[],"mappings":";;;AAKA,uDAAgD;AAChD,2CAMyB;AAEzB,wDAA+C;AAuB/C,MAAM,cAAc,GAAG,eAAe,CAAC;AAWvC;;;;;;;;;GASG;AACH,SAAgB,iCAAiC,CAAC,EAChD,iBAAiB,EACjB,cAAc,EACd,gBAAgB,GACiB;IACjC,OAAO,SAAS,gBAAgB,CAAC,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI;QAC7D,IAAI,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YACtC,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;QAErC,IACE,CAAC,WAAW;YACZ,CAAC,IAAA,mBAAW,EAAC,WAAW,EAAE,2BAAc,CAAC,gBAAgB,CAAC,EAC1D,CAAC;YACD,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,MAAM,iBAAiB,GAAG,WAAW,CAAC,kBAAkB,CAAC,CAAC;QAC1D,MAAM,cAAc,GAAG,iBAAiB,EAAE,OAAO,EAAE,IAAI,CACrD,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,kBAAkB,CAC/C,EAAE,KAA0C,CAAC;QAE9C,MAAM,sBAAsB,GAAG,cAAc,EAAE,cAAc,IAAI,EAAE,CAAC;QACpE,MAAM,sBAAsB,GAAG,cAAc,EAAE,cAAc,IAAI,EAAE,CAAC;QAEpE,MAAM,mBAAmB,GACvB,sBAAsB,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAC/D,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,CAAC,CACzC,IAAI,EAAE,CAAC;QAEV,MAAM,WAAW,GAAG,iBAAiB,EAAE,CAAC;QAExC,IAAI,IAAA,qBAAO,EAAC,WAAW,EAAE,mBAAmB,CAAC,EAAE,CAAC;YAC9C,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,gBAAgB,CAAC;YACf,kBAAkB,EAAE;gBAClB,OAAO,EAAE;oBACP;wBACE,IAAI,EAAE,kBAAkB;wBACxB,KAAK,EAAE;4BACL,cAAc,EAAE,sBAAsB;4BACtC,cAAc,EAAE;gCACd,GAAG,sBAAsB;gCACzB,CAAC,cAAc,CAAC,EAAE;oCAChB,QAAQ,EAAE,WAAW,CAAC,GAAG,CACvB,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,cAAc,IAAI,OAAO,EAAE,CAC5C;iCACF;6BACF;4BACD,iBAAiB,EAAE,EAAE;4BACrB,kBAAkB,EAAE,KAAK;yBAC1B;qBACF;iBACF;aACF;SACF,CAAC,CAAC;QAEH,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AA/DD,8EA+DC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n PermissionConstraint,\n RequestedPermissions,\n} from '@metamask/permission-controller';\nimport { isEqual } from '@metamask/snaps-utils';\nimport {\n type CaipAccountId,\n type CaipChainId,\n hasProperty,\n type Json,\n type JsonRpcParams,\n} from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\n\nexport type PreinstalledSnapsMiddlewareHooks = {\n /**\n * Get all accounts with the eip155 scope.\n *\n * @returns A list of all account addresses from the eip155 scope.\n */\n getAllEvmAccounts: () => string[];\n /**\n * Get the current permissions for the requesting origin.\n *\n * @returns An object containing the metadata about each permission.\n */\n getPermissions: () => Record<string, PermissionConstraint> | undefined;\n /**\n * Grant the passed permissions to the origin.\n *\n * @param permissions\n */\n grantPermissions: (permissions: RequestedPermissions) => void;\n};\n\nconst WILDCARD_SCOPE = 'wallet:eip155';\n\ntype ScopesObject = Record<CaipChainId, { accounts: CaipAccountId[] }>;\n\ntype AuthorizedScopeCaveat = {\n requiredScopes: ScopesObject;\n optionalScopes: ScopesObject;\n sessionProperties: Record<string, Json>;\n isMultichainOrigin: boolean;\n};\n\n/**\n * Create a middleware that automatically grants account permissions to preinstalled Snaps\n * that want to use the Ethereum provider endowment.\n *\n * @param hooks - The hooks used by the middleware.\n * @param hooks.getAllEvmAccounts - Hook for retriveing all available EVM addresses.\n * @param hooks.getPermissions - Hook for retrieving the permissions of the requesting origin.\n * @param hooks.grantPermissions - Hook for granting permissions to the requesting origin.\n * @returns The middleware.\n */\nexport function createPreinstalledSnapsMiddleware({\n getAllEvmAccounts,\n getPermissions,\n grantPermissions,\n}: PreinstalledSnapsMiddlewareHooks): JsonRpcMiddleware<JsonRpcParams, Json> {\n return function methodMiddleware(request, _response, next, _end) {\n if (request.method.startsWith('snap')) {\n return next();\n }\n\n const permissions = getPermissions();\n\n if (\n !permissions ||\n !hasProperty(permissions, SnapEndowments.EthereumProvider)\n ) {\n return next();\n }\n\n const existingEndowment = permissions['endowment:caip25'];\n const existingCaveat = existingEndowment?.caveats?.find(\n (caveat) => caveat.type === 'authorizedScopes',\n )?.value as AuthorizedScopeCaveat | undefined;\n\n const existingRequiredScopes = existingCaveat?.requiredScopes ?? {};\n const existingOptionalScopes = existingCaveat?.optionalScopes ?? {};\n\n const existingEvmAccounts =\n existingOptionalScopes[WILDCARD_SCOPE]?.accounts.map((account) =>\n account.slice(WILDCARD_SCOPE.length + 1),\n ) ?? [];\n\n const evmAccounts = getAllEvmAccounts();\n\n if (isEqual(evmAccounts, existingEvmAccounts)) {\n return next();\n }\n\n grantPermissions({\n 'endowment:caip25': {\n caveats: [\n {\n type: 'authorizedScopes',\n value: {\n requiredScopes: existingRequiredScopes,\n optionalScopes: {\n ...existingOptionalScopes,\n [WILDCARD_SCOPE]: {\n accounts: evmAccounts.map(\n (account) => `${WILDCARD_SCOPE}:${account}`,\n ),\n },\n },\n sessionProperties: {},\n isMultichainOrigin: false,\n },\n },\n ],\n },\n });\n\n return next();\n };\n}\n"]}