Skip to content
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

react-native app crash -> ethers.Wallet.createRandom({locale:'ko'}) #1298

Closed
gruming opened this issue Feb 16, 2021 · 33 comments
Closed

react-native app crash -> ethers.Wallet.createRandom({locale:'ko'}) #1298

gruming opened this issue Feb 16, 2021 · 33 comments
Labels
enhancement New feature or improvement. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@gruming
Copy link

gruming commented Feb 16, 2021

Hi

Thanks for making the library

I am developing a wallet and I want to use the mnemonic code in Korean, but it works well in debug mode, but when I exit the debug mode, the app is terminated.

its work !
const wallet = ethers.Wallet.createRandom({locale:'en'});

but its not working...
const wallet = ethers.Wallet.createRandom({locale:'ko'});

What can I do to prevent the app from crashing?

Any help would be thanks.

English used Google Translate.

Thanks

@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2021

You need to pass in the actual wordlist, not the string "ko". Try using Wallet.createRandom({ locale: ethers.wordlists.ko}) instead.

Also make sure you have followed https://docs.ethers.io/v5/cookbook/react-native/ for setting up the rng.

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Feb 16, 2021
@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2021

Actually, I think strings are fine. But only if the wordlist is included. How are you importing the library?

Can you try console.log(ethers.wordlists)?

@gruming
Copy link
Author

gruming commented Feb 17, 2021

i try this

its work!

console.log(ethers.wordlists)

but its not working and the app is terminated.

import "react-native-get-random-values"
import "@ethersproject/shims/dist/index.js";
import { ethers, Wordlist, wordlists } from "ethers";

const wallet = ethers.Wallet.createRandom(wordlists.ko);

i think app crashed here

    static fromMnemonic(mnemonic: string, path?: string, wordlist?: Wordlist): Wallet {
        if (!path) { path = defaultPath; }
        return new Wallet(HDNode.fromMnemonic(mnemonic, null, wordlist).derivePath(path));
    }

@ricmoo
Copy link
Member

ricmoo commented Feb 17, 2021

Can you please include the output of the console.log?

if (ethers.wordlists.ko == null) {
    console.log("No Korean Wordlist", Object.keys(ethers.wordlists));
} else {
    try {
        const wallet = ethers.Wallet.createRandom({ locale: ethers.wordlists.ko });
        console.log("Mnemonic", wallet, wallet.mnemonic);
    } catch (error) {
        console.log("Error", error);
    }
}

Please include all the output. :)

@gruming
Copy link
Author

gruming commented Feb 18, 2021

i include u r codes

if (ethers.wordlists.ko == null) {
    console.log("No Korean Wordlist", Object.keys(ethers.wordlists));
} else {
    try {
        const wallet = ethers.Wallet.createRandom({ locale: ethers.wordlists.ko });
        console.log("Mnemonic", wallet, wallet.mnemonic);
    } catch (error) {
        console.log("Error", error);
    }
}

result

image

no console.log and just app is terminated.

What am I missing?

@ricmoo
Copy link
Member

ricmoo commented Feb 23, 2021

Can you add a stack trace to the console.log fir the error? console.log("Error", error, error.stack)?

@ricmoo
Copy link
Member

ricmoo commented Feb 25, 2021

Did you ever solve this issue?

@gruming
Copy link
Author

gruming commented Mar 1, 2021

We haven't solved it yet, and we are just using the English mnemonic code.

And the app just quits without being able to check the console.log.

@gruming
Copy link
Author

gruming commented Mar 3, 2021

How can the Korean mnemonic code work properly?

Do you have an idea?

If an error appeared on the console, it would have been informed immediately, but the error did not appear on the console, and the app just crashed.

@ricmoo
Copy link
Member

ricmoo commented Mar 4, 2021

The Korean Mnemonics (in Hangul) work fine in ethers in node:

homestead> w = Wallet.createRandom({ locale: "ko" })
Wallet { address: '0x6297b8bF8857EA882f2090D4fd072B20C9C89266', }
homestead> w.mnemonic
{
  phrase: '적당히 갈증 근본 필자 관리 보통 편지 강사 위협 적응 자존심 인구',
  path: "m/44'/60'/0'/0/0",
  locale: 'ko'
}
homestead> m = '적당히 갈증 근본 필자 관리 보통 편지 강사 위협 적응 자존심 인구'
homestead> w2 = Wallet.fromMnemonic(m, null, ethers.wordlists.ko)
Wallet { address: '0x6297b8bF8857EA882f2090D4fd072B20C9C89266', }
homestead> w2.mnemonic
{
  phrase: '적당히 갈증 근본 필자 관리 보통 편지 강사 위협 적응 자존심 인구',
  path: "m/44'/60'/0'/0/0",
  locale: 'ko'
}
homestead> w2.mnemonic.phrase == w.mnemonic.phrase
true

The test cases (which include Korean) also execute in ReactNative during the CI, which generate Korean mnemonics and compare them against known test vectors. The tests are in the iOS simulator. What platform is this on? Have you tried other platforms (iOS vs Android)?

Could there be something else about your configuration? Does your platform work in general on the Hangul character set?

For example, does this work:

console.log("한글");
console.log("한글".normalize("NFKD"));

@gruming
Copy link
Author

gruming commented Mar 4, 2021

I also confirmed that Node has been working well, but when I worked on the actual Android device, the app was dying.

And in the previous comment, only the console you gave up, and the same symptoms have been shown.

console.log("한글"); << working console output
console.log("한글".normalize("NFKD"));  << this app is crashed

So I have the information I found.

https://stackoverflow.com/questions/54602427/is-javascripts-normalize-function-supported-in-react-native

I found the issue in StackOverflow above.

Can I resolve the issue in ethers.js?

@ricmoo
Copy link
Member

ricmoo commented Mar 4, 2021

That’s strange. The shims test the normalize functionality and should automatically inject unorm if they fail.

Can you try one more thing: console.log("test".normalize("NFKD")). I wonder if your version of Android only conditionally fails to normalize...

@ricmoo
Copy link
Member

ricmoo commented Mar 4, 2021

Oh! Also, console.log("".normalize). It should show something like [function].

@gruming
Copy link
Author

gruming commented Mar 9, 2021

i try this

import "react-native-get-random-values";
import "@ethersproject/shims";
import { ethers }                                               from "ethers";

console.log("test".normalize("NFKD"))  << working console output
console.log("한글".normalize("NFKD"))  << this app is crashed

i used unorm and try again

$> npm i unorm
...

import unorm from 'unorm';

console.log("test".normalize("NFKD"))  << working console output
console.log(unorm.nfkd("한글"))  << working console output

@gruming
Copy link
Author

gruming commented Mar 10, 2021

Can u change automatically inject unorm code ??

I want to change this issue and i waiting for you

@ricmoo
Copy link
Member

ricmoo commented Mar 16, 2021

The ethers shims do automatically inject the unorm code, which is why I'm confused why it isn't for you.

What if you inject the unorm code yourself, before importing the ethers shims?

The fact it doesn't mention the normalize in your injected shims, means it is passing this test. I need a lot more info on your platform though before I can figure out why that passed.

Does this cause your app to crash: "test".normalize("NFKD")?

@gruming
Copy link
Author

gruming commented Apr 14, 2021

import "react-native-get-random-values";
import "@ethersproject/shims";
import { ethers }                                               from "ethers";

console.log("test".normalize("NFKD"))  << working console output
console.log("한글".normalize("NFKD"))  << this app is crashed

There("test".normalize("NFKD")) is no error in the code here, so I can't use unorm and just crash the app.

and i dont know "test".normalize("NFKD") this code is ok but "한글".normalize("NFKD") this code not ok.

and
my real device model is SM-f700M android
ethers: "^5.1.0"
react: "17.0.1"
react-native: "0.64.0"

@ricmoo
Copy link
Member

ricmoo commented Apr 14, 2021

Ok. That gives me an idea on how to fix this. I will try getting a new shims out this week.

Thanks for your help and I’ll let you know soon.

@ricmoo ricmoo added enhancement New feature or improvement. on-deck This Enhancement or Bug is currently being worked on. and removed discussion Questions, feedback and general information. labels Apr 14, 2021
@ricmoo
Copy link
Member

ricmoo commented Apr 18, 2021

I've made a change in 5.1.1 that should help. Can you try it out and let me know?

@ricmoo ricmoo added fixed/complete This Bug is fixed or Enhancement is complete and published. and removed on-deck This Enhancement or Bug is currently being worked on. labels Apr 18, 2021
@zemse
Copy link
Collaborator

zemse commented Apr 26, 2021

Hey @gruming, did you get a chance to try ethers@5.1.1?

pull bot pushed a commit to shapeshift/ethers.js that referenced this issue Jun 4, 2021
@s-jain-cognam
Copy link

i try this

its work!

console.log(ethers.wordlists)

but its not working and the app is terminated.

import "react-native-get-random-values"
import "@ethersproject/shims/dist/index.js";
import { ethers, Wordlist, wordlists } from "ethers";

const wallet = ethers.Wallet.createRandom(wordlists.ko);

i think app crashed here

    static fromMnemonic(mnemonic: string, path?: string, wordlist?: Wordlist): Wallet {
        if (!path) { path = defaultPath; }
        return new Wallet(HDNode.fromMnemonic(mnemonic, null, wordlist).derivePath(path));
    }

I am also facing same issue the UI get frozen on fromMnemonic function and it takes around 10 seconds. but when debugger is on it takes just a sec to run the function. please help.

@ricmoo
Copy link
Member

ricmoo commented Jul 22, 2021

@s-jain-cognam What version are you using?

@ricmoo
Copy link
Member

ricmoo commented Jul 22, 2021

Is the app crashing, or just taking a long time to compute?

@s-jain-cognam
Copy link

s-jain-cognam commented Jul 22, 2021

"@ethersproject/shims": "^5.3.0",
ethers: "version": "5.0.23"
"react-native-get-random-values": "^1.5.1",

@s-jain-cognam
Copy link

Is the app crashing, or just taking a long time to compute?

Taking long time to load function... so ui get frozen cause function is syncronus i guess.

@ricmoo
Copy link
Member

ricmoo commented Jul 22, 2021

There is an issue with React when performing certain cryptographic operations. I’m not certain, but believe in this case it is the sha2-512. It’s strange because it’s not all that intensive of an algorithm.

You mean it runs faster when the debugger is active? I have usually seen things are much faster in RN and browsers with the debugger off.

Does the debugger run the JavaScriptCore engine in a different context? Similar to Nitro on iOS? I don’t really have any way to reproduce it, but if you can isolate which operation is being slow, I might be able to provide more context.

@s-jain-cognam
Copy link

Ohh thank you but i think have figured it out since wallet creation uses scrypt algo, which is a memory and CPU intensive algorithm which computes a key (fixed-length pseudo-random series of bytes) for a given password. since my android is low end device so function uses all of ram and frezzes ui completely. but when i connect debugger and see task manager ram of rn-debugger it get from 0 to 20% when this function run. since it have 16gb ram so it get executed very fast. i can see it in task manager .

@ricmoo
Copy link
Member

ricmoo commented Jul 22, 2021

Ah yes. That would be your bottleneck. Scrypt is intentionally slow. If you use my implementation of scrypt, (scrypt-js) it may be a bit slower, but is designed to not block the UI, as it operates as a co-routine, providing event loop operations during each salsa/chacha round. :)

@s-jain-cognam
Copy link

Ah yes. That would be your bottleneck. Scrypt is intentionally slow. If you use my implementation of scrypt, (scrypt-js) it may be a bit slower, but is designed to not block the UI, as it operates as a co-routine, providing event loop operations during each salsa/chacha round. :)

Yes but it is freezing UI somehow.

@ricmoo
Copy link
Member

ricmoo commented Jul 22, 2021

This library is freezing your UI? Maybe I need to make the rounds configurable.

@s-jain-cognam
Copy link

Yes i guess.

@s-jain-cognam
Copy link

Hello again, this mnemonicToSeed which uses pbkdf2 and takes around 10 second to user access wallet when he have passwords for security perpose obviously. but when i create a wallet and connect it to provider. this function runs 3 time which increases time 10 to 30 seconds. why is that? couldn't it converted to one time run from create wallet to connect to provider.

@ricmoo
Copy link
Member

ricmoo commented May 14, 2022

The issue with the Korean (Hangul) wordlists is unfortunately not something that can be solved on Android with broken implementations of normalize. There is no safe way to even detect the issue, since try..catch cannot trap it; the browser simply crashes.

I'm closing this now, and hopefully the user base on platforms still using the broken String implementation on Android dwindles. This issue is over a year old, so maybe it already has. :)

@ricmoo ricmoo closed this as completed May 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

4 participants