cake_wallet/cw_monero/lib/api/subaddress_list.dart
cyan f8b0c0ad2a
CW-611-Refactor-Address-Handling (#1630)
* subaddress fix

* fix subaddress generation

* rewrite usedAddresses for xmr and wow

* [skip ci] remove print statements

* refactor address handling

* do not remove manual addresses, just mark them

* monero display latest address on receive page when autogenerate is enabled [skip ci]

* WIP subaddresses, hidden addresses, and UI improvements for monero

* update configure script

* fix subaddress generation, display latest address

* Update lib/core/wallet_loading_service.dart

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* Exclude manually created addresses

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* don't call .save function multiple times

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* - revert usedAddress functinality
- add mutex to prevent crashes
- fix UI flashing in tx screen
- fixes from comments

* account index fixes
added code to wownero
code comment

* - added subaddress index
- fixed received count also accounting for change (we don't want that)
- fix bad state: no element
- fix search
- fix automatic generation

* prevent crashes by acquiring mutex before setting the pointer

* - fix ttDetails generation in larger/restored wallets
- show manual add icon in monero/wownero even when autogeneration is enabled
- disable colors on non-debug builds
- cache getAddress call in xmr/wow
[skip ci]

* fix: silent payment error in address setter
enable fancy new features only for xmr / wow

* refresh subaddress list, when we add new address
fix manual addresses marking

* add toggle to hide and show address

* update transaction details after restore

* show only one address in address book for xmr, wow and haven

* fix address book
reset address only when autogenerate is on

* enable isEnabledAutoGenerateSubaddress on new wallets

* hide addresses after exchange only for XMR and WOW

* fix: bad-state no element

* Update cw_monero/lib/monero_wallet_addresses.dart

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* Update cw_monero/lib/monero_wallet_addresses.dart

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* improvements to performance

* 0, 0 -> accountIndex, addressIndex

* make constant variables final

* Update cw_wownero/lib/wownero_wallet_addresses.dart [skip ci]

* Update cw_wownero/lib/wownero_wallet_addresses.dart [skip ci]

* Update cw_monero/lib/monero_wallet.dart [skip ci]

* fix potential exception

* fix after removing late

* remove orElse, replaced it with a try catch block.
fix strings

* fix valid seed function

* fix null check error [skip ci]

* fix updateSubaddressList for wow and haven

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
2024-09-28 05:38:23 +03:00

164 lines
5.1 KiB
Dart

import 'package:cw_monero/api/account_list.dart';
import 'package:cw_monero/api/transaction_history.dart';
import 'package:cw_monero/api/wallet.dart';
import 'package:monero/monero.dart' as monero;
bool isUpdating = false;
class SubaddressInfoMetadata {
SubaddressInfoMetadata({
required this.accountIndex,
});
int accountIndex;
}
SubaddressInfoMetadata? subaddress = null;
String getRawLabel({required int accountIndex, required int addressIndex}) {
return monero.Wallet_getSubaddressLabel(wptr!, accountIndex: accountIndex, addressIndex: addressIndex);
}
void refreshSubaddresses({required int accountIndex}) {
try {
isUpdating = true;
subaddress = SubaddressInfoMetadata(accountIndex: accountIndex);
isUpdating = false;
} catch (e) {
isUpdating = false;
rethrow;
}
}
class Subaddress {
Subaddress({
required this.addressIndex,
required this.accountIndex,
required this.received,
required this.txCount,
});
late String address = getAddress(
accountIndex: accountIndex,
addressIndex: addressIndex,
);
final int addressIndex;
final int accountIndex;
final int received;
final int txCount;
String get label {
final localLabel = monero.Wallet_getSubaddressLabel(wptr!, accountIndex: accountIndex, addressIndex: addressIndex);
if (localLabel.startsWith("#$addressIndex")) return localLabel; // don't duplicate the ID if it was user-providen
return "#$addressIndex ${localLabel}".trim();
}
}
class TinyTransactionDetails {
TinyTransactionDetails({
required this.address,
required this.amount,
});
final List<String> address;
final int amount;
}
int lastWptr = 0;
int lastTxCount = 0;
List<TinyTransactionDetails> ttDetails = [];
List<Subaddress> getAllSubaddresses() {
txhistory = monero.Wallet_history(wptr!);
final txCount = monero.TransactionHistory_count(txhistory!);
if (lastTxCount != txCount && lastWptr != wptr!.address) {
final List<TinyTransactionDetails> newttDetails = [];
lastTxCount = txCount;
lastWptr = wptr!.address;
for (var i = 0; i < txCount; i++) {
final tx = monero.TransactionHistory_transaction(txhistory!, index: i);
if (monero.TransactionInfo_direction(tx) == monero.TransactionInfo_Direction.Out) continue;
final subaddrs = monero.TransactionInfo_subaddrIndex(tx).split(",");
final account = monero.TransactionInfo_subaddrAccount(tx);
newttDetails.add(TinyTransactionDetails(
address: List.generate(subaddrs.length, (index) => getAddress(accountIndex: account, addressIndex: int.tryParse(subaddrs[index])??0)),
amount: monero.TransactionInfo_amount(tx),
));
}
ttDetails.clear();
ttDetails.addAll(newttDetails);
}
final size = monero.Wallet_numSubaddresses(wptr!, accountIndex: subaddress!.accountIndex);
final list = List.generate(size, (index) {
final ttDetailsLocal = ttDetails.where((element) {
final address = getAddress(
accountIndex: subaddress!.accountIndex,
addressIndex: index,
);
if (element.address.contains(address)) return true;
return false;
}).toList();
int received = 0;
for (var i = 0; i < ttDetailsLocal.length; i++) {
received += ttDetailsLocal[i].amount;
}
return Subaddress(
accountIndex: subaddress!.accountIndex,
addressIndex: index,
received: received,
txCount: ttDetailsLocal.length,
);
}).reversed.toList();
if (list.length == 0) {
list.add(
Subaddress(
addressIndex: subaddress!.accountIndex,
accountIndex: 0,
received: 0,
txCount: 0,
));
}
return list;
}
int numSubaddresses(int subaccountIndex) {
return monero.Wallet_numSubaddresses(wptr!, accountIndex: subaccountIndex);
}
void addSubaddressSync({required int accountIndex, required String label}) {
monero.Wallet_addSubaddress(wptr!, accountIndex: accountIndex, label: label);
refreshSubaddresses(accountIndex: accountIndex);
}
void setLabelForSubaddressSync(
{required int accountIndex, required int addressIndex, required String label}) {
monero.Wallet_setSubaddressLabel(wptr!, accountIndex: accountIndex, addressIndex: addressIndex, label: label);
}
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);
}
Future<void> addSubaddress({required int accountIndex, required String label}) async {
_addSubaddress({'accountIndex': accountIndex, 'label': label});
await store();
}
Future<void> setLabelForSubaddress(
{required int accountIndex, required int addressIndex, required String label}) async {
_setLabelForSubaddress({
'accountIndex': accountIndex,
'addressIndex': addressIndex,
'label': label
});
await store();
}