From 31a67fa30dba1e102847bcafd341aa00cf46a189 Mon Sep 17 00:00:00 2001 From: M Date: Tue, 18 Jan 2022 19:10:37 +0200 Subject: [PATCH] Update logic for creation receive and change addresses --- cw_bitcoin/lib/electrum_wallet_addresses.dart | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet_addresses.dart b/cw_bitcoin/lib/electrum_wallet_addresses.dart index 5df3047af..fb0406706 100644 --- a/cw_bitcoin/lib/electrum_wallet_addresses.dart +++ b/cw_bitcoin/lib/electrum_wallet_addresses.dart @@ -53,6 +53,14 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { .where((addr) => addr.isHidden && !addr.isUsed) .toList(); + List get totalReceiveAddresses => addresses + .where((addr) => !addr.isHidden) + .toList(); + + List get totalChangeAddresses => addresses + .where((addr) => addr.isHidden) + .toList(); + Future discoverAddresses() async { await _discoverAddresses(mainHd, false); await _discoverAddresses(sideHd, true); @@ -64,11 +72,12 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { await _generateInitialAddresses(); if (receiveAddresses.isEmpty) { - final count = currentReceiveAddressIndex + gap; final newAddresses = await _createNewAddresses( - count, + gap, hd: mainHd, - startIndex: currentReceiveAddressIndex, + startIndex: totalReceiveAddresses.length > 0 + ? totalReceiveAddresses.length - 1 + : 0, isHidden: false); _addAddresses(newAddresses); } else if (currentReceiveAddressIndex >= receiveAddresses.length) { @@ -82,11 +91,12 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { @action Future nextReceiveAddress() async { if (receiveAddresses.isEmpty) { - final count = currentReceiveAddressIndex + gap; final newAddresses = await _createNewAddresses( - count, - hd: sideHd, - startIndex: currentReceiveAddressIndex, + gap, + hd: mainHd, + startIndex: totalReceiveAddresses.length > 0 + ? totalReceiveAddresses.length - 1 + : 0, isHidden: false); _addAddresses(newAddresses); } else if (currentReceiveAddressIndex >= receiveAddresses.length) { @@ -101,10 +111,12 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { @action Future getChangeAddress() async { if (changeAddresses.isEmpty) { - final count = currentChangeAddressIndex + gap; final newAddresses = await _createNewAddresses( - count, - startIndex: currentChangeAddressIndex, + gap, + hd: sideHd, + startIndex: totalChangeAddresses.length > 0 + ? totalChangeAddresses.length - 1 + : 0, isHidden: true); _addAddresses(newAddresses); } else if (currentChangeAddressIndex >= changeAddresses.length) {