Skip to content

Commit

Permalink
Some safety for invalid private key state
Browse files Browse the repository at this point in the history
  • Loading branch information
bbedward committed Mar 8, 2024
1 parent 5f1a062 commit 8f5115f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/appstate_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

import 'package:flutter_nano_ffi/flutter_nano_ffi.dart';
import 'package:kalium_wallet_flutter/model/available_block_explorer.dart';
import 'package:kalium_wallet_flutter/model/wallet.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -352,6 +353,11 @@ class StateContainerState extends State<StateContainer> {
String address;
if (account.index == -1) {
String privateKey = await sl.get<Vault>().getPrivateKey(account.address);
if (!NanoSeeds.isValidSeed(privateKey)) {
// Change account
await sl.get<DBHelper>().changeToDefaultAccount();
return;
}
address = NanoUtil.privateToAddress(privateKey);
} else {
address = NanoUtil.seedToAddress(
Expand Down Expand Up @@ -745,7 +751,13 @@ class StateContainerState extends State<StateContainer> {
Future<String> _getPrivKey() async {
if (selectedAccount.index == -1) {
// TODO - handle priv key not found
return await sl.get<Vault>().getPrivateKey(selectedAccount.address);
String privKey =
await sl.get<Vault>().getPrivateKey(selectedAccount.address);
if (!NanoSeeds.isValidSeed(privKey)) {
// Change account
await sl.get<DBHelper>().changeToDefaultAccount();
return null;
}
}
return NanoUtil.seedToPrivate(
await sl.get<Vault>().getSeed(), selectedAccount.index);
Expand Down
15 changes: 15 additions & 0 deletions lib/model/db/appdb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ ORDER BY
'UPDATE Accounts SET name = ? WHERE id = ?', [name, account.id]);
}

Future<void> changeToDefaultAccount() async {
var dbClient = await db;
return await dbClient.transaction((Transaction txn) async {
await txn.rawUpdate('UPDATE Accounts set selected = 0');
await txn
.rawUpdate('UPDATE Accounts set selected = 1 where acct_index = 0');
// Get access increment count
List<Map> list = await txn
.rawQuery('SELECT max(last_accessed) as last_access FROM Accounts');
await txn.rawUpdate(
'UPDATE Accounts set selected = ?, last_accessed = ? where acct_index = ?',
[1, list[0]["last_access"] + 1, 0]);
});
}

Future<void> changeAccount(Account account) async {
var dbClient = await db;
return await dbClient.transaction((Transaction txn) async {
Expand Down

0 comments on commit 8f5115f

Please sign in to comment.