This repository provides the following capabilities for English BIP39 mnemonics:
- Convert entropy to a mnemonic sentence
- Convert a mnemonic sentence back to entropy
- Derive a 64-byte seed from a mnemonic and passphrase
- Validate mnemonic structure, word membership, and checksum
- Normalize compatibility input explicitly
- Generate entropy with a secure random source
- Use the implementation from a Java CLI
This repository targets BIP39 only.
- The supported wordlist is English only
- BIP32, derivation paths, address generation, and full wallet UX are out of scope
mnemonicToSeedintentionally does not validate mnemonic semantic correctness by itself
- Java 17
- Maven 3.9.6 or later
The repository includes the Maven Wrapper, so ./mvnw is the preferred way to run builds.
./mvnw -B -ntp testYou can also use the provided Makefile.
make test
make verifyMain targets:
make test- Run unit testsmake verify- Run Spotless, SpotBugs, and testsmake format- Apply Spotless formattingmake lint- Run Spotless and SpotBugs checksmake clean- Remove build outputsmake cli ARGS="..."- Run the CLI
The CLI entry point is src/main/java/com/example/bip39/cli/Bip39Cli.java.
make cli ARGS="help"make cli ARGS="generate --words 12"make cli ARGS="generate --words 24 --show-entropy"make cli ARGS='to-entropy "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"'make cli ARGS='to-seed --passphrase TREZOR "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"'make cli ARGS='validate "abandon ability able"'make cli ARGS=$'normalize " ABANDON\tabandon\nABANDON\rABOUT "'./mvnw -B -ntp -DskipTests package
java -jar target/bip39-java-0.1.0-SNAPSHOT.jar helpThe public API is defined in src/main/java/com/example/bip39/api/Bip39Service.java.
import com.example.bip39.api.Bip39Service;
import com.example.bip39.api.DefaultBip39Service;
import java.util.HexFormat;
Bip39Service service = new DefaultBip39Service();
byte[] entropy = new byte[16];
String mnemonic = service.entropyToMnemonic(entropy);
byte[] roundTrip = service.mnemonicToEntropy(mnemonic);
byte[] seed = service.mnemonicToSeed(mnemonic, "TREZOR");
System.out.println(mnemonic);
System.out.println(HexFormat.of().formatHex(roundTrip));
System.out.println(seed.length);
System.out.println(service.validateMnemonic(mnemonic));mnemonicToEntropy and validateMnemonic are strict.
- They only accept normalized lowercase English mnemonics
- Tabs, newlines, repeated spaces, and leading or trailing spaces fail as-is
- If needed, run the input through the normalization layer first
The compatibility input adapter lives in src/main/java/com/example/bip39/normalize/MnemonicInputNormalizer.java.
It performs the following steps:
- Trim leading and trailing whitespace
- Replace tabs, newlines, and carriage returns with
SPACE - Collapse repeated spaces to a single
SPACE - Apply Unicode NFKD
- Lowercase for the English profile
mnemonicToSeed follows the BIP39 contract.
- It returns 64 bytes
- It uses NFKD + UTF-8 + PBKDF2-HMAC-SHA512
- It does not automatically validate mnemonic semantic correctness
Call validateMnemonic first if your application requires validation before seed derivation.
This repository includes tests for:
- English wordlist and pinned asset integrity
- Bit-level conversion helpers
- Official
entropyToMnemonicvectors - Official
mnemonicToEntropyvectors - Official
mnemonicToSeedvectors - Fixed failure cases and error priority
- Compatibility normalization
- Entropy generation boundaries
- CLI behavior
.
├── src/main/java/com/example/bip39/
│ ├── api/ # Public API
│ ├── bit/ # Bit-level helpers
│ ├── cli/ # CLI
│ ├── crypto/ # SHA-256 / PBKDF2-HMAC-SHA512
│ ├── entropy/ # SecureRandom-based entropy generation
│ ├── error/ # Error codes and exceptions
│ ├── integration/ # Thin UI / external integration helpers
│ ├── model/ # ValidationResult and related types
│ ├── normalize/ # Compatibility normalization
│ ├── parser/ # Strict mnemonic parsing
│ ├── util/ # Constants
│ └── wordlist/ # English wordlist handling
├── src/main/resources/bip39/ # Pinned normative assets
├── src/test/java/com/example/bip39/ # Tests
├── Makefile
├── pom.xml
└── README.md
- Generated mnemonics and seeds are secrets
- Do not log them, screenshot them, or commit them to source control
- All command examples in this README assume Java 17
Apache License 2.0 - see LICENSE.