UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 6.43 kB
{"version":3,"file":"listEntropySources.cjs","sourceRoot":"","sources":["../../src/permitted/listEntropySources.ts"],"names":[],"mappings":";;;AAMA,qDAAsD;AAQtD,uEAAuE;AACvE,2EAA2E;AAC3E,uEAAuE;AACvE,6DAA6D;AAO7D;;;GAGG;AACH,MAAM,oBAAoB,GAAG;IAC3B,wCAAsB,CAAC,UAAU;IACjC,4CAAwB,CAAC,UAAU;IACnC,wCAAsB,CAAC,UAAU;IACjC,8BAAiB,CAAC,UAAU;CAC7B,CAAC;AAEF;;GAEG;AACH,MAAM,eAAe,GAAG,aAAa,CAAC;AAEtC,MAAM,SAAS,GAAqD;IAClE,gBAAgB,EAAE,IAAI;CACvB,CAAC;AAeF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACU,QAAA,yBAAyB,GAAG;IACvC,cAAc,EAAE,gCAAgC;IAChD,SAAS;IACT,WAAW,EAAE;QACX,oCAAoC;QACpC,4BAA4B;KAC7B;CAOF,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,KAAK,UAAU,gCAAgC,CAC7C,OAA2D,EAC3D,QAA0D,EAC1D,KAAc,EACd,GAA6B,EAC7B,EAAE,gBAAgB,EAAiC,EACnD,SAA6D;IAE7D,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE3B,MAAM,WAAW,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE,CAC3D,SAAS,CAAC,IAAI,CAAC,oCAAoC,EAAE,MAAM,EAAE,UAAU,CAAC,CACzE,CAAC;IAEF,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,GAAG,CAAC,2BAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAE7B,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;IAElE,QAAQ,CAAC,MAAM,GAAG,QAAQ;SACvB,GAAG,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE;QACtB,IAAI,OAAO,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;YACrC,OAAO;gBACL,EAAE,EAAE,OAAO,CAAC,QAAQ,CAAC,EAAE;gBACvB,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAC3B,IAAI,EAAE,UAAU;gBAChB,OAAO,EAAE,KAAK,KAAK,CAAC;aACrB,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,aAAa,EAAkC,EAAE,CACxD,OAAO,CAAC,aAAa,CAAC,CACvB,CAAC;IAEJ,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { providerErrors } from '@metamask/rpc-errors';\nimport type {\n EntropySource,\n ListEntropySourcesParams,\n ListEntropySourcesResult,\n} from '@metamask/snaps-sdk';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { getBip32EntropyBuilder } from '../restricted/getBip32Entropy';\nimport { getBip32PublicKeyBuilder } from '../restricted/getBip32PublicKey';\nimport { getBip44EntropyBuilder } from '../restricted/getBip44Entropy';\nimport { getEntropyBuilder } from '../restricted/getEntropy';\nimport type {\n JsonRpcRequestWithOrigin,\n KeyringControllerGetStateAction,\n} from '../types';\nimport type { MethodHooksObject } from '../utils';\n\n/**\n * A list of permissions that the requesting origin must have at least one of\n * in order to call this method.\n */\nconst REQUIRED_PERMISSIONS = [\n getBip32EntropyBuilder.targetName,\n getBip32PublicKeyBuilder.targetName,\n getBip44EntropyBuilder.targetName,\n getEntropyBuilder.targetName,\n];\n\n/**\n * The keyring type used by HD (mnemonic-based) keyrings.\n */\nconst HD_KEYRING_TYPE = 'HD Key Tree';\n\nconst hookNames: MethodHooksObject<ListEntropySourcesMethodHooks> = {\n getUnlockPromise: true,\n};\n\nexport type ListEntropySourcesMethodHooks = {\n /**\n * Wait for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n};\n\nexport type ListEntropySourcesMethodActions =\n | PermissionControllerHasPermissionAction\n | KeyringControllerGetStateAction;\n\n/**\n * Get a list of entropy sources available to the Snap. The requesting origin\n * must have at least one of the following permissions to access entropy source\n * metadata:\n *\n * - `snap_getBip32Entropy`\n * - `snap_getBip32PublicKey`\n * - `snap_getBip44Entropy`\n * - `snap_getEntropy`\n *\n * @example\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_getBip32Entropy\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * const entropySources = await snap.request({ method: 'snap_listEntropySources' });\n * console.log(entropySources);\n * // Example output:\n * // [\n * // {\n * // name: 'Mnemonic 1',\n * // id: 'mnemonic-1',\n * // type: 'mnemonic',\n * // primary: true,\n * // },\n * // {\n * // name: 'Mnemonic 2',\n * // id: 'mnemonic-2',\n * // type: 'mnemonic',\n * // primary: false,\n * // },\n * // ]\n * ```\n */\nexport const listEntropySourcesHandler = {\n implementation: listEntropySourcesImplementation,\n hookNames,\n actionNames: [\n 'PermissionController:hasPermission',\n 'KeyringController:getState',\n ],\n} satisfies MethodHandler<\n ListEntropySourcesMethodHooks,\n ListEntropySourcesMethodActions,\n ListEntropySourcesParams,\n ListEntropySourcesResult,\n { origin: string }\n>;\n\n/**\n * The `snap_listEntropySources` method implementation.\n *\n * @param request - The JSON-RPC request object.\n * @param response - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getUnlockPromise - The function to get the unlock promise.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nasync function listEntropySourcesImplementation(\n request: JsonRpcRequestWithOrigin<ListEntropySourcesParams>,\n response: PendingJsonRpcResponse<ListEntropySourcesResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getUnlockPromise }: ListEntropySourcesMethodHooks,\n messenger: Messenger<string, ListEntropySourcesMethodActions>,\n): Promise<void> {\n const { origin } = request;\n\n const isPermitted = REQUIRED_PERMISSIONS.some((permission) =>\n messenger.call('PermissionController:hasPermission', origin, permission),\n );\n\n if (!isPermitted) {\n return end(providerErrors.unauthorized());\n }\n\n await getUnlockPromise(true);\n\n const { keyrings } = messenger.call('KeyringController:getState');\n\n response.result = keyrings\n .map((keyring, index) => {\n if (keyring.type === HD_KEYRING_TYPE) {\n return {\n id: keyring.metadata.id,\n name: keyring.metadata.name,\n type: 'mnemonic',\n primary: index === 0,\n };\n }\n\n return null;\n })\n .filter((entropySource): entropySource is EntropySource =>\n Boolean(entropySource),\n );\n\n return end();\n}\n"]}