Skip to content

Commit 7e26795

Browse files
committed
refactor: integrate BullbitcoinApiKeyProvider for API key management across exchange features
- Introduced BullbitcoinApiKeyProvider to streamline API key retrieval and management. - Updated VirtualIbanRepository to utilize the new provider instead of the deprecated BullbitcoinApiKeyDatasource. - Refactored ExchangeLocator and RecipientsLocator to register and reuse the API key provider. - Changed imports from user_address entity to value_objects for consistency and clarity. - Removed unused recipient-related classes and methods to clean up the codebase.
1 parent 63ad226 commit 7e26795

17 files changed

Lines changed: 119 additions & 118 deletions

lib/core/exchange/data/mappers/user_summary_mapper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:bb_mobile/core/exchange/data/models/user_summary_model.dart';
2-
import 'package:bb_mobile/core/exchange/domain/entity/user_address.dart';
2+
import 'package:bb_mobile/core/exchange/domain/value_objects/user_address.dart';
33
import 'package:bb_mobile/core/exchange/domain/entity/user_summary.dart';
44

55
class UserSummaryMapper {

lib/core/exchange/data/models/user_summary_model.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:bb_mobile/core/exchange/domain/entity/order.dart';
2-
import 'package:bb_mobile/core/exchange/domain/entity/user_address.dart';
2+
import 'package:bb_mobile/core/exchange/domain/value_objects/user_address.dart';
33
import 'package:bb_mobile/core/exchange/domain/entity/user_summary.dart';
44
import 'package:bb_mobile/features/dca/domain/dca.dart';
55
import 'package:freezed_annotation/freezed_annotation.dart';

lib/core/exchange/data/repository/virtual_iban_repository_impl.dart

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
import 'package:bb_mobile/core/errors/exchange_errors.dart';
22
import 'package:bb_mobile/core/exchange/data/datasources/bullbitcoin_api_datasource.dart';
3-
import 'package:bb_mobile/core/exchange/data/datasources/bullbitcoin_api_key_datasource.dart';
43
import 'package:bb_mobile/core/exchange/data/mappers/virtual_iban_recipient_mapper.dart';
54
import 'package:bb_mobile/core/exchange/domain/entity/virtual_iban_recipient.dart';
65
import 'package:bb_mobile/core/exchange/domain/repositories/virtual_iban_repository.dart';
6+
import 'package:bb_mobile/core/exchange/frameworks/http/bullbitcoin_api_key_provider.dart';
77

88
/// Implementation of [VirtualIbanRepository] that uses the Bull Bitcoin API.
9+
/// Uses [BullbitcoinApiKeyProvider] from core for API key management,
10+
/// following the same pattern as the recipients feature.
911
class VirtualIbanRepositoryImpl implements VirtualIbanRepository {
1012
final BullbitcoinApiDatasource _apiDatasource;
11-
final BullbitcoinApiKeyDatasource _apiKeyDatasource;
13+
final BullbitcoinApiKeyProvider _apiKeyProvider;
1214
final bool _isTestnet;
1315

1416
VirtualIbanRepositoryImpl({
1517
required BullbitcoinApiDatasource apiDatasource,
16-
required BullbitcoinApiKeyDatasource apiKeyDatasource,
18+
required BullbitcoinApiKeyProvider apiKeyProvider,
1719
required bool isTestnet,
1820
}) : _apiDatasource = apiDatasource,
19-
_apiKeyDatasource = apiKeyDatasource,
21+
_apiKeyProvider = apiKeyProvider,
2022
_isTestnet = isTestnet;
2123

2224
Future<String> _getApiKey() async {
23-
final apiKey = await _apiKeyDatasource.get(isTestnet: _isTestnet);
25+
final apiKey = await _apiKeyProvider.getApiKey(isTestnet: _isTestnet);
2426

2527
if (apiKey == null) {
2628
throw ApiKeyException(
2729
'API key not found. Please login to your Bull Bitcoin account.',
2830
);
2931
}
30-
if (!apiKey.isActive) {
31-
throw ApiKeyException(
32-
'API key is inactive. Please login again to your Bull Bitcoin account.',
33-
);
34-
}
3532

36-
return apiKey.key;
33+
return apiKey;
3734
}
3835

3936
@override

lib/core/exchange/domain/entity/user_summary.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:bb_mobile/core/exchange/domain/entity/order.dart';
2-
import 'package:bb_mobile/core/exchange/domain/entity/user_address.dart';
2+
import 'package:bb_mobile/core/exchange/domain/value_objects/user_address.dart';
33
import 'package:bb_mobile/features/dca/domain/dca.dart';
44
import 'package:freezed_annotation/freezed_annotation.dart';
55

lib/core/exchange/domain/entity/user_address.dart renamed to lib/core/exchange/domain/value_objects/user_address.dart

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import 'package:freezed_annotation/freezed_annotation.dart';
33
part 'user_address.freezed.dart';
44
part 'user_address.g.dart';
55

6-
/// Entity representing a user's physical address.
6+
/// Value object representing a user's physical address.
7+
/// Used as a field in entities like VirtualIbanRecipient.
8+
///
9+
/// Note: JSON serialization is kept for compatibility with UserSummary.fromJson,
10+
/// which is auto-generated by freezed. A future refactor could remove JSON
11+
/// from domain entities entirely.
712
@freezed
813
sealed class UserAddress with _$UserAddress {
914
const factory UserAddress({
@@ -15,21 +20,6 @@ sealed class UserAddress with _$UserAddress {
1520
required String countryCode,
1621
}) = _UserAddress;
1722

18-
const UserAddress._();
19-
2023
factory UserAddress.fromJson(Map<String, dynamic> json) =>
2124
_$UserAddressFromJson(json);
22-
23-
/// Returns the address as a single formatted string.
24-
String get addressStringified {
25-
final parts = <String>[
26-
street1,
27-
if (street2 != null && street2!.isNotEmpty) street2!,
28-
city,
29-
if (province != null && province!.isNotEmpty) province!,
30-
postalCode,
31-
countryCode,
32-
];
33-
return parts.join(', ');
34-
}
3525
}

lib/core/exchange/exchange_locator.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:bb_mobile/core/exchange/data/datasources/bullbitcoin_api_datasource.dart';
22
import 'package:bb_mobile/core/exchange/data/datasources/bullbitcoin_api_key_datasource.dart';
3+
import 'package:bb_mobile/core/exchange/frameworks/http/bullbitcoin_api_key_provider.dart';
34
import 'package:bb_mobile/core/exchange/data/datasources/exchange_support_chat_datasource.dart';
45
import 'package:bb_mobile/core/exchange/data/datasources/price_local_datasource.dart';
56
import 'package:bb_mobile/core/exchange/data/datasources/price_remote_datasource.dart';
@@ -64,6 +65,16 @@ class ExchangeLocator {
6465
),
6566
);
6667

68+
// API Key Provider - reusable across all exchange features
69+
// (recipients, virtual_iban, etc.)
70+
locator.registerLazySingleton<BullbitcoinApiKeyProvider>(
71+
() => BullbitcoinApiKeyProvider(
72+
secureStorage: locator<KeyValueStorageDatasource<String>>(
73+
instanceName: LocatorInstanceNameConstants.secureStorageDatasource,
74+
),
75+
),
76+
);
77+
6778
locator.registerLazySingleton<BullbitcoinApiDatasource>(
6879
() => BullbitcoinApiDatasource(
6980
bullbitcoinApiHttpClient: Dio(
@@ -235,13 +246,13 @@ class ExchangeLocator {
235246
instanceName: 'testnetExchangeSupportChatRepository',
236247
);
237248

238-
// Virtual IBAN Repositories
249+
// Virtual IBAN Repositories - uses BullbitcoinApiKeyProvider for API key management
239250
locator.registerLazySingleton<VirtualIbanRepository>(
240251
() => VirtualIbanRepositoryImpl(
241252
apiDatasource: locator<BullbitcoinApiDatasource>(
242253
instanceName: 'mainnetExchangeApiDatasource',
243254
),
244-
apiKeyDatasource: locator<BullbitcoinApiKeyDatasource>(),
255+
apiKeyProvider: locator<BullbitcoinApiKeyProvider>(),
245256
isTestnet: false,
246257
),
247258
instanceName: 'mainnetVirtualIbanRepository',
@@ -251,7 +262,7 @@ class ExchangeLocator {
251262
apiDatasource: locator<BullbitcoinApiDatasource>(
252263
instanceName: 'testnetExchangeApiDatasource',
253264
),
254-
apiKeyDatasource: locator<BullbitcoinApiKeyDatasource>(),
265+
apiKeyProvider: locator<BullbitcoinApiKeyProvider>(),
255266
isTestnet: true,
256267
),
257268
instanceName: 'testnetVirtualIbanRepository',

lib/features/recipients/frameworks/http/authenticated_bullbitcoin_dio_factory.dart renamed to lib/core/exchange/frameworks/http/authenticated_bullbitcoin_dio_factory.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import 'package:bb_mobile/core/errors/exchange_errors.dart';
2-
import 'package:bb_mobile/features/recipients/frameworks/http/bullbitcoin_api_dio_factory.dart';
3-
import 'package:bb_mobile/features/recipients/frameworks/http/bullbitcoin_api_key_provider.dart';
2+
import 'package:bb_mobile/core/exchange/frameworks/http/bullbitcoin_api_dio_factory.dart';
3+
import 'package:bb_mobile/core/exchange/frameworks/http/bullbitcoin_api_key_provider.dart';
44
import 'package:dio/dio.dart';
55

6+
/// Factory for creating Dio instances with automatic API key authentication.
7+
/// Uses an interceptor to add the API key header to all requests.
8+
/// Reusable across all exchange features.
69
class AuthenticatedBullBitcoinDioFactory {
710
static Dio create({
811
required bool isTestnet,

lib/features/recipients/frameworks/http/bullbitcoin_api_dio_factory.dart renamed to lib/core/exchange/frameworks/http/bullbitcoin_api_dio_factory.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:bb_mobile/core/utils/constants.dart';
22
import 'package:dio/dio.dart';
33

4-
// TODO: This should be moved to the core/shared folder since more features
5-
// will need to call the Bull Bitcoin API.
4+
/// Factory for creating Dio instances configured for the Bull Bitcoin API.
5+
/// Reusable across all exchange features.
66
class BullBitcoinApiDioFactory {
77
static Dio create({required bool isTestnet}) {
88
return isTestnet ? _buildBullbitcoinTestnetDio() : _buildBullbitcoinDio();

lib/features/recipients/frameworks/http/bullbitcoin_api_key_provider.dart renamed to lib/core/exchange/frameworks/http/bullbitcoin_api_key_provider.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'package:bb_mobile/core/exchange/data/models/api_key_model.dart';
44
import 'package:bb_mobile/core/storage/data/datasources/key_value_storage/key_value_storage_datasource.dart';
55
import 'package:bb_mobile/core/utils/logger.dart';
66

7+
/// Provider for retrieving the Bull Bitcoin API key from secure storage.
8+
/// Reusable across all exchange features.
79
class BullbitcoinApiKeyProvider {
810
final KeyValueStorageDatasource<String> _secureStorage;
911

lib/features/recipients/application/usecases/filter_recipients_by_virtual_iban_usecase.dart

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)