WIP reserve addresses stuff

This commit is contained in:
julian 2023-08-07 12:54:44 -06:00
parent cdbbb0a3df
commit e4aa34509a
2 changed files with 84 additions and 23 deletions

View file

@ -136,11 +136,11 @@ class BitcoinCashWallet extends CoinServiceAPI
initCache(walletId, coin); initCache(walletId, coin);
initWalletDB(mockableOverride: mockableOverride); initWalletDB(mockableOverride: mockableOverride);
initFusionInterface( initFusionInterface(
walletId: walletId, walletId: walletId,
coin: coin, coin: coin,
db: db, db: db,
getCurrentChangeAddress: () => currentChangeAddress, generateAddressForChain: _generateAddressForChain,
getNextChangeAddress: () => nextChangeAddress); );
initCoinControlInterface( initCoinControlInterface(
walletId: walletId, walletId: walletId,
walletName: walletName, walletName: walletName,
@ -1569,6 +1569,8 @@ class BitcoinCashWallet extends CoinServiceAPI
.typeEqualTo(type) .typeEqualTo(type)
.subTypeEqualTo(subType) .subTypeEqualTo(subType)
.derivationPath((q) => q.valueStartsWith("m/$purpose'/$coinType")) .derivationPath((q) => q.valueStartsWith("m/$purpose'/$coinType"))
.not()
.otherDataEqualTo(kReservedFusionAddress)
.sortByDerivationIndexDesc() .sortByDerivationIndexDesc()
.findFirst(); .findFirst();
return address!.value; return address!.value;
@ -1829,8 +1831,10 @@ class BitcoinCashWallet extends CoinServiceAPI
// Add that new change address // Add that new change address
await db.putAddress(newChangeAddress); await db.putAddress(newChangeAddress);
} else { } else {
// we need to update the address if (existing.otherData != kReservedFusionAddress) {
await db.updateAddress(existing, newChangeAddress); // we need to update the address
await db.updateAddress(existing, newChangeAddress);
}
} }
// keep checking until address with no tx history is set as current // keep checking until address with no tx history is set as current
await _checkChangeAddressForTransactions(); await _checkChangeAddressForTransactions();

View file

@ -4,11 +4,13 @@ import 'package:fusiondart/fusiondart.dart';
import 'package:fusiondart/src/models/address.dart' as fusion_address; import 'package:fusiondart/src/models/address.dart' as fusion_address;
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/db/isar/main_db.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.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/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
const String kReservedFusionAddress = "reserved_fusion_address";
mixin FusionInterface { mixin FusionInterface {
// passed in wallet data // passed in wallet data
late final String _walletId; late final String _walletId;
@ -16,33 +18,88 @@ mixin FusionInterface {
late final MainDB _db; late final MainDB _db;
// passed in wallet functions // passed in wallet functions
late final Future<String> Function() _getCurrentChangeAddress; late final Future<Address> Function(
late final Future<String> Function() _getNextChangeAddress; int chain,
int index,
DerivePathType derivePathType,
) _generateAddressForChain;
void initFusionInterface({ void initFusionInterface({
required String walletId, required String walletId,
required Coin coin, required Coin coin,
required MainDB db, required MainDB db,
required Future<String> Function() getCurrentChangeAddress, required Future<Address> Function(
required Future<String> Function() getNextChangeAddress, int,
int,
DerivePathType,
) generateAddressForChain,
}) { }) {
_walletId = walletId; _walletId = walletId;
_coin = coin; _coin = coin;
_db = db; _db = db;
_getCurrentChangeAddress = getCurrentChangeAddress; _generateAddressForChain = generateAddressForChain;
_getNextChangeAddress = getNextChangeAddress;
} }
static List<Address> reserve_change_addresses(int number_addresses) { Future<Address> createNewReservedChangeAddress() async {
// TODO int? highestChangeIndex = await _db
// get current change address .getAddresses(_walletId)
// get int number_addresses next addresses .filter()
return []; .typeEqualTo(AddressType.p2pkh)
.subTypeEqualTo(AddressSubType.change)
.derivationPath((q) => q.not().valueStartsWith("m/44'/0'"))
.sortByDerivationIndexDesc()
.derivationIndexProperty()
.findFirst();
Address address = await _generateAddressForChain(
1, // change chain
highestChangeIndex ?? 0,
DerivePathTypeExt.primaryFor(_coin),
);
address = address.copyWith(otherData: kReservedFusionAddress);
// TODO if we really want to be sure its not used, call electrumx and check it
await _db.putAddress(address);
return address;
} }
static List<Address> unreserve_change_address(Address addr) { Future<List<Address>> getUnusedReservedChangeAddresses(
//implement later based on wallet. int numberOfAddresses,
return []; ) async {
final txns = await _db
.getTransactions(_walletId)
.filter()
.address((q) => q.otherDataEqualTo(kReservedFusionAddress))
.findAll();
final List<String> usedAddresses = txns
.where((e) => e.address.value != null)
.map((e) => e.address.value!.value)
.toList(growable: false);
final List<Address> addresses = await _db
.getAddresses(_walletId)
.filter()
.otherDataEqualTo(kReservedFusionAddress)
.findAll();
final List<Address> unusedAddresses = [];
for (final address in addresses) {
if (!usedAddresses.contains(address.value)) {
unusedAddresses.add(address);
}
}
if (unusedAddresses.length < numberOfAddresses) {
for (int i = unusedAddresses.length; i < numberOfAddresses; i++) {
unusedAddresses.add(await createNewReservedChangeAddress());
}
}
return unusedAddresses;
} }
void fuse() async { void fuse() async {