UNPKG

@metamask/snaps-rpc-methods

Version:
100 lines 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBip32PublicKeyImplementation = exports.getBip32PublicKeyBuilder = exports.Bip32PublicKeyArgsStruct = void 0; const permission_controller_1 = require("@metamask/permission-controller"); const rpc_errors_1 = require("@metamask/rpc-errors"); const snaps_utils_1 = require("@metamask/snaps-utils"); const superstruct_1 = require("@metamask/superstruct"); const utils_1 = require("@metamask/utils"); const utils_2 = require("../utils.cjs"); const targetName = 'snap_getBip32PublicKey'; exports.Bip32PublicKeyArgsStruct = (0, snaps_utils_1.bip32entropy)((0, superstruct_1.object)({ path: snaps_utils_1.Bip32PathStruct, curve: snaps_utils_1.CurveStruct, compressed: (0, superstruct_1.optional)((0, superstruct_1.boolean)()), source: (0, superstruct_1.optional)((0, superstruct_1.string)()), })); /** * The specification builder for the `snap_getBip32PublicKey` permission. * `snap_getBip32PublicKey` lets the Snap retrieve public keys for a particular * BIP-32 node. * * @param options - The specification builder options. * @param options.methodHooks - The RPC method hooks needed by the method implementation. * @returns The specification for the `snap_getBip32PublicKey` permission. */ const specificationBuilder = ({ methodHooks }) => { return { permissionType: permission_controller_1.PermissionType.RestrictedMethod, targetName, allowedCaveats: [snaps_utils_1.SnapCaveatType.PermittedDerivationPaths], methodImplementation: getBip32PublicKeyImplementation(methodHooks), validator: ({ caveats }) => { if (caveats?.length !== 1 || caveats[0].type !== snaps_utils_1.SnapCaveatType.PermittedDerivationPaths) { throw rpc_errors_1.rpcErrors.invalidParams({ message: `Expected a single "${snaps_utils_1.SnapCaveatType.PermittedDerivationPaths}" caveat.`, }); } }, subjectTypes: [permission_controller_1.SubjectType.Snap], }; }; const methodHooks = { getMnemonic: true, getMnemonicSeed: true, getUnlockPromise: true, getClientCryptography: true, }; exports.getBip32PublicKeyBuilder = Object.freeze({ targetName, specificationBuilder, methodHooks, }); /** * Builds the method implementation for `snap_getBip32PublicKey`. * * @param hooks - The RPC method hooks. * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user. * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user. * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked * and prompts the user to unlock their MetaMask if it is locked. * @param hooks.getClientCryptography - A function to retrieve the cryptographic * functions to use for the client. * @returns The method implementation which returns a public key. * @throws If the params are invalid. */ function getBip32PublicKeyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }) { return async function getBip32PublicKey(args) { await getUnlockPromise(true); (0, utils_1.assertStruct)(args.params, exports.Bip32PublicKeyArgsStruct, 'Invalid BIP-32 public key params', rpc_errors_1.rpcErrors.invalidParams); const { params } = args; // Using the seed is much faster, but we can only do it for these specific curves. if (params.curve === 'secp256k1' || params.curve === 'ed25519') { const seed = await (0, utils_2.getValueFromEntropySource)(getMnemonicSeed, params.source); const node = await (0, utils_2.getNodeFromSeed)({ curve: params.curve, path: params.path, seed, cryptographicFunctions: getClientCryptography(), }); if (params.compressed) { return node.compressedPublicKey; } return node.publicKey; } const secretRecoveryPhrase = await (0, utils_2.getValueFromEntropySource)(getMnemonic, params.source); const node = await (0, utils_2.getNodeFromMnemonic)({ curve: params.curve, path: params.path, secretRecoveryPhrase, cryptographicFunctions: getClientCryptography(), }); if (params.compressed) { return node.compressedPublicKey; } return node.publicKey; }; } exports.getBip32PublicKeyImplementation = getBip32PublicKeyImplementation; //# sourceMappingURL=getBip32PublicKey.cjs.map