@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
61 lines • 2.41 kB
JavaScript
import { PermissionType, SubjectType } from "@metamask/permission-controller";
import { rpcErrors } from "@metamask/rpc-errors";
import { SIP_6_MAGIC_VALUE } from "@metamask/snaps-utils";
import { literal, object, optional, string } from "@metamask/superstruct";
import { assertStruct } from "@metamask/utils";
import { getValueFromEntropySource, deriveEntropyFromSeed } from "../utils.mjs";
const targetName = 'snap_getEntropy';
export const GetEntropyArgsStruct = object({
version: literal(1),
salt: optional(string()),
source: optional(string()),
});
const specificationBuilder = ({ allowedCaveats = null, methodHooks, }) => {
return {
permissionType: PermissionType.RestrictedMethod,
targetName,
allowedCaveats,
methodImplementation: getEntropyImplementation(methodHooks),
subjectTypes: [SubjectType.Snap],
};
};
const methodHooks = {
getMnemonicSeed: true,
getUnlockPromise: true,
getClientCryptography: true,
};
export const getEntropyBuilder = Object.freeze({
targetName,
specificationBuilder,
methodHooks,
});
/**
* Builds the method implementation for `snap_getEntropy`. The implementation
* is based on the reference implementation of
* [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
*
* @param hooks - The RPC method hooks.
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed
* of the user.
* @param hooks.getUnlockPromise - The method to get a promise that resolves
* once the extension is unlocked.
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
* functions to use for the client.
* @returns The method implementation.
*/
function getEntropyImplementation({ getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
return async function getEntropy(options) {
const { params, context: { origin }, } = options;
assertStruct(params, GetEntropyArgsStruct, 'Invalid "snap_getEntropy" parameters', rpcErrors.invalidParams);
await getUnlockPromise(true);
const seed = await getValueFromEntropySource(getMnemonicSeed, params.source);
return deriveEntropyFromSeed({
input: origin,
salt: params.salt,
seed,
magic: SIP_6_MAGIC_VALUE,
cryptographicFunctions: getClientCryptography(),
});
};
}
//# sourceMappingURL=getEntropy.mjs.map