mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-26 06:08:40 +01:00
25 lines
809 B
Dart
25 lines
809 B
Dart
import "package:bip39/bip39.dart" as bip39;
|
|
|
|
class DecredMnemonicIsIncorrectException implements Exception {
|
|
@override
|
|
String toString() =>
|
|
"Decred mnemonic has incorrect format. Mnemonic should be a valid BIP39 seed (12 or 24 words) or a native Decred seed (15 words).";
|
|
}
|
|
|
|
bool isValidDecredMnemonic(String mnemonic, {bool allowNativeSeed = true}) {
|
|
if (bip39.validateMnemonic(mnemonic)) {
|
|
return true;
|
|
}
|
|
if (!allowNativeSeed) {
|
|
return false;
|
|
}
|
|
final wordCount = mnemonic.trim().split(RegExp(r"\s+")).where((word) => word.isNotEmpty).length;
|
|
return wordCount == 15;
|
|
}
|
|
|
|
void validateDecredMnemonic(String mnemonic, {bool allowNativeSeed = true}) {
|
|
if (!isValidDecredMnemonic(mnemonic, allowNativeSeed: allowNativeSeed)) {
|
|
throw DecredMnemonicIsIncorrectException();
|
|
}
|
|
}
|