UNPKG

@polkadot/x-randomvalues

Version:

A cross-environment window.crypto.getRandomValues replacement

30 lines (29 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.crypto = exports.getRandomValues = exports.packageInfo = void 0; const react_native_1 = require("react-native"); const base64_1 = require("@polkadot/wasm-util/base64"); const x_global_1 = require("@polkadot/x-global"); const browser_js_1 = require("./browser.js"); const fallback_js_1 = require("./fallback.js"); var packageInfo_js_1 = require("./packageInfo.js"); Object.defineProperty(exports, "packageInfo", { enumerable: true, get: function () { return packageInfo_js_1.packageInfo; } }); /** * @internal * * A getRandomValues util that detects and uses the available RN * random utiliy generation functions. **/ function getRandomValuesRn(output) { return (0, base64_1.base64Decode)(react_native_1.NativeModules.RNGetRandomValues ? react_native_1.NativeModules.RNGetRandomValues.getRandomBase64(output.length) : react_native_1.NativeModules.ExpoRandom.getRandomBase64String(output.length), output); } exports.getRandomValues = ((typeof x_global_1.xglobal.crypto === 'object' && typeof x_global_1.xglobal.crypto.getRandomValues === 'function') ? browser_js_1.getRandomValues : (typeof x_global_1.xglobal['nativeCallSyncHook'] === 'undefined' || !react_native_1.NativeModules['ExpoRandom']) ? fallback_js_1.insecureRandomValues : getRandomValuesRn); exports.crypto = (exports.getRandomValues === browser_js_1.getRandomValues ? browser_js_1.crypto : { getRandomValues: exports.getRandomValues });