Class Mnemonic

java.lang.Object
com.codename1.crypto.hd.Mnemonic

public final class Mnemonic extends Object

BIP-39 mnemonic sentences: the 12 to 24 English words a wallet shows as its backup, and the 64-byte seed Bip32 and Slip10 derive every key from.

String words = Mnemonic.generate(128);           // 12 words
byte[] seed = Mnemonic.toSeed(words, "");        // the empty passphrase
Bip32 account = Bip32.master(seed).derive("m/44'/60'/0'/0/0");

Only the English wordlist is built in, which is what every wallet exports by default.

  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    fromEntropy(byte[] entropy)
    The mnemonic that encodes entropy (16 to 32 bytes, a multiple of 4).
    static String
    generate(int strengthBits)
    A new mnemonic over fresh entropy from the platform CSPRNG.
    static int
    The position of an English word in the BIP-39 list, or -1.
    static boolean
    isValid(String mnemonic)
    Tells whether the text is a mnemonic with known words and a correct checksum.
    static byte[]
    toEntropy(String mnemonic)
    The entropy a mnemonic encodes, after checking every word and the checksum.
    static byte[]
    toSeed(String mnemonic, String passphrase)
    The 64-byte BIP-39 seed: PBKDF2-HMAC-SHA512 over the words, salted with "mnemonic" + passphrase, 2048 iterations.
    static String
    word(int index)
    The English word at index (0 to 2047).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • generate

      public static String generate(int strengthBits)

      A new mnemonic over fresh entropy from the platform CSPRNG.

      Parameters
      • strengthBits: 128, 160, 192, 224 or 256, for 12, 15, 18, 21 or 24 words
    • fromEntropy

      public static String fromEntropy(byte[] entropy)
      The mnemonic that encodes entropy (16 to 32 bytes, a multiple of 4).
    • toEntropy

      public static byte[] toEntropy(String mnemonic)

      The entropy a mnemonic encodes, after checking every word and the checksum.

      Throws
      • IllegalArgumentException: for an unknown word, a word count other than 12, 15, 18, 21 or 24, or a checksum mismatch
    • isValid

      public static boolean isValid(String mnemonic)
      Tells whether the text is a mnemonic with known words and a correct checksum.
    • toSeed

      public static byte[] toSeed(String mnemonic, String passphrase)

      The 64-byte BIP-39 seed: PBKDF2-HMAC-SHA512 over the words, salted with "mnemonic" + passphrase, 2048 iterations. The words are not checked, as the specification asks; call isValid(String) first to reject a mistyped backup.

      BIP-39 normalizes both strings to Unicode NFKD before encoding them as UTF-8. The words always are ASCII and the device class library has no normalizer, so a passphrase outside ASCII must be given already in NFKD form -- a precomposed accent otherwise derives a different wallet than other software does.

      Parameters
      • mnemonic: the words, separated by whitespace

      • passphrase: the optional extra word ("25th word"); null or "" for none

    • indexOf

      public static int indexOf(String word)
      The position of an English word in the BIP-39 list, or -1.
    • word

      public static String word(int index)
      The English word at index (0 to 2047).