@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
71 lines • 2.94 kB
JavaScript
import { BIP44CoinTypeNode } from "@metamask/key-tree";
import { PermissionType, SubjectType } from "@metamask/permission-controller";
import { rpcErrors } from "@metamask/rpc-errors";
import { SnapCaveatType } from "@metamask/snaps-utils";
import { getValueFromEntropySource } from "../utils.mjs";
const targetName = 'snap_getBip44Entropy';
/**
* The specification builder for the `snap_getBip44Entropy` permission.
* `snap_getBip44Entropy_*` lets the Snap control private keys for a particular
* BIP-32 coin type.
*
* @param options - The specification builder options.
* @param options.methodHooks - The RPC method hooks needed by the method
* implementation.
* @returns The specification for the `snap_getBip44Entropy` permission.
*/
const specificationBuilder = ({ methodHooks }) => {
return {
permissionType: PermissionType.RestrictedMethod,
targetName,
allowedCaveats: [SnapCaveatType.PermittedCoinTypes],
methodImplementation: getBip44EntropyImplementation(methodHooks),
validator: ({ caveats }) => {
if (caveats?.length !== 1 ||
caveats[0].type !== SnapCaveatType.PermittedCoinTypes) {
throw rpcErrors.invalidParams({
message: `Expected a single "${SnapCaveatType.PermittedCoinTypes}" caveat.`,
});
}
},
subjectTypes: [SubjectType.Snap],
};
};
const methodHooks = {
getMnemonicSeed: true,
getUnlockPromise: true,
getClientCryptography: true,
};
export const getBip44EntropyBuilder = Object.freeze({
targetName,
specificationBuilder,
methodHooks,
});
/**
* Builds the method implementation for `snap_getBip44Entropy`.
*
* @param hooks - The RPC method hooks.
* @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 `BIP44CoinTypeNode`.
* @throws If the params are invalid.
*/
export function getBip44EntropyImplementation({ getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
return async function getBip44Entropy(args) {
await getUnlockPromise(true);
// `args.params` is validated by the decorator, so it's safe to assert here.
const params = args.params;
const seed = await getValueFromEntropySource(getMnemonicSeed, params.source);
const node = await BIP44CoinTypeNode.fromSeed({
derivationPath: [seed, `bip32:44'`, `bip32:${params.coinType}'`],
network: 'mainnet',
}, getClientCryptography());
return node.toJSON();
};
}
//# sourceMappingURL=getBip44Entropy.mjs.map