@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
64 lines • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEntropyBuilder = exports.GetEntropyArgsStruct = 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_getEntropy';
exports.GetEntropyArgsStruct = (0, superstruct_1.object)({
version: (0, superstruct_1.literal)(1),
salt: (0, superstruct_1.optional)((0, superstruct_1.string)()),
source: (0, superstruct_1.optional)((0, superstruct_1.string)()),
});
const specificationBuilder = ({ allowedCaveats = null, methodHooks, }) => {
return {
permissionType: permission_controller_1.PermissionType.RestrictedMethod,
targetName,
allowedCaveats,
methodImplementation: getEntropyImplementation(methodHooks),
subjectTypes: [permission_controller_1.SubjectType.Snap],
};
};
const methodHooks = {
getMnemonicSeed: true,
getUnlockPromise: true,
getClientCryptography: true,
};
exports.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;
(0, utils_1.assertStruct)(params, exports.GetEntropyArgsStruct, 'Invalid "snap_getEntropy" parameters', rpc_errors_1.rpcErrors.invalidParams);
await getUnlockPromise(true);
const seed = await (0, utils_2.getValueFromEntropySource)(getMnemonicSeed, params.source);
return (0, utils_2.deriveEntropyFromSeed)({
input: origin,
salt: params.salt,
seed,
magic: snaps_utils_1.SIP_6_MAGIC_VALUE,
cryptographicFunctions: getClientCryptography(),
});
};
}
//# sourceMappingURL=getEntropy.cjs.map