-
Notifications
You must be signed in to change notification settings - Fork 15
Auto-lock wallet after 10 minutes in background #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Ryz0nd
wants to merge
6
commits into
chainapsis:main
Choose a base branch
from
Ryz0nd:feat/wallet-auto-lock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
889c8a3
Serialize secure store wipes
zmanian 5f75fcd
Harden macOS mnemonic secure storage
Thunnini 216a5fb
Auto-lock wallet after 10 minutes in background
Ryz0nd f7989ab
Use monotonic time for auto-lock timeout checks
Ryz0nd 997b2ce
Lock if either monotonic or wall-clock elapsed exceeds threshold
Ryz0nd b18776c
Skip auto-lock before a wallet exists
Ryz0nd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
|
||
| import '../../../main.dart' show log; | ||
| import '../../providers/account_provider.dart'; | ||
| import '../../providers/app_security_provider.dart'; | ||
| import '../../providers/sync_provider.dart'; | ||
|
|
||
| const Duration kAutoLockBackgroundTimeout = Duration(minutes: 10); | ||
|
|
||
| bool shouldAutoLock({ | ||
| required DateTime? hiddenAt, | ||
| required DateTime now, | ||
| Duration threshold = kAutoLockBackgroundTimeout, | ||
| }) { | ||
| if (hiddenAt == null) return false; | ||
| return now.difference(hiddenAt) >= threshold; | ||
| } | ||
|
|
||
| Future<void> lockWalletSession({ | ||
| required AppSecurityNotifier securityNotifier, | ||
| required AccountNotifier accountNotifier, | ||
| required SyncNotifier syncNotifier, | ||
| bool awaitSync = false, | ||
| }) async { | ||
| securityNotifier.lock(); | ||
| accountNotifier.clearSensitiveStateForLock(); | ||
| final syncFuture = syncNotifier.clearSensitiveStateForLock(); | ||
| if (awaitSync) { | ||
| await syncFuture; | ||
| } else { | ||
| unawaited(syncFuture); | ||
| } | ||
| } | ||
|
|
||
| class AutoLockObserver extends ConsumerStatefulWidget { | ||
| const AutoLockObserver({super.key, required this.child}); | ||
|
|
||
| final Widget child; | ||
|
|
||
| @override | ||
| ConsumerState<AutoLockObserver> createState() => _AutoLockObserverState(); | ||
| } | ||
|
|
||
| class _AutoLockObserverState extends ConsumerState<AutoLockObserver> { | ||
| AppLifecycleListener? _listener; | ||
| DateTime? _hiddenAt; | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| _listener = AppLifecycleListener(onHide: _onHide, onShow: _onShow); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _listener?.dispose(); | ||
| _listener = null; | ||
| super.dispose(); | ||
| } | ||
|
|
||
| void _onHide() { | ||
| final security = ref.read(appSecurityProvider); | ||
| if (!security.isUnlocked) return; | ||
| _hiddenAt = DateTime.now(); | ||
| } | ||
|
|
||
| void _onShow() { | ||
| final hiddenAt = _hiddenAt; | ||
| _hiddenAt = null; | ||
| if (hiddenAt == null) return; | ||
| final security = ref.read(appSecurityProvider); | ||
| if (!security.isUnlocked) return; | ||
| final now = DateTime.now(); | ||
| if (!shouldAutoLock(hiddenAt: hiddenAt, now: now)) return; | ||
| log( | ||
| 'AutoLock: hidden for ${now.difference(hiddenAt)}, ' | ||
| 'locking wallet session', | ||
| ); | ||
| lockWalletSession( | ||
| securityNotifier: ref.read(appSecurityProvider.notifier), | ||
| accountNotifier: ref.read(accountProvider.notifier), | ||
| syncNotifier: ref.read(syncProvider.notifier), | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) => widget.child; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.