#bip-39 #bitcoin #cryptography #bip39-rust

bip39-rusty

Bip39 implementation mnemonic system in rust

1 unstable release

new 0.1.0 Jan 23, 2025

#26 in Magic Beans

MIT license

270KB
21K SLoC

BIP-39 Implementation in Rust

This repository contains a custom implementation of the BIP-39 standard in Rust. The implementation allows for the generation of mnemonic phrases based on entropy, checksum, and wordlists. It supports multiple languages and mnemonic types (128-bit and 256-bit entropy). I Developed this to simplify using the bip39 more easily in rust than the current bip39 that i found. The code is easy to understand and well documented, feel free to suggest improvements :)

Features

  • Entropy Generation: Generate random entropy with secure randomness.
  • Checksum Calculation: Append a checksum based on the entropy.
  • Mnemonic Phrase Generation: Convert entropy to mnemonic phrases using predefined wordlists.
  • Language Support: Extendable to multiple languages.

Installation

  1. Clone the repository:

    git clone https://github.com/your-username/bip39-rust.git
    cd bip39-rust
    
  2. Ensure you have Rust installed. If not, install it using rustup:

  3. Build the project:

    cargo build
    
  4. Run the tests:

    cargo test
    

Usage

Here is an example of how to use the library to generate a mnemonic phrase:

use bip39_rusty::{Mnemonic, Language, MnemonicType};

fn main() {
    /*
        Demonstrating the use of the bip39-rusty library to generate a BIP39 mnemonic phrase.

        The `Mnemonic` struct expects:
            - Language (e.g., Language::English)
            - MnemonicType (e.g., Bits128 or Bits256)

        Once created, you can use the following getter method:
            - .mnemonic_phrase() => Returns the generated mnemonic phrase as a Vec<String>.

        Note: If any internal error occurs during mnemonic generation,
              the library will return a default Mnemonic with 256 bits and Language::English type.
    */

    // Create a new mnemonic
    let mnemonic = Mnemonic::new(Language::English, MnemonicType::Bits256);

    // Display the mnemonic phrases
    println!("Generated Mnemonic Phrase: {:?}", mnemonic.mnemonic_phrase());

    // validate the checksum
    let validation_result = mnemonic.validate_checksum();

    match validation_result {
        Ok(value) => {
            println!("Its valid")
        }
        Err(e) => {
            println!("Not valid")
        }
    }

}

Library Structure

Mnemonic

  • Represents a BIP-39 mnemonic phrase.
  • Fields:
    • lang: Language for the wordlist.
    • mnemonic_type: Type of mnemonic.
    • entropy: Generated entropy bytes.
    • checksum: Checksum appended to entropy.
    • mnemonic_phrase: List of words representing the mnemonic phrase.

MnemonicType

  • Enum representing the type of mnemonic:
    • Bits128: 128-bit entropy (12 words).
    • Bits256: 256-bit entropy (24 words).

Language

  • Represents the wordlist language. You can add custom wordlists by extending this module.
pub enum Language {
    ChineseSimplified,
    ChineseTraditional,
    Czech,
    English,
    French,
    Italian,
    Japanese,
    Korean,
    Portuguese,
    Spanish
}

Wordlist Support

The Language module provides predefined wordlists. Currently supported:

  • English
  • Chinese Simplified
  • Chinese Traditional
  • Korean
  • Japanese
  • French
  • Czech
  • Italian
  • Portuguese
  • Spanish

To add more languages, implement the wordlist in the Language module.

Contributing

Contributions are welcome! If you have a feature request, bug report, or want to contribute code, please open an issue or a pull request.

Steps to Contribute

  1. Fork the repository.
  2. Create a feature branch:
    git checkout -b feature-name
    
  3. Commit your changes:
    git commit -m "Add feature-name"
    
  4. Push to your branch:
    git push origin feature-name
    
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.


Feel free to explore, modify, and use this library as per your requirements. Happy coding!

Dependencies

~1–1.7MB
~35K SLoC