@metamask/snaps-jest
Version:
A Jest preset for end-to-end testing MetaMask Snaps, including a Jest environment, and a set of Jest matchers
50 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getScopesFromAssets = exports.getPseudoRandomUuidGenerator = void 0;
const utils_1 = require("@metamask/utils");
const crypto_1 = require("crypto");
/**
* Get a function that generates pseudo-random UUIDs. This function uses a
* counter to generate a unique UUID each time it is called.
*
* This is likely not suitable for production use, as it does not guarantee
* true randomness and is not cryptographically secure. It is intended for
* testing and mock purposes only.
*
* @returns A function that generates a pseudo-random UUID.
*/
function getPseudoRandomUuidGenerator() {
let counter = 0;
return () => {
const bytes = (0, crypto_1.createHash)('sha256')
.update(`${counter}`)
.digest()
.subarray(0, 16);
/* eslint-disable no-bitwise */
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
/* eslint-enable no-bitwise */
counter += 1;
return (bytes
.toString('hex')
.match(/.{1,8}/gu)
?.join('-') ?? '');
};
}
exports.getPseudoRandomUuidGenerator = getPseudoRandomUuidGenerator;
/**
* Get unique scopes from a list of assets. This assumes the assets are in
* CAIP format, where the chain ID is the first part of the asset type.
*
* @param assets - An array of CAIP asset types.
* @returns An array of unique CAIP chain IDs derived from the assets.
*/
function getScopesFromAssets(assets = []) {
const scopes = assets.map((asset) => {
const { chainId } = (0, utils_1.parseCaipAssetType)(asset);
return chainId;
});
return Array.from(new Set(scopes));
}
exports.getScopesFromAssets = getScopesFromAssets;
//# sourceMappingURL=accounts.cjs.map