Skip to content

Commit

Permalink
refactor: single class
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Sep 19, 2024
1 parent 543d6c4 commit 9f10bd8
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions catalyst_voices/lib/common/bip39/seed_phrase.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:bip39/bip39.dart' as bip39;

/// Represents a seed phrase consisting of a mnemonic and its associated seed.
Expand All @@ -9,53 +9,65 @@ import 'package:bip39/bip39.dart' as bip39;
/// cryptocurrency wallets or secure key management.
class SeedPhrase {
final String mnemonic;
final String seed;

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

SeedPhrase.fromUint8ListEntropy(Uint8List encodedData)
: this.fromHexEntropy(_uint8ListToHex(encodedData));

SeedPhrase.fromHexEntropy(String encodedData)
: this.fromMnemonic(bip39.entropyToMnemonic(encodedData));

/// Creates a SeedPhrase from an existing [mnemonic].
///
/// Throws an [ArgumentError] if the mnemonic is invalid.
///
/// [mnemonic]: The mnemonic to derive the seed from.
SeedPhrase.fromMnemonic(this.mnemonic)
: seed = bip39.mnemonicToSeedHex(mnemonic);
: assert(bip39.validateMnemonic(mnemonic), 'Invalid mnemonic phrase');

/// Returns the seed hex string associated with the mnemonic.
String getSeed() {
return seed;
Uint8List getUint8ListSeed() {
return bip39.mnemonicToSeed(mnemonic);
}

String getHexSeed() {
return bip39.mnemonicToSeedHex(mnemonic);
}

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

class SeedPhraseSerialisation {
// Encode the seed phrase into a JSON string
static String encode(SeedPhrase seedPhrase) {
final data = {
'mnemonic': seedPhrase.getMnemonic(),
'seed': seedPhrase.getSeed(),
};
return jsonEncode(data);
Uint8List toUint8ListEntropy() {
return _hexStringToUint8List(toHexEntropy());
}

// Decode the JSON string back into a SeedPhrase object
static SeedPhrase decode(String encodedData) {
final Map<String, dynamic> data = jsonDecode(encodedData);
final String mnemonic = data['mnemonic'];
return SeedPhrase.fromMnemonic(mnemonic);
String toHexEntropy() {
return bip39.mnemonicToEntropy(mnemonic);
}
}

class SeedPhraseFactory {
// Generate a new SeedPhrase
static SeedPhrase generateNew() {
return SeedPhrase();
String _uint8ListToHex(Uint8List encodedData) {
final hexString = StringBuffer();
for (final byte in encodedData) {
hexString.write(byte.toRadixString(16).padLeft(2, '0'));
}
return hexString.toString();
}

// Create a SeedPhrase from an existing mnemonic
static SeedPhrase fromMnemonic(String mnemonic) {
return SeedPhrase.fromMnemonic(mnemonic);
Uint8List _hexStringToUint8List(String hex) {
if (hex.length % 2 != 0) {
throw ArgumentError('Hex string must have an even length.');
}

return Uint8List.fromList(
List.generate(
hex.length ~/ 2,
(i) => int.parse(hex.substring(i * 2, i * 2 + 2), radix: 16),
),
);
}

0 comments on commit 9f10bd8

Please sign in to comment.