Skip to content
Snippets Groups Projects

Spec multilang mnemonic v2s

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by poka
    $ ./main.py
    Mnemonic EN: athlete jealous must predict blush resource develop fuel spawn fossil bronze comfort
    Mnemonic FR: annuel gustatif lucratif océan balcon pétale cuisine évasion risque éteindre biberon chapitre
    Address: 5DyESMW46SkV14WvPWm2N4EW9i1VUtaWkpR2YKPhScJZdVvx
    $ ./main.py "athlete jealous must predict blush resource develop fuel spawn fossil bronze comfort" 
    Detected language: English
    
    Mnemonic (ENGLISH): athlete jealous must predict blush resource develop fuel spawn fossil bronze comfort
    Equivalent mnemonic (FRENCH): annuel gustatif lucratif océan balcon pétale cuisine évasion risque éteindre biberon chapitre
    Address: 5DyESMW46SkV14WvPWm2N4EW9i1VUtaWkpR2YKPhScJZdVvx
    $ ./main.py "annuel gustatif lucratif oce<0301>an balcon pe<0301>tale cuisine e<0301>vasion risque e<0301>teindre biberon chapitre"
    Detected language: French
    
    Mnemonic (FRENCH): annuel gustatif lucratif océan balcon pétale cuisine évasion risque éteindre biberon chapitre
    Equivalent mnemonic (ENGLISH): athlete jealous must predict blush resource develop fuel spawn fossil bronze comfort
    Address: 5DyESMW46SkV14WvPWm2N4EW9i1VUtaWkpR2YKPhScJZdVvx
    Edited
    snippetfile1.txt 2.95 KiB
    #!/usr/bin/env python3
    
    import sys
    from mnemonic import Mnemonic
    from substrateinterface import Keypair
    
    def main():
        if len(sys.argv) > 1:
            # Mnemonic provided as argument
            mnemonic_input = ' '.join(sys.argv[1:])
            detect_language_and_convert(mnemonic_input)
        else:
            # No mnemonic provided, generate one
            generate_and_display_mnemonics()
    
    def generate_and_display_mnemonics():
        # Generate English mnemonic
        mnemo_en = Mnemonic('english')
        mnemonic_en = mnemo_en.generate(strength=128)
    
        # Get word indices
        words_en = mnemonic_en.split()
        indices = [mnemo_en.wordlist.index(word) for word in words_en]
    
        # Get French wordlist
        mnemo_fr = Mnemonic('french')
        words_fr = [mnemo_fr.wordlist[index] for index in indices]
        mnemonic_fr = ' '.join(words_fr)
    
        # Display mnemonics
        print('Mnemonic EN:', mnemonic_en)
        print('Mnemonic FR:', mnemonic_fr)
    
        # Generate seed and wallet from English mnemonic
        keypair = Keypair.create_from_mnemonic(mnemonic_en)
        print('Address:', keypair.ss58_address)
    
    def detect_language_and_convert(mnemonic_input):
        mnemo_en = Mnemonic('english')
        mnemo_fr = Mnemonic('french')
    
        words_input = mnemonic_input.strip().split()
        word_count = len(words_input)
    
        # Check if words are in English or French wordlist
        english_matches = sum(1 for word in words_input if word in mnemo_en.wordlist)
        french_matches = sum(1 for word in words_input if word in mnemo_fr.wordlist)
    
        if english_matches == word_count:
            # All words are in English wordlist
            print('Detected language: English')
            language = 'english'
            mnemo_source = mnemo_en
            mnemo_target = mnemo_fr
            equivalent_language = 'French'
        elif french_matches == word_count:
            # All words are in French wordlist
            print('Detected language: French')
            language = 'french'
            mnemo_source = mnemo_fr
            mnemo_target = mnemo_en
            equivalent_language = 'English'
        else:
            print('Error: Mnemonic words are not all in the same language wordlist')
            sys.exit(1)
    
        # Get word indices
        indices = [mnemo_source.wordlist.index(word) for word in words_input]
        # Get equivalent words in target language
        words_target = [mnemo_target.wordlist[index] for index in indices]
        mnemonic_target = ' '.join(words_target)
    
        # Generate keypair using English mnemonic
        if language == 'english':
            mnemonic_for_keypair = mnemonic_input
        else:
            # Convert French mnemonic back to English for keypair generation
            mnemonic_for_keypair = ' '.join([mnemo_en.wordlist[index] for index in indices])
    
        keypair = Keypair.create_from_mnemonic(mnemonic_for_keypair)
    
        # Display mnemonics and address
        print(f'\nMnemonic ({language.upper()}): {mnemonic_input}')
        print(f'Equivalent mnemonic ({equivalent_language.upper()}): {mnemonic_target}')
        print('Address:', keypair.ss58_address)
    
    if __name__ == '__main__':
        main()
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment