-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = | ||
accountSeedPhrases[i]; | ||
|
||
(String phrase, List<Account> accout) derivativeAccounts = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo. Can we defined it like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we compare not There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. further we use this result, and need Account There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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 =
There was a problem hiding this comment.
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