2024-04-16 15:15:20 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
import 'package:cw_monero/api/account_list.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/wallet.dart';
|
2024-04-10 12:27:10 +00:00
|
|
|
import 'package:monero/monero.dart' as monero;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
bool isUpdating = false;
|
2024-04-16 15:15:20 +00:00
|
|
|
|
|
|
|
class SubaddressInfoMetadata {
|
|
|
|
SubaddressInfoMetadata({
|
|
|
|
required this.accountIndex,
|
|
|
|
});
|
|
|
|
int accountIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubaddressInfoMetadata? subaddress = null;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void refreshSubaddresses({required int accountIndex}) {
|
2021-12-24 12:52:08 +00:00
|
|
|
try {
|
|
|
|
isUpdating = true;
|
2024-04-16 15:15:20 +00:00
|
|
|
subaddress = SubaddressInfoMetadata(accountIndex: accountIndex);
|
2021-12-24 12:52:08 +00:00
|
|
|
isUpdating = false;
|
|
|
|
} catch (e) {
|
|
|
|
isUpdating = false;
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 15:15:20 +00:00
|
|
|
class Subaddress {
|
|
|
|
Subaddress({
|
|
|
|
required this.addressIndex,
|
|
|
|
required this.accountIndex,
|
|
|
|
});
|
|
|
|
String get address => monero.Wallet_address(
|
|
|
|
wptr!,
|
|
|
|
accountIndex: accountIndex,
|
|
|
|
addressIndex: addressIndex,
|
|
|
|
);
|
|
|
|
final int addressIndex;
|
|
|
|
final int accountIndex;
|
|
|
|
String get label => monero.Wallet_getSubaddressLabel(wptr!, accountIndex: accountIndex, addressIndex: addressIndex);
|
|
|
|
}
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-16 15:15:20 +00:00
|
|
|
List<Subaddress> getAllSubaddresses() {
|
|
|
|
final size = monero.Wallet_numSubaddresses(wptr!, accountIndex: subaddress!.accountIndex);
|
2024-04-10 12:27:10 +00:00
|
|
|
return List.generate(size, (index) {
|
2024-04-16 15:15:20 +00:00
|
|
|
return Subaddress(
|
|
|
|
accountIndex: subaddress!.accountIndex,
|
|
|
|
addressIndex: index,
|
|
|
|
);
|
|
|
|
}).reversed.toList();
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void addSubaddressSync({required int accountIndex, required String label}) {
|
2024-04-10 12:27:10 +00:00
|
|
|
monero.Wallet_addSubaddress(wptr!, accountIndex: accountIndex, label: label);
|
2024-04-12 12:54:24 +00:00
|
|
|
refreshSubaddresses(accountIndex: accountIndex);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setLabelForSubaddressSync(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required int accountIndex, required int addressIndex, required String label}) {
|
2024-04-10 12:27:10 +00:00
|
|
|
monero.Wallet_setSubaddressLabel(wptr!, accountIndex: accountIndex, addressIndex: addressIndex, label: label);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _addSubaddress(Map<String, dynamic> args) {
|
|
|
|
final label = args['label'] as String;
|
|
|
|
final accountIndex = args['accountIndex'] as int;
|
|
|
|
|
|
|
|
addSubaddressSync(accountIndex: accountIndex, label: label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setLabelForSubaddress(Map<String, dynamic> args) {
|
|
|
|
final label = args['label'] as String;
|
|
|
|
final accountIndex = args['accountIndex'] as int;
|
|
|
|
final addressIndex = args['addressIndex'] as int;
|
|
|
|
|
|
|
|
setLabelForSubaddressSync(
|
|
|
|
accountIndex: accountIndex, addressIndex: addressIndex, label: label);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> addSubaddress({required int accountIndex, required String label}) async {
|
2024-04-10 12:27:10 +00:00
|
|
|
_addSubaddress({'accountIndex': accountIndex, 'label': label});
|
|
|
|
await store();
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> setLabelForSubaddress(
|
|
|
|
{required int accountIndex, required int addressIndex, required String label}) async {
|
2024-04-10 12:27:10 +00:00
|
|
|
_setLabelForSubaddress({
|
2021-12-24 12:52:08 +00:00
|
|
|
'accountIndex': accountIndex,
|
|
|
|
'addressIndex': addressIndex,
|
|
|
|
'label': label
|
|
|
|
});
|
|
|
|
await store();
|
|
|
|
}
|