From b32c7c4a6d5acecab0e9f7473fef5af3fdcb6eea Mon Sep 17 00:00:00 2001 From: Mohamed Moussa Date: Sat, 26 Oct 2024 10:26:17 -0400 Subject: [PATCH] Match getRandomValues to spec --- src/index.tsx | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index d678ea6..3504b06 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,16 @@ import { NativeModules, Platform } from 'react-native'; +type TypedArray = + | Int8Array + | Uint8Array + | Uint8ClampedArray + | Int16Array + | Uint16Array + | Int32Array + | Uint32Array + | BigInt64Array + | BigUint64Array; + const LINKING_ERROR = `The package 'react-native-random-values-jsi-helper' doesn't seem to be linked. Make sure: \n\n` + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + @@ -25,7 +36,11 @@ if (typeof global.crypto !== 'object') { global.crypto = {}; } // @ts-expect-error -global.crypto.getRandomValues = (array: ArrayBuffer) => { +global.crypto.getRandomValues = (array: TypedArray) => { // @ts-expect-error - return global.getRandomValues(array.byteLength); + const values = global.getRandomValues(array.byteLength); + array.forEach((_, i: number) => { + array[i] = values[i]; + }); + return values; };