Skip to content

Commit a4a7abd

Browse files
committed
look ahead on frost wallet addresses to check for transactions on future addresses
1 parent 6a9c58d commit a4a7abd

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

lib/wallets/wallet/impl/bitcoin_frost_wallet.dart

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,98 @@ class BitcoinFrostWallet<T extends FrostCurrency> extends Wallet<T>
14551455
);
14561456
}
14571457

1458+
Future<void> lookAhead() async {
1459+
Address? currentReceiving = await getCurrentReceivingAddress();
1460+
if (currentReceiving == null) {
1461+
await generateNewReceivingAddress();
1462+
currentReceiving = await getCurrentReceivingAddress();
1463+
}
1464+
Address? currentChange = await getCurrentChangeAddress();
1465+
if (currentChange == null) {
1466+
await generateNewChangeAddress();
1467+
currentChange = await getCurrentChangeAddress();
1468+
}
1469+
1470+
final List<Address> nextReceivingAddresses = [];
1471+
final List<Address> nextChangeAddresses = [];
1472+
1473+
int receiveIndex = currentReceiving!.derivationIndex;
1474+
int changeIndex = currentChange!.derivationIndex;
1475+
for (int i = 0; i < 10; i++) {
1476+
final receiveAddress = await _generateAddressSafe(
1477+
chain: 0,
1478+
startingIndex: receiveIndex + 1,
1479+
);
1480+
receiveIndex = receiveAddress.derivationIndex;
1481+
nextReceivingAddresses.add(receiveAddress);
1482+
1483+
final changeAddress = await _generateAddressSafe(
1484+
chain: 1,
1485+
startingIndex: changeIndex + 1,
1486+
);
1487+
changeIndex = changeAddress.derivationIndex;
1488+
nextChangeAddresses.add(changeAddress);
1489+
}
1490+
1491+
int activeReceiveIndex = currentReceiving.derivationIndex;
1492+
int activeChangeIndex = currentChange.derivationIndex;
1493+
for (final address in nextReceivingAddresses) {
1494+
final txCount = await _fetchTxCount(address: address);
1495+
if (txCount > 0) {
1496+
activeReceiveIndex = max(activeReceiveIndex, address.derivationIndex);
1497+
}
1498+
}
1499+
for (final address in nextChangeAddresses) {
1500+
final txCount = await _fetchTxCount(address: address);
1501+
if (txCount > 0) {
1502+
activeChangeIndex = max(activeChangeIndex, address.derivationIndex);
1503+
}
1504+
}
1505+
1506+
nextReceivingAddresses
1507+
.removeWhere((e) => e.derivationIndex > activeReceiveIndex);
1508+
if (nextReceivingAddresses.isNotEmpty) {
1509+
await mainDB.updateOrPutAddresses(nextReceivingAddresses);
1510+
await info.updateReceivingAddress(
1511+
newAddress: nextReceivingAddresses.last.value,
1512+
isar: mainDB.isar,
1513+
);
1514+
}
1515+
nextChangeAddresses
1516+
.removeWhere((e) => e.derivationIndex > activeChangeIndex);
1517+
if (nextChangeAddresses.isNotEmpty) {
1518+
await mainDB.updateOrPutAddresses(nextChangeAddresses);
1519+
}
1520+
}
1521+
1522+
Future<Address> _generateAddressSafe({
1523+
required final int chain,
1524+
required int startingIndex,
1525+
}) async {
1526+
final serializedKeys = (await getSerializedKeys())!;
1527+
1528+
Address? address;
1529+
while (address == null) {
1530+
try {
1531+
address = await _generateAddress(
1532+
change: chain,
1533+
index: startingIndex,
1534+
serializedKeys: serializedKeys,
1535+
);
1536+
} on FrostdartException catch (e) {
1537+
if (e.errorCode == 72) {
1538+
// rust doesn't like the addressDerivationData
1539+
startingIndex++;
1540+
continue;
1541+
} else {
1542+
rethrow;
1543+
}
1544+
}
1545+
}
1546+
1547+
return address;
1548+
}
1549+
14581550
/// Can and will often throw unless [index], [change], and [account] are zero.
14591551
/// Caller MUST handle exception!
14601552
Future<Address> _generateAddress({

lib/wallets/wallet/wallet.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ abstract class Wallet<T extends CryptoCurrency> {
518518
GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.0, walletId));
519519
await updateChainHeight();
520520

521+
if (this is BitcoinFrostWallet) {
522+
await (this as BitcoinFrostWallet).lookAhead();
523+
}
524+
521525
GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.1, walletId));
522526

523527
// TODO: [prio=low] handle this differently. Extra modification of this file for coin specific functionality should be avoided.

0 commit comments

Comments
 (0)