Changed way for generation of change address for electrum wallets. Remove some yats call.
M committed
Jan 12, 2022 at 15:20 UTC
cfb82dd02de624a0e8524d9eb3046a7a4f749550
8 files changed
+94
-55
cw_bitcoin/lib/electrum_wallet.dart
+16
-1
@@ -1,5 +1,6 @@
1
import 'dart:async';
2
import 'dart:convert';
3
+import 'dart:math';
4
import 'package:cw_core/unspent_coins_info.dart';
5
import 'package:hive/hive.dart';
6
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
@@ -238,7 +239,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
239
}
240
241
final txb = bitcoin.TransactionBuilder(network: networkType);
241
- final changeAddress = walletAddresses.address;
242
+ final changeAddress = getChangeAddress();
243
var leftAmount = totalAmount;
244
var totalInputAmount = 0;
245
@@ -545,4 +546,18 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
546
balance = await _fetchBalances();
547
await save();
548
}
549
+
550
+ String getChangeAddress() {
551
+ const minCountOfHiddenAddresses = 5;
552
+ final random = Random();
553
+ var addresses = walletAddresses.addresses
554
+ .where((addr) => addr.isHidden)
555
+ .toList();
556
+
557
+ if (addresses.length < minCountOfHiddenAddresses) {
558
+ addresses = walletAddresses.addresses.toList();
559
+ }
560
+
561
+ return addresses[random.nextInt(addresses.length)].address;
562
+ }
563
}
cw_bitcoin/lib/electrum_wallet_addresses.dart
+6
@@ -4,6 +4,7 @@ import 'package:cw_core/wallet_addresses.dart';
4
import 'package:cw_core/wallet_info.dart';
5
import 'package:flutter/foundation.dart';
6
import 'package:mobx/mobx.dart';
7
+import 'dart:math';
8
9
part 'electrum_wallet_addresses.g.dart';
10
@@ -129,4 +130,9 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
130
print(e.toString());
131
}
132
}
133
+
134
+ void randomizeAddress() {
135
+ final random = Random();
136
+ address = addresses[random.nextInt(addresses.length)].address;
137
+ }
138
}
\ No newline at end of file
ios/Podfile.lock
+1
-1
@@ -196,4 +196,4 @@ SPEC CHECKSUMS:
196
197
PODFILE CHECKSUM: bc2591d23316907c9c90ca1cd2fce063fd866508
198
199
-COCOAPODS: 1.10.2
199
+COCOAPODS: 1.11.2
lib/bitcoin/cw_bitcoin.dart
+6
@@ -56,6 +56,12 @@ class CWBitcoin extends Bitcoin {
56
final bitcoinWallet = wallet as BitcoinWallet;
57
bitcoinWallet.walletAddresses.nextAddress();
58
}
59
+
60
+ @override
61
+ Future<void> randomAddress(Object wallet) {
62
+ final bitcoinWallet = wallet as BitcoinWallet;
63
+ bitcoinWallet.walletAddresses.randomizeAddress();
64
+ }
65
66
@override
67
Object createBitcoinTransactionCredentials(List<Output> outputs, TransactionPriority priority)
lib/main.dart
+13
-13
@@ -197,7 +197,7 @@ class AppState extends State<App> with SingleTickerProviderStateMixin {
197
void initState() {
198
super.initState();
199
_handleIncomingLinks();
200
- _handleInitialUri();
200
+ //_handleInitialUri();
201
}
202
203
@override
@@ -235,18 +235,18 @@ class AppState extends State<App> with SingleTickerProviderStateMixin {
235
}
236
237
void _fetchEmojiFromUri(Uri uri) {
238
- final queryParameters = uri.queryParameters;
239
- if (queryParameters?.isEmpty ?? true) {
240
- return;
241
- }
242
- final emoji = queryParameters['eid'];
243
- final refreshToken = queryParameters['refresh_token'];
244
- if ((emoji?.isEmpty ?? true)||(refreshToken?.isEmpty ?? true)) {
245
- return;
246
- }
247
- yatStore.emoji = emoji;
248
- yatStore.refreshToken = refreshToken;
249
- yatStore.emojiIncommingSC.add(emoji);
238
+ //final queryParameters = uri.queryParameters;
239
+ //if (queryParameters?.isEmpty ?? true) {
240
+ // return;
241
+ //}
242
+ //final emoji = queryParameters['eid'];
243
+ //final refreshToken = queryParameters['refresh_token'];
244
+ //if ((emoji?.isEmpty ?? true)||(refreshToken?.isEmpty ?? true)) {
245
+ // return;
246
+ //}
247
+ //yatStore.emoji = emoji;
248
+ //yatStore.refreshToken = refreshToken;
249
+ //yatStore.emojiIncommingSC.add(emoji);
250
}
251
252
@override
lib/reactions/on_current_wallet_change.dart
+21
-21
@@ -17,34 +17,34 @@ import 'package:cake_wallet/store/yat/yat_store.dart';
17
18
ReactionDisposer _onCurrentWalletChangeReaction;
19
ReactionDisposer _onCurrentWalletChangeFiatRateUpdateReaction;
20
-ReactionDisposer _onCurrentWalletAddressChangeReaction;
20
+//ReactionDisposer _onCurrentWalletAddressChangeReaction;
21
22
void startCurrentWalletChangeReaction(AppStore appStore,
23
SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
24
_onCurrentWalletChangeReaction?.reaction?.dispose();
25
_onCurrentWalletChangeFiatRateUpdateReaction?.reaction?.dispose();
26
- _onCurrentWalletAddressChangeReaction?.reaction?.dispose();
26
+ //_onCurrentWalletAddressChangeReaction?.reaction?.dispose();
27
28
- _onCurrentWalletAddressChangeReaction = reaction((_) => appStore.wallet.walletAddresses.address,
29
- (String address) async {
30
- if (address == appStore.wallet.walletInfo.yatLastUsedAddress) {
31
- return;
32
- }
28
+ //_onCurrentWalletAddressChangeReaction = reaction((_) => appStore.wallet.walletAddresses.address,
29
+ //(String address) async {
30
+ //if (address == appStore.wallet.walletInfo.yatLastUsedAddress) {
31
+ // return;
32
+ //}
33
34
- try {
35
- final yatStore = getIt.get<YatStore>();
36
- await updateEmojiIdAddress(
37
- appStore.wallet.walletInfo.yatEmojiId,
38
- appStore.wallet.walletAddresses.address,
39
- yatStore.apiKey,
40
- appStore.wallet.type
41
- );
42
- appStore.wallet.walletInfo.yatLastUsedAddress = address;
43
- await appStore.wallet.walletInfo.save();
44
- } catch (e) {
45
- print(e.toString());
46
- }
47
- });
34
+ //try {
35
+ // final yatStore = getIt.get<YatStore>();
36
+ // await updateEmojiIdAddress(
37
+ // appStore.wallet.walletInfo.yatEmojiId,
38
+ // appStore.wallet.walletAddresses.address,
39
+ // yatStore.apiKey,
40
+ // appStore.wallet.type
41
+ // );
42
+ // appStore.wallet.walletInfo.yatLastUsedAddress = address;
43
+ // await appStore.wallet.walletInfo.save();
44
+ //} catch (e) {
45
+ // print(e.toString());
46
+ //}
47
+ //});
48
49
_onCurrentWalletChangeReaction = reaction((_) => appStore.wallet, (WalletBase<
50
Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>
lib/src/screens/dashboard/widgets/address_page.dart
+10
-9
@@ -27,6 +27,7 @@ class AddressPage extends StatelessWidget {
27
28
@override
29
Widget build(BuildContext context) {
30
+ addressListViewModel.generateRandomAddress();
31
autorun((_) async {
32
if (!walletViewModel.isOutdatedElectrumWallet
33
|| !walletViewModel.settingsStore.shouldShowReceiveWarning) {
@@ -124,15 +125,15 @@ class AddressPage extends StatelessWidget {
125
],
126
),
127
),
127
- )
128
- : PrimaryButton(
129
- onPressed: () => addressListViewModel.nextAddress(),
130
- text: 'Next address',
131
- color: Theme.of(context).buttonColor,
132
- textColor: Theme.of(context)
133
- .accentTextTheme
134
- .display3
135
- .backgroundColor);
128
+ ) : Container();
129
+ //: PrimaryButton(
130
+ // onPressed: () => addressListViewModel.nextAddress(),
131
+ // text: 'Next address',
132
+ // color: Theme.of(context).buttonColor,
133
+ // textColor: Theme.of(context)
134
+ // .accentTextTheme
135
+ // .display3
136
+ // .backgroundColor);
137
})
138
],
139
),
lib/view_model/wallet_address_list/wallet_address_list_view_model.dart
+21
-10
@@ -77,16 +77,16 @@ abstract class WalletAddressListViewModelBase with Store {
77
}
78
});
79
80
- reaction((_) => yatStore.emoji, (String emojiId) => this.emoji = emojiId);
81
-
82
- _onLastUsedYatAddressSubscription =
83
- _wallet.walletInfo.yatLastUsedAddressStream.listen((String yatAddress) {
84
- if (yatAddress == _wallet.walletAddresses.address) {
85
- emoji = yatStore.emoji;
86
- } else {
87
- emoji = '';
88
- }
89
- });
80
+ //reaction((_) => yatStore.emoji, (String emojiId) => this.emoji = emojiId);
81
+
82
+ //_onLastUsedYatAddressSubscription =
83
+ // _wallet.walletInfo.yatLastUsedAddressStream.listen((String yatAddress) {
84
+ // if (yatAddress == _wallet.walletAddresses.address) {
85
+ // emoji = yatStore.emoji;
86
+ // } else {
87
+ // emoji = '';
88
+ // }
89
+ //});
90
91
if (_wallet.walletAddresses.address == _wallet.walletInfo.yatLastUsedAddress) {
92
emoji = yatStore.emoji;
@@ -223,4 +223,15 @@ abstract class WalletAddressListViewModelBase with Store {
223
wallet.save();
224
}
225
}
226
+
227
+ @action
228
+ void generateRandomAddress() {
229
+ final wallet = _wallet;
230
+
231
+ if (wallet.type == WalletType.bitcoin
232
+ || wallet.type == WalletType.litecoin) {
233
+ bitcoin.randomAddress(wallet);
234
+ wallet.save();
235
+ }
236
+ }
237
}