UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

58 lines 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Rand { constructor() { const noRand = () => { throw new Error('No secure random number generator is available in this environment.'); }; this._rand = noRand; // Assign the function if (typeof self === 'object') { /* eslint-disable-next-line */ if (self.crypto?.getRandomValues) { this._rand = (n) => { const arr = new Uint8Array(n); /* eslint-disable-next-line */ self.crypto.getRandomValues(arr); return [...arr]; }; } /* if (typeof window === 'object') */ else { this._rand = noRand; } } else { try { /* eslint-disable-next-line */ const crypto = require("crypto"); if (typeof crypto.randomBytes === 'function') { this._rand = (n) => [...crypto.randomBytes(n)]; } } catch { this._rand = noRand; } } } generate(len) { return this._rand(len); } } let ayn = null; /** * Generates a sequence of pseudo-random bytes with the given length. * * @param len - The number of bytes to generate * * @returns The generated bytes * * @example * import Random from '@bsv/sdk/primitives/Random' * const bytes = Random(32) // Produces 32 random bytes */ exports.default = (len) => { if (ayn == null) { ayn = new Rand(); } return ayn.generate(len); }; //# sourceMappingURL=Random.js.map