2022-11-09 22:43:26 +00:00
|
|
|
import 'package:hive/hive.dart';
|
2022-11-04 19:32:02 +00:00
|
|
|
import 'package:stack_wallet_backup/secure_storage.dart';
|
2023-03-01 21:52:13 +00:00
|
|
|
import 'package:stackwallet/db/hive/db.dart';
|
2022-11-04 19:32:02 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
|
|
|
|
const String _kKeyBlobKey = "swbKeyBlobKeyStringID";
|
2023-01-26 15:18:07 +00:00
|
|
|
const String _kKeyBlobVersionKey = "swbKeyBlobVersionKeyStringID";
|
|
|
|
|
|
|
|
const int kLatestBlobVersion = 2;
|
2022-11-04 19:32:02 +00:00
|
|
|
|
|
|
|
String _getMessageFromException(Object exception) {
|
2023-01-26 15:18:07 +00:00
|
|
|
if (exception is IncorrectPassphraseOrVersion) {
|
2022-11-04 19:32:02 +00:00
|
|
|
return exception.errMsg();
|
|
|
|
}
|
|
|
|
if (exception is BadDecryption) {
|
|
|
|
return exception.errMsg();
|
|
|
|
}
|
|
|
|
if (exception is InvalidLength) {
|
|
|
|
return exception.errMsg();
|
|
|
|
}
|
|
|
|
if (exception is EncodingError) {
|
|
|
|
return exception.errMsg();
|
|
|
|
}
|
2023-01-26 15:18:07 +00:00
|
|
|
if (exception is VersionError) {
|
|
|
|
return exception.errMsg();
|
|
|
|
}
|
2022-11-04 19:32:02 +00:00
|
|
|
|
|
|
|
return exception.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
class DPS {
|
|
|
|
StorageCryptoHandler? _handler;
|
|
|
|
|
|
|
|
StorageCryptoHandler get handler {
|
|
|
|
if (_handler == null) {
|
|
|
|
throw Exception(
|
|
|
|
"DPS: attempted to access handler without proper authentication");
|
|
|
|
}
|
|
|
|
return _handler!;
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:43:26 +00:00
|
|
|
DPS();
|
2022-11-04 19:32:02 +00:00
|
|
|
|
|
|
|
Future<void> initFromNew(String passphrase) async {
|
|
|
|
if (_handler != null) {
|
|
|
|
throw Exception("DPS: attempted to re initialize with new passphrase");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-01-26 15:18:07 +00:00
|
|
|
_handler = await StorageCryptoHandler.fromNewPassphrase(
|
|
|
|
passphrase,
|
|
|
|
kLatestBlobVersion,
|
|
|
|
);
|
2022-11-09 22:43:26 +00:00
|
|
|
|
|
|
|
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
|
|
|
|
await DB.instance.put<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
2022-11-04 19:32:02 +00:00
|
|
|
key: _kKeyBlobKey,
|
|
|
|
value: await _handler!.getKeyBlob(),
|
|
|
|
);
|
2023-01-26 15:18:07 +00:00
|
|
|
await _updateStoredKeyBlobVersion(kLatestBlobVersion);
|
2022-11-09 22:43:26 +00:00
|
|
|
await box.close();
|
2022-11-04 19:32:02 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"${_getMessageFromException(e)}\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initFromExisting(String passphrase) async {
|
|
|
|
if (_handler != null) {
|
|
|
|
throw Exception(
|
|
|
|
"DPS: attempted to re initialize with existing passphrase");
|
|
|
|
}
|
2022-11-09 22:43:26 +00:00
|
|
|
|
|
|
|
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
|
|
|
|
final keyBlob = DB.instance.get<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobKey,
|
|
|
|
);
|
|
|
|
await box.close();
|
2022-11-04 19:32:02 +00:00
|
|
|
|
|
|
|
if (keyBlob == null) {
|
|
|
|
throw Exception(
|
|
|
|
"DPS: failed to find keyBlob while attempting to initialize with existing passphrase");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-01-26 15:18:07 +00:00
|
|
|
final blobVersion = await _getStoredKeyBlobVersion();
|
|
|
|
_handler = await StorageCryptoHandler.fromExisting(
|
|
|
|
passphrase,
|
|
|
|
keyBlob,
|
|
|
|
blobVersion,
|
|
|
|
);
|
|
|
|
if (blobVersion < kLatestBlobVersion) {
|
|
|
|
// update blob
|
|
|
|
await _handler!.resetPassphrase(passphrase, kLatestBlobVersion);
|
|
|
|
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
|
|
|
|
await DB.instance.put<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobKey,
|
|
|
|
value: await _handler!.getKeyBlob(),
|
|
|
|
);
|
|
|
|
await _updateStoredKeyBlobVersion(kLatestBlobVersion);
|
|
|
|
await box.close();
|
|
|
|
}
|
2022-11-04 19:32:02 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"${_getMessageFromException(e)}\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
2022-11-09 23:48:43 +00:00
|
|
|
throw Exception(_getMessageFromException(e));
|
2022-11-04 19:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:45:50 +00:00
|
|
|
Future<bool> verifyPassphrase(String passphrase) async {
|
|
|
|
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
|
|
|
|
final keyBlob = DB.instance.get<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobKey,
|
|
|
|
);
|
|
|
|
await box.close();
|
|
|
|
|
|
|
|
if (keyBlob == null) {
|
|
|
|
// no passphrase key blob found so any passphrase is technically bad
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-01-26 15:18:07 +00:00
|
|
|
final blobVersion = await _getStoredKeyBlobVersion();
|
|
|
|
await StorageCryptoHandler.fromExisting(passphrase, keyBlob, blobVersion);
|
2022-11-11 16:45:50 +00:00
|
|
|
// existing passphrase matches key blob
|
|
|
|
return true;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"${_getMessageFromException(e)}\n$s",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
|
|
|
// password is wrong or some other error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 19:24:40 +00:00
|
|
|
Future<bool> changePassphrase(
|
|
|
|
String passphraseOld,
|
|
|
|
String passphraseNew,
|
|
|
|
) async {
|
|
|
|
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
|
|
|
|
final keyBlob = DB.instance.get<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobKey,
|
|
|
|
);
|
|
|
|
await box.close();
|
|
|
|
|
|
|
|
if (keyBlob == null) {
|
|
|
|
// no passphrase key blob found so any passphrase is technically bad
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(await verifyPassphrase(passphraseOld))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:18:07 +00:00
|
|
|
final blobVersion = await _getStoredKeyBlobVersion();
|
|
|
|
|
2022-11-14 19:24:40 +00:00
|
|
|
try {
|
2023-01-26 15:18:07 +00:00
|
|
|
await _handler!.resetPassphrase(passphraseNew, blobVersion);
|
2022-11-14 19:24:40 +00:00
|
|
|
|
|
|
|
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
|
|
|
|
await DB.instance.put<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobKey,
|
|
|
|
value: await _handler!.getKeyBlob(),
|
|
|
|
);
|
2023-01-26 15:18:07 +00:00
|
|
|
await _updateStoredKeyBlobVersion(blobVersion);
|
2022-11-14 19:24:40 +00:00
|
|
|
await box.close();
|
|
|
|
|
|
|
|
// successfully updated passphrase
|
|
|
|
return true;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"${_getMessageFromException(e)}\n$s",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:32:02 +00:00
|
|
|
Future<bool> hasPassword() async {
|
2022-11-09 22:43:26 +00:00
|
|
|
final keyBlob = DB.instance.get<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobKey,
|
|
|
|
);
|
|
|
|
return keyBlob != null;
|
2022-11-04 19:32:02 +00:00
|
|
|
}
|
2023-01-26 15:18:07 +00:00
|
|
|
|
|
|
|
Future<int> _getStoredKeyBlobVersion() async {
|
|
|
|
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
|
|
|
|
final keyBlobVersionString = DB.instance.get<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobVersionKey,
|
|
|
|
);
|
|
|
|
await box.close();
|
|
|
|
return int.tryParse(keyBlobVersionString ?? "1") ?? 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _updateStoredKeyBlobVersion(int version) async {
|
|
|
|
await DB.instance.put<String>(
|
|
|
|
boxName: DB.boxNameDesktopData,
|
|
|
|
key: _kKeyBlobVersionKey,
|
|
|
|
value: version.toString(),
|
|
|
|
);
|
|
|
|
}
|
2022-11-04 19:32:02 +00:00
|
|
|
}
|