-
Notifications
You must be signed in to change notification settings - Fork 41
Description
I've found what looks like a very serious issue where the mnemonics being generated via wasm-crypto-asmjs are generated in a consistent pattern when running on Android in the following environment:
"react-native": "0.81.4",
"expo": "~54.0.13",
"expo-crypto": "~15.0.7",
"react-native-get-random-values": "^1.11.0",
"@polkadot/wasm-crypto-asmjs": "7.4.1",
"@polkadot/util-crypto": "^13.5.4",
I've created a super simple app to showcase the issue: https://github.com/leemcmullen/polkadot-react-native-issue-testapp.
The full description can be found here: https://github.com/leemcmullen/polkadot-react-native-issue-testapp/blob/main/README.md
If you have the above setup, none of which is unusual, then on Android the mnemonics will always be generated in the same order i.e. the 1st will be "issue shove clock draft because sight accident pull torch order quantum fade", the 2nd will be "saddle move hotel donate tell minute patch target smart forum sell model" and so on. When you restart the app, and start the process again, they will be generated in the same order again.
I think I've tracked it down to the fact that wasm-crypto-asmjs only seems to call crypto.getRandomValues once per session if you have expo-crypto installed and that one call results in the following array: {"array": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}. After that, asmjs no longer seems to call crypto.getRandomValues at all until you restart the app. At which point the pattern starts again.
The workaround is easy: pass true as the optional 3rd param to mnemonicGenerate() which then bypasses asmjs, and that works fine. However the default case is to use bip39Generate from wasm-crypto-asmjs, which consistently creates the same outcome.
There is much more detail in the readme of the demo app I linked to above.
Naturally this is VERY BAD if you're relying on mnemonics being random, which is probably what people want 99.9% of the time.
I got as far as I could with trying to track it down but ended up getting lost in wasm-crypto-asmjs/cjs/data.js.
Please let me know if I can help troubleshoot any further.