UNPKG

@metamask/snaps-rpc-methods

Version:
1 lines 4.49 kB
{"version":3,"file":"listEntropySources.mjs","sourceRoot":"","sources":["../../src/permitted/listEntropySources.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,6BAA6B;AAStD,OAAO,EAAE,sBAAsB,EAAE,0CAAsC;AACvE,OAAO,EAAE,wBAAwB,EAAE,4CAAwC;AAC3E,OAAO,EAAE,sBAAsB,EAAE,0CAAsC;AACvE,OAAO,EAAE,iBAAiB,EAAE,qCAAiC;AAG7D;;;GAGG;AACH,MAAM,oBAAoB,GAAG;IAC3B,sBAAsB,CAAC,UAAU;IACjC,wBAAwB,CAAC,UAAU;IACnC,sBAAsB,CAAC,UAAU;IACjC,iBAAiB,CAAC,UAAU;CAC7B,CAAC;AAEF,MAAM,SAAS,GAA+C;IAC5D,aAAa,EAAE,IAAI;IACnB,iBAAiB,EAAE,IAAI;IACvB,gBAAgB,EAAE,IAAI;CACvB,CAAC;AA0BF,MAAM,CAAC,MAAM,yBAAyB,GAIlC;IACF,WAAW,EAAE,CAAC,yBAAyB,CAAC;IACxC,cAAc,EAAE,gCAAgC;IAChD,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,KAAK,UAAU,gCAAgC,CAC7C,QAAkD,EAClD,QAA0D,EAC1D,KAAc,EACd,GAA6B,EAC7B,EACE,aAAa,EACb,iBAAiB,EACjB,gBAAgB,GACQ;IAE1B,MAAM,WAAW,GAAG,oBAAoB,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC7D,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAE7B,QAAQ,CAAC,MAAM,GAAG,iBAAiB,EAAE,CAAC;IACtC,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors } from '@metamask/rpc-errors';\nimport type {\n EntropySource,\n JsonRpcRequest,\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 { 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\nconst hookNames: MethodHooksObject<ListEntropySourcesHooks> = {\n hasPermission: true,\n getEntropySources: true,\n getUnlockPromise: true,\n};\n\nexport type ListEntropySourcesHooks = {\n /**\n * Check if the requesting origin has a given permission.\n *\n * @param permissionName - The name of the permission to check.\n * @returns Whether the origin has the permission.\n */\n hasPermission: (permissionName: string) => boolean;\n\n /**\n * Get the entropy sources from the client.\n *\n * @returns The entropy sources.\n */\n getEntropySources: () => EntropySource[];\n\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 const listEntropySourcesHandler: PermittedHandlerExport<\n ListEntropySourcesHooks,\n ListEntropySourcesParams,\n ListEntropySourcesResult\n> = {\n methodNames: ['snap_listEntropySources'],\n implementation: listEntropySourcesImplementation,\n hookNames,\n};\n\n/**\n * The `snap_listEntropySources` method implementation.\n *\n * @param _request - The JSON-RPC request object. Not used by this function.\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.hasPermission - The function to check if the origin has a\n * permission.\n * @param hooks.getEntropySources - The function to get the entropy sources.\n * @param hooks.getUnlockPromise - The function to get the unlock promise.\n * @returns Noting.\n */\nasync function listEntropySourcesImplementation(\n _request: JsonRpcRequest<ListEntropySourcesParams>,\n response: PendingJsonRpcResponse<ListEntropySourcesResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n hasPermission,\n getEntropySources,\n getUnlockPromise,\n }: ListEntropySourcesHooks,\n): Promise<void> {\n const isPermitted = REQUIRED_PERMISSIONS.some(hasPermission);\n if (!isPermitted) {\n return end(providerErrors.unauthorized());\n }\n\n await getUnlockPromise(true);\n\n response.result = getEntropySources();\n return end();\n}\n"]}