@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
83 lines • 3.58 kB
JavaScript
import { PermissionType, SubjectType } from "@metamask/permission-controller";
import { rpcErrors } from "@metamask/rpc-errors";
import { SnapCaveatType } from "@metamask/snaps-utils";
import { assert } from "@metamask/utils";
import { getNodeFromMnemonic, getNodeFromSeed, getValueFromEntropySource } from "../utils.mjs";
const targetName = 'snap_getBip32Entropy';
/**
* The specification builder for the `snap_getBip32Entropy` permission.
* `snap_getBip32Entropy` lets the Snap control private 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_getBip32Entropy` permission.
*/
const specificationBuilder = ({ methodHooks }) => {
return {
permissionType: PermissionType.RestrictedMethod,
targetName,
allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],
methodImplementation: getBip32EntropyImplementation(methodHooks),
validator: ({ caveats }) => {
if (caveats?.length !== 1 ||
caveats[0].type !== SnapCaveatType.PermittedDerivationPaths) {
throw rpcErrors.invalidParams({
message: `Expected a single "${SnapCaveatType.PermittedDerivationPaths}" caveat.`,
});
}
},
subjectTypes: [SubjectType.Snap],
};
};
const methodHooks = {
getMnemonic: true,
getMnemonicSeed: true,
getUnlockPromise: true,
getClientCryptography: true,
};
export const getBip32EntropyBuilder = Object.freeze({
targetName,
specificationBuilder,
methodHooks,
});
/**
* Builds the method implementation for `snap_getBip32Entropy`.
*
* @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 `JsonSLIP10Node`.
* @throws If the params are invalid.
*/
export function getBip32EntropyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
return async function getBip32Entropy(args) {
await getUnlockPromise(true);
const { params } = args;
assert(params);
// 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 getValueFromEntropySource(getMnemonicSeed, params.source);
const node = await getNodeFromSeed({
curve: params.curve,
path: params.path,
seed,
cryptographicFunctions: getClientCryptography(),
});
return node.toJSON();
}
const secretRecoveryPhrase = await getValueFromEntropySource(getMnemonic, params.source);
const node = await getNodeFromMnemonic({
curve: params.curve,
path: params.path,
secretRecoveryPhrase,
cryptographicFunctions: getClientCryptography(),
});
return node.toJSON();
};
}
//# sourceMappingURL=getBip32Entropy.mjs.map