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

encrypt wallet with Mnemonic Spanish language #685

Closed
elranu opened this issue Dec 11, 2019 · 20 comments
Closed

encrypt wallet with Mnemonic Spanish language #685

elranu opened this issue Dec 11, 2019 · 20 comments
Assignees
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@elranu
Copy link

elranu commented Dec 11, 2019

Hi,
I'm trying to encrypt a wallet created with Mnemonic in Spanish and I get some errors. The strange thing is that if I do it in English works perfect.
First I have tried the following code:

import { ethers } from 'ethers';
import { es } from 'ethers/wordlists';

private async all(){
    const randomBytes = ethers.utils.randomBytes(24);
    const mnemonic =  ethers.utils.HDNode.entropyToMnemonic(randomBytes, es);
    const wallet = ethers.Wallet.fromMnemonic(mnemonic, null, es);
    const json = await wallet.encrypt("123");
    return json;
  }

And I get the following error:

ERROR Error: Uncaught (in promise): Error: entropy and mnemonic mismatch
Error: entropy and mnemonic mismatch
    at Object.r.encrypt (ethers.min.js:1)
    at _.encrypt (ethers.min.js:1)
    at WalletService.<anonymous> (wallet.service.ts:81)
    at Generator.next (<anonymous>)
    at tslib.es6.js:73
    at new ZoneAwarePromise (zone-evergreen.js:876)
    at Module.__awaiter (tslib.es6.js:69)
    at WalletService.all (wallet.service.ts:76)
    at WalletService.<anonymous> (wallet.service.ts:46)
    at Generator.next (<anonymous>)
    at resolvePromise (zone-evergreen.js:797)
    at zone-evergreen.js:707
    at rejected (tslib.es6.js:71)
    at ZoneDelegate.invoke (zone-evergreen.js:359)
    at Object.onInvoke (core.js:39699)
    at ZoneDelegate.invoke (zone-evergreen.js:358)
    at Zone.run (zone-evergreen.js:124)
    at zone-evergreen.js:855
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:39680)

The second code I have tried is:

 private async all(){
    const randomBytes = ethers.utils.randomBytes(24);
    const mnemonic =  ethers.utils.HDNode.entropyToMnemonic(randomBytes, es);
    const entropy = ethers.utils.HDNode.mnemonicToEntropy(mnemonic, es);
    const wallet = ethers.Wallet.fromMnemonic(mnemonic, null, es);
    const json = await wallet.encrypt("123", {entropy: entropy}); 
    return json;
  }

and I get the following error:

ERROR Error: Uncaught (in promise): Error: entropy and mnemonic mismatch
Error: entropy and mnemonic mismatch
    at Object.r.encrypt (ethers.min.js:1)
    at _.encrypt (ethers.min.js:1)
    at WalletService.<anonymous> (wallet.service.ts:81)
    at Generator.next (<anonymous>)
    at tslib.es6.js:73
    at new ZoneAwarePromise (zone-evergreen.js:876)
    at Module.__awaiter (tslib.es6.js:69)
    at WalletService.all (wallet.service.ts:76)
    at WalletService.<anonymous> (wallet.service.ts:46)
    at Generator.next (<anonymous>)
    at resolvePromise (zone-evergreen.js:797)
    at zone-evergreen.js:707
    at rejected (tslib.es6.js:71)
    at ZoneDelegate.invoke (zone-evergreen.js:359)
    at Object.onInvoke (core.js:39699)
    at ZoneDelegate.invoke (zone-evergreen.js:358)
    at Zone.run (zone-evergreen.js:124)
    at zone-evergreen.js:855
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:39680)

What I'm doing wrong???
Thanks in advance :)

@ricmoo ricmoo added the investigate Under investigation and may be a bug. label Dec 14, 2019
@ricmoo
Copy link
Member

ricmoo commented Dec 14, 2019

I have a feeling this may be a bug, but I'll need to investigate. Your code looks correct to me.

I'll investigate tomorrow as today has been 10 hours of writing documentation and my brain is mushy. :)

@ricmoo ricmoo added bug Verified to be an issue. and removed investigate Under investigation and may be a bug. labels Dec 15, 2019
@ricmoo
Copy link
Member

ricmoo commented Dec 15, 2019

I've confirmed this is an issue in v4 and v5. I am trying to figure out a backwards-compatible fix that is safe.

Since v5 is not officially released yet, backwards compatibility isn't as important, so I may fix it there first to try out a few ideas.

I'll keep this ticket updated and sorry for the inconvenience.

@elranu
Copy link
Author

elranu commented Dec 16, 2019

Ok, Thank you!

@elranu
Copy link
Author

elranu commented Jan 16, 2020

Hi any news? Can I help you with something, a PR? just give me explain me where to touch I'll do it.

@ricmoo
Copy link
Member

ricmoo commented Jan 16, 2020

Hiya!

Sorry for the delay. There is not a clean way to solve it in v4 without breaking backwards compatibility, but there is a way I can fix it that is a bit cumbersome to use, but will work. I'll put that into v4 and solve it more completely in v5.

Are you using v4 or v5?

@elranu
Copy link
Author

elranu commented Jan 17, 2020

Hi,
I'm using version 4. But I'm in time to change versions, how stable is version 5? Do yo have an estimated for the release?

@ricmoo
Copy link
Member

ricmoo commented Jan 17, 2020

I was hoping to have a stable release this month, but it is looking like it will likely be next month...

ricmoo added a commit that referenced this issue Jan 19, 2020
@ricmoo ricmoo added the on-deck This Enhancement or Bug is currently being worked on. label Feb 2, 2020
@ricmoo ricmoo self-assigned this Feb 2, 2020
@elranu
Copy link
Author

elranu commented Feb 13, 2020

Hi,
Sorry for asking you again. I know that you ar fixing it on v5, but of 4 will be a way to solve it meanwhile? Because you mentioned a cumbersome fix, so if you can share that code it will be great for me.

@ricmoo
Copy link
Member

ricmoo commented Feb 13, 2020

Yeah, I will probably put the non-ideal fix in for v4. In v5 I've already abstracted the mnemonic out so it will work better. I just haven't put the mnemonic locale into the x-ethers extension for JSON wallets yet. Sorry for the delay, I'll get something for you soon. It may be some code you can cut and paste, but it will be something. :)

@elranu
Copy link
Author

elranu commented Feb 14, 2020

great! thank you very much!! :) I'll wait :)

@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2020

So, a few options...

First of all, v5 fully supports locales in the x-ethers field for import and export (and unrecognized locales will silently drop the mnemonic on import).

For v4, I've made a few changes that should "help", but it is not an ideal solution and may not cover all the cases you need. I don't know how much better this can get though, without introducing fairly intrusive changes to the API. Let me know what you think.

First, the mnemonic (if non-English), will need the wordlist passed into the call to encrypt. This will embed the mnemonic into the x-ethers metadata along with the locale tag, which means it will decrypt and work fine in v5.

However, when decrypting the wallet in v4, if the language is non-English, the mnemonic will not be included in the Wallet object. Again, in v5 everything will work fine (regardless of whether the JSON was generated in v4 or v5).

Here is an example:

(async function(){
    const randomBytes = ethers.utils.randomBytes(24);
    const mnemonic =  ethers.utils.HDNode.entropyToMnemonic(randomBytes, es);
    const wallet = ethers.Wallet.fromMnemonic(mnemonic, null, es);
    console.log("WALLET", wallet, mnemonic);

    // Here we need to pass in the wordlist -------------v
    const json = await wallet.encrypt("123", { wordlist: es });
    console.log("JSON", json);
    // Includes the encrypted mnemonic and the locale "es", which v5 can understand just fine

    const wallet2 = await ethers.Wallet.fromEncryptedJson(json, "123");
    console.log("WALLET2", wallet2);
    // No mnemonic in v4... :'(
})();

The tests are running right now, and I'll publish them once they pass and update this issue.

@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2020

This has been published to 4.0.45. Let me know if it works for you. :)

@elranu
Copy link
Author

elranu commented Feb 18, 2020

Thanks! I just tried on 4.0.45 and works good :) thank you very much for your work!

@elranu
Copy link
Author

elranu commented Feb 18, 2020

You will close the issue, right?

@ricmoo
Copy link
Member

ricmoo commented Feb 18, 2020

Yupp! Glad it worked. Let me know if there is any problem.

Thanks! :)

@ricmoo ricmoo closed this as completed Feb 18, 2020
@ricmoo ricmoo removed the on-deck This Enhancement or Bug is currently being worked on. label Feb 18, 2020
@ricmoo ricmoo added the fixed/complete This Bug is fixed or Enhancement is complete and published. label Apr 24, 2020
@elranu
Copy link
Author

elranu commented Jun 3, 2020

Hi @ricmoo
the fix you did to support correctly other languages mnemonic phrases works good on web. But when I tried to move it to React Native. I get an error on the marked line below:

const randomBytes = ethers.utils.randomBytes(24);
const mnemonic =  ethers.utils.HDNode.entropyToMnemonic(randomBytes, es);
const wallet = ethers.Wallet.fromMnemonic(mnemonic, null, es); //<--- here is the problem

If I turn it to English, everything works ok. So, something in the new process is not working correctly on RN. Below is the error I get on RN:

2020-06-01 17:08:36.172 29646-29752/com.sabrawallet A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 29752 (mqt_js)
2020-06-01 17:08:38.339 29646-29646/com.sabrawallet I/Choreographer: Skipped 129 frames!  The application may be doing too much work on its main thread. 

Any idea what could be? or something I could try?

Regards,
Mariano

@ricmoo
Copy link
Member

ricmoo commented Jun 4, 2020

I have no idea what that error means. Maybe there is more info in the react documentation?

Also, where are you getting the es from? If you are using the dist files, make sure you are using the -all variant which includes all languages. The normal dist file only includes English. Similar things may happen if you use the browser mainField in rollup. Do a console.log on the 'es` object to make sure it is a Wordlist object...

Let me know. :)

michaeltout pushed a commit to michaeltout/ethers.js that referenced this issue Aug 23, 2020
@reallife01
Copy link

Hi there can anybody help me to solve this issue i have been running into since i have run it many times it does not work i even try change the language to English it still doesn't work any help you can render for me for me

let passPhrase = new Mnemonic(Mnemonic.Words.ENGLISH);
^

TypeError: Cannot read properties of undefined (reading 'ENGLISH')
at createHDWallet (C:\Users\USER\OneDrive\Desktop\dltreact-course\test\src\wallet.bitcoin.js:21:48)
at Object. (C:\Users\USER\OneDrive\Desktop\dltreact-course\test\src\app.js:13:13)
at Module._compile (node:internal/modules/cjs/loader:1246:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1300:10)
at Module.load (node:internal/modules/cjs/loader:1103:32)
at Module._load (node:internal/modules/cjs/loader:942:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47

Node.js v19.5.0

@ricmoo
Copy link
Member

ricmoo commented May 10, 2023

@reallife01 What are you trying to do? There is no property Mnemonic.Words. And nothing called ENGLISH. If you are trying to access the English word list, it is ethers.wordlists.en. Everything in ethers uses the ISO names.

@reallife01
Copy link

reallife01 commented May 10, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

3 participants