Skip to content

Commit

Permalink
added exception and prompt on importing invalid seed
Browse files Browse the repository at this point in the history
  • Loading branch information
YannMarti committed May 31, 2022
1 parent 7014498 commit 04cdadb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
8 changes: 8 additions & 0 deletions vidaia/lib/utils/exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class InvalidSeedException implements Exception {
final String cause;
InvalidSeedException(this.cause);
@override
String toString() {
return cause;
}
}
40 changes: 36 additions & 4 deletions vidaia/lib/utils/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:thor_request_dart/contract.dart';
import 'package:thor_request_dart/wallet.dart';
import 'package:vidaia/utils/globals.dart' as global;

import 'exceptions.dart';

createNewWallet() async {
//generate mnemonic phrase and save it on local device
List<String> words = Mnemonic.generate();
Expand Down Expand Up @@ -40,8 +42,8 @@ setPriv() async {
}

recoverWalletFromWords(String words) async {
if (!Mnemonic.validate(words.split(' '))){
throw Exception('Invalid Mnemonic phrase');
if (!Mnemonic.validate(words.split(' '))) {
throw InvalidSeedException('Invalid Mnemonic phrase');
}
final storage = FlutterSecureStorage();
await storage.write(key: "mnemonicPhrase", value: words);
Expand Down Expand Up @@ -240,11 +242,41 @@ Future<void> importWallet(BuildContext context) async {
ElevatedButton(
child: Text('Import'),
onPressed: () {
print(_textFieldController.value.text);
recoverWalletFromWords(_textFieldController.value.text);
try {
recoverWalletFromWords(_textFieldController.value.text);
} on InvalidSeedException {
invalidSeedWords(context);
}
},
),
],
);
});
}

invalidSeedWords(BuildContext context) {
// set up the buttons
Widget cancelButton = TextButton(
child: Text("OK"),
onPressed: () {
Navigator.pop(context);
},
);

// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Invalid Seed Words"),
content: Text("Make sure you typed your mnemonic phrase correctly."),
actions: [
cancelButton,
],
);
// show the dialog
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return alert;
},
);
}

0 comments on commit 04cdadb

Please sign in to comment.