Change way for generate change and receive addresses.

M committed Jan 18, 2022 at 18:27 UTC 25607bdcae05179868b67512240a92a418ecfea4
9 files changed +267 -132
cw_bitcoin/lib/bitcoin_address_record.dart
+20 -8
@@ -1,14 +1,18 @@
1 import 'dart:convert';
2
3 class BitcoinAddressRecord {
4 - BitcoinAddressRecord(this.address, {this.index, bool isHidden})
5 - : _isHidden = isHidden;
4 + BitcoinAddressRecord(this.address,
5 + {this.index, this.isHidden = false, bool isUsed = false})
6 + : _isUsed = isUsed;
7
8 factory BitcoinAddressRecord.fromJSON(String jsonSource) {
9 final decoded = json.decode(jsonSource) as Map;
10
10 - return BitcoinAddressRecord(decoded['address'] as String,
11 - index: decoded['index'] as int, isHidden: decoded['isHidden'] as bool);
11 + return BitcoinAddressRecord(
12 + decoded['address'] as String,
13 + index: decoded['index'] as int,
14 + isHidden: decoded['isHidden'] as bool ?? false,
15 + isUsed: decoded['isUsed'] as bool ?? false);
16 }
17
18 @override
@@ -16,13 +20,21 @@ class BitcoinAddressRecord {
20 o is BitcoinAddressRecord && address == o.address;
21
22 final String address;
19 - bool get isHidden => _isHidden ?? false;
20 - int index;
21 - final bool _isHidden;
23 + final bool isHidden;
24 + final int index;
25 + bool get isUsed => _isUsed;
26
27 @override
28 int get hashCode => address.hashCode;
29
30 + bool _isUsed;
31 +
32 + void setAsUsed() => _isUsed = true;
33 +
34 String toJSON() =>
27 - json.encode({'address': address, 'index': index, 'isHidden': isHidden});
35 + json.encode({
36 + 'address': address,
37 + 'index': index,
38 + 'isHidden': isHidden,
39 + 'isUsed': isUsed});
40 }
cw_bitcoin/lib/bitcoin_wallet.dart
+7 -3
@@ -23,7 +23,8 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
23 @required Box<UnspentCoinsInfo> unspentCoinsInfo,
24 List<BitcoinAddressRecord> initialAddresses,
25 ElectrumBalance initialBalance,
26 - int accountIndex = 0})
26 + int initialRegularAddressIndex = 0,
27 + int initialChangeAddressIndex = 0})
28 : super(
29 mnemonic: mnemonic,
30 password: password,
@@ -34,8 +35,10 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
35 initialBalance: initialBalance) {
36 walletAddresses = BitcoinWalletAddresses(
37 walletInfo,
38 + electrumClient: electrumClient,
39 initialAddresses: initialAddresses,
38 - accountIndex: accountIndex,
40 + initialRegularAddressIndex: initialRegularAddressIndex,
41 + initialChangeAddressIndex: initialChangeAddressIndex,
42 mainHd: hd,
43 sideHd: bitcoin.HDWallet.fromSeed(
44 mnemonicToSeedBytes(mnemonic), network: networkType)
@@ -58,6 +61,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
61 unspentCoinsInfo: unspentCoinsInfo,
62 initialAddresses: snp.addresses,
63 initialBalance: snp.balance,
61 - accountIndex: snp.accountIndex);
64 + initialRegularAddressIndex: snp.regularAddressIndex,
65 + initialChangeAddressIndex: snp.changeAddressIndex);
66 }
67 }
cw_bitcoin/lib/bitcoin_wallet_addresses.dart
+10 -6
@@ -1,4 +1,5 @@
1 import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
2 +import 'package:cw_bitcoin/electrum.dart';
3 import 'package:cw_bitcoin/utils.dart';
4 import 'package:cw_bitcoin/bitcoin_address_record.dart';
5 import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
@@ -16,18 +17,21 @@ abstract class BitcoinWalletAddressesBase extends ElectrumWalletAddresses
17 BitcoinWalletAddressesBase(
18 WalletInfo walletInfo,
19 {@required List<BitcoinAddressRecord> initialAddresses,
19 - int accountIndex = 0,
20 + int initialRegularAddressIndex = 0,
21 + int initialChangeAddressIndex = 0,
22 + ElectrumClient electrumClient,
23 @required bitcoin.HDWallet mainHd,
24 @required bitcoin.HDWallet sideHd,
22 - @required this.networkType})
25 + @required bitcoin.NetworkType networkType})
26 : super(
27 walletInfo,
28 initialAddresses: initialAddresses,
26 - accountIndex: accountIndex,
29 + initialRegularAddressIndex: initialRegularAddressIndex,
30 + initialChangeAddressIndex: initialChangeAddressIndex,
31 mainHd: mainHd,
28 - sideHd: sideHd);
29 -
30 - bitcoin.NetworkType networkType;
32 + sideHd: sideHd,
33 + electrumClient: electrumClient,
34 + networkType: networkType);
35
36 @override
37 String getAddress({@required int index, @required bitcoin.HDWallet hd}) =>
cw_bitcoin/lib/electrum_wallet.dart
+32 -18
@@ -125,6 +125,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
125 Future<void> startSync() async {
126 try {
127 syncStatus = StartingSyncStatus();
128 + await walletAddresses.discoverAddresses();
129 await updateTransactions();
130 _subscribeForUpdates();
131 await _updateBalance();
@@ -206,12 +207,10 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
207 }
208
209 amount = credentialsAmount;
209 -
210 fee = calculateEstimatedFee(transactionCredentials.priority, amount,
211 outputsCount: outputs.length + 1);
212 } else {
213 final output = outputs.first;
214 -
214 credentialsAmount = !output.sendAll
215 ? output.formattedCryptoAmount
216 : 0;
@@ -223,7 +222,6 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
222 amount = output.sendAll || allAmount - credentialsAmount < minAmount
223 ? allAmount
224 : credentialsAmount;
226 -
225 fee = output.sendAll || amount == allAmount
226 ? allAmountFee
227 : calculateEstimatedFee(transactionCredentials.priority, amount);
@@ -240,7 +238,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
238 }
239
240 final txb = bitcoin.TransactionBuilder(network: networkType);
243 - final changeAddress = walletAddresses.addresses.last.address;
241 + final changeAddress = await walletAddresses.getChangeAddress();
242 var leftAmount = totalAmount;
243 var totalInputAmount = 0;
244
@@ -267,7 +265,6 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
265 }
266
267 txb.setVersion(1);
270 -
268 inputs.forEach((input) {
269 if (input.isP2wpkh) {
270 final p2wpkh = bitcoin
@@ -288,11 +285,9 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
285 final outputAmount = hasMultiDestination
286 ? item.formattedCryptoAmount
287 : amount;
291 -
288 final outputAddress = item.isParsedAddress
289 ? item.extractedAddress
290 : item.address;
295 -
291 txb.addOutput(
292 addressToOutputScript(outputAddress, networkType),
293 outputAmount);
@@ -328,7 +323,8 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
323
324 String toJSON() => json.encode({
325 'mnemonic': mnemonic,
331 - 'account_index': walletAddresses.accountIndex.toString(),
326 + 'account_index': walletAddresses.currentReceiveAddressIndex.toString(),
327 + 'change_address_index': walletAddresses.currentChangeAddressIndex.toString(),
328 'addresses': walletAddresses.addresses.map((addr) => addr.toJSON()).toList(),
329 'balance': balance?.toJSON()
330 });
@@ -563,16 +559,34 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
559 }
560
561 Future<ElectrumBalance> _fetchBalances() async {
566 - final balances = await Future.wait(
567 - scriptHashes.map((sh) => electrumClient.getBalance(sh)));
568 - final balance = balances.fold(
569 - ElectrumBalance(confirmed: 0, unconfirmed: 0),
570 - (ElectrumBalance acc, val) => ElectrumBalance(
571 - confirmed: (val['confirmed'] as int ?? 0) + (acc.confirmed ?? 0),
572 - unconfirmed:
573 - (val['unconfirmed'] as int ?? 0) + (acc.unconfirmed ?? 0)));
574 -
575 - return balance;
562 + final addresses = walletAddresses.addresses.toList();
563 + final balanceFutures = <Future<Map<String, dynamic>>>[];
564 +
565 + for (var i = 0; i < addresses.length; i++) {
566 + final addressRecord = addresses[i];
567 + final sh = scriptHash(addressRecord.address, networkType: networkType);
568 + final balanceFuture = electrumClient.getBalance(sh);
569 + balanceFutures.add(balanceFuture);
570 + }
571 +
572 + final balances = await Future.wait(balanceFutures);
573 + var totalConfirmed = 0;
574 + var totalUnconfirmed = 0;
575 +
576 + for (var i = 0; i < balances.length; i++) {
577 + final addressRecord = addresses[i];
578 + final balance = balances[i];
579 + final confirmed = balance['confirmed'] as int ?? 0;
580 + final unconfirmed = balance['unconfirmed'] as int ?? 0;
581 + totalConfirmed += confirmed;
582 + totalUnconfirmed += unconfirmed;
583 +
584 + if (confirmed > 0 || unconfirmed > 0) {
585 + addressRecord.setAsUsed();
586 + }
587 + }
588 +
589 + return ElectrumBalance(confirmed: totalConfirmed, unconfirmed: totalUnconfirmed);
590 }
591
592 Future<void> _updateBalance() async {
cw_bitcoin/lib/electrum_wallet_addresses.dart
+174 -72
@@ -1,5 +1,7 @@
1 import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
2 import 'package:cw_bitcoin/bitcoin_address_record.dart';
3 +import 'package:cw_bitcoin/electrum.dart';
4 +import 'package:cw_bitcoin/script_hash.dart';
5 import 'package:cw_core/wallet_addresses.dart';
6 import 'package:cw_core/wallet_info.dart';
7 import 'package:flutter/foundation.dart';
@@ -14,120 +16,118 @@ class ElectrumWalletAddresses = ElectrumWalletAddressesBase
16 abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
17 ElectrumWalletAddressesBase(WalletInfo walletInfo,
18 {@required List<BitcoinAddressRecord> initialAddresses,
17 - int accountIndex = 0,
19 + int initialRegularAddressIndex = 0,
20 + int initialChangeAddressIndex = 0,
21 this.mainHd,
19 - this.sideHd})
22 + this.sideHd,
23 + this.electrumClient,
24 + this.networkType})
25 : super(walletInfo) {
21 - this.accountIndex = accountIndex;
26 + currentReceiveAddressIndex = initialRegularAddressIndex;
27 + currentChangeAddressIndex = initialChangeAddressIndex;
28 addresses = ObservableList<BitcoinAddressRecord>.of(
29 (initialAddresses ?? []).toSet());
30 }
31
26 - static const regularAddressesCount = 22;
27 - static const hiddenAddressesCount = 17;
32 + static const defaultReceiveAddressesCount = 22;
33 + static const defaultChangeAddressesCount = 17;
34 + static const gap = 20;
35
36 @override
37 @observable
38 String address;
39
40 + int currentReceiveAddressIndex;
41 + int currentChangeAddressIndex;
42 + ElectrumClient electrumClient;
43 + bitcoin.NetworkType networkType;
44 bitcoin.HDWallet mainHd;
45 bitcoin.HDWallet sideHd;
35 -
46 ObservableList<BitcoinAddressRecord> addresses;
47
38 - List<BitcoinAddressRecord> get availableAddresses => addresses
39 - .where((addr) => !addr.isHidden)
48 + List<BitcoinAddressRecord> get receiveAddresses => addresses
49 + .where((addr) => !addr.isHidden && !addr.isUsed)
50 .toList();
51
42 - int accountIndex;
52 + List<BitcoinAddressRecord> get changeAddresses => addresses
53 + .where((addr) => addr.isHidden && !addr.isUsed)
54 + .toList();
55 +
56 + Future<void> discoverAddresses() async {
57 + await _discoverAddresses(mainHd, false);
58 + await _discoverAddresses(sideHd, true);
59 + await updateAddressesInBox();
60 + }
61
62 @override
63 Future<void> init() async {
46 - await generateAddresses();
47 - final _availableAddresses = availableAddresses;
48 -
49 - if (accountIndex >= _availableAddresses.length) {
50 - accountIndex = 0;
64 + await _generateInitialAddresses();
65 +
66 + if (receiveAddresses.isEmpty) {
67 + final count = currentReceiveAddressIndex + gap;
68 + final newAddresses = await _createNewAddresses(
69 + count,
70 + hd: mainHd,
71 + startIndex: currentReceiveAddressIndex,
72 + isHidden: false);
73 + _addAddresses(newAddresses);
74 + } else if (currentReceiveAddressIndex >= receiveAddresses.length) {
75 + currentReceiveAddressIndex = 0;
76 }
77
53 - address = _availableAddresses[accountIndex].address;
78 + address = receiveAddresses[currentReceiveAddressIndex].address;
79 await updateAddressesInBox();
80 }
81
82 @action
58 - Future<void> nextAddress() async {
59 - accountIndex += 1;
60 - final _availableAddresses = availableAddresses;
61 -
62 - if (accountIndex >= _availableAddresses.length) {
63 - accountIndex = 0;
83 + Future<void> nextReceiveAddress() async {
84 + if (receiveAddresses.isEmpty) {
85 + final count = currentReceiveAddressIndex + gap;
86 + final newAddresses = await _createNewAddresses(
87 + count,
88 + hd: sideHd,
89 + startIndex: currentReceiveAddressIndex,
90 + isHidden: false);
91 + _addAddresses(newAddresses);
92 + } else if (currentReceiveAddressIndex >= receiveAddresses.length) {
93 + currentReceiveAddressIndex = 0;
94 }
95
66 - address = _availableAddresses[accountIndex].address;
67 -
96 + address = receiveAddresses[currentReceiveAddressIndex].address;
97 + currentReceiveAddressIndex += 1;
98 await updateAddressesInBox();
99 }
100
71 - Future<void> generateAddresses() async {
72 - final regularAddresses = <BitcoinAddressRecord>[];
73 - final hiddenAddresses = <BitcoinAddressRecord>[];
74 -
75 - addresses.forEach((addr) {
76 - if (addr.isHidden) {
77 - hiddenAddresses.add(addr);
78 - return;
79 - }
80 -
81 - regularAddresses.add(addr);
82 - });
83 -
84 - if (regularAddresses.length < regularAddressesCount) {
85 - final addressesCount = regularAddressesCount - regularAddresses.length;
86 - await generateNewAddresses(addressesCount,
87 - startIndex: regularAddresses.length, hd: mainHd, isHidden: false);
101 + @action
102 + Future<String> getChangeAddress() async {
103 + if (changeAddresses.isEmpty) {
104 + final count = currentChangeAddressIndex + gap;
105 + final newAddresses = await _createNewAddresses(
106 + count,
107 + startIndex: currentChangeAddressIndex,
108 + isHidden: true);
109 + _addAddresses(newAddresses);
110 + } else if (currentChangeAddressIndex >= changeAddresses.length) {
111 + currentChangeAddressIndex = 0;
112 }
113
90 - if (hiddenAddresses.length < hiddenAddressesCount) {
91 - final addressesCount = hiddenAddressesCount - hiddenAddresses.length;
92 - await generateNewAddresses(addressesCount,
93 - startIndex: hiddenAddresses.length, hd: sideHd, isHidden: true);
94 - }
114 +
115 + final address = changeAddresses[currentChangeAddressIndex].address;
116 + currentChangeAddressIndex += 1;
117 + return address;
118 }
119
120 Future<BitcoinAddressRecord> generateNewAddress(
121 {bool isHidden = false, bitcoin.HDWallet hd}) async {
99 - accountIndex += 1;
122 + currentReceiveAddressIndex += 1;
123 final address = BitcoinAddressRecord(
101 - getAddress(index: accountIndex, hd: hd),
102 - index: accountIndex,
124 + getAddress(index: currentReceiveAddressIndex, hd: hd),
125 + index: currentReceiveAddressIndex,
126 isHidden: isHidden);
127 addresses.add(address);
128 return address;
129 }
130
108 - Future<List<BitcoinAddressRecord>> generateNewAddresses(int count,
109 - {int startIndex = 0, bitcoin.HDWallet hd, bool isHidden = false}) async {
110 - final list = <BitcoinAddressRecord>[];
111 -
112 - for (var i = startIndex; i < count + startIndex; i++) {
113 - final address = BitcoinAddressRecord(getAddress(index: i, hd: hd),
114 - index: i, isHidden: isHidden);
115 - list.add(address);
116 - }
117 -
118 - addresses.addAll(list);
119 - return list;
120 - }
121 -
122 - /*Future<void> updateAddress(String address) async {
123 - for (final addr in addresses) {
124 - if (addr.address == address) {
125 - await save();
126 - break;
127 - }
128 - }
129 - }*/
130 -
131 String getAddress({@required int index, @required bitcoin.HDWallet hd}) => '';
132
133 @override
@@ -135,7 +135,6 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
135 try {
136 addressesMap.clear();
137 addressesMap[address] = '';
138 -
138 await saveAddressesInBox();
139 } catch (e) {
140 print(e.toString());
@@ -155,4 +154,107 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
154
155 address = availableAddresses[random.nextInt(availableAddresses.length)].address;
156 }
157 +
158 + Future<void> _discoverAddresses(bitcoin.HDWallet hd, bool isHidden) async {
159 + var hasAddrUse = true;
160 + List<BitcoinAddressRecord> addrs;
161 +
162 + if (addresses.isNotEmpty) {
163 + addrs = addresses
164 + .where((addr) => addr.isHidden == isHidden)
165 + .toList();
166 + } else {
167 + addrs = await _createNewAddresses(
168 + isHidden
169 + ? defaultChangeAddressesCount
170 + : defaultReceiveAddressesCount,
171 + startIndex: 0,
172 + hd: hd,
173 + isHidden: isHidden);
174 + }
175 +
176 + while(hasAddrUse) {
177 + final addr = addrs.last.address;
178 + hasAddrUse = await _validateAddressUsing(addr);
179 +
180 + if (!hasAddrUse) {
181 + break;
182 + }
183 +
184 + final start = addrs.length;
185 + final count = start + gap;
186 + final batch = await _createNewAddresses(
187 + count,
188 + startIndex: start,
189 + hd: hd,
190 + isHidden: isHidden);
191 + addrs.addAll(batch);
192 + }
193 +
194 + if (addresses.length < addrs.length) {
195 + _addAddresses(addrs);
196 + }
197 + }
198 +
199 + Future<void> _generateInitialAddresses() async {
200 + var countOfReceiveAddresses = 0;
201 + var countOfHiddenAddresses = 0;
202 +
203 + addresses.forEach((addr) {
204 + if (addr.isHidden) {
205 + countOfHiddenAddresses += 1;
206 + return;
207 + }
208 +
209 + countOfReceiveAddresses += 1;
210 + });
211 +
212 + if (countOfReceiveAddresses < defaultReceiveAddressesCount) {
213 + final addressesCount = defaultReceiveAddressesCount - countOfReceiveAddresses;
214 + final newAddresses = await _createNewAddresses(
215 + addressesCount,
216 + startIndex: countOfReceiveAddresses,
217 + hd: mainHd,
218 + isHidden: false);
219 + addresses.addAll(newAddresses);
220 + }
221 +
222 + if (countOfHiddenAddresses < defaultChangeAddressesCount) {
223 + final addressesCount = defaultChangeAddressesCount - countOfHiddenAddresses;
224 + final newAddresses = await _createNewAddresses(
225 + addressesCount,
226 + startIndex: countOfHiddenAddresses,
227 + hd: sideHd,
228 + isHidden: true);
229 + addresses.addAll(newAddresses);
230 + }
231 + }
232 +
233 + Future<List<BitcoinAddressRecord>> _createNewAddresses(int count,
234 + {int startIndex = 0, bitcoin.HDWallet hd, bool isHidden = false}) async {
235 + final list = <BitcoinAddressRecord>[];
236 +
237 + for (var i = startIndex; i < count + startIndex; i++) {
238 + final address = BitcoinAddressRecord(
239 + getAddress(index: i, hd: hd),
240 + index: i,
241 + isHidden: isHidden);
242 + list.add(address);
243 + }
244 +
245 + return list;
246 + }
247 +
248 + void _addAddresses(Iterable<BitcoinAddressRecord> addresses) {
249 + final addressesSet = this.addresses.toSet();
250 + addressesSet.addAll(addresses);
251 + this.addresses.removeRange(0, this.addresses.length);
252 + this.addresses.addAll(addressesSet);
253 + }
254 +
255 + Future<bool> _validateAddressUsing(String address) async {
256 + final sh = scriptHash(address, networkType: networkType);
257 + final balance = await electrumClient.getBalance(sh);
258 + return balance.isEmpty;
259 + }
260 }
\ No newline at end of file
cw_bitcoin/lib/electrum_wallet_snapshot.dart
+6 -3
@@ -15,7 +15,8 @@ class ElectrumWallletSnapshot {
15 String mnemonic;
16 List<BitcoinAddressRecord> addresses;
17 ElectrumBalance balance;
18 - int accountIndex;
18 + int regularAddressIndex;
19 + int changeAddressIndex;
20
21 Future<void> load() async {
22 try {
@@ -30,10 +31,12 @@ class ElectrumWallletSnapshot {
31 .toList();
32 balance = ElectrumBalance.fromJSON(data['balance'] as String) ??
33 ElectrumBalance(confirmed: 0, unconfirmed: 0);
33 - accountIndex = 0;
34 + regularAddressIndex = 0;
35 + changeAddressIndex = 0;
36
37 try {
36 - accountIndex = int.parse(data['account_index'] as String);
38 + regularAddressIndex = int.parse(data['account_index'] as String);
39 + changeAddressIndex = int.parse(data['change_address_index'] as String);
40 } catch (_) {}
41 } catch (e) {
42 print(e);
cw_bitcoin/lib/litecoin_wallet.dart
+7 -3
@@ -26,7 +26,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
26 @required Box<UnspentCoinsInfo> unspentCoinsInfo,
27 List<BitcoinAddressRecord> initialAddresses,
28 ElectrumBalance initialBalance,
29 - int accountIndex = 0})
29 + int initialRegularAddressIndex = 0,
30 + int initialChangeAddressIndex = 0})
31 : super(
32 mnemonic: mnemonic,
33 password: password,
@@ -37,8 +38,10 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
38 initialBalance: initialBalance) {
39 walletAddresses = LitecoinWalletAddresses(
40 walletInfo,
41 + electrumClient: electrumClient,
42 initialAddresses: initialAddresses,
41 - accountIndex: accountIndex,
43 + initialRegularAddressIndex: initialRegularAddressIndex,
44 + initialChangeAddressIndex: initialChangeAddressIndex,
45 mainHd: hd,
46 sideHd: bitcoin.HDWallet
47 .fromSeed(mnemonicToSeedBytes(mnemonic), network: networkType)
@@ -61,7 +64,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
64 unspentCoinsInfo: unspentCoinsInfo,
65 initialAddresses: snp.addresses,
66 initialBalance: snp.balance,
64 - accountIndex: snp.accountIndex);
67 + initialRegularAddressIndex: snp.regularAddressIndex,
68 + initialChangeAddressIndex: snp.changeAddressIndex);
69 }
70
71 @override
cw_bitcoin/lib/litecoin_wallet_addresses.dart
+10 -18
@@ -1,4 +1,5 @@
1 import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
2 +import 'package:cw_bitcoin/electrum.dart';
3 import 'package:cw_bitcoin/utils.dart';
4 import 'package:cw_bitcoin/bitcoin_address_record.dart';
5 import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
@@ -16,32 +17,23 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses
17 LitecoinWalletAddressesBase(
18 WalletInfo walletInfo,
19 {@required List<BitcoinAddressRecord> initialAddresses,
19 - int accountIndex = 0,
20 + int initialRegularAddressIndex = 0,
21 + int initialChangeAddressIndex = 0,
22 + ElectrumClient electrumClient,
23 @required bitcoin.HDWallet mainHd,
24 @required bitcoin.HDWallet sideHd,
22 - @required this.networkType})
25 + @required bitcoin.NetworkType networkType})
26 : super(
27 walletInfo,
28 initialAddresses: initialAddresses,
26 - accountIndex: accountIndex,
29 + initialRegularAddressIndex: initialRegularAddressIndex,
30 + initialChangeAddressIndex: initialChangeAddressIndex,
31 mainHd: mainHd,
28 - sideHd: sideHd);
29 -
30 - bitcoin.NetworkType networkType;
31 -
32 + sideHd: sideHd,
33 + electrumClient: electrumClient,
34 + networkType: networkType);
35
36 @override
37 String getAddress({@required int index, @required bitcoin.HDWallet hd}) =>
38 generateP2WPKHAddress(hd: hd, index: index, networkType: networkType);
36 -
37 - @override
38 - Future<void> generateAddresses() async {
39 - if (addresses.length < 33) {
40 - final addressesCount = 22 - addresses.length;
41 - await generateNewAddresses(addressesCount,
42 - hd: mainHd, startIndex: addresses.length);
43 - await generateNewAddresses(11,
44 - startIndex: 0, hd: sideHd, isHidden: true);
45 - }
46 - }
39 }
\ No newline at end of file
lib/bitcoin/cw_bitcoin.dart
+1 -1
@@ -57,7 +57,7 @@ class CWBitcoin extends Bitcoin {
57 @override
58 Future<void> nextAddress(Object wallet) {
59 final bitcoinWallet = wallet as ElectrumWallet;
60 - bitcoinWallet.walletAddresses.nextAddress();
60 + bitcoinWallet.walletAddresses.nextReceiveAddress();
61 }
62
63 @override