Skip to content

Commit

Permalink
Import ad hoc accounts with mnemonic
Browse files Browse the repository at this point in the history
  • Loading branch information
yekta committed Mar 5, 2024
1 parent d353e60 commit cfcedcf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 49 deletions.
7 changes: 6 additions & 1 deletion lib/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ class AppLocalization {
}

String get seedInvalid {
return Intl.message('Seed is Invalid',
return Intl.message('Seed is invalid',
desc: 'intro_seed_invalid', name: 'seedInvalid');
}

String get secretPhraseInvalid {
return Intl.message('Secret phrase is invalid',
desc: 'Secret phrase is invalid message.', name: 'secretPhraseInvalid');
}

String get seedCopied {
return Intl.message(
'Seed Copied to Clipboard\nIt is pasteable for 2 minutes.',
Expand Down
105 changes: 57 additions & 48 deletions lib/ui/accounts/import_external_account_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@ class _ImportExternalAccountSheetState
bool _mnemonicIsValid = false;
String _mnemonicError;

void _importAdHocAccount() async {
// Validations
int selectedIndex = _indexInputController.text.isEmpty
? 0
: int.tryParse(_indexInputController.text);
if (selectedIndex == null || selectedIndex < 0) {
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context).adHocAccountInvalidIndex, context);
return;
}

if (_seedMode && !NanoSeeds.isValidSeed(_seedInputController.text)) {
sl
.get<UIUtil>()
.showSnackbar(AppLocalization.of(context).seedInvalid, context);
return;
}

if (!_seedMode &&
!NanoMnemomics.validateMnemonic(_mnemonicController.text.split(' '))) {
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context).secretPhraseInvalid, context);
return;
}

String seed = _seedMode
? _seedInputController.text
: NanoMnemomics.mnemonicListToSeed(_mnemonicController.text.split(' '));

// Derive private key
String privateKey = NanoUtil.seedToPrivate(seed, selectedIndex);

// See if account exists
bool accountExists = await sl.get<DBHelper>().accountExists(privateKey);
if (accountExists) {
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context).importedAdHocAccountAlreadyExists,
context);
return;
}

// Add account
try {
Account acct = await sl.get<DBHelper>().addAccountWithPrivateKey(
nameBuilder: AppLocalization.of(context).defaultNewAccountNameAdHoc,
privateKey: privateKey);
widget.accountAddedCallback(acct);
Navigator.of(context).pop();
} catch (e) {
sl.get<Logger>().e("Error importing account: $e");
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context).adHocAccountImportedError, context);
}
}

@override
Widget build(BuildContext context) {
// The main column that holds everything
Expand Down Expand Up @@ -535,54 +590,8 @@ class _ImportExternalAccountSheetState
context,
AppButtonType.PRIMARY,
AppLocalization.of(context).import,
Dimens.BUTTON_TOP_DIMENS, onPressed: () async {
// Validations
int selectedIndex = _indexInputController.text.isEmpty
? 0
: int.tryParse(_indexInputController.text);
if (selectedIndex == null || selectedIndex < 0) {
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context)
.adHocAccountInvalidIndex,
context);
return;
}
if (!NanoSeeds.isValidSeed(_seedInputController.text)) {
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context).seedInvalid, context);
return;
}
// Derive private key
String privateKey = NanoUtil.seedToPrivate(
_seedInputController.text, selectedIndex);
// See if account exists
bool accountExists =
await sl.get<DBHelper>().accountExists(privateKey);
if (accountExists) {
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context)
.importedAdHocAccountAlreadyExists,
context);
return;
}
// Add account
try {
Account acct = await sl
.get<DBHelper>()
.addAccountWithPrivateKey(
nameBuilder: AppLocalization.of(context)
.defaultNewAccountNameAdHoc,
privateKey: privateKey);
widget.accountAddedCallback(acct);
Navigator.of(context).pop();
} catch (e) {
sl.get<Logger>().e("Error importing account: $e");
sl.get<UIUtil>().showSnackbar(
AppLocalization.of(context)
.adHocAccountImportedError,
context);
}
}),
Dimens.BUTTON_TOP_DIMENS,
onPressed: _importAdHocAccount),
],
),
Row(
Expand Down

0 comments on commit cfcedcf

Please sign in to comment.