Skip to content

Commit

Permalink
fix: docs
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Sep 19, 2024
1 parent 9f10bd8 commit 312cf7d
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions catalyst_voices/lib/common/bip39/seed_phrase.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import 'dart:typed_data';
import 'package:bip39/bip39.dart' as bip39;

/// Represents a seed phrase consisting of a mnemonic and its associated seed.
/// This class provides methods for generating a new seed phrase and creating
/// one from an existing mnemonic.
/// Represents a seed phrase consisting of a mnemonic and provides methods for
/// generating and deriving cryptographic data from the mnemonic.
///
/// The seed phrase is used for cryptographic purposes, such as generating
/// cryptocurrency wallets or secure key management.
/// The `SeedPhrase` class allows creation of a seed phrase either randomly,
/// from a given mnemonic, or from entropy data. It supports converting between
/// different formats, including Uint8List and hex strings.
class SeedPhrase {
final String mnemonic;

/// Generates a new seed phrase with a random mnemonic
/// and its corresponding seed.
SeedPhrase() : this.fromMnemonic(bip39.generateMnemonic());

/// Creates a SeedPhrase from an existing [Uint8List] entropy.
///
/// [encodedData]: The entropy data as a Uint8List.
SeedPhrase.fromUint8ListEntropy(Uint8List encodedData)
: this.fromHexEntropy(_uint8ListToHex(encodedData));

/// Creates a SeedPhrase from an existing hex-encoded entropy.
///
/// [encodedData]: The entropy data as a hex string.
SeedPhrase.fromHexEntropy(String encodedData)
: this.fromMnemonic(bip39.entropyToMnemonic(encodedData));

Expand All @@ -28,27 +34,30 @@ class SeedPhrase {
SeedPhrase.fromMnemonic(this.mnemonic)
: assert(bip39.validateMnemonic(mnemonic), 'Invalid mnemonic phrase');

/// Returns the seed hex string associated with the mnemonic.
/// Returns the seed derived from the mnemonic as a Uint8List.
Uint8List getUint8ListSeed() {
return bip39.mnemonicToSeed(mnemonic);
}

/// Returns the seed derived from the mnemonic as a hex-encoded string.
String getHexSeed() {
return bip39.mnemonicToSeedHex(mnemonic);
}

/// Returns the mnemonic phrase.
String getMnemonic() {
return mnemonic;
}

/// Returns the entropy derived from the mnemonic as a Uint8List.
Uint8List toUint8ListEntropy() {
return _hexStringToUint8List(toHexEntropy());
}

/// Returns the entropy derived from the mnemonic as a hex-encoded string.
String toHexEntropy() {
return bip39.mnemonicToEntropy(mnemonic);
}

/// Returns the mnemonic phrase.
String getMnemonic() {
return mnemonic;
}
}

String _uint8ListToHex(Uint8List encodedData) {
Expand Down

0 comments on commit 312cf7d

Please sign in to comment.