Consensys / eth-lightwallet

Lightweight JS Wallet for Node and the browser
MIT License
1.47k stars 503 forks source link

Uncaught Error: KeyStore: Invalid mnemonic #182

Open DalderupMaurice opened 6 years ago

DalderupMaurice commented 6 years ago

Upgrading from eth-lightwallet 2.5.4 to 3.0.x I am having the issue that I am unable to catch the error whenever a wrong mnemonic is passed to lightwallet.keystore.createVault.

Example:

  lightwallet.keystore.createVault({
    password: password,
    seedPhrase: seed, // Optionally provide a 12-word seed phrase
    hdPathString: "m/44'/60'/0'/0" // Optional custom HD Path String
  }, (err, ks) => {
    // Any code here is NEVER executed when the mnemonic is invalid
    if (err) throw new Error("Wrong password"); // Not triggered on invalid mnemonic
    console.log('test'); // Not triggered on invalid mnemonic
  });

In this example, whenever the password is wrong, the callback does not retrieve an error keystore. It just displays an uncaught error in the console of the browser. I also tried placing a try - catch around it, but it still does not work.

This is the error shown in the browser:

Uncaught Error: KeyStore: Invalid mnemonic
    at KeyStore.init (keystore.js:75)
    at keystore.js:114
    at cb (keystore.js:352)
    at scrypt-async.js:518
    at scrypt-async.js:481
    at run (setImmediate.js:40)
    at runIfPresent (setImmediate.js:69)
    at onGlobalMessage (setImmediate.js:109)
pdanysz commented 6 years ago

I believe there is some kind of problem in:

#FILE: lightwallet.js / Line: 250
KeyStore.prototype.init = function (mnemonic, pwDerivedKey, hdPathString, salt) {
...
    if (!Mnemonic.isValid(mnemonic, Mnemonic.Words.ENGLISH) || words.length !== 12) {
      throw new Error('KeyStore: Invalid mnemonic');
    }

So, I checked and problem probably is on library var Mnemonic = require('bitcore-mnemonic');

I have my own 12-word seed and it fails anytime. And I used this below, and Validation was OK. select scout crash enforce riot rival spring whale hollow radar rule sentence

I still try to find where problem is ;/

pdanysz commented 6 years ago

OK, I can confirm that problem is with "12 word seed" , or I'm doing something wrong with the words.

This seed work:

select scout crash enforce riot rival spring whale hollow radar rule sentence

this not:

select scout crash road riot rival spring whale hollow radar rule sentence

I changed enforce => road, and I got error about wrong mnemonic.

coder5876 commented 6 years ago

@pdanysz Not every 12 words is a valid mnemonic, the mnemonic contains a checksum, so if you just change one word it will not be a valid mnemonic.

pdanysz commented 6 years ago

@christianlundkvist yes, correct, thank you!

DalderupMaurice commented 6 years ago

Well, my problem persists. Whenever an invalid password is matched with the given mnemonic, I am unable to catch the error. That was the essence of this topic, not the generation of a mnemonic :)

abhiramhatolkar commented 6 years ago

Hi @DalderupMaurice , this is due to the fact that init function throws error if invalid mnemonic appears. But that error is not caught by it's calling function viz. createVault function. I have created a pull request for this (https://github.com/ConsenSys/eth-lightwallet/pull/193).

For now, you can import "bitcore-mnemonic" library and prior to calling createVault, you can check if your mnemonic is correct using isValid function of this library.

DalderupMaurice commented 6 years ago

Thanks man, really appreciate the effort :D

360disrupt commented 6 years ago

@christianlundkvist can you explain this:

Not every 12 words is a valid mnemonic, the mnemonic contains a checksum, so if you just change one word it will not be a valid mnemonic.

So I cannot use any 12 words, is there a schema to follow? Are only certain words allowed, is there a good resource for this?

E.g. can I use any combination of those: https://gist.github.com/fogleman/c4a1f69f34c7e8a00da8 https://github.com/sindresorhus/mnemonic-words

I'm not getting this explanation: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki

EDIT: this library makes it easier: https://github.com/ggozad/mnemonic.js/tree/master But I still get invalid mnemonic with the generated seed phrase: cousin,gun,muscle,radio,total,rhythm,written,built,compare,rip,child,knowledge

abhiramhatolkar commented 6 years ago

@360disrupt Not any 12 random words make a correct mnemonic (not at least for Ethereum and Bitcoin chains). You can view the wordlist here. Again, you cannot arrange these words randomly yourself to make a valid mnemonic. This has steps to create one. You can visit https://iancoleman.io/bip39/ to generate mnemonic for yourself. Or include this in your codebase to create wallets. Here you can see that eth-lightwallet uses it as well ;)