2024-04-16 15:15:20 +00:00
|
|
|
import 'package:cw_core/subaddress.dart';
|
2023-08-24 13:54:05 +00:00
|
|
|
import 'package:cw_monero/api/coins_info.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/subaddress_list.dart' as subaddress_list;
|
2024-04-16 15:15:20 +00:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
part 'monero_subaddress_list.g.dart';
|
|
|
|
|
2023-08-29 16:11:51 +00:00
|
|
|
class MoneroSubaddressList = MoneroSubaddressListBase with _$MoneroSubaddressList;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
abstract class MoneroSubaddressListBase with Store {
|
2022-10-12 17:09:57 +00:00
|
|
|
MoneroSubaddressListBase()
|
2023-08-29 16:11:51 +00:00
|
|
|
: _isRefreshing = false,
|
|
|
|
_isUpdating = false,
|
|
|
|
subaddresses = ObservableList<Subaddress>();
|
|
|
|
|
|
|
|
final List<String> _usedAddresses = [];
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableList<Subaddress> subaddresses;
|
|
|
|
|
|
|
|
bool _isRefreshing;
|
|
|
|
bool _isUpdating;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void update({required int accountIndex}) {
|
2023-08-24 13:54:05 +00:00
|
|
|
refreshCoins(accountIndex);
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
if (_isUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isUpdating = true;
|
|
|
|
refresh(accountIndex: accountIndex);
|
|
|
|
subaddresses.clear();
|
|
|
|
subaddresses.addAll(getAll());
|
|
|
|
_isUpdating = false;
|
|
|
|
} catch (e) {
|
|
|
|
_isUpdating = false;
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Subaddress> getAll() {
|
|
|
|
var subaddresses = subaddress_list.getAllSubaddresses();
|
|
|
|
|
|
|
|
if (subaddresses.length > 2) {
|
|
|
|
final primary = subaddresses.first;
|
|
|
|
final rest = subaddresses.sublist(1).reversed;
|
|
|
|
subaddresses = [primary] + rest.toList();
|
|
|
|
}
|
|
|
|
|
2024-04-16 15:15:20 +00:00
|
|
|
return subaddresses.map((s) {
|
|
|
|
final address = s.address;
|
|
|
|
final label = s.label;
|
|
|
|
final id = s.addressIndex;
|
2023-08-29 16:11:51 +00:00
|
|
|
final hasDefaultAddressName =
|
2024-04-10 12:27:10 +00:00
|
|
|
label.toLowerCase() == 'Primary account'.toLowerCase() ||
|
|
|
|
label.toLowerCase() == 'Untitled account'.toLowerCase();
|
|
|
|
final isPrimaryAddress = id == 0 && hasDefaultAddressName;
|
2023-08-29 16:11:51 +00:00
|
|
|
return Subaddress(
|
2024-04-10 12:27:10 +00:00
|
|
|
id: id,
|
|
|
|
address: address,
|
2023-08-29 16:11:51 +00:00
|
|
|
label: isPrimaryAddress
|
|
|
|
? 'Primary address'
|
|
|
|
: hasDefaultAddressName
|
|
|
|
? ''
|
2024-04-10 12:27:10 +00:00
|
|
|
: label);
|
2023-08-29 16:11:51 +00:00
|
|
|
}).toList();
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> addSubaddress({required int accountIndex, required String label}) async {
|
2023-08-29 16:11:51 +00:00
|
|
|
await subaddress_list.addSubaddress(accountIndex: accountIndex, label: label);
|
2021-12-24 12:52:08 +00:00
|
|
|
update(accountIndex: accountIndex);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> setLabelSubaddress(
|
|
|
|
{required int accountIndex, required int addressIndex, required String label}) async {
|
2021-12-24 12:52:08 +00:00
|
|
|
await subaddress_list.setLabelForSubaddress(
|
|
|
|
accountIndex: accountIndex, addressIndex: addressIndex, label: label);
|
|
|
|
update(accountIndex: accountIndex);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void refresh({required int accountIndex}) {
|
2021-12-24 12:52:08 +00:00
|
|
|
if (_isRefreshing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isRefreshing = true;
|
|
|
|
subaddress_list.refreshSubaddresses(accountIndex: accountIndex);
|
|
|
|
_isRefreshing = false;
|
|
|
|
} on PlatformException catch (e) {
|
|
|
|
_isRefreshing = false;
|
|
|
|
print(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 16:11:51 +00:00
|
|
|
|
|
|
|
Future<void> updateWithAutoGenerate({
|
|
|
|
required int accountIndex,
|
|
|
|
required String defaultLabel,
|
|
|
|
required List<String> usedAddresses,
|
|
|
|
}) async {
|
|
|
|
_usedAddresses.addAll(usedAddresses);
|
|
|
|
if (_isUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isUpdating = true;
|
|
|
|
refresh(accountIndex: accountIndex);
|
|
|
|
subaddresses.clear();
|
|
|
|
final newSubAddresses =
|
|
|
|
await _getAllUnusedAddresses(accountIndex: accountIndex, label: defaultLabel);
|
|
|
|
subaddresses.addAll(newSubAddresses);
|
|
|
|
} catch (e) {
|
|
|
|
rethrow;
|
|
|
|
} finally {
|
|
|
|
_isUpdating = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Subaddress>> _getAllUnusedAddresses(
|
|
|
|
{required int accountIndex, required String label}) async {
|
|
|
|
final allAddresses = subaddress_list.getAllSubaddresses();
|
2024-04-16 15:15:20 +00:00
|
|
|
final lastAddress = allAddresses.last.address;
|
2024-04-10 12:27:10 +00:00
|
|
|
if (allAddresses.isEmpty || _usedAddresses.contains(lastAddress)) {
|
2023-08-29 16:11:51 +00:00
|
|
|
final isAddressUnused = await _newSubaddress(accountIndex: accountIndex, label: label);
|
|
|
|
if (!isAddressUnused) {
|
|
|
|
return await _getAllUnusedAddresses(accountIndex: accountIndex, label: label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allAddresses
|
2024-04-16 15:15:20 +00:00
|
|
|
.map((s) {
|
|
|
|
final id = s.addressIndex;
|
|
|
|
final address = s.address;
|
|
|
|
final label = s.label;
|
2024-04-10 12:27:10 +00:00
|
|
|
return Subaddress(
|
|
|
|
id: id,
|
|
|
|
address: address,
|
|
|
|
label: id == 0 &&
|
|
|
|
label.toLowerCase() == 'Primary account'.toLowerCase()
|
2023-08-29 16:11:51 +00:00
|
|
|
? 'Primary address'
|
2024-04-10 12:27:10 +00:00
|
|
|
: label);
|
|
|
|
})
|
2023-08-29 16:11:51 +00:00
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> _newSubaddress({required int accountIndex, required String label}) async {
|
|
|
|
await subaddress_list.addSubaddress(accountIndex: accountIndex, label: label);
|
|
|
|
|
|
|
|
return subaddress_list
|
|
|
|
.getAllSubaddresses()
|
2024-04-16 15:15:20 +00:00
|
|
|
.where((s) {
|
|
|
|
final address = s.address;
|
2024-04-10 12:27:10 +00:00
|
|
|
return !_usedAddresses.contains(address);
|
|
|
|
})
|
2023-08-29 16:11:51 +00:00
|
|
|
.isNotEmpty;
|
|
|
|
}
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|