-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cat-voices): keychain and session (#995)
* feat(vat-voices): keychain * docs(cat-voices): typo * feat: dummy states * refactor: code review feedback * refactor: cleanup * fix: dummy states * refactor: cleanup
- Loading branch information
Showing
22 changed files
with
467 additions
and
123 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
102 changes: 85 additions & 17 deletions
102
catalyst_voices/packages/catalyst_voices_blocs/lib/src/session/session_bloc.dart
This file contains 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 |
---|---|---|
@@ -1,33 +1,101 @@ | ||
import 'package:catalyst_voices_blocs/src/session/session_event.dart'; | ||
import 'package:catalyst_voices_blocs/src/session/session_state.dart'; | ||
import 'package:catalyst_voices_models/catalyst_voices_models.dart'; | ||
import 'package:catalyst_voices_services/catalyst_voices_services.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
// TODO(dtscalac): unlock session | ||
/// Manages the user session. | ||
final class SessionBloc extends Bloc<SessionEvent, SessionState> { | ||
SessionBloc() : super(const VisitorSessionState()) { | ||
on<SessionEvent>(_handleSessionEvent); | ||
final Keychain _keychain; | ||
|
||
SessionBloc(this._keychain) : super(const VisitorSessionState()) { | ||
on<RestoreSessionEvent>(_onRestoreSessionEvent); | ||
on<NextStateSessionEvent>(_onNextStateEvent); | ||
on<VisitorSessionEvent>(_onVisitorEvent); | ||
on<GuestSessionEvent>(_onGuestEvent); | ||
on<ActiveUserSessionEvent>(_onActiveUserEvent); | ||
on<RemoveKeychainSessionEvent>(_onRemoveKeychainEvent); | ||
} | ||
|
||
Future<void> _onRestoreSessionEvent( | ||
RestoreSessionEvent event, | ||
Emitter<SessionState> emit, | ||
) async { | ||
if (!await _keychain.hasSeedPhrase) { | ||
emit(const VisitorSessionState()); | ||
} else if (await _keychain.isUnlocked) { | ||
emit(ActiveUserSessionState(user: _dummyUser)); | ||
} else { | ||
emit(const GuestSessionState()); | ||
} | ||
} | ||
|
||
void _handleSessionEvent( | ||
SessionEvent event, | ||
void _onNextStateEvent( | ||
NextStateSessionEvent event, | ||
Emitter<SessionState> emit, | ||
) { | ||
final nextState = switch (event) { | ||
NextStateSessionEvent() => switch (state) { | ||
VisitorSessionState() => const GuestSessionState(), | ||
GuestSessionState() => const ActiveUserSessionState( | ||
user: User(name: 'Account'), | ||
), | ||
ActiveUserSessionState() => const VisitorSessionState(), | ||
}, | ||
VisitorSessionEvent() => const VisitorSessionState(), | ||
GuestSessionEvent() => const GuestSessionState(), | ||
ActiveUserSessionEvent() => const ActiveUserSessionState( | ||
user: User(name: 'Account'), | ||
), | ||
final nextState = switch (state) { | ||
VisitorSessionState() => const GuestSessionState(), | ||
GuestSessionState() => ActiveUserSessionState(user: _dummyUser), | ||
ActiveUserSessionState() => const VisitorSessionState(), | ||
}; | ||
|
||
emit(nextState); | ||
} | ||
|
||
Future<void> _onVisitorEvent( | ||
VisitorSessionEvent event, | ||
Emitter<SessionState> emit, | ||
) async { | ||
await _keychain.clearAndLock(); | ||
|
||
emit(const VisitorSessionState()); | ||
} | ||
|
||
Future<void> _onGuestEvent( | ||
GuestSessionEvent event, | ||
Emitter<SessionState> emit, | ||
) async { | ||
await _keychain.setLockAndBeginWith( | ||
seedPhrase: _dummySeedPhrase, | ||
unlockFactor: _dummyUnlockFactor, | ||
unlocked: false, | ||
); | ||
|
||
emit(const GuestSessionState()); | ||
} | ||
|
||
Future<void> _onActiveUserEvent( | ||
ActiveUserSessionEvent event, | ||
Emitter<SessionState> emit, | ||
) async { | ||
await _keychain.setLockAndBeginWith( | ||
seedPhrase: _dummySeedPhrase, | ||
unlockFactor: _dummyUnlockFactor, | ||
unlocked: true, | ||
); | ||
|
||
emit(ActiveUserSessionState(user: _dummyUser)); | ||
} | ||
|
||
Future<void> _onRemoveKeychainEvent( | ||
RemoveKeychainSessionEvent event, | ||
Emitter<SessionState> emit, | ||
) async { | ||
await _keychain.clearAndLock(); | ||
emit(const VisitorSessionState()); | ||
} | ||
|
||
/// Temporary implementation for testing purposes. | ||
User get _dummyUser => const User(name: 'Account'); | ||
|
||
/// Temporary implementation for testing purposes. | ||
SeedPhrase get _dummySeedPhrase => SeedPhrase.fromMnemonic( | ||
'few loyal swift champion rug peace dinosaur' | ||
' erase bacon tone install universe', | ||
); | ||
|
||
/// Temporary implementation for testing purposes. | ||
LockFactor get _dummyUnlockFactor => const PasswordLockFactor('Test1234'); | ||
} |
This file contains 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
20 changes: 17 additions & 3 deletions
20
catalyst_voices/packages/catalyst_voices_models/lib/src/account/account_role.dart
This file contains 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 |
---|---|---|
@@ -1,5 +1,19 @@ | ||
enum AccountRole { | ||
voter, | ||
proposer, | ||
drep, | ||
voter(roleNumber: 0), | ||
|
||
// TODO(dtscalac): the RBAC specification doesn't define yet the role number | ||
// for the proposer, replace this arbitrary number when it's specified. | ||
proposer(roleNumber: 1), | ||
|
||
// TODO(dtscalac): the RBAC specification doesn't define yet the role number | ||
// for the drep, replace this arbitrary number when it's specified. | ||
drep(roleNumber: 2); | ||
|
||
/// The RBAC specified role number. | ||
final int roleNumber; | ||
|
||
const AccountRole({required this.roleNumber}); | ||
|
||
/// Returns the role which is assigned to every user. | ||
static AccountRole get root => voter; | ||
} |
This file contains 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 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 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
2 changes: 2 additions & 0 deletions
2
catalyst_voices/packages/catalyst_voices_services/lib/src/catalyst_voices_services.dart
This file contains 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
Oops, something went wrong.