Files
hash-wallet/cw_decred/lib/mnemonic_validation.dart
T
2026-08-26 01:01:52 +08:00

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();
}
}