UNPKG

@polkadot/x-randomvalues

Version:

A cross-environment window.crypto.getRandomValues replacement

26 lines (25 loc) 1.15 kB
import { NativeModules } from 'react-native'; import { base64Decode } from '@polkadot/wasm-util/base64'; import { xglobal } from '@polkadot/x-global'; import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } from './browser.js'; import { insecureRandomValues } from './fallback.js'; export { packageInfo } from './packageInfo.js'; /** * @internal * * A getRandomValues util that detects and uses the available RN * random utiliy generation functions. **/ function getRandomValuesRn(output) { return base64Decode(NativeModules.RNGetRandomValues ? NativeModules.RNGetRandomValues.getRandomBase64(output.length) : NativeModules.ExpoRandom.getRandomBase64String(output.length), output); } export const getRandomValues = ((typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function') ? getRandomValuesBrowser : (typeof xglobal['nativeCallSyncHook'] === 'undefined' || !NativeModules['ExpoRandom']) ? insecureRandomValues : getRandomValuesRn); export const crypto = (getRandomValues === getRandomValuesBrowser ? cryptoBrowser : { getRandomValues });