mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 05:38:03 +00:00
ec06da0ebc
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
// Copyright 2017-2025 @polkadot/x-randomvalues authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Adapted from https://github.com/LinusU/react-native-get-random-values/blob/85f48393821c23b83b89a8177f56d3a81dc8b733/index.js
|
|
//
|
|
// Copyright (c) 2018, 2020 Linus Unnebäck
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
import { base64Decode } from '@pezkuwi/wasm-util/base64';
|
|
import { xglobal } from '@pezkuwi/x-global';
|
|
|
|
import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } from './browser.js';
|
|
|
|
export { packageInfo } from './packageInfo.js';
|
|
|
|
interface RNExt {
|
|
ExpoRandom: {
|
|
getRandomBase64String: (length: number) => string;
|
|
};
|
|
RNGetRandomValues: {
|
|
getRandomBase64: (length: number) => string;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @internal
|
|
*
|
|
* A getRandomValues util that detects and uses the available RN
|
|
* random utiliy generation functions.
|
|
**/
|
|
function getRandomValuesRn (output: Uint8Array): Uint8Array {
|
|
if (!NativeModules['ExpoRandom'] && !(NativeModules as RNExt).RNGetRandomValues) {
|
|
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues and no React Native secure RNG module is available.');
|
|
}
|
|
|
|
return base64Decode(
|
|
(NativeModules as RNExt).RNGetRandomValues
|
|
? (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length)
|
|
: (NativeModules as RNExt).ExpoRandom.getRandomBase64String(output.length),
|
|
output
|
|
);
|
|
}
|
|
|
|
// Check for native RN modules first (highest priority)
|
|
const hasNativeRNModules = !!NativeModules['ExpoRandom'] || !!(NativeModules as RNExt).RNGetRandomValues;
|
|
const hasNativeCrypto = typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function';
|
|
|
|
export const getRandomValues = (
|
|
hasNativeRNModules
|
|
? getRandomValuesRn
|
|
: hasNativeCrypto
|
|
? getRandomValuesBrowser
|
|
: () => {
|
|
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues.');
|
|
}
|
|
);
|
|
|
|
export const crypto = (
|
|
getRandomValues === getRandomValuesBrowser
|
|
? cryptoBrowser
|
|
: { getRandomValues }
|
|
);
|