check change addresses for transactions automatically in fusion interface change address getter function

This commit is contained in:
julian 2023-09-22 16:39:59 -06:00
parent a4f8c52148
commit 55d8738acb
2 changed files with 23 additions and 40 deletions

View file

@ -139,9 +139,11 @@ class BitcoinCashWallet extends CoinServiceAPI
walletId: walletId, walletId: walletId,
coin: coin, coin: coin,
db: db, db: db,
generateAddressForChain: _generateAddressForChain,
getWalletCachedElectrumX: () => cachedElectrumXClient, getWalletCachedElectrumX: () => cachedElectrumXClient,
); getNextUnusedChangeAddress: () async {
await checkCurrentChangeAddressesForTransactions();
return await _currentChangeAddress;
});
initCoinControlInterface( initCoinControlInterface(
walletId: walletId, walletId: walletId,
walletName: walletName, walletName: walletName,

View file

@ -15,7 +15,6 @@ import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/services/fusion_tor_service.dart'; import 'package:stackwallet/services/fusion_tor_service.dart';
import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
import 'package:stackwallet/utilities/extensions/impl/string.dart'; import 'package:stackwallet/utilities/extensions/impl/string.dart';
import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/stack_file_system.dart';
@ -30,12 +29,7 @@ mixin FusionWalletInterface {
late final FusionTorService _torService; late final FusionTorService _torService;
// Passed in wallet functions. // Passed in wallet functions.
late final Future<Address> Function( late final Future<Address> Function() _getNextUnusedChangeAddress;
int chain,
int index,
DerivePathType derivePathType,
) _generateAddressForChain;
late final CachedElectrumX Function() _getWalletCachedElectrumX; late final CachedElectrumX Function() _getWalletCachedElectrumX;
/// Initializes the FusionWalletInterface mixin. /// Initializes the FusionWalletInterface mixin.
@ -47,18 +41,14 @@ mixin FusionWalletInterface {
required String walletId, required String walletId,
required Coin coin, required Coin coin,
required MainDB db, required MainDB db,
required Future<Address> Function( required Future<Address> Function() getNextUnusedChangeAddress,
int,
int,
DerivePathType,
) generateAddressForChain,
required CachedElectrumX Function() getWalletCachedElectrumX, required CachedElectrumX Function() getWalletCachedElectrumX,
}) async { }) async {
// Set passed in wallet data. // Set passed in wallet data.
_walletId = walletId; _walletId = walletId;
_coin = coin; _coin = coin;
_db = db; _db = db;
_generateAddressForChain = generateAddressForChain; _getNextUnusedChangeAddress = getNextUnusedChangeAddress;
_torService = FusionTorService.sharedInstance; _torService = FusionTorService.sharedInstance;
_getWalletCachedElectrumX = getWalletCachedElectrumX; _getWalletCachedElectrumX = getWalletCachedElectrumX;
} }
@ -105,28 +95,14 @@ mixin FusionWalletInterface {
/// Creates a new reserved change address. /// Creates a new reserved change address.
Future<fusion_address.Address> createNewReservedChangeAddress() async { Future<fusion_address.Address> createNewReservedChangeAddress() async {
int? highestChangeIndex = await _db // _getNextUnusedChangeAddress() grabs the latest unused change address
.getAddresses(_walletId) // from the wallet.
.filter() // CopyWith to mark it as a fusion reserved change address
.typeEqualTo(AddressType.p2pkh) final address = (await _getNextUnusedChangeAddress())
.subTypeEqualTo(AddressSubType.change) .copyWith(otherData: kReservedFusionAddress);
.derivationPath((q) => q.not().valueStartsWith("m/44'/0'"))
.sortByDerivationIndexDesc()
.derivationIndexProperty()
.findFirst();
Address address = await _generateAddressForChain( final _address = await _db.getAddress(_walletId, address.value);
1, // change chain
highestChangeIndex ?? 0,
DerivePathTypeExt.primaryFor(_coin),
);
address = address.copyWith(otherData: kReservedFusionAddress);
// TODO if we really want to be sure it's not used, call electrumx and check it
Address? _address = await _db.getAddress(_walletId, address.value);
if (_address != null) { if (_address != null) {
// throw Exception("Address already exists");
await _db.updateAddress(_address, address); await _db.updateAddress(_address, address);
} else { } else {
await _db.putAddress(address); await _db.putAddress(address);
@ -145,7 +121,10 @@ mixin FusionWalletInterface {
final txns = await _db final txns = await _db
.getTransactions(_walletId) .getTransactions(_walletId)
.filter() .filter()
.address((q) => q.otherDataEqualTo(kReservedFusionAddress)) .address((q) => q
.otherDataEqualTo(kReservedFusionAddress)
.and()
.subTypeEqualTo(AddressSubType.change))
.findAll(); .findAll();
// Fetch all addresses that have been used in a transaction. // Fetch all addresses that have been used in a transaction.
@ -159,6 +138,8 @@ mixin FusionWalletInterface {
.getAddresses(_walletId) .getAddresses(_walletId)
.filter() .filter()
.otherDataEqualTo(kReservedFusionAddress) .otherDataEqualTo(kReservedFusionAddress)
.and()
.subTypeEqualTo(AddressSubType.change)
.findAll(); .findAll();
// Initialize a list of unused reserved change addresses. // Initialize a list of unused reserved change addresses.