Giter Club home page Giter Club logo

bip39's Introduction

Download Build Status codecov

Read all about how I wrote this and understanding BIP39 here.

Apart from generating a seed, only English, French, Spanish and Japanese currently packaged, but as WordList is an interface and you can provide your own.

Install

Use either of these repositories:

repositories {
    jcenter()
}

Or:

repositories {
    maven {
        url 'https://dl.bintray.com/novacrypto/BIP/'
    }
}

Add dependency:

dependencies {
    compile 'io.github.novacrypto:BIP39:2019.01.27'
}

Usage

Generate a mnemonic

Using a StringBuilder:

StringBuilder sb = new StringBuilder();
byte[] entropy = new byte[Words.TWELVE.byteLength()];
new SecureRandom().nextBytes(entropy);
new MnemonicGenerator(English.INSTANCE)
    .createMnemonic(entropy, sb::append);
System.out.println(sb.toString());

If you're paranoid and/or need higher than normal memory security, consider using a SecureCharBuffer:

try (SecureCharBuffer secure = new SecureCharBuffer()) {
    byte[] entropy = new byte[Words.TWELVE.byteLength()];
    new SecureRandom().nextBytes(entropy);
    new MnemonicGenerator(English.INSTANCE)
        .createMnemonic(entropy, secure::append);
    Arrays.fill(entropy, (byte) 0); //empty entropy
    //do something with your secure mnemonic
}

Validate a mnemonic

try {
    MnemonicValidator
        .ofWordList(English.INSTANCE)
        .validate(mnemonic);
} catch (UnexpectedWhiteSpaceException e) {
   ...
} catch (InvalidWordCountException e) {
    ...
} catch (InvalidChecksumException e) {
     ...
} catch (WordNotFoundException e) {
    ...
    //e.getSuggestion1()
    //e.getSuggestion2()
}

Or if you have a list of words from a word list:

MnemonicValidator
        .ofWordList(English.INSTANCE)
        .validate(mnemonicWordsInAList);

Generate a seed

As does not use a word list, can be used now for any language.

byte[] seed = new SeedCalculator().calculateSeed(mnemonic, passphrase);

Or if you have a list of words from a word list:

byte[] seed = new SeedCalculator()
                     .withWordsFromWordList(English.INSTANCE)
                     .calculateSeed(mnemonicWordsInAList, passphrase);

Note: it will work for words off of the word list, but it allows use of secure CharSequences if they match the wordlist, normalized or not (as they are never toStringed)

Those examples both use SpongyCastle, if you don't need or want that dependency, you can use javax.crypto like so:

byte[] seed = new SeedCalculator(JavaxPBKDF2WithHmacSHA512.INSTANCE).calculateSeed(mnemonic, passphrase);

That will not work on Android API < 26 https://developer.android.com/reference/javax/crypto/SecretKeyFactory.html and see Issue #17.

bip39's People

Contributors

diomchen avatar westonal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.