| 1 | import "package:bip39/bip39.dart" as bip39; |
| 2 | |
| 3 | class DecredMnemonicIsIncorrectException implements Exception { |
| 4 | @override |
| 5 | String toString() => |
| 6 | "Decred mnemonic has incorrect format. Mnemonic should be a valid BIP39 seed (12 or 24 words) or a native Decred seed (15 words)."; |
| 7 | } |
| 8 | |
| 9 | bool isValidDecredMnemonic(String mnemonic, {bool allowNativeSeed = true}) { |
| 10 | if (bip39.validateMnemonic(mnemonic)) { |
| 11 | return true; |
| 12 | } |
| 13 | if (!allowNativeSeed) { |
| 14 | return false; |
| 15 | } |
| 16 | final wordCount = mnemonic.trim().split(RegExp(r"\s+")).where((word) => word.isNotEmpty).length; |
| 17 | return wordCount == 15; |
| 18 | } |
| 19 | |
| 20 | void validateDecredMnemonic(String mnemonic, {bool allowNativeSeed = true}) { |
| 21 | if (!isValidDecredMnemonic(mnemonic, allowNativeSeed: allowNativeSeed)) { |
| 22 | throw DecredMnemonicIsIncorrectException(); |
| 23 | } |
| 24 | } |