Skip to content

Commit

Permalink
subaddress fix (#1620)
Browse files Browse the repository at this point in the history
* subaddress fix

* fix subaddress generation

* rewrite usedAddresses for xmr and wow

* [skip ci] remove print statements
  • Loading branch information
MrCyjaneK authored Aug 26, 2024
1 parent 518bfbe commit 4c795ea
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 13 deletions.
27 changes: 22 additions & 5 deletions cw_monero/lib/api/transaction_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ List<Transaction> getAllTransactions() {
confirmations: 0,
blockheight: 0,
accountIndex: i,
addressIndex: 0,
addressIndexList: [0],
paymentId: "",
amount: fullBalance - availBalance,
isSpend: false,
Expand Down Expand Up @@ -245,19 +247,30 @@ Future<PendingTransactionDescription> createTransactionMultDest(

class Transaction {
final String displayLabel;
String subaddressLabel = monero.Wallet_getSubaddressLabel(wptr!, accountIndex: 0, addressIndex: 0);
late final String address = monero.Wallet_address(
String get subaddressLabel => monero.Wallet_getSubaddressLabel(
wptr!,
accountIndex: 0,
addressIndex: 0,
accountIndex: accountIndex,
addressIndex: addressIndex,
);
String get address => monero.Wallet_address(
wptr!,
accountIndex: accountIndex,
addressIndex: addressIndex,
);
List<String> get addressList => List.generate(addressIndexList.length, (index) =>
monero.Wallet_address(
wptr!,
accountIndex: accountIndex,
addressIndex: addressIndexList[index],
));
final String description;
final int fee;
final int confirmations;
late final bool isPending = confirmations < 10;
final int blockheight;
final int addressIndex = 0;
final int addressIndex;
final int accountIndex;
final List<int> addressIndexList;
final String paymentId;
final int amount;
final bool isSpend;
Expand Down Expand Up @@ -303,6 +316,8 @@ class Transaction {
amount = monero.TransactionInfo_amount(txInfo),
paymentId = monero.TransactionInfo_paymentId(txInfo),
accountIndex = monero.TransactionInfo_subaddrAccount(txInfo),
addressIndex = int.tryParse(monero.TransactionInfo_subaddrIndex(txInfo).split(", ")[0]) ?? 0,
addressIndexList = monero.TransactionInfo_subaddrIndex(txInfo).split(", ").map((e) => int.tryParse(e) ?? 0).toList(),
blockheight = monero.TransactionInfo_blockHeight(txInfo),
confirmations = monero.TransactionInfo_confirmations(txInfo),
fee = monero.TransactionInfo_fee(txInfo),
Expand All @@ -316,6 +331,8 @@ class Transaction {
required this.confirmations,
required this.blockheight,
required this.accountIndex,
required this.addressIndexList,
required this.addressIndex,
required this.paymentId,
required this.amount,
required this.isSpend,
Expand Down
9 changes: 6 additions & 3 deletions cw_monero/lib/monero_subaddress_list.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:cw_core/subaddress.dart';
import 'package:cw_monero/api/coins_info.dart';
import 'package:cw_monero/api/subaddress_list.dart' as subaddress_list;
import 'package:cw_monero/api/wallet.dart';
import 'package:flutter/services.dart';
import 'package:mobx/mobx.dart';

Expand Down Expand Up @@ -103,6 +104,9 @@ abstract class MoneroSubaddressListBase with Store {
required List<String> usedAddresses,
}) async {
_usedAddresses.addAll(usedAddresses);
final _all = _usedAddresses.toSet().toList();
_usedAddresses.clear();
_usedAddresses.addAll(_all);
if (_isUpdating) {
return;
}
Expand All @@ -124,7 +128,7 @@ abstract class MoneroSubaddressListBase with Store {
Future<List<Subaddress>> _getAllUnusedAddresses(
{required int accountIndex, required String label}) async {
final allAddresses = subaddress_list.getAllSubaddresses();
if (allAddresses.isEmpty || _usedAddresses.contains(allAddresses.last)) {
if (allAddresses.isEmpty || _usedAddresses.contains(allAddresses.first.address)) {
final isAddressUnused = await _newSubaddress(accountIndex: accountIndex, label: label);
if (!isAddressUnused) {
return await _getAllUnusedAddresses(accountIndex: accountIndex, label: label);
Expand All @@ -143,8 +147,7 @@ abstract class MoneroSubaddressListBase with Store {
label.toLowerCase() == 'Primary account'.toLowerCase()
? 'Primary address'
: label);
})
.toList();
}).toList().reversed.toList();
}

Future<bool> _newSubaddress({required int accountIndex, required String label}) async {
Expand Down
5 changes: 5 additions & 0 deletions cw_monero/lib/monero_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
reaction((_) => isEnabledAutoGenerateSubaddress, (bool enabled) {
_updateSubAddress(enabled, account: walletAddresses.account);
});
reaction((_) => transactionHistory, (__) {
_updateSubAddress(isEnabledAutoGenerateSubaddress, account: walletAddresses.account);
});
}

static const int _autoSaveInterval = 30;
Expand Down Expand Up @@ -130,6 +133,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,

monero_wallet.SyncListener? _listener;
ReactionDisposer? _onAccountChangeReaction;
ReactionDisposer? _onTxHistoryChangeReaction;
bool _isTransactionUpdating;
bool _hasSyncAfterStartup;
Timer? _autoSaveTimer;
Expand Down Expand Up @@ -169,6 +173,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
void close() async {
_listener?.stop();
_onAccountChangeReaction?.reaction.dispose();
_onTxHistoryChangeReaction?.reaction.dispose();
_autoSaveTimer?.cancel();
}

Expand Down
20 changes: 20 additions & 0 deletions cw_monero/lib/monero_wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:cw_core/address_info.dart';
import 'package:cw_core/subaddress.dart';
import 'package:cw_core/wallet_addresses.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_monero/api/transaction_history.dart';
import 'package:cw_monero/api/wallet.dart';
import 'package:cw_monero/monero_account_list.dart';
import 'package:cw_monero/monero_subaddress_list.dart';
Expand Down Expand Up @@ -37,6 +38,25 @@ abstract class MoneroWalletAddressesBase extends WalletAddresses with Store {

MoneroAccountList accountList;

@override
Set<String> get usedAddresses {
final txs = getAllTransactions();
final adds = _originalUsedAddresses.toList();
for (var i = 0; i < txs.length; i++) {
for (var j = 0; j < txs[i].addressList.length; j++) {
adds.add(txs[i].addressList[j]);
}
}
return adds.toSet();
}

Set<String> _originalUsedAddresses = Set();

@override
set usedAddresses(Set<String> _usedAddresses) {
_originalUsedAddresses = _usedAddresses;
}

@override
Future<void> init() async {
accountList.update();
Expand Down
23 changes: 18 additions & 5 deletions cw_wownero/lib/api/transaction_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ List<Transaction> getAllTransactions() {
confirmations: 0,
blockheight: 0,
accountIndex: i,
addressIndex: 0,
addressIndexList: [0],
paymentId: "",
amount: fullBalance - availBalance,
isSpend: false,
Expand Down Expand Up @@ -243,19 +245,26 @@ Future<PendingTransactionDescription> createTransactionMultDest(

class Transaction {
final String displayLabel;
String subaddressLabel = wownero.Wallet_getSubaddressLabel(wptr!, accountIndex: 0, addressIndex: 0);
late final String address = wownero.Wallet_address(
String get subaddressLabel => wownero.Wallet_getSubaddressLabel(wptr!, accountIndex: 0, addressIndex: 0);
String get address => wownero.Wallet_address(
wptr!,
accountIndex: 0,
addressIndex: 0,
accountIndex: accountIndex,
addressIndex: addressIndex,
);
List<String> get addressList => List.generate(addressIndexList.length, (index) =>
wownero.Wallet_address(
wptr!,
accountIndex: accountIndex,
addressIndex: addressIndexList[index],
));
final String description;
final int fee;
final int confirmations;
late final bool isPending = confirmations < 3;
final int blockheight;
final int addressIndex = 0;
final int addressIndex;
final int accountIndex;
final List<int> addressIndexList;
final String paymentId;
final int amount;
final bool isSpend;
Expand Down Expand Up @@ -301,6 +310,8 @@ class Transaction {
amount = wownero.TransactionInfo_amount(txInfo),
paymentId = wownero.TransactionInfo_paymentId(txInfo),
accountIndex = wownero.TransactionInfo_subaddrAccount(txInfo),
addressIndex = int.tryParse(wownero.TransactionInfo_subaddrIndex(txInfo).split(", ")[0]) ?? 0,
addressIndexList = wownero.TransactionInfo_subaddrIndex(txInfo).split(", ").map((e) => int.tryParse(e) ?? 0).toList(),
blockheight = wownero.TransactionInfo_blockHeight(txInfo),
confirmations = wownero.TransactionInfo_confirmations(txInfo),
fee = wownero.TransactionInfo_fee(txInfo),
Expand All @@ -314,6 +325,8 @@ class Transaction {
required this.confirmations,
required this.blockheight,
required this.accountIndex,
required this.addressIndex,
required this.addressIndexList,
required this.paymentId,
required this.amount,
required this.isSpend,
Expand Down
4 changes: 4 additions & 0 deletions cw_wownero/lib/wownero_subaddress_list.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:cw_core/subaddress.dart';
import 'package:cw_wownero/api/coins_info.dart';
import 'package:cw_wownero/api/subaddress_list.dart' as subaddress_list;
import 'package:cw_wownero/api/wallet.dart';
import 'package:flutter/services.dart';
import 'package:mobx/mobx.dart';

Expand Down Expand Up @@ -103,6 +104,9 @@ abstract class WowneroSubaddressListBase with Store {
required List<String> usedAddresses,
}) async {
_usedAddresses.addAll(usedAddresses);
final _all = _usedAddresses.toSet().toList();
_usedAddresses.clear();
_usedAddresses.addAll(_all);
if (_isUpdating) {
return;
}
Expand Down
6 changes: 6 additions & 0 deletions cw_wownero/lib/wownero_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ abstract class WowneroWalletBase
reaction((_) => isEnabledAutoGenerateSubaddress, (bool enabled) {
_updateSubAddress(enabled, account: walletAddresses.account);
});

_onTxHistoryChangeReaction = reaction((_) => transactionHistory, (__) {
_updateSubAddress(isEnabledAutoGenerateSubaddress, account: walletAddresses.account);
});
}

static const int _autoSaveInterval = 30;
Expand Down Expand Up @@ -123,6 +127,7 @@ abstract class WowneroWalletBase

wownero_wallet.SyncListener? _listener;
ReactionDisposer? _onAccountChangeReaction;
ReactionDisposer? _onTxHistoryChangeReaction;
bool _isTransactionUpdating;
bool _hasSyncAfterStartup;
Timer? _autoSaveTimer;
Expand Down Expand Up @@ -158,6 +163,7 @@ abstract class WowneroWalletBase
void close() async {
_listener?.stop();
_onAccountChangeReaction?.reaction.dispose();
_onTxHistoryChangeReaction?.reaction.dispose();
_autoSaveTimer?.cancel();
}

Expand Down
18 changes: 18 additions & 0 deletions cw_wownero/lib/wownero_wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:cw_core/address_info.dart';
import 'package:cw_core/subaddress.dart';
import 'package:cw_core/wallet_addresses.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_wownero/api/transaction_history.dart';
import 'package:cw_wownero/api/wallet.dart';
import 'package:cw_wownero/wownero_account_list.dart';
import 'package:cw_wownero/wownero_subaddress_list.dart';
Expand Down Expand Up @@ -36,7 +37,24 @@ abstract class WowneroWalletAddressesBase extends WalletAddresses with Store {
WowneroSubaddressList subaddressList;

WowneroAccountList accountList;
@override
Set<String> get usedAddresses {
final txs = getAllTransactions();
final adds = _originalUsedAddresses.toList();
for (var i = 0; i < txs.length; i++) {
for (var j = 0; j < txs[i].addressList.length; j++) {
adds.add(txs[i].addressList[j]);
}
}
return adds.toSet();
}

Set<String> _originalUsedAddresses = Set();

@override
set usedAddresses(Set<String> _usedAddresses) {
_originalUsedAddresses = _usedAddresses;
}
@override
Future<void> init() async {
accountList.update();
Expand Down

0 comments on commit 4c795ea

Please sign in to comment.