@zlattice/lattice-js
Version:
Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)
101 lines • 3.68 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.initRNGPool = initRNGPool;
exports.randomBytes = randomBytes;
const DEFAULT_PRNG_POOL_SIZE = 16384;
let prngPool = new Uint8Array(0);
let _syncCrypto;
async function initRNGPool() {
if ('crypto' in globalThis) {
_syncCrypto = globalThis.crypto;
return; // no need to use pooling
}
if (prngPool.length > DEFAULT_PRNG_POOL_SIZE / 2)
return; // there is sufficient number
// we always populate full pool size
// since numbers may be consumed during micro tasks.
// @ts-ignore
if ('wx' in globalThis && 'getRandomValues' in globalThis.wx) {
prngPool = await new Promise(r => {
wx.getRandomValues({
length: DEFAULT_PRNG_POOL_SIZE,
success(res) {
r(new Uint8Array(res.randomValues));
}
});
});
}
else {
// check if node or browser, use webcrypto if available
try {
// node 19+ and browser
if (globalThis.crypto) {
_syncCrypto = globalThis.crypto;
}
else {
// node below 19
const crypto = await Promise.resolve().then(() => __importStar(require(/* webpackIgnore: true */ '../../crypto/index.js')));
_syncCrypto = crypto.webcrypto;
}
const array = new Uint8Array(DEFAULT_PRNG_POOL_SIZE);
_syncCrypto.getRandomValues(array);
prngPool = array;
}
catch (error) {
throw new Error('no available csprng, abort.');
}
}
}
initRNGPool();
function consumePool(length) {
if (prngPool.length > length) {
const prng = prngPool.slice(0, length);
prngPool = prngPool.slice(length);
initRNGPool();
return prng;
}
throw new Error('random number pool is not ready or insufficient, prevent getting too long random values or too often.');
}
function randomBytes(length = 0) {
const array = new Uint8Array(length);
if (_syncCrypto) {
return _syncCrypto.getRandomValues(array);
}
// no sync crypto available, use async pool
const result = consumePool(length);
return result;
}
//# sourceMappingURL=rng.js.map