-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError:null is not an object(evaluating 'RNRdandomBytes.seed') #23
Comments
I have the same issue, but there's a small typo in the text @leeeverest included in the issue name and description: RNRdandomBytes.seed -> RNRandomBytes.seed |
The advice to get Of course, if your project doesn't need a shim for the const nodelibs = require("node-libs-react-native");
nodelibs.crypto = null;
module.exports = {
resolver: {
extraNodeModules: nodelibs
}
}; |
Perhaps we should remove this polyfill and just mention it in the docs instead of automatically including it since it requires a manual build. This has been my strategy with other modules that require |
@parshap Yep, that seems like a good change to make! |
Sorry for the typo @pcowgill ,i try react-native link ,but it did't work(EXPO bare flow,SDK 36,RN version 0.61.4),using react-native-crypto-js instead,it works,my fork: https://github.com/leeeverest/node-libs-react-native @parshap |
@leeeverest Thanks for the updated. You might want to update the Looks like the package you're using instead, |
No,you are exactly right! @pcowgill ,i need ecdsa-secp256k1 or ed25519 algorithm,but this package didn't have these functions,i got new error in my app:not implemented yet,so sad. |
@leeeverest did you ever get it to work? I'm encountering the same issue. |
If I understand correctly, the I end up a solution making custom The only function I need for my crypto is
// metro.config.js
const nodelibs = require("node-libs-react-native");
nodelibs.crypto = `${__dirname}/src/crypto.js`;
module.exports = {
resolver: {
extraNodeModules: nodelibs,
},
}; // src/crypto.js
'use strict'
import { Buffer } from 'buffer';
const { NativeModules } = require('react-native');
const randomBytes = (size) => {
if (NativeModules.RNGetRandomValues) {
return Buffer(NativeModules.RNGetRandomValues.getRandomBase64(size));
} else {
throw new Error('Native module not found')
}
};
exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = randomBytes; This solves my need for |
Following @quybeans suggestion and code, I was able to patch the
$ yarn add react-native-get-random-values patch-package
diff --git a/node_modules/react-native-crypto/index.js b/node_modules/react-native-crypto/index.js
index f644543..e2ff55a 100644
--- a/node_modules/react-native-crypto/index.js
+++ b/node_modules/react-native-crypto/index.js
@@ -1,7 +1,17 @@
'use strict'
-import { randomBytes } from 'react-native-randombytes'
-exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = randomBytes
+import { Buffer } from 'buffer';
+const { NativeModules } = require('react-native');
+
+const randomBytes = (size) => {
+ if (NativeModules.RNGetRandomValues) {
+ return Buffer(NativeModules.RNGetRandomValues.getRandomBase64(size));
+ } else {
+ throw new Error('Native module not found')
+ }
+};
+
+exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = randomBytes;
// implement window.getRandomValues(), for packages that rely on it
if (typeof window === 'object') {
$ yarn patch-package
"scripts": {
// other scripts
"postinstall": "patch-package"
} |
Here is a package written for Expo projects, which fixed this issue for me. I have been running into this issue for some time now, but following the instructions to define If you are not running an Expo project you could easily apply the same technique to fix this issue. The "magic" can be found in /*global crypto*/
import getRandomValues from './getRandomValues';
class Crypto {
getRandomValues<TArray extends ArrayBufferView>(values: TArray): TArray {
return getRandomValues(values);
}
}
const webCrypto = typeof crypto !== 'undefined' ? crypto : new Crypto();
export default webCrypto;
export function polyfillWebCrypto(): void {
if (typeof crypto === 'undefined') {
Object.defineProperty(window, 'crypto', {
configurable: true,
enumerable: true,
get: () => webCrypto,
});
}
} |
Hey there 👋 I'm running in this error too. Will this been solved one day or should we do the workaround ? |
To add the
Now, you should have successfully added the two libraries to your project. |
I just gave up on using this lib. After some research I found out Expo has a Crypto module that works fine. I recommend using it: https://docs.expo.dev/versions/latest/sdk/crypto My use case was something like this: global.Buffer = require('buffer').Buffer;
import { entropyToMnemonic } from 'bip39';
import * as Crypto from 'expo-crypto';
export function generateMnemonicList(): string[] {
const entropy = Buffer.from(Crypto.getRandomBytes(32));
return entropyToMnemonic(entropy).split(' ');
} |
Error:Requiring module "node_modules\react-native-crypto\index.js",which threw an exception:TypeError:null is not an object(evaluating 'RNRdandomBytes.seed')
The text was updated successfully, but these errors were encountered: