Skip to content

Commit

Permalink
refactor: code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dtscalac committed Oct 14, 2024
1 parent 3c8d71c commit 9231ca6
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 43 deletions.
20 changes: 16 additions & 4 deletions catalyst_voices/lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,33 @@ import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

final class App extends StatelessWidget {
final class App extends StatefulWidget {
final RouterConfig<Object> routerConfig;

const App({
super.key,
required this.routerConfig,
});

@override
State<App> createState() => _AppState();
}

class _AppState extends State<App> {
final SessionBloc _sessionBloc = Dependencies.instance.get();

@override
void initState() {
super.initState();
_sessionBloc.add(const RestoreSessionEvent());
}

@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: _multiBlocProviders(),
child: AppContent(
routerConfig: routerConfig,
routerConfig: widget.routerConfig,
),
);
}
Expand All @@ -31,8 +44,7 @@ final class App extends StatelessWidget {
create: (_) => Dependencies.instance.get<LoginBloc>(),
),
BlocProvider<SessionBloc>(
create: (_) => Dependencies.instance.get<SessionBloc>()
..add(const RestoreSessionEvent()),
create: (_) => _sessionBloc,
),
];
}
Expand Down
14 changes: 7 additions & 7 deletions catalyst_voices/lib/dependency/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Dependencies extends DependencyProvider {
),
)
..registerLazySingleton<SessionBloc>(
() => SessionBloc(get<KeychainService>()),
() => SessionBloc(get<Keychain>()),
)
// Factory will rebuild it each time needed
..registerFactory<RegistrationCubit>(() {
Expand Down Expand Up @@ -62,18 +62,18 @@ final class Dependencies extends DependencyProvider {
registerLazySingleton<Downloader>(Downloader.new);
registerLazySingleton<CatalystCardano>(() => CatalystCardano.instance);

registerLazySingleton<KeyDerivationService>(KeyDerivationService.new);
registerLazySingleton<KeychainService>(
() => KeychainService(
get<KeyDerivationService>(),
registerLazySingleton<KeyDerivation>(KeyDerivation.new);
registerLazySingleton<Keychain>(
() => Keychain(
get<KeyDerivation>(),
get<Vault>(),
),
);
registerLazySingleton<RegistrationService>(
() => RegistrationService(
get<TransactionConfigRepository>(),
get<KeychainService>(),
get<KeyDerivationService>(),
get<Keychain>(),
get<KeyDerivation>(),
get<CatalystCardano>(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import 'package:flutter_bloc/flutter_bloc.dart';
// TODO(dtscalac): unlock session
/// Manages the user session.
final class SessionBloc extends Bloc<SessionEvent, SessionState> {
final KeychainService _keychainService;
final Keychain _keychain;

SessionBloc(this._keychainService) : super(const VisitorSessionState()) {
SessionBloc(this._keychain) : super(const VisitorSessionState()) {
on<RestoreSessionEvent>(_onRestoreSessionEvent);
on<NextStateSessionEvent>(_onNextStateEvent);
on<VisitorSessionEvent>(_onVisitorEvent);
Expand All @@ -22,9 +22,9 @@ final class SessionBloc extends Bloc<SessionEvent, SessionState> {
RestoreSessionEvent event,
Emitter<SessionState> emit,
) async {
if (!await _keychainService.hasSeedPhrase) {
if (!await _keychain.hasSeedPhrase) {
emit(const VisitorSessionState());
} else if (await _keychainService.isUnlocked) {
} else if (await _keychain.isUnlocked) {
emit(ActiveUserSessionState(user: _dummyUser));
} else {
emit(const GuestSessionState());
Expand All @@ -37,9 +37,7 @@ final class SessionBloc extends Bloc<SessionEvent, SessionState> {
) {
final nextState = switch (state) {
VisitorSessionState() => const GuestSessionState(),
GuestSessionState() => const ActiveUserSessionState(
user: User(name: 'Account'),
),
GuestSessionState() => ActiveUserSessionState(user: _dummyUser),
ActiveUserSessionState() => const VisitorSessionState(),
};

Expand All @@ -50,7 +48,7 @@ final class SessionBloc extends Bloc<SessionEvent, SessionState> {
VisitorSessionEvent event,
Emitter<SessionState> emit,
) async {
await _keychainService.remove();
await _keychain.remove();

emit(const VisitorSessionState());
}
Expand All @@ -59,7 +57,7 @@ final class SessionBloc extends Bloc<SessionEvent, SessionState> {
GuestSessionEvent event,
Emitter<SessionState> emit,
) async {
await _keychainService.init(
await _keychain.init(
seedPhrase: _dummySeedPhrase,
unlockFactor: _dummyUnlockFactor,
);
Expand All @@ -71,12 +69,12 @@ final class SessionBloc extends Bloc<SessionEvent, SessionState> {
ActiveUserSessionEvent event,
Emitter<SessionState> emit,
) async {
await _keychainService.init(
await _keychain.init(
seedPhrase: _dummySeedPhrase,
unlockFactor: _dummyUnlockFactor,
);

await _keychainService.unlock(_dummyUnlockFactor);
await _keychain.unlock(_dummyUnlockFactor);

emit(ActiveUserSessionState(user: _dummyUser));
}
Expand All @@ -85,7 +83,7 @@ final class SessionBloc extends Bloc<SessionEvent, SessionState> {
RemoveKeychainSessionEvent event,
Emitter<SessionState> emit,
) async {
await _keychainService.remove();
await _keychain.remove();
emit(const VisitorSessionState());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export 'downloader/downloader.dart';
export 'keychain/key_derivation_service.dart';
export 'keychain/keychain_service.dart';
export 'keychain/key_derivation.dart';
export 'keychain/keychain.dart';
export 'registration/registration_service.dart';
export 'registration/registration_transaction_builder.dart';
export 'storage/dummy_auth_storage.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:ed25519_hd_key/ed25519_hd_key.dart';

/// Derives key pairs from a seed phrase.
class KeyDerivationService {
class KeyDerivation {
/// Derives an [Ed25519KeyPair] from a [seedPhrase] and [path].
///
/// Example [path]: m/0'/2147483647'
Expand Down Expand Up @@ -40,7 +40,7 @@ class KeyDerivationService {
/// to generate a key pair from a seed phrase.
///
// TODO(dtscalac): update when RBAC specifies it
static String _roleKeyDerivationPath(AccountRole role) {
String _roleKeyDerivationPath(AccountRole role) {
return "m/${role.roleNumber}'/1234'";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:catalyst_cardano_serialization/catalyst_cardano_serialization.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_services/src/keychain/key_derivation_service.dart';
import 'package:catalyst_voices_services/src/keychain/key_derivation.dart';
import 'package:catalyst_voices_services/src/storage/vault/lock_factor.dart';
import 'package:catalyst_voices_services/src/storage/vault/vault.dart';
import 'package:logging/logging.dart';
Expand All @@ -13,13 +13,13 @@ const _seedPhraseKey = 'keychain_seed_phrase';
// TODO(dtscalac): in the future when key derivation algorithm spec
// will become stable consider to store derived keys instead of deriving
// them each time they are needed.
class KeychainService {
final _logger = Logger('KeychainService');
class Keychain {
final _logger = Logger('Keychain');

final KeyDerivationService _keyDerivationService;
final KeyDerivation _keyDerivation;
final Vault _vault;

KeychainService(this._keyDerivationService, this._vault);
Keychain(this._keyDerivation, this._vault);

/// Returns true if the keychain is unlocked, false otherwise.
Future<bool> get isUnlocked => _vault.isUnlocked;
Expand Down Expand Up @@ -77,7 +77,7 @@ class KeychainService {

final seedPhrase = await _readSeedPhrase();
if (seedPhrase != null) {
return _keyDerivationService.deriveAccountRoleKeyPair(
return _keyDerivation.deriveAccountRoleKeyPair(
seedPhrase: seedPhrase,
role: role,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ final _testNetAddress = ShelleyAddress.fromBech32(
/// Manages the user registration.
final class RegistrationService {
final TransactionConfigRepository _transactionConfigRepository;
final KeychainService _keychainService;
final KeyDerivationService _keyDerivationService;
final Keychain _keychain;
final KeyDerivation _keyDerivation;
final CatalystCardano _cardano;

const RegistrationService(
this._transactionConfigRepository,
this._keychainService,
this._keyDerivationService,
this._keychain,
this._keyDerivation,
this._cardano,
);

Expand All @@ -33,7 +33,7 @@ final class RegistrationService {
required SeedPhrase seedPhrase,
required String unlockPassword,
}) async {
await _keychainService.init(
await _keychain.init(
seedPhrase: seedPhrase,
unlockFactor: PasswordLockFactor(unlockPassword),
);
Expand Down Expand Up @@ -102,7 +102,7 @@ final class RegistrationService {
try {
final walletApi = await wallet.enable();

final keyPair = await _keyDerivationService.deriveAccountRoleKeyPair(
final keyPair = await _keyDerivation.deriveAccountRoleKeyPair(
seedPhrase: seedPhrase,
role: AccountRole.root,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_services/src/keychain/key_derivation_service.dart';
import 'package:catalyst_voices_services/src/keychain/key_derivation.dart';
import 'package:test/test.dart';

void main() {
group(KeyDerivationService, () {
late KeyDerivationService service;
group(KeyDerivation, () {
late KeyDerivation service;
late SeedPhrase seedPhrase;

setUp(() {
service = KeyDerivationService();
service = KeyDerivation();
seedPhrase = SeedPhrase.fromMnemonic(
'few loyal swift champion rug peace dinosaur'
' erase bacon tone install universe',
Expand Down

0 comments on commit 9231ca6

Please sign in to comment.