ebellocchia / bip_utils

Generation of mnemonics, seeds, private/public keys and addresses for different types of cryptocurrencies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Language error when using ElectrumV1MnemonicDecoder

Anynomouss opened this issue · comments

When I try to use the ElectrumV1MnemonicDecoder(my_mnemonic) class, I get the error
TypeError: Language is not an enumerative of Bip39Languages

I also tried with a newly generated mnemonic:

v1_standard_mnemonic = ElectrumV1MnemonicGenerator().FromWordsNumber(ElectrumV1WordsNum.WORDS_NUM_12)
my_mnemonic = v1_standard_mnemonic.ToStr()
ElectrumV1MnemonicDecoder(my_mnemonic)

If I understood the description of the ElectrumV1MnemonicDecoder, feeding it a mnemnonic string should be sufficient.
Am I overlooking something?

Hi, you have to pass the language when constructing the class (default is English) and the mnemonic when you call the Decode function, so:

ElectrumV1MnemonicDecoder().Decode(mnemonic)

With some further testing of round conversions I still encounter issues when converting from a string mnemonic.
The below lines reproduce the error I encountered. When going from a mnemonic object - > string -> seed, I get different seed_bytes then when I directly get the seed from the mnemonic object:

## a) Below lines produces correct seed_bytes
v1_standard_mnemonic = ElectrumV1MnemonicGenerator().FromWordsNumber(ElectrumV1WordsNum.WORDS_NUM_12)
v1_seed_bytes_correct = ElectrumV1SeedGenerator(v1_standard_mnemonic).Generate() #32 bytes long
print("Correct seed bytes: {}".format(v1_seed_bytes_correct))
v1_standard_mnemonic = ElectrumV1.FromSeed(v1_seed_bytes_correct ) # No error

## b) Below line produces a different, shorter seed bytes that trows an error based on the string version of the mnemonic
my_mnemonic = v1_standard_mnemonic.ToStr()
v1_seed_bytes_not_correct = ElectrumV1MnemonicDecoder().Decode(my_mnemonic) #16 bytes long
print("Incorrect seed bytes: {}".format(v1_seed_bytes_not_correct))
v1_standard_mnemonic = ElectrumV1.FromSeed(v1_seed_bytes_not_correct) # Throws error and different seed_bytes, 16 bytes instead of 32 bytes

The last line that use the mnemonic as string as input give the error "ValueError: Invalid private key bytes"

Maybe you have to check again the docs, but what you are doing is wrong. The decoder is the opposite of the encoder:

  • Encoder: entropy bytes -> mnemonic string
  • Decoder: mnemonic string -> entropy bytes

So the decoder gives you back the entropy bytes, which is in fact 16 bytes long, not the seed (it wouldn't make sense to give back the seed). In some mnemonic schemes it may happen that the entropy and seed bytes are the same, but it's not the case of Electrum V1.
This applies to any mnemonic encoder/decoder, e.g. BIP39, Monero and so on.

My bad, I thought entropy_bytes and seed_bytes were the same. I will read up on the documentation.
The proper way to get seed bytes from the mnemonic is:

v1_standard_mnemonic = ElectrumV2MnemonicGenerator(ElectrumV2MnemonicTypes.STANDARD).FromWordsNumber(ElectrumV1WordsNum.WORDS_NUM_12)
v2_standard_mnemonic = v1_standard_mnemonic.FromString(my_mnemonic)
v2_standard_seed_bytes = ElectrumV2SeedGenerator(v1_standard_mnemonic).Generate()