mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-28 06:28:41 +01:00
Upstream reformatted its whole tree in 16ddc0837. Matching it removes
formatting from the conflict surface of this and every later sync.
89 lines
2.3 KiB
Dart
89 lines
2.3 KiB
Dart
import 'package:hash_wallet/core/auth_service.dart';
|
|
import 'package:hash_wallet/entities/encrypt.dart';
|
|
import 'package:hash_wallet/entities/secret_store_key.dart';
|
|
import 'package:hash_wallet/store/settings_store.dart';
|
|
|
|
class SetupPinCodeViewModel {
|
|
SetupPinCodeViewModel(this._authService, this._settingsStore, {this.isDuressPin = false})
|
|
: _pinCodeLength = _settingsStore.pinCodeLength;
|
|
|
|
String originalPinCode = '';
|
|
|
|
String repeatedPinCode = '';
|
|
|
|
Future<void> setPinCode(String pinCode) async {
|
|
if (!isOriginalPinCodeFull) {
|
|
await setOriginalPinCode(pinCode);
|
|
return;
|
|
}
|
|
|
|
repeatedPinCode = pinCode;
|
|
}
|
|
|
|
int get pinCodeLength => _pinCodeLength;
|
|
|
|
set pinCodeLength(int length) {
|
|
_pinCodeLength = length;
|
|
reset();
|
|
}
|
|
|
|
bool get isOriginalPinCodeFull => originalPinCode.length == pinCodeLength;
|
|
|
|
bool get isRepeatedPinCodeFull => repeatedPinCode.length == pinCodeLength;
|
|
|
|
bool get isPinCodeCorrect =>
|
|
originalPinCode.length == pinCodeLength &&
|
|
repeatedPinCode.length == pinCodeLength &&
|
|
originalPinCode == repeatedPinCode;
|
|
|
|
final SettingsStore _settingsStore;
|
|
final AuthService _authService;
|
|
final bool isDuressPin;
|
|
int _pinCodeLength;
|
|
|
|
Future<void> setOriginalPinCode(String pin) async {
|
|
originalPinCode = pin;
|
|
|
|
if (isDuressPin && pin.length == pinCodeLength) {
|
|
final regularKey = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
|
|
final encodedRegularPin = await _authService.secureStorage.read(key: regularKey);
|
|
|
|
if (encodedRegularPin != null && encodedRegularPin.isNotEmpty) {
|
|
final realPin = decodedPinCode(pin: encodedRegularPin);
|
|
|
|
if (pin == realPin) {
|
|
reset();
|
|
throw Exception('Duress PIN cannot be the same as regular PIN');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void setRepeatedPinCode(String pinCode) {
|
|
if (isRepeatedPinCodeFull) {
|
|
return;
|
|
}
|
|
|
|
repeatedPinCode = pinCode;
|
|
}
|
|
|
|
void reset() {
|
|
originalPinCode = '';
|
|
repeatedPinCode = '';
|
|
}
|
|
|
|
Future<void> setupPinCode() async {
|
|
if (!isPinCodeCorrect) {
|
|
return;
|
|
}
|
|
|
|
if (isDuressPin) {
|
|
await _authService.setDuressPin(repeatedPinCode);
|
|
return;
|
|
}
|
|
|
|
await _authService.setPassword(repeatedPinCode);
|
|
_settingsStore.pinCodeLength = pinCodeLength;
|
|
}
|
|
}
|