Skip to content
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

Add account manager #37

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/fuelet_secure_layer/lib/fuelet_secure_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export 'src/di/public/public_locator.dart';
export 'src/features/account/entity/account_x.dart';
export 'src/features/account/entity/export.dart';
export 'src/features/account/repository/accounts_local_repository.dart';
export 'src/features/account/accounts_manager/accounts_manager.dart';
export 'src/features/balance/entity/export.dart';
export 'src/features/cloud_backup/repository/cloud_backup_repository.dart';
export 'src/features/graph_ql/repository/graph_ql_repository.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:flutter_cloud_kit/flutter_cloud_kit.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:fuelet_secure_layer/src/di/common/common_locator.dart';
import 'package:fuelet_secure_layer/src/di/public/public_locator.dart';
import 'package:fuelet_secure_layer/src/features/account/accounts_manager/accounts_manager.dart';
import 'package:fuelet_secure_layer/src/features/account/accounts_manager/accounts_manager_impl.dart';
import 'package:fuelet_secure_layer/src/features/account/entity/account_private_data.dart';
import 'package:fuelet_secure_layer/src/features/account/repository/accounts_local_repository.dart';
import 'package:fuelet_secure_layer/src/features/account/repository/accounts_local_repository_impl.dart';
Expand Down Expand Up @@ -159,6 +161,11 @@ class PublicSecureLayerRegister {
_privateSecureLayerLocator<WalletUnlockedService>(),
secureLayerLocator<IAccountsLocalRepository>(),
),
);
)
..registerFactoryAsync<AccountsManager>(() async => AccountsManagerImpl(
secureLayerLocator<IAccountsLocalRepository>(),
_privateSecureLayerLocator<IAccountsPrivateDataRepository>(),
secureLayerLocator<IWalletCreateRepository>(),
await secureLayerLocator.getAsync<FuelNetworkManager>()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:fuelet_secure_layer/src/features/account/entity/derivative_account.dart';

abstract class AccountsManager {
Future<List<DerivativeAccount>> getDerivativeAccounts();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:fuelet_secure_layer/src/features/account/accounts_manager/accounts_manager.dart';
import 'package:fuelet_secure_layer/src/features/account/entity/account.dart';
import 'package:fuelet_secure_layer/src/features/account/entity/account_private_data.dart';
import 'package:fuelet_secure_layer/src/features/account/entity/derivative_account.dart';
import 'package:fuelet_secure_layer/src/features/account/repository/accounts_local_repository.dart';
import 'package:fuelet_secure_layer/src/features/account/repository/accounts_private_data_repository.dart';
import 'package:fuelet_secure_layer/src/features/network/repository/network_provider_repository.dart';
import 'package:fuelet_secure_layer/src/features/wallet_create/repository/wallet_create_repository.dart';
import 'package:fuelet_secure_layer/src/features/wallet_import/entity/wallet_import_typedef.dart';

class AccountsManagerImpl extends AccountsManager {
final int _loadingDerivativeAccountCount = 20;

final IAccountsLocalRepository _accountsLocalRepository;
final IAccountsPrivateDataRepository _privateDataRepository;
final IWalletCreateRepository _walletCreateRepository;
final FuelNetworkProviderRepository _fuelNetworkProviderRepository;

AccountsManagerImpl(
this._accountsLocalRepository,
this._privateDataRepository,
this._walletCreateRepository,
this._fuelNetworkProviderRepository);

@override
Future<List<DerivativeAccount>> getDerivativeAccounts() async {
List<DerivativeAccount> resultMap = [];

List<Account> currentAccounts =
await _accountsLocalRepository.loadAccounts();

List<(Account account, String seedPhrase)> accountSeedPhrases =
await _defineAccountSeedPhrases(currentAccounts);

for (int i = 0; i < accountSeedPhrases.length; i++) {
(Account account, String seedPhrase) accountSeedPhraseItem =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same: do we need variable names here in defintion? Why not just (Account, String) accountSeedPhraseItem =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not problem, but, if Account is obvious, then String, what is it

accountSeedPhrases[i];

(String phrase, List<Account> accout) derivativeAccounts =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo. Can we defined it like (String, List<Account>) or just final or var?

Copy link
Contributor Author

@rscripnic rscripnic Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's for you, more readable

await _defineDerivativeAccounts(accountSeedPhraseItem.$2);

List<Account> derivatives = [];
for (int k = 0; k < currentAccounts.length; k++) {
Account currentAccountItem = currentAccounts[k];

if (derivativeAccounts.$2.any((element) =>
element.fuelAddress.bech32Address ==
currentAccountItem.fuelAddress.bech32Address)) {
Comment on lines +47 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we compare not bech32Address, but just addresses? element.fuelAddress == currentAccountItem.fuelAddress?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think, comparing by primitive type is better, because Account object is not finished yet

derivatives.add(currentAccountItem);
}
}
resultMap.add(DerivativeAccount(
rootAccount: accountSeedPhraseItem.$1,
seedPhraseAlias: "Seed phrase ${i + 1}",
derivativeAccounts: derivatives));
}

return resultMap;
}

Future<List<(Account, String)>> _defineAccountSeedPhrases(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that several accounts will have the same seed phrase, we should filter them out here at return just distinct phrases. Also, I don't think that we need to return an account from this function, just List<String> should be enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

further we use this result, and need Account

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just use the first account from the list of derivative accounts, that'd be more accurate

List<Account> accounts) async {
List<(Account, String)> response = [];
try {
for (int i = 0; i < accounts.length; i++) {
Account accountItem = accounts[i];
final AccountPrivateData? privateData = await _privateDataRepository
.getAccountPrivateData(accountItem.fuelAddress.bech32Address);
String? seedPhrase = privateData?.seedPhrase;
if (seedPhrase != null && seedPhrase.isNotEmpty) {
response.add((accountItem, seedPhrase));
}
}
} catch (_) {}
return response;
}

Future<(String seedPhrase, List<Account> derivativeAccounts)>
_defineDerivativeAccounts(String seedPhrase) async {
WalletsImportResponse? response;
try {
response =
await _walletCreateRepository.importDerivativeAccountsWithMnemonic(
seedPhrase,
count: _loadingDerivativeAccountCount,
fromIndex: 0,
currentNetworkUrl: _fuelNetworkProviderRepository.currentNetwork,
);
} catch (_) {}
return (seedPhrase, response?.getOrElse(() => []) ?? []);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:fuelet_secure_layer/src/features/account/entity/account.dart';

class DerivativeAccount {
final String seedPhraseAlias;
final Account rootAccount;
final List<Account> derivativeAccounts;

DerivativeAccount({
required this.rootAccount,
required this.seedPhraseAlias,
required this.derivativeAccounts,
});

DerivativeAccount copyWith({
String? seedPhraseAlias,
Account? rootAccount,
List<Account>? derivativeAccounts,
}) {
return DerivativeAccount(
seedPhraseAlias: seedPhraseAlias ?? this.seedPhraseAlias,
rootAccount: rootAccount ?? this.rootAccount,
derivativeAccounts: derivativeAccounts ?? this.derivativeAccounts,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export 'adding_method.dart';
export 'address.dart';
export 'derivative_info.dart';
export 'wallet_group.dart';
export 'derivative_account.dart';
2 changes: 1 addition & 1 deletion packages/fuelet_secure_layer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fuelet_secure_layer
description: "Fuelet wallet secure layer features"
version: 1.0.31
version: 1.0.32

publish_to: none

Expand Down