UNPKG

@metamask/snaps-rpc-methods

Version:
108 lines 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listEntropySourcesHandler = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const getBip32Entropy_1 = require("../restricted/getBip32Entropy.cjs"); const getBip32PublicKey_1 = require("../restricted/getBip32PublicKey.cjs"); const getBip44Entropy_1 = require("../restricted/getBip44Entropy.cjs"); const getEntropy_1 = require("../restricted/getEntropy.cjs"); /** * A list of permissions that the requesting origin must have at least one of * in order to call this method. */ const REQUIRED_PERMISSIONS = [ getBip32Entropy_1.getBip32EntropyBuilder.targetName, getBip32PublicKey_1.getBip32PublicKeyBuilder.targetName, getBip44Entropy_1.getBip44EntropyBuilder.targetName, getEntropy_1.getEntropyBuilder.targetName, ]; /** * The keyring type used by HD (mnemonic-based) keyrings. */ const HD_KEYRING_TYPE = 'HD Key Tree'; const hookNames = { getUnlockPromise: true, }; /** * Get a list of entropy sources available to the Snap. The requesting origin * must have at least one of the following permissions to access entropy source * metadata: * * - `snap_getBip32Entropy` * - `snap_getBip32PublicKey` * - `snap_getBip44Entropy` * - `snap_getEntropy` * * @example * ```json name="Manifest" * { * "initialPermissions": { * "snap_getBip32Entropy": {} * } * } * ``` * ```ts name="Usage" * const entropySources = await snap.request({ method: 'snap_listEntropySources' }); * console.log(entropySources); * // Example output: * // [ * // { * // name: 'Mnemonic 1', * // id: 'mnemonic-1', * // type: 'mnemonic', * // primary: true, * // }, * // { * // name: 'Mnemonic 2', * // id: 'mnemonic-2', * // type: 'mnemonic', * // primary: false, * // }, * // ] * ``` */ exports.listEntropySourcesHandler = { implementation: listEntropySourcesImplementation, hookNames, actionNames: [ 'PermissionController:hasPermission', 'KeyringController:getState', ], }; /** * The `snap_listEntropySources` method implementation. * * @param request - The JSON-RPC request object. * @param response - The JSON-RPC response object. * @param _next - The `json-rpc-engine` "next" callback. Not used by this * function. * @param end - The `json-rpc-engine` "end" callback. * @param hooks - The RPC method hooks. * @param hooks.getUnlockPromise - The function to get the unlock promise. * @param messenger - The messenger used to call controller actions. * @returns Nothing. */ async function listEntropySourcesImplementation(request, response, _next, end, { getUnlockPromise }, messenger) { const { origin } = request; const isPermitted = REQUIRED_PERMISSIONS.some((permission) => messenger.call('PermissionController:hasPermission', origin, permission)); if (!isPermitted) { return end(rpc_errors_1.providerErrors.unauthorized()); } await getUnlockPromise(true); const { keyrings } = messenger.call('KeyringController:getState'); response.result = keyrings .map((keyring, index) => { if (keyring.type === HD_KEYRING_TYPE) { return { id: keyring.metadata.id, name: keyring.metadata.name, type: 'mnemonic', primary: index === 0, }; } return null; }) .filter((entropySource) => Boolean(entropySource)); return end(); } //# sourceMappingURL=listEntropySources.cjs.map