ethers.js/packages/random/lib/browser.js

30 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bytes_1 = require("@ethersproject/bytes");
2019-08-02 09:10:58 +03:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-15 01:48:48 +03:00
var shuffle_1 = require("./shuffle");
exports.shuffled = shuffle_1.shuffled;
var crypto = global.crypto || global.msCrypto;
if (!crypto || !crypto.getRandomValues) {
2019-08-02 09:10:58 +03:00
logger.warn("WARNING: Missing strong random number source");
2019-05-15 01:48:48 +03:00
crypto = {
getRandomValues: function (buffer) {
2019-08-02 09:10:58 +03:00
return logger.throwError("no secure random source avaialble", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
2019-05-15 01:48:48 +03:00
operation: "crypto.getRandomValues"
});
}
};
}
function randomBytes(length) {
if (length <= 0 || length > 1024 || parseInt(String(length)) != length) {
2019-08-02 09:10:58 +03:00
logger.throwArgumentError("invalid length", "length", length);
2019-05-15 01:48:48 +03:00
}
var result = new Uint8Array(length);
crypto.getRandomValues(result);
return bytes_1.arrayify(result);
}
exports.randomBytes = randomBytes;
;