Changed way for generation of change address for electrum wallets. Remove some yats call.

This commit is contained in:
M 2022-01-12 15:20:43 +02:00
parent 862401147b
commit cfb82dd02d
8 changed files with 93 additions and 54 deletions

View file

@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:hive/hive.dart';
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
@ -238,7 +239,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
}
final txb = bitcoin.TransactionBuilder(network: networkType);
final changeAddress = walletAddresses.address;
final changeAddress = getChangeAddress();
var leftAmount = totalAmount;
var totalInputAmount = 0;
@ -545,4 +546,18 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
balance = await _fetchBalances();
await save();
}
String getChangeAddress() {
const minCountOfHiddenAddresses = 5;
final random = Random();
var addresses = walletAddresses.addresses
.where((addr) => addr.isHidden)
.toList();
if (addresses.length < minCountOfHiddenAddresses) {
addresses = walletAddresses.addresses.toList();
}
return addresses[random.nextInt(addresses.length)].address;
}
}

View file

@ -4,6 +4,7 @@ import 'package:cw_core/wallet_addresses.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:flutter/foundation.dart';
import 'package:mobx/mobx.dart';
import 'dart:math';
part 'electrum_wallet_addresses.g.dart';
@ -129,4 +130,9 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
print(e.toString());
}
}
void randomizeAddress() {
final random = Random();
address = addresses[random.nextInt(addresses.length)].address;
}
}

View file

@ -196,4 +196,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: bc2591d23316907c9c90ca1cd2fce063fd866508
COCOAPODS: 1.10.2
COCOAPODS: 1.11.2

View file

@ -57,6 +57,12 @@ class CWBitcoin extends Bitcoin {
bitcoinWallet.walletAddresses.nextAddress();
}
@override
Future<void> randomAddress(Object wallet) {
final bitcoinWallet = wallet as BitcoinWallet;
bitcoinWallet.walletAddresses.randomizeAddress();
}
@override
Object createBitcoinTransactionCredentials(List<Output> outputs, TransactionPriority priority)
=> BitcoinTransactionCredentials(

View file

@ -197,7 +197,7 @@ class AppState extends State<App> with SingleTickerProviderStateMixin {
void initState() {
super.initState();
_handleIncomingLinks();
_handleInitialUri();
//_handleInitialUri();
}
@override
@ -235,18 +235,18 @@ class AppState extends State<App> with SingleTickerProviderStateMixin {
}
void _fetchEmojiFromUri(Uri uri) {
final queryParameters = uri.queryParameters;
if (queryParameters?.isEmpty ?? true) {
return;
}
final emoji = queryParameters['eid'];
final refreshToken = queryParameters['refresh_token'];
if ((emoji?.isEmpty ?? true)||(refreshToken?.isEmpty ?? true)) {
return;
}
yatStore.emoji = emoji;
yatStore.refreshToken = refreshToken;
yatStore.emojiIncommingSC.add(emoji);
//final queryParameters = uri.queryParameters;
//if (queryParameters?.isEmpty ?? true) {
// return;
//}
//final emoji = queryParameters['eid'];
//final refreshToken = queryParameters['refresh_token'];
//if ((emoji?.isEmpty ?? true)||(refreshToken?.isEmpty ?? true)) {
// return;
//}
//yatStore.emoji = emoji;
//yatStore.refreshToken = refreshToken;
//yatStore.emojiIncommingSC.add(emoji);
}
@override

View file

@ -17,34 +17,34 @@ import 'package:cake_wallet/store/yat/yat_store.dart';
ReactionDisposer _onCurrentWalletChangeReaction;
ReactionDisposer _onCurrentWalletChangeFiatRateUpdateReaction;
ReactionDisposer _onCurrentWalletAddressChangeReaction;
//ReactionDisposer _onCurrentWalletAddressChangeReaction;
void startCurrentWalletChangeReaction(AppStore appStore,
SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
_onCurrentWalletChangeReaction?.reaction?.dispose();
_onCurrentWalletChangeFiatRateUpdateReaction?.reaction?.dispose();
_onCurrentWalletAddressChangeReaction?.reaction?.dispose();
//_onCurrentWalletAddressChangeReaction?.reaction?.dispose();
_onCurrentWalletAddressChangeReaction = reaction((_) => appStore.wallet.walletAddresses.address,
(String address) async {
if (address == appStore.wallet.walletInfo.yatLastUsedAddress) {
return;
}
//_onCurrentWalletAddressChangeReaction = reaction((_) => appStore.wallet.walletAddresses.address,
//(String address) async {
//if (address == appStore.wallet.walletInfo.yatLastUsedAddress) {
// return;
//}
try {
final yatStore = getIt.get<YatStore>();
await updateEmojiIdAddress(
appStore.wallet.walletInfo.yatEmojiId,
appStore.wallet.walletAddresses.address,
yatStore.apiKey,
appStore.wallet.type
);
appStore.wallet.walletInfo.yatLastUsedAddress = address;
await appStore.wallet.walletInfo.save();
} catch (e) {
print(e.toString());
}
});
//try {
// final yatStore = getIt.get<YatStore>();
// await updateEmojiIdAddress(
// appStore.wallet.walletInfo.yatEmojiId,
// appStore.wallet.walletAddresses.address,
// yatStore.apiKey,
// appStore.wallet.type
// );
// appStore.wallet.walletInfo.yatLastUsedAddress = address;
// await appStore.wallet.walletInfo.save();
//} catch (e) {
// print(e.toString());
//}
//});
_onCurrentWalletChangeReaction = reaction((_) => appStore.wallet, (WalletBase<
Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>

View file

@ -27,6 +27,7 @@ class AddressPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
addressListViewModel.generateRandomAddress();
autorun((_) async {
if (!walletViewModel.isOutdatedElectrumWallet
|| !walletViewModel.settingsStore.shouldShowReceiveWarning) {
@ -124,15 +125,15 @@ class AddressPage extends StatelessWidget {
],
),
),
)
: PrimaryButton(
onPressed: () => addressListViewModel.nextAddress(),
text: 'Next address',
color: Theme.of(context).buttonColor,
textColor: Theme.of(context)
.accentTextTheme
.display3
.backgroundColor);
) : Container();
//: PrimaryButton(
// onPressed: () => addressListViewModel.nextAddress(),
// text: 'Next address',
// color: Theme.of(context).buttonColor,
// textColor: Theme.of(context)
// .accentTextTheme
// .display3
// .backgroundColor);
})
],
),

View file

@ -77,16 +77,16 @@ abstract class WalletAddressListViewModelBase with Store {
}
});
reaction((_) => yatStore.emoji, (String emojiId) => this.emoji = emojiId);
//reaction((_) => yatStore.emoji, (String emojiId) => this.emoji = emojiId);
_onLastUsedYatAddressSubscription =
_wallet.walletInfo.yatLastUsedAddressStream.listen((String yatAddress) {
if (yatAddress == _wallet.walletAddresses.address) {
emoji = yatStore.emoji;
} else {
emoji = '';
}
});
//_onLastUsedYatAddressSubscription =
// _wallet.walletInfo.yatLastUsedAddressStream.listen((String yatAddress) {
// if (yatAddress == _wallet.walletAddresses.address) {
// emoji = yatStore.emoji;
// } else {
// emoji = '';
// }
//});
if (_wallet.walletAddresses.address == _wallet.walletInfo.yatLastUsedAddress) {
emoji = yatStore.emoji;
@ -223,4 +223,15 @@ abstract class WalletAddressListViewModelBase with Store {
wallet.save();
}
}
@action
void generateRandomAddress() {
final wallet = _wallet;
if (wallet.type == WalletType.bitcoin
|| wallet.type == WalletType.litecoin) {
bitcoin.randomAddress(wallet);
wallet.save();
}
}
}